Skip to content

Commit 77b40d6

Browse files
authored
Merge pull request #15927 from truemedian/http-bugs
std.http: fix infinite read loop, deduplicate connection code, add TlsAlert errors
2 parents 9461ed5 + 23ccff9 commit 77b40d6

File tree

6 files changed

+331
-374
lines changed

6 files changed

+331
-374
lines changed

lib/std/crypto/tls.zig

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,35 @@ pub const AlertLevel = enum(u8) {
138138
};
139139

140140
pub const AlertDescription = enum(u8) {
141+
pub const Error = error{
142+
TlsAlertUnexpectedMessage,
143+
TlsAlertBadRecordMac,
144+
TlsAlertRecordOverflow,
145+
TlsAlertHandshakeFailure,
146+
TlsAlertBadCertificate,
147+
TlsAlertUnsupportedCertificate,
148+
TlsAlertCertificateRevoked,
149+
TlsAlertCertificateExpired,
150+
TlsAlertCertificateUnknown,
151+
TlsAlertIllegalParameter,
152+
TlsAlertUnknownCa,
153+
TlsAlertAccessDenied,
154+
TlsAlertDecodeError,
155+
TlsAlertDecryptError,
156+
TlsAlertProtocolVersion,
157+
TlsAlertInsufficientSecurity,
158+
TlsAlertInternalError,
159+
TlsAlertInappropriateFallback,
160+
TlsAlertMissingExtension,
161+
TlsAlertUnsupportedExtension,
162+
TlsAlertUnrecognizedName,
163+
TlsAlertBadCertificateStatusResponse,
164+
TlsAlertUnknownPskIdentity,
165+
TlsAlertCertificateRequired,
166+
TlsAlertNoApplicationProtocol,
167+
TlsAlertUnknown,
168+
};
169+
141170
close_notify = 0,
142171
unexpected_message = 10,
143172
bad_record_mac = 20,
@@ -166,6 +195,39 @@ pub const AlertDescription = enum(u8) {
166195
certificate_required = 116,
167196
no_application_protocol = 120,
168197
_,
198+
199+
pub fn toError(alert: AlertDescription) Error!void {
200+
return switch (alert) {
201+
.close_notify => {}, // not an error
202+
.unexpected_message => error.TlsAlertUnexpectedMessage,
203+
.bad_record_mac => error.TlsAlertBadRecordMac,
204+
.record_overflow => error.TlsAlertRecordOverflow,
205+
.handshake_failure => error.TlsAlertHandshakeFailure,
206+
.bad_certificate => error.TlsAlertBadCertificate,
207+
.unsupported_certificate => error.TlsAlertUnsupportedCertificate,
208+
.certificate_revoked => error.TlsAlertCertificateRevoked,
209+
.certificate_expired => error.TlsAlertCertificateExpired,
210+
.certificate_unknown => error.TlsAlertCertificateUnknown,
211+
.illegal_parameter => error.TlsAlertIllegalParameter,
212+
.unknown_ca => error.TlsAlertUnknownCa,
213+
.access_denied => error.TlsAlertAccessDenied,
214+
.decode_error => error.TlsAlertDecodeError,
215+
.decrypt_error => error.TlsAlertDecryptError,
216+
.protocol_version => error.TlsAlertProtocolVersion,
217+
.insufficient_security => error.TlsAlertInsufficientSecurity,
218+
.internal_error => error.TlsAlertInternalError,
219+
.inappropriate_fallback => error.TlsAlertInappropriateFallback,
220+
.user_canceled => {}, // not an error
221+
.missing_extension => error.TlsAlertMissingExtension,
222+
.unsupported_extension => error.TlsAlertUnsupportedExtension,
223+
.unrecognized_name => error.TlsAlertUnrecognizedName,
224+
.bad_certificate_status_response => error.TlsAlertBadCertificateStatusResponse,
225+
.unknown_psk_identity => error.TlsAlertUnknownPskIdentity,
226+
.certificate_required => error.TlsAlertCertificateRequired,
227+
.no_application_protocol => error.TlsAlertNoApplicationProtocol,
228+
_ => error.TlsAlertUnknown,
229+
};
230+
}
169231
};
170232

171233
pub const SignatureScheme = enum(u16) {

lib/std/crypto/tls/Client.zig

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,11 @@ pub const StreamInterface = struct {
8989
};
9090

9191
pub fn InitError(comptime Stream: type) type {
92-
return std.mem.Allocator.Error || Stream.WriteError || Stream.ReadError || error{
92+
return std.mem.Allocator.Error || Stream.WriteError || Stream.ReadError || tls.AlertDescription.Error || error{
9393
InsufficientEntropy,
9494
DiskQuota,
9595
LockViolation,
9696
NotOpenForWriting,
97-
TlsAlert,
9897
TlsUnexpectedMessage,
9998
TlsIllegalParameter,
10099
TlsDecryptFailure,
@@ -251,8 +250,11 @@ pub fn init(stream: anytype, ca_bundle: Certificate.Bundle, host: []const u8) In
251250
const level = ptd.decode(tls.AlertLevel);
252251
const desc = ptd.decode(tls.AlertDescription);
253252
_ = level;
254-
_ = desc;
255-
return error.TlsAlert;
253+
254+
// if this isn't a error alert, then it's a closure alert, which makes no sense in a handshake
255+
try desc.toError();
256+
// TODO: handle server-side closures
257+
return error.TlsUnexpectedMessage;
256258
},
257259
.handshake => {
258260
try ptd.ensure(4);
@@ -1071,8 +1073,10 @@ pub fn readvAdvanced(c: *Client, stream: anytype, iovecs: []const std.os.iovec)
10711073
const level = @intToEnum(tls.AlertLevel, frag[in]);
10721074
const desc = @intToEnum(tls.AlertDescription, frag[in + 1]);
10731075
_ = level;
1074-
_ = desc;
1075-
return error.TlsAlert;
1076+
1077+
try desc.toError();
1078+
// TODO: handle server-side closures
1079+
return error.TlsUnexpectedMessage;
10761080
},
10771081
.application_data => {
10781082
const cleartext = switch (c.application_cipher) {
@@ -1112,7 +1116,10 @@ pub fn readvAdvanced(c: *Client, stream: anytype, iovecs: []const std.os.iovec)
11121116
return vp.total;
11131117
}
11141118
_ = level;
1115-
return error.TlsAlert;
1119+
1120+
try desc.toError();
1121+
// TODO: handle server-side closures
1122+
return error.TlsUnexpectedMessage;
11161123
},
11171124
.handshake => {
11181125
var ct_i: usize = 0;

0 commit comments

Comments
 (0)