Skip to content

Commit 9ca6d67

Browse files
committed
std.crypto.tls.Certificate: make the current time a parameter
1 parent 97acdee commit 9ca6d67

3 files changed

Lines changed: 8 additions & 8 deletions

File tree

lib/std/crypto/Certificate.zig

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

422-
pub fn verify(subject: Certificate, issuer: Certificate) !void {
421+
pub 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

428427
pub fn contents(cert: Certificate, elem: der.Element) []const u8 {

lib/std/crypto/Certificate/Bundle.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub const VerifyError = Certificate.Parsed.VerifyError || error{
1313
CertificateIssuerNotFound,
1414
};
1515

16-
pub fn verify(cb: Bundle, subject: Certificate.Parsed) VerifyError!void {
16+
pub 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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {

0 commit comments

Comments
 (0)