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, 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
Links
Software
- The Windows Certificate Import Wizard and the Certificates console, built into Windows
- Keychain Access, built into macOS
- OpenSSL, whose
pkcs12command reads and writes these files (Windows, macOS, Linux) - KeyStore Explorer (Windows, macOS, Linux)
- keytool, the key tool that comes with Java
Developers
- cryptography, Python library that loads and writes PKCS #12
- Bouncy Castle, Java and C# libraries with PKCS #12 support
Reference
- RFC 7292, PKCS #12: Personal Information Exchange Syntax v1.1