alonbl / gnupg-pkcs11-scd

PKCS#11 GnuPG SCD
http://gnupg-pkcs11.sourceforge.net/
Other
68 stars 17 forks source link

Problem in loading keys to the gpg #38

Closed alireza11048 closed 2 years ago

alireza11048 commented 2 years ago

Hi, I have a pkcs11 capable token and private-key, certificate, and public key in it. this is the output of SCD LEARN:

SCD LEARN
EOF
OK Pleased to meet you
gnupg-pkcs11-scd[17127.1195300672]: Listening to socket '/tmp/gnupg-pkcs11-scd.QGvO7h/agent.S'
gnupg-pkcs11-scd[17127.1195300672]: accepting connection
gnupg-pkcs11-scd[17127]: chan_0 -> OK PKCS#11 smart-card server for GnuPG ready
gnupg-pkcs11-scd[17127.1195300672]: processing connection
gnupg-pkcs11-scd[17127]: chan_0 <- GETINFO socket_name
gnupg-pkcs11-scd[17127]: chan_0 -> D /tmp/gnupg-pkcs11-scd.QGvO7h/agent.S
gnupg-pkcs11-scd[17127]: chan_0 -> OK
gnupg-pkcs11-scd[17127]: chan_0 <- LEARN
gnupg-pkcs11-scd[17127]: chan_0 -> S SERIALNO D276000124011150313195AC51031111
gnupg-pkcs11-scd[17127]: chan_0 -> S APPTYPE PKCS11
S SERIALNO D276000124011150313195AC51031111
S APPTYPE PKCS11
gnupg-pkcs11-scd[17127]: chan_0 -> S KEY-FRIEDNLY EA518B3E6D66EDCA8B9DA5ADF59798B34C5D1A51 /CN=CN on infratesttune
gnupg-pkcs11-scd[17127]: chan_0 -> S CERTINFO 101 Amnafzar\x20Corp\x2E/C/52FBF4423C4292A6/infratesttune/85
gnupg-pkcs11-scd[17127]: chan_0 -> S KEYPAIRINFO EA518B3E6D66EDCA8B9DA5ADF59798B34C5D1A51 Amnafzar\x20Corp\x2E/C/52FBF4423C4292A6/infratesttune/85
S KEY-FRIEDNLY EA518B3E6D66EDCA8B9DA5ADF59798B34C5D1A51 /CN=CN on infratesttune
S CERTINFO 101 Amnafzar\x20Corp\x2E/C/52FBF4423C4292A6/infratesttune/85
S KEYPAIRINFO EA518B3E6D66EDCA8B9DA5ADF59798B34C5D1A51 Amnafzar\x20Corp\x2E/C/52FBF4423C4292A6/infratesttune/85
gnupg-pkcs11-scd[17127]: chan_0 -> S KEY-FRIEDNLY 04B70DFA6C7A559FC5E1B932ADAF9F0804B25CE2 /CN=CN on infratesttune
gnupg-pkcs11-scd[17127]: chan_0 -> S CERTINFO 101 Amnafzar\x20Corp\x2E/C/52FBF4423C4292A6/infratesttune/7777
S KEY-FRIEDNLY 04B70DFA6C7A559FC5E1B932ADAF9F0804B25CE2 /CN=CN on infratesttunegnupg-pkcs11-scd[17127]: 
chan_0 -> S KEYPAIRINFO 04B70DFA6C7A559FC5E1B932ADAF9F0804B25CE2 Amnafzar\x20Corp\x2E/C/52FBF4423C4292A6/infratesttune/7777
S CERTINFO 101 Amnafzar\x20Corp\x2E/C/52FBF4423C4292A6/infratesttune/7777
gnupg-pkcs11-scd[17127]: chan_0 -> OK
S KEYPAIRINFO 04B70DFA6C7A559FC5E1B932ADAF9F0804B25CE2 Amnafzar\x20Corp\x2E/C/52FBF4423C4292A6/infratesttune/7777
OK
gnupg-pkcs11-scd[17127]: chan_0 <- RESTART
gnupg-pkcs11-scd[17127]: chan_0 -> OK
gnupg-pkcs11-scd[17127]: chan_0 <- [eof]
gnupg-pkcs11-scd[17127.1195300672]: post-processing connection
gnupg-pkcs11-scd[17127.1195300672]: accepting connection
gnupg-pkcs11-scd[17127.1195300672]: cleanup connection
gnupg-pkcs11-scd[17127.1195300672]: Terminating
gnupg-pkcs11-scd[17127.1169278720]: Thread command terminate
gnupg-pkcs11-scd[17127.1169278720]: Cleaning up threads

But when I try to load the key with "04B70DFA6C7A559FC5E1B932ADAF9F0804B25CE2" key-friendly, i got the below result:

gpg --expert --full-generate-key
gpg (GnuPG) 2.2.19; Copyright (C) 2019 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? 13
Enter the keygrip: 04B70DFA6C7A559FC5E1B932ADAF9F0804B25CE2
No Key with this keygrip

I want to point that the same process is successful with an older key that exists in the token with the "EA518B3E6D66EDCA8B9DA5ADF59798B34C5D1A51" key-friendly.

any advice to solve the problem is appreciated.

the log file is as below:

gnupg-pkcs11-scd[2208.2169505600]: Listening to socket '/tmp/gnupg-pkcs11-scd.BLemoa/agent.S'
gnupg-pkcs11-scd[2208.2169505600]: accepting connection
gnupg-pkcs11-scd[2208]: chan_0 -> OK PKCS#11 smart-card server for GnuPG ready
gnupg-pkcs11-scd[2208.2169505600]: processing connection
gnupg-pkcs11-scd[2208]: chan_0 <- GETINFO socket_name
gnupg-pkcs11-scd[2208]: chan_0 -> D /tmp/gnupg-pkcs11-scd.BLemoa/agent.S
gnupg-pkcs11-scd[2208]: chan_0 -> OK
gnupg-pkcs11-scd[2208]: chan_0 <- LEARN
gnupg-pkcs11-scd[2208]: chan_0 -> S SERIALNO D276000124011150313195AC51031111
gnupg-pkcs11-scd[2208]: chan_0 -> S APPTYPE PKCS11
gnupg-pkcs11-scd[2208]: chan_0 -> S KEY-FRIEDNLY EA518B3E6D66EDCA8B9DA5ADF59798B34C5D1A51 /CN=CN on infratesttune
gnupg-pkcs11-scd[2208]: chan_0 -> S CERTINFO 101 Amnafzar\x20Corp\x2E/C/52FBF4423C4292A6/infratesttune/85
gnupg-pkcs11-scd[2208]: chan_0 -> S KEYPAIRINFO EA518B3E6D66EDCA8B9DA5ADF59798B34C5D1A51 Amnafzar\x20Corp\x2E/C/52FBF4423C4292A6/infratesttune/85
gnupg-pkcs11-scd[2208]: chan_0 -> S KEY-FRIEDNLY 04B70DFA6C7A559FC5E1B932ADAF9F0804B25CE2 /CN=CN on infratesttune
gnupg-pkcs11-scd[2208]: chan_0 -> S KEY-FPR 1 04B70DFA6C7A559FC5E1B932ADAF9F0804B25CE2
gnupg-pkcs11-scd[2208]: chan_0 -> S CERTINFO 101 Amnafzar\x20Corp\x2E/C/52FBF4423C4292A6/infratesttune/7777
gnupg-pkcs11-scd[2208]: chan_0 -> S KEYPAIRINFO 04B70DFA6C7A559FC5E1B932ADAF9F0804B25CE2 Amnafzar\x20Corp\x2E/C/52FBF4423C4292A6/infratesttune/7777
gnupg-pkcs11-scd[2208]: chan_0 -> OK
gnupg-pkcs11-scd[2208]: chan_0 <- RESTART
gnupg-pkcs11-scd[2208]: chan_0 -> OK
gnupg-pkcs11-scd[2208]: chan_0 <- [eof]
gnupg-pkcs11-scd[2208.2169505600]: post-processing connection
gnupg-pkcs11-scd[2208.2169505600]: accepting connection
gnupg-pkcs11-scd[2208.2169505600]: cleanup connection
gnupg-pkcs11-scd[2208.2169505600]: Terminating
gnupg-pkcs11-scd[2208.2143487744]: Thread command terminate
gnupg-pkcs11-scd[2208.2143487744]: Cleaning up threads
alonbl commented 2 years ago

Please send gnupg-pkcs11-scd debug log.

alireza11048 commented 2 years ago

Please send gnupg-pkcs11-scd debug log.

Could you please tell me how can i get the debug log?

alonbl commented 2 years ago

Configuration

           # Log file.
           #log-file log1

           # Default is not verbose.
           #verbose

           # Default is no debugging.
           #debug-all

Please also attach the entire configuration.

chris-dmg commented 2 years ago

Dear all, I guess I face the same problem. I created a RSA-Keypair on my hsm device (nitrokey hsm2) and imported the certificate:

$ pkcs11-tool --keypairgen --key-type rsa:3072 --label gpg.test --login
# create self signed certificate
openssl req -x509 -engine pkcs11 -keyform engine -new -key 08e88816bac7acbdab75869d91829d87e3a521f5 -sha256 -out cert.pem -subj "/CN=gpg.test"
# convert certificate
openssl x509 -outform der -in cert.pem -out cert.der
# write certificate to hsm
pkcs11-tool -l --write-object cert.der --type cert --id 08e88816bac7acbdab75869d91829d87e3a521f5 --label "gpg.test"
$ pkcs11-tool --list-objects
Using slot 0 with a present token (0x0)
Certificate Object; type = X.509 cert
  label:      gpg.test
  subject:    DN: CN=gpg.test
  ID:         08e88816bac7acbdab75869d91829d87e3a521f5
Public Key Object; RSA 3072 bits
  label:      gpg.test
  ID:         08e88816bac7acbdab75869d91829d87e3a521f5
  Usage:      encrypt, verify
  Access:     local

gpg --status-card provides the following output:

$ gpg --card-status
gpg: WARNUNG: Der Server 'scdaemon' is älter als wir selbst (Version 0.9.2 < 2.2.27)
gpg: Hinweis: Wichtige Sicherheits-Fixes können in veralteten Servern fehlen.
gpg: Hinweis: Der Befehl "gpgconf --kill all" startet diese Server neu.
Reader ...........: [none]
Application ID ...: D2760001240111503131A4240B5E1111
Application type .: OpenPGP
Version ..........: 11.50
Manufacturer .....: ?
Serial number ....: A4240B5E
Name of cardholder: [nicht gesetzt]
Language prefs ...: [nicht gesetzt]
Salutation .......: 
URL of public key : [nicht gesetzt]
Login data .......: [nicht gesetzt]
Signature PIN ....: zwingend
Key attributes ...: rsa48 rsa48 rsa48
Max. PIN lengths .: 0 0 0
PIN retry counter : 0 0 0
Signature counter : 0
Signature key ....: [none]
Encryption key....: [none]
Authentication key: [none]
General key info..: [none]

Output of SCD LEARN:

$ gpg-agent --server gpg-connect-agent << EOF
SCD LEARN
EOF
gpg-agent[11147]: enabled debug flags: mpi crypto memory cache memstat hashing ipc
OK Pleased to meet you
S SERIALNO D2760001240111503131A4240B5E1111
S APPTYPE PKCS11
S KEY-FRIEDNLY 6C3EF6D3AAFDA54DBD2FB5B01952950B034ACF43 /CN=gpg.test on CELOS OS BIOS Keys (UserPIN)
S CERTINFO 101 www\x2ECardContact\x2Ede/PKCS\x2315\x20emulated/DENK0106097/CELOS\x20OS\x20BIOS\x20Keys\x20\x28UserPIN\x29/08E88816BAC7ACBDAB75869D91829D87E3A521F5
S KEYPAIRINFO 6C3EF6D3AAFDA54DBD2FB5B01952950B034ACF43 www\x2ECardContact\x2Ede/PKCS\x2315\x20emulated/DENK0106097/CELOS\x20OS\x20BIOS\x20Keys\x20\x28UserPIN\x29/08E88816BAC7ACBDAB75869D91829D87E3A521F5
OK

Output when I try to import the key to GPG (key with given keygrip not found)

$ gpg --expert --full-generate-key --homedir .
gpg: WARNUNG: Unsichere Zugriffsrechte des Home-Verzeichnis `/home/chris/workspace/tmp'
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.

Bitte wählen Sie, welche Art von Schlüssel Sie möchten:
   (1) RSA und RSA (voreingestellt)
   (2) DSA und Elgamal
   (3) DSA (nur signieren/beglaubigen)
   (4) RSA (nur signieren/beglaubigen)
   (7) DSA (Nutzung selber einstellbar)
   (8) RSA (Nutzung selber einstellbar)
  (9) ECC und ECC
  (10) ECC (nur signieren)
  (11) ECC (Nutzung selber einstellbar)
   (13) Vorhandener Schlüssel
   (14) Vorhandener Schlüssel auf der Karte
Ihre Auswahl? 13
Geben Sie den "Keygrip" ein: 6C3EF6D3AAFDA54DBD2FB5B01952950B034ACF43
Kein Schlüssel mit diesem "Keygrip"

my config files:

cat ~/.gnupg/gpg-agent.conf 
scdaemon-program /usr/bin/gnupg-pkcs11-scd
pinentry-program /usr/bin/pinentry
log-file /home/chris/workspace/tmp/gpg-agent.log
verbose
debug-all

cat ~/.gnupg/gnupg-pkcs11-scd.conf 
log-file /home/chris/workspace/tmp/pkcs11-scd.log
verbose
debug-all
providers smartcardhsm
provider-smartcardhsm-library /usr/lib/x86_64-linux-gnu/opensc-pkcs11.so
#provider-smartcardhsm-cert-private

pkcs11-scd.log gpg-agent.log

alonbl commented 2 years ago

Hello @chris-dmg,

You are using non-default complex configuration for testing... please revert to a minimum configuration and test, it is much easier to reach a working setup and then break it than debug a complex non working one.

In your case I can see that:

$ gpg --card-status

is run within different home directory than the second command.

also when pasting please set LC_ALL=C so that the output will be in English so people can understand :)

Thanks,

chris-dmg commented 2 years ago

Hello @alonbl ,

thanks for your hint and sorry for pasting german stuff. I now added a "export LC_ALL=C" to my .bashrc and now it worked without problems to my surprise. Is it possible that there is something which depends on the locale?

Thanks, Chris

alonbl commented 2 years ago

The card status and the generate key should use the same home.

chris-dmg commented 2 years ago

Thanks - it works now as expected!