authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-28 17:11:10-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-02 16:57:16-07:00
log16af6286c8055a870128aa1f7c785273b23cad55
tree0af9dfd8a54fd1bc5f01b6017bc5ea52b8a758a7
parent940d368e7ea95d2bb8185e71af3d1ec0328917dc

std.crypto.tls.Client: support SignatureScheme.ecdsa_secp384r1_sha384


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

lib/std/crypto/tls/Client.zig+22-11
......@@ -500,7 +500,7 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8)
500500 else => return error.TlsUnexpectedMessage,
501501 }
502502
503 const algorithm = @intToEnum(tls.SignatureScheme, mem.readIntBig(u16, handshake[0..2]));
503 const scheme = @intToEnum(tls.SignatureScheme, mem.readIntBig(u16, handshake[0..2]));
504504 const sig_len = mem.readIntBig(u16, handshake[2..4]);
505505 if (4 + sig_len > handshake.len) return error.TlsBadLength;
506506 const encoded_sig = handshake[4..][0..sig_len];
......@@ -520,23 +520,25 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8)
520520 };
521521 const main_cert_pub_key = main_cert_pub_key_buf[0..main_cert_pub_key_len];
522522
523 switch (algorithm) {
524 .ecdsa_secp256r1_sha256 => {
523 switch (scheme) {
524 inline .ecdsa_secp256r1_sha256,
525 .ecdsa_secp384r1_sha384,
526 => |comptime_scheme| {
525527 if (main_cert_pub_key_algo != .X9_62_id_ecPublicKey)
526 return error.TlsBadSignatureAlgorithm;
527 const P256 = std.crypto.sign.ecdsa.EcdsaP256Sha256;
528 const sig = try P256.Signature.fromDer(encoded_sig);
529 const key = try P256.PublicKey.fromSec1(main_cert_pub_key);
528 return error.TlsBadSignatureScheme;
529 const Ecdsa = SchemeEcdsa(comptime_scheme);
530 const sig = try Ecdsa.Signature.fromDer(encoded_sig);
531 const key = try Ecdsa.PublicKey.fromSec1(main_cert_pub_key);
530532 try sig.verify(verify_bytes, key);
531533 },
532534 .rsa_pss_rsae_sha256 => {
533 @panic("TODO signature algorithm: rsa_pss_rsae_sha256");
535 @panic("TODO signature scheme: rsa_pss_rsae_sha256");
534536 },
535537 else => {
536 //std.debug.print("signature algorithm: {any}\n", .{
537 // algorithm,
538 //std.debug.print("signature scheme: {any}\n", .{
539 // scheme,
538540 //});
539 return error.TlsBadSignatureAlgorithm;
541 return error.TlsBadSignatureScheme;
540542 },
541543 }
542544 },
......@@ -1008,6 +1010,15 @@ inline fn big(x: anytype) @TypeOf(x) {
10081010 };
10091011}
10101012
1013fn SchemeEcdsa(comptime scheme: tls.SignatureScheme) type {
1014 return switch (scheme) {
1015 .ecdsa_secp256r1_sha256 => crypto.sign.ecdsa.EcdsaP256Sha256,
1016 .ecdsa_secp384r1_sha384 => crypto.sign.ecdsa.EcdsaP384Sha384,
1017 .ecdsa_secp521r1_sha512 => crypto.sign.ecdsa.EcdsaP512Sha512,
1018 else => @compileError("bad scheme"),
1019 };
1020}
1021
10111022/// The priority order here is chosen based on what crypto algorithms Zig has
10121023/// available in the standard library as well as what is faster. Following are
10131024/// a few data points on the relative performance of these algorithms.