Categories
Smartcards

Creating keys on an OpenPGP smartcard

After purchasing your OpenPGP smartcard, you will want to initialize it with keys. This article shows strategies to create and backup keys and explains how to realize these strategies in practice.

Primary key storage strategy

OpenPGP supports primary keys and sub keys. The primary key can be used to create and revoke sub keys. That means that the primary key is used in the following cases:

  • Sub key creation (this is where we are right now)
  • If you lose your sub keys: Sub key revocation and new sub key creation
  • If the primary key is valid longer than the sub keys: When the sub keys expire and you want to create new sub keys with the same primary key

In other words, you will most probably need it every few years, maybe never after sub key creation. Your OpenPGP smartcard will be used daily. It can easily be stolen and there is no need to have such a level of exposure on your primary key. That is why I do not think that the primary key should be stored on your OpenPGP smartcard. I think it is a better idea to save the primary key in a secure place and only get it out when it is needed.

Sub key storage strategy

An OpenPGP smartcard supports a total of three keys that are used for different operations:

  • Signing data
  • Decrypting data
  • Authentication

There are two topics that should be explained before creating those sub keys: Backing up sub keys and generating sub keys on your smartcard.

Backing up sub keys enables you to restore them, if you lose them (e.g. by losing your smartcard). At the same time, it enables other people to use your backed up sub keys (e.g. by breaking into your apartment and stealing your key material). This danger can be mitigated by encrypting your backup with a password. But if that password shall be really strong, where do you store the password? Smartcards are used when you do not want to have to remember a strong password.

Generating sub keys on your smartcard instead of on your computer is a more secure way of generating keys. The key has never been on an HDD or in RAM. The downside is that you are not able to backup your key. You cannot extract private keys from your OpenPGP smartcard (that is a security feature).

So from my point of view the best strategy for sub keys is: If you need a backup, create one. If not, generate the sub key directly on the card instead of generating it on your PC and then moving it onto the card.

So do I need a backup for my sub keys? It depends on the sub key type:

  • The signing sub key: If I lose this key, I will not be able to sign data anymore with this key. That is not a big problem, I can just create another one.
  • The decryption sub key: If I lose this key, I will not be able to decrypt data anymore with this key. That can be a big problem, because there might be data that is only encrypted with this sub key and not with a password as well. I would lose access to this data. This can be e.g. emails.
  • The authentication sub key: If I lose this key, I cannot authenticate myself anymore with this key. In my experience, there is always a way of regaining access to systems if you lose a key. Usually there is also the possibility to login with a password, to gain physical access and store a new key, to ask the support of the system to store a new key etc. So losing this key should in my case not lead to big problems. Of course, YMMV. If you can lock yourself out irrevocably by losing your authentication key, create a backup.

So my personal sub key storage strategy is to generate the signing sub key and the authentication sub key on the smartcard. The decryption sub key will be generated on my PC, then backed up, and then moved to then smartcard.

Generating and storing the keys

I will perform the following steps:

  1. Generate a primary key on my PC
  2. Generate a decryption sub key on my PC
  3. Copy the primary key and the decryption sub key to a storage that is kept in a secure place
  4. Move the decryption sub key to the OpenPGP smartcard
  5. Generate a signing sub key on the OpenPGP smartcard
  6. Generate an authentication sub key on the OpenPGP smartcard
  7. Delete the primary key from my PC

I used the following command for step 1 (generating a primary key on my PC):

~ $ gpg --full-generate-key

When asked what type of key I wanted to create, I chose:

(4) RSA (sign only)

When GnuPG is done, we are done with step 1.

I used the following commands for step 2 (generating a decryption sub key on my PC; you should insert your the ID of your primary key from step 1 for “$YOUR_PRIMARY_KEY_ID”):

~ $ gpg --edit-key $YOUR_PRIMARY_KEY_ID
gpg> addkey

When asked what type of key I wanted to create, I chose:

(6) RSA (encrypt only)

Afterwards I saved the changes:

gpg> save

And then we are done with step 2.

For step 3 (copying the primary key and the decryption sub key to a storage that is kept in a secure place), I used the following command (it is a good idea to perform these steps on a RAM disk, so there are no files created on your HDD):

~ $ mkdir -p /tmp/gpg-keys
~ $ sudo mount -t tmpfs none /tmp/gpg-keys
~ $ cd /tmp/gpg-keys/
/tmp/gpg-keys $ gpg --export-secret-keys --armor $YOUR_PRIMARY_KEY_ID > $YOUR_PRIMARY_KEY_ID.asc

GnuPG will store the key encrypted with your password. If you want to analyze the file before moving it to a storage, you can do so with the following command:

/tmp/gpg-keys $ gpg --list-packets --verbose $YOUR_PRIMARY_KEY_ID.asc

It should show you the key package and the sub key package and that the secret keys are protected.

Then I burned the asc file on an optical storage medium with a good durability and put it in a secure place. Afterwards, I deleted the file:

/tmp/gpg-keys $ rm $YOUR_PRIMARY_KEY_ID.asc

Afterwards, we are done with step 3.

For step 4 (moving the decryption sub key to the OpenPGP smartcard), I used the following command:

~ $ gpg --edit-key $YOUR_PRIMARY_KEY_ID

GnuPG will show your primary key and your decryption sub key. You need to copy the key ID of your decryption sub key to select it and tell GnuPG to move it to the smartcard:

gpg> key $YOUR_DECRYPTION_SUB_KEY_ID
gpg> keytocard

When asked where to store the key, I chose:

(2) Encryption key

Afterwards I saved the changes:

gpg> save

And then we are done with step 4.

For step 5 (generating a signing sub key on the OpenPGP smartcard), I used the following command:

~ $ gpg --edit-key $YOUR_PRIMARY_KEY_ID
gpg> addcardkey

When asked for the key type, I chose:

(1) Signature key

Afterwards I saved the changes:

gpg> save

And then we are done with step 5.

For step 6 (generating an authentication sub key on the OpenPGP smartcard), I used the following command:

~ $ gpg --edit-key $YOUR_PRIMARY_KEY_ID
gpg> addcardkey

When asked for the key type, I chose:

(3) Authentication key

Afterwards I saved the changes:

gpg> save

And then we are done with step 6.

For step 7 (deleting the primary key from my PC), I used the following command:

~ $ gpg --delete-secret-key $YOUR_PRIMARY_KEY_ID\!

Do not miss the backslash and the exclamation mark, these are for selecting the primary key only (and not also the sub keys).

And then we are done. As a result, we have all three sub key types on the OpenPGP smartcard and a backup of the primary key and the decryption sub key in a secure place.

Leave a Reply

Your email address will not be published. Required fields are marked *