forked from glowfishAPI/httpsclient-particle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpsclient-particle.cpp
More file actions
546 lines (495 loc) · 15.6 KB
/
httpsclient-particle.cpp
File metadata and controls
546 lines (495 loc) · 15.6 KB
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/*
This file follows client.c from matrixSSL-3.7.2b closely to make a post/
get using https. It is also inspired by the httpclient library available
on github for the particle photon
(https://github.com/nmattisson/HttpClient)
*/
// WARNING WARNING WARNING:
// This is for test purposes only, the RSA keys included in header files are SAMPLE
// If you use these keys in production, an attacker can de-crypt your data, and look
// at it, and you might as well have been using http without https.
#include "httpsclient-particle.h"
#ifdef ID_RSA
#include "2048_RSA.h"
#include "2048_RSA_KEY.h"
#include "ALL_RSA_CAS.h"
#endif
uint32_t freemem;
int32 rc, CAstreamLen, i;
sslKeys_t *keys;
sslSessionId_t *sid;
//struct g_sslstats stats;
unsigned char *CAstream;
tlsExtension_t *extension;
int32 len, sessionFlag, extLen;
ssl_t *ssl;
unsigned char *g_buf, *ext;
sslSessOpts_t options;
const int g_key_len = 2048;
uint32 g_cipher[1] = {60};
const int g_ciphers = 1;
const uint16_t TIMEOUT = 1000; // Allow maximum 1s between data packets(?)
unsigned char * g_httpRequestHdr;
const char end_header[] = "\r\n\r\n";
// TODO: Complete HACK, is it necessary to know how many bytes to expect from
// the server?
uint32 g_bytes_requested = 100000;
#ifdef ID_RSA
int32 loadRsaKeys(uint32 key_len, sslKeys_t *keys,
unsigned char *CAstream, int32 CAstreamLen) {
int32 rc;
if (g_https_trace) Serial.println("Using 2048 bit RSA private key");
rc = matrixSslLoadRsaKeysMem(keys, RSA2048, sizeof(RSA2048), RSA2048KEY,
sizeof(RSA2048KEY), CAstream, CAstreamLen);
// if (key_len == 1024) {
// #ifdef LOGGING_DEBUG
// Serial.println("Using 1024 bit RSA private key");
// #endif
// rc = matrixSslLoadRsaKeysMem(keys, RSA1024, sizeof(RSA1024),
// RSA1024KEY, sizeof(RSA1024KEY), CAstream, CAstreamLen);
// } else if (key_len == 2048) {
// #ifdef LOGGING_DEBUG
// Serial.println("Using 2048 bit RSA private key");
// #endif
// rc = matrixSslLoadRsaKeysMem(keys, RSA2048, sizeof(RSA2048),
// RSA2048KEY, sizeof(RSA2048KEY), CAstream, CAstreamLen);
// } else if (key_len == 4096) {
// #ifdef LOGGING_DEBUG
// Serial.println("Using 4096 bit RSA private key");
// #endif
// rc = matrixSslLoadRsaKeysMem(keys, RSA4096, sizeof(RSA4096),
// RSA4096KEY, sizeof(RSA4096KEY), CAstream, CAstreamLen);
// } else {
// rc = -1;
// psAssert((key_len == 1024) || (key_len == 2048) || (key_len == 4096));
// }
if (rc < 0) {
if (g_https_trace) _psTrace("No certificate material loaded. Exiting");
if (CAstream) {
psFree(CAstream, NULL);
}
matrixSslDeleteKeys(keys);
matrixSslClose();
}
return rc;
}
#endif
const char * g_host;
const char * g_path;
int httpsclientSetup(const char * g_ip_str, const char * host,
const char * path) {
int rc;
g_host = host;
g_path = path;
if ((rc = matrixSslOpen()) < 0) {
if (g_https_trace) _psTrace("MatrixSSL library init failure.");
return rc;
}
if ((rc = matrixSslNewKeys(&keys, NULL)) < 0) {
if (g_https_trace) Serial.println("MatrixSSL library key init failure.");
return HTTPS_ERROR;
}
#ifndef USE_ONLY_PSK_CIPHER_SUITE
/*
In-memory based keys
Build the CA list first for potential client auth usage
*/
CAstreamLen = 0;
#ifdef USE_RSA_CIPHER_SUITE
CAstreamLen += sizeof(RSACAS);
#ifdef USE_ECC_CIPHER_SUITE
CAstreamLen += sizeof(ECDHRSACAS);
#endif
#endif
#ifdef USE_ECC_CIPHER_SUITE
CAstreamLen += sizeof(ECCAS);
#endif
if (CAstreamLen > 0) {
CAstream = (unsigned char *) psMalloc(NULL, CAstreamLen);
} else {
CAstream = NULL;
}
CAstreamLen = 0;
#ifdef USE_RSA_CIPHER_SUITE
memcpy(CAstream, RSACAS, sizeof(RSACAS));
CAstreamLen += sizeof(RSACAS);
#ifdef USE_ECC_CIPHER_SUITE
memcpy(CAstream + CAstreamLen, ECDHRSACAS, sizeof(ECDHRSACAS));
CAstreamLen += sizeof(ECDHRSACAS);
#endif
#endif
#ifdef USE_ECC_CIPHER_SUITE
memcpy(CAstream + CAstreamLen, ECCAS, sizeof(ECCAS));
CAstreamLen += sizeof(ECCAS);
#endif
#ifdef ID_RSA
rc = loadRsaKeys(g_key_len, keys, CAstream, CAstreamLen);
if (rc < 0) {
if (g_https_trace) {
Serial.print("Keys didn't load!: loadRsaKeys returned: ");
Serial.println(rc);
}
return rc;
}
if (g_https_trace) _psTrace("Keys Loaded");
#endif
if (CAstream) psFree(CAstream, NULL);
#endif /* USE_ONLY_PSK_CIPHER_SUITE */
matrixSslNewSessionId(&sid, NULL);
if (g_https_trace) Serial.println("New Session key!: ");
sessionFlag = SSL_FLAGS_TLS_1_2;
}
static int32 httpWriteRequest() {
unsigned char *buf;
int32 available, requested;
requested = strlen((char *)g_httpRequestHdr) + strlen(g_path) + 1;
if ((available = matrixSslGetWritebuf(ssl, &buf, requested)) < 0) {
return PS_MEM_FAIL;
}
requested = min(requested, available);
snprintf((char *)buf, requested, (char *)g_httpRequestHdr, g_path);
if (matrixSslEncodeWritebuf(ssl, strlen((char *)buf)) < 0) {
return PS_MEM_FAIL;
}
return MATRIXSSL_REQUEST_SEND;
}
static int32 certCb(ssl_t *ssl, psX509Cert_t *cert, int32 alert) {
#ifndef USE_ONLY_PSK_CIPHER_SUITE
if (g_https_trace) {
Serial.print("certCb invoked: "); Serial.println(alert);
}
/* Did we even find a CA that issued the certificate? */
if (alert == SSL_ALERT_UNKNOWN_CA) {
/* Example to allow anonymous connections based on a define */
if (ALLOW_ANON_CONNECTIONS) {
if (g_https_trace)
Serial.println("Allowing anonymous connection for:");
//cert->subject.commonName holds the value?
return SSL_ALLOW_ANON_CONNECTION;
}
if (g_https_trace)
Serial.println("ERROR: No matching CA found. Terminating connection");
}
psX509Cert_t *next;
/* If the expectedName passed to matrixSslNewClientSession does not
match any of the server subject name or subjAltNames, we will have
the alert below.
For security, the expected name (typically a domain name) _must_
match one of the certificate subject names, or the connection
should not continue.
The default MatrixSSL certificates use localhost and 127.0.0.1 as
the subjects, so unless the server IP matches one of those, this
alert will happen.
To temporarily disable the subjet name validation, NULL can be passed
as expectedName to matrixNewClientSession.
*/
if (alert == SSL_ALERT_CERTIFICATE_UNKNOWN) {
//ssl->expectedName not found in cert subject names
if (g_https_trace)
Serial.println("ERROR: expectedName not found in cert subject names");
}
if (alert == SSL_ALERT_CERTIFICATE_EXPIRED) {
#ifdef POSIX
if (g_https_trace)
Serial.println("ERROR: A cert did not fall within the "
"notBefore/notAfter window");
#else
if (g_https_trace)
Serial.println("WARNING: Certificate date window validation not "
"implemented");
alert = 0;
#endif
}
if (alert == SSL_ALERT_ILLEGAL_PARAMETER) {
if (g_https_trace)
Serial.println("ERROR: Found correct CA but X.509 extension details are "
"wrong");
}
/* Key usage related problems on chain */
for (next = cert; next != NULL; next = next->next) {
if (next->authStatus == PS_CERT_AUTH_FAIL_EXTENSION) {
if (next->authFailFlags & PS_CERT_AUTH_FAIL_KEY_USAGE_FLAG) {
if (g_https_trace)
Serial.println("CA keyUsage extension doesn't allow cert signing");
}
if (next->authFailFlags & PS_CERT_AUTH_FAIL_EKU_FLAG) {
if (g_https_trace)
Serial.println("Cert extendedKeyUsage extension doesn't allow TLS");
}
}
}
if (alert == SSL_ALERT_BAD_CERTIFICATE) {
/* Should never let a connection happen if this is set. There was
either a problem in the presented chain or in the final CA test */
if (g_https_trace)
Serial.println("ERROR: Problem in certificate validation. Exiting.");
}
if (alert == 0) {
// Passes test: cert->subject.commonName
if (g_https_trace) Serial.println("SUCCESS: Validated!");
}
#endif /* !USE_ONLY_PSK_CIPHER_SUITE */
return alert;
}
static int32 extensionCb(ssl_t *ssl, unsigned short extType,
unsigned short extLen, void *e) {
unsigned char *c;
short len;
char proto[128];
c = (unsigned char*)e;
if (extType == EXT_ALPN) {
memset(proto, 0x0, 128);
/* two byte proto list len, one byte proto len, then proto */
c += 2; /* Skip proto list len */
len = *c; c++;
memcpy(proto, c, len);
if (g_https_trace) {
Serial.print("Server agreed to use ");
Serial.println(proto);
}
}
return PS_SUCCESS;
}
static int32 TCPRead (int len) {
unsigned int bufferPosition = 0;
char c;
unsigned long lastRead = millis();
bool error = false;
bool timeout = false;
do {
while (client.available()) {
c = client.read();
if (g_https_trace) {
if (bufferPosition == 0) Serial.println("TCP Receiving ...");
if ((c < 32) || (c > 126)) Serial.print('.');
else Serial.print(c);
if (bufferPosition % 80 == 0) Serial.println();
}
lastRead = millis();
if (c == -1) {
error = true;
if (g_https_trace)
Serial.println("HttpClient>\tError: No data available.");
break;
}
// Check that received character fits in buffer before storing.
if (bufferPosition < len) {
g_buf[bufferPosition++] = c;
}
if (bufferPosition == len) {
return bufferPosition;
}
}
if (g_https_trace) {
_psTrace("HttpClient>\tDone before full len ");
Serial.println(bufferPosition);
}
// Check for timeout since last read
timeout = millis() - lastRead > TIMEOUT;
if (!error && !timeout) {
delay(200);
}
} while (client.connected() && !timeout && (bufferPosition == 0));
return bufferPosition;
}
int httpsClientConnection(unsigned char * requestContent) {
int32 rc, len, transferred;
g_httpRequestHdr = requestContent;
memset(&options, 0x0, sizeof(sslSessOpts_t));
options.versionFlag = sessionFlag;
options.userPtr = keys;
matrixSslNewHelloExtension(&extension, NULL);
matrixSslCreateSNIext(NULL, (unsigned char*)g_host, (uint32)strlen(g_host),
&ext, &extLen);
matrixSslLoadHelloExtension(extension, ext, extLen, EXT_SNI);
// TOOD: Dynamic memory allocation, possible memory leak
psFree(ext, NULL);
rc = matrixSslNewClientSession(&ssl, keys, sid, g_cipher, g_ciphers, certCb,
NULL, extension, extensionCb, &options);
if (g_https_trace) {
Serial.print("matrixSslNewClientSession:");
Serial.println(rc);
}
matrixSslDeleteHelloExtension(extension);
if (rc != MATRIXSSL_REQUEST_SEND) {
if (g_https_trace)
Serial.println("New Client Session Failed: Exiting\n");
return HTTPS_ERROR;
}
if (g_https_trace) {
freemem = System.freeMemory();
Serial.print("free memory 3: ");
Serial.println(freemem);
}
WRITE_MORE:
while ((len = matrixSslGetOutdata(ssl, &g_buf)) > 0) {
transferred = client.write(g_buf, len);
if (transferred <= 0) {
goto L_CLOSE_ERR;
}
else {
/* Indicate that we've written > 0 bytes of data */
if (g_https_trace) {
Serial.print("Bytes sent Successfully?!: ");Serial.println(len);
}
if ((rc = matrixSslSentData(ssl, transferred)) < 0) {
goto L_CLOSE_ERR;
}
if (g_https_trace) {
Serial.print("matrixSslSentData: ");
Serial.println(rc);
}
if (rc == MATRIXSSL_REQUEST_CLOSE) {
// TOOD: Anything here?
return MATRIXSSL_SUCCESS;
}
if (rc == MATRIXSSL_HANDSHAKE_COMPLETE) {
/* If we sent the Finished SSL message, initiate the HTTP req */
/* (This occurs on a resumption handshake) */
if (httpWriteRequest() < 0) {
goto L_CLOSE_ERR;
}
goto WRITE_MORE;
}
/* SSL_REQUEST_SEND is handled by loop logic */
if (g_https_trace)
Serial.println("Sent Successfully?!, everything good");
}
}
READ_MORE:
if ((len = matrixSslGetReadbuf(ssl, &g_buf)) <= 0) {
if (g_https_trace) {
Serial.print("matrixSslGetReadbuf: ");
Serial.println(len);
}
goto L_CLOSE_ERR;
}
if (g_https_trace) {
Serial.print("matrixSslGetReadbuf: ");
Serial.println(len);
}
if ((transferred = TCPRead(len)) < 0) {
if (g_https_trace) {
Serial.print("Received: ");
Serial.println(transferred);
}
goto L_CLOSE_ERR;
}
if (g_https_trace) {
Serial.print("Received: ");
Serial.println(transferred);
}
if (transferred == 0) {
goto L_CLOSE_ERR;
}
if ((rc = matrixSslReceivedData(ssl, (int32)transferred, &g_buf,
(uint32*)&len)) < 0) {
goto L_CLOSE_ERR;
}
if (g_https_trace) {
Serial.print("matrixSslReceivedData: Tx: ");
Serial.print((int32) transferred);
Serial.print(" Len: "); Serial.print(len);
Serial.print(" rc: "); Serial.println(rc);
}
PROCESS_MORE:
switch (rc) {
case MATRIXSSL_HANDSHAKE_COMPLETE:
#ifdef REHANDSHAKE_TEST
/*
Test rehandshake capabilities of server. If a successful
session resmption rehandshake occurs, this client will be last to
send handshake data and MATRIXSSL_HANDSHAKE_COMPLETE will hit on
the WRITE_MORE handler and httpWriteRequest will occur there.
NOTE: If the server doesn't support session resumption it is
possible to fall into an endless rehandshake loop
*/
if (matrixSslEncodeRehandshake(ssl, NULL, NULL, 0,
g_cipher, g_ciphers) < 0) {
goto L_CLOSE_ERR;
}
#else
/* We got the Finished SSL message, initiate the HTTP req */
if (httpWriteRequest() < 0) {
goto L_CLOSE_ERR;
}
#endif
goto WRITE_MORE;
case MATRIXSSL_APP_DATA:
case MATRIXSSL_APP_DATA_COMPRESSED:
g_bytes_received += len;
if (g_https_trace) {
for (int i = 0 ; i < len; i++) {
Serial.print((char)g_buf[i]);
}
Serial.println();
}
if (!g_https_complete) {
if (strstr((const char *) g_buf, end_header)) {
g_https_complete = true;
}
}
rc = matrixSslProcessedData(ssl, &g_buf, (uint32*)&len);
if (g_https_trace) {
Serial.print("matrixSslProcessedData: ");
Serial.println(rc);
}
if (rc < 0) {
goto L_CLOSE_ERR;
}
if (g_bytes_requested > 0) {
if (g_bytes_received >= g_bytes_requested) {
/* We've received all that was requested, so close */
if (g_https_trace)
Serial.println("Stopping connection for a stupid reason");
return MATRIXSSL_SUCCESS;
}
if (rc == 0) {
/* We processed a partial HTTP message */
goto READ_MORE;
}
}
goto PROCESS_MORE;
case MATRIXSSL_REQUEST_SEND:
goto WRITE_MORE;
case MATRIXSSL_REQUEST_RECV:
goto READ_MORE;
case MATRIXSSL_RECEIVED_ALERT:
/* The first byte of the buffer is the level */
/* The second byte is the description */
if (*g_buf == SSL_ALERT_LEVEL_FATAL) {
if (g_https_trace) Serial.println("Fatal alert: %d, closing connection.");
goto L_CLOSE_ERR;
}
/* Closure alert is normal (and best) way to close */
if (*(g_buf + 1) == SSL_ALERT_CLOSE_NOTIFY) {
if (g_https_trace) Serial.println("Gentle Close");
// TODO: Do something with this whole parsing stuff
return MATRIXSSL_SUCCESS;
}
if (g_https_trace) Serial.println("Warning alert");
if ((rc = matrixSslProcessedData(ssl, &g_buf, (uint32*)&len)) == 0) {
/* No more data in buffer. Might as well read for more. */
if (g_https_trace) Serial.println("Reading more ...");
goto READ_MORE;
}
if (g_https_trace) Serial.println("rc: %d, Processing more ..");
goto PROCESS_MORE;
default:
/* If rc <= 0 we fall here */
goto L_CLOSE_ERR;
}
L_CLOSE_ERR:
if (!g_https_complete) {
if (g_https_trace) Serial.println("FAIL: No HTTP Response");
} else {
if (g_https_trace) Serial.println("Received something");
}
matrixSslDeleteSession(ssl);
return MATRIXSSL_ERROR;
}
void httpsclientCleanUp() {
matrixSslDeleteSessionId(sid);
matrixSslDeleteKeys(keys);
matrixSslClose();
}