authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-11-05 02:24:14-05:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-11-07 20:25:26-05:00
loga6ede7ba86987b9ae2bb6b8aac60f66af56e7b08
treededfcb6fedf772ec5e8d78f10ca413ea2d68a88e
parentde53e6e4f2dc7a41dc50b309fee87e06475e4838

std.crypto.tls: support handshake fragments


1 files changed, 32 insertions(+), 22 deletions(-)

lib/std/crypto/tls/Client.zig+32-22
......@@ -274,13 +274,14 @@ pub fn init(stream: anytype, options: Options) InitError(@TypeOf(stream))!Client
274274 }
275275
276276 var tls_version: tls.ProtocolVersion = undefined;
277 // This is used for two purposes:
277 // These are used for two purposes:
278278 // * Detect whether a certificate is the first one presented, in which case
279279 // we need to verify the host name.
280 var cert_index: usize = 0;
280281 // * Flip back and forth between the two cleartext buffers in order to keep
281282 // the previous certificate in memory so that it can be verified by the
282283 // next one.
283 var cert_index: usize = 0;
284 var cert_buf_index: usize = 0;
284285 var write_seq: u64 = 0;
285286 var read_seq: u64 = 0;
286287 var prev_cert: Certificate.Parsed = undefined;
......@@ -315,10 +316,12 @@ pub fn init(stream: anytype, options: Options) InitError(@TypeOf(stream))!Client
315316 var main_cert_pub_key: CertificatePublicKey = undefined;
316317 const now_sec = std.time.timestamp();
317318
319 var cleartext_fragment_start: usize = 0;
320 var cleartext_fragment_end: usize = 0;
318321 var cleartext_bufs: [2][tls.max_ciphertext_inner_record_len]u8 = undefined;
319322 var handshake_buffer: [tls.max_ciphertext_record_len]u8 = undefined;
320323 var d: tls.Decoder = .{ .buf = &handshake_buffer };
321 while (true) {
324 fragment: while (true) {
322325 try d.readAtLeastOurAmt(stream, tls.record_header_len);
323326 const record_header = d.buf[d.idx..][0..tls.record_header_len];
324327 const record_ct = d.decode(tls.ContentType);
......@@ -332,15 +335,16 @@ pub fn init(stream: anytype, options: Options) InitError(@TypeOf(stream))!Client
332335 std.debug.assert(tls_version == .tls_1_3);
333336 if (record_ct != .application_data) return error.TlsUnexpectedMessage;
334337 try record_decoder.ensure(record_len);
335 const cleartext_buf = &cleartext_bufs[cert_index % 2];
336 const cleartext = cleartext: switch (handshake_cipher) {
338 const cleartext_buf = &cleartext_bufs[cert_buf_index % 2];
339 switch (handshake_cipher) {
337340 inline else => |*p| {
338341 const pv = &p.version.tls_1_3;
339342 const P = @TypeOf(p.*).A;
340343 if (record_len < P.AEAD.tag_length) return error.TlsRecordOverflow;
341344 const ciphertext = record_decoder.slice(record_len - P.AEAD.tag_length);
342 if (ciphertext.len > cleartext_buf.len) return error.TlsRecordOverflow;
343 const cleartext = cleartext_buf[0..ciphertext.len];
345 const cleartext_fragment_buf = cleartext_buf[cleartext_fragment_end..];
346 if (ciphertext.len > cleartext_fragment_buf.len) return error.TlsRecordOverflow;
347 const cleartext = cleartext_fragment_buf[0..ciphertext.len];
344348 const auth_tag = record_decoder.array(P.AEAD.tag_length).*;
345349 const nonce = if (builtin.zig_backend == .stage2_x86_64 and
346350 P.AEAD.nonce_length > comptime std.simd.suggestVectorLength(u8) orelse 1)
......@@ -357,27 +361,29 @@ pub fn init(stream: anytype, options: Options) InitError(@TypeOf(stream))!Client
357361 };
358362 P.AEAD.decrypt(cleartext, ciphertext, auth_tag, record_header, nonce, pv.server_handshake_key) catch
359363 return error.TlsBadRecordMac;
360 break :cleartext mem.trimRight(u8, cleartext, "\x00");
364 cleartext_fragment_end += std.mem.trimRight(u8, cleartext, "\x00").len;
361365 },
362 };
366 }
363367 read_seq += 1;
364 const ct: tls.ContentType = @enumFromInt(cleartext[cleartext.len - 1]);
368 cleartext_fragment_end -= 1;
369 const ct: tls.ContentType = @enumFromInt(cleartext_buf[cleartext_fragment_end]);
365370 if (ct != .handshake) return error.TlsUnexpectedMessage;
366 break :content .{ tls.Decoder.fromTheirSlice(@constCast(cleartext[0 .. cleartext.len - 1])), ct };
371 break :content .{ tls.Decoder.fromTheirSlice(@constCast(cleartext_buf[cleartext_fragment_start..cleartext_fragment_end])), ct };
367372 },
368373 .application => {
369374 std.debug.assert(tls_version == .tls_1_2);
370375 if (record_ct != .handshake) return error.TlsUnexpectedMessage;
371376 try record_decoder.ensure(record_len);
372 const cleartext_buf = &cleartext_bufs[cert_index % 2];
373 const cleartext = cleartext: switch (handshake_cipher) {
377 const cleartext_buf = &cleartext_bufs[cert_buf_index % 2];
378 switch (handshake_cipher) {
374379 inline else => |*p| {
375380 const pv = &p.version.tls_1_2;
376381 const P = @TypeOf(p.*).A;
377382 if (record_len < P.record_iv_length + P.mac_length) return error.TlsRecordOverflow;
378383 const message_len: u16 = record_len - P.record_iv_length - P.mac_length;
379 if (message_len > cleartext_buf.len) return error.TlsRecordOverflow;
380 const cleartext = cleartext_buf[0..message_len];
384 const cleartext_fragment_buf = cleartext_buf[cleartext_fragment_end..];
385 if (message_len > cleartext_fragment_buf.len) return error.TlsRecordOverflow;
386 const cleartext = cleartext_fragment_buf[0..message_len];
381387 const ad = std.mem.toBytes(big(read_seq)) ++
382388 record_header[0 .. 1 + 2] ++
383389 std.mem.toBytes(big(message_len));
......@@ -400,16 +406,16 @@ pub fn init(stream: anytype, options: Options) InitError(@TypeOf(stream))!Client
400406 const ciphertext = record_decoder.slice(message_len);
401407 const auth_tag = record_decoder.array(P.mac_length);
402408 P.AEAD.decrypt(cleartext, ciphertext, auth_tag.*, ad, nonce, pv.app_cipher.server_write_key) catch return error.TlsBadRecordMac;
403 break :cleartext cleartext;
409 cleartext_fragment_end += message_len;
404410 },
405 };
411 }
406412 read_seq += 1;
407 break :content .{ tls.Decoder.fromTheirSlice(cleartext), record_ct };
413 break :content .{ tls.Decoder.fromTheirSlice(cleartext_buf[cleartext_fragment_start..cleartext_fragment_end]), record_ct };
408414 },
409415 };
410416 switch (ct) {
411417 .alert => {
412 try ctd.ensure(2);
418 ctd.ensure(2) catch continue :fragment;
413419 const level = ctd.decode(tls.AlertLevel);
414420 const desc = ctd.decode(tls.AlertDescription);
415421 _ = level;
......@@ -420,15 +426,15 @@ pub fn init(stream: anytype, options: Options) InitError(@TypeOf(stream))!Client
420426 return error.TlsUnexpectedMessage;
421427 },
422428 .change_cipher_spec => {
423 try ctd.ensure(1);
429 ctd.ensure(1) catch continue :fragment;
424430 if (ctd.decode(tls.ChangeCipherSpecType) != .change_cipher_spec) return error.TlsIllegalParameter;
425431 cipher_state = pending_cipher_state;
426432 },
427433 .handshake => while (true) {
428 try ctd.ensure(4);
434 ctd.ensure(4) catch continue :fragment;
429435 const handshake_type = ctd.decode(tls.HandshakeType);
430436 const handshake_len = ctd.decode(u24);
431 var hsd = try ctd.sub(handshake_len);
437 var hsd = ctd.sub(handshake_len) catch continue :fragment;
432438 const wrapped_handshake = ctd.buf[ctd.idx - handshake_len - 4 .. ctd.idx];
433439 switch (handshake_type) {
434440 .server_hello => {
......@@ -657,6 +663,7 @@ pub fn init(stream: anytype, options: Options) InitError(@TypeOf(stream))!Client
657663 prev_cert = subject;
658664 cert_index += 1;
659665 }
666 cert_buf_index += 1;
660667 },
661668 .server_key_exchange => {
662669 if (tls_version != .tls_1_2) return error.TlsUnexpectedMessage;
......@@ -892,9 +899,12 @@ pub fn init(stream: anytype, options: Options) InitError(@TypeOf(stream))!Client
892899 else => return error.TlsUnexpectedMessage,
893900 }
894901 if (ctd.eof()) break;
902 cleartext_fragment_start = ctd.idx;
895903 },
896904 else => return error.TlsUnexpectedMessage,
897905 }
906 cleartext_fragment_start = 0;
907 cleartext_fragment_end = 0;
898908 }
899909}
900910