@@ -49,8 +49,8 @@ pub const hello_retry_request_sequence = [32]u8{
49
49
};
50
50
51
51
pub const close_notify_alert = [_ ]u8 {
52
- @intFromEnum (AlertLevel .warning ),
53
- @intFromEnum (AlertDescription .close_notify ),
52
+ @intFromEnum (Alert . Level .warning ),
53
+ @intFromEnum (Alert . Description .close_notify ),
54
54
};
55
55
56
56
pub const ProtocolVersion = enum (u16 ) {
@@ -138,103 +138,108 @@ pub const ExtensionType = enum(u16) {
138
138
_ ,
139
139
};
140
140
141
- pub const AlertLevel = enum (u8 ) {
142
- warning = 1 ,
143
- fatal = 2 ,
144
- _ ,
145
- };
141
+ pub const Alert = struct {
142
+ level : Level ,
143
+ description : Description ,
146
144
147
- pub const AlertDescription = enum (u8 ) {
148
- pub const Error = error {
149
- TlsAlertUnexpectedMessage ,
150
- TlsAlertBadRecordMac ,
151
- TlsAlertRecordOverflow ,
152
- TlsAlertHandshakeFailure ,
153
- TlsAlertBadCertificate ,
154
- TlsAlertUnsupportedCertificate ,
155
- TlsAlertCertificateRevoked ,
156
- TlsAlertCertificateExpired ,
157
- TlsAlertCertificateUnknown ,
158
- TlsAlertIllegalParameter ,
159
- TlsAlertUnknownCa ,
160
- TlsAlertAccessDenied ,
161
- TlsAlertDecodeError ,
162
- TlsAlertDecryptError ,
163
- TlsAlertProtocolVersion ,
164
- TlsAlertInsufficientSecurity ,
165
- TlsAlertInternalError ,
166
- TlsAlertInappropriateFallback ,
167
- TlsAlertMissingExtension ,
168
- TlsAlertUnsupportedExtension ,
169
- TlsAlertUnrecognizedName ,
170
- TlsAlertBadCertificateStatusResponse ,
171
- TlsAlertUnknownPskIdentity ,
172
- TlsAlertCertificateRequired ,
173
- TlsAlertNoApplicationProtocol ,
174
- TlsAlertUnknown ,
145
+ pub const Level = enum (u8 ) {
146
+ warning = 1 ,
147
+ fatal = 2 ,
148
+ _ ,
175
149
};
176
150
177
- close_notify = 0 ,
178
- unexpected_message = 10 ,
179
- bad_record_mac = 20 ,
180
- record_overflow = 22 ,
181
- handshake_failure = 40 ,
182
- bad_certificate = 42 ,
183
- unsupported_certificate = 43 ,
184
- certificate_revoked = 44 ,
185
- certificate_expired = 45 ,
186
- certificate_unknown = 46 ,
187
- illegal_parameter = 47 ,
188
- unknown_ca = 48 ,
189
- access_denied = 49 ,
190
- decode_error = 50 ,
191
- decrypt_error = 51 ,
192
- protocol_version = 70 ,
193
- insufficient_security = 71 ,
194
- internal_error = 80 ,
195
- inappropriate_fallback = 86 ,
196
- user_canceled = 90 ,
197
- missing_extension = 109 ,
198
- unsupported_extension = 110 ,
199
- unrecognized_name = 112 ,
200
- bad_certificate_status_response = 113 ,
201
- unknown_psk_identity = 115 ,
202
- certificate_required = 116 ,
203
- no_application_protocol = 120 ,
204
- _ ,
151
+ pub const Description = enum (u8 ) {
152
+ pub const Error = error {
153
+ TlsAlertUnexpectedMessage ,
154
+ TlsAlertBadRecordMac ,
155
+ TlsAlertRecordOverflow ,
156
+ TlsAlertHandshakeFailure ,
157
+ TlsAlertBadCertificate ,
158
+ TlsAlertUnsupportedCertificate ,
159
+ TlsAlertCertificateRevoked ,
160
+ TlsAlertCertificateExpired ,
161
+ TlsAlertCertificateUnknown ,
162
+ TlsAlertIllegalParameter ,
163
+ TlsAlertUnknownCa ,
164
+ TlsAlertAccessDenied ,
165
+ TlsAlertDecodeError ,
166
+ TlsAlertDecryptError ,
167
+ TlsAlertProtocolVersion ,
168
+ TlsAlertInsufficientSecurity ,
169
+ TlsAlertInternalError ,
170
+ TlsAlertInappropriateFallback ,
171
+ TlsAlertMissingExtension ,
172
+ TlsAlertUnsupportedExtension ,
173
+ TlsAlertUnrecognizedName ,
174
+ TlsAlertBadCertificateStatusResponse ,
175
+ TlsAlertUnknownPskIdentity ,
176
+ TlsAlertCertificateRequired ,
177
+ TlsAlertNoApplicationProtocol ,
178
+ TlsAlertUnknown ,
179
+ };
205
180
206
- pub fn toError (alert : AlertDescription ) Error ! void {
207
- switch (alert ) {
208
- .close_notify = > {}, // not an error
209
- .unexpected_message = > return error .TlsAlertUnexpectedMessage ,
210
- .bad_record_mac = > return error .TlsAlertBadRecordMac ,
211
- .record_overflow = > return error .TlsAlertRecordOverflow ,
212
- .handshake_failure = > return error .TlsAlertHandshakeFailure ,
213
- .bad_certificate = > return error .TlsAlertBadCertificate ,
214
- .unsupported_certificate = > return error .TlsAlertUnsupportedCertificate ,
215
- .certificate_revoked = > return error .TlsAlertCertificateRevoked ,
216
- .certificate_expired = > return error .TlsAlertCertificateExpired ,
217
- .certificate_unknown = > return error .TlsAlertCertificateUnknown ,
218
- .illegal_parameter = > return error .TlsAlertIllegalParameter ,
219
- .unknown_ca = > return error .TlsAlertUnknownCa ,
220
- .access_denied = > return error .TlsAlertAccessDenied ,
221
- .decode_error = > return error .TlsAlertDecodeError ,
222
- .decrypt_error = > return error .TlsAlertDecryptError ,
223
- .protocol_version = > return error .TlsAlertProtocolVersion ,
224
- .insufficient_security = > return error .TlsAlertInsufficientSecurity ,
225
- .internal_error = > return error .TlsAlertInternalError ,
226
- .inappropriate_fallback = > return error .TlsAlertInappropriateFallback ,
227
- .user_canceled = > {}, // not an error
228
- .missing_extension = > return error .TlsAlertMissingExtension ,
229
- .unsupported_extension = > return error .TlsAlertUnsupportedExtension ,
230
- .unrecognized_name = > return error .TlsAlertUnrecognizedName ,
231
- .bad_certificate_status_response = > return error .TlsAlertBadCertificateStatusResponse ,
232
- .unknown_psk_identity = > return error .TlsAlertUnknownPskIdentity ,
233
- .certificate_required = > return error .TlsAlertCertificateRequired ,
234
- .no_application_protocol = > return error .TlsAlertNoApplicationProtocol ,
235
- _ = > return error .TlsAlertUnknown ,
181
+ close_notify = 0 ,
182
+ unexpected_message = 10 ,
183
+ bad_record_mac = 20 ,
184
+ record_overflow = 22 ,
185
+ handshake_failure = 40 ,
186
+ bad_certificate = 42 ,
187
+ unsupported_certificate = 43 ,
188
+ certificate_revoked = 44 ,
189
+ certificate_expired = 45 ,
190
+ certificate_unknown = 46 ,
191
+ illegal_parameter = 47 ,
192
+ unknown_ca = 48 ,
193
+ access_denied = 49 ,
194
+ decode_error = 50 ,
195
+ decrypt_error = 51 ,
196
+ protocol_version = 70 ,
197
+ insufficient_security = 71 ,
198
+ internal_error = 80 ,
199
+ inappropriate_fallback = 86 ,
200
+ user_canceled = 90 ,
201
+ missing_extension = 109 ,
202
+ unsupported_extension = 110 ,
203
+ unrecognized_name = 112 ,
204
+ bad_certificate_status_response = 113 ,
205
+ unknown_psk_identity = 115 ,
206
+ certificate_required = 116 ,
207
+ no_application_protocol = 120 ,
208
+ _ ,
209
+
210
+ pub fn toError (description : Description ) Error ! void {
211
+ switch (description ) {
212
+ .close_notify = > {}, // not an error
213
+ .unexpected_message = > return error .TlsAlertUnexpectedMessage ,
214
+ .bad_record_mac = > return error .TlsAlertBadRecordMac ,
215
+ .record_overflow = > return error .TlsAlertRecordOverflow ,
216
+ .handshake_failure = > return error .TlsAlertHandshakeFailure ,
217
+ .bad_certificate = > return error .TlsAlertBadCertificate ,
218
+ .unsupported_certificate = > return error .TlsAlertUnsupportedCertificate ,
219
+ .certificate_revoked = > return error .TlsAlertCertificateRevoked ,
220
+ .certificate_expired = > return error .TlsAlertCertificateExpired ,
221
+ .certificate_unknown = > return error .TlsAlertCertificateUnknown ,
222
+ .illegal_parameter = > return error .TlsAlertIllegalParameter ,
223
+ .unknown_ca = > return error .TlsAlertUnknownCa ,
224
+ .access_denied = > return error .TlsAlertAccessDenied ,
225
+ .decode_error = > return error .TlsAlertDecodeError ,
226
+ .decrypt_error = > return error .TlsAlertDecryptError ,
227
+ .protocol_version = > return error .TlsAlertProtocolVersion ,
228
+ .insufficient_security = > return error .TlsAlertInsufficientSecurity ,
229
+ .internal_error = > return error .TlsAlertInternalError ,
230
+ .inappropriate_fallback = > return error .TlsAlertInappropriateFallback ,
231
+ .user_canceled = > {}, // not an error
232
+ .missing_extension = > return error .TlsAlertMissingExtension ,
233
+ .unsupported_extension = > return error .TlsAlertUnsupportedExtension ,
234
+ .unrecognized_name = > return error .TlsAlertUnrecognizedName ,
235
+ .bad_certificate_status_response = > return error .TlsAlertBadCertificateStatusResponse ,
236
+ .unknown_psk_identity = > return error .TlsAlertUnknownPskIdentity ,
237
+ .certificate_required = > return error .TlsAlertCertificateRequired ,
238
+ .no_application_protocol = > return error .TlsAlertNoApplicationProtocol ,
239
+ _ = > return error .TlsAlertUnknown ,
240
+ }
236
241
}
237
- }
242
+ };
238
243
};
239
244
240
245
pub const SignatureScheme = enum (u16 ) {
@@ -650,22 +655,24 @@ pub const Decoder = struct {
650
655
}
651
656
652
657
/// Use this function to increase `their_end`.
653
- pub fn readAtLeast (d : * Decoder , stream : anytype , their_amt : usize ) ! void {
658
+ pub fn readAtLeast (d : * Decoder , stream : * std.io.Reader , their_amt : usize ) ! void {
654
659
assert (! d .disable_reads );
655
660
const existing_amt = d .cap - d .idx ;
656
661
d .their_end = d .idx + their_amt ;
657
662
if (their_amt <= existing_amt ) return ;
658
663
const request_amt = their_amt - existing_amt ;
659
664
const dest = d .buf [d .cap .. ];
660
665
if (request_amt > dest .len ) return error .TlsRecordOverflow ;
661
- const actual_amt = try stream .readAtLeast (dest , request_amt );
662
- if (actual_amt < request_amt ) return error .TlsConnectionTruncated ;
663
- d .cap += actual_amt ;
666
+ stream .readSlice (dest [0.. request_amt ]) catch | err | switch (err ) {
667
+ error .EndOfStream = > return error .TlsConnectionTruncated ,
668
+ error .ReadFailed = > return error .ReadFailed ,
669
+ };
670
+ d .cap += request_amt ;
664
671
}
665
672
666
673
/// Same as `readAtLeast` but also increases `our_end` by exactly `our_amt`.
667
674
/// Use when `our_amt` is calculated by us, not by them.
668
- pub fn readAtLeastOurAmt (d : * Decoder , stream : anytype , our_amt : usize ) ! void {
675
+ pub fn readAtLeastOurAmt (d : * Decoder , stream : * std.io.Reader , our_amt : usize ) ! void {
669
676
assert (! d .disable_reads );
670
677
try readAtLeast (d , stream , our_amt );
671
678
d .our_end = d .idx + our_amt ;
0 commit comments