authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-02 13:18:56-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-02 16:57:16-07:00
log9ca6d673457723548d8fe721b44499817ead1d2d
tree699b91e6953809a86ea6df6ae866f7d6832599ed
parent97acdeeca86af4111972aeb57dd9c792e7f1f419

std.crypto.tls.Certificate: make the current time a parameter


3 files changed, 8 insertions(+), 8 deletions(-)

lib/std/crypto/Certificate.zig+3-4
......@@ -198,14 +198,13 @@ pub const Parsed = struct {
198198 /// * That the subject's issuer is indeed the provided issuer.
199199 /// * The time validity of the subject.
200200 /// * The signature.
201 pub fn verify(parsed_subject: Parsed, parsed_issuer: Parsed) VerifyError!void {
201 pub fn verify(parsed_subject: Parsed, parsed_issuer: Parsed, now_sec: i64) VerifyError!void {
202202 // Check that the subject's issuer name matches the issuer's
203203 // subject name.
204204 if (!mem.eql(u8, parsed_subject.issuer(), parsed_issuer.subject())) {
205205 return error.CertificateIssuerMismatch;
206206 }
207207
208 const now_sec = std.time.timestamp();
209208 if (now_sec < parsed_subject.validity.not_before)
210209 return error.CertificateNotYetValid;
211210 if (now_sec > parsed_subject.validity.not_after)
......@@ -419,10 +418,10 @@ pub fn parse(cert: Certificate) !Parsed {
419418 };
420419}
421420
422pub fn verify(subject: Certificate, issuer: Certificate) !void {
421pub fn verify(subject: Certificate, issuer: Certificate, now_sec: i64) !void {
423422 const parsed_subject = try subject.parse();
424423 const parsed_issuer = try issuer.parse();
425 return parsed_subject.verify(parsed_issuer);
424 return parsed_subject.verify(parsed_issuer, now_sec);
426425}
427426
428427pub fn contents(cert: Certificate, elem: der.Element) []const u8 {
lib/std/crypto/Certificate/Bundle.zig+2-2
......@@ -13,7 +13,7 @@ pub const VerifyError = Certificate.Parsed.VerifyError || error{
1313 CertificateIssuerNotFound,
1414};
1515
16pub fn verify(cb: Bundle, subject: Certificate.Parsed) VerifyError!void {
16pub fn verify(cb: Bundle, subject: Certificate.Parsed, now_sec: i64) VerifyError!void {
1717 const bytes_index = cb.find(subject.issuer()) orelse return error.CertificateIssuerNotFound;
1818 const issuer_cert: Certificate = .{
1919 .buffer = cb.bytes.items,
......@@ -22,7 +22,7 @@ pub fn verify(cb: Bundle, subject: Certificate.Parsed) VerifyError!void {
2222 // Every certificate in the bundle is pre-parsed before adding it, ensuring
2323 // that parsing will succeed here.
2424 const issuer = issuer_cert.parse() catch unreachable;
25 try subject.verify(issuer);
25 try subject.verify(issuer, now_sec);
2626}
2727
2828/// The returned bytes become invalid after calling any of the rescan functions
lib/std/crypto/tls/Client.zig+3-2
......@@ -351,6 +351,7 @@ pub fn init(stream: anytype, ca_bundle: Certificate.Bundle, host: []const u8) !C
351351 var main_cert_pub_key_algo: Certificate.AlgorithmCategory = undefined;
352352 var main_cert_pub_key_buf: [300]u8 = undefined;
353353 var main_cert_pub_key_len: u16 = undefined;
354 const now_sec = std.time.timestamp();
354355
355356 while (true) {
356357 try d.readAtLeastOurAmt(stream, tls.record_header_len);
......@@ -458,10 +459,10 @@ pub fn init(stream: anytype, ca_bundle: Certificate.Bundle, host: []const u8) !C
458459 @memcpy(&main_cert_pub_key_buf, pub_key.ptr, pub_key.len);
459460 main_cert_pub_key_len = @intCast(@TypeOf(main_cert_pub_key_len), pub_key.len);
460461 } else {
461 try prev_cert.verify(subject);
462 try prev_cert.verify(subject, now_sec);
462463 }
463464
464 if (ca_bundle.verify(subject)) |_| {
465 if (ca_bundle.verify(subject, now_sec)) |_| {
465466 handshake_state = .trust_chain_established;
466467 break :cert;
467468 } else |err| switch (err) {