Documentation
¶
Overview ¶
Package pkcs12 decodes PKCS#12 (.p12 / .pfx) files, RFC 7292, into a private key, its certificate and the rest of the chain. It reads what OpenSSL 3 and 4 and current Windows write (PBES2 with PBKDF2 and AES, HMAC-SHA-2 MACs, PBMAC1) and what older Windows and Java wrote (3DES and RC2, SHA-1 MACs).
It decodes only, and it is built for untrusted uploads: every length is bounded before it is used, the MAC is checked in constant time before anything is decrypted, and the cost of key derivation is capped (Options).
Index ¶
Constants ¶
const ( DefaultMaxInputSize = 8 << 20 // bytes of input DefaultMaxIterations = 1_000_000 // per key derivation DefaultMaxWork = 5_000_000 // iterations x output blocks, over the whole file DefaultMaxDepth = 16 // nested indefinite-length / constructed-string levels DefaultMaxBags = 1024 // SafeBags, over the whole file DefaultMaxAuthSafes = 32 // ContentInfos in the AuthenticatedSafe // DefaultMaxCertSize bounds one certificate's DER. Real certificates are // 1 to 4 KiB, a 100-name multi-domain one about 8 KiB, and the largest // seen in the wild 10 to 20 KiB; post-quantum ones (ML-DSA-87) are about // 8 KiB. 32 KiB leaves room for all of them. The bound exists because // crypto/x509 allocates 50 times the input for a certificate made of // thousands of tiny names, and that factor cannot be removed from outside. DefaultMaxCertSize = 32 << 10 // DefaultMaxCertBytes bounds all certificate DER of one file together. A // leaf with its chain is 4 to 16 KiB; a bundle of every public root is // about 200 KiB. 512 KiB holds that, and bounds what x509 can allocate for // a hostile file to about 26 MB. DefaultMaxCertBytes = 512 << 10 // DefaultMaxRSABits bounds an RSA modulus. Real keys are 2048 to 4096 bits. // crypto/rsa itself sets no upper limit (it only refuses fewer than 1024 // bits unless GODEBUG says otherwise), and OpenSSL stops at 16384 // (OPENSSL_RSA_MAX_MODULUS_BITS); 8192 is twice the largest key in use. DefaultMaxRSABits = 8192 )
Defaults for Options. A zero (or negative) field means "the default"; no field can switch a bound off.
Variables ¶
var ( // ErrMalformed: the input is not a PKCS#12 file this package can read. ErrMalformed = errors.New("pkcs12: malformed input") // ErrUnsupported: a well-formed file that uses something not implemented // here (public-key integrity mode, RC4, scrypt, an unknown OID...). ErrUnsupported = errors.New("pkcs12: unsupported") // ErrLimit: a resource bound was exceeded (input size, nesting depth, // iteration count, bag count...). The bounds are Options. ErrLimit = errors.New("pkcs12: resource limit exceeded") // ErrIncorrectPassword: the integrity MAC did not verify. A wrong password // and a tampered file are indistinguishable here, by design: the MAC is // the only oracle this package offers. (When the file carries no MAC and // Options.AllowNoMAC is set, a failed decryption is reported the same // way, since nothing else can tell.) ErrIncorrectPassword = errors.New("pkcs12: incorrect password or corrupted file") // ErrNoMAC: the file has no integrity MAC and Options.AllowNoMAC is false. ErrNoMAC = errors.New("pkcs12: file has no MAC (set Options.AllowNoMAC to read it unauthenticated)") // ErrNoPrivateKey: the file holds no private key. ErrNoPrivateKey = errors.New("pkcs12: no private key in file") // ErrMultipleKeys: the file holds more than one private key; which one is // wanted is not guessed. ErrMultipleKeys = errors.New("pkcs12: more than one private key in file") // ErrNoCertificate: no certificate matches the private key. ErrNoCertificate = errors.New("pkcs12: no certificate matches the private key") // ErrPassword: the password cannot be used (invalid UTF-8, too long, or // contradicts Options.NoPassword). ErrPassword = errors.New("pkcs12: unusable password") )
Every refusal Decode returns wraps exactly one of these, so a caller can tell a wrong password from a hostile file from a format this package does not speak. The fuzz targets hold that as an invariant.
Functions ¶
This section is empty.
Types ¶
type Bundle ¶
type Bundle struct {
// PrivateKey is *rsa.PrivateKey, *ecdsa.PrivateKey, ed25519.PrivateKey or
// *ecdh.PrivateKey (X25519): whatever crypto/x509.ParsePKCS8PrivateKey
// returns.
PrivateKey crypto.PrivateKey
// Certificate is the certificate whose public key is PrivateKey's.
Certificate *x509.Certificate
// CACerts are the other certificates, in file order.
CACerts []*x509.Certificate
}
Bundle is what a PKCS#12 file held.
func Decode ¶
Decode reads a PKCS#12 file. See Options for the bounds and for the two ambiguities of the format (a missing MAC, an empty password). Every error wraps one of ErrMalformed, ErrUnsupported, ErrLimit, ErrIncorrectPassword, ErrNoMAC, ErrNoPrivateKey, ErrMultipleKeys, ErrNoCertificate, ErrPassword.
Not supported, and refused as ErrUnsupported: public-key integrity or privacy modes (signedData, envelopedData), RC4, scrypt, PBES1 other than the PKCS#12 ones, SHA-1 inside PBMAC1, and BER (rather than DER) inside a certificate or a PKCS#8 key.
type Options ¶
type Options struct {
// MaxInputSize bounds len(data). Default 8 MiB.
MaxInputSize int
// MaxIterations bounds the iteration count of any one key derivation
// (PKCS#12 B.2 or PBKDF2). A file asking for more is refused with
// ErrLimit before any hashing. Default 1 000 000, about half a second of
// PBKDF2-HMAC-SHA-256 on one modern core; real exports use 1 000 to
// 600 000.
MaxIterations int
// MaxWork bounds the total cost of every key derivation of one Decode, in
// iterations times output blocks, so that many individually acceptable
// derivations cannot add up to a CPU bomb. Default 5 000 000.
MaxWork int64
// MaxDepth bounds BER nesting: indefinite-length elements inside one
// another, and constructed OCTET STRING chunks. Default 16.
MaxDepth int
// MaxBags bounds the SafeBags in the whole file. Default 1024.
MaxBags int
// MaxAuthSafes bounds the ContentInfos of the AuthenticatedSafe. Default 32.
MaxAuthSafes int
// MaxCertSize bounds the DER of one certificate, checked before
// crypto/x509 sees it. Default 32 KiB.
MaxCertSize int
// MaxCertBytes bounds the DER of all certificates of the file together,
// checked before crypto/x509 sees them. Default 512 KiB.
MaxCertBytes int
// MaxRSABits bounds the modulus of an RSA private key, and with it every
// number of the key, checked before crypto/x509 sees it. Default 8192.
MaxRSABits int
// AllowNoMAC lets Decode read a file that has no integrity MAC (made by
// `openssl pkcs12 -nomac`). Nothing then authenticates the content, and a
// wrong password is only noticed by a failed decryption. Default false:
// such a file is refused with ErrNoMAC.
AllowNoMAC bool
// NoPassword says the file was exported with no password at all (a NULL
// password, a zero-length byte string), as opposed to the empty password
// "" (a BMPString holding only its terminator). The two derive different
// keys. With the password "" and NoPassword false, Decode tries the empty
// password first and then the absent one, because exporters disagree about
// which they wrote; with NoPassword true only the absent one is tried, and
// the password argument must be "".
NoPassword bool
}
Options tunes Decode. The zero value, or a nil *Options, is the safe default.