-
Notifications
You must be signed in to change notification settings - Fork 23
/
hid.go
281 lines (237 loc) · 6.33 KB
/
hid.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Package u2fhid implements the low-level FIDO U2F HID protocol.
package u2fhid
import (
"bytes"
"crypto/rand"
"encoding/binary"
"fmt"
"io"
"sync"
"time"
"github.com/flynn/hid"
)
const (
cmdPing = 0x80 | 0x01
cmdMsg = 0x80 | 0x03
cmdLock = 0x80 | 0x04
cmdInit = 0x80 | 0x06
cmdWink = 0x80 | 0x08
cmdSync = 0x80 | 0x3c
cmdError = 0x80 | 0x3f
broadcastChannel = 0xffffffff
capabilityWink = 1
minMessageLen = 7
maxMessageLen = 7609
minInitResponseLen = 17
responseTimeout = 3 * time.Second
fidoUsagePage = 0xF1D0
u2fUsage = 1
)
var errorCodes = map[uint8]string{
1: "invalid command",
2: "invalid parameter",
3: "invalid message length",
4: "invalid message sequencing",
5: "message timed out",
6: "channel busy",
7: "command requires channel lock",
8: "sync command failed",
}
// Devices lists available HID devices that advertise the U2F HID protocol.
func Devices() ([]*hid.DeviceInfo, error) {
devices, err := hid.Devices()
if err != nil {
return nil, err
}
res := make([]*hid.DeviceInfo, 0, len(devices))
for _, d := range devices {
if d.UsagePage == fidoUsagePage && d.Usage == u2fUsage {
res = append(res, d)
}
}
return res, nil
}
// Open initializes a communication channel with a U2F HID device.
func Open(info *hid.DeviceInfo) (*Device, error) {
hidDev, err := info.Open()
if err != nil {
return nil, err
}
d := &Device{
info: info,
device: hidDev,
readCh: hidDev.ReadCh(),
}
if err := d.init(); err != nil {
return nil, err
}
return d, nil
}
// A Device is used to communicate with a U2F HID device.
type Device struct {
ProtocolVersion uint8
MajorDeviceVersion uint8
MinorDeviceVersion uint8
BuildDeviceVersion uint8
// RawCapabilities is the raw capabilities byte provided by the device
// during initialization.
RawCapabilities uint8
// CapabilityWink is true if the device advertised support for the wink
// command during initilization. Even if this flag is true, the device may
// not actually do anything if the command is called.
CapabilityWink bool
info *hid.DeviceInfo
device hid.Device
channel uint32
mtx sync.Mutex
readCh <-chan []byte
buf []byte
}
func (d *Device) sendCommand(channel uint32, cmd byte, data []byte) error {
if len(data) > maxMessageLen {
return fmt.Errorf("u2fhid: message is too long")
}
// zero buffer
for i := range d.buf {
d.buf[i] = 0
}
binary.BigEndian.PutUint32(d.buf[1:], channel)
d.buf[5] = cmd
binary.BigEndian.PutUint16(d.buf[6:], uint16(len(data)))
n := copy(d.buf[8:], data)
data = data[n:]
if err := d.device.Write(d.buf); err != nil {
return err
}
var seq uint8
for len(data) > 0 {
// zero buffer
for i := range d.buf {
d.buf[i] = 0
}
binary.BigEndian.PutUint32(d.buf[1:], channel)
d.buf[5] = seq
seq++
n := copy(d.buf[6:], data)
data = data[n:]
if err := d.device.Write(d.buf); err != nil {
return err
}
}
return nil
}
func (d *Device) readResponse(channel uint32, cmd byte) ([]byte, error) {
timeout := time.After(responseTimeout)
haveFirst := false
var buf []byte
var expected int
for {
select {
case msg, ok := <-d.readCh:
if len(msg) < minMessageLen {
return nil, fmt.Errorf("u2fhid: message is too short, only received %d bytes", len(msg))
}
if !ok {
return nil, fmt.Errorf("u2fhid: error reading response, device closed")
}
if channel != binary.BigEndian.Uint32(msg) {
continue
}
if msg[4] == cmdError {
errMsg, ok := errorCodes[msg[7]]
if !ok {
return nil, fmt.Errorf("u2fhid: received unknown error response %d", msg[7])
}
return nil, fmt.Errorf("u2fhid: received error from device: %s", errMsg)
}
if !haveFirst {
if msg[4] != cmd {
return nil, fmt.Errorf("u2fhid: error reading response, unexpected command %d, wanted %d", msg[4], cmd)
}
haveFirst = true
expected = int(binary.BigEndian.Uint16(msg[5:]))
buf = make([]byte, 0, expected)
msg = msg[7:]
if len(msg) > expected {
msg = msg[:expected]
}
buf = append(buf, msg...)
} else {
if msg[4]&0x80 != 0 {
return nil, fmt.Errorf("u2fhid: error reading response, unexpected command %d, wanted continuation", msg[4])
}
msg = msg[5:]
if len(msg) > expected-len(buf) {
msg = msg[:expected-len(buf)]
}
buf = append(buf, msg...)
}
if len(buf) >= expected {
return buf, nil
}
case <-timeout:
return nil, fmt.Errorf("u2fhid: error reading response, read timed out")
}
}
}
func (d *Device) init() error {
d.buf = make([]byte, d.info.OutputReportLength+1)
nonce := make([]byte, 8)
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
return err
}
if err := d.sendCommand(broadcastChannel, cmdInit, nonce); err != nil {
return err
}
for {
res, err := d.readResponse(broadcastChannel, cmdInit)
if err != nil {
return err
}
if len(res) < minInitResponseLen {
return fmt.Errorf("u2fhid: init response is short, wanted %d, got %d bytes", minInitResponseLen, len(res))
}
if !bytes.Equal(nonce, res[:8]) {
// nonce doesn't match, this init reply isn't for us
continue
}
d.channel = binary.BigEndian.Uint32(res[8:])
d.ProtocolVersion = res[12]
d.MajorDeviceVersion = res[13]
d.MinorDeviceVersion = res[14]
d.BuildDeviceVersion = res[15]
d.RawCapabilities = res[16]
d.CapabilityWink = d.RawCapabilities&capabilityWink != 0
break
}
return nil
}
// Command sends a command and associated data to the device and returns the
// response.
func (d *Device) Command(cmd byte, data []byte) ([]byte, error) {
d.mtx.Lock()
defer d.mtx.Unlock()
if err := d.sendCommand(d.channel, cmd, data); err != nil {
return nil, err
}
return d.readResponse(d.channel, cmd)
}
// Ping sends data to the device that should be echoed back verbatim.
func (d *Device) Ping(data []byte) ([]byte, error) {
return d.Command(cmdPing, data)
}
// Wink performs a vendor-defined action to identify the device, like blinking
// an LED. It is not implemented correctly or at all on all devices.
func (d *Device) Wink() error {
_, err := d.Command(cmdWink, nil)
return err
}
// Message sends an encapsulated U2F protocol message to the device and returns
// the response.
func (d *Device) Message(data []byte) ([]byte, error) {
return d.Command(cmdMsg, data)
}
// Close closes the device and frees associated resources.
func (d *Device) Close() {
d.device.Close()
}