[ TechnoCage | Caskey | gpg ]

enCrypted Mail Transport

using gpg

To set up encrypted mail transport under gpg, the following things must be done.

Recipient keys

The whole key making process can be kinda tricky as the gpg package likes to use the home directory for the default ring and it can really screw up your tests if you accidentally pollute either your key rings, or the two key rings you are creating.

To create a key ring under gpg you do the following. Note the --homedir [dir] parameter. This forces gpg to look in the current directory rather than your own private directory for the key ring files.

$ mkdir keys; cd keys; mkdir sender; <---------
$ mkdir recipient; cd sender <---------
$ gpg --homedir . --gen-key <---------
gpg (GNUPG) 0.3.2; Copyright (C) 1998 Free Software Foundation, Inc.
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions. See the file COPYING for details.

gpg: ./secring.gpg: keyring created
gpg: ./pubring.gpg: keyring created
Please select what kind of key you want:
   (1) DSA and ElGamal (default)
   (2) ElGamal (sign and encrypt)
   (3) ElGamal (encrypt only)
   (4) DSA (sign only)
   (5) ElGamal in a v3 packet
Your selection? 1   <---------
DSA keypair will have 1024 bits.
About to generate a new ELG keypair.
              minimum keysize is  768 bits
              default keysize is 1024 bits
    highest suggested keysize is 2048 bits
What keysize do you want? (1024) 1024   <---------
Requested keysize is 1024 bits   
Please specify how long the key should be valid.
         0 = key does not expire
        = key expires in n days
      w = key expires in n weeks
      m = key expires in n months
      y = key expires in n years
Key is valid for? (0) 1y   <---------
Key expires at 1999-07-21
Is this correct (y/n)? y   <---------
                        
You need a User-ID to identify your key; the software constructs the user
id from Real Name, Comment and Email Address in this form:
    "Heinrich Heine (Der Dichter) "

Real name:  SenderKey   <---------
Email address: sender@example.com   <---------
Comment:  der Xmit   <---------
You selected this USER-ID:
    "SenderKey (der Xmit) "

Edit (N)ame, (C)omment, (E)mail or (O)kay?  o   <---------
You need a Passphrase to protect your secret key.
Passphrase here
Passphrase here again

We need to generate a lot of random bytes. It is a good idea to perform
some other action (work in another window, move the mouse, utilize the
network and the disks) during the prime generation; this gives the random
number generator a better chance to gain enough entropy.
.+++++.+++++...++++++++++........++++++++++.+++++++++++++++...++++++++++.+++++
..++++++++++.......+++++....+++++......+++++......+++++.....+++++..........+++
.+++++.+++++...++++++++++........++++++++++.+++++++++++++++...++++++++++.+++++
..++++++++++.......+++++....+++++......+++++......+++++.....+++++..........+++
.+++++.+++++...++++++++++........++++++++++.+++++++++++++++...++++++++++.+++++
..++++++++++.......+++++....+++++......+++++......+++++.....+++++..........+++


Well, eventually you see the following:

public and secret key created and signed.
$

This means you have got your public and private keys created. An 'ls' should reveal several files; pubring.gpg, secring.gpg, trustdb.gpg and backups of them. Let's take a look...

[caskey@polo sender]$ ls
pubring.gpg   pubring.gpg~  secring.gpg   secring.gpg~  trustdb.gpg

Recipient keys

The recipient keys are made in much the same process, excepting for a different directory and a different set of information for the key IDs.

[caskey@polo sender]$ cd ../recipient/    
[caskey@polo recipient]$ gpg --gen-key --homedir .
[caskey@polo recipient]$ ls
pubring.gpg   pubring.gpg~  secring.gpg   secring.gpg~  trustdb.gpg

I suffered from much confusion when dealing with two nearly identical directories. Hopefully you will do better.

Exporting the sender key

Now that we have a recipient and a sender, we may go about giving the recipient's public key to the sender. First we extract the key from the keyring.

[caskey@polo recipient]$ gpg --homedir . -a --export recipient
gpg (GNUPG) 0.3.2; Copyright (C) 1998 Free Software Foundation, Inc.
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions. See the file COPYING for details.

-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GNUPG v0.3.2 (GNU/Linux)
Comment: Get GNUPG from ftp://ftp.guug.de/pub/gcrypt/

mQGiBDW0LAERBACAOtDO0rjr/37VjL8MWd610f+TBecGQ1qVuQfS8voQ/JbZqndAWh65kmic
AVXWWcCVvR/P8z0EqkTkkT086toMdUpReHvM6dZ/si7RPhfE7s6hI44PCCOHu/wOJFNsgLRv
YIqJN531q5ZH1lZLDdQ/nocB2YWbHw6Yg7ny6hLO6wCg+4RkF8wcwu9lCUiV8TS6lqu3c4ED
/0KlmvGnLQemVaViss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RB37UFxC1fsByIDrHDW
=BwjJ
-----END PGP PUBLIC KEY BLOCK-----

This should be redirected to a file and so we do thus:

[caskey@polo recipient]$ gpg --homedir . -a --export recipient > recip.pub.key
gpg (GNUPG) 0.3.2; Copyright (C) 1998 Free Software Foundation, Inc.
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions. See the file COPYING for details.

[caskey@polo recipient]$ cd ../sender/ [caskey@polo sender]$ cp ../recipient/recip.pub.key .

Importing the key to the sender's ring

Now that we have a convenient file with the recipient key in it, we can go back to the sender's key directory and run the import command

[caskey@polo sender]$ cat recip.pub.key | gpg --homedir . --import
gpg (GNUPG) 0.3.2; Copyright (C) 1998 Free Software Foundation, Inc.
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions. See the file COPYING for details.

gpg:[stdin]: key 303E553D: public key imported

A quick peek at the public key ring now in the sender's posession reveals:

[caskey@polo sender]$ gpg --homedir . --list-keys
gpg (GNUPG) 0.3.2; Copyright (C) 1998 Free Software Foundation, Inc.
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions. See the file COPYING for details.

./pubring.gpg
-------------
pub  1024D/24FEEA49 1998-07-21 SenderKey (der xmit) 
sub  1024G/0DB26610 1998-07-21 
pub  1024D/303E553D 1998-07-21 Recipient (der recv) 
sub  1024G/FDF99707 1998-07-21 

We can compare that to the contents of our secret ring like so

[caskey@polo sender]$ gpg --homedir . --list-secret-keys
gpg (GNUPG) 0.3.2; Copyright (C) 1998 Free Software Foundation, Inc.
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions. See the file COPYING for details.

./secring.gpg
-------------
sec  1024D/24FEEA49 1998-07-21 SenderKey (der xmit) 
ssb  1024G/0DB26610 1998-07-21 

As can be expected, the sender only has her own private key.

Signing of the key

If you were to try and encrypt a message immediately you would get the following error message:

[caskey@polo sender]$ echo Hello World | \
>  gpg --homedir . -a -e -r recipient --batch --yes
gpg: no info to calculate a trust probability
gpg: no valid addressees
gpg: [stdin]: encryption failed: No such user id

Were this a simple manual execution (i.e. without the --batch and --yes) we would've been prompted to confirm that this was the key to use. As we desre to automate the process, we will have to find a way of resolving the trust probability issue. As luck would have it, all we need to do is sign the key.

[caskey@polo sender]$ gpg --homedir . --sign-key recipient
gpg (GNUPG) 0.3.2; Copyright (C) 1998 Free Software Foundation, Inc.
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions. See the file COPYING for details.

Checking signatures of this public key certificate:
pub  1024D/303E553D 1998-07-21   Recipient (der recv) 
             Fingerprint: 643D A791 B995 E5C4 D560  12FA E971 17A0 303E 553D

Sign this key? yes

A peek at the signatures on the key shows us that everything went as expected.

[caskey@polo sender]$ gpg --homedir . --list-sigs         
gpg (GNUPG) 0.3.2; Copyright (C) 1998 Free Software Foundation, Inc.
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions. See the file COPYING for details.

./pubring.gpg
-------------
pub  1024D/24FEEA49 1998-07-21 SenderKey (der xmit) 
sig        24FEEA49 1998-07-21   [selfsig]
sub  1024G/0DB26610 1998-07-21 
sig        24FEEA49 1998-07-21   [keybind]
pub  1024D/303E553D 1998-07-21 Recipient (der recv) 
sig        303E553D 1998-07-21   [selfsig]
sub  1024G/FDF99707 1998-07-21 
sig        303E553D 1998-07-21   [keybind]
sig        24FEEA49 1998-07-21   SenderKey (der xmit) 

Now we may make our final tests

[caskey@polo sender]$ echo hello world | \
> gpg --homedir . --armor --encrypt --batch --yes -r Recipient
gpg: no info to calculate a trust probability
gpg: no valid addressees
gpg: [stdin]: encryption failed: No such user id

This doesn't look good so we'll try moving our existing owner trust database out of the way where it won't do any harm.

[caskey@polo sender]$ mv trustdb.gpg tdb
[caskey@polo sender]$ echo hello world | \
> gpg --homedir . --armor --encrypt  --yes -r recipient
gpg (GNUPG) 0.3.2; Copyright (C) 1998 Free Software Foundation, Inc.
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions. See the file COPYING for details.

gpg: key FDF99707.5: inserted into trustdb
-----BEGIN PGP MESSAGE-----
Version: GNUPG v0.3.2 (GNU/Linux)
Comment: Get GNUPG from ftp://ftp.guug.de/pub/gcrypt/

hQEOA98LvWL9+ZcHFAP/eCzgiLKL/URy0JDpjvD9PQBshvXtDDZOhMnxYCNuCqwY3tNH/9TQ
lSHlN60NRWacEH9poIQV9PFoE6cEX9xV2HtGzX5/CjOe0l8TCXfWy0KySH4TS3k2m53DU/L7
qy+WhuBpw5a8RMt6PnvijSD/gHgs0xNTeryyjQFAf26Et6gD/2BbB6N5OC5HHMRiuWhgxo8N
NX27LlPIxbrtxE21YC2hACjJg/3Ujefy0nDxQVV11ASFy1ujKOL/H0t8XmZKdOtSEQrMCY5F
/JYduEZxQeuO51HNVL/mINlhRE7d2wGfgENva6nmNwWEikABOa8Dhv+JSOiefeGddFun00+6
abJhySiSG98PxibTlyyxxVtrwBSFSgiuhXUfxGa6SdIfCTMPx6s/JtLOD1QA
=uCfD
-----END PGP MESSAGE-----

It would appear that this caused the trust database to be re-built and the encryption to complete. The final test is a decryption using our private key in the recipient's directory.

[caskey@polo sender]$ echo hello world | \
> gpg --homedir . --armor --encrypt  --yes -r recipient | \
> (cd ../recipient/; gpg --homedir . --decrypt )
gpg (GNUPG) 0.3.2; Copyright (C) 1998 Free Software Foundation, Inc.
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions. See the file COPYING for details.

gpg (GNUPG) 0.3.2; Copyright (C) 1998 Free Software Foundation, Inc.
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions. See the file COPYING for details.

hello world

And we clearly see two invocations of gpg and the resulting cleartext at the end.

.qmail

The qmail configuration is in two parts. The first is the alias that will receive plaintext mail and encrypt it. The second is the alias that will receive cyphertext and decrypt it.