Skip to content
FutureBlitz

    Install

    Import std.crypto.pem

    Certificate

    std.crypto.pem

    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)

    std/crypto/pem.fb, line 126

    • Public ReadOnly Field Subject: String field #

      Who it's for, like "C=NL, O=Example, CN=example.com".

    • Public ReadOnly Field Issuer: String field #

      Who signed it, as Subject says who it's for.

    • Public ReadOnly Field SerialNumber: String field #

      Its serial number, as hex bytes with colons between them.

    • Public ReadOnly Field NotBefore: String field #

      The UTC time it's valid from, like "2026-10-23T12:00:00Z".

    • Public ReadOnly Field NotAfter: String field #

      The UTC time it's valid until, like "2026-10-23T12:00:00Z".

    • Public ReadOnly Field PublicKey: Key? field #

      Its public key, or Null when it's of a kind there's no KeyKind for.

    • Public ReadOnly Field Der: UInt8[] field #

      Its DER bytes.

    • Public Static Method FromPem(pem: String): Certificate Throws ParseError static method #

      The first certificate in pem. Throws a ParseError when pem has none, or one that doesn't read.

    • Public Static Method AllFromPem(pem: String): Certificate[] Throws ParseError static method #

      Each certificate in pem, in order, as a chain or a bundle of trusted ones has them. Throws a ParseError when pem has none, or one that doesn't read.

    Report an issue with this page or with FutureBlitz on GitHub.