-
Notifications
You must be signed in to change notification settings - Fork 49
/
ledger_hid.go
266 lines (220 loc) · 6.32 KB
/
ledger_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
//go:build !ledger_mock && !ledger_zemu
// +build !ledger_mock,!ledger_zemu
/*******************************************************************************
* (c) Zondax AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/
package ledger_go
import (
"errors"
"fmt"
"log"
"sync"
"time"
"github.com/zondax/hid"
)
const (
VendorLedger = 0x2c97
UsagePageLedgerNanoS = 0xffa0
Channel = 0x0101
PacketSize = 64
)
type LedgerAdminHID struct{}
type LedgerDeviceHID struct {
device *hid.Device
readCo *sync.Once
readChannel chan []byte
}
// list of supported product ids as well as their corresponding interfaces
// based on https://github.com/LedgerHQ/ledger-live/blob/develop/libs/ledgerjs/packages/devices/src/index.ts
var supportedLedgerProductID = map[uint8]int{
0x40: 0, // Ledger Nano X
0x10: 0, // Ledger Nano S
0x50: 0, // Ledger Nano S Plus
0x60: 0, // Ledger Stax
0x70: 0, // Ledger Flex
}
func NewLedgerAdmin() LedgerAdmin {
return &LedgerAdminHID{}
}
func (admin *LedgerAdminHID) ListDevices() ([]string, error) {
devices := hid.Enumerate(0, 0)
if len(devices) == 0 {
log.Println("No devices. Ledger LOCKED OR Other Program/Web Browser may have control of device.")
}
for _, d := range devices {
logDeviceInfo(d)
}
return []string{}, nil
}
func logDeviceInfo(d hid.DeviceInfo) {
log.Printf("============ %s\n", d.Path)
log.Printf("VendorID : %x\n", d.VendorID)
log.Printf("ProductID : %x\n", d.ProductID)
log.Printf("Release : %x\n", d.Release)
log.Printf("Serial : %x\n", d.Serial)
log.Printf("Manufacturer : %s\n", d.Manufacturer)
log.Printf("Product : %s\n", d.Product)
log.Printf("UsagePage : %x\n", d.UsagePage)
log.Printf("Usage : %x\n", d.Usage)
log.Printf("\n")
}
func isLedgerDevice(d hid.DeviceInfo) bool {
deviceFound := d.UsagePage == UsagePageLedgerNanoS
// Workarounds for possible empty usage pages
productIDMM := uint8(d.ProductID >> 8)
if interfaceID, supported := supportedLedgerProductID[productIDMM]; deviceFound || (supported && (interfaceID == d.Interface)) {
return true
}
return false
}
func (admin *LedgerAdminHID) CountDevices() int {
devices := hid.Enumerate(0, 0)
count := 0
for _, d := range devices {
if isLedgerDevice(d) {
count++
}
}
return count
}
func newDevice(dev *hid.Device) *LedgerDeviceHID {
return &LedgerDeviceHID{
device: dev,
readCo: new(sync.Once),
readChannel: make(chan []byte),
}
}
func (admin *LedgerAdminHID) Connect(requiredIndex int) (LedgerDevice, error) {
devices := hid.Enumerate(VendorLedger, 0)
currentIndex := 0
for _, d := range devices {
if isLedgerDevice(d) {
if currentIndex == requiredIndex {
device, err := d.Open()
if err != nil {
return nil, err
}
deviceHID := newDevice(device)
return deviceHID, nil
}
currentIndex++
if currentIndex > requiredIndex {
break
}
}
}
return nil, fmt.Errorf("LedgerHID device (idx %d) not found: device may be locked or in use by another application", requiredIndex)
}
func (ledger *LedgerDeviceHID) write(buffer []byte) (int, error) {
totalBytes := len(buffer)
totalWrittenBytes := 0
for totalBytes > totalWrittenBytes {
writtenBytes, err := ledger.device.Write(buffer)
if err != nil {
return totalWrittenBytes, err
}
buffer = buffer[writtenBytes:]
totalWrittenBytes += writtenBytes
}
return totalWrittenBytes, nil
}
func (ledger *LedgerDeviceHID) Read() <-chan []byte {
ledger.readCo.Do(ledger.initReadChannel)
return ledger.readChannel
}
func (ledger *LedgerDeviceHID) initReadChannel() {
ledger.readChannel = make(chan []byte, 30)
go ledger.readThread()
}
func (ledger *LedgerDeviceHID) readThread() {
defer close(ledger.readChannel)
for {
buffer := make([]byte, PacketSize)
readBytes, err := ledger.device.Read(buffer)
// Check for HID Read Error (May occur even during normal runtime)
if err != nil {
continue
}
// Discard all zero packets from Ledger Nano X on macOS
allZeros := true
for i := 0; i < len(buffer); i++ {
if buffer[i] != 0 {
allZeros = false
break
}
}
// Discard all zero packet
if allZeros {
// HID Returned Empty Packet - Retry Read
continue
}
select {
case ledger.readChannel <- buffer[:readBytes]:
// Send data to UnwrapResponseAPDU
default:
// Possible source of bugs
// Drop a buffer if ledger.readChannel is busy
}
}
}
func (ledger *LedgerDeviceHID) drainRead() {
// Allow time for late packet arrivals (When main program doesn't read enough packets)
<-time.After(50 * time.Millisecond)
for {
select {
case <-ledger.readChannel:
default:
return
}
}
}
func (ledger *LedgerDeviceHID) Exchange(command []byte) ([]byte, error) {
log.Printf("Sending command: %X", command)
// Purge messages that arrived after previous exchange completed
ledger.drainRead()
if len(command) < 5 {
return nil, fmt.Errorf("APDU commands should not be smaller than 5")
}
if (byte)(len(command)-5) != command[4] {
return nil, fmt.Errorf("APDU[data length] mismatch")
}
serializedCommand, err := WrapCommandAPDU(Channel, command, PacketSize)
if err != nil {
return nil, err
}
// Write all the packets
_, err = ledger.write(serializedCommand)
if err != nil {
return nil, err
}
readChannel := ledger.Read()
response, err := UnwrapResponseAPDU(Channel, readChannel, PacketSize)
if err != nil {
return nil, err
}
if len(response) < 2 {
return nil, fmt.Errorf("len(response) < 2")
}
swOffset := len(response) - 2
sw := codec.Uint16(response[swOffset:])
if sw != 0x9000 {
return response[:swOffset], errors.New(ErrorMessage(sw))
}
log.Printf("Received response: %X", response)
return response[:swOffset], nil
}
func (ledger *LedgerDeviceHID) Close() error {
return ledger.device.Close()
}