-
Notifications
You must be signed in to change notification settings - Fork 0
/
yubikey.go
151 lines (133 loc) · 3.96 KB
/
yubikey.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// YubiKey
// For the full copyright and license information, please view the LICENSE.txt file.
// Package yubikey provides PIV smart card interface for YubiKey security keys.
package yubikey
import (
"errors"
"fmt"
"strings"
"sync"
"github.com/go-piv/piv-go/piv"
)
var (
// Mutex to support concurrent requests and prevent "connections outstanding" errors.
openMu = sync.Mutex{}
// DefaultPIN holds the default card PIN.
DefaultPIN = piv.DefaultPIN
// DefaultPUK holds the default card PUK.
DefaultPUK = piv.DefaultPUK
// DefaultManagementKey holds the default card management key.
DefaultManagementKey = piv.DefaultManagementKey
// ErrOutstandingConnections returns an outstanding connections error.
ErrOutstandingConnections = errors.New("outstanding connections")
)
// Cards returns the connected YubiKey smart cards.
func Cards() ([]*Card, error) {
openMu.Lock()
defer openMu.Unlock()
// Get the card list
pivCards, err := piv.Cards()
if err != nil {
return nil, fmt.Errorf("couldn't get the smart card list: %s", err)
}
// Iterate over the card list and initialize the card instances
var cards []*Card
for _, v := range pivCards {
card := Card{
name: v,
pin: DefaultPIN,
puk: DefaultPUK,
manKey: DefaultManagementKey,
}
card.keyAuth = piv.KeyAuth{
PINPrompt: func() (string, error) {
return card.pin, nil
},
}
// Connect to the smart card and set the card info
yk, err := piv.Open(card.name)
if err != nil {
if strings.Contains(err.Error(), "connections outstanding") {
return nil, ErrOutstandingConnections
}
return nil, fmt.Errorf("couldn't connect to the YubiKey smart card (%s): %s", card.name, err)
}
s, err := yk.Serial()
if err != nil {
yk.Close()
return nil, fmt.Errorf("couldn't determined the YubiKey serial (%s): %s", card.name, err)
}
card.serial = fmt.Sprintf("%d", s)
card.version = yk.Version()
// Check the version
// Ref: https://developers.yubico.com/PIV/Introduction/PIV_attestation.html
if card.version.Major < 4 || (card.version.Major == 4 && card.version.Minor < 3) {
yk.Close()
return nil, fmt.Errorf("version of the YubiKey (%s) is not supported: %s", card.serial, card.Version())
}
// Add it into the list and close the card.
cards = append(cards, &card)
yk.Close()
}
return cards, nil
}
// CardSlots returns the card slots by the given card serials, slots and pins.
// It doesn't return error if the given serial or slot not found.
func CardSlots(serials, slots, pins []string) (map[string]map[string]*Slot, error) {
// Get the card list
cards, err := Cards()
if err != nil {
return nil, err
}
// Iterate over the given serial numbers
result := make(map[string]map[string]*Slot)
for k, serial := range serials {
// Iterate over the connected cards
for _, card := range cards {
if card.Serial() != serial {
continue
}
if result[serial] == nil {
result[serial] = make(map[string]*Slot)
}
// TODO: Check https://github.com/go-piv/piv-go/issues/47
if k < len(pins) {
card.SetPIN(pins[k])
}
// Get the card slots
cardSlots, err := card.SlotsByKey(slots)
if err != nil {
return nil, err
}
// Iterate over the card slots
for _, slot := range cardSlots {
result[serial][slot.key] = slot
}
}
}
return result, nil
}
// CardSlot returns a card slot by the given card serial, slot and pin.
func CardSlot(serial, slot, pin string) (*Slot, error) {
// Check args
if serial == "" || slot == "" {
return nil, errors.New("missing key serial or slot")
}
// Get the card slots
slots, err := CardSlots([]string{serial}, []string{slot}, []string{pin})
if err != nil {
return nil, fmt.Errorf("couldn't retrieve slot list: %s", err)
}
if len(slots) > 0 {
for k, v := range slots {
if k == serial {
if slot, ok := v[slot]; ok {
return slot, nil
}
break
}
}
return nil, fmt.Errorf("key slot not found: %s:%s", serial, slot)
}
return nil, fmt.Errorf("key not found: %s", serial)
}