Help us shape the future of Go! Take the 2026 Go Developer Survey

testca

package
v0.22.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package testca is a small certificate authority for the tests of the approver identity feature (docs/design/approver-identity.md, "Test plan"). It uses only the standard library, builds roots, intermediates and leaves with chosen keys, validity, names and extensions, and is deterministic where the tests need it to be: every time is an argument, nothing reads the clock.

It is test support. Nothing outside tests imports it.

Index

Constants

This section is empty.

Variables

View Source
var (
	// EKU is the bank-assigned private extended key usage the approver certificates carry.
	EKU = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 99999, 1, 1}
	// PolicyOID is the certificate policy the bank's CA asserts after MFA step-up and a hardware key.
	PolicyOID = mustOID("1.3.6.1.4.1.99999.2.1")
)
View Source
var T0 = time.Date(2026, 10, 1, 12, 0, 0, 0, time.UTC)

T0 is the instant the tests treat as "now". Certificates are issued around it.

Functions

func NewKey

func NewKey(tb testing.TB, k KeyKind) crypto.Signer

NewKey generates a key of kind k.

func Person

func Person(n int) string

Person returns the opaque person URI the default identity profile accepts for host "idp.bank.example" and the 32 hex digits made of n.

func Revoked

func Revoked(cert *x509.Certificate, t time.Time, code int) x509.RevocationListEntry

Revoked is an entry for the serial of cert revoked at t with reason code code (0 omits it).

func WithInvalidity

WithInvalidity adds an invalidity date (entry extension 2.5.29.24) to e.

Types

type CA

type CA struct {
	Cert *x509.Certificate
	Key  crypto.Signer
	DER  []byte
	// contains filtered or unexported fields
}

CA is a certificate authority: a certificate and the key that signs for it.

func NewRoot

func NewRoot(tb testing.TB, name string, o Opts) *CA

NewRoot returns a self-signed root.

func (*CA) NewCRL

func (c *CA) NewCRL(tb testing.TB, o CRLOpts) []byte

NewCRL signs a CRL as c and returns its DER.

func (*CA) NewIntermediate

func (c *CA) NewIntermediate(tb testing.TB, name string, o Opts) *CA

NewIntermediate returns a CA issued by c.

func (*CA) NewLeaf

func (c *CA) NewLeaf(tb testing.TB, o Opts) *Leaf

NewLeaf issues an approver certificate: one URI SAN (Person(1)), digital signature, the bank EKU and the bank policy, valid from an hour before T0 to an hour after it. Mutate changes any of it.

func (*CA) RawCRL

func (c *CA) RawCRL(tb testing.TB, o RawCRLOpts) []byte

RawCRL builds and signs a CRL by marshalling the TBSCertList itself.

func (*CA) TryCRL

func (c *CA) TryCRL(o CRLOpts) ([]byte, error)

TryCRL is NewCRL that reports an error when the standard library refuses to create the CRL.

func (*CA) TryLeaf

func (c *CA) TryLeaf(tb testing.TB, o Opts) (*Leaf, error)

TryLeaf is NewLeaf that reports an error when the standard library refuses to issue or to parse the certificate, for tests of inputs the library itself may block.

type CRLOpts

type CRLOpts struct {
	Number     int64
	ThisUpdate time.Time
	NextUpdate time.Time
	Entries    []x509.RevocationListEntry
	Extra      []pkix.Extension // raw CRL extensions, such as the delta indicator or expiredCertsOnCRL
	SigAlg     x509.SignatureAlgorithm
}

CRLOpts shapes one CRL signed by a CA.

type KeyKind

type KeyKind int

KeyKind names a key type for a CA or a leaf.

const (
	P256 KeyKind = iota // ECDSA P-256 (the default)
	P384
	P521
	Ed25519
	MLDSA65
	RSA2048
	RSA3072
)

The key kinds.

type Leaf

type Leaf struct {
	Cert *x509.Certificate
	Key  crypto.Signer
	DER  []byte
	// contains filtered or unexported fields
}

Leaf is an issued end-entity certificate with its key.

func (*Leaf) Chain

func (l *Leaf) Chain() [][]byte

Chain returns the leaf-first DER chain a decision carries: the leaf, then every issuing CA up to but excluding the root (the root is the trust anchor the verifier holds).

func (*Leaf) ChainWithRoot

func (l *Leaf) ChainWithRoot() [][]byte

ChainWithRoot is Chain plus the root, for tests that show a carried root is ignored.

type Opts

type Opts struct {
	Key     KeyKind                 // the new certificate's key kind (default P-256)
	KeyWith crypto.Signer           // use this key instead of generating one
	Pub     crypto.PublicKey        // a leaf for this public key, whose private half the test does not hold (an HSM's); Leaf.Key is then nil
	Mutate  func(*x509.Certificate) // last word on the template, before signing
	SigAlg  x509.SignatureAlgorithm // the signature algorithm, when the default is not wanted
}

Opts shapes one certificate. The zero value gives the default for the certificate's role.

type RawCRLOpts

type RawCRLOpts struct {
	Number      *big.Int // nil omits the cRLNumber extension
	This, Next  time.Time
	OmitNext    bool
	OmitVersion bool       // a version 1 CRL (no version field)
	Serials     []*big.Int // revoked at This, no extensions
	Entries     []RawEntry // revoked entries with their own times and extensions
	Extensions  []pkix.Extension
	PadBytes    int // adds a non-critical unknown extension of about this many bytes
	// InnerAlg and OuterAlg override the signature algorithm identifiers in the TBSCertList and in
	// the CertificateList (the signature itself is still the CA key's). OuterParams is the full DER of
	// the outer identifier's parameters (for RSASSA-PSS).
	InnerAlg, OuterAlg asn1.ObjectIdentifier
	OuterParams        []byte
	// Indefinite re-encodes the outer SEQUENCE with an indefinite length (BER).
	Indefinite bool
	// NoAKI omits the authority key identifier extension.
	NoAKI bool
}

RawCRLOpts shapes a hand-built CRL for inputs the standard library will not create: a missing CRL number, missing or backwards times, odd algorithm identifiers, padding. The CRL is signed with the CA's key (ECDSA P-256 or Ed25519 CAs only) so it verifies unless the options say otherwise; only its content is malformed.

type RawEntry

type RawEntry struct {
	Serial *big.Int
	Time   time.Time
	Exts   []pkix.Extension
}

RawEntry is a revoked entry of a hand-built CRL.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL