跳转到内容

std.crypto.Certificate.Chain

标准库Zig 0.17.0官方原文
源码std/crypto/Certificate/Chain.zig

第 1 行

证书序列,其中每个证书由下一个证书验证。

字段

store: ?crypt32.HCERTSTORE
primary: ?*const crypt32.CERT_CONTEXT

std.crypto.Certificate.Chain.addCert

函数

第 17 行

pub fn addCert(chain: *Chain, cert: []const u8) std.Io.UnexpectedError!void

参数

chain: *Chain
cert: []const u8

错误

Unexpected

操作系统返回了未记录在文档中的错误码。

理论上不可能出现此错误,但处理 此错误优于触发未定义行为。

观察到此错误码时,通常意味着 Zig 标准 库需要小幅修补,将该错误码加入 相应函数的错误集合。

std.crypto.Certificate.Chain.ALLOWED_EKUS

常量

第 90 行

std.crypto.Certificate.Chain.assert

别名

第 92 行

别名目标 std.debug.assert

std.crypto.Certificate.Chain.builtin

常量

第 93 行

std.crypto.Certificate.Chain.Chain

别名

第 2 行

别名目标 std.crypto.Certificate.Chain

std.crypto.Certificate.Chain.crypt32

别名

第 95 行

别名目标 std.os.windows.crypt32

std.crypto.Certificate.Chain.deinit

函数

第 9 行

pub fn deinit(chain: *Chain) void

参数

chain: *Chain

std.crypto.Certificate.Chain.empty

常量

第 7 行

Chain

std.crypto.Certificate.Chain.std

别名

第 94 行

别名目标 std

std.crypto.Certificate.Chain.verify

函数

第 43 行

pub fn verify(chain: *const Chain, now: std.Io.Timestamp) VerifyError!void

参数

chain: *const Chain
now: std.Io.Timestamp

错误

TlsCertificateNotVerified
Unexpected UnexpectedError

操作系统返回了未记录在文档中的错误码。

理论上不可能出现此错误,但处理 此错误优于触发未定义行为。

观察到此错误码时,通常意味着 Zig 标准 库需要小幅修补,将该错误码加入 相应函数的错误集合。

std.crypto.Certificate.Chain.VerifyError

错误集合

第 39 行

错误

TlsCertificateNotVerified
Unexpected UnexpectedError

操作系统返回了未记录在文档中的错误码。

理论上不可能出现此错误,但处理 此错误优于触发未定义行为。

观察到此错误码时,通常意味着 Zig 标准 库需要小幅修补,将该错误码加入 相应函数的错误集合。

源码

std/crypto/Certificate/Chain.zig

//! 证书序列,其中每个证书由下一个证书验证。
const Chain = @This();

store: ?crypt32.HCERTSTORE,
primary: ?*const crypt32.CERT_CONTEXT,

pub const empty: Chain = .{ .store = null, .primary = null };

pub fn deinit(chain: *Chain) void {
    if (chain.primary) |primary| assert(crypt32.CertFreeCertificateContext(primary).toBool());
    if (chain.store) |store| if (!crypt32.CertCloseStore(store, .{
        .CHECK = std.debug.runtime_safety,
    }).toBool()) std.os.windows.unexpectedError(std.os.windows.GetLastError()) catch unreachable;
    chain.* = .empty;
}

pub fn addCert(chain: *Chain, cert: []const u8) std.Io.UnexpectedError!void {
    const store = chain.store orelse store: {
        const store = crypt32.CertOpenStore(
            .MEMORY,
            .{},
            .NULL,
            .{},
            null,
        ) orelse return std.os.windows.unexpectedError(std.os.windows.GetLastError());
        chain.store = store;
        break :store store;
    };
    if (!crypt32.CertAddEncodedCertificateToStore(
        store,
        .{ .CERT = .ASN },
        cert.ptr,
        @intCast(cert.len),
        .ALWAYS,
        if (chain.primary) |_| null else &chain.primary,
    ).toBool()) return std.os.windows.unexpectedError(std.os.windows.GetLastError());
}

pub const VerifyError = error{
    TlsCertificateNotVerified,
} || std.Io.UnexpectedError;

pub fn verify(chain: *const Chain, now: std.Io.Timestamp) VerifyError!void {
    const now_win = @divFloor(now.nanoseconds - std.time.epoch.windows * std.time.ns_per_s, 100);
    var cert_chain: *const crypt32.CERT_CHAIN.CONTEXT = undefined;
    if (!crypt32.CertGetCertificateChain(
        .CURRENT_USER,
        chain.primary orelse return error.TlsCertificateNotVerified,
        &.{
            .dwLowDateTime = @bitCast(@as(i32, @truncate(now_win >> 0))),
            .dwHighDateTime = @bitCast(@as(i32, @intCast(now_win >> 32))),
        },
        null,
        &.{ .RequestedUsage = .{ .dwType = .AND, .Usage = .{
            .cUsageIdentifier = ALLOWED_EKUS.len,
            .rgpszUsageIdentifier = &ALLOWED_EKUS,
        } } },
        .{ .REVOCATION_CHECK_END_CERT = true, .REVOCATION_ACCUMULATIVE_TIMEOUT = true },
        null,
        &cert_chain,
    ).toBool()) return std.os.windows.unexpectedError(std.os.windows.GetLastError());
    defer crypt32.CertFreeCertificateChain(cert_chain);
    var status: crypt32.CERT_CHAIN.POLICY.STATUS = .{
        .dwError = undefined,
        .lChainIndex = undefined,
        .lElementIndex = undefined,
        .pvExtraPolicyStatus = undefined,
    };
    if (!crypt32.CertVerifyCertificateChainPolicy(
        .SSL,
        cert_chain,
        &.{
            .dwFlags = .{
                .IGNORE_END_REV_UNKNOWN = true,
                .IGNORE_CTL_SIGNER_REV_UNKNOWN = true,
                .IGNORE_CA_REV_UNKNOWN = true,
                .IGNORE_ROOT_REV_UNKNOWN = true,
            },
            .pvExtraPolicyPara = @constCast(&crypt32.HTTPSPolicyCallbackData{ .dwAuthType = .SERVER }),
        },
        &status,
    ).toBool()) return std.os.windows.unexpectedError(std.os.windows.GetLastError());
    switch (status.dwError) {
        .SUCCESS => return,
        .CERT_E_UNTRUSTEDROOT => return error.TlsCertificateNotVerified,
        else => |err| return std.os.windows.unexpectedError(err),
    }
}

const ALLOWED_EKUS = [_][*:0]const u8{"1.3.6.1.5.5.7.3.1"};

const assert = std.debug.assert;
const builtin = @import("builtin");
const std = @import("std");
const crypt32 = std.os.windows.crypt32;