Skip to content

Instantly share code, notes, and snippets.

@hamgammon
Forked from abeluck/gpg-offline-master.md
Last active January 8, 2017 01:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hamgammon/727b4aaecbbfab56d77a to your computer and use it in GitHub Desktop.
Save hamgammon/727b4aaecbbfab56d77a to your computer and use it in GitHub Desktop.
GPG Offline Master Key w/ smartcard

#GPG Offline Master Key + Smart Card#

This is a local copy of the commands from:

This guide serves as a reference of collected information necessary for strict management of PGP keys. This includes keeping a master key that always remains offline and while subkeys on an OpenPGP smart card. OpenPGP cards can be purchased at kernelconcepts.de. More information can be found at: http://g10code.com/p-card.html

Requirements

  • Bootable offline cd with gpg: http://tails.boum.org
  • OpenPGP smartcard
  • a card reader
  • your PIN
  • your Admin PIN
  • TWO USB sticks
    • one for master key backup
    • one to shuttle drivers, subkeys, and public keys between offline computer and your main computer.

Initial Commands

Setting up a Secure Environment

As of Tails 1.7, gpg and gpg2 can see the USB sim sized card reader and expresscard reader, no futher setup was required. Disable networking (wired,wifi and any other radios/connections) and reboot into the livecd.

Generate Master Key

I choose no expiration for the master key, and 1 year for subkeys. They can be extended any time later. (prob look at adding a expiration for the master key as this can always been changed? also note and link about not using the commment field in your key noted here - https://www.debian-administration.org/users/dkg/weblog/97# #0 )

We generate the master key using the --expert flag to make sure the master key can only "certify" (sign other keys), and not sign or perform encryption.

$ gpg --expert --gen-key
gpg (GnuPG) 1.4.11; Copyright (C) 2010 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Please select what kind of key you want:
   (1) RSA and RSA (default)
   (2) DSA and Elgamal
   (3) DSA (sign only)
   (4) RSA (sign only)
   (7) DSA (set your own capabilities)
   (8) RSA (set your own capabilities)
Your selection? 8

Possible actions for a RSA key: Sign Certify Encrypt Authenticate 
Current allowed actions: Sign Certify Encrypt 

   (S) Toggle the sign capability
   (E) Toggle the encrypt capability
   (A) Toggle the authenticate capability
   (Q) Finished

Your selection? s

Possible actions for a RSA key: Sign Certify Encrypt Authenticate 
Current allowed actions: Certify Encrypt 

   (S) Toggle the sign capability
   (E) Toggle the encrypt capability
   (A) Toggle the authenticate capability
   (Q) Finished

Your selection? e

Possible actions for a RSA key: Sign Certify Encrypt Authenticate 
Current allowed actions: Certify 

   (S) Toggle the sign capability
   (E) Toggle the encrypt capability
   (A) Toggle the authenticate capability
   (Q) Finished

Your selection? q
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048) 4096
Requested keysize is 4096 bits
Please specify how long the key should be valid.
     0 = key does not expire
      <n>  = key expires in n days
      <n>w = key expires in n weeks
      <n>m = key expires in n months
      <n>y = key expires in n years
Key is valid for? (0) 5y
Key expires at Tue  4 Oct 22:32:57 2016 ADT
Is this correct? (y/N) 

You need a user ID to identify your key; the software constructs the user ID
from the Real Name, Comment and Email Address in this form:
    "Heinrich Heine (Der Dichter) <heinrichh@duesseldorf.de>"

Real name: Kenny MacDermid
Email address: 
Comment: 
You selected this USER-ID:
    "Kenny MacDermid"

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o
You need a Passphrase to protect your secret key.

We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
.....+++++
..............+++++
gpg: key 5AD20E1D marked as ultimately trusted
public and secret key created and signed.

gpg: checking the trustdb
gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
gpg: depth: 0  valid:   4  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 4u
gpg: next trustdb check due at 2012-10-05
pub   4096R/5AD20E1D 2011-10-07 [expires: 2016-10-05]
      Key fingerprint = 2B85 0108 8296 B9FC 4FB8  AC86 8A4D 4610 5AD2 0E1D
uid                  Kenny MacDermid

Generate the sub keys

First we add the signing subkey, then the encryption subkey. Both are 3072 bits.

$ gpg --edit-key 5AD20E1D
gpg (GnuPG) 1.4.11; Copyright (C) 2010 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Secret key is available.

pub  4096R/5AD20E1D  created: 2011-10-07  expires: 2016-10-05  usage: C   
             trust: ultimate      validity: ultimate
[ultimate] (1). Kenny MacDermid

gpg> addkey
Key is protected.

You need a passphrase to unlock the secret key for
user: "Kenny MacDermid"
4096-bit RSA key, ID 5AD20E1D, created 2011-10-07

Please select what kind of key you want:
   (3) DSA (sign only)
   (4) RSA (sign only)
   (5) Elgamal (encrypt only)
   (6) RSA (encrypt only)
Your selection? 4
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048) 3072
Requested keysize is 3072 bits
Please specify how long the key should be valid.
     0 = key does not expire
      <n>  = key expires in n days
      <n>w = key expires in n weeks
      <n>m = key expires in n months
      <n>y = key expires in n years
Key is valid for? (0) 1y
Key expires at Fri  5 Oct 22:42:39 2012 ADT
Is this correct? (y/N) y
Really create? (y/N) y
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
.........+++++
............+++++

pub  4096R/5AD20E1D  created: 2011-10-07  expires: 2016-10-05  usage: C   
             trust: ultimate      validity: ultimate
sub  3072R/8DD8FF1A  created: 2011-10-07  expires: 2012-10-06  usage: S   
[ultimate] (1). Kenny MacDermid

gpg> addkey
Key is protected.

You need a passphrase to unlock the secret key for
user: "Kenny MacDermid"
4096-bit RSA key, ID 5AD20E1D, created 2011-10-07

Please select what kind of key you want:
   (3) DSA (sign only)
   (4) RSA (sign only)
   (5) Elgamal (encrypt only)
   (6) RSA (encrypt only)
Your selection? 6
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048) 3072
Requested keysize is 3072 bits
Please specify how long the key should be valid.
     0 = key does not expire
      <n>  = key expires in n days
      <n>w = key expires in n weeks
      <n>m = key expires in n months
      <n>y = key expires in n years
Key is valid for? (0) 1y
Key expires at Fri  5 Oct 22:43:07 2012 ADT
Is this correct? (y/N) y
Really create? (y/N) y
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
....+++++
.+++++

pub  4096R/5AD20E1D  created: 2011-10-07  expires: 2016-10-05  usage: C   
             trust: ultimate      validity: ultimate
sub  3072R/8DD8FF1A  created: 2011-10-07  expires: 2012-10-06  usage: S   
sub  3072R/9B51554C  created: 2011-10-07  expires: 2012-10-06  usage: E   
[ultimate] (1). Kenny MacDermid

gpg> save

Setup uids

UIDs can be added or deleted at any point in time. Just edit the key ID of your master key from Tails (the only time you should have access to your master key).

$ gpg --edit-key 5AD20E1D
gpg> adduid

If you type help at the gpg> prompt you can see other commands that let you perform other actions such as deleting uids.

Edit card content

gpg --card-edit

If you run this you will

Afterwards, your card content should look similar to this:

$ gpg --card-status
Application ID ...: D2760001240101010001000002290000
Version ..........: 1.1
Manufacturer .....: PPC Card Systems
Serial number ....: 00000229
Name of cardholder: Test Card User
Language prefs ...: en
Sex ..............: male
URL of public key : http://url.of/publickey.asc
Login data .......: [not set]
Private DO 1 .....: [not set]
Private DO 2 .....: [not set]
Signature PIN ....: forced
Max. PIN lengths .: 254 254 254
PIN retry counter : 3 3 3
Signature counter : 0
Signature key ....: [none]
Encryption key....: [none]
Authentication key: [none]
General key info..: [none]

Create Backups

Export the secret key for your identity key:

 $ gpg -a --export-secret-keys > master-secret-key.gpg

Export the secret keys for your subkeys (does NOT include master secret key):

 $ gpg -a --export-secret-subkeys > sub-secret-keys.gpg

Save Backups

Backup the following to the super-safe USB stick that is never plugged in to an online machine. Store this USB stick in your ork guarded vault:

  1. master-secret-key.gpg - copy of your master secret key
  2. sub-secret-keys.gpg - copy of your secret sub keys
  3. ~/.gnupg - your entire keyring. It will be used as the GNUPGHOME for future subkeys.

Move the subkeys to the card

Now we will transfer the subkeys generated before to the smartcard. The existing secret keys will be replaced by stubs. If your card gets damaged, you can repeat that step by simply using the backup we brought to the Orks.

The following was copy/pasted from the FSFE article, which uses a smaller key size. What we're doing here is selecting the encryption and signing subkeys and moving them to the card.

You can tell which subkey is which by looking at the usage: X, where X is S for signing or E for encryption.

After this is done, your secret-subkeys will only exist:

  1. On the smartcard

  2. On the ork-guarded USB stick (in the saved .gnupg/ and sub-secret-keys.gpg)

    $ gpg --edit-key 559C215F gpg (GnuPG) 1.4.9; Copyright (C) 2008 Free Software Foundation, Inc. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

    Secret key is available.

    pub 1024D/559C215F created: 2009-05-04 expires: never usage: SC
    trust: ultimate validity: ultimate
    sub 1024R/E1D9B30D created: 2009-05-13 expires: never usage: S
    sub 1024R/EDDA691E created: 2009-05-13 expires: never usage: E
    [ultimate] (1). Martin Gollowitzer (Testing environment) gollo@fsfe.org

    Command> toggle

    sec 1024D/559C215F created: 2009-05-04 expires: never
    ssb 1024R/E1D9B30D created: 2009-05-13 expires: never
    ssb 1024R/EDDA691E created: 2009-05-13 expires: never
    (1) Martin Gollowitzer (Testing environment) gollo@fsfe.org

    Command> key 1

    sec 1024D/559C215F created: 2009-05-04 expires: never

    ssb* 1024R/E1D9B30D created: 2009-05-13 expires: never
    ssb 1024R/EDDA691E created: 2009-05-13 expires: never
    (1) Martin Gollowitzer (Testing environment) gollo@fsfe.org

    Command> keytocard Signature key ....: [none] Encryption key....: [none] Authentication key: [none]

    Please select where to store the key: (1) Signature key (3) Authentication key Your selection? 1

    You need a passphrase to unlock the secret key for user: "Martin Gollowitzer (Testing environment) gollo@fsfe.org" 1024-bit RSA key, ID E1D9B30D, created 2009-05-13

    gpg: generating new key gpg: 3 Admin PIN attempts remaining before card is permanently locked

    Admin PIN

    MAY ASK FOR ADMIN PIN TWICE - this is OK! The first access resizes the key slot to 3072 bits, the second actually moves the key.

    sec 1024D/559C215F created: 2009-05-04 expires: never
    ssb* 1024R/E1D9B30D created: 2009-05-13 expires: never
    card-no: 0001 00000229 ssb 1024R/EDDA691E created: 2009-05-13 expires: never
    (1) Martin Gollowitzer (Testing environment) gollo@fsfe.org

    Command> key 1

    sec 1024D/559C215F created: 2009-05-04 expires: never

    ssb 1024R/E1D9B30D created: 2009-05-13 expires: never
    card-no: 0001 00000229 ssb 1024R/EDDA691E created: 2009-05-13 expires: never
    (1) Martin Gollowitzer (Testing environment) gollo@fsfe.org

    Command> key 2

    sec 1024D/559C215F created: 2009-05-04 expires: never
    ssb 1024R/E1D9B30D created: 2009-05-13 expires: never
    card-no: 0001 00000229 ssb* 1024R/EDDA691E created: 2009-05-13 expires: never
    (1) Martin Gollowitzer (Testing environment) gollo@fsfe.org

    Command> keytocard Signature key ....: [none] Encryption key....: [none] Authentication key: [none]

    Please select where to store the key: (2) Encryption key Your selection? 2

    You need a passphrase to unlock the secret key for user: "Martin Gollowitzer (Testing environment) gollo@fsfe.org" 1024-bit RSA key, ID EDDA691E, created 2009-05-13

    gpg: generating new key

    sec 1024D/559C215F created: 2009-05-04 expires: never
    ssb 1024R/E1D9B30D created: 2009-05-13 expires: never
    card-no: 0001 00000229 ssb* 1024R/EDDA691E created: 2009-05-13 expires: never
    card-no: 0001 00000229 (1) Martin Gollowitzer (Testing environment) gollo@fsfe.org

    Command> save

Export Stubs and Public Key

Export the public key for your identity key:

 $ gpg -a --export 5AD20E1D > pub-key.gpg

Export the secret key stubs for your subkeys. This doesn't include the actual secret keys since they were moved to the smart card, but act as a pointer of sorts to your smartcard. When you import this and your public key to your work computer gnupg will know to look on your smart card when it needs to access one of your private subkeys.

 $ gpg -a --export-secret-subkeys > sub-key-stubs.gpg

Backup pub-key.gpg and sub-key-stubs.gpg to the other USB stick that you used to transfer the drivers. This will be imported on your main machine.

Import keys on main machine

Reboot into your main machine. Insert the usb key from above.

$ gpg --import pub-key.gpg
gpg: key 5AD20E1D: public key "Kenny MacDermid" imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)
$ gpg --import sub-key-stubs.gpg
gpg: key 5AD20E1D: secret key imported
gpg:   secret keys imported: 1
$ gpg --list-secret-keys
sec#  4096R/5AD20E1D 2011-10-07 [expires: 2016-10-05]
uid                  Kenny MacDermid
ssb   2048R/8DD8FF1A 2011-10-07
ssb   2048R/9B51554C 2011-10-07

The secret key should say sec# instead of sec.

Mark your master key as ultimately trusted:

$ gpg --edit-key <UID or KeyID>
>gpg trust
Your Decision? 5

Setting up the Smart Card reader on the online machine

Make sure that gpg2 has gpg-agent and scdaemon installed (also had libccid and pcscd installed at the same time) and run this script from the FSFE page (https://wiki.fsfe.org/Card_howtos/Card_reader_setup_%28udev%29). Sets up udev rules and also adds the user to the scard group. Follow steps and reboot. This allows the usb sim sized card reader from kernalconcepts.de to work.This also worked with a expresscard smartcard reader as well (for full sized cards.) Paying the Orks a visit

There are a few occasions on which you will need the backup on your USB stick:

you need your main key (e.g. to sign another PGP key)
you have to replace your card and want to reuse the subkeys
your card was lost or stolen and you need to revoke the subkeys 

If you want to know how to deal with these situations, read the sections below. Before performing the steps described here, make sure you use a computer you can fully trust. Read the "Go offline" section at the beginning of this howto.

Using your main key

If you want to sign a PGP key (e.g. after a keysigning party) or need to decrypt a file that was only encrypted with your main key (e.g. if you have been using your key without the card earlier), do the following:

Go to the place where you have hidden the USB stick with the backup of your keyrings. Bring food for the Orks.
Get GnuPG to use your backup secret keyring instead of the clean keyring you're using for the card:
    Move your clean keyring out of the way: 

$ mv ~/.gnupg/secring.gpg ~/.gnupg/secring.gpg.clean

Mount your backup medium
Create a symbolic link from the backup to the .gnupg directory: 

$ cd ~/.gnupg $ ln -s <path/of/backup>/secring.gpg .

Do what you need to use the main key for: 

Signing a key

$ gpg --sign-key

Decrypting a file

$ gpg -d

Transfering the subkeys to a new card

See the description in the according chapter "Move the subkeys to the card" of this howto.

ATTENTION: Do not use the backup medium directly for this. The subkeys would be removed from the backup medium if you did. Copy the backup secret keyring to your computer and repeat the whole procedure. If you are using more than one secret key, the best way is to export the secret keys not used on the card and reimport them to your new secret keyring after you repeated the procedure, since otherwise a wrong card ID may be stored in your secret keyring.

Revoking a key

To revoke any of your keys, run

$ gpg --edit-key 559C215F

and use the revkey command.

Return to a clean and safe state: 

$ rm ~/.gnupg/secring.gpg $ mv ~/.gnupg/secring.gpg.clean ~/.gnupg/secring.gpg

Unmount the backup medium and carry it back to the Orks. 

TODO

  • Signing new subkeys.
  • Signing other peoples keys.
  • Figure out importing signatures.
  • Generate and backup revocation certificate gpg --output revoke.asc --gen-revoke KEYNAME?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment