| ... | ... | @@ -500,7 +500,7 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8) |
| 500 | 500 | else => return error.TlsUnexpectedMessage, |
| 501 | 501 | } |
| 502 | 502 | |
| 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 | 504 | const sig_len = mem.readIntBig(u16, handshake[2..4]); |
| 505 | 505 | if (4 + sig_len > handshake.len) return error.TlsBadLength; |
| 506 | 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 | 520 | }; |
| 521 | 521 | const main_cert_pub_key = main_cert_pub_key_buf[0..main_cert_pub_key_len]; |
| 522 | 522 | |
| 523 | | switch (algorithm) { |
| 524 | | .ecdsa_secp256r1_sha256 => { |
| 523 | switch (scheme) { |
| 524 | inline .ecdsa_secp256r1_sha256, |
| 525 | .ecdsa_secp384r1_sha384, |
| 526 | => |comptime_scheme| { |
| 525 | 527 | 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); |
| 530 | 532 | try sig.verify(verify_bytes, key); |
| 531 | 533 | }, |
| 532 | 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 | 537 | else => { |
| 536 | | //std.debug.print("signature algorithm: {any}\n", .{ |
| 537 | | // algorithm, |
| 538 | //std.debug.print("signature scheme: {any}\n", .{ |
| 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 | 1010 | }; |
| 1009 | 1011 | } |
| 1010 | 1012 | |
| 1013 | fn 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 | 1022 | /// The priority order here is chosen based on what crypto algorithms Zig has |
| 1012 | 1023 | /// available in the standard library as well as what is faster. Following are |
| 1013 | 1024 | /// a few data points on the relative performance of these algorithms. |