authorgravatar for truemedian@gmail.comNameless <truemedian@gmail.com> 2023-03-06 20:11:56-06:00
committergravatar for truemedian@gmail.comNameless <truemedian@gmail.com> 2023-03-09 14:54:26-06:00
log8d86194b6e31788263d2cbdd03e2a8cde4134c37
tree87c0066328b68b1bf597c95063d0dda544cc2dd9
parentafb26f4e6b39431001eff75cc8ce19144cb5301a
signaturelock-open Commit is signed but in an unrecognized format.

add error sets to tcpConnect* and tls.Client.init


2 files changed, 80 insertions(+), 4 deletions(-)

lib/std/crypto/tls/Client.zig+49-1
......@@ -88,11 +88,59 @@ pub const StreamInterface = struct {
8888 }
8989};
9090
91pub fn InitError(comptime Stream: type) type {
92 return std.mem.Allocator.Error || Stream.WriteError || Stream.ReadError || error {
93 InsufficientEntropy,
94 DiskQuota,
95 LockViolation,
96 NotOpenForWriting,
97 TlsAlert,
98 TlsUnexpectedMessage,
99 TlsIllegalParameter,
100 TlsDecryptFailure,
101 TlsRecordOverflow,
102 TlsBadRecordMac,
103 CertificateFieldHasInvalidLength,
104 CertificateHostMismatch,
105 CertificatePublicKeyInvalid,
106 CertificateExpired,
107 CertificateFieldHasWrongDataType,
108 CertificateIssuerMismatch,
109 CertificateNotYetValid,
110 CertificateSignatureAlgorithmMismatch,
111 CertificateSignatureAlgorithmUnsupported,
112 CertificateSignatureInvalid,
113 CertificateSignatureInvalidLength,
114 CertificateSignatureNamedCurveUnsupported,
115 CertificateSignatureUnsupportedBitCount,
116 TlsCertificateNotVerified,
117 TlsBadSignatureScheme,
118 TlsBadRsaSignatureBitCount,
119 InvalidEncoding,
120 IdentityElement,
121 SignatureVerificationFailed,
122 TlsDecryptError,
123 TlsConnectionTruncated,
124 TlsDecodeError,
125 UnsupportedCertificateVersion,
126 CertificateTimeInvalid,
127 CertificateHasUnrecognizedObjectId,
128 CertificateHasInvalidBitString,
129 MessageTooLong,
130 NegativeIntoUnsigned,
131 TargetTooSmall,
132 BufferTooSmall,
133 InvalidSignature,
134 NotSquare,
135 NonCanonical,
136 };
137}
138
91139/// Initiates a TLS handshake and establishes a TLSv1.3 session with `stream`, which
92140/// must conform to `StreamInterface`.
93141///
94142/// `host` is only borrowed during this function call.
95pub fn init(stream: anytype, ca_bundle: Certificate.Bundle, host: []const u8) !Client {
143pub fn init(stream: anytype, ca_bundle: Certificate.Bundle, host: []const u8) InitError(@TypeOf(stream))!Client {
96144 const host_len = @intCast(u16, host.len);
97145
98146 var random_buffer: [128]u8 = undefined;
lib/std/net.zig+31-3
......@@ -702,8 +702,10 @@ pub const AddressList = struct {
702702 }
703703};
704704
705pub const TcpConnectToHostError = GetAddressListError || TcpConnectToAddressError;
706
705707/// All memory allocated with `allocator` will be freed before this function returns.
706pub fn tcpConnectToHost(allocator: mem.Allocator, name: []const u8, port: u16) !Stream {
708pub fn tcpConnectToHost(allocator: mem.Allocator, name: []const u8, port: u16) TcpConnectToHostError!Stream {
707709 const list = try getAddressList(allocator, name, port);
708710 defer list.deinit();
709711
......@@ -720,7 +722,9 @@ pub fn tcpConnectToHost(allocator: mem.Allocator, name: []const u8, port: u16) !
720722 return std.os.ConnectError.ConnectionRefused;
721723}
722724
723pub fn tcpConnectToAddress(address: Address) !Stream {
725pub const TcpConnectToAddressError = std.os.SocketError || std.os.ConnectError;
726
727pub fn tcpConnectToAddress(address: Address) TcpConnectToAddressError!Stream {
724728 const nonblock = if (std.io.is_async) os.SOCK.NONBLOCK else 0;
725729 const sock_flags = os.SOCK.STREAM | nonblock |
726730 (if (builtin.target.os.tag == .windows) 0 else os.SOCK.CLOEXEC);
......@@ -737,8 +741,32 @@ pub fn tcpConnectToAddress(address: Address) !Stream {
737741 return Stream{ .handle = sockfd };
738742}
739743
744const GetAddressListError = std.mem.Allocator.Error || std.fs.File.OpenError || std.fs.File.ReadError || std.os.SocketError || std.os.BindError || error {
745 // TODO: break this up into error sets from the various underlying functions
746
747 TemporaryNameServerFailure,
748 NameServerFailure,
749 AddressFamilyNotSupported,
750 UnknownHostName,
751 ServiceUnavailable,
752 Unexpected,
753
754 HostLacksNetworkAddresses,
755
756 InvalidCharacter,
757 InvalidEnd,
758 NonCanonical,
759 Overflow,
760 Incomplete,
761 InvalidIpv4Mapping,
762 InvalidIPAddressFormat,
763
764 InterfaceNotFound,
765 FileSystem,
766};
767
740768/// Call `AddressList.deinit` on the result.
741pub fn getAddressList(allocator: mem.Allocator, name: []const u8, port: u16) !*AddressList {
769pub fn getAddressList(allocator: mem.Allocator, name: []const u8, port: u16) GetAddressListError!*AddressList {
742770 const result = blk: {
743771 var arena = std.heap.ArenaAllocator.init(allocator);
744772 errdefer arena.deinit();