Documentation ¶
Overview ¶
Package piv implements management functionality for the YubiKey PIV applet.
Index ¶
- Constants
- Variables
- type Algorithm
- type Attestation
- type AuthError
- type Card
- func (c *Card) Attest(slot Slot) (cert *x509.Certificate, err error)
- func (c *Card) AttestationCertificate() (*x509.Certificate, error)
- func (c *Card) Certificate(slot Slot) (*x509.Certificate, error)
- func (c *Card) Close() error
- func (c *Card) GenerateKey(key ManagementKey, slot Slot, opts Key) (crypto.PublicKey, error)
- func (c *Card) Metadata(slot Slot) (*Metadata, error)
- func (c *Card) PinProtectedData(pin string) (*PinProtectedData, error)
- func (c *Card) PrivateKey(slot Slot, public crypto.PublicKey, auth KeyAuth) (crypto.PrivateKey, error)
- func (c *Card) Reset() error
- func (c *Card) Retries() (int, error)
- func (c *Card) Serial() (uint32, error)
- func (c *Card) SetCertificate(key ManagementKey, slot Slot, cert *x509.Certificate) error
- func (c *Card) SetManagementKey(oldKey, newKey ManagementKey) error
- func (c *Card) SetManagementKeyPinProtected(oldKey ManagementKey, pin string) error
- func (c *Card) SetPIN(oldPIN, newPIN string) error
- func (c *Card) SetPUK(oldPUK, newPUK string) error
- func (c *Card) SetPinProtectedData(key ManagementKey, ppd *PinProtectedData) error
- func (c *Card) SetPrivateKeyInsecure(key ManagementKey, slot Slot, private crypto.PrivateKey, policy Key) error
- func (c *Card) SupportsAttestation() bool
- func (c *Card) Unblock(puk, newPIN string) error
- func (c *Card) VerifyPIN(pin string) error
- func (c *Card) Version() iso.Version
- type ECPPPrivateKey
- type FormFactor
- type Key
- type KeyAuth
- type ManagementKey
- type Metadata
- type Object
- type Origin
- type PINPolicy
- type PinProtectedData
- type Slot
- type TouchPolicy
- type UnsupportedCurveError
- type Verifier
- Bugs
Constants ¶
const ( AlgTypeRSA algorithmType = iota + 1 AlgTypeECCP AlgTypeEd25519 )
const ( FormFactorUSBAKeychain = 0x1 FormFactorUSBANano = 0x2 FormFactorUSBCKeychain = 0x3 FormFactorUSBCNano = 0x4 FormFactorUSBCLightningKeychain = 0x5 FormFactorUSBAKeychainFIPS = 0x80 + FormFactorUSBAKeychain FormFactorUSBANanoFIPS = 0x80 + FormFactorUSBANano FormFactorUSBCKeychainFIPS = 0x80 + FormFactorUSBCKeychain FormFactorUSBCNanoFIPS = 0x80 + FormFactorUSBCNano FormFactorUSBCLightningKeychainFIPS = 0x80 + FormFactorUSBCLightningKeychain )
Formfactors recognized by this package. See the reference for more information: https://developers.yubico.com/yubikey-manager/Config_Reference.html#_form_factor
const ( // DefaultPIN for the PIV applet. The PIN is used to change the Management Key, // and slots can optionally require it to perform signing operations. DefaultPIN = "123456" // DefaultPUK for the PIV applet. The PUK is only used to reset the PIN when // the card's PIN retries have been exhausted. DefaultPUK = "12345678" )
Variables ¶
var ( SupportsAttestation = yubikey.HasVersionStr("4.3.0") SupportsMetadata = yubikey.HasVersionStr("5.3.0") SupportsAlgorithmEC384 = yubikey.HasVersionStr("4.3.0") )
var ( SlotAuthentication = Slot{keyAuthentication, doCertAuthentication} SlotSignature = Slot{keySignature, doCertSignature} SlotCardAuthentication = Slot{keyCardAuthentication, doCertCardAuthentication} SlotKeyManagement = Slot{keyKeyManagement, doCertKeyManagement} // YubiKey specific SlotAttestation = Slot{keyAttestation, doCertAttestation} )
Slot combinations pre-defined by this package.
Object IDs are specified in NIST 800-73-4 section 4.3: https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-73-4.pdf#page=30
Key IDs are specified in NIST 800-73-4 section 5.1: https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-73-4.pdf#page=32
var DefaultManagementKey = ManagementKey{
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
}
DefaultManagementKey for the PIV applet. The Management Key is a Triple-DES key required for slot actions such as generating keys, setting certificates, and signing.
var ErrNotFound = errors.New("data object or application not found")
ErrNotFound is returned when the requested object on the smart card is not found.
Functions ¶
This section is empty.
Types ¶
type Algorithm ¶
type Algorithm byte
Algorithm represents a specific algorithm and bit size supported by the PIV specification.
const ( Alg3DES Algorithm = 0x03 AlgRSA1024 Algorithm = 0x06 AlgRSA2048 Algorithm = 0x07 AlgECCP256 Algorithm = 0x11 AlgECCP384 Algorithm = 0x14 // Non-standard; as implemented by SoloKeys. Chosen for low probability of eventual // clashes, if and when PIV standard adds Ed25519 support AlgEd25519 Algorithm = 0x22 )
Algorithms supported by this package. Note that not all cards will support every algorithm.
AlgorithmEd25519 is currently only implemented by SoloKeys.
For algorithm discovery, see: https://github.com/ericchiang/piv-go/issues/1
type Attestation ¶
type Attestation struct { // Version of the YubiKey's firmware. Version iso.Version // Serial is the YubiKey's serial number. Serial uint32 // FormFactor indicates the physical type of the YubiKey. // // FormFactor may be empty FormFactor(0) for some YubiKeys. FormFactor FormFactor // PINPolicy set on the slot. PINPolicy PINPolicy // TouchPolicy set on the slot. TouchPolicy TouchPolicy // Slot is the inferred slot the attested key resides in based on the // common name in the attestation. If the slot cannot be determined, // this field will be an empty struct. Slot Slot }
Attestation returns additional information about a key attested to be generated on a card. See https://developers.yubico.com/PIV/Introduction/PIV_attestation.html for more information.
func Verify ¶
func Verify(attestationCert, slotCert *x509.Certificate) (*Attestation, error)
Verify proves that a key was generated on a YubiKey. It ensures the slot and YubiKey certificate chains up to the Yubico CA, parsing additional information out of the slot certificate, such as the touch and PIN policies of a key.
type AuthError ¶
type AuthError struct { // Retries is the number of retries remaining if this error resulted from a retry-able // authentication attempt. If the authentication method is blocked or does not support // retries, this will be 0. Retries int }
AuthError is an error indicating an authentication error occurred (wrong PIN or blocked).
type Card ¶
Card is an exclusive open connection to a Card smart card. While open, no other process can query the given card.
To release the connection, call the Close method.
func (*Card) Attest ¶
func (c *Card) Attest(slot Slot) (cert *x509.Certificate, err error)
Attest generates a certificate for a key, signed by the YubiKey's attestation certificate. This can be used to prove a key was generate on a specific YubiKey.
This method is only supported for YubiKey versions >= 4.3.0. https://developers.yubico.com/PIV/Introduction/PIV_attestation.html
Certificates returned by this method MUST NOT be used for anything other than attestation or determining the slots public key. For example, the certificate is NOT suitable for TLS.
If the slot doesn't have a key, the returned error wraps ErrNotFound.
func (*Card) AttestationCertificate ¶
func (c *Card) AttestationCertificate() (*x509.Certificate, error)
AttestationCertificate returns the YubiKey's attestation certificate, which is unique to the key and signed by Yubico.
func (*Card) Certificate ¶
func (c *Card) Certificate(slot Slot) (*x509.Certificate, error)
Certificate returns the certificate object stored in a given slot.
If a certificate hasn't been set in the provided slot, the returned error wraps ErrNotFound.
func (*Card) GenerateKey ¶
GenerateKey generates an asymmetric key on the card, returning the key's public key.
func (*Card) Metadata ¶
Metadata returns public information about the given key slot. It is only supported by YubiKeys with a version >= 5.3.0.
func (*Card) PinProtectedData ¶ added in v0.3.0
func (c *Card) PinProtectedData(pin string) (*PinProtectedData, error)
PinProtectedData returns protected data stored on the card. This can be used to retrieve PIN protected management keys.
func (*Card) PrivateKey ¶
func (c *Card) PrivateKey(slot Slot, public crypto.PublicKey, auth KeyAuth) (crypto.PrivateKey, error)
PrivateKey is used to access signing and decryption options for the key stored in the slot. The returned key implements crypto.Signer and/or crypto.Decrypter depending on the key type.
If the public key hasn't been stored externally, it can be provided by fetching the slot's attestation certificate:
cert, err := c.Attest(slot) if err != nil { // ... } priv, err := c.PrivateKey(slot, cert.PublicKey, auth)
func (*Card) Reset ¶
Reset resets the PIV applet to its factory settings, wiping all slots and resetting the PIN, PUK, and Management Key to their default values. This does NOT affect data on other applets, such as GPG or U2F.
func (*Card) SetCertificate ¶
func (c *Card) SetCertificate(key ManagementKey, slot Slot, cert *x509.Certificate) error
SetCertificate stores a certificate object in the provided slot. Setting a certificate isn't required to use the associated key for signing or decryption.
func (*Card) SetManagementKey ¶
func (c *Card) SetManagementKey(oldKey, newKey ManagementKey) error
SetManagementKey updates the management key to a new key. Management keys are triple-des keys, however padding isn't verified. To generate a new key, generate 24 random bytes.
var newKey ManagementKey if _, err := io.ReadFull(rand.Reader, newKey[:]); err != nil { // ... } if err := c.SetManagementKey(piv.DefaultManagementKey, newKey); err != nil { // ... }
func (*Card) SetManagementKeyPinProtected ¶ added in v0.3.0
func (c *Card) SetManagementKeyPinProtected(oldKey ManagementKey, pin string) error
https://docs.yubico.com/yesdk/users-manual/application-piv/pin-only.html https://docs.yubico.com/yesdk/users-manual/application-piv/piv-objects.html#pinprotecteddata
func (*Card) SetPIN ¶
SetPIN updates the PIN to a new value. For compatibility, PINs should be 1-8 numeric characters.
To generate a new PIN, use the crypto/rand package.
// Generate a 6 character PIN. newPINInt, err := rand.Int(rand.Reader, bit.NewInt(1_000_000)) if err != nil { // ... } // Format with leading zeros. newPIN := fmt.Sprintf("%06d", newPINInt) if err := c.SetPIN(piv.DefaultPIN, newPIN); err != nil { // ... }
func (*Card) SetPUK ¶
SetPUK updates the PUK to a new value. For compatibility, PUKs should be 1-8 numeric characters.
To generate a new PUK, use the crypto/rand package.
// Generate a 8 character PUK. newPUKInt, err := rand.Int(rand.Reader, big.NewInt(100_000_000)) if err != nil { // ... } // Format with leading zeros. newPUK := fmt.Sprintf("%08d", newPUKInt) if err := c.SetPUK(piv.DefaultPUK, newPUK); err != nil { // ... }
func (*Card) SetPinProtectedData ¶ added in v0.3.0
func (c *Card) SetPinProtectedData(key ManagementKey, ppd *PinProtectedData) error
SetMetadata sets PIN protected metadata on the key. This is primarily to store the management key on the smart card instead of managing the PIN and management key separately.
func (*Card) SetPrivateKeyInsecure ¶
func (c *Card) SetPrivateKeyInsecure(key ManagementKey, slot Slot, private crypto.PrivateKey, policy Key) error
SetPrivateKeyInsecure is an insecure method which imports a private key into the slot. Users should almost always use GeneratePrivateKey() instead.
Importing a private key breaks functionality provided by this package, including AttestationCertificate() and Attest(). There are no stability guarantees for other methods for imported private keys.
Keys generated outside of the YubiKey should not be considered hardware-backed, as there's no way to prove the key wasn't copied, exfiltrated, or replaced with malicious material before being imported.
func (*Card) SupportsAttestation ¶ added in v0.3.0
func (*Card) VerifyPIN ¶
VerifyPIN attempts to authenticate against the card with the provided PIN.
PIN authentication for other operations are handled separately, and VerifyPIN does not need to be called before those methods.
After a specific number of authentication attempts with an invalid PIN, usually 3, the PIN will become block and refuse further attempts. At that point the PUK must be used to unblock the PIN.
Use DefaultPIN if the PIN hasn't been set.
type ECPPPrivateKey ¶ added in v0.3.0
type ECPPPrivateKey struct {
// contains filtered or unexported fields
}
ECPPPrivateKey is a crypto.PrivateKey implementation for EC keys. It implements crypto.Signer and the method SharedKey performs Diffie-Hellman key agreements.
Keys returned by Card.PrivateKey() may be type asserted to *ECPPPrivateKey, if the slot contains an EC key.
func (*ECPPPrivateKey) Public ¶ added in v0.3.0
func (k *ECPPPrivateKey) Public() crypto.PublicKey
Public returns the public key associated with this private key.
func (*ECPPPrivateKey) SharedKey ¶ added in v0.3.0
func (k *ECPPPrivateKey) SharedKey(peer *ecdsa.PublicKey) ([]byte, error)
SharedKey performs a Diffie-Hellman key agreement with the peer to produce a shared secret key.
Peer's public key must use the same algorithm as the key in this slot, or an error will be returned.
Length of the result depends on the types and sizes of the keys used for the operation. Callers should use a cryptographic key derivation function to extract the amount of bytes they need.
func (*ECPPPrivateKey) Sign ¶ added in v0.3.0
func (k *ECPPPrivateKey) Sign(_ io.Reader, digest []byte, _ crypto.SignerOpts) ([]byte, error)
Sign implements crypto.Signer.
type FormFactor ¶
type FormFactor int
FormFactor enumerates the physical set of forms a key can take. USB-A vs. USB-C and Keychain vs. Nano (and FIPS variants for these).
func (FormFactor) String ¶
func (f FormFactor) String() string
String returns the human-readable description for the given form-factor value, or a fallback value for any other, unknown form-factor.
type Key ¶
type Key struct { // Algorithm to use when generating the key. Algorithm Algorithm // PINPolicy for the key. // // BUG(ericchiang): some older YubiKeys (third generation) will silently // drop this value. If PINPolicyNever or PINPolicyOnce is supplied but the // key still requires a PIN every time, you may be using a buggy key and // should supply PINPolicyAlways. See https://cunicu.li/go-piv/issues/60 PINPolicy PINPolicy // TouchPolicy for the key. TouchPolicy TouchPolicy }
Key is used for key generation and holds different options for the key.
While keys can have default PIN and touch policies, this package currently doesn't support this option, and all fields must be provided.
type KeyAuth ¶
type KeyAuth struct { // PIN, if provided, is a static PIN used to authenticate against the key. // If provided, PINPrompt is ignored. PIN string // PINPrompt can be used to interactively request the PIN from the user. The // method is only called when needed. For example, if a key specifies // PINPolicyOnce, PINPrompt will only be called once per card struct. PINPrompt func() (pin string, err error) // PINPolicy can be used to specify the PIN caching strategy for the slot. If // not provided, this will be inferred from the attestation certificate. // // This field is required on older (<4.3.0) YubiKeys when using PINPrompt, // as well as for keys imported to the card. PINPolicy PINPolicy }
KeyAuth is used to authenticate against the card on each signing and decryption request.
type ManagementKey ¶ added in v0.3.0
type ManagementKey [24]byte
type Metadata ¶
type Metadata struct { Algorithm Algorithm PINPolicy PINPolicy TouchPolicy TouchPolicy Origin Origin PublicKey crypto.PublicKey }
Metadata holds unprotected metadata about a key slot.
type Origin ¶
type Origin int
Origin represents whether a key was generated on the hardware, or has been imported into it.
type PINPolicy ¶
type PINPolicy int
PINPolicy represents PIN requirements when signing or decrypting with an asymmetric key in a given slot.
PIN policies supported by this package.
BUG(ericchiang): Caching for PINPolicyOnce isn't supported on YubiKey versions older than 4.3.0 due to issues with verifying if a PIN is needed. If specified, a PIN will be required for every operation.
type PinProtectedData ¶ added in v0.3.0
PinProtectedData holds PIN protected data. This is primarily used by YubiKey manager to implement PIN protect management keys, storing management keys on the card guarded by the PIN.
func (PinProtectedData) ManagementKey ¶ added in v0.3.0
func (d PinProtectedData) ManagementKey() (k ManagementKey, err error)
func (*PinProtectedData) SetManagementKey ¶ added in v0.3.0
func (d *PinProtectedData) SetManagementKey(key ManagementKey) error
type Slot ¶
type Slot struct { // Key is a reference for a key type. // // See: https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-73-4.pdf#page=32 Key byte // Object is a reference for data object. // // See: https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-73-4.pdf#page=30 Object Object }
Slot is a private key and certificate combination managed by the security key.
func SlotRetiredKeyManagement ¶ added in v0.3.0
SlotRetiredKeyManagement provides access to "retired" slots. Slots meant for old Key Management keys that have been rotated. YubiKeys 4 and later support values between 0x82 and 0x95 (inclusive).
slot, ok := SlotRetiredKeyManagement(0x82) if !ok { // unrecognized slot } pub, err := c.GenerateKey(managementKey, slot, key)
type TouchPolicy ¶
type TouchPolicy int
TouchPolicy represents proof-of-presence requirements when signing or decrypting with asymmetric key in a given slot.
const ( TouchPolicyNever TouchPolicy = iota + 1 TouchPolicyAlways TouchPolicyCached )
Touch policies supported by this package.
type UnsupportedCurveError ¶ added in v0.3.0
type UnsupportedCurveError struct {
// contains filtered or unexported fields
}
UnsupportedCurveError is used when a key has an unsupported curve
func (UnsupportedCurveError) Error ¶ added in v0.3.0
func (e UnsupportedCurveError) Error() string
type Verifier ¶
type Verifier struct { // Root certificates to use to validate challenges. If nil, this defaults to Yubico's // CA bundle. // // https://developers.yubico.com/PIV/Introduction/PIV_attestation.html // https://developers.yubico.com/PIV/Introduction/piv-attestation-ca.pem // https://developers.yubico.com/U2F/yubico-u2f-ca-certs.txt Roots *x509.CertPool }
Verifier allows specifying options when verifying attestations produced by YubiKeys.
func (*Verifier) Verify ¶
func (v *Verifier) Verify(attestationCert, slotCert *x509.Certificate) (*Attestation, error)
Verify proves that a key was generated on a YubiKey.
As opposed to the package level Verify, it uses any options enabled on the Verifier.
Notes ¶
Bugs ¶
some older YubiKeys (third generation) will silently drop this value. If PINPolicyNever or PINPolicyOnce is supplied but the key still requires a PIN every time, you may be using a buggy key and should supply PINPolicyAlways. See https://cunicu.li/go-piv/issues/60
Caching for PINPolicyOnce isn't supported on YubiKey versions older than 4.3.0 due to issues with verifying if a PIN is needed. If specified, a PIN will be required for every operation.