PKCS11-TOOL(1) | OpenSC Tools | PKCS11-TOOL(1) |
NAME¶
pkcs11-tool - utility for managing and using PKCS #11 security tokens
SYNOPSIS¶
pkcs11-tool [OPTIONS]
DESCRIPTION¶
The pkcs11-tool utility is used to manage the data objects on smart cards and similar PKCS #11 security tokens. Users can list and read PINs, keys and certificates stored on the token. User PIN authentication is performed for those operations that require it.
OPTIONS¶
--attr-from filename
--change-pin, -c
--unlock-pin
--hash, -h
--hash-algorithm mechanism
Note that the input to RSA-PKCS-PSS has to be of the size equal to the specified hash algorithm. E.g., for SHA256 the signature input must be exactly 32 bytes long (for mechanisms SHA256-RSA-PKCS-PSS there is no such restriction). For RSA-OAEP, the plaintext input size mLen must be at most keyLen - 2 - 2*hashLen. For example, for RSA 3072-bit key and SHA384, the longest plaintext to encrypt with RSA-OAEP is (with all sizes in bytes): 384 - 2 - 2*48 = 286, aka 286 bytes.
--id id, -d id
--init-pin
--init-token
--input-file filename, -i filename
--keypairgen, -k
--keygen
--key-type specification
--usage-sign
--usage-decrypt
For RSA keys, sets DECRYPT in privkey and ENCRYPT in pubkey. For secret keys, sets both DECRYPT and ENCRYPT.
--usage-derive
--usage-wrap
--label name, -a name
--list-mechanisms, -M
--list-objects, -O
The options --keytype, --label , --id or --application-id can be used to filter the listed objects.
--list-slots, -L
--list-token-slots, -T
--list-interfaces
--session-rw,
--login, -l
--login-type
--mechanism mechanism, -m mechanism
--mgf function
--module mod
--moz-cert filename, -z filename
--output-file filename, -o filename
--pin pin, -p pin
This option will also set the --login option.
--puk puk
--new-pin pin
--sensitive
--extractable
--undestroyable
--set-id id, -e id
--show-info, -I
--sign, -s
--decrypt,
--encrypt,
--unwrap,
--wrap,
--derive,
--derive-pass-der,
--salt-len bytes
--slot id
--slot-description description
--slot-index index
--object-index index
--use-locking
--test-threads options
--token-label label
--so-pin pin
--test, -t
--test-hotplug
--private
--always-auth
--allowed-mechanisms mechanisms
--test-ec
--test-fork
--type type, -y type
--verbose, -v
NB! This does not affect OpenSC debugging level! To set OpenSC PKCS#11 module into debug mode, set the OPENSC_DEBUG environment variable to a non-zero number.
--verify,
--read-object, -r
--delete-object, -b
--application-label label
--application-id id
--issuer data
--subject data
--signature-file filename
--signature-format format
--write-object filename, -w filename
--generate-random num
--allow-sw
--iv data
EXAMPLES¶
Perform a basic functionality test of the card:
pkcs11-tool --test --login
List all certificates on the smart card:
pkcs11-tool --list-objects --type cert
Read the certificate with ID CERT_ID in DER format from smart card and convert it to PEM via OpenSSL:
pkcs11-tool --read-object --id $CERT_ID --type cert \ --output-file cert.der openssl x509 -inform DER -in cert.der -outform PEM > cert.pem
Write a certificate to token:
pkcs11-tool --login --write-object certificate.der --type cert
Generate new RSA Key pair:
pkcs11-tool --login --keypairgen --key-type RSA:2048
Generate new extractable RSA Key pair:
pkcs11-tool --login --keypairgen --key-type RSA:2048 --extractable
Generate an elliptic curve key pair with OpenSSL and import it to the card as $ID:
openssl genpkey -out EC_private.der -outform DER \ -algorithm EC -pkeyopt ec_paramgen_curve:P-521 pkcs11-tool --write-object EC_private.der --id "$ID" \ --type privkey --label "EC private key" -p "$PIN" openssl pkey -in EC_private.der -out EC_public.der \ -pubout -inform DER -outform DER pkcs11-tool --write-object EC_public.der --id "$ID" \ --type pubkey --label "EC public key" -p $PIN
List private keys:
pkcs11-tool --login --list-objects --type privkey
Sign some data stored in file data using the private key with ID ID and using the RSA-PKCS mechanism:
pkcs11-tool --sign --id $ID --mechanism RSA-PKCS \ --input-file data --output-file data.sig
The same is also possible by piping the data from stdin rather than specifying a input file:
dd if=data bs=128 count=1 \ | pkcs11-tool --sign --id $ID --mechanism RSA-PKCS \ > data.sig
Verify the signed data:
pkcs11-tool --id ID --verify -m RSA-PKCS \ --input-file data --signature-file data.sig
To encrypt file using the AES key with ID 85 and using mechanism AES-CBC with padding:
pkcs11-tool --login --encrypt --id 85 -m AES-CBC-PAD \ --iv "00000000000000000000000000000000" \ -i file.txt -o encrypted_file.data
Decipher the encrypted file:
pkcs11-tool --login --decrypt --id 85 -m AES-CBC-PAD \ --iv "00000000000000000000000000000000" \ --i encrypted_file.data -o decrypted.txt
Use the key with ID 75 using mechanism AES-CBC-PAD, with initialization vector "00000000000000000000000000000000" to wrap the key with ID 76 into output file exported_aes.key
pkcs11-tool --login --wrap --id 75 --mechanism AES-CBC-PAD \ --iv "00000000000000000000000000000000" \ --application-id 76 \ --output-file exported_aes.key
Use the key with ID 22 and mechanism RSA-PKCS to unwrap key from file aes_wrapped.key. After a successful unwrap operation, a new AES key is created on token. ID of this key is set to 90 and label of this key is set to unwrapped-key Note: for the MyEID card, the AES key size must be present in key specification i.e. AES:16
pkcs11-tool --login --unwrap --mechanism RSA-PKCS --id 22 \ -i aes_wrapped.key --key-type AES: \ --application-id 90 --applicatin-label unwrapped-key
Use the SO-PIN to initialize or re-set the PIN:
pkcs11-tool --login --login-type so --init-pin
AUTHORS¶
pkcs11-tool was written by Olaf Kirch <okir@suse.de>.
08/29/2024 | opensc |