strongSwan smart card configuration HOWTO » History » Version 120

« Previous - Version 120/159 (diff) - Next » - Current version
Jean-Michel Pouré, 27.12.2009 22:20

strongSwan Smartcard configuration HOWTO

Smartcards are a mature technology which avoid your PKIs from being stolen by a theft.
strongSwan relies on OpenSC to query the smartcard according to PKCS#11 RSA standard.
In this HOWTO, we give minimal information how to use a reader, initialize cards and use strongSwan.


You need a USB smartcard reader and a blank smart card.

Compatible card readers

OpenSC supports CCID compatible readers using pcsclite libraries.
Recent usb readers most likely implement the CCID specification and therefore should work.

You may refer to the matrix of supported smartcard readers published by pcsclite project.

These Ominikey readers are quite popular:
  • Second hand Omnikey 3121 CardMan USB smartcard readers can be found on eBay for less than 10€. These are good units for testing a setup.
  • Smartcard 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 smartcard reader with LCD display and keypad for secure PIN entry may be a good choice.

Compatible smartcards

You may use blank cards with support of 1024/2048 RSA to store credentials:
  • Cryptoflex E-Gate 32k were the best choice (but no longer available).
  • 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 bank them using Windows software.

A list of compatible cards is listed here.

You may also use read-only, pre-personalized read-only cards:
  • eID cards. Many European countries offer them and 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:

Fix-me, please provide more IT shops. We are still looking for a 10€ compatible card.

Software requirements

To install pcsc-tools with ccid and java cards support, under Debian based distributions:

apt-get install pcsc-tools libccid

strongSwan supports PKCS#11 RSA standard using opensc libraries, which specifies how to store cryptographic information on devices.

To install opensc:

sudo apt-get install opensc

Certification Authority

We recommend using a certificate GUI 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 is 2048 bits are known to work.

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

Smartcard configuration

Testing the card

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
1      openct     OpenCT reader (detached)
2      openct     OpenCT reader (detached)
3      openct     OpenCT reader (detached)
4      openct     OpenCT reader (detached)
5      openct     OpenCT reader (detached)

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
Cryptoflex 32K e-gate v4

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)

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 --create-pkcs15

a fresh PKCS#15 file structure is created on a smartcard or cryptotoken. With the next command

    pkcs15-init --auth-id 1 --store-pin --pin "12345678" 
                --puk "87654321" --label "my PIN" 

a secret PIN code with auth-id 1 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 smartcard will be locked and the PUK code can be used to unlock the card again.

Next the RSA private key is transferred to the smartcard

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

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

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 smartcard.

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

strongSwan management

Configuring peers

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

./configure <add your options there> \
sudo make install

Defining a smartcard-based connection in ipsec.conf is easy:

    conn sun

In most cases there is a single smartcard reader or cryptotoken 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 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 smartcard signing operation needed to sign the hash with the RSA private key during IKE 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

Smartcard 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 smartcard 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.


This article was adapted by Smartcard HOWTO written by Michele Baldessari. Permission granted by Michele Baldessari to reproduce the text on strongSwan wiki. strongSwan configuration is taken from strongSwan manual.