Encripted email with S/MIME

ok, proviamo a cifrare le nostre mail esattamente con lo stesso metodo in cui vengono cifrate le informazioni dei siti web che usano https, ovvero con openSSl.
Il meccanismo è piuttosto simile a quello client/server appunto, ma avviene tra due pc di utenti.
Il meccanismo ha analogie anche con PGP perchè funziona a coppie di chiavi, una privata ed una pubblica. Quindi per fare in modo che Margherita possa scriverti, tu devi inviarle la tua chiave pubblica e tenere ben segreta l’altra. Anche lei ti invierà la sua chiave pubblica e così le potrai scrivere. Tu saprai che è davvero lei che ti scrive perché firmerà la lettera con la sua chiave privata e viceversa. Tu sai anche che stai usando la sua reale chiave pubblica e quindi inviando davvero a lei il messaggio cifrato, perché la sua chiave pubblica è firmata da altre persone sue amiche e magari anche da te stessa dopo un primo utilizzo.
Vediamo…

1 Installa OpenSSL

$ sudo apt-get install openssl

$ openssl

Dovrebbe darti >
che significa ora sei in una shell openssl. Fai ctrl+c per uscire.
 

2 Crea il tuo certificato SSL

Crea una cartella e vacci dentro:

mkdir /home/ctest/ssl
cd /home/ctest/ssl

Ora pensa ad una buona pasw, e genera la tua chiave segreta:

openssl genrsa -des3 -out cert.key 4096

Vedrai una roba simile:

ctest-System-Product-Name ssl # openssl genrsa -des3 -out cert.key 4096
Generating RSA private key, 4096 bit long modulus
.......++
...............................++
e is 65537 (0x10001)
Enter pass phrase for cert.key:

Ora genera la richiesta di firma per la tua chiave, ovvero il file con estenzione csr:Certification Sign Request, una sorta di certificato intermedio:

openssl req -new -key cert.key -out cert.csr

Puoi non mettergli niente in nessun campo,TRANNE quello della email. Se metti qualche valore in Common name puoi far confondere i server, quello proprio meglio non metterlo.

ctest-System-Product-Name ssl # openssl req -new -key cert.key -out cert.csr Enter pass phrase for cert.key:
 You are about to be asked to enter information that will be incorporated
 into your certificate request.
 What you are about to enter is what is called a Distinguished Name or a DN.
 There are quite a few fields but you can leave some blank
 For some fields there will be a default value,
 If you enter '.', the field will be left blank.
 -----
 Country Name (2 letter code) [AU]:
 State or Province Name (full name) [Some-State]:
 Locality Name (eg, city) []:
 Organization Name (eg, company) [Internet Widgits Pty Ltd]:
 Organizational Unit Name (eg, section) []:
 Common Name (eg, YOUR name) []:
 Email Address []:
Please enter the following 'extra' attributes
 to be sent with your certificate request
 A challenge password []:
 An optional company name []:
 ctest-System-Product-Name ssl #

Ora firma questa richiesta intermedia di firma:
Fai conto che questa procedura viene di solito fatta da una certification autoriti, in questo caso invece tu ti sei creata la richiesta e tu te la firmi!

openssl x509 -req -days 365 -in cert.csr -signkey cert.key -out cert.crt

Ti dirà:

ctest-System-Product-Name ssl # openssl x509 -req -days 365 -in cert.csr -signkey cert.key -out cert.crt
 Signature ok
 [...]
 Getting Private key
 Enter pass phrase for cert.key:
 ctest-System-Product-Name ssl #

Generally, the certificate is created now, but we need to do one more step to be able to use it on our mail clients. These expect the certificates to be in the .p12 format, which is why we need to convert them. Do so with (Replace Your Name with something serious, your name for example. This is what the certificate will be called.):

openssl pkcs12 -export -in cert.crt -inkey cert.key -name “Your Name” -out cert.p12

Puoi decidere una nuova pasw, differente dall’altra, che serve per importare il certificato nel tuo PC:
ctest-System-Product-Name ssl # openssl pkcs12 -export -in cert.crt -inkey cert.key -name “Your Name” -out cert.p12 Enter pass phrase for cert.key: Enter Export Password: Verifying – Enter Export Password: ctest-System-Product-Name ssl #
Alla fine se li vuoi listare tutti sono:

ls -l
ctest-System-Product-Name ssl # ls -l
 total 20
 -rw-r--r-- 1 root root 2004 2011-11-28 12:52 cert.crt
 -rw-r--r-- 1 root root 1752 2011-11-28 12:32 cert.csr
 -rw-r--r-- 1 root root 3311 2011-11-28 12:31 cert.key
 -rw-r--r-- 1 root root 4290 2011-11-28 12:53 cert.p12
 ctest-System-Product-Name ssl #

Ovvero:
.key  è la tua chiave privata
.csr è la tua richiesta di firma
.crt è il certificato firmato
.p12 è un altro formato con la chiave privata e la tua pubblica ed un’altra pasw che ti serve per decifrare le tue email.
Se i permessi non sono giusti, digita:

$ chmod 0644 *

3 Importa le chiavi in Thunderbird

ora hai da importare il certificato tuo nel tuo client di posta, che ti chiederà questa ultima pasw inserita e poi hai da importare le chiavi pubbliche dei tuoi amici.
Apri thunderbird, io sto usando la 52.3;
Modifica > preferenze > avanzate > certificati
Importa il tuo file cert.p12
Ora lo devi associare al tuo account mail in thunderbird, quindi fai:
Modifica > impostazioni account > sicurezza
 


fonte: https://www.howtoforge.com/how-to-encrypt-mails-with-ssl-certificates-s-mime