From Fedora Project Wiki

Testing PKCS#11 support

The proposed packaging guidelines say that any program which can accept SSL certificates from a file should also allow them to come from a PKCS#11 token. This page exists to help packagers understand those guidelines and test their packages.

But I don't have any PKCS#11 hardware

You don't need hardware. There are plenty of PKCS#11 providers which are purely software. These include

  • NSS Certificate Database (Firefox, Evolution, Chrome)
  • GNOME keyring
  • SoftHSM

The simplest one to test with is probably GNOME keyring. Obviously not everyone will be running GNOME for their day-to-day usage but it shouldn't be too hard to use GNOME keyring just for a simple test.

But I have the PKCS#11 hardware

Following people might help testing with their hardware:

  • User:tuju CCID, NFC, USB2, USB-C readrs, gov issued id-cards
  • User:kalev CCID readers, gov issued id-cards

Generate a certificate

Useful certificates are actually signed by someone to vouch for the owner of the certificate. If you have one of those (and if you're a Fedora packager, you do) you can use it. Alternatively, just create your own for testing:

$ openssl req -x509 -days 3653 -new -nodes -out testkey.pem -keyout testkey.pem -subj /CN=testkey

Import certificate

The seahorse GUI tool allows you to browse the contents of PKCS#11 tokens and import certificates and keys. If you simply run seahorse under GNOME you should see a 'Gnome2 Key Storage' token listed under the 'Certificates' heading. You can select the 'File'... 'Import' menu item to import a certificate from a file into the GNOME keyring (or indeed any other provider you choose to use).

If you import the testkey.pem generated in the above example, give "testkey" as the label when prompted. If you import your Fedora certificate from ~/.fedora.cert then use your Fedora username (e.g. dwmw2). You are only being asked for the label for the private key, and life is easier if that matches the label of the cert (which is automatically taken from the subject of the cert).

Determine the PKCS#11 URI of your certificate

Unfortunately, seahorse doesn't show the PKCS#11 URI of the objects when you're browsing (GNOME bug #749071). So you'll want to use p11tool to list them and find the URI:

$ p11tool --list-certs --login pkcs11:token=Gnome2%20Key%20Storage
Object 0:
	URL: pkcs11:model=1.0;manufacturer=Gnome%20Keyring;serial=1%3aUSER%3aDEFAULT;token=Gnome2%20Key%20Storage;id=%f5%25%95%6c%95%9b%c3%b4%7f%19%b7%a1%0d%92%a8%b5%a3%57%4b%5f;object=testkey;type=cert
	Type: X.509 Certificate
	Label: testkey
	ID: f5:25:95:6c:95:9b:c3:b4:7f:19:b7:a1:0d:92:a8:b5:a3:57:4b:5f

The interesting part there is the URL. In fact a lot of the information there is redundant; all you need is enough match criteria to uniquely specify the object. So these are suitable URIs for referring to this certificate:

  • pkcs11:token=Gnome2%20Key%20Storage;object=testkey
  • pkcs11:token=Gnome2%20Key%20Storage;id=%f5%25%95%6c%95%9b%c3%b4%7f%19%b7%a1%0d%92%a8%b5%a3%57%4b%5f
  • pkcs11:serial=1:USER:DEFAULT;object=testkey

In fact with some clients like GnuTLS you don't even need to specify the token, although it helps to speed things up by finding it more quickly. You could probably get away with:

  • pkcs11:object=testkey

See if you can use it

Now let's pretend I'm packaging the OpenConnect VPN client. It has fairly reasonable documentation (if I do say so myself) on how to use it with PKCS#11. It looks like it should comply with the Fedora guidelines and just accept a PKCS#11 URI on the command line with the -c option, in place of a filename.

Let's test...

$ openconnect -c 'pkcs11:serial=1:USER:DEFAULT;object=testkey'
Attempting to connect to server
Using client certificate 'testkey'

It'll fail to actually make a VPN connection, but that's expected — that server isn't running an AnyConnect VPN service. The important part is that the VPN client was attempting to use the correct client certificate.

Now let's pretend that I'm packaging curl. Note that curl expects to be given "CERTIFICATE:PASSWORD" in its -E option, so we need to escape any colons in the certificate URI:

$ curl -E 'pkcs11\:serial=1\:USER\:DEFAULT;object=testkey' -I -v
*   Trying
* Connected to ( port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* NSS: client certificate not found: pkcs11:serial=1:USER:DEFAULT;object=testkey

Oops, that 'client certificate not found' doesn't look good. If curl was built with GnuTLS instead of NSS that actually would have worked fine. As it is, let's file bug 1219544 against curl...

Crypto library support


If your package builds against GnuTLS, then in most cases it should Just Work. The common GnuTLS APIs for handling certificates and keys will silently do the right thing.


OpenSSL has no native support for PKCS#11, but there are a number of external tools which can make it work with PKCS#11. There are pkcs11-helper and libp11 helper libraries which can be used to add PKCs#11 support to an application which uses OpenSSL, but the simplest option is probably to use engine_pkcs11.


NSS is entirely based around PKCS#11 and it loads the p11-kit proxy by default so in most cases it should Just Work.