Import std.crypto.pem
Certificate
struct
Public Struct Certificate
An X.509 certificate, read from PEM: who it's for and who signed it, as Python's ssl shows them, "C=NL, O=Example, CN=example.com", in the certificate's order, as UTF-8 text with RFC 4514's escapes; its serial number as hex bytes with colons between them; the UTC times it's valid from and to, as "2026-10-23T12:00:00Z"; its public key; and its DER. Checking that one certificate signed another, or that one is trusted, is TLS's.
Examples
Local pem = r"""
-----BEGIN CERTIFICATE-----
MIIBkjCCATigAwIBAgIBBzAKBggqhkjOPQQDAjAoMRAwDgYDVQQKDAdFeGFtcGxl
MRQwEgYDVQQDDAtleGFtcGxlLmNvbTAeFw0yNjEwMzAwODA2MzBaFw0zNjEwMjcw
ODA2MzBaMCgxEDAOBgNVBAoMB0V4YW1wbGUxFDASBgNVBAMMC2V4YW1wbGUuY29t
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEuoR7/qV4GrMf2sAuOMkR+/vx2oqp
cyMbBNqdVgnmnhcTFpPNCpRilQhfBbAgQxACoj9Sd4UjZvjxqK/JqHJxJKNTMFEw
HQYDVR0OBBYEFLEj6Qfik6Vrt/6Kc7QYyvyxtxPiMB8GA1UdIwQYMBaAFLEj6Qfi
k6Vrt/6Kc7QYyvyxtxPiMA8GA1UdEwEB/wQFMAMBAf8wCgYIKoZIzj0EAwIDSAAw
RQIhANoLqQc7YxRjHvgUjU72HUW3ucpLRmi2LK/tUwiOss1hAiB0VCMesezWW8W0
gPTHZ/E8f7x3Heu+SKfp/SO5Igym9w==
-----END CERTIFICATE-----
"""
Local certificate = Try Certificate.FromPem(pem)
Expect(certificate.Subject == "O=Example, CN=example.com")
Expect(certificate.Issuer == certificate.Subject)
Expect(certificate.SerialNumber == "07")
Expect(certificate.NotAfter == "2036-10-27T08:06:30Z")
Expect(certificate.PublicKey?.Kind == KeyKind.ECDSA_P256)
Expect((Try Certificate.AllFromPem(pem)).Count == 1)-
Who it's for, like "C=NL, O=Example, CN=example.com".
-
Who signed it, as Subject says who it's for.
-
Its serial number, as hex bytes with colons between them.
-
The UTC time it's valid from, like "2026-10-23T12:00:00Z".
-
The UTC time it's valid until, like "2026-10-23T12:00:00Z".
-
Its public key, or Null when it's of a kind there's no KeyKind for.
-
Its DER bytes.
-
The first certificate in
pem. Throws a ParseError whenpemhas none, or one that doesn't read. -
Each certificate in
pem, in order, as a chain or a bundle of trusted ones has them. Throws a ParseError whenpemhas none, or one that doesn't read.
Report an issue with this page or with FutureBlitz on GitHub.