-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathYubiKeyHID.cpp
185 lines (153 loc) · 4.97 KB
/
YubiKeyHID.cpp
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
#include <limits.h>
#include <usbhid.h>
#include "global.h"
#include "led.h"
#include "eeprom.h"
#include "YubiKeyHID.h"
/*
SL, StickLock
provides an electronic lock with USB security tokens as keys.
Copyright (C) 2019 richard.prinz@min.at
COMMERCIAL USAGE PROHIBITED!
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program (see file gpl-3.0.txt).
If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CONFIG
uint8_t YubiKeyHID::Init(uint8_t parent, uint8_t port, bool lowspeed) {
#ifdef ENABLE_UI
Serial.println(F("\r\nUSB device connected ..."));
#endif
uint8_t rcode = HIDUniversal::Init(parent, port, lowspeed);
if(rcode != 0)
return rcode;
// vendor and product id
#ifdef ENABLE_UI
E_Notify(PSTR("VID: 0x"), 0x80);
PrintHex<uint16_t > (VID, 0x80);
E_Notify(PSTR("\r\nPID: 0x"), 0x80);
PrintHex<uint16_t > (PID, 0x80);
E_Notify(PSTR("\r\n"), 0x80);
#endif
DeviceVID = VID;
DevicePID = PID;
// Get device serial number.
const uint8_t constBufSize = sizeof (USB_DEVICE_DESCRIPTOR);
uint8_t buf[constBufSize];
USB_DEVICE_DESCRIPTOR * udd = reinterpret_cast<USB_DEVICE_DESCRIPTOR*>(buf);
// Free prev serial buffer.
if(DeviceSerial != NULL) {
free(DeviceSerial);
DeviceSerial = NULL;
}
// Get device descriptor.
rcode = pUsb->getDevDescr(bAddress, 0, sizeof(USB_DEVICE_DESCRIPTOR), (uint8_t*)buf);
if(rcode)
goto NO_SERIAL_NUMBER;
serialNumberIndex = udd->iSerialNumber;
if(!serialNumberIndex)
goto NO_SERIAL_NUMBER;
uint8_t bufx[255];
rcode = pUsb->getStrDescr(bAddress, 0, sizeof(bufx), serialNumberIndex, 0, bufx);
if(rcode)
goto NO_SERIAL_NUMBER;
// Copy serial num to buffer.
DeviceSerialLength = bufx[0] - 2;
DeviceSerial = (uint8_t *)malloc(DeviceSerialLength);
memcpy(DeviceSerial, &bufx[2], DeviceSerialLength);
#ifdef ENABLE_UI
E_Notify(PSTR("SER: "), 0x80);
Serial.print(DeviceSerialLength);
E_Notify(PSTR(" / "), 0x80);
for(int i=0; i<DeviceSerialLength; i++) {
PrintHex<uint8_t> (DeviceSerial[i], 0x80);
E_Notify(PSTR(" "), 0x80);
}
E_Notify(PSTR("\r\n"), 0x80);
#endif
goto CHECK_DEVICE;
NO_SERIAL_NUMBER:
serialNumberIndex = 0;
DeviceSerialLength = 0;
// Free previous serial number buffer.
if(DeviceSerial != NULL) {
free(DeviceSerial);
DeviceSerial = NULL;
}
#ifdef ENABLE_UI
E_Notify(PSTR("SER: no serial number\r\n"), 0x80);
#endif
CHECK_DEVICE:
#ifndef DISABLE_SUPPORTED_DEVICE_CHECKS
// Check for supported devices.
DeviceSupported = false;
Device_t device;
uint8_t deviceCount = ReadDeviceCount();
uint16_t addr = ReadDeviceStart();
addr += sizeof(uint8_t);
for(int d=0; d<deviceCount; d++) {
uint16_t deviceLen = ReadDevice(addr, &device);
if(device.pid == DevicePID && device.vid == DeviceVID) {
DeviceSupported = true;
break;
}
InitDevice(&device);
addr += deviceLen;
}
if(DeviceSupported) {
#ifdef ENABLE_UI
Serial.print(F("Supported device: "));
Serial.println(device.name);
#endif
InitDevice(&device);
// green LED 4 times
Lock.DeviceSupported();
// immediately after device connect check for serial number only
// keys which dont need any other user intervention (like pushing
// a token button)
#ifdef ENABLE_UI
Serial.println(F("Check Serial Only keys"));
#endif
Lock.CheckInput(0, NULL,
DeviceSerialLength, DeviceSerial);
// Power Off 10 seconds after last activity
PowerOff.Blink(1000, POWER_OFF_TIMEOUT, 1, LOW);
}
else {
#ifdef ENABLE_UI
Serial.println(F("Unsupported device"));
#endif
// red LED on continuous
RedLed.On();
}
#else
#ifdef ENABLE_UI
Serial.print(F("Supported device: any device accepted"));
#endif
// green LED 4 times
Lock.DeviceSupported();
// immediately after device connect check for serial number only
// keys which dont need any other user intervention (like pushing
// a token button)
#ifdef ENABLE_UI
Serial.println(F("Check Serial Only keys"));
#endif
Lock.CheckInput(0, NULL,
DeviceSerialLength, DeviceSerial);
// Power Off 10 seconds after last activity
PowerOff.Blink(1000, POWER_OFF_TIMEOUT, 1, LOW);
#endif
#ifdef ENABLE_UI
Serial.println();
#endif
return 0;
}
#endif