There are several questions that crop up frequently relating to PKCS#12 support in OpenSSL. After replying to the same things several times I decided to write an FAQ to deal with them.
Note: from this version onwards I am not including details of
the obsolete PKCS#12 patch, the external PKCS#12 program or ca-fix. The
FAQ was getting confusing when I needed to include several versions of
the same commands for older versions of OpenSSL and SSLeay. Therefore
all commands assume you are using OpenSSL
0.9.5a
or
later. It is also assumed that you are using MSIE 5 or
later, there are some security issues with MSIE4 mentioned in the older FAQ.
You may have noticed that this version looks different. My thanks to
Barry Rountree for
reformatting
it.
nsCertType=email,client. Alternatively you
can comment out the nsCertType line completely to get S/MIME and SSL
client use (which is probably what you want).Otherwise things proceed as per the instructions for the signing tools. It appears that (unlike email) Netscape does not install an object signing CA certificate as untrusted if it does not recognize it. This means that if you want your object signing CA to be recognized its certificate needs to be already loaded and trusted in the user's database.
Q. I'm having real problems getting a certificate into Netscape. Help!
CA.pl -newca.openssl x509 -in demoCA/cacert.pem -days 1024 -out
cacert.pem -signkey demoCA/private/cakey.pemCA.pl -newreqCA.pl -signreqopenssl pkcs12 -export -in newcert.pem -inkey newreq.pem
-certfile demoCA/cacert.pem -name "MY CERTIFICATE" -out mycert.p12To generate more certificates just repeat steps 5 to 10.
Alternatively if you want to use the key for signing only then you may be in luck: check out the signature key question.
openssl x509 -in cacert.pem -days 1024 -out newca.pem
-signkey private/cakey.pem
and then replacing cacert.pem with newca.pem.
1. Create a CA. E.g. use CA.pl -newca.
2. Extend the CA expiry date with e.g.:
openssl x509 -in newca.pem -days 1024 -out cacert.pem
-signkey demoCA/private/cakey.pem
3. Replace the CA certificate (demoCA/cacert.pem) with the one created
above.
4. If you are using MSIE5 then skip to 7, otherwise create a
certificate file with:
openssl x509 -in demoCA/cacert.pem -outform DER -out
cacert.der
You now need to open the cacert.der file with MSIE as MIME type
application/x-x509-ca-cert. Files with extension .der seem to
be already registered as this type so just transfer cacert.der to the
PC and double click on it. You should then be able to open the file
and set it to be trusted as a CA. Check it appears in
View->Internet Options->Content->Authorities.
5. If you are using the export version edit the default_bits
section in openssl.cnf to set the key size to 512 bits.
6. Create a new certificate request with CA.sh -newreq
7. Sign the request with CA.sh -signreq
8 .Create a PKCS#12 file with:
openssl pkcs12 -export -in newcert.pem -inkey newreq.pem
-name "MY CERTIFICATE" -certfile demoCA/cacert.pem -out mycert.p12
If you are using MSIE5 then also add the maciter option to make
the PKCS#12 file more secure.
9. Choose Tools->Internet
Options->Content->Certificates->Import and follow the
instructions in the wizard. If you haven't got the root CA added to
the database it will be added automatically, but you need to confirm
that you want to do this.
10. The certificate should now be present in the list.
11. If you are using Outlook Express or Outlook try signing mail with
the certificate.
12. After you are satisfied that it works OK check the private
key
security
question and increase the security level of your
newly imported private key.
If you want to generate more certificates repeat steps 7 to 10.
My thanks to Miguel Angel Fraga for some initial suggestions about how this might be done.
1. Make sure you have the latest Authenticode tools. The only
version of signcode this is known to work with is 5.101.1663.1
several older versions most definitely do notwork (I had all
manner of problems until I tried the latest version). The easiest
place to get these is in the Microsoft
Internet
Client
SDK. You need to use MSIE to download this
properly. The latest tools are part of the common stuff that is
downloaded whenever you download anything, so select a small package
to download and you'll get the tools as well. They are in the bin
directory.
2. Generate a CA certificate, trust it for software publishing and
import
an
end user PKCS#12 file with the appropriate values in the
certificate (if unsure how to so this just read the MSIE
certificate
help
question). If you wish to
use a key larger than 512 bits in size and you do not have the
domestic security patch installed then check the signature
key
question. If you've already got a CA installed but you
haven't set it for software publishing then you can just select
View->Internet->Options->Content->Authorities and select software
publishing
in the listbox headed Issuer Type. Find your
CA in the window and check the box next to it, assuming you
haven't already done so. Increase the security level of the private
key see the private key security question. The
comments made there are doubly important for Authenticode private
keys: it is strongly advised that you use high security and
pick a good password. Never ever click on the remember
password option when you access an Authenticode key.
3. Find out the commonName (CN) of your user certificate. If
unsure you can use:
openssl x509 -in cert.pem -noout -subject
In case it isn't obvious the CN is the bit after CN= part. The file cert.pem
if the user certificate file. If you don't have it you can always
extract it from the PKCS#12 file with:
openssl pkcs12 -in myfile.p12 -clcerts -nokeys -out cert.pem
4. You should now be able to sign a something with:
signcode -cn "My Object Cert" file.dll
5. Test out the file with:
chktrust file.dll
A nice friendly dialog box should appear letting you examine who it
thinks signed it and allowing all sorts of info to be displayed.
openssl pkcs7 -inform DER -in mycert.spc -print_certs -out
certs.pem openssl pkcs12 -in mycert.p12 -nokeys -out certs.pem
Then use the OpenSSL program crl2pkcs7 to generate a DER
encoded PKCS#7 file:
openssl crl2pkcs7 -nocrl -certfile certs.pem -outform DER
-out mycert.spc
You can then use the file mycert.spc just like any other SPC
file.
That's the easy way. Slightly harder is to convert each
certificate to DER form with:
openssl x509 -in cert1.pem -outform DER -out cert1.der
Then use the supplied cert2spc tool:
cert2spc cert1.der cert2.der ... mycert.spc
signcode -spc mycert.spc -k "MY KEY" file.dll pvk -in key.pem -topvk -out key.pvk
You would then use this with signcode with:
signcode -spc mycert.spc -v key.pvk file.dll
Using this method there is no need to import or generate the PKCS#12 file at all. Simply use the private keys and certificates converted directly from OpenSSL.
If you wish to use a PVK file (and this is discouraged due to the weak encryption) then you can just use the sig option to create a PVK file with the private key marked as a signature key, future versions of the PVK conversion program will have this option set by default. For example:
pvk -sig -in key.pem -topvk -out key.pvk
If you wish to keep the key in the registry and import a PKCS#12 file then you need version 0.52 or later of my PKCS#12 program. If you supply the keysig option a special attribute is set in the PKCS#12 file and the private key is imported as a signature key. For example:
openssl pkcs12 -export -in newcert.pem -inkey newreq.pem
-name "MY CERTIFICATE" -certfile demoCA/cacert.pem -out mycert.p12
-keysig
This keysig option is off by default: this is because
signature keys cannot be used for S/MIME encryption.
While OpenSSL can handle PKCS#5 v1.5 and v2.0 in PKCS#12 files other implementations may not.
I've decided to clarify the supported encryption of various
implementations with a table.
| Software and mode. | Certificate encryption | Private key encryption |
| MSIE4 (domestic and export versions) PKCS#12 export. | 40 bit RC2. | 40 bit RC2 |
| MSIE4, 5(domestic and export versions) PKCS#12 import. | 40 bit RC2, 3 key triple DES. | 40 bit RC2, 3 key triple DES. |
| MSIE5 PKCS#12 export. | 40 bit RC2 | 3 key triple DES with SHA1 (168 bits) |
| Netscape Communicator (domestic and export versions) PKCS#12 export | 40 bit RC2. | 3 key triple DES with SHA1 (168 bits) |
| Netscape Communicator (export version) PKCS#12 import. | 40 bit ciphers only. | All. |
| Netscape Comminicator (domestic or fortified version) PKCS#12 import. | All. | All. |
| OpenSSL PKCS#12 code. | All. | All. |
If you want to see for yourself what is used try the info option to pkcs12.
By default the strongest encryption supported by all implementations is used in the pkcs12 application: 3DES for private keys and RC2-40 for certificates. The descert option allows certificates to be encrypted with 3DES as well.
It should be noted that while many versions of Netscape will import files using a variety of algorithms MSIE seems to support 40 bit RC2 and triple DES only.
You can also use the keypbe and certpbe command line options to specify other encryption algorithms for private keys and certificates.
It should be noted that superencryption (that is placing one encrypted structure within another) .may not work with either browser.
As for the OpenSSL output files, the encryption is whatever you set it to (default triple DES).
Q. What's the MAC it keeps saying is OK?
A. This is an integrity check. When used with the correct
password it can be used to verify that the file has not been
corrupted.
My pkcs12 application (and NS/MSIE) currently uses the same password
for integrity (MAC) and privacy (encryption) by default. If you use
the twopass option you can set and input separate passwords:
such files cannot be imported into current versions of MSIE or NS.
A. By default I set both iteration counts to 2048. If you use the -nomaciter option the MAC iteration count is also set to 1 some software such as MSIE4 needs this option because it does not support mac iteration counts. If you use the noiter option the iteration count is set to 1: since this makes dictionary attacks on the password easier this is not recommended.
MSIE5 uses 2000 for the encryption iteration count. If you have the 'enable strong protection' option checked then it uses 2000 for the MAC count otherwise it uses 1 (for compatability with earlier versions of MSIE).
NS will happily import files with MAC and encryption iteration counts.
NS uses the name in the list box for the friendly name and the key id is the SHA-1 hash of the certificate.
MSIE uses the key container name (a weird bunch of digits which looks suspiciously like a GUID) for the friendly name and 00 00 00 01 for the key id.
Some more recent versions of Netscape browsers using PSM can contain multiple private keys and certificates in a single PKCS#12 file. OpenSSL can parse such files and extract all the keys and certificates but not create them (yet).
The PKCS#12 files exported by MSIE follow the traditional rules. However some recent tests show that some versions of MSIE can import much simpler PKCS#12 files, in particular it can handle files without the MAC and even without any certificates. MSIE gives an alert box saying such files are "empty" but still imports the private key into CryptoAPI.