Introduction
A PFX or P12 file carries a private key together with its certificate, and usually the chain of certificates above it, in one password-protected file. It is the usual way to move a certificate and its key between machines: exporting a certificate with its key from Windows, from macOS Keychain Access or from a browser gives a file in this format.
The format is PKCS #12, first published by RSA Laboratories and now RFC 7292. It grew
out of Microsoft’s earlier PFX format, which is why Windows uses .pfx and
most other systems .p12; the contents are the same.
Structure
The file is DER-encoded ASN.1 (older writers use BER, with
indefinite lengths and an OCTET STRING in pieces), so it starts with the byte 30. The top level is a PFX SEQUENCE holding a version number, 3, the
authenticated content, and usually a MAC computed from the password, which proves the
file has not been changed.
The content is a list of SafeContents, each either plain or encrypted with a key derived from the password. Inside are SafeBags: a certificate bag for each certificate, and a shrouded key bag holding the private key encrypted as PKCS #8. Bags can carry a friendly name and a local key ID that ties a key to its certificate. Older files use weak encryption such as 40-bit RC2 or triple DES; newer ones use AES with PBKDF2.
0PFX SEQUENCE: version 3varies…authSafe SafeContents, some encrypted, holding certificate and key bagsvaries…macData the MAC, its salt and iteration countvaries
Viewer
Drop a .p12 or .pfx file on the explorer to see it as a structure tree with a node
for every tag, length and value, and tables of its contents, bags, certificates, private keys, bag attributes
and protection parameters (how the viewer reads it). Type the
file’s password in the Password field and the encrypted parts are decrypted, the MAC is checked and the
certificates and the private key structure are shown. Private keys are never printed: the viewer shows their
algorithm, sizes, curve, public values and a fingerprint of the public key, and the secret numbers stay bytes
in the Hex view. A decrypted part opens as a file of its own.
It reads the algorithms in use: the PKCS #12 password-based ciphers (3DES, RC2, RC4), PKCS #5 version 1.5, and PBES2 with PBKDF2 (any HMAC) or scrypt and AES, 3DES, DES or RC2; MACs with SHA-1 to SHA-512 and PBMAC1; BER with indefinite lengths; and files with no MAC, no encryption or only a public-key signature.
Not described
Known, not yet covered
- Viewer: the GOST algorithms some Russian tools use in a PKCS #12 file (GOST 28147-89 encryption and GOST R 34.11 MACs) are not implemented: a part using them shows its parameters and is listed as not decrypted. The PKCS #5 version 1.5 algorithms built on MD2 (RFC 8018) are not implemented either.
- Viewer: an EnvelopedData part (public-key privacy mode) is shown as structure only, since decrypting it needs a recipient’s private key; the signature of a SignedData authSafe (public-key integrity mode) is not verified. A MAC over more than 256 MiB of content is not checked, because the hash is computed in one piece; an encrypted part larger than 96 MiB is decrypted in windows when opened but not listed in the tables, and only when its cipher is a CBC one (a larger RC4 part is not decrypted).
- Viewer: a certificate of type sdsiCertificate is shown as its text, and a CRL as structure without its entries named.
Links
Software
- Windows Certificate Import Wizard – built into Windows, with the Certificates console, imports and exports PFX files (Windows)
- Keychain Access – built into macOS, imports and exports P12 files (macOS)
- OpenSSL – command-line toolkit whose
pkcs12command reads and writes these files (Windows, macOS, Linux) - KeyStore Explorer – free key and certificate manager (Windows, macOS, Linux)
- keytool – the command-line key tool that comes with Java (Windows, macOS, Linux)
Developers
- cryptography – library that loads and writes PKCS #12 (Python)
- Bouncy Castle – libraries with PKCS #12 support (Java, C#)
Reference
- RFC 7292 – PKCS #12: Personal Information Exchange Syntax v1.1