Yubico PIV Tool
repository·master·Indexed 43 days ago
https://github.com/yubico/yubico-piv-toolA shared library and command-line utility for interacting with the Personal Identity Verification (PIV) application on YubiKeys. It provides functionality for key generation (RSA, ECC, ED25519, X25519), certificate importing, CSR management, and administrative security settings including PIN/PUK management and management key configuration.
What's inside Yubico PIV Tool
- For PIV applications to be usable in Windows, the CHUID (Card Holder Unique Identifier) must be set and must be unique. Because card contents are aggressively cached, you must change the CHUID whenever the card contents change to ensure the system recognizes the update.
How Attestation Certificates work in YKCS11
masterAttestation certificates are accessible via the YKCS11 module. They are regular X.509 certificates that share the same
CKA_IDand public key as the key they are attesting.Key characteristics:
- They are not stored in the YubiKey (
CKA_TOKENisFALSE). - They are generated dynamically when accessed.
- They are not stored in the YubiKey (
Configure PIN and Touch policies
masterFor YubiKey 4 or newer, you can set security policies for the slot containing the imported key using
--pin-policyand--touch-policy.PIN Policies:
never: No PIN required.once: PIN required once.always: PIN required every time.matchonce: Applicable with biometric verification.matchalways: Applicable with biometric verification.
Note: Default policies are
alwayson slot9candonceon slots9a,9d, and9e.Touch Policies:
never: No touch required.always: Touch required every time.cached: Touch required periodically (cached).
Key Aliases (Labels) per Slot and Object Type
masterApplications (like Java) often refer to keys by their label. In YKCS11, object labels are fixed and unmodifiable. Use the following mapping to identify the correct slot and object type for your needs.
Standard PIV Slots
- Slot 9a: PIV Authentication (Private Key, Public Key, X.509 Certificate, Attestation Certificate, and Data Object).
- Slot 9c: Digital Signature (Private Key, Public Key, X.509 Certificate, Attestation Certificate, and Data Object).
- Slot 9d: Key Management (Private Key, Public Key, X.509 Certificate, Attestation Certificate, and Data Object).
- Slot 9e: Card Authentication (Private Key, Public Key, X.509 Certificate, Attestation Certificate, and Data Object).
Retired Key Slots
Slots 82 through 95 correspond to Retired Keys 1 through 20. Each slot contains:
- A Private Key
- A Public Key
- An X.509 Certificate
- An Attestation Certificate
- A Data Object (the X.509 Certificate)
Attestation Slot
- Slot f9: Dedicated to PIV Attestation (Private Key, Public Key, X.509 Certificate, and Attestation Certificate).
How to perform the full key and certificate lifecycle
masterThe PIV interface requires a specific sequence of actions to populate a slot with a private key and a certificate.
Standard Sequence:
- Generate: Create the key pair (
-a generate). - Verify: Authenticate the session (
-a verify-pin). - Sign/Request: Create a self-signed cert (
-a selfsign) or a CSR (-a request-certificate). - Import: Load the resulting certificate into the slot (
-a import-certificate).
You can chain these actions into a single command to simplify the process.
# Combined command for generating a key, self-signing it, and importing it in one go yubico-piv-tool -a generate -a verify-pin -a selfsign -a import-certificate -s 9a -k -A ECCP256 -S '/CN=piv_auth/OU=test/O=example.com/'- Generate: Create the key pair (
YKCS11 User Types and Authentication
masterYKCS11 defines two user types mapped to specific operations:
- Regular User: Performs PIN-associated operations (using private key material).
- Security Officer (SO): Performs device management (using the management key).
Default Credentials
- Default User PIN:
123456 - Default Management Key:
010203040506070801020304050607080102030405060708
Authentication Methods
- PIN: Required for regular user operations. Subsequent operations might require a new login depending on the key's PIN policy. The module supports the
CONTEXT_SPECIFICuser for this. - PIN Policy "never": You can skip PIN verification for these keys by using
VERIFY_NONEas the PIN. - Fingerprint (YubiKey Bio): To trigger bio verification, attempt to login with an empty PIN or use
VERIFY_BIOas the PIN. The YubiKey will blink while waiting for a scan.
Enable Secure Channel Communication
masterYubiKeys with firmware version 5.7.0 or higher support encrypted communication via the SCP11b protocol. This protects sensitive communication (e.g., over NFC) from eavesdropping.
- CLI usage: Add the
--encflag to your command. - Library usage (
libykpiv): Open a connection with the encryption boolean set totrue.
Note: In the current SCP11b implementation, the On-Card certificate (
CERT.SD.ECKA) used to establish the session is not validated.- CLI usage: Add the
Enable and generate code coverage
masterCode coverage is supported via
lcovandCMake-codecov. This functionality currently requires usingmakerather thancmake --build.- Enable coverage during the configuration step with
-DENABLE_COVERAGE=1. - Build the project using
make. - Run tests using
make test. - Generate the HTML report using
make lcov.
$ cmake -DENABLE_COVERAGE=1 .. $ make $ make test $ make lcov- Enable coverage during the configuration step with
Use YKCS11 with OpenSC's pkcs11-tool
masterThe YKCS11 module is compatible with
pkcs11-tool. Note that older versions of OpenSC (common on some Linux distributions) may cause errors; it is recommended to use a newer version of OpenSC.To use the module, you must provide the path to
libykcs11.sousing the--moduleflag in your commands.$ pkcs11-tool --module /path/to/libykcs11.so --show-infoBuild yubico-piv-tool on POSIX platforms
masterTo build the tool on Debian-based systems, ensure you have the following dependencies installed:
cmake libtool libssl-dev pkg-config check libpcsclite-dev gengetopt help2man zlib-develNote: On Redhat-based systems, use
openssl-develinstead oflibssl-devandpcsc-lite-develinstead oflibpcsclite-dev.Build Steps
- Navigate to the
yubico-piv-tooldirectory. - Create a build directory and compile.
- Install the binaries.
macOS Specifics
You may need to provide the path to the Homebrew OpenSSL installation via
PKG_CONFIG_PATHduring thecmakestep.Backend Selection
The backend is decided at compile time. You can manually select a backend using
--with-backend=foowherefoois:pcsc: Used for GNU-like systems.macscard: Used for Mac OS X.winscard: Used for Windows.
In most cases,
cmakewill automatically detect the correct backend.$ cd yubico-piv-tool $ mkdir build; cd build $ cmake .. $ make $ sudo make install $ sudo ldconfig- Navigate to the
Delete a certificate from a YubiKey slot
masterUse the
delete-certificateaction to remove a certificate from a specific PIV slot.Important Notes:
- Private Keys: Deleting a certificate does not delete the corresponding private key unless that key is subsequently overwritten.
- Authentication: This action requires authentication using a management key. If you do not provide a key via the
--keyflag, the tool will prompt you to enter it manually. If no key is provided and no prompt occurs, it defaults to010203040506070801020304050607080102030405060708. - Security Warning: It is strongly recommended to change the YubiKey's PIN, PUK, and management key before use.
yubico-piv-tool -a delete-certificate -s <slot> -kImport a key and certificate using yubico-piv-tool
masterUse the
import-keyandimport-certificateactions to import private keys, certificates, or both into a specific YubiKey PIV slot.Key Constraints:
- The largest accepted keys are 2025 bytes for YubiKey NEO and 3049 bytes for YubiKey 5.
- Larger certificates may require compression (e.g., using
GZIPformat) to fit. - This action can also be used to import decryption keys (typically found in slot
9d) into retired slots (82-95).
Authentication: Importing requires authentication via a management key. If the
-kflag is provided without a value, the tool will prompt for it. If omitted, the tool attempts to use the default management key:010203040506070801020304050607080102030405060708.# Import both a key and a certificate from a PKCS12 file into slot 9c $ yubico-piv-tool -a import-key -a import-certificate -s 9c -k -i key.pfx -K PKCS12