Documentation
¶
Overview ¶
Package crypto provides cryptographic utilities for certificate management.
Index ¶
- Variables
- func CAFingerprintFromPEM(certPEM []byte) (string, error)
- func GenerateCSR(hostname string) (csrPEM, keyPEM []byte, err error)
- func GenerateCSRFromKey(hostname string, keyPEM []byte) (csrPEM []byte, err error)
- func OpenWithAAD(key, ciphertext, aad []byte) ([]byte, error)
- func SealWithAAD(key, plaintext, aad []byte) ([]byte, error)
- func VerifyCAContinuity(oldCAPEM, newCAPEM []byte) error
Constants ¶
This section is empty.
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") )
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 GenerateCSR ¶
GenerateCSR creates a new ECDSA P-256 key pair and returns the CSR (PEM) and private key (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 PEM-encoded ECDSA private key. This is used for certificate renewal where the key pair is reused.
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 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 ¶
This section is empty.