Difference between revisions of "GnuPG"

From Gentoo Wiki
Jump to: navigation, search
(Marked this version for translation)
m (added missing space)
(5 intermediate revisions by one user not shown)
Line 14: Line 14:
  
 
<!--T:4-->
 
<!--T:4-->
This guide assumes that you are familiar with public-key cryptography, encryption, and digital signatures. If this is not the case jump to [[#doc_chap7]] or take a look at the [http://www.gnupg.org/documentation/guides.html GnuPG handbook] , chapter 2, and then come back.  
+
This guide assumes that you are familiar with public-key cryptography, encryption, and digital signatures. If this is not the case take a look at the [http://www.gnupg.org/documentation/guides.html GnuPG handbook], chapter 2, and then come back.  
  
 
<!--T:5-->
 
<!--T:5-->
This guide will teach you how to install GnuPG, how to create your key pair, how to add keys to your keyring, how to submit your public key to a key server and how to sign, encrypt, verify or decode messages you send or receive. You will also learn how to encrypt files on your local computer to prevent people from reading their contents.  
+
This guide will teach you how to install GnuPG, how to create your key pair, how to add keys to your keyring, how to submit your public key to a key server and how to sign, encrypt, verify or decode messages you send or receive. You will also learn how to encrypt files on your local computer to prevent people from reading their contents.
  
 
=== Installation of required software === <!--T:6-->
 
=== Installation of required software === <!--T:6-->
  
 
<!--T:7-->
 
<!--T:7-->
At a very basic level you need to <code>emerge gnupg</code> . Many aplications today have some sort of support for gpg, so having ''crypt'' in your USE variable is probably a good idea. If you wish to have an email client capable of using gnupg you can use pine ( <code>emerge pinepgp</code> ), mutt ( <code>emerge mutt</code> ), Mozilla Thunderbird ( <code>emerge thunderbird</code> ), evolution (evolution is a GNOME Microsoft Outlook work alike) and KDE's own KMail.  
+
At a very basic level you need to <code>emerge gnupg</code>. Many aplications today have some sort of support for gpg, so having ''crypt'' in your USE variable is probably a good idea. If you wish to have an email client capable of using gnupg you can use pine (<code>emerge pinepgp</code>), mutt (<code>emerge mutt</code>), Mozilla Thunderbird (<code>emerge thunderbird</code>), evolution (evolution is a GNOME Microsoft Outlook work alike) and KDE's own KMail.  
  
 
<!--T:8-->
 
<!--T:8-->
Line 32: Line 32:
  
 
<!--T:11-->
 
<!--T:11-->
To create your key, just run <code>gpg --gen-key</code> . The first time you run it, it will create some directories; run it again to create the keys:  
+
To create your key, just run <code>gpg --gen-key</code>. The first time you run it, it will create some directories; run it again to create the keys:  
  
 
<!--T:12-->
 
<!--T:12-->
Line 104: Line 104:
  
 
<!--T:21-->
 
<!--T:21-->
{{Important|This part is very important and you must do it ''NOW'' .}}
+
{{Important|This part is very important and you must do it ''NOW''.}}
  
 
<!--T:22-->
 
<!--T:22-->
Line 153: Line 153:
  
 
<!--T:25-->
 
<!--T:25-->
The <code>gpg --list-keys</code> command lists keys in your public keyring. You may use it to see the ID of your key so that you can create the revocation certificate. Now it is a good idea to copy all the .gnupg directory and the revocation certificate (in ASCII armor - {{Path|revoke.asc}} ) to some secure medium (two floppy's or a CD-R you store in safe location). Remember that {{Path|revoke.asc}} can be used to revoke your keys and make them unusable in the future.  
+
The <code>gpg --list-keys</code> command lists keys in your public keyring. You may use it to see the ID of your key so that you can create the revocation certificate. Now it is a good idea to copy all the .gnupg directory and the revocation certificate (in ASCII armor - {{Path|revoke.asc}}) to some secure medium (two floppy's or a CD-R you store in safe location). Remember that {{Path|revoke.asc}} can be used to revoke your keys and make them unusable in the future.  
  
 
<!--T:26-->
 
<!--T:26-->
Line 161: Line 161:
  
 
<!--T:28-->
 
<!--T:28-->
To export your key, you type <code>gpg --armor --output john.asc --export john@nowhere.someplace.flick</code> . You can almost always use the key ID or something that identifies the key (here we used an email address). John now has a {{Path|john.asc}} that he can send his friends, or place on his web page so that people can communicate safely with him.  
+
To export your key, you type <code>gpg --armor --output john.asc --export john@nowhere.someplace.flick</code>. You can almost always use the key ID or something that identifies the key (here we used an email address). John now has a {{Path|john.asc}} that he can send his friends, or place on his web page so that people can communicate safely with him.  
  
 
=== Importing keys === <!--T:29-->
 
=== Importing keys === <!--T:29-->
Line 263: Line 263:
  
 
<!--T:41-->
 
<!--T:41-->
Now that you have your key, it is probably a good idea to send it to the world key server. There are a lot of keyservers in the world and most of them exchange keys between them. Here we are going to send John Doe's key to the subkeys.pgp.net server. This uses HTTP, so if you need to use a proxy for HTTP traffic don't forget to set it ( <code>export http_proxy=http://proxy_host:port/</code> ). The command for sending the key is: <code>gpg --keyserver subkeys.pgp.net --keyserver-options honor-http-proxy --send-key 75447B14</code> where <code>75447B14</code> is the key ID. If you don't need a HTTP proxy you can remove the ''--keyserver-options honor-http-proxy'' .  
+
Now that you have your key, it is probably a good idea to send it to the world key server. There are a lot of keyservers in the world and most of them exchange keys between them. Here we are going to send John Doe's key to the subkeys.pgp.net server. This uses HTTP, so if you need to use a proxy for HTTP traffic don't forget to set it (<code>export http_proxy=http://proxy_host:port/</code>). The command for sending the key is: <code>gpg --keyserver subkeys.pgp.net --keyserver-options honor-http-proxy --send-key 75447B14</code> where <code>75447B14</code> is the key ID. If you don't need a HTTP proxy you can remove the ''--keyserver-options honor-http-proxy''.  
  
 
<!--T:42-->
 
<!--T:42-->
Line 296: Line 296:
  
 
<!--T:46-->
 
<!--T:46-->
As you can see from the server response I have a few keys submitted to the key server, but I currently only use ''B9F2D52A'' . Now John Doe can get it and sign it if he trusts it.  
+
As you can see from the server response I have a few keys submitted to the key server, but I currently only use ''B9F2D52A''. Now John Doe can get it and sign it if he trusts it.  
  
 
== Using a GPG Agent == <!--T:47-->
 
== Using a GPG Agent == <!--T:47-->
Line 309: Line 309:
  
 
<!--T:51-->
 
<!--T:51-->
Gentoo provides a few GPG agent applications. The <code>app-crypt/gnupg</code> package contains what could be considered the reference one, and will be the one we'll use in this document.  
+
Gentoo provides a few GPG agent applications. The {{Package|app-crypt/gnupg}} package contains what could be considered the reference one, and will be the one we'll use in this document.
  
 
=== Configuring gpg-agent and pinentry === <!--T:52-->
 
=== Configuring gpg-agent and pinentry === <!--T:52-->
  
 
<!--T:53-->
 
<!--T:53-->
GnuPG includes <code>gpg-agent</code> and <code>pinentry</code> . <code>pinentry</code> is the helper application that gpg-agent uses to request the passphrase in a graphical window. It comes in three flavors: it can popup a window using the gtk+, Qt, or curses library (depending on your USE flags in {{Path|/etc/portage/make.conf}} ).  
+
GnuPG includes <code>gpg-agent</code> and <code>pinentry</code>. <code>pinentry</code> is the helper application that gpg-agent uses to request the passphrase in a graphical window. It comes in three flavors: it can popup a window using the gtk+, Qt, or curses library (depending on your USE flags in {{Path|/etc/portage/make.conf}}).  
  
 
<!--T:54-->
 
<!--T:54-->
If you installed <code>pinentry</code> with more than one popup window type, you can choose between them with <code>eselect-pinentry</code> :  
+
If you installed <code>pinentry</code> with more than one popup window type, you can choose between them with <code>eselect-pinentry</code>:  
  
 
<!--T:55-->
 
<!--T:55-->
Line 376: Line 376:
  
 
<!--T:67-->
 
<!--T:67-->
If you use a different graphical environment, put that line (the same one as mentioned above) in {{Path|~/.xinitrc}} (if you use <code>startx</code> ) or {{Path|~/.xsession}} (if you use XDM/GDM/KDM/...).  
+
If you use a different graphical environment, put that line (the same one as mentioned above) in {{Path|~/.xinitrc}} (if you use <code>startx</code>) or {{Path|~/.xsession}} (if you use XDM/GDM/KDM/...).
  
 
== Working with documents == <!--T:68-->
 
== Working with documents == <!--T:68-->
Line 399: Line 399:
  
 
<!--T:75-->
 
<!--T:75-->
Suppose that you have received a file which is encrypted to you. The command to decrypt it is <code>gpg --output document --decrypt encrypted_doc.gpg</code> . This will decrypt the document and verify the signature (if there is one).  
+
Suppose that you have received a file which is encrypted to you. The command to decrypt it is <code>gpg --output document --decrypt encrypted_doc.gpg</code>. This will decrypt the document and verify the signature (if there is one).  
  
 
=== Encrypting and decrypting without keys === <!--T:76-->
 
=== Encrypting and decrypting without keys === <!--T:76-->
  
 
<!--T:77-->
 
<!--T:77-->
It is also possible to encrypt files using passwords instead of keys. Well, the password itself will function as the key - it will be used as a ''symmetric cypher'' . You can encrypt the file using <code>gpg</code> 's <code>--symmetric</code> argument; decrypting uses the same command as we talked about before.  
+
It is also possible to encrypt files using passwords instead of keys. Well, the password itself will function as the key - it will be used as a ''symmetric cypher''. You can encrypt the file using <code>gpg</code> 's <code>--symmetric</code> argument; decrypting uses the same command as we talked about before.  
  
 
<!--T:78-->
 
<!--T:78-->
Line 429: Line 429:
  
 
<!--T:84-->
 
<!--T:84-->
Another nice command is <code>gpg --refresh-keys</code> . This will contact the keyserver defined in the options file and refresh public keys in your local key ring from there, searching for revoked keys, new IDs, and new signatures on keys. You should probably run this once or twice a month so that if someone revokes his key you will be notified.  
+
Another nice command is <code>gpg --refresh-keys</code>. This will contact the keyserver defined in the options file and refresh public keys in your local key ring from there, searching for revoked keys, new IDs, and new signatures on keys. You should probably run this once or twice a month so that if someone revokes his key you will be notified.  
  
 
== GnuPG interfaces == <!--T:85-->
 
== GnuPG interfaces == <!--T:85-->
Line 483: Line 483:
  
 
<!--T:99-->
 
<!--T:99-->
You can verify if KMail is properly configured by going to <code>Settings</code>, <code>Configure KMail</code>, <code>Security</code>, <code>Crypto Backends</code>. You should see a GpgME-based backend listed and you should be able to fill the OpenPGP checkbox. If it is listed but grayed out, click on <code>Rescan</code> . If the GpgME-based backend remains grayed out, KMail is not working properly.  
+
You can verify if KMail is properly configured by going to <code>Settings</code>, <code>Configure KMail</code>, <code>Security</code>, <code>Crypto Backends</code>. You should see a GpgME-based backend listed and you should be able to fill the OpenPGP checkbox. If it is listed but grayed out, click on <code>Rescan</code>. If the GpgME-based backend remains grayed out, KMail is not working properly.  
  
 
<!--T:100-->
 
<!--T:100-->
Line 501: Line 501:
  
 
<!--T:106-->
 
<!--T:106-->
The concept of public key cryptography was originally devised by Whitfield Diffie and Martin Hellman in 1976. When I first heard the words "public key" and "cryptography" in the same sentence back in '93 I thought to myself that it would be impossible to do such a thing. In those days there was no Internet (well there was, but not for me) so I went to the public library and asked for books on Cryptography. I must say that I was 16 at the time so the clerk there looked to me in astonishment and brought me a book for children on substitution cyphers (those where you change a letter for another like the famous Caesar Cypher or ROT-13 (Tragbb Ebpxf, naq lbh xabj vg vf tbbq orpnhfr lbh ner ernqvat guvf qbp.), ( <code>emerge rotix</code> if you cannot read the preceding text)). I was very upset with this and started to search for more info. It is good to have mathematicians in the family, because as soon as I talked to one of them I was introduced to a new world.  
+
The concept of public key cryptography was originally devised by Whitfield Diffie and Martin Hellman in 1976. When I first heard the words "public key" and "cryptography" in the same sentence back in '93 I thought to myself that it would be impossible to do such a thing. In those days there was no Internet (well there was, but not for me) so I went to the public library and asked for books on Cryptography. I must say that I was 16 at the time so the clerk there looked to me in astonishment and brought me a book for children on substitution cyphers (those where you change a letter for another like the famous Caesar Cypher or ROT-13 (Tragbb Ebpxf, naq lbh xabj vg vf tbbq orpnhfr lbh ner ernqvat guvf qbp.), (<code>emerge rotix</code> if you cannot read the preceding text)). I was very upset with this and started to search for more info. It is good to have mathematicians in the family, because as soon as I talked to one of them I was introduced to a new world.  
  
 
<!--T:107-->
 
<!--T:107-->
Line 539: Line 539:
  
 
<!--T:112-->
 
<!--T:112-->
One consequence of the above is that if you cypher a message to me, and you loose the original uncyphered message you will no longer be able to retrieve it from the cyphered version.  
+
One consequence of the above is that if you cypher a message to me, and you loose the original uncyphered message you will no longer be able to retrieve it from the cyphered version.
  
 
=== Signatures === <!--T:113-->
 
=== Signatures === <!--T:113-->
Line 588: Line 588:
  
 
<!--T:130-->
 
<!--T:130-->
<code>gpg</code> is a very complex tool, it lets you do much more than what I have covered here. This document is for the user who is new to GnuPG. For more information, you should check the[http://www.gnupg.org GnuPG Website] .  
+
<code>gpg</code> is a very complex tool, it lets you do much more than what I have covered here. This document is for the user who is new to GnuPG. For more information, you should check the [http://www.gnupg.org GnuPG Website].  
  
 
<!--T:131-->
 
<!--T:131-->
I did not write about other tools like <code>pgp4pine</code> , <code>gpgpine</code> , <code>evolution</code> and maybe Windows tools, but I will probably extend this document in the future.  
+
I did not write about other tools like <code>pgp4pine</code>, <code>gpgpine</code>, <code>evolution</code> and maybe Windows tools, but I will probably extend this document in the future.
  
 
=== Credits === <!--T:132-->
 
=== Credits === <!--T:132-->

Revision as of 12:51, 9 March 2014

Other languages:English 100% • ‎español 100% • ‎français 1% • ‎italiano 19% • ‎한국어 100% • ‎русский 100% • ‎中文(中国大陆)‎ 32%

This small guide will teach you the basics of using GnuPG, a tool for secure communication.

Introduction

What you will get in this guide

This guide assumes that you are familiar with public-key cryptography, encryption, and digital signatures. If this is not the case take a look at the GnuPG handbook, chapter 2, and then come back.

This guide will teach you how to install GnuPG, how to create your key pair, how to add keys to your keyring, how to submit your public key to a key server and how to sign, encrypt, verify or decode messages you send or receive. You will also learn how to encrypt files on your local computer to prevent people from reading their contents.

Installation of required software

At a very basic level you need to emerge gnupg. Many aplications today have some sort of support for gpg, so having crypt in your USE variable is probably a good idea. If you wish to have an email client capable of using gnupg you can use pine (emerge pinepgp), mutt (emerge mutt), Mozilla Thunderbird (emerge thunderbird), evolution (evolution is a GNOME Microsoft Outlook work alike) and KDE's own KMail.

Kgpg might interest you if you use KDE. This small program allows you to generate key pairs, import keys from ASCII files, sign imported keys, export keys and a few more features.

Generating your key and adding keys to your public keyring

Creating your key

To create your key, just run gpg --gen-key. The first time you run it, it will create some directories; run it again to create the keys:

user $ gpg --gen-key
gpg (GnuPG) 1.0.7; Copyright (C) 2002 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.
  
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)
   Your selection? 1

Here you can choose the type of key you want to use. Most users will go for the default RSA and RSA. Next is the key size - remember that bigger is better but don't use a size larger than 2048 with DSA/ElGamal keys. Generally 2048 is more than enough for normal email.

After size comes the expiration date. Here smaller is better, but most users can go for a key that never expires or to something like 2 or 3 years.

CodeChoosing key size

RSA keypair will have 1024 bits.
RSA keys may be between 1024 and 4096 bits long.
    What keysize do you want? (2048) 2048
Requested keysize is 2048 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) 0
Key does not expire at all

Now it is time to enter some personal information about yourself. If you are going to send your public key to other people you have to use your real email address here.

CodeEntering user information

Is this correct (y/n)? y
                          
GnuPG needs to construct a user ID to identify your key.
  
Real name: John Doe
Email address: john@nowhere.someplace.flick
Comment: The Real John Doe
You selected this USER-ID:
"John Doe (The Real John Doe) <john@nowhere.someplace.flick>"
  
Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O 
You need a Passphrase to protect your secret key.    
  
Enter passphrase: 

Now enter your key passphrase twice. It is a good idea to use a strong password. If someone ever gets hold of your private key and cracks your password, they will be able to send messages signed by "you", making everyone believe the mails were sent by you.

Next, GnuPG will generate your key. Moving the mouse or having a mp3 playing in the background will help speed up the process because it generates random data.

Generating a revocation certificate

Important
This part is very important and you must do it NOW.

After creating your keys you should create a revocation certificate. Doing this allows you to revoke your key in case something nasty happens to your key (someone gets hold of your key/passphrase).

user $ gpg --list-keys
/home/humpback/.gnupg/pubring.gpg
---------------------------------
pub  1024D/75447B14 2002-12-08 John Doe (The Real John Doe) <john@nowhere.someplace.flick>
sub  2048g/96D6CDAD 2002-12-08
user $ gpg --output revoke.asc --gen-revoke 75447B14
sec  1024D/75447B14 2002-12-08   John Doe (The Real John Doe) <john@nowhere.someplace.flick>
  
Create a revocation certificate for this key? y
Please select the reason for the revocation:   
  0 = No reason specified
  1 = Key has been compromised
  2 = Key is superseded
  3 = Key is no longer used
  Q = Cancel
(Probably you want to select 1 here)
Your decision? 1
Enter an optional description; end it with an empty line:
> Someone cracked me and got my key and passphrase
>
Reason for revocation: Key has been compromised
Someone cracked me and got my key and passphrase
Is this okay? y
  
You need a passphrase to unlock the secret key for
user: "John Doe (The Real John Doe) <john@nowhere.someplace.flick>"
1024-bit DSA key, ID 75447B14, created 2002-12-08
  
ASCII armored output forced.
Revocation certificate created.
  
Please move it to a medium which you can hide away; if Mallory gets
access to this certificate he can use it to make your key unusable.
It is smart to print this certificate and store it away, just in case
your media become unreadable.  But have some caution:  The print system of
your machine might store the data and make it available to others!

The gpg --list-keys command lists keys in your public keyring. You may use it to see the ID of your key so that you can create the revocation certificate. Now it is a good idea to copy all the .gnupg directory and the revocation certificate (in ASCII armor - revoke.asc) to some secure medium (two floppy's or a CD-R you store in safe location). Remember that revoke.asc can be used to revoke your keys and make them unusable in the future.

Note
If you have several email addresses that you would like to use with this key, you can run gpg --edit-key YOUR_ID and then use the adduid command. It will ask you for the name, email and comment of the second ID you will be using.

Exporting keys

To export your key, you type gpg --armor --output john.asc --export john@nowhere.someplace.flick. You can almost always use the key ID or something that identifies the key (here we used an email address). John now has a john.asc that he can send his friends, or place on his web page so that people can communicate safely with him.

Importing keys

To add files to your public keyring, you must first import it, then check the key fingerprint. After you have verified the fingerprint you should validate it.

Note
You should be careful when verifying keys. This is one of the weak points of public key cryptography.

Now we will be adding Luis Pinto's (a friend of mine) public key to our public keyring. After giving him a call and asking him for his key fingerprint, I compare the fingerprint with the output of the fpr command. As the key is authentic, I add it to the public keyring. In this particular case, Luis's key will expire in 2003-12-01 so I am asked if I want my signature on his key to expire at the same time.

user $ gpg --import luis.asc
gpg: key 462405BB: public key imported
gpg: Total number processed: 1
gpg:               imported: 1
user $ gpg --list-keys
/home/humpback/.gnupg/pubring.gpg
---------------------------------
pub  1024D/75447B14 2002-12-08 John Doe (The Real John Doe) <john@nowhere.someplace.flick>
sub  2048g/96D6CDAD 2002-12-08
  
pub  1024D/462405BB 2002-12-01 Luis Pinto <lmpinto@student.dei.uc.pt>
uid                            Luis Pinto <lmpinto@dei.uc.pt>
sub  4096g/922175B3 2002-12-01 [expires: 2003-12-01]
user $ gpg --edit-key lmpinto@dei.uc.pt
gpg (GnuPG) 1.0.7; Copyright (C) 2002 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: checking the trustdb
gpg: checking at depth 0 signed=0 ot(-/q/n/m/f/u)=0/0/0/0/0/1
pub  1024D/462405BB  created: 2002-12-01 expires: 2003-12-01 trust: -/-
sub  4096g/922175B3  created: 2002-12-01 expires: 2003-12-01
(1)  Luis Pinto <lmpinto@dei.uc.pt>
(2). Luis Pinto <lmpinto@student.dei.uc.pt>
Command> fpr
pub  1024D/462405BB 2002-12-01 Luis Pinto <lmpinto@dei.uc.pt>
             Fingerprint: F056 3697 ADE3 CF98 B80B  8494 0AD3 E57B 4624 05BB
Command> sign
Really sign all user IDs? y
                           
pub  1024D/462405BB  created: 2002-12-01 expires: 2003-12-01 trust: -/-
             Fingerprint: F056 3697 ADE3 CF98 B80B  8494 0AD3 E57B 4624 05BB
  
     Luis Pinto <lmpinto@dei.uc.pt>
     Luis Pinto <lmpinto@student.dei.uc.pt>
  
This key is due to expire on 2003-12-01.
Do you want your signature to expire at the same time? (Y/n) Y
How carefully have you verified the key you are about to sign actually belongs
to the person named above?  If you don't know what to answer, enter "0".
  
   (0) I will not answer. (default)
   (1) I have not checked at all.
   (2) I have done casual checking.
   (3) I have done very careful checking.
  
   Your selection? 3
Are you really sure that you want to sign this key
with your key: "John Doe (The Real John Doe) <john@nowhere.someplace.flick>"
  
I have checked this key very carefully.
  
Really sign? y
              
You need a passphrase to unlock the secret key for
user: "John Doe (The Real John Doe) <john@nowhere.someplace.flick>"
1024-bit DSA key, ID 75447B14, created 2002-12-08
Command> check
uid  Luis Pinto <lmpinto@dei.uc.pt>
sig!3       462405BB 2002-12-01   [self-signature]
sig!3       75447B14 2002-12-08   John Doe (The Real John Doe) <john@nowhe
uid  Luis Pinto <lmpinto@student.dei.uc.pt>
sig!3       462405BB 2002-12-01   [self-signature]
sig!3       75447B14 2002-12-08   John Doe (The Real John Doe) <john@nowhe

Exchanging keys with keyservers

Sending keys to keyservers

Now that you have your key, it is probably a good idea to send it to the world key server. There are a lot of keyservers in the world and most of them exchange keys between them. Here we are going to send John Doe's key to the subkeys.pgp.net server. This uses HTTP, so if you need to use a proxy for HTTP traffic don't forget to set it (export http_proxy=http://proxy_host:port/). The command for sending the key is: gpg --keyserver subkeys.pgp.net --keyserver-options honor-http-proxy --send-key 75447B14 where 75447B14 is the key ID. If you don't need a HTTP proxy you can remove the --keyserver-options honor-http-proxy.

You can also send other people's keys that you have signed to the keyserver. We could send Luis Pinto's key to the keyserver. This way someone who trusts your key can use the signature that you have placed there to trust Luis's key.

Getting Keys from keyservers

Now we are going to search for Gustavo Felisberto's key and add it to the keyring of John Doe (just in case you did not notice Gustavo Felisberto is the author this guide :)).

user $ gpg --keyserver subkeys.pgp.net --keyserver-options honor-http-proxy --search-keys humpback@felisberto.net
gpg: searching for "humpback@felisberto.net" from HKP server subkeys.pgp.net
Keys 1-5 of 5 for "humpback@felisberto.net"
(1)Gustavo Felisberto (apt-get install anarchy) <humpback@felisberto.net> 1024
  created 2002-12-06, key B9F2D52A
(2)Gustavo Felisberto <humpback@altavista.net> 1024
  created 1999-08-03, key E97E0B46
(3)Gustavo A.S.R. Felisberto <humpback@altavista.net> 1024
  created 1998-12-10, key B59AB043
(4)Gustavo Adolfo Silva Ribeiro Felisberto <humpback@altavista.net> 1024
  created 1998-08-26, key 39EB133D
(5)Gustavo Adolfo Silva Ribeiro Felisberto <humpback@altavista.net> 1024
  created 1998-06-14, key AE02AF87
  Enter number(s), N)ext, or Q)uit >1
gpg: requesting key B9F2D52A from HKP keyserver subkeys.pgp.net
gpg: key B9F2D52A: public key imported
gpg: Total number processed: 1
gpg:               imported: 1

As you can see from the server response I have a few keys submitted to the key server, but I currently only use B9F2D52A. Now John Doe can get it and sign it if he trusts it.

Using a GPG Agent

What is a GPG Agent?

Sometimes working with certain applications requires you to use your GPG key very frequently, which means that you have to type your passphrase a lot of times. Several applications used to support a passphrase caching mechanism to make life easier for users. However, this disallowed sharing this cache across programs (how secure would that be?) and forced applications to reinvent the wheel over and over again.

A GPG agent is a separate application that GPG uses to cache the passphrase in a standard and secure way. It allows applications to use GPG concurrently: if you enter your passphrase while working in one application, the other application can work with GPG without reiterating the request for the passphrase to unlock the key - if the GPG agent is configured to allow so, of course.

Gentoo provides a few GPG agent applications. The app-crypt/gnupg package contains what could be considered the reference one, and will be the one we'll use in this document.

Configuring gpg-agent and pinentry

GnuPG includes gpg-agent and pinentry. pinentry is the helper application that gpg-agent uses to request the passphrase in a graphical window. It comes in three flavors: it can popup a window using the gtk+, Qt, or curses library (depending on your USE flags in /etc/portage/make.conf).

If you installed pinentry with more than one popup window type, you can choose between them with eselect-pinentry:

root # eselect pinentry list
Available pinentry implementations:
  [1]   pinentry-gtk-2 *
  [2]   pinentry-curses
  [3]   pinentry-qt4
root # eselect pinentry set 1

Now create a file called ~/.gnupg/gpg-agent.conf and enter the following lines which define the default timeout of the passphrase (e.g. 30 minutes) and the application to be called for when the passphrase should be retrieved the first time (e.g. the gtk+ version of pinentry).

File~/.gnupg/gpg-agent.conf

pinentry-program /usr/bin/pinentry-gtk-2
no-grab
default-cache-ttl 1800

Now configure GnuPG to use an agent when appropriate. Edit ~/.gnupg/gpg.conf and add the following line:

File~/.gnupg/gpg.confConfiguring GnuPG to use a GPG Agent

use-agent

Now your system is (almost) set to use the GPG agent.

Automatically Starting the GPG Agent

If you use KDE as your graphical environment, edit /etc/kde/startup/agent-startup.sh and uncomment the following (system-wide) or ~/.kde4/env/gpg-agent.sh (local user) and add the following command to it to have KDE automatically starting the GPG agent:

File/etc/kde/startup/agent-startup.kshMake KDE automatically start the GPG agent

eval "$(gpg-agent --daemon)"

Additionally, uncomment the following in /etc/kde/shutdown/agent-shutdown.sh (system-wide) or add it to ~/.kde4/shutdown/gpg-agent.sh (local user):

File/etc/kde/shutdown/agent-shutdown.shMake KDE shut down the GPG agent

if [ -n "${GPG_AGENT_INFO}" ]; then
  kill $(echo ${GPG_AGENT_INFO} | cut -d':' -f 2) >/dev/null 2>&1
fi

If you use a different graphical environment, put that line (the same one as mentioned above) in ~/.xinitrc (if you use startx) or ~/.xsession (if you use XDM/GDM/KDM/...).

Working with documents

Encrypting and signing

Let's say that you have a file that you wish to send Luis. You can encrypt it, sign it, or encrypt it and sign it. Encrypting means that only Luis will be able to open it. The signature tells Luis that it was really you who created the file.

The next three commands will do just that, encrypt, sign and encrypt/sign.

user $ gpg --output doc.gpg --encrypt --recipient lmpinto@dei.uc.pt doc_to_encrypt
user $
gpg --output doc.gpg --sign --recipient lmpinto@dei.uc.pt doc_to_sign
user $
gpg --output doc.gpg --encrypt --sign --recipient lmpinto@dei.uc.pt doc_to_encrypt_and_sign

This will create binary files. If you wish to create ASCII files, just add a --clearsign to the beginning of the command.

Decrypting and verifying signatures

Suppose that you have received a file which is encrypted to you. The command to decrypt it is gpg --output document --decrypt encrypted_doc.gpg. This will decrypt the document and verify the signature (if there is one).

Encrypting and decrypting without keys

It is also possible to encrypt files using passwords instead of keys. Well, the password itself will function as the key - it will be used as a symmetric cypher. You can encrypt the file using gpg 's --symmetric argument; decrypting uses the same command as we talked about before.

user $ gpg --output document.gpg --symmetric document

GnuPG will ask for a passphrase and a passphrase verification.


Advanced Features

There are some nice advanced features in GnuPG. To find them, open the ~/.gnupg/gpg.conf file.

File~/.gnupg/gpg.conf

#keyserver x-hkp://subkeys.pgp.net
#keyserver-options auto-key-retrieve include-disabled include-revoked

Search for the above two lines and uncomment them. With this any time GnuPG needs to check a signature and it does not find the public key on the local keyring it will contact the key server at subkeys.pgp.net and will try to fetch it from there.

Another nice command is gpg --refresh-keys. This will contact the keyserver defined in the options file and refresh public keys in your local key ring from there, searching for revoked keys, new IDs, and new signatures on keys. You should probably run this once or twice a month so that if someone revokes his key you will be notified.

GnuPG interfaces

About email signatures

95% of the time you will use GnuPG with email, signing/encrypting your outgoing messages and reading signed/encrypted messages. So it is only fair that I talk about that first.

There are two ways two sign/encrypt a email with GnuPG, the old way and the new way :). In the old way messages would appear in plain text, with no possible formatting and attached files would be unsigned/unencrypted. Here is an example of a message signed the old way:

CodeA plain text signature

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
  
Test message
  
-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 6.5.8 for non-commercial use
  
iQA/AwUBP8461jMX0745gR7AEQIEOwCg011GbufXO3ED3FkLWXmfzg7xm1cAoJD0
0EU3Kd2EKNCqataEqM5qjpPs
=LchZ
-----END PGP SIGNATURE-----

Messages this way are no good in today's world, where we have nice GUIs and email readers that understand html.

To solve this an addition to the MIME (Multipurpose Internet Mail Extensions) was created. This adds a field to the email that tells the mail reader that the full content of the message is signed and/or encrypted. The problem with this is that not all mail readers support this. And some even mess up the content; Microsoft's Outlook is famous for not working with this.

Kgpg

Kgpg is a nice GUI for GnuPG. In the main screen you can paste the text that you wish to sign or encrypt, and you can also paste the ASCII armored text that you which to decrypt.

From within the main screen you can decrypt text (you will have to provide your password), encrypt other files, paste new text to sign....

Seahorse

Seahorse aims to be a GnuPG GUI interface for the Gnome desktop. The software has been evolving fast, but it still lacks many important features that can be found in Kgpg or the command line version.

KMail

If you have the crypt USE flag set, KMail will be compiled with gpg support, and will be able to encrypt and decrypt inline PGP mails automatically as well as encrypting OpenPGP/MIME mails. If you want to decrypt OpenPGP/MIME mails as well (which you probably want) you need to have a running GPG agent.

You can verify if KMail is properly configured by going to Settings, Configure KMail, Security, Crypto Backends. You should see a GpgME-based backend listed and you should be able to fill the OpenPGP checkbox. If it is listed but grayed out, click on Rescan. If the GpgME-based backend remains grayed out, KMail is not working properly.

If you still are unable to get KMail to behave, please see the KMail PGP HowTo page for more information.

Claws-Mail

This mail reader is very fast with big mailboxes, has all the nice features one wants in mail readers and works pretty well with gpg. The only problem is that it does not work with the old PGP signatures, so when you receive those kind of mails you have to hand check the signatures.

To use your gpg key with Claws-Mail just go to the account configuration and select the privacy tab. Once there just choose which key to use, probably most users will go with the default key.

Public Key Cryptography

Basic Public Key Cryptography

The concept of public key cryptography was originally devised by Whitfield Diffie and Martin Hellman in 1976. When I first heard the words "public key" and "cryptography" in the same sentence back in '93 I thought to myself that it would be impossible to do such a thing. In those days there was no Internet (well there was, but not for me) so I went to the public library and asked for books on Cryptography. I must say that I was 16 at the time so the clerk there looked to me in astonishment and brought me a book for children on substitution cyphers (those where you change a letter for another like the famous Caesar Cypher or ROT-13 (Tragbb Ebpxf, naq lbh xabj vg vf tbbq orpnhfr lbh ner ernqvat guvf qbp.), (emerge rotix if you cannot read the preceding text)). I was very upset with this and started to search for more info. It is good to have mathematicians in the family, because as soon as I talked to one of them I was introduced to a new world.

And now a bit of mathematics:

CodeMathematical Concepts

Definitions:
  
1- A prime number is a positive integer number greater than one that is only
divisible by 1 and itself (the remainder of the division is 0).
The first 8 prime numbers are 2,3,5,7,11,13,17,19
  
Theorem (No proof here)
1- For any non prime positive integer it is possible to break it as the product
of prime numbers, and that product is unique.
4=2*2
6=2*3
8=2*4=2*2*2
10=2*5
12=2*6=2*2*3
  
"Facts":
1- It is mathematically easy to multiply two large integers
2- It is hard to find the prime factors of a given positive integer.

If I give you the number 35 and I tell you that this number is the product of two prime numbers it is easy to find that it was 5 and 7. But if I tell you the same for 1588522601 you will spend alot of time (or CPU cycles) to find it was 49811*31891. And if this number is really really big this task becomes "impossible". So now if I give the world my large number that is the product of two primes I know something about that number that no one else knows.

This is the basis for Public Key Cryptography (PKC) implementations today. As an (unrealistic) example, I give anyone my number and that someone will use if for cyphering a message to me. Anyone can see the cyphered message, because I am the only one who knows a shortcut to read it, anyone else will first have to "split" that big number to be able to read the message, and it is a "fact" that it is impossible to do that in a short amount of time (todays methods and the fastest computers in the world would take thousands of years to do that). In this setup the two large prime numbers would be called the PRIVATE KEY, and the large non prime number is the PUBLIC KEY.

In practice this is not 100% accurate with reality, but will give a good idea to the newcomer. For more information, check Wikipedia on the Diffie-Hellman protocol. For even more info go to the public library and grab a copy of the "Handbook of Applied Cryptography" by Alfred J. Menezes, Paul C. van Oorschot and Scott A. Vanstone. This book is also available online for free at the above site.

One consequence of the above is that if you cypher a message to me, and you loose the original uncyphered message you will no longer be able to retrieve it from the cyphered version.

Signatures

We already saw how someone can send us a cyphered message if they have our public key. But how do we know that the author of the message is really who he claims to be? Or in other words: If I receive an email from you how do I really know it was you and not someone else claiming to be you?

Remember me saying that PKC was not as simple as I had said? The idea is that when you cypher a message to me you sign it with your private key so that, when I receive it, I can first use your public key to check your signature and then use my private key to decypher the message. As you can see we could not do that in the setup I described before.

It's also very important to sign messages so that you don't have to cypher them beforehand. Now you can create messages that can be read by anyone, but that come with your "branding". And if any single character is changed in the message it can (and will) be detected.

Key Servers and Signed Keys

But let's say that I have no previous contact with you until you send me a message: how do I get your public key, and how do I really know it is yours?

To solve this problem public Key Servers were created. When you create your key pair (Public and Private key), you send your public key to the key server. After this everyone can retrieve your key from there. This solves the problem of finding the key. But how do I really know that that key is the author's key? For this another concept must be introduced, and that is key signing:

Key signing means that if I have the public key of another person, and I know for sure that it is really that persons key (it is my personal friend, someone I know in real life, etc.) I can sign that public key and send it to keyservers, that way I am telling the world: "This key really belongs to the person it claims to belong.". That way persons that have my public key and trust me can use that trust to trust other keys.

This can sometimes be confusing so let's see a real world situation.

Let's imagine a 3 person situation: John, Mary, and Lisa. John is a good friend of Mary but does not know Lisa; Lisa is a good friend of Mary but does not know John. One day Lisa sends John a signed email. John will fetch Lisa's Public Key from the keyserver and test the message, if all went ok he will see that whoever wrote that message also created that key. But how do I know it was really the person it claims to be?

He then sees that it is signed by Mary, which he can check because he already has Mary's key and he trusts that key. With this ring of trust he continues to conclude that the email he received was really written by Lisa.

You are now ready to use this guide, you can go back to chapter 1 and learn how to use gpg.

Final thoughts and Credits

Some problems

I had some problems with photos in keys. Check the version you are using. If you have GnuPG 1.2.1-r1 and up you are probably OK, older versions may have problems. Also most keyservers don't like keys with photos, so you are better if you don't add photos.

The latest versions of gnupg don't seem to work with the gpg --send-keys that was used so send all keys in your keyring to the public server.

What is not here

gpg is a very complex tool, it lets you do much more than what I have covered here. This document is for the user who is new to GnuPG. For more information, you should check the GnuPG Website.

I did not write about other tools like pgp4pine, gpgpine, evolution and maybe Windows tools, but I will probably extend this document in the future.

Credits

John Michael Ashley's GnuPG Handbook it is a very good book for beginners.

Swift (Sven Vermeulen) for pushing me to re-write this.

Everyone in the #gentoo-doc team you guys rock.

Tiago Serra for getting me back to the privacy track.

Acknowledgements

We would like to thank the following authors and editors for their contributions to this guide:


  • Gustavo Felisberto
  • John P. Davis
  • Sven Vermeulen
  • nightmorph