Create a .pfx/.p12 Certificate File Using OpenSSL
Create a .pfx/.p12 Certificate File Using OpenSSL
In cryptography, the PKCS#12 or PFX format is a binary format often used to store all elements of the chain of trust, such as the server certificate, any intermediate certificates, and the private key into a single encryptable file. PFX files are usually found with the extensions .pfx and .p12. PFX files are typically used on Windows and macOS machines to import and export certificates and private keys.
Requirements
- The original private key used for the certificate
- A PEM (.pem, .crt, .cer) or PKCS#7/P7B (.p7b, .p7c) File
- OpenSSL (included with Linux/Unix and macOS, and easily installed on Windows with Cygwin)
The commands below demonstrate examples of how to create a .pfx/.p12 file in the command line using OpenSSL:
PEM (.pem, .crt, .cer) to PFX
openssl pkcs12 -export -out certificate.pfx -inkey privateKey.key -in certificate.crt -certfile more.crt
Breaking down the command:
openssl
– the command for executing OpenSSLpkcs12
– the file utility for PKCS#12 files in OpenSSL-export -out certificate.pfx
– export and save the PFX file as certificate.pfx-inkey privateKey.key
– use the private key file privateKey.key as the private key to combine with the certificate.-in certificate.crt
– use certificate.crt as the certificate the private key will be combined with.-certfile more.crt
– This is optional, this is if you have any additional certificates you would like to include in the PFX file.
ca-bundle-client.crt
in your PFX file. For example:
openssl pkcs12 -export -out certificate.pfx -inkey privateKey.key -in certificate.crt -certfile ca-bundle-client.crt
Difference between .pfx and .cert certificates
What is the difference between .pfx
and .cert
certificate files?
Do we distribute .pfx
or .cert
for client authentication?
回答1
There are two objects: the private key, which is what the server owns, keeps secret, and uses to receive new SSL connections; and the public key which is mathematically linked to the private key, and made "public": it is sent to every client as part of the initial steps of the connection.
The certificate is, nominally, a container for the public key. It includes the public key, the server name, some extra information about the server, and a signature computed by a certification authority (CA). When the server sends its public key to a client, it actually sends its certificate, with a few other certificates (the certificate which contains the public key of the CA which signed its certificate, and the certificate for the CA which signed the CA's certificate, and so on). Certificates are intrinsically public objects.
Some people use the term "certificate" to designate both the certificate and the private key; this is a common source of confusion. I personally stick to the strict definition for which the certificate is the signed container for the public key only.
A .pfx
file is a PKCS#12 archive: a bag which can contain a lot of objects with optional password protection; but, usually, a PKCS#12 archive contains a certificate (possibly with its assorted set of CA certificates) and the corresponding private key.
On the other hand, a .cert
(or .cer
or .crt
) file usually contains a single certificate, alone and without any wrapping (no private key, no password protection, just the certificate).
评论:
PKCS #12
In cryptography, PKCS #12 defines an archive file format for storing many cryptography objects as a single file.
It is commonly used to bundle a private key with its X.509 certificate or to bundle all the members of a chain of trust.
作者:Chuck Lu GitHub |