tech:notes_hsm
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédente | |||
| tech:notes_hsm [2025/04/14 15:05] – Jean-Baptiste | tech:notes_hsm [2026/07/16 12:01] (Version actuelle) – Jean-Baptiste | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | < | ||
| + | {{tag> | ||
| + | |||
| + | # Notes HSM | ||
| + | |||
| + | Todo : | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * http:// | ||
| + | * https:// | ||
| + | |||
| + | Matériel : | ||
| + | * https:// | ||
| + | * Nitrokey HSM | ||
| + | |||
| + | Logiciels : | ||
| + | * hsmwiz | ||
| + | * sc-hsm-tool | ||
| + | * Pkcs11Admin | ||
| + | * https:// | ||
| + | * https:// | ||
| + | |||
| + | Logiciel pour simuler une HSM | ||
| + | * https:// | ||
| + | |||
| + | Tuto : | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | |||
| + | Voir aussi : | ||
| + | * https:// | ||
| + | * RSA / ECDSA / ECDH (no perfect forward secrecy) | ||
| + | |||
| + | |||
| + | ~~~bash | ||
| + | hsmwiz | ||
| + | pkcs15-tool --dump | ||
| + | |||
| + | opensc-explorer | ||
| + | # Si KO faire : | ||
| + | hsmwiz explorer | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | The DKEK must be set during initialization and before any other keys are generated. For a device initialized without a DKEK, keys can never be exported. | ||
| + | |||
| + | ~~~bash | ||
| + | #set +o history | ||
| + | export HISTCONTROL = ignorespace | ||
| + | | ||
| + | ~~~ | ||
| + | |||
| + | ~~~bash | ||
| + | #set +o history | ||
| + | export HISTCONTROL = ignorespace | ||
| + | | ||
| + | ~~~ | ||
| + | |||
| + | ~~~bash | ||
| + | sc-hsm-tool --create-dkek-share dkek-share-1.pbe --pwd-shares-threshold 3 --pwd-shares-total 5 | ||
| + | ~~~ | ||
| + | |||
| + | Encrypting Files | ||
| + | |||
| + | The device only supports asymmetric mechanisms. To do file encryption, you will need to generate AES keys locally, which you can encrypt with your RSA public key (this is how the Nitrokey storage key works); or by using ECDH to generate a shared secret from a locally generated public key | ||
| + | |||
| + | ECDH | ||
| + | Where possible, e.g. over networks, you should use ephemeral keys, to allow for perfect forward secrecy. Smartcard HSM’s ECDH is only useful when need to repeatedly retrieve the same shared secret, e.g. encrypting files in a hybrid cryptosystem. | ||
| + | |||
| + | |||
| + | ## Notes | ||
| + | |||
| + | ### pkcs15-init | ||
| + | |||
| + | Please note, that the SmartCard-HSM is not compatible with the pkcs15-init command. In particular it does not support pkcs15-init to import a key from PKCS#12 files. Doing so will just create certificate objects and the private key metadata, but no key. Please use the Smart Card Shell to import keys and certificates from PKCS#12 files. | ||
| + | |||
| + | Source: https:// | ||
| + | |||
| + | Semble contradictoire avec : | ||
| + | * https:// | ||
| + | |||
| + | ## Initialisation / Réinitialiseation (Reset) | ||
| + | |||
| + | Voir DKEK : Device Key Encryption Key | ||
| + | |||
| + | ~~~bash | ||
| + | sc-hsm-tool --initialize --so-pin 3537363231383830 --pin 648219 --dkek-shares 1 | ||
| + | |||
| + | # Ou | ||
| + | sc-hsm-tool --initialize --so-pin 3537363231383830 --pin 648219 --dkek-shares 2 | ||
| + | ~~~ | ||
| + | |||
| + | Test | ||
| + | ~~~bash | ||
| + | pkcs11-tool --test --login --pin 648219 | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ~~~bash | ||
| + | #set +o history | ||
| + | export HISTCONTROL = ignorespace | ||
| + | | ||
| + | ~~~ | ||
| + | |||
| + | Ou | ||
| + | ~~~bash | ||
| + | pkcs11-tool --init-token --init-pin --so-pin=3537363231383830 --label=" | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | |||
| + | ## Changer PIN | ||
| + | |||
| + | Changer PIN / Débloquer carte | ||
| + | ~~~bash | ||
| + | #set +o history | ||
| + | export HISTCONTROL = ignorespace | ||
| + | | ||
| + | ~~~ | ||
| + | |||
| + | ## Génération d'une clef privée sur puce | ||
| + | |||
| + | ~~~bash | ||
| + | #set +o history | ||
| + | export HISTCONTROL = ignorespace | ||
| + | | ||
| + | ~~~ | ||
| + | |||
| + | ~~~bash | ||
| + | pkcs11-tool -l --keypairgen --key-type EC: | ||
| + | ~~~ | ||
| + | |||
| + | ## Importer une clef privée existante | ||
| + | |||
| + | TODO | ||
| + | |||
| + | |||
| + | ## Exporter une clef publique | ||
| + | |||
| + | ~~~bash | ||
| + | pkcs15-tool -D | ||
| + | pkcs15-tool --read-public-key 0988309300f4f26c1ff2279c09026490df89189e > publickey.pem | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## Chiffrer avec la clef publique | ||
| + | |||
| + | ~~~bash | ||
| + | # Depretated but work with ECC | ||
| + | #openssl rsautl -inkey publickey.pem -pubin -encrypt -pkcs -in plop.txt -out plop.enc | ||
| + | |||
| + | # Don't work with ECC | ||
| + | openssl pkeyutl -inkey c_rsa.pub -pubin -encrypt -in plop.txt -out plop.enc | ||
| + | ~~~ | ||
| + | |||
| + | ### Pb | ||
| + | |||
| + | ~~~ | ||
| + | $ openssl pkeyutl -inkey c_ecc.pub -pubin -encrypt -in plop.txt -out plop.enc | ||
| + | pkeyutl: Error initializing context | ||
| + | C0E2525D567F0000: | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## Déchiffrer avec la clef privée | ||
| + | |||
| + | ~~~bash | ||
| + | pkcs15-tool -D | ||
| + | |||
| + | pkcs15-crypt --decipher --key f144aec7f488e2795069256c64fa27039c1a2ed2 --input plop.enc --pkcs1 --raw > plop.txt | ||
| + | ~~~ | ||
| + | |||
| + | ## Signer (RSA) | ||
| + | |||
| + | ~~~bash | ||
| + | pkcs11-tool --sign --id f144aec7f488e2795069256c64fa27039c1a2ed2 --mechanism RSA-PKCS --input-file plop.txt --output-file plop.txt.sig | ||
| + | ~~~ | ||
| + | |||
| + | ## Vérifier la signature (RSA) | ||
| + | |||
| + | ~~~bash | ||
| + | pkcs11-tool --verif --input-file plop.txt --mechanism RSA-PKCS --signature-file plop.txt.sig | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## Utiliser la clef privée pour se connecter en SSH | ||
| + | |||
| + | Voir : | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | |||
| + | |||
| + | ## Sauvegarder vos clefs privées en les exportant chiffrées | ||
| + | |||
| + | Ne fonctionne que si vous avez | ||
| + | * tous les fichiers nécessaire crées avec la commande `sc-hsm-tool --create-dkek-share` | ||
| + | * Si vous avez la passphrase associée | ||
| + | * Que si à l' | ||
| + | |||
| + | ~~~bash | ||
| + | sc-hsm-tool --wrap-key wrap-key.bin --key-reference 1 --pin 648219 | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## Secrets partagés n-of-m | ||
| + | |||
| + | Voir aussi [[Crypto - Découper ses clefs en plusieurs morceaux grâce au secret reparti]] | ||
| + | |||
| + | Génération d'un DKEK | ||
| + | ~~~bash | ||
| + | sc-hsm-tool --create-dkek-share dkek-share-1.pbe --pwd-shares-threshold 3 --pwd-shares-total 5 | ||
| + | ~~~ | ||
| + | |||
| + | Importation d'un DKEK | ||
| + | ~~~bash | ||
| + | sc-hsm-tool --import-dkek-share dkek-share-1.pbe --pwd-shares-total 3 | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## Effacer une clef privée | ||
| + | |||
| + | ~~~bash | ||
| + | pkcs11-tool -l --delete-object --type privkey | ||
| + | |||
| + | pkcs11-tool -l --delete-object --type privkey | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## Importer un certificat dans la puce | ||
| + | |||
| + | ~~~bash | ||
| + | hsmwiz putcrt cert.pem | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## Déchiffrer les clefs privées | ||
| + | |||
| + | Voir https:// | ||
| + | |||
| + | |||
| + | ## Pb | ||
| + | |||
| + | ### Unable to select file HHHH: Incorrect parameters in APDU | ||
| + | |||
| + | ~~~ | ||
| + | $ opensc-explorer | ||
| + | OpenSC Explorer version 0.22.0 | ||
| + | Using reader with a card: HID Global OMNIKEY 3x21 Smart Card Reader [OMNIKEY 3x21 Smart Card Reader] 00 00 | ||
| + | OpenSC [3F00]> ls | ||
| + | FileID | ||
| + | Unable to select file 2F02: Incorrect parameters in APDU | ||
| + | Unable to select file CE01: Incorrect parameters in APDU | ||
| + | Unable to select file C401: Incorrect parameters in APDU | ||
| + | Unable to select file CE02: Incorrect parameters in APDU | ||
| + | Unable to select file C402: Incorrect parameters in APDU | ||
| + | Unable to select file CC00: Incorrect parameters in APDU | ||
| + | Unable to select file CC01: Incorrect parameters in APDU | ||
| + | Unable to select file CC02: Incorrect parameters in APDU | ||
| + | ~~~ | ||
| + | |||
| + | #### Solution | ||
| + | |||
| + | ~~~bash | ||
| + | hsmwiz explore | ||
| + | ~~~ | ||
| + | |||
| + | -------------------------------- | ||
| + | |||
| + | |||
| + | ~~~bash | ||
| + | sc-hsm-tool --create-dkek-share dkek-share-alice.pbe | ||
| + | sc-hsm-tool --create-dkek-share dkek-share-bob.pbe | ||
| + | |||
| + | openssl base64 -in dkek-share-alice.pbe | ||
| + | |||
| + | #set +o history | ||
| + | export HISTCONTROL = ignorespace | ||
| + | | ||
| + | |||
| + | sc-hsm-tool --import-dkek-share dkek-share-alice.pbe | ||
| + | sc-hsm-tool --import-dkek-share dkek-share-bob.pbe | ||
| + | ~~~ | ||
| + | |||
| + | ~~~ | ||
| + | Deciphering DKEK share, please wait... | ||
| + | DKEK share imported | ||
| + | DKEK shares | ||
| + | DKEK key check value : 4A44853AAAF1253E | ||
| + | ~~~ | ||
| + | |||
| + | Générer une paire de clefs | ||
| + | ~~~bash | ||
| + | | ||
| + | ~~~ | ||
| + | |||
| + | Voir les clefs | ||
| + | ~~~bash | ||
| + | pkcs15-tool -D | ||
| + | ~~~ | ||
| + | |||
| + | Exporter la clef privée générée (chiffrée) | ||
| + | ~~~bash | ||
| + | #set +o history | ||
| + | export HISTCONTROL = ignorespace | ||
| + | | ||
| + | ~~~ | ||
| + | |||
| + | ### Importation carte backup | ||
| + | |||
| + | ~~~bash | ||
| + | sc-hsm-tool --import-dkek-share dkek-share-alice.pbe | ||
| + | sc-hsm-tool --import-dkek-share dkek-share-bob.pbe | ||
| + | #set +o history | ||
| + | export HISTCONTROL = ignorespace | ||
| + | | ||
| + | ~~~ | ||
| + | |||
| + | # PKI | ||
| + | |||
| + | ~~~bash | ||
| + | pkcs11-tool -l --keypairgen --key-type EC: | ||
| + | |||
| + | |||
| + | ~~~ | ||
| + | |||
| + | # Avoir des informations | ||
| + | |||
| + | ~~~bash | ||
| + | sc-hsm-tool | ||
| + | |||
| + | pkcs15-tool -D | ||
| + | pkcs11-tool -O --id 0 | ||
| + | pkcs11-tool -O --id 0988309300f4f26c1ff2279c09026490df89189e | ||
| + | ~~~ | ||
| + | |||
| + | ---------- | ||
| + | |||
| + | ## Autres | ||
| + | |||
| + | ### CA | ||
| + | |||
| + | https:// | ||
| + | |||
| + | |||
| + | ~~~bash | ||
| + | pkcs15-init --delete-objects privkey, | ||
| + | ~~~ | ||
| + | |||
| + | https:// | ||
| + | |||
| + | ~~~ | ||
| + | pkcs11-tool -l --login-type so --keypairgen --key-type EC: | ||
| + | pkcs15-init --delete-objects privkey, | ||
| + | ~~~ | ||
| + | |||
| + | ~~~ | ||
| + | $ pkcs11-tool -l --login-type so --keypairgen --key-type EC: | ||
| + | Using slot 0 with a present token (0x0) | ||
| + | Logging in to " | ||
| + | Please enter SO PIN: | ||
| + | Key pair generated: | ||
| + | Private Key Object; EC | ||
| + | label: | ||
| + | ID: | ||
| + | Usage: | ||
| + | Access: | ||
| + | Public Key Object; EC EC_POINT 2044 bits | ||
| + | EC_POINT: | ||
| + | EC_PARAMS: | ||
| + | label: | ||
| + | ID: | ||
| + | Usage: | ||
| + | Access: | ||
| + | ~~~ | ||
| + | |||
| + | ~~~bash | ||
| + | pkcs11-tool --list-objects --type cert --slot 1 | ||
| + | pkcs11-tool --list-objects --type cert --slot 0 | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | # Autres | ||
| + | |||
| + | ~~~bash | ||
| + | #set +o history | ||
| + | export HISTCONTROL = ignorespace | ||
| + | | ||
| + | ~~~ | ||
| + | |||
| + | Autres | ||
| + | ~~~ | ||
| + | opensc-tool --atr | ||
| + | opensc-explorer --mf aid: | ||
| + | |||
| + | pkcs15-init --delete-objects privkey, | ||
| + | |||
| + | pkcs11-tool -l --write-object keyrsaplop --type privkey --key-type RSA:3072 --pin 648219 --so-pin 3537363231383830 | ||
| + | |||
| + | --usage-sign | ||
| + | encrypt, verify | ||
| + | sign | ||
| + | |||
| + | |||
| + | openssl genrsa -out rsa2048 2048 | ||
| + | ~~~ | ||
| + | |||
| + | Autres | ||
| + | ~~~ | ||
| + | | ||
| + | | ||
| + | | ||
| + | |||
| + | Elliptic Curve private + public key pair for use with ES512 signatures: | ||
| + | openssl ecparam -genkey -name secp521r1 -noout -out ec512-key-pair.pem | ||
| + | |||
| + | openssl rsa -in key.pem -outform PEM -pubout -out public.pem | ||
| + | openssl ec -in ec512-key-pair.pem -outform PEM -pubout -out public.pem | ||
| + | |||
| + | $ openssl req -x509 -sha512 -days 365 -key ec512-key-pair.pem -in cert.csr -out certificate.pem | ||
| + | Warning: No -copy_extensions given; ignoring any extensions in the request | ||
| + | |||
| + | cat ec512-key-pair.pem certificate.pem > certificated-key.pem | ||
| + | |||
| + | openssl pkcs12 -export -out keyStore.p12 -inkey ec512-key-pair.pem -in certificate.pem | ||
| + | |||
| + | |||
| + | https:// | ||
| + | |||
| + | hsmwiz putcrt keyStore.p12 | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | |||
