Documentation
¶
Overview ¶
Package crypto provides cryptographic utilities for certificate management.
Index ¶
- Constants
- Variables
- func CAFingerprintFromPEM(certPEM []byte) (string, error)
- func FieldSealContext(direction SealDirection, message, field string, bindings ...string) ([]byte, string, error)
- func GenerateCSR(hostname string) (csrPEM, keyPEM []byte, err error)
- func GenerateCSRFromKey(hostname string, keyPEM []byte) (csrPEM []byte, err error)
- func GenerateX25519() (*ecdh.PrivateKey, error)
- func OpenWithAAD(key, ciphertext, aad []byte) ([]byte, error)
- func OpenWithPrivateKey(priv *ecdh.PrivateKey, sealed, aad []byte, info string) ([]byte, error)
- func ParseX25519PublicKey(raw []byte) (*ecdh.PublicKey, error)
- func SealToPublicKey(recipient *ecdh.PublicKey, plaintext, aad []byte, info string) ([]byte, error)
- func SealWithAAD(key, plaintext, aad []byte) ([]byte, error)
- func VerifyCAContinuity(oldCAPEM, newCAPEM []byte) error
- type SealDirection
Constants ¶
const ( // MinSealedLen is the smallest possible valid sealed blob: the // construction overhead plus one plaintext byte. Boundary validators use // it to reject anything that cannot possibly be a sealed blob. MinSealedLen = sealedOverhead + 1 )
Variables ¶
var ( // ErrInvalidKey is returned when the key is not exactly 32 bytes (AES-256). ErrInvalidKey = errors.New("crypto: key must be 32 bytes (AES-256)") // ErrAADRequired is returned when the AAD is empty. Domain-separation AAD is // MANDATORY: a naked AEAD call (nil/empty AAD) is refused by construction, so // two different ciphertext domains can never be confused or cross-decrypted, // and "forgot the AAD" is impossible to do silently. ErrAADRequired = errors.New("crypto: AAD is required (domain separation; no naked AEAD calls)") // ErrMalformedCiphertext is returned when a ciphertext is too short to carry // its prepended nonce. ErrMalformedCiphertext = errors.New("crypto: ciphertext too short") )
var ErrInfoRequired = errors.New("crypto: HKDF info is required (domain separation; no naked seal calls)")
ErrInfoRequired is returned when the HKDF info string is empty. Like the AEAD's mandatory AAD, the domain-separation info is required by construction so two sealing surfaces can never derive the same key from the same recipient.
var ErrUnsupportedKeyType = errors.New("crypto: device identity key must be Ed25519")
ErrUnsupportedKeyType is returned when a stored device key is not Ed25519. The Control Server's CA refuses any other identity key type, so a renewal CSR built from one could never be signed.
Functions ¶
func CAFingerprintFromPEM ¶
CAFingerprintFromPEM returns the lowercase-hex SHA-256 of the certificate's DER bytes. It is byte-for-byte identical to the control server's ca.FingerprintFromPEM, so an operator-delivered pin computed from the control CA (e.g. `openssl x509 -in ca.crt -outform DER | sha256sum`) matches what the agent derives from the CA returned at registration. This is the out-of-band trust anchor for the optional enrollment CA-pin.
func FieldSealContext ¶ added in v0.5.4
func FieldSealContext(direction SealDirection, message, field string, bindings ...string) ([]byte, string, error)
FieldSealContext constructs the shared AAD and HKDF domain used by control and agent for one secret-classified protobuf field. Bindings name the device followed by the relevant action, delivery, terminal session, or username. Length prefixes make the encoding unambiguous without restricting values.
func GenerateCSR ¶
GenerateCSR creates a new Ed25519 key pair and returns the CSR (PEM) and private key (PKCS#8 PEM).
The CSR carries no SANs (no DNSNames, IPAddresses, EmailAddresses, or URIs). Agent certificates are client certs — identified by the deviceID that the Control Server writes into the issued cert's Subject.SerialNumber, not by anything the agent puts in the CSR. The Control Server's CA rejects any CSR with SANs:
internal/ca/ca.go: "CSR must not request subject alternative names"
so including a DNS SAN here fails registration immediately. The hostname is still put in the CSR's CN for operator debuggability (the CA discards the CN and replaces it with the device ID), but the DNS SAN is omitted.
func GenerateCSRFromKey ¶
GenerateCSRFromKey creates a CSR using an existing PKCS#8 PEM-encoded Ed25519 private key. This is used for certificate renewal where the key pair is reused: the CA's renewal proof-of-possession requires the CSR's public key to equal the held certificate's.
func GenerateX25519 ¶ added in v0.5.3
func GenerateX25519() (*ecdh.PrivateKey, error)
GenerateX25519 generates a recipient keypair for SealToPublicKey / OpenWithPrivateKey. The private key's Bytes() form is what a recipient persists (encrypted at rest); reconstruct with ecdh.X25519().NewPrivateKey.
func OpenWithAAD ¶
OpenWithAAD reverses SealWithAAD: it splits the prepended nonce, then authenticates and decrypts under key with the SAME aad. Any authentication failure — a wrong key, a wrong-domain aad, or a tampered ciphertext — returns an error and NO plaintext. A 32-byte key and a non-empty aad are required.
func OpenWithPrivateKey ¶ added in v0.5.3
OpenWithPrivateKey reverses SealToPublicKey: it splits the ephemeral public key, re-derives the sealing key with the SAME info, and authenticates + decrypts under the SAME aad. Any mismatch — a tampered byte anywhere in the blob, a different aad, a different info, or the wrong private key — returns an error and no plaintext.
func ParseX25519PublicKey ¶ added in v0.5.3
ParseX25519PublicKey parses the 32-byte encoding of an X25519 public key (the PublicKey().Bytes() form that crosses the wire).
func SealToPublicKey ¶ added in v0.5.3
SealToPublicKey encrypts plaintext so that only the holder of recipient's private key can read it, binding aad as authenticated context and info as the mandatory domain-separation string. Returns ephemeralPub(32) || nonce || ciphertext || tag.
A fresh ephemeral keypair and a fresh AEAD nonce are generated per call from crypto/rand; an RNG failure fails closed. Both a non-empty aad and a non-empty info are required by construction.
func SealWithAAD ¶
SealWithAAD encrypts plaintext with AES-256-GCM under key, binding aad as additional authenticated data, and returns nonce||ciphertext||tag.
A fresh random 96-bit nonce is generated PER CALL and prepended to the output. The nonce is generated here, never taken from the caller, so nonce reuse under the same key (catastrophic for GCM) cannot be introduced by a caller mistake; a crypto/rand failure fails closed rather than emitting a predictable nonce.
Both a 32-byte key and a NON-EMPTY aad are required: an empty aad is rejected (ErrAADRequired) so every ciphertext is domain-separated. The AEAD tag covers the plaintext AND the aad, so a wrong key, a wrong-domain aad, or any tamper fails authentication in OpenWithAAD.
func VerifyCAContinuity ¶
VerifyCAContinuity reports whether newCAPEM is an acceptable rotation of oldCAPEM: it must be byte-identical to the enrolled CA, OR be a CA that is cross-signed by (chains to) the enrolled CA. An unrelated CA is refused — that is the trust-anchor swap an attacker (or a compromised/MITM'd control origin) would attempt at certificate renewal. A hard CA swap (a new self-signed root not cross-signed by the old one) is intentionally refused too: it requires re-enrollment, not silent adoption over the renewal channel.
Types ¶
type SealDirection ¶ added in v0.5.4
type SealDirection string
SealDirection is the authenticated transport direction of a sealed field.
const ( DirectionAgentToControl SealDirection = "agent-to-control" DirectionControlToAgent SealDirection = "control-to-agent" )