A .pfx or .p12 file is one ASN.1 structure, BER-encoded
(see DER), defined by PKCS #12 version 1.1, published as
RFC 7292. It wraps keys and certificates in layers: a MAC over everything, a list of
content blocks that may each be encrypted, and inside them typed “bags”. The OIDs from
PKCS #5, #7 and #9 that it uses are given here too. Sources: RFC 7292 (sections 4 and 5,
appendices B to D), RFC 5652 for the content type OIDs, RFC 2985 for the PKCS #9
attributes and the certTypes and crlTypes arcs, RFC 8018 for
PBES2 and PBKDF2, RFC 3565 for the AES OIDs, and RFC 9579 for PBMAC1.
PFX
PFX ::= SEQUENCE {
version INTEGER {v3(3)}(v3,...),
authSafe ContentInfo,
macData MacData OPTIONAL }
MacData ::= SEQUENCE {
mac DigestInfo, -- digest algorithm and the MAC value
macSalt OCTET STRING,
iterations INTEGER DEFAULT 1 }
versionis 3 (RFC 7292 section 4).authSafeis a ContentInfo of type data (1.2.840.113549.1.7.1) in password integrity mode, or signedData (1.2.840.113549.1.7.2) in public-key integrity mode. Its content is the BER encoding of an AuthenticatedSafe (RFC 7292 section 4.1; OIDs from RFC 5652).macData, present only in password integrity mode, is an HMAC over that content, keyed from the password, the salt and the iteration count. RFC 7292 section 5.1 allows SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, SHA-512/224 or SHA-512/256 as the hash, named inmac. The default iteration count of 1 is “for historical reasons and its use is deprecated”; RFC 7292 points to NIST SP 800-132 for choosing a count.
Authenticated safe and safe contents
AuthenticatedSafe ::= SEQUENCE OF ContentInfo. Each ContentInfo is
one of (RFC 7292 sections 4.1 and 5.1; OIDs from RFC 5652 section 12.1):
| Content type | OID | Holds |
|---|---|---|
| data | 1.2.840.113549.1.7.1 | An OCTET STRING with the BER encoding of SafeContents, not encrypted. |
| encryptedData | 1.2.840.113549.1.7.6 | SafeContents encrypted with a key from the password. OpenSSL’s PKCS12_create stores certificates this way (see below). |
| envelopedData | 1.2.840.113549.1.7.3 | SafeContents encrypted to a recipient’s public key. |
SafeContents ::= SEQUENCE OF SafeBag
SafeBag ::= SEQUENCE {
bagId OBJECT IDENTIFIER,
bagValue [0] EXPLICIT ANY DEFINED BY bagId,
bagAttributes SET OF PKCS12Attribute OPTIONAL }
The SafeBag definition is simplified from RFC 7292 section 4.2, which writes
bagId and bagValue with information object classes.
Bag types
Bag OIDs are bagtypes = {pkcs-12 10 1}, where
pkcs-12 is 1.2.840.113549.1.12 (RFC 7292 section 4.2 and
appendix D).
| OID | Bag | Value |
|---|---|---|
1.2.840.113549.1.12.10.1.1 | keyBag | A PKCS #8 PrivateKeyInfo, not encrypted. |
1.2.840.113549.1.12.10.1.2 | pkcs8ShroudedKeyBag | A PKCS #8 EncryptedPrivateKeyInfo. OpenSSL’s PKCS12_create stores the private key this way. |
1.2.840.113549.1.12.10.1.3 | certBag | SEQUENCE { certId OID, certValue [0] EXPLICIT }; see below. |
1.2.840.113549.1.12.10.1.4 | crlBag | SEQUENCE { crlId OID, crlValue [0] EXPLICIT }: x509CRL, {crlTypes 1} = 1.2.840.113549.1.9.23.1, is a DER X.509 CRL in an OCTET STRING. |
1.2.840.113549.1.12.10.1.5 | secretBag | SEQUENCE { secretTypeId OID, secretValue [0] EXPLICIT }: any other secret. |
1.2.840.113549.1.12.10.1.6 | safeContentsBag | A nested SafeContents. |
Certificate types sit under certTypes = {pkcs-9 22} and
CRL types under crlTypes = {pkcs-9 23}, where
pkcs-9 is 1.2.840.113549.1.9 (RFC 2985 appendix A; the
types themselves are in RFC 7292 sections 4.2.3 and 4.2.4).
| certId | Certificate |
|---|---|
1.2.840.113549.1.9.22.1 | x509Certificate: a DER X.509 certificate in an OCTET STRING |
1.2.840.113549.1.9.22.2 | sdsiCertificate: a base64 SDSI certificate in an IA5String |
RFC 7292 section 4.2 names two bag attributes, both from PKCS #9 (RFC 2985 sections 5.5.1, 5.5.2 and appendix A):
| Attribute OID | Name | Value |
|---|---|---|
1.2.840.113549.1.9.20 | friendlyName | A single BMPString: “a user-friendly name of the object it belongs to”. OpenSSL’s PKCS12_create sets it from its name argument or the certificate’s alias. |
1.2.840.113549.1.9.21 | localKeyId | A single OCTET STRING identifying a key, “only to be used locally in applications”. OpenSSL’s PKCS12_create puts the same value, the SHA-1 digest of the certificate, on the key bag and its certificate bag. |
RFC 7292 allows other attributes (“Other attributes are allowed”) and says an
importing application “should ignore any object identifiers that it is not familiar
with” (section 5.2). One example is Microsoft’s CSP name,
1.3.6.1.4.1.311.17.1 (OpenSSL objects.txt), which OpenSSL’s
pkcs12 -CSP option writes.
Password-based encryption
RFC 7292 appendix C defines its own password-based algorithms, kept for backwards
compatibility with PKCS #12 v1.0 and not recommended. All use SHA-1, under
pkcs-12PbeIds = {pkcs-12 1}, with parameters
SEQUENCE { salt OCTET STRING, iterations INTEGER }:
| OID | Algorithm |
|---|---|
1.2.840.113549.1.12.1.1 | pbeWithSHAAnd128BitRC4 |
1.2.840.113549.1.12.1.2 | pbeWithSHAAnd40BitRC4 |
1.2.840.113549.1.12.1.3 | pbeWithSHAAnd3-KeyTripleDES-CBC |
1.2.840.113549.1.12.1.4 | pbeWithSHAAnd2-KeyTripleDES-CBC |
1.2.840.113549.1.12.1.5 | pbeWithSHAAnd128BitRC2-CBC |
1.2.840.113549.1.12.1.6 | pbewithSHAAnd40BitRC2-CBC (so spelt in RFC 7292) |
For these, keys and IVs come from the password by the PKCS #12 key derivation of
RFC 7292 appendix B, which also makes the MAC key for macData (appendix
B.4): the password is taken as a BMPString with a null terminator (each character as
2 bytes, most significant first, then two zero bytes; appendix B.1), and an ID byte
chooses what is made: 1 an encryption key, 2 an IV, 3 a MAC key (appendix B.3).
The key derivation of appendix B.2
RFC 7292 calls this method deprecated for encryption in new files (use PBES2 with PBKDF2) but it still makes the MAC key. Let H be the hash, u its output size and v the block size its compression function takes, in bits: MD2 and MD5 128/512; SHA-1 160/512; SHA-224 224/512; SHA-256 256/512; SHA-384 384/1024; SHA-512 512/1024; SHA-512/224 224/1024; SHA-512/256 256/1024. Let r be the iteration count, s the salt, p the formatted password (the BMPString bytes above), n the bits wanted and ID the purpose byte. All lengths are multiples of 8.
- D, the diversifier: v/8 copies of the ID byte.
- S: copies of the salt joined, to a length of v × ⌈s/v⌉ bits (the last copy truncated); empty if the salt is empty.
- P: the same for the password; empty if the password is empty.
- I = S ‖ P.
- c = ⌈n/u⌉.
- For i = 1 to c:
- Ai = Hr(D ‖ I): hash D ‖ I, then hash the result again, r times in all.
- B: copies of Ai joined to a length of v bits (the last copy truncated).
- Treat I as k blocks I0 … Ik−1 of v bits each, k = ⌈s/v⌉ + ⌈p/v⌉, and replace each Ij with (Ij + B + 1) mod 2v.
- A = A1 ‖ A2 ‖ … ‖ Ac.
- The result is the first n bits of A.
For a DES-family key, make 64 bits and then set the parity bits (the same for 2-key and 3-key triple DES and any key with built-in parity). Appendix B.4: the MAC key uses the hash that does the MACing and has that hash’s output length (160, 224, 256, 384 or 512 bits for SHA-1, SHA-224, SHA-256, SHA-384 or SHA-512; the RFC also lists SHA-512/224 and SHA-512/256). Appendix C recommends an iteration count of 1024 or more for password privacy mode.
Structures used
MacData ::= SEQUENCE {
mac DigestInfo, -- from PKCS #7 (RFC 2315)
macSalt OCTET STRING,
iterations INTEGER DEFAULT 1 } -- default kept for history; its use is deprecated
DigestInfo ::= SEQUENCE {
digestAlgorithm AlgorithmIdentifier,
digest OCTET STRING }
SafeBag ::= SEQUENCE {
bagId OBJECT IDENTIFIER,
bagValue [0] EXPLICIT ANY DEFINED BY bagId,
bagAttributes SET OF PKCS12Attribute OPTIONAL }
PKCS12Attribute ::= SEQUENCE { -- compatible with the X.500 Attribute type
attrId OBJECT IDENTIFIER,
attrValues SET OF ANY DEFINED BY attrId }
pkcs-12PbeParams ::= SEQUENCE { -- for the six pkcs-12PbeIds above
salt OCTET STRING,
iterations INTEGER }
PBES2-params ::= SEQUENCE { -- id-PBES2, RFC 8018
keyDerivationFunc AlgorithmIdentifier, -- id-PBKDF2 with PBKDF2-params
encryptionScheme AlgorithmIdentifier } -- left to the application
PBKDF2-params ::= SEQUENCE {
salt CHOICE { specified OCTET STRING, otherSource AlgorithmIdentifier },
iterationCount INTEGER (1..MAX),
keyLength INTEGER (1..MAX) OPTIONAL,
prf AlgorithmIdentifier DEFAULT algid-hmacWithSHA1 } -- parameters NULL
PBMAC1-params ::= SEQUENCE { -- id-PBMAC1 = 1.2.840.113549.1.5.14
keyDerivationFunc AlgorithmIdentifier, -- id-PBKDF2 with PBKDF2-params
messageAuthScheme AlgorithmIdentifier } -- an HMAC, left to the application
The PBKDF2 pseudorandom functions are HMAC algorithms under
1.2.840.113549.2: .7 hmacWithSHA1 (the default),
.8 SHA-224, .9 SHA-256, .10 SHA-384,
.11 SHA-512, .12 SHA-512/224 and .13
SHA-512/256 (RFC 8018). The friendlyName and localKeyId attributes are from PKCS #9
(RFC 2985 section 5.5, each with a single value).
PBMAC1 in PKCS #12 (RFC 9579). The DigestAlgorithmIdentifier inside
MacData.mac may be id-PBMAC1; its parameters must be present
and be PBMAC1-params. The digest value is then the PBMAC1 result over the
authSafe contents, macSalt and iterations are
ignored (but, for compatibility, the salt should not be empty and the iterations should
be positive), passwords are encoded as in appendix B.1, an implementation must support
PBKDF2 with HMAC-SHA-256 as both the PRF and the integrity check, and the
keyLength field of PBKDF2-params must be present (a reader must refuse
one without it) and should equal the HMAC output size, for example 32 bytes for
HMAC-SHA-256.
The alternative is PBES2 (id-PBES2 = {pkcs-5 13} =
1.2.840.113549.1.5.13) with PBKDF2 (id-PBKDF2 =
{pkcs-5 12} = 1.2.840.113549.1.5.12), both from RFC 8018,
and a cipher such as AES-256-CBC (id-aes256-CBC =
2.16.840.1.101.3.4.1.42, RFC 3565). OpenSSL’s PKCS12_create
defaults to PBES2 with PBKDF2 and AES-256-CBC for both keys and certificates, with
HMAC-SHA-256 for the MAC, while the MAC key is still derived by the appendix B method
(its documentation calls it PKCS12KDF). RFC 9579 adds PBMAC1 from RFC 8018 as a MAC
that uses PBKDF2 instead.
Passwords and parameters of PBES1 and PBES2 in a file
The PKCS #12 algorithms of appendix C (1.2.840.113549.1.12.1.1 to .6) use the
appendix B derivation with the BMPString password: ID 1 for the key and ID 2 for the IV (8 bytes for triple DES
and RC2, none for RC4). Their key sizes are 16 bytes for RC4-128, 5 for RC4-40, 24 for three-key triple DES, 16 for
two-key triple DES (used as K1, K2, K1), 16 for RC2-128 and 5 for RC2-40, with the RC2 effective key length equal to
the key size in bits. Ciphertext is padded as PKCS #7 for the CBC algorithms; RC4 has no padding, so only the MAC or
the structure of the decrypted bytes says whether the password was right.
The older PKCS #5 version 1.5 algorithms are also found in a shrouded key bag:
pbeWithMD5AndDES-CBC (1.2.840.113549.1.5.3), pbeWithSHA1AndDES-CBC
(.5.10), pbeWithMD5AndRC2-CBC (.5.6) and pbeWithSHA1AndRC2-CBC
(.5.11). The key and IV are the first and second 8 bytes of PBKDF1 (RFC 8018 section 5.1: the hash
applied to password then salt, then to its own output, iterations times in all), and the RC2 key has a
64-bit effective length.
PBES2 (RFC 8018) derives the key with PBKDF2 or scrypt (1.3.6.1.4.1.11591.4.11, RFC 7914, with
parameters SEQUENCE { salt, costParameter N, blockSize r, parallelizationParameter p, keyLength OPTIONAL }) and
decrypts with the encryptionScheme: AES-128, AES-192 or AES-256 in CBC mode
(2.16.840.1.101.3.4.1.2, .22, .42; the parameter is the 16-byte IV), triple DES
(1.2.840.113549.3.7, 8-byte IV), DES (1.3.14.3.2.7) or RC2 (1.2.840.113549.3.2,
whose parameters are the IV alone or SEQUENCE { rc2ParameterVersion, iv }, the version 160 meaning 40 bits,
120 64 bits, 58 128 bits and a larger number the number of bits). The key length is
keyLength when present, otherwise the cipher’s; a keyLength longer than the cipher’s
(a file from an old NSS writes 32 for AES-128) is not by itself an error, as the first bytes of a longer PBKDF2
output are the shorter output.
The password for PBKDF1 and PBKDF2 is the UTF-8 text in files made by OpenSSL, and a BMPString with a null terminator, as for the PKCS #12 derivation, in some files made by NSS (RFC 9579 section 6 says every password in a PKCS #12 file is of that second form). A reader that has a password tries the UTF-8 form first and then the BMPString forms.
The MAC and its check
In password integrity mode the macData digest is a MAC over the content octets of the
authSafe: the bytes inside the OCTET STRING that holds the AuthenticatedSafe, without that OCTET
STRING’s tag and length. When the OCTET STRING is written in pieces (a constructed OCTET STRING, as BER writers
produce it) the pieces are joined first, so the same MAC holds however the content is framed.
- Take the hash named in
mac.digestAlgorithm: SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, SHA-512/224 or SHA-512/256. - Make the key with the appendix B derivation (above) with ID 3, the password,
macSaltanditerations, as many bytes as the hash gives out. - Compute HMAC with that hash and key over the content octets and compare the result with
mac.digest. They are equal when the password is right and the content is unchanged; a failure does not say which of the two is the cause.
The password as bytes
The key derivation takes the password as a BMPString with a null terminator: each character as two bytes, most
significant first, then 00 00. For an empty password there are two forms in files:
the empty string, which is just the two terminator bytes, and no password at all, which is no bytes. They give
different keys, so a reader that has an empty password tries both. A password with characters outside ASCII is
written as UTF-16. Older OpenSSL versions instead took the UTF-8 bytes and widened each byte to a two-byte
character; current OpenSSL still opens such files when the UTF-16 reading fails, and prints “using broken
algorithm”.
PBMAC1 (RFC 9579)
When digestAlgorithm is id-PBMAC1 the parameters are PBMAC1-params. The key is the
PBKDF2 output of the length in keyLength (which must be present), and the MAC is the HMAC named in
messageAuthScheme over the same content octets. macSalt and iterations
are not used. Section 6 of RFC 9579 says the password is a BMPString with a null terminator, but the example
files in RFC 9579 appendix A (A.1 to A.3 verify, A.4 and A.5 must fail, A.6 has no keyLength and
must be refused) and the PBMAC1 files made by OpenSSL, GnuTLS and NSS all verify with the password as plain UTF-8
bytes handed to PBKDF2, and that is how a reader checks them.
Private keys: PKCS #8
A keyBag holds a PrivateKeyInfo and a pkcs8ShroudedKeyBag an EncryptedPrivateKeyInfo
that decrypts to one (RFC 5208, RFC 5958):
PrivateKeyInfo ::= SEQUENCE {
version INTEGER, -- 0, or 1 when publicKey is present
privateKeyAlgorithm AlgorithmIdentifier,
privateKey OCTET STRING, -- the key, in the form the algorithm defines
attributes [0] IMPLICIT Attributes OPTIONAL,
publicKey [1] IMPLICIT BIT STRING OPTIONAL }
EncryptedPrivateKeyInfo ::= SEQUENCE {
encryptionAlgorithm AlgorithmIdentifier, -- a PBES1 or PBES2 algorithm, see above
encryptedData OCTET STRING } -- the PrivateKeyInfo, encrypted
| privateKeyAlgorithm | Contents of privateKey |
|---|---|
rsaEncryption 1.2.840.113549.1.1.1 (parameters NULL); RSASSA-PSS 1.2.840.113549.1.1.10 | RSAPrivateKey (RFC 8017 appendix A.1.2): version, modulus, publicExponent, privateExponent, prime1, prime2, exponent1, exponent2, coefficient (and otherPrimeInfos when version is 1). |
ecPublicKey 1.2.840.10045.2.1, parameters the curve OID | ECPrivateKey (RFC 5915): version 1, privateKey OCTET STRING (the private value), [0] parameters (optional, the curve again), [1] publicKey BIT STRING (optional, the public point). |
id-Ed25519 1.3.101.112, id-Ed448 1.3.101.113, id-X25519 1.3.101.110, id-X448 1.3.101.111 | CurvePrivateKey (RFC 8410): an OCTET STRING of the private value (32 bytes for Ed25519 and X25519, 57 for Ed448, 56 for X448), inside the privateKey OCTET STRING. The public key is not stored unless version is 1. |
dsa 1.2.840.10040.4.1, parameters SEQUENCE { p, q, g } | An INTEGER, the private value x. |
ML-DSA 2.16.840.1.101.3.4.3.17 to .19, ML-KEM 2.16.840.1.101.3.4.4.1 to .3 | The algorithm’s own key encoding (a seed, an expanded key, or both); the viewer names the algorithm and shows the size. |
The secret numbers in these structures are key material. The viewer lists each as “key material, N bytes” and does not print it; see the viewer.
A file made by OpenSSL
With a key, its certificate and default settings, OpenSSL’s
PKCS12_create (crypto/pkcs12/p12_crt.c) builds:
- PFX, version 3, with MacData.
- authSafe: data, holding an AuthenticatedSafe of two ContentInfos.
- First: encryptedData, holding SafeContents with one certBag for the certificate and one for each extra CA certificate passed in.
- Second: data, holding SafeContents with one pkcs8ShroudedKeyBag for the private key.
- The key bag and its certificate bag share a localKeyId (the certificate’s SHA-1 digest) and, when a name is given or the certificate has an alias, a friendlyName.
The viewer
Drop a .p12 or .pfx file on the explorer and it is shown as a structure tree with a
node for every tag, every length and every value, from the PFX SEQUENCE down to the last INTEGER of a
certificate, plus tables that list what is inside. A file is taken for PKCS #12 only when it is a BER or DER
SEQUENCE of an INTEGER 3, a ContentInfo of type data or signedData, and optionally a MacData; a Java keystore,
a DER certificate or key, or anything else that happens to use the name is not.
Tables
| Section | What it lists |
|---|---|
| PKCS #12 | Format and version; integrity mode, the MAC algorithm, salt, iterations and value, and the result of checking it; the contents, bag, certificate and key counts; the encryption algorithms; the state of the password; the note on private keys. |
| Contents | Each ContentInfo of the AuthenticatedSafe: type, whether it is encrypted and decrypted, the number of bags, size and protection. A decrypted part has an Open button. |
| Bags | Every SafeBag, nested ones included: its safe, number, bag type, friendly name, local key ID, what it holds (a certificate’s subject, a key’s algorithm and size, a nested safe’s bag count) and its attribute names. Long lists are shown a page at a time. |
| Certificates | Subject, issuer, serial number, validity, public key, signature algorithm, role (end entity, CA, self-signed), SHA-1 and SHA-256 fingerprints. |
| Private keys | State, algorithm, structure (RSA modulus bits and exponent, EC curve, Ed25519, DSA, ML-DSA), a SHA-256 fingerprint of the public key where the key holds enough to build it, and which certificate in the file has that public key. |
| Bag attributes | Every attribute of every bag with its OID name and values: friendlyName, localKeyId, Microsoft CSP name, Java trusted key usage and any other. |
| Protection | For every encrypted ContentInfo, every shrouded key and the MAC: the algorithm family, key derivation, salt, iteration count, key length, cipher, IV and state. |
The password
The Password field is the only option. With it the viewer decrypts every encrypted SafeContents and shrouded
key (PKCS #12 derivation with IDs 1 and 2, PBKDF1, PBKDF2 or scrypt, for the ciphers in the
encryption section), checks the MAC, and lists what is inside. The empty password is
always tried first, so a file saved without one opens untouched. Without a password the encrypted parts stay
encrypted and their parameters are shown in full. A password that matches neither the MAC nor the encrypted
parts is reported as wrong, and the structure is still shown. A MAC that matches while a part does not decrypt
is not a wrong password: the part has another password (openssl pkcs12 -twopass) or is encrypted
in a way the structure does not state; the part is listed as not decrypted.
The password is used for the key derivation and nowhere else. It is held in memory for the open file, is not put in any table, text or error, and is forgotten when the file is closed or the password turns out wrong.
Decrypted parts open as files
The bytes of a decrypted part are not in the file, so they cannot be nodes of its tree. They open as files of their own from the Open buttons: a decrypted SafeContents (and an AuthenticatedSafe written in several BER pieces, joined) opens as a tree of its bags, certificates and key structures; a decrypted private key opens as a PKCS #8 PrivateKeyInfo. A key shrouded inside a decrypted SafeContents is decrypted again there when the password is typed in that file’s own Password field.
Private keys are not printed
The viewer shows the structure of a private key and does not print the key. For an RSA key it shows the modulus size, the public exponent and whether the CRT parameters are present; for an EC key the curve and the size of the private value and whether the public point is stored; for Ed25519 the seed length; for DSA the parameter size. The private exponent, primes, EC private value and seeds appear as “key material, N bytes (not shown here)”, in no node text, table, note or error. The fingerprint is of the public key. The bytes themselves remain bytes of the file or the decrypted part: the Hex view shows them like any other bytes and selecting a node marks them there.
Damaged and unusual files
Indefinite lengths and constructed OCTET STRINGs, as BER writers produce them, are read. A truncated file, a bad length or tag, or trailing bytes are shown as far as they go with the unread bytes as their own nodes and a problem listed; every byte of the file belongs to one chain of nodes. A part that uses an algorithm that is not implemented (the GOST algorithms of some Russian tools, MD2-based PKCS #5 schemes) shows its parameters and is listed as not decrypted. An EnvelopedData ContentInfo (public-key privacy mode) is shown as structure only, since decrypting it needs a recipient’s private key; a SignedData authSafe (public-key integrity mode) is shown as structure with its certificates, and the signature is not verified.