Documentation ¶
Overview ¶
Package cat implements a set of shorthand/utility functions for performing CA tasks during testing.
Index ¶
- func ParseDurationP(value string) time.Duration
- func ParseTimeP(layout, value string) time.Time
- func RandBytes(len int) ([]byte, error)
- func RandBytesP(len int) []byte
- func RandHexBytes(len int) ([]byte, error)
- func RandHexBytesP(len int) []byte
- func RandInt(bitMax uint) (*big.Int, error)
- func RandIntP(bitMax uint) *big.Int
- type Chain
- func DecodeChain(data, pass []byte) (Chain, error)
- func DecodeChainP(data, pass []byte) Chain
- func GenerateChain(tmps []*Template) (Chain, error)
- func GenerateChainP(tmps []*Template) Chain
- func GenerateClientChain(tmps []*Template) (Chain, error)
- func GenerateClientChainP(tmps []*Template) Chain
- func GenerateServerChain(tmps []*Template) (Chain, error)
- func GenerateServerChainP(tmps []*Template) Chain
- func ReadChainFile(name string, pass []byte) (Chain, error)
- func ReadChainFileP(name string, pass []byte) Chain
- func (ch Chain) CertPool() *x509.CertPool
- func (ch Chain) Encode(withKey bool) ([]byte, error)
- func (ch Chain) EncodeP(withKey bool) []byte
- func (ch Chain) Encrypt(alg x509.PEMCipher, pass []byte) ([]byte, error)
- func (ch Chain) EncryptP(alg x509.PEMCipher, pass []byte) []byte
- func (ch Chain) Head(num int) Chain
- func (ch Chain) IssueChain(tmps []*Template) (Chain, error)
- func (ch Chain) IssueChainP(tmps []*Template) Chain
- func (ch Chain) IssueClientChain(tmps []*Template) (Chain, error)
- func (ch Chain) IssueClientChainP(tmps []*Template) Chain
- func (ch Chain) IssueServerChain(tmps []*Template) (Chain, error)
- func (ch Chain) IssueServerChainP(tmps []*Template) Chain
- func (ch Chain) SelfSigned() bool
- func (ch Chain) String() string
- func (ch Chain) TLS() *tls.Certificate
- func (ch Chain) Tail(num int) Chain
- func (ch Chain) Write(out io.Writer, withKey bool) error
- func (ch Chain) WriteEncrypted(out io.Writer, alg x509.PEMCipher, pass []byte) error
- func (ch Chain) WriteEncryptedFile(name string, mode os.FileMode, alg x509.PEMCipher, pass []byte) error
- func (ch Chain) WriteEncryptedFileP(name string, mode os.FileMode, alg x509.PEMCipher, pass []byte)
- func (ch Chain) WriteEncryptedP(out io.Writer, alg x509.PEMCipher, pass []byte)
- func (ch Chain) WriteFile(name string, mode os.FileMode, withKey bool) error
- func (ch Chain) WriteFileP(name string, mode os.FileMode, withKey bool)
- func (ch Chain) WriteP(out io.Writer, withKey bool)
- func (ch Chain) X509() []*x509.Certificate
- type Key
- func (k *Key) Encode() ([]byte, error)
- func (k *Key) EncodeP() []byte
- func (k *Key) EncodePublic() ([]byte, error)
- func (k *Key) EncodePublicP() []byte
- func (k *Key) Encrypt(alg x509.PEMCipher, pass []byte) ([]byte, error)
- func (k *Key) EncryptP(alg x509.PEMCipher, pass []byte) []byte
- func (k *Key) Equal(x crypto.PrivateKey) bool
- func (k *Key) Public() crypto.PublicKey
- func (k *Key) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) (signature []byte, err error)
- func (k *Key) Write(out io.Writer) error
- func (k *Key) WriteEncrypted(out io.Writer, alg x509.PEMCipher, pass []byte) error
- func (k *Key) WriteEncryptedFile(name string, mode os.FileMode, alg x509.PEMCipher, pass []byte) error
- func (k *Key) WriteEncryptedFileP(name string, mode os.FileMode, alg x509.PEMCipher, pass []byte)
- func (k *Key) WriteEncryptedP(out io.Writer, alg x509.PEMCipher, pass []byte)
- func (k *Key) WriteFile(name string, mode os.FileMode) error
- func (k *Key) WriteFileP(name string, mode os.FileMode)
- func (k *Key) WriteP(out io.Writer)
- func (k *Key) WritePublic(out io.Writer) error
- func (k *Key) WritePublicFile(name string, mode os.FileMode) error
- func (k *Key) WritePublicFileP(name string, mode os.FileMode)
- func (k *Key) WritePublicP(out io.Writer)
- type KeyFormat
- type Pair
- func (p *Pair) CertPool() *x509.CertPool
- func (p *Pair) Encode(withKey bool) ([]byte, error)
- func (p *Pair) EncodeP(withKey bool) []byte
- func (p *Pair) Encrypt(alg x509.PEMCipher, pass []byte) ([]byte, error)
- func (p *Pair) EncryptP(alg x509.PEMCipher, pass []byte) []byte
- func (p *Pair) SelfSigned() bool
- func (p *Pair) String() string
- func (p *Pair) TLS() *tls.Certificate
- func (p *Pair) Write(out io.Writer, withKey bool) error
- func (p *Pair) WriteEncrypted(out io.Writer, alg x509.PEMCipher, pass []byte) error
- func (p *Pair) WriteEncryptedFile(name string, mode os.FileMode, alg x509.PEMCipher, pass []byte) error
- func (p *Pair) WriteEncryptedFileP(name string, mode os.FileMode, alg x509.PEMCipher, pass []byte)
- func (p *Pair) WriteEncryptedP(out io.Writer, alg x509.PEMCipher, pass []byte)
- func (p *Pair) WriteFile(name string, mode os.FileMode, withKey bool) error
- func (p *Pair) WriteFileP(name string, mode os.FileMode, withKey bool)
- func (p *Pair) WriteP(out io.Writer, withKey bool)
- type Template
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ParseDurationP ¶
Alternate version of time.ParseDuration that panics on error.
func ParseTimeP ¶
Alternate version of time.Parse that panics on error.
func RandBytesP ¶ added in v0.1.1
Alternate version of RandBytes that panics on error.
func RandHexBytes ¶ added in v0.1.1
Return a slice of len hex-encoded random bytes from rand.Read.
func RandHexBytesP ¶ added in v0.1.1
Alternate version of RandHexBytes that panics on error.
Types ¶
type Chain ¶
type Chain []*Pair
Represents a generated chain of certificate pairs, ordered leaf-first.
func DecodeChain ¶ added in v0.1.2
Decode a chain of PEM-encoded certificate pairs.
This function attempts to consume the entire input, and will return an error if it does not find at least one PEM-encoded certificate or certificate pair, or if it encounters any unrecognised PEM blocks. If pass is not nil it will be used as a passphrase to attempt to decrypt private keys, otherwise encrypted keys will be rejected.
func DecodeChainP ¶ added in v0.1.2
Alternate version of DecodeChain that panics on error.
func GenerateChain ¶
Generate a certificate chain according to the provided templates. The first template is assumed to represent a self-signed root CA, and any subsequent templates are assumed to represent intermediate CAs issued in sequence from that root.
If Key field in the template is populated it will be used as private key for that pair, otherwise a new private key is generated according to the value of the KeyFormat field. If neither Key or KeyFormat are populated the template will inherit the KeyFormat of its parent template and a new private key will be generated.
The contents of the Certificate struct in each template are parsed identically to (and ultimately by) x509.CreateCertificate, but with some additional logic allowing them to be automatically populated beforehand if left unset:
- If the SerialNumber field is unpopulated it will either inherit the serial number of the parent template (incremented by one), or be set to 1 if no parent exists.
- If both NotBefore and NotAfter are unpopulated they will either inherit the validity period of the parent template, or be set to a 24-hour period starting from the current time if no parent exists.
- If all of KeyUsage, ExtKeyUsage, IsCA, BasicConstraintsValid, MaxPathLen, and MaxPathLenZero are unpopulated then the template will be configured as a CA certificate with unlimited path length.
- If MaxPathLen or MaxPathLenZero are populated but all of KeyUsage, ExtKeyUsage, IsCA, and BasicConstraintsValid are not then the template will be configured as a CA certificate with the specified path length.
The resulting certificate chain is returned in reverse order relative to the templates, starting with the leaf-most certificate pair.
func GenerateChainP ¶
Alternate version of GenerateChain that panics on error.
func GenerateClientChain ¶
Generate a certificate chain ending with a client end-entity certificate, according to the provided templates.
This function behaves identically to GenerateChain, save for the fact that the leaf-most certificate in the chain will be configured as a client end-entity certificate rather than a CA certificate if none of its KeyUsage, ExtKeyUsage, IsCA, MaxPathLen, MaxPathLenZero, or BasicConstraintsValid fields are populated in the template.
If the provided chain consists of a single template, a single self-signed client end-entity certificate pair will be produced.
func GenerateClientChainP ¶
Alternate version of GenerateClientChain that panics on error.
func GenerateServerChain ¶
Generate a certificate chain ending with a server end-entity certificate, according to the provided templates.
This function behaves identically to GenerateChain, save for the fact that the leaf-most certificate in the chain will be configured as a server end-entity certificate rather than a CA certificate if none of its KeyUsage, ExtKeyUsage, IsCA, MaxPathLen, MaxPathLenZero, or BasicConstraintsValid fields are populated in the template.
If the provided chain consists of a single template, a single self-signed server end-entity certificate pair will be produced.
func GenerateServerChainP ¶
Alternate version of GenerateServerChain that panics on error.
func ReadChainFile ¶ added in v0.1.2
Read and decode a chain of PEM-encoded certificate pairs from a file
This function attempts to consume the entire file, and will return an error if it does not find at least one PEM-encoded certificate or certificate pair, or if it encounters any unrecognised PEM blocks. If pass is not nil it will be used as a passphrase to attempt to decrypt private keys, otherwise encrypted keys will be rejected.
func ReadChainFileP ¶ added in v0.1.2
Alternate version of ReadChainFile that panics on error.
func (Chain) CertPool ¶ added in v0.2.0
Return an x509.CertPool instance containing the root-most certificate in this chain.
func (Chain) Encode ¶
Encode certificate chain as PEM, optionally preceded by a plain-text copy of the leaf certificate private key.
func (Chain) Encrypt ¶
Encode certificate chain as PEM, preceded by a passphrase-encrypted copy of the leaf certificate private key.
func (Chain) Head ¶
Returns a slice from the front of the chain; non-negative values specify how many pairs to keep, negative values specify how many pairs to drop.
func (Chain) IssueChain ¶
Generate a new certificate chain according to the provided templates, using this chain as the issuing CA.
This function behaves identically to GenerateChain, save for the fact that the leaf-most certificate in this chain will be used as the issuing CA for the new chain, rather than it beginning with a self-signed root.
The resulting certificate chain will be a concatenation of the two, starting with the leaf-most certificate pair.
func (Chain) IssueChainP ¶
Alternate version of IssueChain that panics on error.
func (Chain) IssueClientChain ¶
Generate a new certificate chain according to the provided templates, ending with a client end-entity certificate, and using this chain as the issuing CA.
This function behaves identically to GenerateClientChain, save for the fact that the leaf-most certificate in this chain will be used as the issuing CA for the new chain, rather than it beginning with a self-signed root.
The resulting certificate chain will be a concatenation of the two, starting with the leaf-most certificate pair.
func (Chain) IssueClientChainP ¶
Alternate version of IssueClientChain that panics on error.
func (Chain) IssueServerChain ¶
Generate a new certificate chain according to the provided templates, ending with a server end-entity certificate, and using this chain as the issuing CA.
This function behaves identically to GenerateServerChain, save for the fact that the leaf-most certificate in this chain will be used as the issuing CA for the new chain, rather than it beginning with a self-signed root.
The resulting certificate chain will be a concatenation of the two, starting with the leaf-most certificate pair.
func (Chain) IssueServerChainP ¶
Alternate version of IssueServerChain that panics on error.
func (Chain) SelfSigned ¶ added in v0.2.0
Returns true if the root-most certificate in this chain is self-signed.
func (Chain) TLS ¶ added in v0.2.0
func (ch Chain) TLS() *tls.Certificate
Return a tls.Certificate instance created from this chain.
func (Chain) Tail ¶
Returns a slice from the back of the chain; non-negative values specify how many pairs to keep, negative values specify how many pairs to drop.
func (Chain) Write ¶
Encode certificate chain as PEM, optionally preceded by a plain-text copy of the leaf certificate private key, then write it to a stream.
func (Chain) WriteEncrypted ¶
Encode certificate chain as PEM, preceded by a passphrase-encrypted copy of the leaf certificate private key, then write it to a stream.
func (Chain) WriteEncryptedFile ¶
func (ch Chain) WriteEncryptedFile(name string, mode os.FileMode, alg x509.PEMCipher, pass []byte) error
Encode certificate chain as PEM, preceded by a passphrase-encrypted copy of the leaf certificate private key, then write it to a file.
func (Chain) WriteEncryptedFileP ¶
Alternate version of WriteEncryptedFile that panics on error.
func (Chain) WriteEncryptedP ¶
Alternate version of WriteEncrypted that panics on error.
func (Chain) WriteFile ¶
Encode certificate chain as PEM, optionally preceded by a plain-text copy of the leaf certificate private key, then write it to a file.
func (Chain) WriteFileP ¶
Alternate version of WriteFile that panics on error.
func (Chain) X509 ¶ added in v0.2.0
func (ch Chain) X509() []*x509.Certificate
Returns this chain as an x509.Certificate array.
type Key ¶
type Key struct { crypto.PrivateKey crypto.Signer Key any // The actual private key object wrapped by this one. Format KeyFormat // The associated format for this private key. }
Represents a combination of a private key and associated format information.
Implements crypto.PrivateKey and crypto.Signer so it can be used directly with most functions expecting a standard Go private key.
func DecodeKey ¶ added in v0.1.2
Decode a PEM-encoded private key and return it alongside any unconsumed bytes.
Returns an error if the provided data contains no PEM blocks, or if the first PEM block found cannot be decoded as a private key. If pass is not nil it will be used as a passphrase to attempt to decrypt private keys, otherwise encrypted keys will be rejected.
func DecodeKeyP ¶ added in v0.1.2
Alternate version of DecodeKey that panics on error.
func GenerateKey ¶
Generate a new private key according to the specified format.
func GenerateKeyP ¶
Alternate version of GenerateKey that panics on error.
func ReadKeyFile ¶ added in v0.1.2
Read and decode a PEM-encoded private key from a file.
Returns an error if the provided file contains no PEM blocks, or if the first PEM block found cannot be decoded as a private key. If pass is not nil it will be used as a passphrase to attempt to decrypt private keys, otherwise encrypted keys will be rejected.
func ReadKeyFileP ¶ added in v0.1.2
Alternate version of ReadKeyFile that panics on error.
func (*Key) EncodePublic ¶ added in v0.2.1
Encode public portion of key as PEM according to its associated key format.
Most private key formats defined in this library do not have a directly equivalent public key format. RSA+PKCS1 formats will encode public keys using PKCS1; all other formats use PKIX, a.k.a SubjectPublicKeyInfo.
func (*Key) EncodePublicP ¶ added in v0.2.1
Alternate version of EncodePublic that panics on error.
func (*Key) Encrypt ¶
Encode private key as passphrase-encrypted PEM according to its associated key format.
func (*Key) Equal ¶
func (k *Key) Equal(x crypto.PrivateKey) bool
Implements crypto.PrivateKey.Equal; equality comparison between this private key and another.
func (*Key) Public ¶
Implements crypto.PrivateKey.Public; returns the public key for this private key.
func (*Key) Sign ¶
func (k *Key) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) (signature []byte, err error)
Implements crypto.Signer.Sign; signs a digest with this private key.
func (*Key) Write ¶
Encode private key as PEM according to its associated key format, then write it to a stream.
func (*Key) WriteEncrypted ¶
Encode private key as passphrase-encrypted PEM according to its associated key format, then write it to a stream.
func (*Key) WriteEncryptedFile ¶
func (k *Key) WriteEncryptedFile(name string, mode os.FileMode, alg x509.PEMCipher, pass []byte) error
Encode private key as passphrase-encrypted PEM according to its associated key format, then write it to a file.
func (*Key) WriteEncryptedFileP ¶
Alternate version of WriteEncryptedFile that panics on error.
func (*Key) WriteEncryptedP ¶
Alternate version of WriteEncrypted that panics on error.
func (*Key) WriteFile ¶
Encode private key as PEM according to its associated key format, then write it to a file.
func (*Key) WriteFileP ¶
Alternate version of WriteFile that panics on error.
func (*Key) WritePublic ¶ added in v0.2.1
Encode public portion of key as PEM according to its associated key format, then write it to a stream.
Most private key formats defined in this library do not have a directly equivalent public key format. RSA+PKCS1 formats will encode public keys using PKCS1; all other formats use PKIX, a.k.a SubjectPublicKeyInfo.
func (*Key) WritePublicFile ¶ added in v0.2.1
Encode public portion of a key as PEM according to its associated key format, then write it to a file.
Most private key formats defined in this library do not have a directly equivalent public key format. RSA+PKCS1 formats will encode public keys using PKCS1; all other formats use PKIX, a.k.a SubjectPublicKeyInfo.
func (*Key) WritePublicFileP ¶ added in v0.2.1
Alternate version of WritePublicFile that panics on error.
func (*Key) WritePublicP ¶ added in v0.2.1
Alternate version of WritePublic that panics on error.
type KeyFormat ¶
type KeyFormat int
Represents a combination of a private key algorithm, generation parameters such as key size or elliptic curve, and marshaling/encoding parameters.
const ( // 1024-bit RSA key marshaled using PKCS #1. RSA_1024_PKCS1 KeyFormat = RSA_1024 | PKCS1 // 2048-bit RSA key marshaled using PKCS #1. RSA_2048_PKCS1 KeyFormat = RSA_2048 | PKCS1 // 3072-bit RSA key marshaled using PKCS #1. RSA_3072_PKCS1 KeyFormat = RSA_3072 | PKCS1 // 4096-bit RSA key marshaled using PKCS #1. RSA_4096_PKCS1 KeyFormat = RSA_4096 | PKCS1 // 1024-bit RSA key marshaled using PKCS #8. RSA_1024_PKCS8 KeyFormat = RSA_1024 | PKCS8 // 2048-bit RSA key marshaled using PKCS #8. RSA_2048_PKCS8 KeyFormat = RSA_2048 | PKCS8 // 3072-bit RSA key marshaled using PKCS #8. RSA_3072_PKCS8 KeyFormat = RSA_3072 | PKCS8 // 4096-bit RSA key marshaled using PKCS #8. RSA_4096_PKCS8 KeyFormat = RSA_4096 | PKCS8 // ECDSA key using NIST P-224 marshaled using SEC 1. ECDSA_P224_SEC1 KeyFormat = ECDSA_P224 | SEC1 // ECDSA key using NIST P-256 marshaled using SEC 1. ECDSA_P256_SEC1 KeyFormat = ECDSA_P256 | SEC1 // ECDSA key using NIST P-384 marshaled using SEC 1. ECDSA_P384_SEC1 KeyFormat = ECDSA_P384 | SEC1 // ECDSA key using NIST P-521 marshaled using SEC 1. ECDSA_P521_SEC1 KeyFormat = ECDSA_P521 | SEC1 // ECDSA key using NIST P-224 marshaled using PKCS #8. ECDSA_P224_PKCS8 KeyFormat = ECDSA_P224 | PKCS8 // ECDSA key using NIST P-256 marshaled using PKCS #8. ECDSA_P256_PKCS8 KeyFormat = ECDSA_P256 | PKCS8 // ECDSA key using NIST P-384 marshaled using PKCS #8. ECDSA_P384_PKCS8 KeyFormat = ECDSA_P384 | PKCS8 // ECDSA key using NIST P-521 marshaled using PKCS #8. ECDSA_P521_PKCS8 KeyFormat = ECDSA_P521 | PKCS8 // Ed25519 key marshaled using PKCS #8. ED25519_PKCS8 KeyFormat = ED25519 | PKCS8 )
Private key type and encoding combinations.
func IdentifyKey ¶
Determine the key format for the given private key.
func IdentifyKeyP ¶
Alternate version of IdentifyKey that panics on error.
type Pair ¶
type Pair struct { Key *Key Cert *x509.Certificate }
Represents a generated certificate with its associated private key.
func DecodePair ¶ added in v0.1.2
Decode a PEM-encoded certificate pair and return it alongside any unconsumed bytes.
Returns an error if the provided data does not contain either a PEM-encoded certificate, or a PEM-encoded private key followed by a certificate. If pass is not nil it will be used as a passphrase to attempt to decrypt private keys, otherwise encrypted keys will be rejected.
func DecodePairP ¶ added in v0.1.2
Alternate version of DecodePair that panics on error.
func ReadPairFile ¶ added in v0.1.2
Read and decode a PEM-encoded certificate pair key from a file.
Returns an error if the provided file does not contain either a PEM-encoded certificate, or a PEM-encoded private key followed by a certificate. If pass is not nil it will be used as a passphrase to attempt to decrypt private keys, otherwise encrypted keys will be rejected.
func ReadPairFileP ¶ added in v0.1.2
Alternate version of ReadPairFile that panics on error.
func (*Pair) Encode ¶
Encode certificate as PEM, optionally preceded by a plain-text copy of its private key.
func (*Pair) Encrypt ¶
Encode certificate as PEM, preceded by a passphrase-encrypted copy of its private key.
func (*Pair) SelfSigned ¶
Returns true if this certificate is self-signed.
func (*Pair) TLS ¶
func (p *Pair) TLS() *tls.Certificate
Return a tls.Certificate instance created from this certifcate pair.
func (*Pair) Write ¶
Encode certificate as PEM, optionally preceded by a plain-text copy of its private key, then write it to a stream.
func (*Pair) WriteEncrypted ¶
Encode certificate as PEM, preceded by a passphrase-encrypted copy of its private key, then write it to a stream.
func (*Pair) WriteEncryptedFile ¶
func (p *Pair) WriteEncryptedFile(name string, mode os.FileMode, alg x509.PEMCipher, pass []byte) error
Encode certificate as PEM, preceded by a passphrase-encrypted copy of its private key, then write it to a file.
func (*Pair) WriteEncryptedFileP ¶
Alternate version of WriteEncryptedFile that panics on error.
func (*Pair) WriteEncryptedP ¶
Alternate version of WriteEncrypted that panics on error.
func (*Pair) WriteFile ¶
Encode certificate as PEM, optionally preceded by a plain-text copy of its private key, then write it to a file.
func (*Pair) WriteFileP ¶
Alternate version of WriteFile that panics on error.