-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpsclient.c
474 lines (402 loc) · 15.6 KB
/
httpsclient.c
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
/*
* Copyright (c) 2016, Freescale Semiconductor, Inc.
* Copyright 2017 NXP. Not a Contribution
* All rights reserved.
*
*
* SPDX-License-Identifier: BSD-3-Clause
*/
/*******************************************************************************
* Includes
******************************************************************************/
#include "lwip/opt.h"
#include "lwip/debug.h"
#include "lwip/stats.h"
#include "lwip/tcp.h"
#include "lwip/sockets.h"
#include "httpsclient.h"
#include "lwip/netdb.h"
#include "fsl_debug_console.h"
#include <stdlib.h>
#include <stdio.h>
#include "memfault/components.h"
#include "memfault/http/root_certs.h"
const char memfault_cert[] = MEMFAULT_ROOT_CERTS_DIGICERT_GLOBAL_ROOT_CA;
// Memfault project key
const char *memfault_project_key = "<YOUR PROJECT KEY HERE>";
// Switch this to get verbose debug prints
#define DEBUG_PRINTF(...)
// #define DEBUG_PRINTF(...) PRINTF(__VA_ARGS__)
/*******************************************************************************
* Definitions
******************************************************************************/
/* This is the value used for ssl read timeout */
#define IOT_SSL_READ_TIMEOUT 10
#define GET_REQUEST \
"GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n" \
"HOST: os.mbed.com\r\n\r\n"
#define DEBUG_LEVEL 0
/*******************************************************************************
* Prototypes
******************************************************************************/
/*******************************************************************************
* Variables
******************************************************************************/
TLSDataParams tlsDataParams;
const char *HTTPS_SERVER_NAME = MEMFAULT_HTTP_CHUNKS_API_HOST;
const char *HTTPS_SERVER_PORT = "443";
unsigned char https_buf[1024];
/*******************************************************************************
* Code
******************************************************************************/
/* Send function used by mbedtls ssl */
static int lwipSend(void *fd, unsigned char const *buf, size_t len)
{
return lwip_send((*(int *)fd), buf, len, 0);
}
/* Send function used by mbedtls ssl */
static int lwipRecv(void *fd, unsigned char const *buf, size_t len)
{
return lwip_recv((*(int *)fd), (void *)buf, len, 0);
}
int write_request(void *chunk_data, size_t chunk_data_len)
{
/*
* Write the POST request
*/
int ret = 0;
// format string for building the HTTP header
#define POST_REQUEST \
"POST /api/v0/chunks/%s HTTP/1.1\r\n" \
"Host:chunks.memfault.com\r\n" \
"User-Agent: MemfaultSDK/0.4.2\r\n" \
"Memfault-Project-Key:%s\r\n" \
"Content-Type:application/octet-stream\r\n" \
"Content-Length:%d\r\n\r\n"
// format the request
unsigned char sendbuf[1048];
sMemfaultDeviceInfo device_info;
memfault_platform_get_device_info(&device_info);
size_t len =
sprintf((char *)sendbuf, POST_REQUEST, device_info.device_serial,
memfault_project_key, chunk_data_len);
DEBUG_PRINTF( " > Write to server:" );
DEBUG_PRINTF("\nHeader: \n%s", sendbuf);
// send the header
while( ( ret = mbedtls_ssl_write( &(tlsDataParams.ssl), sendbuf, len ) ) <= 0 )
{
if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
{
PRINTF( "\n failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
goto exit;
}
}
len = ret;
DEBUG_PRINTF( "\n %d header bytes written\n\n", len);
// send the payload
while ((ret = mbedtls_ssl_write(&(tlsDataParams.ssl),
(const unsigned char *)chunk_data,
chunk_data_len)) <= 0) {
if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
PRINTF(" failed\n ! mbedtls_ssl_write returned %d\n\n", ret);
goto exit;
}
}
len = ret;
DEBUG_PRINTF(" %d bytes written\n\n%s", len, (char *)https_buf);
return ret;
exit:
https_client_tls_release();
return -1;
}
int read_request(void)
{
/*
* Read the HTTPS response
*/
int ret = 0;
int len = 0;
DEBUG_PRINTF(" < Read from server:");
do
{
len = sizeof(https_buf) - 1;
memset(https_buf, 0, sizeof(https_buf));
ret = mbedtls_ssl_read(&(tlsDataParams.ssl), https_buf, len);
if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE)
continue;
if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY)
break;
if (ret < 0)
{
PRINTF("failed\n ! mbedtls_ssl_read returned %d\n\n", ret);
goto exit;
}
if (ret == 0)
{
DEBUG_PRINTF("\n\nEOF\n\n");
break;
}
len = ret;
DEBUG_PRINTF(" %d bytes read\n\n%s", len, (char *)https_buf);
// the connection doesn't hang up until 30 seconds after the HTTP
// request completes, so check for an HTTP response code
if (strstr(https_buf, "HTTP/1.1 ")) {
DEBUG_PRINTF(" . Response received, exiting\n");
break;
}
} while (1);
if (strstr(https_buf, "HTTP/1.1 202 Accepted")) {
PRINTF(" < HTTP 202 Accepted received!\n");
ret = 0;
} else {
PRINTF(" HTTP Response error:\n%s\n", https_buf);
ret = -1;
}
return ret;
exit:
https_client_tls_release();
return -1;
}
static int _iot_tls_verify_cert(void *data, mbedtls_x509_crt *crt, int depth, uint32_t *flags)
{
char buf[1024];
((void)data);
DEBUG_PRINTF("\nVerify requested for (Depth %d):\n", depth);
mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt);
DEBUG_PRINTF("%s", buf);
if ((*flags) == 0)
{
DEBUG_PRINTF(" This certificate has no flags\n");
}
else
{
DEBUG_PRINTF(buf, sizeof(buf), " ! ", *flags);
DEBUG_PRINTF("%s\n", buf);
}
return 0;
}
#ifdef MBEDTLS_DEBUG_C
static void my_debug(void *ctx, int level, const char *file, int line, const char *str)
{
((void)level);
DEBUG_PRINTF("\r\n%s, at line %d in file %s\n", str, line, file);
}
#endif
int https_client_tls_init(void)
{
int ret = 0;
const char *pers = "aws_iot_tls_wrapper";
char vrfy_buf[512];
bool ServerVerificationFlag = false;
const mbedtls_md_info_t *md_info;
#ifdef MBEDTLS_DEBUG_C
unsigned char buf[MBEDTLS_SSL_MAX_CONTENT_LEN + 1];
#endif
mbedtls_ssl_init(&(tlsDataParams.ssl));
mbedtls_ssl_config_init(&(tlsDataParams.conf));
mbedtls_hmac_drbg_init(&(tlsDataParams.hmac_drbg));
mbedtls_x509_crt_init(&(tlsDataParams.cacert));
mbedtls_x509_crt_init(&(tlsDataParams.clicert));
mbedtls_pk_init(&(tlsDataParams.pkey));
#if defined(MBEDTLS_DEBUG_C)
/* Enable debug output of mbedtls */
mbedtls_ssl_conf_dbg(&(tlsDataParams.conf), my_debug, NULL);
mbedtls_debug_set_threshold(DEBUG_LEVEL);
#endif
DEBUG_PRINTF("\n . Seeding the random number generator...");
mbedtls_entropy_init(&(tlsDataParams.entropy));
md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
if ((ret = mbedtls_hmac_drbg_seed(&(tlsDataParams.hmac_drbg), md_info, mbedtls_entropy_func,
&(tlsDataParams.entropy), (const unsigned char *)pers, strlen(pers))) != 0)
{
PRINTF(" failed\n ! mbedtls_hmac_drbg_seed returned -0x%x\n", -ret);
return NETWORK_MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED;
}
DEBUG_PRINTF(" . Loading the CA root certificate ...");
ret = mbedtls_x509_crt_parse(&(tlsDataParams.cacert), (const unsigned char *)mbedtls_test_ca_crt,
mbedtls_test_ca_crt_len);
if (ret < 0)
{
PRINTF(" failed\n ! mbedtls_x509_crt_parse returned -0x%x while parsing root cert\n\n", -ret);
return NETWORK_X509_ROOT_CRT_PARSE_ERROR;
}
DEBUG_PRINTF(" ok (%d skipped)\n", ret);
DEBUG_PRINTF(" . Loading the client cert. and key...");
ret = mbedtls_x509_crt_parse(&(tlsDataParams.clicert), (const unsigned char *)mbedtls_test_cli_crt,
mbedtls_test_cli_crt_len);
if (ret != 0)
{
PRINTF(" failed\n ! mbedtls_x509_crt_parse returned -0x%x while parsing device cert\n\n", -ret);
return NETWORK_X509_DEVICE_CRT_PARSE_ERROR;
}
ret = mbedtls_pk_parse_key(&(tlsDataParams.pkey), (const unsigned char *)mbedtls_test_cli_key,
mbedtls_test_cli_key_len, NULL, 0);
if (ret != 0)
{
PRINTF(" failed\n ! mbedtls_pk_parse_key returned -0x%x while parsing private key\n\n", -ret);
return NETWORK_PK_PRIVATE_KEY_PARSE_ERROR;
}
DEBUG_PRINTF(" ok\n");
PRINTF("Connecting to %s/%s ... ", HTTPS_SERVER_NAME, HTTPS_SERVER_PORT);
struct addrinfo hints;
struct addrinfo *res;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
ret = getaddrinfo(HTTPS_SERVER_NAME, HTTPS_SERVER_PORT, &hints, &res);
if ((ret != 0) || (res == NULL))
{
return NETWORK_ERR_NET_UNKNOWN_HOST;
}
tlsDataParams.fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (tlsDataParams.fd < 0)
{
return NETWORK_ERR_NET_SOCKET_FAILED;
}
ret = connect(tlsDataParams.fd, res->ai_addr, res->ai_addrlen);
freeaddrinfo(res);
if (ret != 0)
{
close(tlsDataParams.fd);
return NETWORK_ERR_NET_CONNECT_FAILED;
}
DEBUG_PRINTF(" . Setting up the SSL/TLS structure...");
if ((ret = mbedtls_ssl_config_defaults(&(tlsDataParams.conf), MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM,
MBEDTLS_SSL_PRESET_DEFAULT)) != 0)
{
PRINTF(" failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n", -ret);
return SSL_CONNECTION_ERROR;
}
mbedtls_ssl_conf_verify(&(tlsDataParams.conf), _iot_tls_verify_cert, NULL);
if (ServerVerificationFlag == true)
{
mbedtls_ssl_conf_authmode(&(tlsDataParams.conf), MBEDTLS_SSL_VERIFY_REQUIRED);
}
else
{
mbedtls_ssl_conf_authmode(&(tlsDataParams.conf), MBEDTLS_SSL_VERIFY_OPTIONAL);
}
mbedtls_ssl_conf_rng(&(tlsDataParams.conf), mbedtls_hmac_drbg_random, &(tlsDataParams.hmac_drbg));
mbedtls_ssl_conf_ca_chain(&(tlsDataParams.conf), &(tlsDataParams.cacert), NULL);
if ((ret = mbedtls_ssl_conf_own_cert(&(tlsDataParams.conf), &(tlsDataParams.clicert), &(tlsDataParams.pkey))) != 0)
{
PRINTF(" failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret);
return SSL_CONNECTION_ERROR;
}
if ((ret = mbedtls_ssl_setup(&(tlsDataParams.ssl), &(tlsDataParams.conf))) != 0)
{
PRINTF(" failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", -ret);
return SSL_CONNECTION_ERROR;
}
if ((ret = mbedtls_ssl_set_hostname(&(tlsDataParams.ssl), HTTPS_SERVER_NAME)) != 0)
{
PRINTF(" failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret);
return SSL_CONNECTION_ERROR;
}
DEBUG_PRINTF("\n\nSSL state connect : %d ", tlsDataParams.ssl.state);
mbedtls_ssl_set_bio(&(tlsDataParams.ssl), &(tlsDataParams.fd), lwipSend, (mbedtls_ssl_recv_t *)lwipRecv, NULL);
PRINTF(" ok\n");
DEBUG_PRINTF("\n\nSSL state connect : %d ", tlsDataParams.ssl.state);
DEBUG_PRINTF(" . Performing the SSL/TLS handshake...");
while ((ret = mbedtls_ssl_handshake(&(tlsDataParams.ssl))) != 0)
{
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE)
{
PRINTF(" failed\n ! mbedtls_ssl_handshake returned -0x%x\n", -ret);
if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED)
{
PRINTF(
" Unable to verify the server's certificate. "
" Alternatively, you may want to use "
"auth_mode=optional for testing purposes.\n");
}
return SSL_CONNECTION_ERROR;
}
}
DEBUG_PRINTF(" ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n", mbedtls_ssl_get_version(&(tlsDataParams.ssl)),
mbedtls_ssl_get_ciphersuite(&(tlsDataParams.ssl)));
if ((ret = mbedtls_ssl_get_record_expansion(&(tlsDataParams.ssl))) >= 0)
{
DEBUG_PRINTF(" [ Record expansion is %d ]\n", ret);
}
else
{
DEBUG_PRINTF(" [ Record expansion is unknown (compression) ]\n");
}
DEBUG_PRINTF(" . Verifying peer X.509 certificate...");
if (ServerVerificationFlag == true)
{
if ((tlsDataParams.flags = mbedtls_ssl_get_verify_result(&(tlsDataParams.ssl))) != 0)
{
PRINTF(" failed\n");
mbedtls_x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), " ! ", tlsDataParams.flags);
PRINTF("%s\n", vrfy_buf);
ret = SSL_CONNECTION_ERROR;
}
else
{
DEBUG_PRINTF(" ok\n");
ret = SUCCESS;
}
}
else
{
DEBUG_PRINTF(" Server Verification skipped\n");
ret = SUCCESS;
}
#ifdef MBEDTLS_DEBUG_C
if (mbedtls_ssl_get_peer_cert(&(tlsDataParams.ssl)) != NULL)
{
DEBUG_PRINTF(" . Peer certificate information ...\n");
mbedtls_x509_crt_info((char *)buf, sizeof(buf) - 1, " ", mbedtls_ssl_get_peer_cert(&(tlsDataParams.ssl)));
DEBUG_PRINTF("%s\n", buf);
}
#endif
mbedtls_ssl_conf_read_timeout(&(tlsDataParams.conf), IOT_SSL_READ_TIMEOUT);
// buffer to copy chunk data into
while (memfault_packetizer_data_available()) {
uint8_t buf[512];
size_t buf_len = sizeof(buf);
bool data_available = memfault_packetizer_get_chunk(buf, &buf_len);
if (!data_available ) {
return false; // no more data to send
}
// // example chunk data
// const unsigned char chunk[] = {
// 0x08, 0x02, 0xa7, 0x02, 0x01, 0x03, 0x01, 0x07, 0x6a, 0x54, 0x45,
// 0x53, 0x54, 0x53, 0x45, 0x52, 0x49, 0x41, 0x4c, 0x0a, 0x6d, 0x74,
// 0x65, 0x73, 0x74, 0x2d, 0x73, 0x6f, 0x66, 0x74, 0x77, 0x61, 0x72,
// 0x65, 0x09, 0x6a, 0x31, 0x2e, 0x30, 0x2e, 0x30, 0x2d, 0x74, 0x65,
// 0x73, 0x74, 0x06, 0x6d, 0x74, 0x65, 0x73, 0x74, 0x2d, 0x68, 0x61,
// 0x72, 0x64, 0x77, 0x61, 0x72, 0x65, 0x04, 0xa1, 0x01, 0xa1, 0x72,
// 0x63, 0x68, 0x75, 0x6e, 0x6b, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x5f,
// 0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x01, 0x31, 0xe4};
PRINTF("\nSending chunk of size %d\n", buf_len);
ret = write_request(buf, buf_len);
if (ret != buf_len) {
PRINTF("Error! chunk write request failed\n");
break;
}
ret = read_request();
if (ret != 0) {
PRINTF("Error! chunk write response failed\n");
break;
}
}
https_client_tls_release();
return (Error_t)ret;
}
/* Release TLS */
void https_client_tls_release(void)
{
close(tlsDataParams.fd);
mbedtls_x509_crt_free(&(tlsDataParams.clicert));
mbedtls_x509_crt_free(&(tlsDataParams.cacert));
mbedtls_pk_free(&(tlsDataParams.pkey));
mbedtls_ssl_free(&(tlsDataParams.ssl));
mbedtls_ssl_config_free(&(tlsDataParams.conf));
mbedtls_hmac_drbg_free(&(tlsDataParams.hmac_drbg));
mbedtls_entropy_free(&(tlsDataParams.entropy));
}