« PGP - GPG » : différence entre les versions

De Justine's wiki
Aller à la navigation Aller à la recherche
(Page créée avec « = Sources = * [https://grimoire.carcano.ch/blog/a-quick-easy-yet-comprehensive-gpg-tutorial/ Ce post de blog qui n'est pas si rapide qu'il le dit] = Signification, désambiguation = PGP (Pretty Good Privacy) est un logiciel de chiffrement crée par Phil Zimmerman, qui utilise une suite de hashage, chiffrement, clefs symétriques et asymétriques, etc... dans le but de fournir du chiffrement et de l'authentification dans les communications. Il peut être utilisé... »)
 
Aucun résumé des modifications
Ligne 88 : Ligne 88 :
|-
|-
|}
|}
On peut voir les capacités avec la commande suivante (ne pas aller au bout du process, c'est juste pour avoir la liste):
<nowiki>
baz@debian:~$ gpg --expert --full-gen-key
gpg (GnuPG) 2.2.27; Copyright (C) 2021 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)
  (9) ECC and ECC
  (10) ECC (sign only)
  (11) ECC (set your own capabilities)
  (13) Existing key
  (14) Existing key from card
Your selection?
</nowiki>
=== Mise en oeuvre ===
==== Clefs RSA ====
L'environnement d'un utilisateur est initialisé dès qu'il génère ou importe sa clef privée. On conseille les arguments --expert et --full-gen-key pour activer toutes les features et tous les types de clefs. On génère les clefs de "bar" en ssh (vraiment pas compréhensible...)
<nowiki>
ssh -t bar@localhost "gpg --quick-gen-key 'Bar User (Bar User Primary Key) <bar@squi.fr>' rsa sign,cert never"
</nowiki>
On nous demande un mdp pour la clef, puis on nous dit que:
<nowiki>
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: /home/bar/.gnupg/trustdb.gpg: trustdb created
gpg: key 7BF3AF5FF1E9220A marked as ultimately trusted
gpg: directory '/home/bar/.gnupg/openpgp-revocs.d' created
gpg: revocation certificate stored as '/home/bar/.gnupg/openpgp-revocs.d/F12E5320C9CA75322FD0BDB57BF3AF5FF1E9220A.rev'
public and secret key created and signed.
Note that this key cannot be used for encryption.  You may want to use
the command "--edit-key" to generate a subkey for this purpose.
pub  rsa3072 2023-05-13 [SC]
      F12E5320C9CA75322FD0BDB57BF3AF5FF1E9220A
uid                      Bar User (Bar User Primary Key) <bar@squi.fr>
Connection to localhost closed.
</nowiki>
Cette clef ne peut pas être utilisée pour le chiffrement, seulement pour identication (cf le tableau au-dessus). La bonne pratique de toute façon consiste à éviter d'utiliser la clef primaire, mais plutôt à générer des subkeys et à planquer la clef primaire en lieu sûr. On évite de se faire voler sa clef primaire ou de la perdre suite à une panne. Il est important de la stocker dans un endroit safe, autrement on ne peut plus rien faire !
Bref, on va faire la même chose en passant directement sur un compte parce qu'on a rien compris à la commande au-dessus. Baz, à toi.
<nowiki>
baz@debian:~$ gpg --pinentry-mode loopback --quick-gen-key 'Baz User (Baz User Primary Key) <baz@squi.fr>' rsa sign,cert never
#Ici, on nous demande un mdp
gpg: directory '/home/baz/.gnupg' created
gpg: keybox '/home/baz/.gnupg/pubring.kbx' created
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: /home/baz/.gnupg/trustdb.gpg: trustdb created
gpg: key 90E0D47DC4115033 marked as ultimately trusted
gpg: directory '/home/baz/.gnupg/openpgp-revocs.d' created
gpg: revocation certificate stored as '/home/baz/.gnupg/openpgp-revocs.d/634B60C0CE2AEE31F2EE62B990E0D47DC4115033.rev'
public and secret key created and signed.
Note that this key cannot be used for encryption.  You may want to use
the command "--edit-key" to generate a subkey for this purpose.
pub  rsa3072 2023-05-13 [SC]
      634B60C0CE2AEE31F2EE62B990E0D47DC4115033
uid                      Baz User (Baz User Primary Key) <baz@squi.fr>
</nowiki>
On voit ici qu'on génère une clef RSA, qui n'expire jamais, et a les capacités Sign et Cert (correspondant au choix " (8) RSA (set your own capabilities)").
On fait un exit du compte de baz.
==== Clefs "Elliptic Curve" ====
On va générer à foo une clef de type:
* ECC avec nistp256 comme elliptic curve
* Sign et Cert
* N'expire pas
<nowiki>
justine@debian:~$ sudo -u foo gpg --pinentry-mode loopback --quick-gen-key 'Foo User (Foo User Primary Key) <foo@squi.fr>' nistp256 sign,cert never
#Nous refait le même speech qu'en RSA
</nowiki>
Pareil pour qux et fred mais en RSA:
<nowiki>
justine@debian:~$ sudo -u qux gpg --pinentry-mode loopback --quick-gen-key 'Qux User (Qux User Primary Key) <qux@squi.fr>' rsa sign,cert never
justine@debian:~$ sudo -u fred gpg --pinentry-mode loopback --quick-gen-key 'Fred User (Fred User Primary Key) <fred@squi.fr>' rsa sign,cert never
</nowiki>
==== Génération de subkey ====
On va prendre le compte de foo pour générer une subkey à des fins de chiffrement.
Voici l'intégralité de l'extrait - j'ai dû rajouter --pinentrymode=loopback sinon erreur. En gros, on choisit une clef ECC (encrypt only), avec la curve NIST P-256, valable 1 an:
<nowiki>
foo@debian:~$ gpg --verbose --expert --pinentry-mode=loopback --edit-key foo@squi.fr addkey
Secret key is available.
gpg: using pgp trust model
sec  nistp256/14692EE589B4006C
    created: 2023-05-13  expires: never      usage: SC 
    trust: ultimate      validity: ultimate
[ultimate] (1). Foo User (Foo User Primary Key) <foo@squi.fr>
Please select what kind of key you want:
  (3) DSA (sign only)
  (4) RSA (sign only)
  (5) Elgamal (encrypt only)
  (6) RSA (encrypt only)
  (7) DSA (set your own capabilities)
  (8) RSA (set your own capabilities)
  (10) ECC (sign only)
  (11) ECC (set your own capabilities)
  (12) ECC (encrypt only)
  (13) Existing key
  (14) Existing key from card
Your selection? 12
Please select which elliptic curve you want:
  (1) Curve 25519
  (3) NIST P-256
  (4) NIST P-384
  (5) NIST P-521
  (6) Brainpool P-256
  (7) Brainpool P-384
  (8) Brainpool P-512
  (9) secp256k1
Your selection? 3
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 Sun 12 May 2024 12:39:44 PM CEST
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.
gpg: writing key binding signature
gpg: ECDSA/SHA512 signature from: "14692EE589B4006C Foo User (Foo User Primary Key) <foo@squi.fr>"
sec  nistp256/14692EE589B4006C
    created: 2023-05-13  expires: never      usage: SC 
    trust: ultimate      validity: ultimate
ssb  nistp256/6031A802EF033CDE
    created: 2023-05-13  expires: 2024-05-12  usage: E 
[ultimate] (1). Foo User (Foo User Primary Key) <foo@squi.fr>
</nowiki>
Voir la fin où nous donne les clefs dispo, avec notre "ssb  nistp256/6031A802EF033CDE" nouvellement créée.
On sauvegarde:
gpg> save
!!!!!CONTINUER à "Let's create an signing subkey also for the "baz" user - just exit " sur https://grimoire.carcano.ch/blog/a-quick-easy-yet-comprehensive-gpg-tutorial/

Version du 13 mai 2023 à 10:43

Sources

Signification, désambiguation

PGP (Pretty Good Privacy) est un logiciel de chiffrement crée par Phil Zimmerman, qui utilise une suite de hashage, chiffrement, clefs symétriques et asymétriques, etc... dans le but de fournir du chiffrement et de l'authentification dans les communications. Il peut être utilisé pour signer, chiffrer du texte, des mails, des fichiers, et même des partitions de disque. PGP suit le standard OpenPGP ainsi que la RFC 4880.

GPG (Pour "Gnu Privacy Guard", ou GnuPG ce qui est vachement mieux) est une *implémentation* de PGP. Il est intéropérable avec les autres car compliant avec la RFC4880.

Mise en oeuvre de GnuPG

Je pars d'une machine avec différent users pour simuler le fait de se connecter en SSH.

Plugin VIM

La source donne un plugin vim permettant de lire les fichiers chiffrés à la volée - je le met en oeuvre, je cite :

sudo mkdir -p  /etc/skel/.vim/pack/bundle/start
sudo apt install -y git
sudo git clone https://github.com/jamessan/vim-gnupg.git /etc/skel/.vim/pack/bundle/start/vim-gnupg

Créer /etc/skel/.vimrc avec:

/etc/skel/.vimrc

Puis enfin, on donne des variables relatives à l'agent pour qu'il fonctionne correctement (je passe les détails...)

sudo bash -c "cat >> /etc/profile.d/gpg.sh" << "EOF"
GPG_TTY=`tty`
export GPG_TTY
EOF

Users

Ma machine a 5 users (foo, bar, baz, qux, fred) en plus du mien. On pourra se connecter en SSH local pour simuler.

GnuPG

Il est installé de base sur Deb12, en version 2.2.27 dans mon cas:

justine@debian:~$ gpg --version
gpg (GnuPG) 2.2.27
libgcrypt 1.8.8
Copyright (C) 2021 Free Software Foundation, Inc.
License GNU GPL-3.0-or-later <https://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Home: /home/justine/.gnupg
Supported algorithms:
Pubkey: RSA, ELG, DSA, ECDH, ECDSA, EDDSA
Cipher: IDEA, 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH,
        CAMELLIA128, CAMELLIA192, CAMELLIA256
Hash: SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224
Compression: Uncompressed, ZIP, ZLIB, BZIP2

On voit qu'ECDH est supporté. On peut lister les curves ECDH supportées avec :

justine@debian:~$ gpg --with-colons --list-config curve
cfg:curve:cv25519;ed25519;nistp256;nistp384;nistp521;brainpoolP256r1;brainpoolP384r1;brainpoolP512r1;secp256k1

Fonctionnement et Génération des clefs

Quoi qu'est-ce ?

GPG utilise à la fois le chiffrement symétrique et asymétrique:

  • Des clefs publiques pour chiffrer les fichiers et vérifier les signatures
  • Des clefs privées pour signer les fichiers et les déchiffrer.

On a différent type de clefs avec différentes capacités.

Ces clefs sont appellées:

  • Keys (clefs primaires)  : ont des capacités de chiffrement et signature. "sec" identifie la clef secrète et "pub" la clef publique.
  • Subkeys (clefs liées à la clef primaire) : ssb est la subkey secrète, sub est la subkey publique.
Tableau des capacités
Capacité Description Key Subkey
C Certification - Certifie une identité. Une subkey ne peut pas le faire. X
S Signer d'autres clefs ou des données. X X
E Chiffrer ("encrypting"). X
A Authent. Par exemple, SSH, TLS avec GnuTLS... X

On peut voir les capacités avec la commande suivante (ne pas aller au bout du process, c'est juste pour avoir la liste):

baz@debian:~$ gpg --expert --full-gen-key
gpg (GnuPG) 2.2.27; Copyright (C) 2021 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)
   (9) ECC and ECC
  (10) ECC (sign only)
  (11) ECC (set your own capabilities)
  (13) Existing key
  (14) Existing key from card
Your selection?

Mise en oeuvre

Clefs RSA

L'environnement d'un utilisateur est initialisé dès qu'il génère ou importe sa clef privée. On conseille les arguments --expert et --full-gen-key pour activer toutes les features et tous les types de clefs. On génère les clefs de "bar" en ssh (vraiment pas compréhensible...)

ssh -t bar@localhost "gpg --quick-gen-key 'Bar User (Bar User Primary Key) <bar@squi.fr>' rsa sign,cert never"

On nous demande un mdp pour la clef, puis on nous dit que:

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: /home/bar/.gnupg/trustdb.gpg: trustdb created
gpg: key 7BF3AF5FF1E9220A marked as ultimately trusted
gpg: directory '/home/bar/.gnupg/openpgp-revocs.d' created
gpg: revocation certificate stored as '/home/bar/.gnupg/openpgp-revocs.d/F12E5320C9CA75322FD0BDB57BF3AF5FF1E9220A.rev'
public and secret key created and signed.

Note that this key cannot be used for encryption.  You may want to use
the command "--edit-key" to generate a subkey for this purpose.
pub   rsa3072 2023-05-13 [SC]
      F12E5320C9CA75322FD0BDB57BF3AF5FF1E9220A
uid                      Bar User (Bar User Primary Key) <bar@squi.fr>

Connection to localhost closed.

Cette clef ne peut pas être utilisée pour le chiffrement, seulement pour identication (cf le tableau au-dessus). La bonne pratique de toute façon consiste à éviter d'utiliser la clef primaire, mais plutôt à générer des subkeys et à planquer la clef primaire en lieu sûr. On évite de se faire voler sa clef primaire ou de la perdre suite à une panne. Il est important de la stocker dans un endroit safe, autrement on ne peut plus rien faire !

Bref, on va faire la même chose en passant directement sur un compte parce qu'on a rien compris à la commande au-dessus. Baz, à toi.

baz@debian:~$ gpg --pinentry-mode loopback --quick-gen-key 'Baz User (Baz User Primary Key) <baz@squi.fr>' rsa sign,cert never
#Ici, on nous demande un mdp
gpg: directory '/home/baz/.gnupg' created
gpg: keybox '/home/baz/.gnupg/pubring.kbx' created
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: /home/baz/.gnupg/trustdb.gpg: trustdb created
gpg: key 90E0D47DC4115033 marked as ultimately trusted
gpg: directory '/home/baz/.gnupg/openpgp-revocs.d' created
gpg: revocation certificate stored as '/home/baz/.gnupg/openpgp-revocs.d/634B60C0CE2AEE31F2EE62B990E0D47DC4115033.rev'
public and secret key created and signed.

Note that this key cannot be used for encryption.  You may want to use
the command "--edit-key" to generate a subkey for this purpose.
pub   rsa3072 2023-05-13 [SC]
      634B60C0CE2AEE31F2EE62B990E0D47DC4115033
uid                      Baz User (Baz User Primary Key) <baz@squi.fr>

On voit ici qu'on génère une clef RSA, qui n'expire jamais, et a les capacités Sign et Cert (correspondant au choix " (8) RSA (set your own capabilities)").

On fait un exit du compte de baz.

Clefs "Elliptic Curve"

On va générer à foo une clef de type:

  • ECC avec nistp256 comme elliptic curve
  • Sign et Cert
  • N'expire pas
justine@debian:~$ sudo -u foo gpg --pinentry-mode loopback --quick-gen-key 'Foo User (Foo User Primary Key) <foo@squi.fr>' nistp256 sign,cert never
#Nous refait le même speech qu'en RSA

Pareil pour qux et fred mais en RSA:

justine@debian:~$ sudo -u qux gpg --pinentry-mode loopback --quick-gen-key 'Qux User (Qux User Primary Key) <qux@squi.fr>' rsa sign,cert never
justine@debian:~$ sudo -u fred gpg --pinentry-mode loopback --quick-gen-key 'Fred User (Fred User Primary Key) <fred@squi.fr>' rsa sign,cert never

Génération de subkey

On va prendre le compte de foo pour générer une subkey à des fins de chiffrement.

Voici l'intégralité de l'extrait - j'ai dû rajouter --pinentrymode=loopback sinon erreur. En gros, on choisit une clef ECC (encrypt only), avec la curve NIST P-256, valable 1 an:

foo@debian:~$ gpg --verbose --expert --pinentry-mode=loopback --edit-key foo@squi.fr addkey
Secret key is available.

gpg: using pgp trust model
sec  nistp256/14692EE589B4006C
     created: 2023-05-13  expires: never       usage: SC  
     trust: ultimate      validity: ultimate
[ultimate] (1). Foo User (Foo User Primary Key) <foo@squi.fr>

Please select what kind of key you want:
   (3) DSA (sign only)
   (4) RSA (sign only)
   (5) Elgamal (encrypt only)
   (6) RSA (encrypt only)
   (7) DSA (set your own capabilities)
   (8) RSA (set your own capabilities)
  (10) ECC (sign only)
  (11) ECC (set your own capabilities)
  (12) ECC (encrypt only)
  (13) Existing key
  (14) Existing key from card
Your selection? 12
Please select which elliptic curve you want:
   (1) Curve 25519
   (3) NIST P-256
   (4) NIST P-384
   (5) NIST P-521
   (6) Brainpool P-256
   (7) Brainpool P-384
   (8) Brainpool P-512
   (9) secp256k1
Your selection? 3
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 Sun 12 May 2024 12:39:44 PM CEST
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.
gpg: writing key binding signature
gpg: ECDSA/SHA512 signature from: "14692EE589B4006C Foo User (Foo User Primary Key) <foo@squi.fr>"

sec  nistp256/14692EE589B4006C
     created: 2023-05-13  expires: never       usage: SC  
     trust: ultimate      validity: ultimate
ssb  nistp256/6031A802EF033CDE
     created: 2023-05-13  expires: 2024-05-12  usage: E   
[ultimate] (1). Foo User (Foo User Primary Key) <foo@squi.fr>

Voir la fin où nous donne les clefs dispo, avec notre "ssb nistp256/6031A802EF033CDE" nouvellement créée.

On sauvegarde:

gpg> save

!!!!!CONTINUER à "Let's create an signing subkey also for the "baz" user - just exit " sur https://grimoire.carcano.ch/blog/a-quick-easy-yet-comprehensive-gpg-tutorial/