-
Notifications
You must be signed in to change notification settings - Fork 14
/
ykoath.go
206 lines (157 loc) · 4.36 KB
/
ykoath.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package ykoath
import (
"encoding/binary"
"fmt"
"strconv"
"strings"
"time"
"github.com/ebfe/scard"
"github.com/pkg/errors"
)
type card interface {
Disconnect(scard.Disposition) error
Transmit([]byte) ([]byte, error)
}
type context interface {
Release() error
}
type debugger func(string, ...interface{})
// OATH implements most parts of the TOTP portion of the YKOATH specification
// https://developers.yubico.com/OATH/YKOATH_Protocol.html
type OATH struct {
card card
Clock func() time.Time
context context
Debug debugger
}
const (
errFailedToConnect = "failed to connect to reader"
errFailedToDisconnect = "failed to disconnect from reader"
errFailedToEstablishContext = "failed to establish context"
errFailedToListReaders = "failed to list readers"
errFailedToListSuitableReader = "no suitable reader found (out of %d readers)"
errFailedToReleaseContext = "failed to release context"
errFailedToTransmit = "failed to transmit APDU"
errFailedToReadSerial = "failed to read serial"
errUnknownTag = "unknown tag (%x)"
)
// New initializes a new OATH session
func New() (*OATH, error) {
return NewFromSerialList([]string{})
}
// NewFromSerial creates an OATH session for a specific key
func NewFromSerial(serial string) (*OATH, error) {
return NewFromSerialList([]string{serial})
}
// NewFromSerialList creates an OATH session from the first match found for a list of keys
func NewFromSerialList(serialList []string) (*OATH, error) {
yubikeys, err := NewSet()
if err != nil {
return nil, err
}
for _, yubikey := range yubikeys {
if len(serialList) == 0 {
return yubikey, nil
}
serial, err := yubikey.Serial()
if err != nil {
return nil, errors.Wrapf(err, errFailedToReadSerial)
}
for _, match := range serialList {
if serial == match {
return yubikey, nil
}
}
}
return nil, fmt.Errorf(errFailedToListSuitableReader, len(yubikeys))
}
// NewSet returns a slice of all Yubikeys on the system
func NewSet() ([]*OATH, error) {
context, err := scard.EstablishContext()
if err != nil {
return []*OATH{}, errors.Wrapf(err, errFailedToEstablishContext)
}
readers, err := context.ListReaders()
if err != nil {
return []*OATH{}, errors.Wrapf(err, errFailedToListReaders)
}
var yubikeys []*OATH
for _, reader := range readers {
if !strings.Contains(strings.ToLower(reader), "yubikey") {
continue
}
card, err := context.Connect(reader, scard.ShareShared, scard.ProtocolAny)
if err != nil {
return nil, errors.Wrapf(err, errFailedToConnect)
}
o := OATH{
card: card,
Clock: time.Now,
context: context,
}
yubikeys = append(yubikeys, &o)
}
return yubikeys, nil
}
// Close terminates an OATH session
func (o *OATH) Close() error {
if err := o.card.Disconnect(scard.LeaveCard); err != nil {
return errors.Wrapf(err, errFailedToDisconnect)
}
if err := o.context.Release(); err != nil {
return errors.Wrapf(err, errFailedToReleaseContext)
}
return nil
}
func (o *OATH) Serial() (string, error) {
_, err := o.card.Transmit([]byte{0x00, 0xA4, 0x04, 0x00, 0x08, 0xA0, 0x00, 0x00, 0x05, 0x27, 0x47, 0x11, 0x17})
if err != nil {
return "", err
}
resp, err := o.card.Transmit([]byte{0x00, 0x1d, 0x00, 0x00})
if err != nil {
return "", err
}
kvs := read(resp[1 : len(resp)-2])
for _, item := range kvs {
if item.tag == 0x02 {
return strconv.FormatUint(uint64(binary.BigEndian.Uint32(item.value)), 10), nil
}
}
return "", errors.Wrapf(fmt.Errorf("no serial tag found"), errFailedToReleaseContext)
}
// send sends an APDU to the card
func (o *OATH) send(cla, ins, p1, p2 byte, data ...[]byte) (tvs, error) {
var (
code code
results []byte
send = append([]byte{cla, ins, p1, p2}, write(0x00, data...)...)
)
for {
if o.Debug != nil {
o.Debug("SEND % x (%d)", send, len(send))
}
res, err := o.card.Transmit(send)
if err != nil {
return nil, errors.Wrapf(err, errFailedToTransmit)
}
if o.Debug != nil {
o.Debug("RECV % x (%d)", res, len(res))
}
code = res[len(res)-2:]
results = append(results, res[0:len(res)-2]...)
if code.IsMore() {
send = []byte{0x00, 0xa5, 0x00, 0x00}
if o.Debug != nil {
o.Debug("MORE %d", int(code[1]))
}
} else if code.IsSuccess() {
if o.Debug != nil {
o.Debug("DONE")
}
return read(results), nil
} else {
return nil, code
}
}
}