Documentation
¶
Overview ¶
Package certkit parses and assembles X.509 certificate material across the common container formats (PEM, DER, PKCS#12, PKCS#7, JKS/JCEKS).
Index ¶
- Variables
- func Export(b Bundle, f Format, newPassphrase string) ([]byte, error)
- func ExportContext(ctx context.Context, b Bundle, f Format, newPassphrase string, opts ...Option) ([]byte, error)
- type Bundle
- func GenerateSelfSigned(opts GenOpts) (Bundle, error)
- func Parse(data []byte, passphrase string) (Bundle, error)
- func ParseContext(ctx context.Context, data []byte, passphrase string, opts ...Option) (Bundle, error)
- func ParseEntry(data []byte, passphrase, alias string) (Bundle, error)
- func ParseEntryContext(ctx context.Context, data []byte, passphrase, alias string, opts ...Option) (Bundle, error)
- func Rotate(current Bundle, opts GenOpts) (Bundle, error)
- type ErrMultipleEntries
- type Format
- type GenOpts
- type Meta
- type Option
Constants ¶
This section is empty.
Variables ¶
var ( // ErrWrongPassphrase is returned when the supplied passphrase fails to // decrypt an encrypted private key, PKCS#12 archive or JKS/JCEKS // keystore. ErrWrongPassphrase = errors.New("certkit: wrong passphrase") // ErrUnrecognizedFormat is returned when the input does not match any // supported container format. ErrUnrecognizedFormat = errors.New("certkit: unrecognized format") // ErrNoPrivateKey is returned when a caller requests a key-bearing // export (e.g. PKCS#12, JKS) from a Bundle that has no private key. ErrNoPrivateKey = errors.New("certkit: no private key") )
Sentinel errors returned by Parse, ParseEntry and Export.
Functions ¶
func Export ¶
Export assembles a Bundle into the given container Format, returning the encoded bytes. newPassphrase protects the output for formats that support encryption: PKCS#12 and JKS always, and the private key of PEMBundle and PEMKeyOnly (emitted as an "ENCRYPTED PRIVATE KEY" block when a passphrase is supplied, plaintext PKCS#8 otherwise). It is ignored by the cert-only PEM/DER/PKCS#7 formats, which carry no key.
Exporting a key-bearing format (PKCS12, JKS, PEMBundle, PEMKeyOnly) from a Bundle with no private key returns ErrNoPrivateKey.
Export is the observability-free wrapper over ExportContext.
func ExportContext ¶
func ExportContext(ctx context.Context, b Bundle, f Format, newPassphrase string, opts ...Option) ([]byte, error)
ExportContext is Export with optional, opt-in observability. With no opts it behaves exactly like Export and emits nothing. WithLogger and WithTracing enable structured logging and an OpenTelemetry span ("certkit.Export"); neither ever records newPassphrase, key material, or certificate bytes.
Types ¶
type Bundle ¶
Bundle is the normalized, in-memory representation of a parsed certificate/key container: a leaf certificate, its optional private key, an optional chain of intermediate/root certificates, and derived metadata. LeafPEM, KeyPEM and each entry of ChainPEM are PEM-encoded blocks.
func GenerateSelfSigned ¶ added in v1.1.0
GenerateSelfSigned creates a fresh RSA keypair and a self-signed leaf certificate for it, returning both as a Bundle. The returned Bundle has no ChainPEM, since a self-signed leaf is its own trust anchor.
The certificate is suited to a TLS/signing service leaf: it carries DigitalSignature and KeyEncipherment key usage, ExtKeyUsageServerAuth and ExtKeyUsageClientAuth extended usages, and is not a CA.
func Parse ¶
Parse decodes data (in any supported container format) into a Bundle. passphrase is used to decrypt an encrypted private key, PKCS#12 archive or JKS/JCEKS keystore; pass "" when the input is not encrypted.
If the container holds more than one distinct entry (e.g. a multi-alias JKS/JCEKS keystore, or a PKCS#7 bag with multiple leaf-like certificates) Parse returns *ErrMultipleEntries carrying the entries' aliases/subjects; use ParseEntry (JKS/JCEKS) to select one.
Parse is the observability-free wrapper over ParseContext; see it for the optional logging/tracing variant.
func ParseContext ¶
func ParseContext(ctx context.Context, data []byte, passphrase string, opts ...Option) (Bundle, error)
ParseContext is Parse with optional, opt-in observability. With no opts it behaves exactly like Parse and emits nothing. WithLogger and WithTracing enable structured logging and an OpenTelemetry span ("certkit.Parse") respectively; neither ever records the passphrase, key material, or raw input bytes.
func ParseEntry ¶
ParseEntry decodes the named alias of a JKS/JCEKS keystore into a Bundle. Use it after Parse reports *ErrMultipleEntries for a multi-alias keystore. It is the observability-free wrapper over ParseEntryContext.
func ParseEntryContext ¶
func ParseEntryContext(ctx context.Context, data []byte, passphrase, alias string, opts ...Option) (Bundle, error)
ParseEntryContext is ParseEntry with optional, opt-in observability (span "certkit.ParseEntry"). See ParseContext for the option semantics; the alias name, passphrase, and key material are never logged or placed on the span.
func Rotate ¶ added in v1.1.0
Rotate generates a fresh keypair and self-signed certificate, returning it as a brand-new Bundle for graceful-overlap rotation: callers keep serving current while next is distributed, then cut over. Rotate never mutates current and never reuses its private key.
If opts is the zero value, CommonName and DNSNames are derived from current's metadata so a caller can rotate without restating them.
type ErrMultipleEntries ¶
type ErrMultipleEntries struct {
Aliases []string
}
ErrMultipleEntries is returned by Parse when a container holds more than one distinct end-entity entry (e.g. a multi-alias JKS/JCEKS keystore or a PKCS#7 bag with more than one leaf-like certificate) and the caller must pick one explicitly -- via ParseEntry for JKS/JCEKS.
func (*ErrMultipleEntries) Error ¶
func (e *ErrMultipleEntries) Error() string
Error implements the error interface.
type Format ¶
type Format int
Format identifies a certificate/key container format.
Supported container formats.
Unknown is returned by DetectFormat when the input cannot be classified.
func DetectFormat ¶
DetectFormat returns a best-effort guess of the container format of data. It returns Unknown if no format could be identified. Parse does not rely on this being authoritative -- it dispatches on the hint but falls back to trying other parsers.
type GenOpts ¶ added in v1.1.0
type GenOpts struct {
// CommonName is the certificate Subject's common name. Required.
CommonName string
// DNSNames populates the certificate's Subject Alternative Names.
DNSNames []string
// TTL is the certificate lifetime, measured from the time of
// generation. A zero value defaults to 365 days.
TTL time.Duration
// KeyBits is the RSA modulus size in bits. A zero value defaults to
// 2048.
KeyBits int
}
GenOpts configures GenerateSelfSigned and Rotate.
type Meta ¶
type Meta struct {
Subject string
Issuer string
SANs []string // DNS names + IP addresses
NotBefore time.Time
NotAfter time.Time
SerialNumber string
FingerprintSHA256 string
KeyAlgorithm string
KeyBits int
IsCA bool
}
Meta holds certificate metadata derived from a Bundle's leaf certificate.
type Option ¶
type Option func(*obsConfig)
Option configures the optional logging/tracing of a context-aware call.
func WithLogger ¶
WithLogger injects a go-log neutral Logger. Success is logged at debug level and failure at error level (with the typed error); both carry only non-sensitive attributes.
func WithTracing ¶
func WithTracing() Option
WithTracing enables a single OpenTelemetry span around the operation, started from the global TracerProvider. It is a no-op unless the application has installed a provider.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Command example demonstrates parsing a PEM certificate bundle into a certkit.Bundle, printing its metadata, and converting it to another container format.
|
Command example demonstrates parsing a PEM certificate bundle into a certkit.Bundle, printing its metadata, and converting it to another container format. |