ykpiv: pault.ag/go/ykpiv Index | Files

package ykpiv

import "pault.ag/go/ykpiv"

Index

Package Files

decrypt.go errors.go generate.go pivman.go sign.go slot.go tls.go ykpiv.go

Variables

var (
    MemoryError         = Error{Code: C.YKPIV_MEMORY_ERROR, Message: "Memory Error"}
    PCSCError           = Error{Code: C.YKPIV_PCSC_ERROR, Message: "PKCS Error"}
    SizeError           = Error{Code: C.YKPIV_SIZE_ERROR, Message: "Size Error"}
    AppletError         = Error{Code: C.YKPIV_APPLET_ERROR, Message: "Applet Error"}
    AuthenticationError = Error{Code: C.YKPIV_AUTHENTICATION_ERROR, Message: "Authentication Error"}
    RandomnessError     = Error{Code: C.YKPIV_RANDOMNESS_ERROR, Message: "Randomness Error"}
    GenericError        = Error{Code: C.YKPIV_GENERIC_ERROR, Message: "Generic Error"}
    KeyError            = Error{Code: C.YKPIV_KEY_ERROR, Message: "Key Error"}
    ParseError          = Error{Code: C.YKPIV_PARSE_ERROR, Message: "Parse Error"}
    WrongPIN            = Error{Code: C.YKPIV_WRONG_PIN, Message: "Wrong PIN"}
    InvalidObject       = Error{Code: C.YKPIV_INVALID_OBJECT, Message: "Invalid Object"}
    AlgorithmError      = Error{Code: C.YKPIV_ALGORITHM_ERROR, Message: "Algorithm Error"}
    PINLockedError      = Error{Code: C.YKPIV_PIN_LOCKED, Message: "PIN Locked"}

    SecurityStatusError = Error{Code: C.SW_ERR_SECURITY_STATUS, Message: "Security Status Error"}
    AuthBlocked         = Error{Code: C.SW_ERR_AUTH_BLOCKED, Message: "Auth Blocked"}
    IncorrectParam      = Error{Code: C.SW_ERR_INCORRECT_PARAM, Message: "Incorrect Param"}
    IncorrectSlot       = Error{Code: C.SW_ERR_INCORRECT_SLOT, Message: "Incorrect Slot"}
)

func Readers Uses

func Readers() ([]string, error)

Get a list of strings that the ykpiv library has identified as unique ways to fetch every reader attached to the system. This can be handy to define a "Reader" argument in ykpiv.Options, and may include things ykpiv can't talk to.

type Error Uses

type Error struct {

    // int representing the underlying error code as ykpiv had given us. The
    // exact numbers can be found in your local ykpiv.h, inside the ykpiv_rc
    // enum.
    Code int

    // Human readable message regarding what happened.
    Message string
    // contains filtered or unexported fields
}

Go wrapper around the internal C ykpiv error integers. Error codes as they exist in ykpiv.h are brought into Go, since we can add some additional context around them, as well as implement the Error interface.

func (Error) Equal Uses

func (e Error) Equal(err error) bool

Check to see if another Error is the same as our struct. This compares the underlying Code integer.

func (Error) Error Uses

func (e Error) Error() string

Error interface. This will sprintf a string containing where this error came from, the human message, and the underlying ykpiv code, to aid with debugging.

type Options Uses

type Options struct {

    // When true, this will cause the underlying ykpiv library to emit additional
    // information to stderr. This can be helpful when debugging why something
    // isn't working as expected.
    Verbose bool

    // String to be used when searching for a Yubikey. The comparison
    // will be against the output you can observe from
    // `yubico-piv-tool -a list-readers`.
    Reader string

    // PIN is the pin that will be used when logging in
    PIN *string

    // PUK is the PUK to be used when logging in
    PUK *string

    // ManagementKey is the Management Key to be used for key operations
    ManagementKey []byte

    // Flag to let ykpiv know if this PIV token has a ManagementKey that was
    // set by pivman, which is a PBKDF2 SHA1 key derived with a salt held on
    // chip in the internal pivman data.
    //
    // If this is `true`, ManagementKey will be ignored in favor of deriving
    // the key from the PIN.
    ManagementKeyIsPIN bool
}

Configuration for initialization of the Yubikey, as well as options that may be used during runtime.

func (Options) GetManagementKey Uses

func (o Options) GetManagementKey(y Yubikey) ([]byte, error)

Get the Management Key.

On some configurations, users have set the Management Key to a PBKDF2 SHA1 key derived from the PIN, so this may return one of two things:

If `ManagementKeyIsPIN` is false, the `ManagementKey` byte array will be returned.

If `ManagementKeyIsPIN` is true, the `PIN` will be used, in conjunction with a salt held within the PIVMON object address to compute the ManagementKey. If PIN is nil, this will result in an error being returned.

func (Options) GetPIN Uses

func (o Options) GetPIN() (string, error)

Get the user defined PIN. This will return an error if PUK is nil.

func (Options) GetPUK Uses

func (o Options) GetPUK() (string, error)

Get the user defined PUK. This will return an error if PUK is nil.

type Slot Uses

type Slot struct {
    Id          SlotId
    PublicKey   crypto.PublicKey
    Certificate *x509.Certificate
    // contains filtered or unexported fields
}

Slot abstracts a public key, private key, and x509 Certificate stored on the PIV device.

Internally, this keeps track of the Yubikey this came from, the underlying object identifiers for the Certificate and Key we care about, as well as other bits and bobs of state.

func (Slot) Decrypt Uses

func (s Slot) Decrypt(rand io.Reader, msg []byte, opts crypto.DecrypterOpts) ([]byte, error)

Decrypt decrypts ciphertext with the private key backing the Slot we're operating on. This implements the crypto.Decrypter interface.

The `rand` argument is disregarded in favor of the on-chip RNG on the Yubikey The `opts` argument is not used at this time, but may in the future.

func (Slot) GetCertificate Uses

func (y Slot) GetCertificate() (*x509.Certificate, error)

Get the x509.Certificate stored in the PIV Slot off the chip

func (Slot) Public Uses

func (s Slot) Public() crypto.PublicKey

Return the crypto.PublicKey that we know corresponds to the Certificate we have on hand.

func (Slot) Sign Uses

func (s Slot) Sign(_ io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error)

Sign implements the crypto.Signer.Sign interface.

Unlike other Sign implementations, `rand` will be completely discarded in favor of the on-chip RNG.

The output will be a PKCS#1 v1.5 signature over the digest.

func (Slot) TLSCertificate Uses

func (slot Slot) TLSCertificate() tls.Certificate

Create a tls.Certificate fit for use in crypto/tls applications, such as net/http, or grpc.

func (*Slot) Update Uses

func (y *Slot) Update(cert x509.Certificate) error

Write the x509 Certificate to the Yubikey.

type SlotId Uses

type SlotId struct {
    Certificate int32
    Key         int32
    Name        string
}

SlotId encapsulates the Identifiers required to preform key operations on the Yubikey. The identifier most people would know (if this is a thing that people do who don't write PIV aware applications) would be the `Key` Id, something like 0x9A.

var (
    // PIV Authentication, which is a certificate and key pair and can be used
    // to verify that the PIV credential was issued by an authorized entity,
    // has not expired, has not been revoked, and holder of the credential
    // (YOU) is the same individual it was issued to.
    Authentication SlotId = SlotId{
        Certificate: C.YKPIV_OBJ_AUTHENTICATION,
        Key:         C.YKPIV_KEY_AUTHENTICATION,
        Name:        "Authentication",
    }

    // Digital Signature, which is a certificate and key pair allows the YOU to
    // digitally sign a document or email, providing both integrity and
    // non-repudiation.
    Signature SlotId = SlotId{
        Certificate: C.YKPIV_OBJ_SIGNATURE,
        Key:         C.YKPIV_KEY_SIGNATURE,
        Name:        "Digital Signature",
    }

    // Card Authentication, which is a certificate and key pair that can be
    // used to verify that the PIV credential was issued by an authorized
    // entity, has not expired, and has not been revoked.
    CardAuthentication SlotId = SlotId{
        Certificate: C.YKPIV_OBJ_CARD_AUTH,
        Key:         C.YKPIV_KEY_CARDAUTH,
        Name:        "Card Authentication",
    }

    KeyManagement SlotId = SlotId{
        Certificate: C.YKPIV_OBJ_KEY_MANAGEMENT,
        Key:         C.YKPIV_KEY_KEYMGM,
        Name:        "Key Management",
    }
)

More information regarding the basic PIV slots can be founnd at the FICAM piv-gude: https://piv.idmanagement.gov/elements/

func (SlotId) String Uses

func (s SlotId) String() string

Return a human readable string mostly useful for debugging which Slot you might have your hands on. Since most people (for some value of "most") would want the Key, this only includes that.

type Yubikey Uses

type Yubikey struct {
    // contains filtered or unexported fields
}

Encapsulation of the ykpiv internal state object, and the configuration in new. This needs to be initalized through `ykpiv.New` to ensure the internal state is brought up correctly.

This object represents a single physical yubikey that we've connected to. This object provides a number of helper functions hanging off the struct to avoid keeping and passing the internal ykpiv state object in C.

`.Close()` must be called, or this will leak memory.

func New Uses

func New(opts Options) (*Yubikey, error)

Create a new Yubikey.

This will use the options in the given `ykpiv.Options` struct to find the correct Yubikey, initialize the underlying state, and ensure the right bits are set.

func (Yubikey) Authenticate Uses

func (y Yubikey) Authenticate() error

Authenticate to the Yubikey using the Management Key. This key is a 24 byte key that's used as a 3DES key internally to write new Certificates, or create a new keypair.

func (Yubikey) Authentication Uses

func (y Yubikey) Authentication() (*Slot, error)

Get the PIV Authentication Slot off the Yubikey. This is identical to invoking `yubikey.Slot(ykpiv.Authentication)`.

func (Yubikey) CardAuthentication Uses

func (y Yubikey) CardAuthentication() (*Slot, error)

Get the PIV Card Authentication Slot off the Yubikey. This is identical to invoking `yubikey.Slot(ykpiv.CardAuthentication)`

func (Yubikey) ChangePIN Uses

func (y Yubikey) ChangePIN(oldPin, newPin string) error

Change your PIN on the Yubikey from the oldPin to the newPin.

func (Yubikey) ChangePUK Uses

func (y Yubikey) ChangePUK(newPuk string) error

Change the PUK.

func (Yubikey) Close Uses

func (y Yubikey) Close() error

Close the Yubikey object, and preform any finization needed to avoid leaking memory or holding locks.

func (Yubikey) GenerateRSA Uses

func (y Yubikey) GenerateRSA(id SlotId, bits int) (*Slot, error)

Generate an RSA Keypair in slot `id` (using a modulus size of `bits`), and construct a Certificate-less Slot. This Slot can not be recovered later, so it should be used to sign a CSR or Self-Signed Certificate before we lose the key material.

func (Yubikey) GetCertificate Uses

func (y Yubikey) GetCertificate(slotId SlotId) (*x509.Certificate, error)

func (Yubikey) GetObject Uses

func (y Yubikey) GetObject(id int) ([]byte, error)

Get the raw bytes out of a slot stored on the Yubikey. Callers to this function should only do so if they understand exactly what data they're reading, what the data should look like, and avoid rebuilding existing interfaces if at all possible.

The related method, SaveObject, can be used to write data to be read back later. Care must be taken to ensure the `id` is *not* being used by any other applications.

func (Yubikey) KeyManagement Uses

func (y Yubikey) KeyManagement() (*Slot, error)

Get the PIV Key Management Slot off the Yubikey. This is identical to invoking `yubikey.Slot(ykpiv.KeyManagement)`

func (Yubikey) Login Uses

func (y Yubikey) Login() error

Log into the Yubikey using the user PIN.

func (Yubikey) PINRetries Uses

func (y Yubikey) PINRetries() (int, error)

PIN Retries

func (Yubikey) Reset Uses

func (y Yubikey) Reset() error

Reset the Yubikey.

This can only be done if both the PIN and PUK have been blocked, and will wipe all data on the Key. This includes all Certificates, public and private key material.

func (Yubikey) SaveCertificate Uses

func (y Yubikey) SaveCertificate(slotId SlotId, cert x509.Certificate) error

Write the x509 Certificate to the Yubikey.

func (Yubikey) SaveObject Uses

func (y Yubikey) SaveObject(id int32, data []byte) error

Write the raw bytes out of a slot stored on the Yubikey. Callers to this function should only do so if they understand exactly what data they're writnig, what the data should look like, and avoid rebuilding existing interfaces if at all possible.

The related method, GetObject, can be used to read data later. Care must be taken to ensure the `id` is *not* being used by any other applications.

func (Yubikey) SetMGMKey Uses

func (y Yubikey) SetMGMKey(key []byte) error

Set the Yubikey Management Key. The Management key is a 24 byte key that's used as a 3DES key internally to preform key operations, such as Certificate import, or keypair generation.

func (Yubikey) Signature Uses

func (y Yubikey) Signature() (*Slot, error)

Get the Digital Signature Slot off the Yubikey. This is identical to invoking `yubikey.Slot(ykpiv.Signature)`

func (Yubikey) Slot Uses

func (y Yubikey) Slot(id SlotId) (*Slot, error)

Get a Slot off of the Yubikey by the SlotId.

This will trigger an attempt to get (and parse) the x509 Certificate for this slot. Only slots with an x509 Certificate can be used.

func (Yubikey) UnblockPIN Uses

func (y Yubikey) UnblockPIN(newPin string) error

Using the PUK, unblock the PIN, resetting the retry counter.

func (Yubikey) Version Uses

func (y Yubikey) Version() ([]byte, error)

Return the ykpiv application version. This is expected to be in the format of '1.2.3', but is up to the underlying ykpiv application code.

Package ykpiv imports 14 packages (graph). Updated 2017-09-02. Refresh now. Tools for package owners.