-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathSSLClient.cpp
413 lines (354 loc) · 10.1 KB
/
SSLClient.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
/*
SSLClient.cpp - Base class that provides Client SSL to ESP32
Additions (c) 2011 Adrian McEwen. All right reserved.
Additions Copyright (C) 2017 Evandro Luis Copercini.
Additions Copyright (C) 2019 Vadim Govorovski.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "SSLClient.h"
#include "SE05X.h"
#include "mbedtls/pem.h"
#include <errno.h>
#undef connect
#undef write
#undef read
SSLClient::SSLClient()
{
_connected = false;
sslclient = new sslclient_context;
_timeout = 1000;
_use_insecure = false;
_CA_cert = NULL;
_CA_path = NULL;
_cert = NULL;
_private_key = NULL;
_pskIdent = NULL;
_psKey = NULL;
}
SSLClient::SSLClient(Client* client)
{
_connected = false;
sslclient = new sslclient_context;
_timeout = 1000;
_use_insecure = false;
_CA_cert = NULL;
_CA_path = "/mbedtls";
_cert = NULL;
_private_key = NULL;
_pskIdent = NULL;
_psKey = NULL;
ssl_init(sslclient, client, _CA_path);
sslclient->handshake_timeout = 5000;
}
SSLClient::SSLClient(Client* client, String ca_path)
{
_connected = false;
sslclient = new sslclient_context;
_timeout = 1000;
_use_insecure = false;
_CA_cert = NULL;
_CA_path = ca_path.c_str();
_cert = NULL;
_private_key = NULL;
_pskIdent = NULL;
_psKey = NULL;
ssl_init(sslclient, client, _CA_path);
sslclient->handshake_timeout = 5000;
}
SSLClient::~SSLClient()
{
stop();
delete sslclient->client;
delete sslclient;
}
void SSLClient::setClient(Client& client)
{
ssl_init(sslclient, &client, _CA_path);
sslclient->handshake_timeout = 5000;
}
void SSLClient::stop()
{
if (sslclient->client >= 0) {
//sslclient->client->stop();
_connected = false;
_peek = -1;
}
stop_ssl_socket(sslclient, _CA_cert, _cert, _private_key);
}
int SSLClient::connect(IPAddress ip, uint16_t port)
{
if (_pskIdent && _psKey)
return connect(ip, port, _pskIdent, _psKey);
return connect(ip, port, _CA_cert, _CA_path, _cert, _private_key);
}
int SSLClient::connect(IPAddress ip, uint16_t port, int32_t timeout){
_timeout = timeout;
return connect(ip, port);
}
int SSLClient::connect(const char *host, uint16_t port)
{
if (_pskIdent && _psKey)
return connect(host, port, _pskIdent, _psKey);
return connect(host, port, _CA_cert, _CA_path, _cert, _private_key);
}
int SSLClient::connect(const char *host, uint16_t port, int32_t timeout){
_timeout = timeout;
return connect(host, port);
}
int SSLClient::connect(IPAddress ip, uint16_t port, const char *_CA_cert, const char *_CA_path, const char *_cert, const char *_private_key)
{
return connect(ip.toString().c_str(), port, _CA_cert, _CA_path, _cert, _private_key);
}
int SSLClient::connect(const char *host, uint16_t port, const char *_CA_cert, const char *_CA_path, const char *_cert, const char *_private_key)
{
log_d("Connecting to %s:%d", host, port);
int ret = start_ssl_client(sslclient, host, port, _timeout, _CA_cert, _CA_path, _cert, _private_key, NULL, NULL, _use_insecure);
_lastError = ret;
if (ret < 0) {
log_e("start_ssl_client: %d", ret);
stop();
return 0;
}
log_i("SSL connection established");
_connected = true;
return 1;
}
int SSLClient::connect(IPAddress ip, uint16_t port, const char *pskIdent, const char *psKey) {
return connect(ip.toString().c_str(), port,_pskIdent, _psKey);
}
int SSLClient::connect(const char *host, uint16_t port, const char *pskIdent, const char *psKey) {
log_v("start_ssl_client with PSK");
int ret = start_ssl_client(sslclient, host, port, _timeout, NULL, NULL, NULL, NULL, _pskIdent, _psKey, _use_insecure);
_lastError = ret;
if (ret < 0) {
log_e("start_ssl_client: %d", ret);
stop();
return 0;
}
_connected = true;
return 1;
}
int SSLClient::peek(){
if(_peek >= 0){
return _peek;
}
_peek = timedRead();
return _peek;
}
size_t SSLClient::write(uint8_t data)
{
return write(&data, 1);
}
int SSLClient::read()
{
uint8_t data = -1;
int res = read(&data, 1);
if (res < 0) {
return res;
}
return data;
}
size_t SSLClient::write(const uint8_t *buf, size_t size)
{
if (!_connected) {
return 0;
}
int res = send_ssl_data(sslclient, buf, size);
if (res < 0) {
stop();
res = 0;
}
return res;
}
int SSLClient::read(uint8_t *buf, size_t size)
{
int peeked = 0;
int avail = available();
if ((!buf && size) || avail <= 0) {
return -1;
}
if(!size){
return 0;
}
if(_peek >= 0){
buf[0] = _peek;
_peek = -1;
size--;
avail--;
if(!size || !avail){
return 1;
}
buf++;
peeked = 1;
}
int res = get_ssl_receive(sslclient, buf, size);
if (res < 0) {
stop();
return peeked?peeked:res;
}
return res + peeked;
}
int SSLClient::available()
{
int peeked = (_peek >= 0);
if (!_connected) {
return peeked;
}
int res = data_to_read(sslclient);
if (res < 0) {
stop();
return peeked?peeked:res;
}
return res+peeked;
}
uint8_t SSLClient::connected()
{
uint8_t dummy = 0;
read(&dummy, 0);
return _connected;
}
void SSLClient::setInsecure()
{
_CA_cert = NULL;
_cert = NULL;
_private_key = NULL;
_pskIdent = NULL;
_psKey = NULL;
_use_insecure = true;
}
void SSLClient::setCACert (const char *rootCA)
{
log_d("Set root CA");
_CA_cert = rootCA;
}
void SSLClient::setCAPath (const char *rootCAPath)
{
log_d("Set root CA Path");
_CA_path = rootCAPath;
}
void SSLClient::setCertificate (const char *client_ca)
{
log_d("Set client CA");
_cert = client_ca;
}
void SSLClient::setPrivateKey (const char *private_key)
{
log_d("Set client PK");
_private_key = private_key;
}
void SSLClient::setPreSharedKey(const char *pskIdent, const char *psKey) {
log_d("Set PSK");
_pskIdent = pskIdent;
_psKey = psKey;
}
bool SSLClient::verify(const char* fp, const char* domain_name)
{
if (!sslclient)
return false;
return verify_ssl_fingerprint(sslclient, fp, domain_name);
}
char *SSLClient::_streamLoad(Stream& stream, size_t size) {
char *dest = (char*)malloc(size+1);
if (!dest) {
return nullptr;
}
if (size != stream.readBytes(dest, size)) {
free(dest);
dest = nullptr;
return nullptr;
}
dest[size] = '\0';
return dest;
}
bool SSLClient::loadCACert(Stream& stream, size_t size) {
if (_CA_cert != NULL) free(const_cast<char*>(_CA_cert));
char *dest = _streamLoad(stream, size);
bool ret = false;
if (dest) {
setCACert(dest);
ret = true;
}
return ret;
}
bool SSLClient::loadCertificate(Stream& stream, size_t size) {
if (_cert != NULL) free(const_cast<char*>(_cert));
char *dest = _streamLoad(stream, size);
bool ret = false;
if (dest) {
setCertificate(dest);
ret = true;
}
return ret;
}
bool SSLClient::loadPrivateKey(Stream& stream, size_t size) {
if (_private_key != NULL) free(const_cast<char*>(_private_key));
char *dest = _streamLoad(stream, size);
bool ret = false;
if (dest) {
setPrivateKey(dest);
ret = true;
}
return ret;
}
void SSLClient::setEccSlot(int KeySlot, const byte cert[], int certLen) {
unsigned char buf[1024];
size_t olen;
int ret;
if ((ret = mbedtls_pem_write_buffer("-----BEGIN CERTIFICATE-----\n",
"-----END CERTIFICATE-----\n",
cert, certLen,
&buf[0], sizeof(buf), &olen)) != 0) {
log_e("setEccSlot: client certificate error");
}
char *dest = (char*)malloc(olen+1);
memcpy(dest,buf,olen);
setCertificate(dest);
log_d("%s", _cert);
uint8_t key[121] = {
0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0xA5, 0xA6, 0xB5,
0xB6, 0xA5, 0xA6, 0xB5, 0xB6, 0x10, 0x00, 0xA0, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
0x03, 0x01, 0x07, 0xA1, 0x44, 0x03, 0x42, 0x00, 0x04, 0xC1, 0x84, 0x04, 0x32, 0x06, 0x0A, 0x8C,
0x0C, 0xDB, 0xB4, 0x61, 0x40, 0x98, 0xD4, 0xF9, 0x41, 0xB4, 0x1F, 0x99, 0x33, 0x6F, 0xCF, 0xE0,
0x53, 0x1B, 0xAF, 0xD5, 0xF9, 0x8B, 0xEC, 0x27, 0x90, 0x40, 0xA9, 0xE0, 0x86, 0xFC, 0x27, 0xBD,
0xD8, 0x20, 0x53, 0x01, 0x7B, 0xF6, 0xBC, 0x59, 0x0F, 0xE4, 0x93, 0xE6, 0xE3, 0x05, 0xBB, 0xBE,
0x83, 0xA3, 0x5E, 0x5B, 0x64, 0x1D, 0x29, 0xED, 0x85
};
key[28] = KeySlot;
if ((ret = mbedtls_pem_write_buffer("-----BEGIN EC PRIVATE KEY-----\n",
"-----END EC PRIVATE KEY-----\n",
(const unsigned char *)key, 121,
&buf[0], sizeof(buf), &olen)) != 0) {
log_e("setEccSlot: client key error");
}
dest = (char*)malloc(olen+1);
memcpy(dest,buf,olen);
setPrivateKey(dest);
log_d("%s", _private_key);
}
int SSLClient::lastError(char *buf, const size_t size)
{
if (!_lastError) {
return 0;
}
mbedtls_strerror(_lastError, buf, size);
return _lastError;
}
void SSLClient::setHandshakeTimeout(unsigned long handshake_timeout)
{
sslclient->handshake_timeout = handshake_timeout * 1000;
}
void SSLClient::setTimeout(unsigned long seconds)
{
_timeout = seconds * 1000;
}