forked from arduino-libraries/ArduinoBearSSL
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBearSSLClient.cpp
496 lines (394 loc) · 10.7 KB
/
BearSSLClient.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
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
485
486
487
488
489
490
491
492
493
494
495
/*
* Copyright (c) 2018 Arduino SA. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "ArduinoBearSSL.h"
#ifndef ARDUINO_DISABLE_ECCX08
#include <ArduinoECCX08.h>
#endif
#include "BearSSLTrustAnchors.h"
#include "utility/eccX08_asn1.h"
#include "BearSSLClient.h"
BearSSLClient::BearSSLClient(Client& client) :
BearSSLClient(&client, TAs, TAs_NUM)
{
}
BearSSLClient::BearSSLClient(Client& client, const br_x509_trust_anchor* myTAs, int myNumTAs)
: BearSSLClient(&client, myTAs, myNumTAs)
{
}
BearSSLClient::BearSSLClient(Client* client, const br_x509_trust_anchor* myTAs, int myNumTAs) :
_client(client),
_TAs(myTAs),
_numTAs(myNumTAs),
_noSNI(false),
_ecChainLen(0)
{
#ifndef ARDUINO_DISABLE_ECCX08
_ecVrfy = eccX08_vrfy_asn1;
_ecSign = eccX08_sign_asn1;
#else
_ecVrfy = br_ecdsa_vrfy_asn1_get_default();
_ecSign = br_ecdsa_sign_asn1_get_default();
#endif
_ecKey.curve = 0;
_ecKey.x = NULL;
_ecKey.xlen = 0;
for (size_t i = 0; i < BEAR_SSL_CLIENT_CHAIN_SIZE; i++) {
_ecCert[i].data = NULL;
_ecCert[i].data_len = 0;
}
_ecCertDynamic = false;
}
BearSSLClient::~BearSSLClient()
{
if (_ecCertDynamic && _ecCert[0].data) {
free(_ecCert[0].data);
_ecCert[0].data = NULL;
}
}
int BearSSLClient::connect(IPAddress ip, uint16_t port)
{
if (!_client->connect(ip, port)) {
return 0;
}
return connectSSL(NULL);
}
int BearSSLClient::connect(const char* host, uint16_t port)
{
if (!_client->connect(host, port)) {
return 0;
}
return connectSSL(_noSNI ? NULL : host);
}
size_t BearSSLClient::write(uint8_t b)
{
return write(&b, sizeof(b));
}
size_t BearSSLClient::write(const uint8_t *buf, size_t size)
{
size_t written = 0;
while (written < size) {
int result = br_sslio_write(&_ioc, buf, size);
if (result < 0) {
break;
}
buf += result;
written += result;
}
if (written == size && br_sslio_flush(&_ioc) < 0) {
return 0;
}
return written;
}
int BearSSLClient::available()
{
int available = br_sslio_read_available(&_ioc);
if (available < 0) {
available = 0;
}
return available;
}
int BearSSLClient::read()
{
byte b;
if (read(&b, sizeof(b)) == sizeof(b)) {
return b;
}
return -1;
}
int BearSSLClient::read(uint8_t *buf, size_t size)
{
return br_sslio_read(&_ioc, buf, size);
}
int BearSSLClient::peek()
{
byte b;
if (br_sslio_peek(&_ioc, &b, sizeof(b)) == sizeof(b)) {
return b;
}
return -1;
}
void BearSSLClient::flush()
{
br_sslio_flush(&_ioc);
_client->flush();
}
void BearSSLClient::stop()
{
if (_client->connected()) {
if ((br_ssl_engine_current_state(&_sc.eng) & BR_SSL_CLOSED) == 0) {
br_sslio_close(&_ioc);
}
_client->stop();
}
}
uint8_t BearSSLClient::connected()
{
if (!_client->connected()) {
return 0;
}
unsigned state = br_ssl_engine_current_state(&_sc.eng);
if (state == BR_SSL_CLOSED) {
return 0;
}
return 1;
}
BearSSLClient::operator bool()
{
return (*_client);
}
void BearSSLClient::setInsecure(SNI insecure)
{
switch (insecure) {
case SNI::Insecure : _noSNI = true; break;
default: _noSNI = false;
}
}
void BearSSLClient::setEccVrfy(br_ecdsa_vrfy vrfy)
{
_ecVrfy = vrfy;
}
void BearSSLClient::setEccSign(br_ecdsa_sign sign)
{
_ecSign = sign;
}
void BearSSLClient::setEccCert(br_x509_certificate cert)
{
_ecCert[0] = cert;
_ecChainLen = 1;
}
void BearSSLClient::setEccChain(br_x509_certificate* chain, size_t chainLen)
{
if (chainLen > BEAR_SSL_CLIENT_CHAIN_SIZE)
return;
for (size_t i = 0; i < chainLen; i++) {
_ecCert[i] = chain[i];
}
_ecChainLen = chainLen;
}
void BearSSLClient::setEccSlot(int ecc508KeySlot, const byte cert[], int certLength)
{
// HACK: put the key slot info. in the br_ec_private_key structure
_ecKey.curve = 23;
_ecKey.x = (unsigned char*)ecc508KeySlot;
_ecKey.xlen = 32;
_ecCert[0].data = (unsigned char*)cert;
_ecCert[0].data_len = certLength;
_ecChainLen = 1;
_ecCertDynamic = false;
#ifndef ARDUINO_DISABLE_ECCX08
_ecVrfy = eccX08_vrfy_asn1;
_ecSign = eccX08_sign_asn1;
#else
_ecVrfy = br_ecdsa_vrfy_asn1_get_default();
_ecSign = br_ecdsa_sign_asn1_get_default();
#endif
}
void BearSSLClient::setEccSlot(int ecc508KeySlot, const char cert[])
{
// try to decode the cert
br_pem_decoder_context pemDecoder;
size_t certLen = strlen(cert);
// free old data
if (_ecCertDynamic && _ecCert[0].data) {
free(_ecCert[0].data);
_ecCert[0].data = NULL;
}
// assume the decoded cert is 3/4 the length of the input
_ecCert[0].data = (unsigned char*)malloc(((certLen * 3) + 3) / 4);
_ecCert[0].data_len = 0;
_ecChainLen = 1;
br_pem_decoder_init(&pemDecoder);
while (certLen) {
size_t len = br_pem_decoder_push(&pemDecoder, cert, certLen);
cert += len;
certLen -= len;
switch (br_pem_decoder_event(&pemDecoder)) {
case BR_PEM_BEGIN_OBJ:
br_pem_decoder_setdest(&pemDecoder, BearSSLClient::clientAppendCert, this);
break;
case BR_PEM_END_OBJ:
if (_ecCert[0].data_len) {
// done
setEccSlot(ecc508KeySlot, _ecCert[0].data, _ecCert[0].data_len);
_ecCertDynamic = true;
return;
}
break;
case BR_PEM_ERROR:
// failure
free(_ecCert[0].data);
setEccSlot(ecc508KeySlot, NULL, 0);
return;
}
}
}
void BearSSLClient::setEccCertParent(const char cert[])
{
// try to decode the cert
br_pem_decoder_context pemDecoder;
size_t certLen = strlen(cert);
// free old data
if (_ecCertDynamic && _ecCert[1].data) {
free(_ecCert[1].data);
_ecCert[1].data = NULL;
}
// assume the decoded cert is 3/4 the length of the input
_ecCert[1].data = (unsigned char*)malloc(((certLen * 3) + 3) / 4);
_ecCert[1].data_len = 0;
_ecChainLen = 2;
br_pem_decoder_init(&pemDecoder);
while (certLen) {
size_t len = br_pem_decoder_push(&pemDecoder, cert, certLen);
cert += len;
certLen -= len;
switch (br_pem_decoder_event(&pemDecoder)) {
case BR_PEM_BEGIN_OBJ:
br_pem_decoder_setdest(&pemDecoder, BearSSLClient::parentAppendCert, this);
break;
case BR_PEM_END_OBJ:
if (_ecCert[1].data_len) {
// done
_ecCertDynamic = true;
return;
}
break;
case BR_PEM_ERROR:
// failure
free(_ecCert[1].data);
return;
}
}
}
int BearSSLClient::errorCode()
{
return br_ssl_engine_last_error(&_sc.eng);
}
int BearSSLClient::connectSSL(const char* host)
{
// initialize client context with all algorithms and hardcoded trust anchors
br_ssl_client_init_full(&_sc, &_xc, _TAs, _numTAs);
br_ssl_engine_set_buffers_bidi(&_sc.eng, _ibuf, sizeof(_ibuf), _obuf, sizeof(_obuf));
// inject entropy in engine
unsigned char entropy[32];
#ifndef ARDUINO_DISABLE_ECCX08
if (!ECCX08.begin() || !ECCX08.locked() || !ECCX08.random(entropy, sizeof(entropy))) {
#endif
// no ECCX08 or random failed, fallback to pseudo random
for (size_t i = 0; i < sizeof(entropy); i++) {
entropy[i] = random(0, 255);
}
#ifndef ARDUINO_DISABLE_ECCX08
}
#endif
br_ssl_engine_inject_entropy(&_sc.eng, entropy, sizeof(entropy));
// add custom ECDSA vfry and EC sign
br_ssl_engine_set_ecdsa(&_sc.eng, _ecVrfy);
br_x509_minimal_set_ecdsa(&_xc, br_ssl_engine_get_ec(&_sc.eng), br_ssl_engine_get_ecdsa(&_sc.eng));
// enable client auth
if (_ecCert[0].data_len) {
br_ssl_client_set_single_ec(&_sc, _ecCert, _ecChainLen, &_ecKey, BR_KEYTYPE_KEYX | BR_KEYTYPE_SIGN, BR_KEYTYPE_EC, br_ec_get_default(), _ecSign);
}
// set the hostname used for SNI
br_ssl_client_reset(&_sc, host, 0);
// get the current time and set it for X.509 validation
uint32_t now = ArduinoBearSSL.getTime();
uint32_t days = now / 86400 + 719528;
uint32_t sec = now % 86400;
br_x509_minimal_set_time(&_xc, days, sec);
// use our own socket I/O operations
br_sslio_init(&_ioc, &_sc.eng, BearSSLClient::clientRead, _client, BearSSLClient::clientWrite, _client);
br_sslio_flush(&_ioc);
while (1) {
unsigned state = br_ssl_engine_current_state(&_sc.eng);
if (state & BR_SSL_SENDAPP) {
break;
} else if (state & BR_SSL_CLOSED) {
return 0;
}
}
return 1;
}
// #define DEBUGSERIAL Serial
int BearSSLClient::clientRead(void *ctx, unsigned char *buf, size_t len)
{
Client* c = (Client*)ctx;
if (!c->connected()) {
return -1;
}
int result = c->read(buf, len);
if (result == -1) {
return 0;
}
#ifdef DEBUGSERIAL
DEBUGSERIAL.print("BearSSLClient::clientRead - ");
DEBUGSERIAL.print(result);
DEBUGSERIAL.print(" - ");
for (size_t i = 0; i < result; i++) {
byte b = buf[i];
if (b < 16) {
DEBUGSERIAL.print("0");
}
DEBUGSERIAL.print(b, HEX);
}
DEBUGSERIAL.println();
#endif
return result;
}
int BearSSLClient::clientWrite(void *ctx, const unsigned char *buf, size_t len)
{
Client* c = (Client*)ctx;
#ifdef DEBUGSERIAL
DEBUGSERIAL.print("BearSSLClient::clientWrite - ");
DEBUGSERIAL.print(len);
DEBUGSERIAL.print(" - ");
for (size_t i = 0; i < len; i++) {
byte b = buf[i];
if (b < 16) {
DEBUGSERIAL.print("0");
}
DEBUGSERIAL.print(b, HEX);
}
DEBUGSERIAL.println();
#endif
if (!c->connected()) {
return -1;
}
int result = c->write(buf, len);
if (result == 0) {
return -1;
}
return result;
}
void BearSSLClient::clientAppendCert(void *ctx, const void *data, size_t len)
{
BearSSLClient* c = (BearSSLClient*)ctx;
memcpy(&c->_ecCert[0].data[c->_ecCert[0].data_len], data, len);
c->_ecCert[0].data_len += len;
}
void BearSSLClient::parentAppendCert(void *ctx, const void *data, size_t len)
{
BearSSLClient* c = (BearSSLClient*)ctx;
memcpy(&c->_ecCert[1].data[c->_ecCert[1].data_len], data, len);
c->_ecCert[1].data_len += len;
}