strongSwan smart card configuration HOWTO » History » Version 158

« Previous - Version 158/159 (diff) - Next » - Current version
Tobias Brunner, 14.11.2011 11:56
note about IKEv2 added, some editorial changes

strongSwan smart card configuration HOWTO

Smart cards are a mature technology which avoid your PKIs from being stolen by a theft.
strongSwan relies on OpenSC to query the smart card according to the PKCS#11 RSA standard. Actually, any shared library implementing the PKCS#11 API can be used.
In this HOWTO, we give minimal information how to use a reader, initialize cards and configure strongSwan.

Note: The configuration for IKEv2 is slightly different than for IKEv1 which is described here. Refer to Using Smartcards with IKEv2 for details.

Compatible hardware

You need a USB smart card reader and a blank smart card, preferably with support of 2048-bit RSA keys.
Since 768-bit RSA keys have been broken, the NSA recommends using 2048-bit RSA key.

Compatible card readers

Thanks to OpenSC , GNU/Linux supports most CCID smart card readers, using the PCSC-Lite library.

Most recent USB card readers are compatible. You may refer to the matrix of supported smartcard readers published by the PCSC-Lite project.

These Omnikey readers are quite popular:
  • Second hand Omnikey 3121 CardMan USB smart card readers can be found on eBay for less than 10€. These are good units for testing a setup.
  • Smart card readers with an integrated PIN pad offer an increased security level because the PIN entry cannot be sniffed on the host computer e.g. by a surrepticiously installed key logger. The Omnikey 3821 secure smart card reader with LCD display and keypad for secure PIN entry may be a good choice.

Compatible smart cards

You may use blank cards with support for 1024/2048 bit RSA to store credentials:
  • Feitian PKI card. The original author of this HOWTO recommends using Feitian PKI cards. Feitian PKI cards allow 2048 bit RSA key and are very well supported by GNU/Linux.
  • STARCOS SPK 2.4 cards are compatible, but cannot be erased, therefore any error may be fatal. You may buy developer versions which can be erased.
  • Siemens Card OS 4.3 B may be a good choice, but OpenSC does not know how to initialize them. You have to blank them using Windows software.
  • ACOS5 PKI cards are cheap, but unsupported. With a little work, OpenSC could support them.

The OpenSC project maintains a list of compatible cards.

You may also use read-only, pre-personalized read-only cards:
  • eID cards. Many European countries offer them and you don't need to buy extra cards for VPN use.
  • [fix-me] Please provide us with names of providers.
Where to buy: in Europe, you may try:
  • Gooze sells FEITIAN PKI cards and refurbished smartcard readers. The original author of this HOWTO started the Gooze store to lower the price of security solutions. You can find a smart card reader and a card for as little as 25€. Gooze and FEITIAN also donate free FEITIAN PKI cards to interested free software developers. You may apply for free cards here.
  • Cryptoshop sells cards and readers from multiple manufacturers (Gemalto, STARCOS SPK, Siemens Card OS).
  • Smartcard Focus also sells cards and readers from several different manufacturers.

These shops are not related to the strongSwan community in any way.


Smart card reader

To install pcsc-tools with ccid support, under Debian based distributions use:

sudo apt-get install pcsc-tools libccid

strongSwan supports the PKCS#11 RSA standard using the OpenSC library, which specifies how to access cryptographic information on devices.

To install OpenSC use:

sudo apt-get install opensc

Open /etc/opensc/opensc.conf.

Edit this line to use only pcsc drivers:

reader_drivers = pcsc;

Do not install the OpenCT package, as it is incompatible with the pcsc-lite package.

Check that the card reader is correctly recognized by OpenSC:

$ opensc-tool -l
Readers known about:
Nr.    Driver     Name
0      pcsc       OmniKey CardMan 3121 00 00

At Nr. 0 we have our recognized Omnikey CardMan 3121 reader. Let's insert our smart card in the reader (note that when buying the card you'll also receive the TRANSPORT KEY. Make sure that the transport key proposed by OpenSC matches the one you got in the mail. You will destroy the card by entering the wrong Key three times):

Let's double check that the card is recongized by printing its ATR:

$ opensc-tool -r0 -a

We can also check the name of the card with the -n switch (we can omit the -r0 since we only have one reader connected):

$ opensc-tool -n
Using reader with a card: OmniKey CardMan 3121 00 00

At this point we know both the card and reader are fully recognized and functional, and we can proceed to erase the card (you will be asked for the transport key you got in your mail).

Certification Authority

To set up your CA you may use OpenSSL or our own PKI tool. To simplify things you may also use a graphical user interface to set up your CA. One important thing to keep in mind is that, you shouldn't create private keys with a length not supported by your smart card (check the specs to be sure). Keys with a maximum length of 2048 bits are known to work.

Make a backup of your keys/certificates on a CD-ROM and store it in a safe place.

Configuring a smartcard with pkcsc15-init

strongSwan's smartcard solution is based on the PKCS#15 "Cryptographic Token Information Format Standard" fully supported by OpenSC library functions. Using the command

    pkcs15-init --erase-card

This may result in a error if the card is already blank.

A fresh PKCS#15 file structure is created on a smart card or crypto token. With the next command

pkcs15-init  --create-pkcs15 --profile pkcs15+onepin \
             --use-default-transport-key \
             --pin 0000 --puk 111111 \
             --label "Test" 

a secret PIN code is stored in an unretrievable location on the smart card. The PIN will protect the RSA signing operation. If the PIN is entered incorrectly more than three times then the smart card will be locked and the PUK code can be used to unlock the card again.

Next the RSA private key is transferred to the smart card

    pkcs15-init --auth-id 1 --store-private-key myKey.pem
               [--id 45]

By default the PKCS#15 smart card record will be assigned the ID 45. Using the --id option, multiple key records can be stored on a smart card.

At last we load the matching X.509 certificate onto the smartcard

    pkcs15-init --auth-id 1 --store-certificate myCert.pem
               [--id 45]

The pkcs15-tool can now be used to verify the contents of the smart card.

    pkcs15-tool --list-pins --list-keys --list-certificates

strongSwan configuration

Note: The configuration for IKEv2 is slightly different than for IKEv1 which is described here. Refer to Using Smartcards with IKEv2 for details.

Configuring peers

To enable smart card support in the IKEv1 daemon pluto, you may need to compile strongSwan from sources:

./configure <add your options there> --enable-smartcard
sudo make install

Defining a smart card based connection in ipsec.conf is easy:

    conn sun

In most cases there is a single smart card reader or crypto token and only one RSA private key safely stored on the crypto device. Thus usually the entry


which stands for the full notation


is sufficient where the first certificate/private key object enumerated by the PKCS#11 module is used. If several certificate/private key objects are present then the nth object can be selected using


The command

    ipsec listcards

gives an overview over all certifcate objects made available by the PKCS#11 module. CA certificates are automatically available as trust anchors without the need to copy them into the /etc/ipsec.d/cacerts/ directory first.

As an alternative the certificate ID and/or the slot number defined by the PKCS#11 standard can be specified using the notation

    leftcert=%smartcard<slot nr>:<key id in hex format>



will look in all available slots for ID 0x50 starting with the first slot (usually slot 0) whereas


will directly check slot 4 (which is usually the first slot on the second reader/token when using the OpenSC library) for a key with ID 0x50.

Entering the PIN code

Since the smart card signing operation needed to sign the hash with the RSA private key during IKEv1 Main Mode is protected by a PIN code, the secret PIN must be made available to pluto.

For gateways that must be able to start IPsec tunnels automatically in unattended mode after a reboot, the secret PIN can be stored statically in ipsec.secrets

    : PIN %smartcard "12345678" 

or with the general notation

    : PIN %smartcard<nr> "<PIN code>" 

or alternatively

    : PIN %smartcard<slot nr>:<key id> "<PIN code>" 

On a personal notebook computer that could get stolen, you wouldn't want to store your PIN in ipsec.secrets.

Thus the alternative form

    : PIN %smartcard %prompt

will prompt you for the PIN when you start up the first IPsec connection using the command

    ipsec up sun

The ipsec up command calls the whack function which in turn communicates with pluto over a socket. Since the whack function call is executed from a command window, pluto can prompt you for the PIN over this socket connection. Unfortunately roadwarrior connections which just wait passively for peers cannot be initiated via the command window:

    conn rw

But if there is a corresponding entry

    : PIN %smartcard1:50 %prompt

in ipsec.secrets, then the standard command

    ipsec rereadsecrets

or the alias

    ipsec secrets

can be used to enter the PIN code for this connection interactively. The command

    ipsec listcards

can be executed at any time to check the current status of the PIN code[s].

PIN-pad equipped smartcard readers

Smart card readers with an integrated PIN pad offer an increased security level because the PIN entry cannot be sniffed on the host computer e.g. by a surrepticiously installed key logger. In order to tell pluto not to prompt for the PIN on the host itself, the entry

    : PIN %smartcard:50 %pinpad

can be used in ipsec.secrets. Because the key pad does not cache the PIN in the smart card reader, it must be entered for every PKCS#11 session login. By default pluto does a session logout after every RSA signature. In order to avoid the repeated entry of the PIN code during the periodic IKE main mode rekeyings, the following parameter can be set in the config setup section of ipsec.conf:

    config setup

The default setting is pkcs11keepstate=no.

Acknowledgements and other resources