Sicurezza e GPG

Loris Tissino

16 maggio 2009

GPG

Il programma standard usato per la crittografia privata è GPG (GNU Privacy Guard), implementazione libera di PGP.

GPG offre funzionalità di cifratura a chiave simmetrica e a chiave pubblica.

Cifratura simmetrica

Per cifrare un documento si può utilizzare l'opzione -c (--symmetric):

$ ls
miodocumento.txt

$ gpg --symmetric --force-mdc miodocumento.txt
Enter passphrase:
Repeat passphrase:

$ ls
miodocumento.txt  miodocumento.txt.gpg

(MDC sta per modification detection code: usando l'opzione --force-mdc vengono introdotti dei controlli aggiuntivi relativi all'assenza di modifiche al documento)

Decifrazione simmetrica

Per decifrare il documento si utilizza gpg senza opzioni:

$ ls
miodocumento.txt.gpg

$ gpg miodocumento.txt.gpg 
gpg: CAST5 encrypted data
gpg: encrypted with 1 passphrase
Enter passphrase:

$ ls
miodocumento.txt  miodocumento.txt.gpg

Generazione della chiave con GPG

Per la generazione di una coppia di chiavi si usa il programma gpg con l'opzione --gen-key

(Quando molti utenti generano le coppie di chiavi su un calcolatore raggiunto via ssh può essere utile avviare rngd -r /dev/urandom al fine di non incappare nel problema della mancanza di un numero sufficiente di valori casuali. Questo però solo a scopo di test/esercitazione, visto che /dev/random non è valida fonte di entropia.)

Tipo, lunghezza, durata della chiave

Innanzitutto, viene chiesto di scegliere il tipo di chiave, la lunghezza, la durata.

$ gpg --gen-key

Please select what kind of key you want:
   (1) DSA and Elgamal (default)
   (2) DSA (sign only)
   (5) RSA (sign only) 
Your selection? 1
...
What keysize do you want? (2048) 1024
Requested keysize is 1024 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
Is this correct? (y/N) y

Dati personali

Successivamente, vengono richiesti i dati personali (nome, indirizzo e-mail, commento) e la passphrase:

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: Bob
Email address: bob@mattiussilab.net
Comment: 
You selected this USER-ID:
    "Bob <bob@mattiussilab.net>"

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

Generazione effettiva

Infine, le chiavi vengono effettivamente generate. Per aumentare la casualità dei valori, viene raccomandato di muovere il mouse e/o premere dei tasti durante la fase di generazione:

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

Rapporto di generazione

Infine, viene mostrato il rapporto di su cosa è stato creato:

gpg: /home/bob/.gnupg/trustdb.gpg: trustdb created
gpg: key 0488EA60 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:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
pub   1024D/0488EA60 2009-05-16
  Key fingerprint = 5ACD FD1E A2F9 BE7E 9A4A  B2A4 DD20 6BA2 0488 EA60
uid                  Bob <bob@mattiussilab.net>
sub   1024g/42257BFD 2009-05-16

Firma digitale di un documento

Per firmare digitalmente un documento si usa l'opzione -s (o --sign):

$ ls
miodocumento.txt
$ gpg --sign miodocumento.txt 

You need a passphrase to unlock the secret key for
user: "Bob <bob@mattiussilab.net>"
1024-bit DSA key, ID 0488EA60, created 2009-05-16

$ ls
miodocumento.txt  miodocumento.txt.gpg

Firma in file separato

Se lo si desidera, la firma può consistere in un file separato:

$ gpg --sign --detach-sign miodocumento.txt 
$ ls
miodocumento.txt  miodocumento.txt.sig

Verifica della firma digitale

Una firma digitale può essere verificata con l'opzione --verify:

$ gpg --verify miodocumento.txt.sig 
gpg: Signature made sab 16 mag 2009 15:18:57 CEST using DSA key ID 0488EA60
gpg: Good signature from "Bob <bob@mattiussilab.net>"

Nel caso di un file che incorpora documento e firma, il documento può essere estratto direttamente:

$ ls
miodocumento.txt.gpg
$ gpg miodocumento.txt.gpg 
gpg: Signature made sab 16 mag 2009 15:22:16 CEST using DSA key ID 0488EA60
gpg: Good signature from "Bob <bob@mattiussilab.net>"
$ ls
miodocumento.txt  miodocumento.txt.gpg

Fallimento della verifica

Se il file originale viene modificato, la verifica ovviamente fallisce:

$ # dopo aver modificato il file miodocumento.txt...

$ gpg miodocumento.txt.sig 
gpg: Signature made sab 16 mag 2009 15:55:26 CEST using DSA key ID 0488EA60
gpg: BAD signature from "Bob <bob@mattiussilab.net>"

Cifratura per un particolare destinatario

Per cifrare un documento in modo che possa essere letto solo da un particolare destinatario, è necessario specificare il suo ID e usare l'opzione -e (--encrypt):

$ ls 
miodocumento.txt
$ gpg --encrypt --recipient "Bob" miodocumento.txt 
$ ls
miodocumento.txt  miodocumento.txt.gpg

L'ID è la combinazione del nome e del commento (quest'ultimo tra parentesi).

Decifrazione del contenuto da parte del destinatario

Il legittimo destinatario può usare gpg per decifrare il contenuto del file, usando la passphrase per sbloccare la chiave privata:

$ gpg miodocumento.txt.gpg 

You need a passphrase to unlock the secret key for
user: "Bob <bob@mattiussilab.net>"
1024-bit ELG-E key, ID 42257BFD, created 2009-05-16 (main key ID 0488EA60)

gpg: encrypted with 1024-bit ELG-E key, ID 42257BFD, created 2009-05-16
      "Bob <bob@mattiussilab.net>"

Trasformazione dei file binari in file di testo (e viceversa)

I file binari prodotti da gpg possono essere trasformati in file di testo mediante la loro codifica in base 64:

$ gpg --enarmor miodocumento.txt.sig 

$ ls
miodocumento.txt  miodocumento.txt.sig  miodocumento.txt.sig.asc

$ cat miodocumento.txt.sig.asc 
-----BEGIN PGP ARMORED FILE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Use "gpg --dearmor" for unpacking

iEYEABECAAYFAkoOu5UACgkQ3SBrogSI6mCZGwCfWKTU4b659p8QVBhOWcHJ6P7l
Mt4AmgMAsQUr7RuxB50ZDB0RBOIVauj2
=V1yF
-----END PGP ARMORED FILE-----

Esportazione della propria chiave pubblica

Se vogliamo esportare la nostra chiave pubblica (per consegnarla ad un nostro corrispondente) possiamo usare l'opzione --export di gpg. Ad esempio, Alice potrebbe esportare la propria chiave così:

alice@biegacz:~$ gpg --output alice_pubkey.txt --armor --export

Importazione della chiave pubblica altrui nel proprio portachiavi

Avendo a disposizione una chiave pubblica altrui possiamo importarla semplicemente con l'opzione --import di gpg:

$ gpg --import alice_pubkey.txt 
gpg: key 65BC13ED: public key "Alice (Ufficio Acquisti) <alice@mattiussilab.net>" imported
gpg: Total number processed: 1
gpg:               imported: 1

Elenco delle chiavi del proprio portachiavi

L'elenco delle chiavi a disposizione può essere ottenuto con l'opzione --list-keys:

$ gpg --list-keys
/home/bob/.gnupg/pubring.gpg
-------------------------------
pub   1024D/0488EA60 2009-05-16
uid                  Bob <bob@mattiussilab.net>
sub   1024g/42257BFD 2009-05-16

pub   1024D/65BC13ED 2009-05-16
uid                  Alice (Ufficio Acquisti) <alice@mattiussilab.net>
sub   1024g/D67E74AF 2009-05-16