-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeylessClient.ino
484 lines (423 loc) · 14.3 KB
/
KeylessClient.ino
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#include <ESP8266WiFi.h>
#include <LittleFS.h>
#include <SimpleFTPServer.h>
#include <OPTIGATrustM.h>
// MbedTLS includes
#include "mbedtls/config.h"
#include "mbedtls/asn1write.h"
#include "mbedtls/oid.h"
#include "csr.h"
#define OPTIGA_TRUST_M_RESET_GPIO 12
#define OPTIGA_TRUST_M_POWER_GPIO 13
#define FTP_MODE_GPIO 14
#define KEY_LEN 256 // Bytes (2048 bit)
#define KEY_MAXLEN 300
#define SIG_LEN KEY_LEN
#define HASH_LEN 32 // SHA256
#define UID_LENGTH 27
// WiFi settings
const char* ssid = "xxxxxxxxxx";
const char* password = "xxxxxxxxxx";
// Keyless server
const char* servername = "192.168.1.9";
// CSR/certificate settings
const char* countryName = "DE";
const char* state = "BW";
const char* locality = "Heimsheim";
const char* organizationName = "Geiger";
const char* commonName = "Key";
FtpServer ftpSrv;
WiFiClient client;
uint8_t ftp_mode;
uint8_t request_service_buf[1000];
uint8_t challenge_buf[HASH_LEN + 3];
uint8_t authgrant_buf[1];
uint8_t sig_buf[SIG_LEN + 3];
uint16_t sig_bytes = SIG_LEN;
enum MessageId
{
REQUEST_SERVICE_MSG = 0x40,
SIGNATURE_MSG = 0x41
};
enum RequestServiceResponseCode
{
VALID_CHALLENGE = 0x60,
UNEXPECTED_ERROR_OCCURED_1 = 0x61,
VALUE_ERROR = 0x62,
ERROR_LOADING_CERTIFICATE = 0x63,
INVALID_CA_AUTHENTICATION = 0x64,
INVALID_CERTIFICATE_DATA = 0x65,
EXPIRED_CERTIFICATE = 0x66,
TIME_DELAY_NOT_EXPIRED = 0x67,
REVOKED_CERTIFICATE = 0x68
};
enum SignatureResponseCode
{
AUTHORIZATION_GRANTED = 0x70,
UNEXPECTED_ERROR_OCCURED_2 = 0x71,
SIGNATURE_VERIFICATION_FAILED = 0x72
};
void dumpHex(const void* data, size_t size)
{
char ascii[17];
size_t i, j;
ascii[16] = '\0';
for (i = 0; i < size; ++i)
{
if ((i % 16) == 0)
{
Serial.printf("%06X: ", i);
}
Serial.printf("%02X ", ((unsigned char*)data)[i]);
if ((((unsigned char*)data)[i] >= ' ') && (((unsigned char*)data)[i] <= '~'))
{
ascii[i % 16] = ((unsigned char*)data)[i];
}
else
{
ascii[i % 16] = '.';
}
if ((((i + 1) % 8) == 0) || ((i + 1) == size))
{
Serial.printf(" ");
if (((i + 1) % 16) == 0)
{
Serial.printf("| %s \n", ascii);
}
else if ((i + 1) == size)
{
ascii[(i + 1) % 16] = '\0';
if (((i + 1) % 16) <= 8)
{
Serial.printf(" ");
}
for (j = (i + 1) % 16; j < 16; ++j)
{
Serial.printf(" ");
}
Serial.printf("| %s \n", ascii);
}
}
}
}
void _callback(FtpOperation ftpOperation, unsigned int freeSpace, unsigned int totalSpace)
{
switch (ftpOperation)
{
case FTP_CONNECT:
Serial.println(F("FTP: Connected!"));
break;
case FTP_DISCONNECT:
Serial.println(F("FTP: Disconnected!"));
break;
case FTP_FREE_SPACE_CHANGE:
Serial.printf("FTP: Free space change, free %u of %u!\n", freeSpace, totalSpace);
break;
default:
break;
}
}
void _transferCallback(FtpTransferOperation ftpOperation, const char* name, unsigned int transferredSize)
{
switch (ftpOperation)
{
case FTP_UPLOAD_START:
Serial.println(F("FTP: Upload start!"));
break;
case FTP_UPLOAD:
Serial.printf("FTP: Upload of file %s byte %u\n", name, transferredSize);
break;
case FTP_TRANSFER_STOP:
Serial.println(F("FTP: Finish transfer!"));
break;
case FTP_TRANSFER_ERROR:
Serial.println(F("FTP: Transfer error!"));
break;
default:
break;
}
}
void unlock(void)
{
int32_t ret;
uint16_t challenge_bytes;
unsigned long startTime;
const unsigned long timeout = 200; // 200ms timeout
int index;
// Connect to the server
if (client.connect(servername, 8881))
{
Serial.println("Connected to server.");
// Open the cerificate
File crt_file = LittleFS.open("/key.crt", "r");
if (!crt_file)
{
Serial.println("Failed to open CRT file for reading!");
}
else
{
// Read the cerificate
uint16_t request_service_bytes = crt_file.read(request_service_buf + 3, sizeof(request_service_buf) - 3);
crt_file.close();
// Send binary data to the server
request_service_buf[0] = REQUEST_SERVICE_MSG;
request_service_buf[1] = (uint8_t)(request_service_bytes >> 8);
request_service_buf[2] = (uint8_t)(request_service_bytes >> 0);
client.write(request_service_buf, request_service_bytes + 3);
// Wait for response
startTime = millis();
index = 0;
while (client.connected())
{
if ((millis() - startTime) >= timeout)
{
Serial.println("Error response timeout.");
Serial.println("Disconnected from server.");
client.stop();
return;
}
if (client.available())
{
challenge_buf[index ++] = client.read();
startTime = millis(); // Reset the timeout counter
if ((index >= sizeof(challenge_buf)) || (challenge_buf[0] != ((uint8_t)VALID_CHALLENGE)))
{
// Response received
break;
}
}
}
switch(challenge_buf[0])
{
case (uint8_t)VALID_CHALLENGE:
challenge_bytes = (((uint16_t)challenge_buf[1]) << 8) | ((uint16_t)challenge_buf[2]);
if (challenge_bytes == HASH_LEN)
{
Serial.println("Challenge received.");
//dumpHex(challenge_buf + 3, HASH_LEN);
}
// calculate the RSASSA-PKCS1-v1_5 2048bit signature (takes ~400ms on the ESP8266 with an OPTIGA Trust M)
ret = trustM.calculateSignatureRSA(&challenge_buf[3], HASH_LEN, OPTIGA_KEY_ID_E0FD, sig_buf + 3, sig_bytes);
Serial.println("Signature sent.");
//dumpHex(sig_buf + 3, sig_bytes);
// send signature to the server
sig_buf[0] = SIGNATURE_MSG;
sig_buf[1] = (uint8_t)(sig_bytes >> 8);
sig_buf[2] = (uint8_t)(sig_bytes >> 0);
client.write(sig_buf, sig_bytes + 3);
break;
case (uint8_t)ERROR_LOADING_CERTIFICATE:
Serial.println("Error loading user certificate.");
break;
case (uint8_t)VALUE_ERROR:
Serial.println("Value error.");
break;
case (uint8_t)INVALID_CA_AUTHENTICATION:
Serial.println("Invalid CA authentication.");
break;
case (uint8_t)INVALID_CERTIFICATE_DATA:
Serial.println("Invalid certificate data.");
break;
case (uint8_t)EXPIRED_CERTIFICATE:
Serial.println("Expired certificate.");
break;
case (uint8_t)UNEXPECTED_ERROR_OCCURED_1:
default:
Serial.println("Unexpected error occured.");
break;
}
startTime = millis();
index = 0;
while (client.connected())
{
if ((millis() - startTime) >= timeout)
{
Serial.println("Error response timeout.");
Serial.println("Disconnected from server");
client.stop();
return;
}
if (client.available())
{
authgrant_buf[index ++] = client.read();
startTime = millis(); // Reset the timeout counter
if (index >= sizeof(authgrant_buf))
{
// Response received
break;
}
}
}
switch(authgrant_buf[0])
{
case (uint8_t)AUTHORIZATION_GRANTED:
Serial.println("Authorization granted!");
break;
case (uint8_t)SIGNATURE_VERIFICATION_FAILED:
Serial.println("Authorization not granted!");
break;
case (uint8_t)UNEXPECTED_ERROR_OCCURED_2:
default:
Serial.println("Unexpected error occured. Authorization not granted!");
break;
}
// Disconnect from the server
client.stop();
Serial.println("Disconnected from server");
}
}
else
{
Serial.println("Connection to server failed");
}
}
void enterDeepSleep(void)
{
Serial.println("Entering into deep sleep ...");
// wait 1 sec before going to deep sleep (recover from deep sleep with the reset button)
delay(1000);
// consume less than 20µA in deep sleep
ESP.deepSleep(0);
delay(1000);
}
void setup(void)
{
int32_t ret;
uint8_t *csr_start;
uint16_t csr_len;
File csr_file;
bool csr_file_exists;
// GPIO pin modes
pinMode(OPTIGA_TRUST_M_POWER_GPIO, OUTPUT);
pinMode(OPTIGA_TRUST_M_RESET_GPIO, OUTPUT);
pinMode(FTP_MODE_GPIO, INPUT_PULLUP);
// read jumper setting for FTP mode: 0 - inactive (jumper), 1 - active (no jumper)
ftp_mode = digitalRead(FTP_MODE_GPIO);
// keep OPTIGA Trust M in reset
digitalWrite(OPTIGA_TRUST_M_RESET_GPIO, LOW);
if (ftp_mode == 1)
{
// power off the OPTIGA Trust M
digitalWrite(OPTIGA_TRUST_M_POWER_GPIO, LOW);
}
else
{
// power on the OPTIGA Trust M
digitalWrite(OPTIGA_TRUST_M_POWER_GPIO, HIGH);
// release the OPTIGA Trust M from reset
digitalWrite(OPTIGA_TRUST_M_RESET_GPIO, HIGH);
}
// initialize UART for logging
Serial.begin(115200);
Serial.println();
// start LittleFS before ftp server
if (LittleFS.begin())
{
Serial.println("LittleFS opened!");
// store if CSR file exists
csr_file_exists = LittleFS.exists("/key.csr");
if ((ftp_mode == 1) || (csr_file_exists == false))
{
Serial.println("Slow Wifi init.");
// slow and bad for flash life, only CSR does not exist yet (initially) or when in FTP mode
WiFi.persistent(true);
WiFi.begin(ssid, password);
}
else
{
Serial.println("Fast Wifi init.");
// fast, use last stored wifi settings
WiFi.begin();
}
while (WiFi.status() != WL_CONNECTED)
{
delay(100);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (ftp_mode == 1)
{
// initialize FTP client
Serial.println("Starting FTP client ...");
ftpSrv.setCallback(_callback);
ftpSrv.setTransferCallback(_transferCallback);
ftpSrv.begin("user","password"); //username, password for ftp. (default 21, 50009 for PASV)
}
else
{
Serial.print("Begin to trust ... ");
ret = trustM.begin();
if (ret != 0)
{
Serial.println("Failed");
enterDeepSleep();
}
else
{
Serial.println("Ok");
}
Serial.print("Limiting Current consumption (12mA - means max what a GPIO of the ESP8266 can deliver) ... ");
ret = trustM.setCurrentLimit(12);
if (ret != 0)
{
Serial.println("Failed");
enterDeepSleep();
}
else
{
Serial.println("Ok");
}
if (csr_file_exists == false)
{
Serial.print("Generate Cerificate Signing Request ... ");
ret = generateCertificateSigningRequestRSA2048((char*)countryName, (char*)state, (char*)locality, (char*)organizationName, NULL, (char*)commonName, &csr_start, &csr_len);
if (ret != 0)
{
Serial.println("Failed");
enterDeepSleep();
}
else
{
Serial.println("Ok");
}
Serial.println("Cerificate Signing Request:");
dumpHex(csr_start, csr_len);
csr_file = LittleFS.open("/key.csr", "w");
if (!csr_file)
{
Serial.println("Failed to open CSR file for writing!");
enterDeepSleep();
}
if (csr_file.write(csr_start, csr_len) != csr_len)
{
Serial.println("Failed to write data to CSR file!");
csr_file.close();
enterDeepSleep();
}
csr_file.close();
}
else
{
Serial.println("CSR file already created and present.");
}
}
}
if (ftp_mode == 0)
{
unlock();
// power down the OPTIGA Trust M chip
digitalWrite(OPTIGA_TRUST_M_POWER_GPIO, LOW);
enterDeepSleep();
}
}
void loop(void)
{
if (ftp_mode == 1)
{
ftpSrv.handleFTP();
}
}