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)...@@ -500,7 +500,7 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8)
500 else => return error.TlsUnexpectedMessage,500 else => return error.TlsUnexpectedMessage,
501 }501 }
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]));
504 const sig_len = mem.readIntBig(u16, handshake[2..4]);504 const sig_len = mem.readIntBig(u16, handshake[2..4]);
505 if (4 + sig_len > handshake.len) return error.TlsBadLength;505 if (4 + sig_len > handshake.len) return error.TlsBadLength;
506 const encoded_sig = handshake[4..][0..sig_len];506 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)...@@ -520,23 +520,25 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8)
520 };520 };
521 const main_cert_pub_key = main_cert_pub_key_buf[0..main_cert_pub_key_len];521 const main_cert_pub_key = main_cert_pub_key_buf[0..main_cert_pub_key_len];
522522
523 switch (algorithm) {523 switch (scheme) {
524 .ecdsa_secp256r1_sha256 => {524 inline .ecdsa_secp256r1_sha256,
525 .ecdsa_secp384r1_sha384,
526 => |comptime_scheme| {
525 if (main_cert_pub_key_algo != .X9_62_id_ecPublicKey)527 if (main_cert_pub_key_algo != .X9_62_id_ecPublicKey)
526 return error.TlsBadSignatureAlgorithm;528 return error.TlsBadSignatureScheme;
527 const P256 = std.crypto.sign.ecdsa.EcdsaP256Sha256;529 const Ecdsa = SchemeEcdsa(comptime_scheme);
528 const sig = try P256.Signature.fromDer(encoded_sig);530 const sig = try Ecdsa.Signature.fromDer(encoded_sig);
529 const key = try P256.PublicKey.fromSec1(main_cert_pub_key);531 const key = try Ecdsa.PublicKey.fromSec1(main_cert_pub_key);
530 try sig.verify(verify_bytes, key);532 try sig.verify(verify_bytes, key);
531 },533 },
532 .rsa_pss_rsae_sha256 => {534 .rsa_pss_rsae_sha256 => {
533 @panic("TODO signature algorithm: rsa_pss_rsae_sha256");535 @panic("TODO signature scheme: rsa_pss_rsae_sha256");
534 },536 },
535 else => {537 else => {
536 //std.debug.print("signature algorithm: {any}\n", .{538 //std.debug.print("signature scheme: {any}\n", .{
537 // algorithm,539 // scheme,
538 //});540 //});
539 return error.TlsBadSignatureAlgorithm;541 return error.TlsBadSignatureScheme;
540 },542 },
541 }543 }
542 },544 },
...@@ -1008,6 +1010,15 @@ inline fn big(x: anytype) @TypeOf(x) {...@@ -1008,6 +1010,15 @@ inline fn big(x: anytype) @TypeOf(x) {
1008 };1010 };
1009}1011}
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
1011/// The priority order here is chosen based on what crypto algorithms Zig has1022/// The priority order here is chosen based on what crypto algorithms Zig has
1012/// available in the standard library as well as what is faster. Following are1023/// available in the standard library as well as what is faster. Following are
1013/// a few data points on the relative performance of these algorithms.1024/// a few data points on the relative performance of these algorithms.