|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto" |
| 5 | + "crypto/x509" |
| 6 | + "fmt" |
| 7 | + "github.com/github/certstore" |
| 8 | + "github.com/go-piv/piv-go/piv" |
| 9 | + "github.com/pkg/errors" |
| 10 | + "golang.org/x/term" |
| 11 | + "io" |
| 12 | +) |
| 13 | + |
| 14 | +// PivIdentities enumerates identities stored in the signature slot inside hardware keys |
| 15 | +func PivIdentities() ([]PivIdentity, error) { |
| 16 | + cards, err := piv.Cards() |
| 17 | + if err != nil { |
| 18 | + return nil, err |
| 19 | + } |
| 20 | + var identities []PivIdentity |
| 21 | + for _, card := range cards { |
| 22 | + yk, err := piv.Open(card) |
| 23 | + if err != nil { |
| 24 | + continue |
| 25 | + } |
| 26 | + cert, err := yk.Certificate(piv.SlotSignature) |
| 27 | + if err != nil { |
| 28 | + continue |
| 29 | + } |
| 30 | + if cert != nil { |
| 31 | + pin := promptHardwareKeyPin(card) |
| 32 | + ident := PivIdentity{card: card, pin: pin, yk: yk} |
| 33 | + identities = append(identities, ident) |
| 34 | + } |
| 35 | + } |
| 36 | + return identities, nil |
| 37 | +} |
| 38 | + |
| 39 | +// PivIdentity is an entity identity stored in a hardware key PIV applet |
| 40 | +type PivIdentity struct { |
| 41 | + card string |
| 42 | + pin string |
| 43 | + yk *piv.YubiKey |
| 44 | +} |
| 45 | + |
| 46 | +var _ certstore.Identity = (*PivIdentity)(nil) |
| 47 | +var _ crypto.Signer = (*PivIdentity)(nil) |
| 48 | + |
| 49 | +// Certificate implements the certstore.Identity interface |
| 50 | +func (ident *PivIdentity) Certificate() (*x509.Certificate, error) { |
| 51 | + return ident.yk.Certificate(piv.SlotSignature) |
| 52 | +} |
| 53 | + |
| 54 | +// CertificateChain implements the certstore.Identity interface |
| 55 | +func (ident *PivIdentity) CertificateChain() ([]*x509.Certificate, error) { |
| 56 | + return []*x509.Certificate{}, nil |
| 57 | +} |
| 58 | + |
| 59 | +// Signer implements the certstore.Identity interface |
| 60 | +func (ident *PivIdentity) Signer() (crypto.Signer, error) { |
| 61 | + return ident, nil |
| 62 | +} |
| 63 | + |
| 64 | +// Delete implements the certstore.Identity interface |
| 65 | +func (ident *PivIdentity) Delete() error { |
| 66 | + panic("deleting identities on PIV applet is not supported") |
| 67 | +} |
| 68 | + |
| 69 | +// Close implements the certstore.Identity interface |
| 70 | +func (ident *PivIdentity) Close() { |
| 71 | + _ = ident.yk.Close() |
| 72 | +} |
| 73 | + |
| 74 | +// Public implements the crypto.Signer interface |
| 75 | +func (ident *PivIdentity) Public() crypto.PublicKey { |
| 76 | + cert, err := ident.Certificate() |
| 77 | + if err != nil { |
| 78 | + return nil |
| 79 | + } |
| 80 | + return cert.PublicKey |
| 81 | +} |
| 82 | + |
| 83 | +// Sign implements the crypto.Signer interface |
| 84 | +func (ident *PivIdentity) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) { |
| 85 | + fmt.Printf("Touch \"%v\" now to sign\n", ident.card) |
| 86 | + private, err := ident.yk.PrivateKey(piv.SlotSignature, ident.Public(), piv.KeyAuth{PIN: ident.pin}) |
| 87 | + if err != nil { |
| 88 | + return nil, errors.Wrap(err, "failed to get private key for signing") |
| 89 | + } |
| 90 | + switch private.(type) { |
| 91 | + case *piv.ECDSAPrivateKey: |
| 92 | + return private.(*piv.ECDSAPrivateKey).Sign(rand, digest, opts) |
| 93 | + default: |
| 94 | + return nil, fmt.Errorf("invalid key type") |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func promptHardwareKeyPin(name string) string { |
| 99 | + fmt.Printf("enter PIN for hardware key \"%v\" (press enetr for default):\n", name) |
| 100 | + pin, err := term.ReadPassword(0) |
| 101 | + var hardwareKeyPin string |
| 102 | + if err != nil { |
| 103 | + hardwareKeyPin = piv.DefaultPIN |
| 104 | + } else { |
| 105 | + if len(pin) > 0 { |
| 106 | + hardwareKeyPin = string(pin) |
| 107 | + } else { |
| 108 | + hardwareKeyPin = piv.DefaultPIN |
| 109 | + } |
| 110 | + } |
| 111 | + return hardwareKeyPin |
| 112 | +} |
0 commit comments