Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/adapters/tlsio_wolfssl.c

raw
1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4 #include <stdlib.h>
5 #define WOLFSSL_OPTIONS_IGNORE_SYS
6 #include "wolfssl/options.h"
7 #include "wolfssl/ssl.h"
8 #include "wolfssl/error-ssl.h"
9 #include "wolfssl/wolfcrypt/types.h"
10 #include <stdio.h>
11 #include <stdbool.h>
12 #include <string.h>
13 #include "azure_c_shared_utility/gballoc.h"
14 #include "azure_c_shared_utility/tlsio.h"
15 #include "azure_c_shared_utility/tlsio_wolfssl.h"
16 #include "azure_c_shared_utility/socketio.h"
17 #include "azure_c_shared_utility/crt_abstractions.h"
18 #include "azure_c_shared_utility/optimize_size.h"
19 #include "azure_c_shared_utility/xlogging.h"
20 #include "azure_c_shared_utility/shared_util_options.h"
21 #include "azure_c_shared_utility/safe_math.h"
22
23 typedef enum TLSIO_STATE_ENUM_TAG
24 {
25 TLSIO_STATE_NOT_OPEN,
26 TLSIO_STATE_OPENING_UNDERLYING_IO,
27 TLSIO_STATE_IN_HANDSHAKE,
28 TLSIO_STATE_OPEN,
29 TLSIO_STATE_CLOSING,
30 TLSIO_STATE_ERROR
31 } TLSIO_STATE_ENUM;
32
33 typedef struct TLS_IO_INSTANCE_TAG
34 {
35 XIO_HANDLE socket_io;
36 ON_BYTES_RECEIVED on_bytes_received;
37 ON_IO_OPEN_COMPLETE on_io_open_complete;
38 ON_IO_CLOSE_COMPLETE on_io_close_complete;
39 ON_IO_ERROR on_io_error;
40 void* on_bytes_received_context;
41 void* on_io_open_complete_context;
42 void* on_io_close_complete_context;
43 void* on_io_error_context;
44 WOLFSSL* ssl;
45 WOLFSSL_CTX* ssl_context;
46 TLSIO_STATE_ENUM tlsio_state;
47 unsigned char* socket_io_read_bytes;
48 size_t socket_io_read_byte_count;
49 ON_SEND_COMPLETE on_send_complete;
50 void* on_send_complete_callback_context;
51 char* certificate;
52 char* x509certificate;
53 char* x509privatekey;
54 int wolfssl_device_id;
55 char* hostname;
56 bool ignore_host_name_check;
57 } TLS_IO_INSTANCE;
58
59 STATIC_VAR_UNUSED const char* const OPTION_WOLFSSL_SET_DEVICE_ID = "SetDeviceId";
60 static const size_t SOCKET_READ_LIMIT = 5;
61
62 /*this function will clone an option given by name and value*/
63 static void* tlsio_wolfssl_CloneOption(const char* name, const void* value)
64 {
65 void* result;
66 if ((name == NULL) || (value == NULL))
67 {
68 LogError("invalid parameter detected: const char* name=%p, const void* value=%p", name, value);
69 result = NULL;
70 }
71 else
72 {
73 if (strcmp(name, OPTION_TRUSTED_CERT) == 0)
74 {
75 if (mallocAndStrcpy_s((char**)&result, value) != 0)
76 {
77 LogError("unable to mallocAndStrcpy_s TrustedCerts value");
78 result = NULL;
79 }
80 else
81 {
82 /*return as is*/
83 }
84 }
85 else if (strcmp(name, SU_OPTION_X509_CERT) == 0)
86 {
87 if (mallocAndStrcpy_s((char**)&result, value) != 0)
88 {
89 LogError("unable to mallocAndStrcpy_s x509certificate value");
90 result = NULL;
91 }
92 else
93 {
94 /*return as is*/
95 }
96 }
97 else if (strcmp(name, SU_OPTION_X509_PRIVATE_KEY) == 0)
98 {
99 if (mallocAndStrcpy_s((char**)&result, value) != 0)
100 {
101 LogError("unable to mallocAndStrcpy_s x509privatekey value");
102 result = NULL;
103 }
104 else
105 {
106 /*return as is*/
107 }
108 }
109 #ifdef INVALID_DEVID
110 else if(strcmp(name, OPTION_WOLFSSL_SET_DEVICE_ID) == 0 )
111 {
112 int* value_clone;
113
114 if ((value_clone = malloc(sizeof(int))) == NULL)
115 {
116 LogError("unable to clone device id option");
117 }
118 else
119 {
120 *value_clone = *(int*)value;
121 }
122
123 result = value_clone;
124 }
125 #endif
126 else
127 {
128 LogError("not handled option : %s", name);
129 result = NULL;
130 }
131 }
132 return result;
133 }
134
135 /*this function destroys an option previously created*/
136 static void tlsio_wolfssl_DestroyOption(const char* name, const void* value)
137 {
138 /*since all options for this layer are actually string copies., disposing of one is just calling free*/
139 if ((name == NULL) || (value == NULL))
140 {
141 LogError("invalid parameter detected: const char* name=%p, const void* value=%p", name, value);
142 }
143 else
144 {
145 if ((strcmp(name, OPTION_TRUSTED_CERT) == 0) ||
146 (strcmp(name, SU_OPTION_X509_CERT) == 0) ||
147 (strcmp(name, SU_OPTION_X509_PRIVATE_KEY) == 0) ||
148 (strcmp(name, OPTION_WOLFSSL_SET_DEVICE_ID) == 0))
149 {
150 free((void*)value);
151 }
152 else
153 {
154 LogError("not handled option : %s", name);
155 }
156 }
157 }
158
159 static OPTIONHANDLER_HANDLE tlsio_wolfssl_retrieveoptions(CONCRETE_IO_HANDLE tls_io)
160 {
161 OPTIONHANDLER_HANDLE result;
162 if (tls_io == NULL)
163 {
164 LogError("NULL tls_io parameter");
165 result = NULL;
166 }
167 else
168 {
169 result = OptionHandler_Create(tlsio_wolfssl_CloneOption, tlsio_wolfssl_DestroyOption, tlsio_wolfssl_setoption);
170 if (result == NULL)
171 {
172 LogError("unable to OptionHandler_Create");
173 /*return as is*/
174 }
175 else
176 {
177 /*this layer cares about the certificates and the x509 credentials*/
178 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io;
179 if (
180 (tls_io_instance->x509certificate != NULL) &&
181 (OptionHandler_AddOption(result, SU_OPTION_X509_CERT, tls_io_instance->x509certificate) != OPTIONHANDLER_OK)
182 )
183 {
184 LogError("unable to save x509certificate option");
185 OptionHandler_Destroy(result);
186 result = NULL;
187 }
188 else if (
189 (tls_io_instance->x509privatekey != NULL) &&
190 (OptionHandler_AddOption(result, SU_OPTION_X509_PRIVATE_KEY, tls_io_instance->x509privatekey) != OPTIONHANDLER_OK)
191 )
192 {
193 LogError("unable to save x509privatekey option");
194 OptionHandler_Destroy(result);
195 result = NULL;
196 }
197 else if (
198 (tls_io_instance->certificate != NULL) &&
199 (OptionHandler_AddOption(result, OPTION_TRUSTED_CERT, tls_io_instance->certificate) != OPTIONHANDLER_OK)
200 )
201 {
202 LogError("unable to save TrustedCerts option");
203 OptionHandler_Destroy(result);
204 result = NULL;
205 }
206 #ifdef INVALID_DEVID
207 else if (
208 (tls_io_instance->wolfssl_device_id != INVALID_DEVID) &&
209 (OptionHandler_AddOption(result, OPTION_WOLFSSL_SET_DEVICE_ID, &tls_io_instance->wolfssl_device_id) != OPTIONHANDLER_OK)
210 )
211 {
212 LogError("unable to save deviceid option");
213 OptionHandler_Destroy(result);
214 result = NULL;
215 }
216 #endif
217 else
218 {
219 /*all is fine, all interesting options have been saved*/
220 /*return as is*/
221 }
222 }
223 }
224
225 return result;
226 }
227
228 static const IO_INTERFACE_DESCRIPTION tlsio_wolfssl_interface_description =
229 {
230 tlsio_wolfssl_retrieveoptions,
231 tlsio_wolfssl_create,
232 tlsio_wolfssl_destroy,
233 tlsio_wolfssl_open,
234 tlsio_wolfssl_close,
235 tlsio_wolfssl_send,
236 tlsio_wolfssl_dowork,
237 tlsio_wolfssl_setoption
238 };
239
240 static void indicate_error(TLS_IO_INSTANCE* tls_io_instance)
241 {
242 if (tls_io_instance->on_io_error != NULL)
243 {
244 tls_io_instance->on_io_error(tls_io_instance->on_io_error_context);
245 }
246 }
247
248 static void indicate_open_complete(TLS_IO_INSTANCE* tls_io_instance, IO_OPEN_RESULT open_result)
249 {
250 if (tls_io_instance->on_io_open_complete != NULL)
251 {
252 tls_io_instance->on_io_open_complete(tls_io_instance->on_io_open_complete_context, open_result);
253 }
254 }
255
256 static int decode_ssl_received_bytes(TLS_IO_INSTANCE* tls_io_instance)
257 {
258 int result = 0;
259 unsigned char buffer[64];
260
261 int rcv_bytes = 0;
262 do
263 {
264 rcv_bytes = wolfSSL_read(tls_io_instance->ssl, buffer, sizeof(buffer));
265 if (rcv_bytes > 0)
266 {
267 if (tls_io_instance->on_bytes_received != NULL)
268 {
269 tls_io_instance->on_bytes_received(tls_io_instance->on_bytes_received_context, buffer, rcv_bytes);
270 }
271 }
272 } while (rcv_bytes > 0);
273 return result;
274 }
275
276 static void on_underlying_io_open_complete(void* context, IO_OPEN_RESULT open_result)
277 {
278 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context;
279
280 if (open_result != IO_OPEN_OK)
281 {
282 LogError("Underlying IO open failed");
283 tls_io_instance->tlsio_state = TLSIO_STATE_ERROR;
284 indicate_open_complete(tls_io_instance, IO_OPEN_ERROR);
285 }
286 else
287 {
288 int res;
289 tls_io_instance->tlsio_state = TLSIO_STATE_IN_HANDSHAKE;
290
291 res = wolfSSL_connect(tls_io_instance->ssl);
292 if (res != SSL_SUCCESS)
293 {
294 // Error codes explained in https://www.wolfssl.com/docs/wolfssl-manual/appendix-c/
295 LogError("WolfSSL connect failed (%d)", wolfSSL_get_error(tls_io_instance->ssl, res));
296 indicate_open_complete(tls_io_instance, IO_OPEN_ERROR);
297 tls_io_instance->tlsio_state = TLSIO_STATE_ERROR;
298 }
299 }
300 }
301
302 static void on_underlying_io_bytes_received(void* context, const unsigned char* buffer, size_t size)
303 {
304 if (context != NULL)
305 {
306 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context;
307 unsigned char* new_socket_io_read_bytes;
308
309 size_t realloc_size = safe_add_size_t(tls_io_instance->socket_io_read_byte_count, size);
310 if (realloc_size == SIZE_MAX ||
311 (new_socket_io_read_bytes = (unsigned char*)realloc(tls_io_instance->socket_io_read_bytes, realloc_size)) == NULL)
312 {
313 LogError("Failed allocating memory for received bytes, size:%zu", realloc_size);
314 tls_io_instance->tlsio_state = TLSIO_STATE_ERROR;
315 indicate_error(tls_io_instance);
316 }
317 else
318 {
319 tls_io_instance->socket_io_read_bytes = new_socket_io_read_bytes;
320 (void)memcpy(tls_io_instance->socket_io_read_bytes + tls_io_instance->socket_io_read_byte_count, buffer, size);
321 tls_io_instance->socket_io_read_byte_count += size;
322 }
323 }
324 else
325 {
326 LogInfo("Supplied context is NULL on bytes_received");
327 }
328 }
329
330 static void on_underlying_io_error(void* context)
331 {
332 if (context != NULL)
333 {
334 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context;
335
336 switch (tls_io_instance->tlsio_state)
337 {
338 default:
339 LogError("Unknown TLS IO WolfSSL state: %d", (int)tls_io_instance->tlsio_state);
340 break;
341
342 case TLSIO_STATE_NOT_OPEN:
343 case TLSIO_STATE_ERROR:
344 break;
345
346 case TLSIO_STATE_OPENING_UNDERLYING_IO:
347 case TLSIO_STATE_IN_HANDSHAKE:
348 tls_io_instance->tlsio_state = TLSIO_STATE_ERROR;
349 indicate_open_complete(tls_io_instance, IO_OPEN_ERROR);
350 break;
351
352 case TLSIO_STATE_OPEN:
353 tls_io_instance->tlsio_state = TLSIO_STATE_ERROR;
354 indicate_error(tls_io_instance);
355 break;
356 }
357 }
358 else
359 {
360 LogInfo("Supplied context is NULL on io_error");
361 }
362 }
363
364 static void on_underlying_io_close_complete(void* context)
365 {
366 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context;
367
368 if (tls_io_instance->tlsio_state != TLSIO_STATE_CLOSING)
369 {
370 LogError("on_underlying_io_close_complete called when not in CLOSING state");
371 }
372 else
373 {
374 if (tls_io_instance->on_io_close_complete != NULL)
375 {
376 tls_io_instance->on_io_close_complete(tls_io_instance->on_io_close_complete_context);
377 }
378 tls_io_instance->tlsio_state = TLSIO_STATE_NOT_OPEN;
379 }
380 }
381
382 static int on_io_recv(WOLFSSL *ssl, char *buf, int sz, void *context)
383 {
384 int result;
385 if (context != NULL)
386 {
387 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context;
388 unsigned char* new_socket_io_read_bytes;
389 size_t socket_reads = 0;
390
391 AZURE_UNREFERENCED_PARAMETER(ssl);
392 while (tls_io_instance->socket_io_read_byte_count == 0 && socket_reads < SOCKET_READ_LIMIT)
393 {
394 xio_dowork(tls_io_instance->socket_io);
395 if (tls_io_instance->tlsio_state != TLSIO_STATE_IN_HANDSHAKE)
396 {
397 break;
398 }
399 socket_reads++;
400 }
401
402 result = tls_io_instance->socket_io_read_byte_count;
403 if (result > sz)
404 {
405 result = sz;
406 }
407
408 if (result > 0)
409 {
410 size_t read_byte_count = safe_subtract_size_t(tls_io_instance->socket_io_read_byte_count, (size_t)result);
411 (void)memcpy(buf, tls_io_instance->socket_io_read_bytes, result);
412 (void)memmove(tls_io_instance->socket_io_read_bytes, tls_io_instance->socket_io_read_bytes + result, read_byte_count);
413
414 tls_io_instance->socket_io_read_byte_count = read_byte_count;
415 if (tls_io_instance->socket_io_read_byte_count > 0)
416 {
417 new_socket_io_read_bytes = (unsigned char*)realloc(tls_io_instance->socket_io_read_bytes, tls_io_instance->socket_io_read_byte_count);
418 if (new_socket_io_read_bytes != NULL)
419 {
420 tls_io_instance->socket_io_read_bytes = new_socket_io_read_bytes;
421 }
422 }
423 else
424 {
425 free(tls_io_instance->socket_io_read_bytes);
426 tls_io_instance->socket_io_read_bytes = NULL;
427 }
428 }
429
430 if (tls_io_instance->tlsio_state == TLSIO_STATE_ERROR)
431 {
432 result = WOLFSSL_CBIO_ERR_GENERAL;
433 }
434 else if ( (result == 0) && (tls_io_instance->tlsio_state == TLSIO_STATE_OPEN))
435 {
436 #ifdef HAVE_SECURE_RENEGOTIATION
437 if (wolfSSL_SSL_renegotiate_pending(tls_io_instance->ssl) == 0) // SERVER_HELLODONE_COMPLETE
438 {
439 result = WOLFSSL_CBIO_ERR_WANT_READ;
440 }
441 // If Server Hello not complete during renegotiation, do not return error.
442 #else
443 result = WOLFSSL_CBIO_ERR_WANT_READ;
444 #endif
445 }
446 else if ((result == 0) && (tls_io_instance->tlsio_state == TLSIO_STATE_CLOSING || tls_io_instance->tlsio_state == TLSIO_STATE_NOT_OPEN))
447 {
448 result = WOLFSSL_CBIO_ERR_CONN_CLOSE;
449 }
450 }
451 else
452 {
453 result = WOLFSSL_CBIO_ERR_GENERAL;
454 }
455 return result;
456 }
457
458 static int on_io_send(WOLFSSL *ssl, char *buf, int sz, void *context)
459 {
460 int result;
461 AZURE_UNREFERENCED_PARAMETER(ssl);
462
463 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context;
464
465 if (xio_send(tls_io_instance->socket_io, buf, sz, tls_io_instance->on_send_complete, tls_io_instance->on_send_complete_callback_context) != 0)
466 {
467 LogError("Failed sending bytes through underlying IO");
468 tls_io_instance->tlsio_state = TLSIO_STATE_ERROR;
469 indicate_error(tls_io_instance);
470 result = WOLFSSL_CBIO_ERR_GENERAL;
471 }
472 else
473 {
474 result = sz;
475 }
476
477 return result;
478 }
479
480 static int on_handshake_done(WOLFSSL* ssl, void* context)
481 {
482 AZURE_UNREFERENCED_PARAMETER(ssl);
483 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context;
484 if (tls_io_instance->tlsio_state != TLSIO_STATE_IN_HANDSHAKE)
485 {
486 LogInfo("on_handshake_done called when not in IN_HANDSHAKE state");
487 }
488 else
489 {
490 tls_io_instance->tlsio_state = TLSIO_STATE_OPEN;
491 indicate_open_complete(tls_io_instance, IO_OPEN_OK);
492 }
493
494 return 0;
495 }
496
497 static int add_certificate_to_store(TLS_IO_INSTANCE* tls_io_instance)
498 {
499 int result;
500 if (tls_io_instance->certificate != NULL)
501 {
502 int res = wolfSSL_CTX_load_verify_buffer(tls_io_instance->ssl_context, (const unsigned char*)tls_io_instance->certificate, strlen(tls_io_instance->certificate), SSL_FILETYPE_PEM);
503 if (res != SSL_SUCCESS)
504 {
505 LogError("wolfSSL_CTX_load_verify_buffer failed");
506 result = MU_FAILURE;
507 }
508 else
509 {
510 result = 0;
511 }
512 }
513 else
514 {
515 result = 0;
516 }
517 return result;
518 }
519
520 static int x509_wolfssl_add_credentials(WOLFSSL* ssl, char* x509certificate, char* x509privatekey) {
521
522 int result;
523
524 if (wolfSSL_use_certificate_chain_buffer(ssl, (unsigned char*)x509certificate, strlen(x509certificate)) != SSL_SUCCESS)
525 {
526 LogError("unable to load x509 client certificate");
527 result = MU_FAILURE;
528 }
529 else if (wolfSSL_use_PrivateKey_buffer(ssl, (unsigned char*)x509privatekey, strlen(x509privatekey), SSL_FILETYPE_PEM) != SSL_SUCCESS)
530 {
531 LogError("unable to load x509 client private key");
532 result = MU_FAILURE;
533 }
534 else
535 {
536 result = 0;
537 }
538 return result;
539 }
540
541 static void destroy_wolfssl_instance(TLS_IO_INSTANCE* tls_io_instance)
542 {
543 wolfSSL_free(tls_io_instance->ssl);
544 tls_io_instance->ssl = NULL;
545 }
546
547 static int create_wolfssl_instance(TLS_IO_INSTANCE* tls_io_instance)
548 {
549 int result;
550 tls_io_instance->ssl = wolfSSL_new(tls_io_instance->ssl_context);
551 if (tls_io_instance->ssl == NULL)
552 {
553 LogError("Failed to add certificates to store");
554 result = MU_FAILURE;
555 }
556 else
557 {
558 tls_io_instance->socket_io_read_bytes = NULL;
559 tls_io_instance->socket_io_read_byte_count = 0;
560 tls_io_instance->on_send_complete = NULL;
561 tls_io_instance->on_send_complete_callback_context = NULL;
562 #ifdef INVALID_DEVID
563 tls_io_instance->wolfssl_device_id = INVALID_DEVID;
564 #endif
565
566 wolfSSL_set_using_nonblock(tls_io_instance->ssl, 1);
567 wolfSSL_SetHsDoneCb(tls_io_instance->ssl, on_handshake_done, tls_io_instance);
568 wolfSSL_SetIOWriteCtx(tls_io_instance->ssl, tls_io_instance);
569 wolfSSL_SetIOReadCtx(tls_io_instance->ssl, tls_io_instance);
570
571 tls_io_instance->tlsio_state = TLSIO_STATE_NOT_OPEN;
572 result = 0;
573
574 #ifdef HAVE_SECURE_RENEGOTIATION
575 if (wolfSSL_UseSecureRenegotiation(tls_io_instance->ssl) != SSL_SUCCESS)
576 {
577 LogError("unable to enable secure renegotiation");
578 result = MU_FAILURE;
579 }
580 #endif
581 }
582 return result;
583 }
584
585 static int enable_domain_check(TLS_IO_INSTANCE* tls_io_instance)
586 {
587 int result = 0;
588
589 if (!tls_io_instance->ignore_host_name_check)
590 {
591 if (wolfSSL_check_domain_name(tls_io_instance->ssl, tls_io_instance->hostname) != WOLFSSL_SUCCESS)
592 {
593 result = MU_FAILURE;
594 }
595 }
596
597 return result;
598 }
599
600 static int prepare_wolfssl_open(TLS_IO_INSTANCE* tls_io_instance)
601 {
602 int result;
603
604 if (enable_domain_check(tls_io_instance))
605 {
606 LogError("Failed to configure domain name verification");
607 result = MU_FAILURE;
608 }
609 else if (add_certificate_to_store(tls_io_instance) != 0)
610 {
611 LogError("Failed to add certificates to store");
612 result = MU_FAILURE;
613 }
614 /*x509 authentication can only be build before underlying connection is realized*/
615 else if ((tls_io_instance->x509certificate != NULL) &&
616 (tls_io_instance->x509privatekey != NULL) &&
617 (x509_wolfssl_add_credentials(tls_io_instance->ssl, tls_io_instance->x509certificate, tls_io_instance->x509privatekey) != 0))
618 {
619 destroy_wolfssl_instance(tls_io_instance);
620 LogError("unable to use x509 authentication");
621 result = MU_FAILURE;
622 }
623 else
624 {
625 result = 0;
626 }
627 return result;
628 }
629
630 int tlsio_wolfssl_init(void)
631 {
632 (void)wolfSSL_library_init();
633 wolfSSL_load_error_strings();
634 return 0;
635 }
636
637 void tlsio_wolfssl_deinit(void)
638 {
639 }
640
641 CONCRETE_IO_HANDLE tlsio_wolfssl_create(void* io_create_parameters)
642 {
643 TLS_IO_INSTANCE* result;
644
645 if (io_create_parameters == NULL)
646 {
647 LogError("NULL io_create_parameters");
648 result = NULL;
649 }
650 else
651 {
652 TLSIO_CONFIG* tls_io_config = io_create_parameters;
653
654 result = (TLS_IO_INSTANCE*)malloc(sizeof(TLS_IO_INSTANCE));
655 if (result == NULL)
656 {
657 LogError("Failed allocating memory for the TLS IO instance.");
658 }
659 else
660 {
661 (void)memset(result, 0, sizeof(TLS_IO_INSTANCE));
662 result->tlsio_state = TLSIO_STATE_NOT_OPEN;
663
664 result->ssl_context = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
665 if (result->ssl_context == NULL)
666 {
667 LogError("Cannot create the wolfSSL context");
668 free(result);
669 result = NULL;
670 }
671 else if (mallocAndStrcpy_s(&result->hostname, tls_io_config->hostname) != 0)
672 {
673 LogError("Failed copying the target hostname.");
674 free(result);
675 result = NULL;
676 }
677 else
678 {
679 // Set the recv and send function on the wolfssl context object
680 wolfSSL_SetIOSend(result->ssl_context, on_io_send);
681 wolfSSL_SetIORecv(result->ssl_context, on_io_recv);
682
683 SOCKETIO_CONFIG socketio_config;
684 const IO_INTERFACE_DESCRIPTION* underlying_io_interface;
685 void* io_interface_parameters;
686
687 if (tls_io_config->underlying_io_interface != NULL)
688 {
689 underlying_io_interface = tls_io_config->underlying_io_interface;
690 io_interface_parameters = tls_io_config->underlying_io_parameters;
691 }
692 else
693 {
694 socketio_config.hostname = tls_io_config->hostname;
695 socketio_config.port = tls_io_config->port;
696 socketio_config.accepted_socket = NULL;
697
698 underlying_io_interface = socketio_get_interface_description();
699 io_interface_parameters = &socketio_config;
700 }
701
702 if (underlying_io_interface == NULL)
703 {
704 LogError("Failed getting socket IO interface description.");
705 wolfSSL_CTX_free(result->ssl_context);
706 free(result->hostname);
707 free(result);
708 result = NULL;
709 }
710 else
711 {
712 result->socket_io = xio_create(underlying_io_interface, io_interface_parameters);
713 if (result->socket_io == NULL)
714 {
715 LogError("Failure connecting to underlying socket_io");
716 wolfSSL_CTX_free(result->ssl_context);
717 free(result->hostname);
718 free(result);
719 result = NULL;
720 }
721 else if (create_wolfssl_instance(result) != 0)
722 {
723 LogError("Failure connecting to underlying socket_io");
724 wolfSSL_CTX_free(result->ssl_context);
725 free(result->hostname);
726 free(result);
727 result = NULL;
728 }
729 }
730 }
731 }
732 }
733
734 return result;
735 }
736
737 void tlsio_wolfssl_destroy(CONCRETE_IO_HANDLE tls_io)
738 {
739 if (tls_io != NULL)
740 {
741 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io;
742 if (tls_io_instance->socket_io_read_bytes != NULL)
743 {
744 free(tls_io_instance->socket_io_read_bytes);
745 tls_io_instance->socket_io_read_bytes = NULL;
746 }
747 if (tls_io_instance->certificate != NULL)
748 {
749 free(tls_io_instance->certificate);
750 tls_io_instance->certificate = NULL;
751 }
752 if (tls_io_instance->x509certificate != NULL)
753 {
754 free(tls_io_instance->x509certificate);
755 tls_io_instance->x509certificate = NULL;
756 }
757 if (tls_io_instance->x509privatekey != NULL)
758 {
759 free(tls_io_instance->x509privatekey);
760 tls_io_instance->x509privatekey = NULL;
761 }
762 destroy_wolfssl_instance(tls_io_instance);
763
764 wolfSSL_CTX_free(tls_io_instance->ssl_context);
765 tls_io_instance->ssl_context = NULL;
766
767 xio_destroy(tls_io_instance->socket_io);
768 free(tls_io_instance->hostname);
769 free(tls_io);
770 }
771 }
772
773 int tlsio_wolfssl_open(CONCRETE_IO_HANDLE tls_io, ON_IO_OPEN_COMPLETE on_io_open_complete, void* on_io_open_complete_context, ON_BYTES_RECEIVED on_bytes_received, void* on_bytes_received_context, ON_IO_ERROR on_io_error, void* on_io_error_context)
774 {
775 int result;
776
777 if (tls_io == NULL)
778 {
779 LogError("NULL tls_io instance");
780 result = MU_FAILURE;
781 }
782 else
783 {
784 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io;
785
786 if (tls_io_instance->tlsio_state != TLSIO_STATE_NOT_OPEN)
787 {
788 LogError("Invalid state encountered.");
789 result = MU_FAILURE;
790 }
791 else
792 {
793 tls_io_instance->on_bytes_received = on_bytes_received;
794 tls_io_instance->on_bytes_received_context = on_bytes_received_context;
795
796 tls_io_instance->on_io_open_complete = on_io_open_complete;
797 tls_io_instance->on_io_open_complete_context = on_io_open_complete_context;
798
799 tls_io_instance->on_io_error = on_io_error;
800 tls_io_instance->on_io_error_context = on_io_error_context;
801
802 tls_io_instance->tlsio_state = TLSIO_STATE_OPENING_UNDERLYING_IO;
803
804 if (prepare_wolfssl_open(tls_io_instance) != 0)
805 {
806 LogError("Cannot create wolfssl instance.");
807 tls_io_instance->tlsio_state = TLSIO_STATE_NOT_OPEN;
808 result = MU_FAILURE;
809 }
810 else if (xio_open(tls_io_instance->socket_io, on_underlying_io_open_complete, tls_io_instance, on_underlying_io_bytes_received, tls_io_instance, on_underlying_io_error, tls_io_instance) != 0)
811 {
812 LogError("Cannot open the underlying IO.");
813 tls_io_instance->tlsio_state = TLSIO_STATE_NOT_OPEN;
814 result = MU_FAILURE;
815 }
816 else
817 {
818 result = 0;
819 }
820 }
821 }
822
823 return result;
824 }
825
826 int tlsio_wolfssl_close(CONCRETE_IO_HANDLE tls_io, ON_IO_CLOSE_COMPLETE on_io_close_complete, void* callback_context)
827 {
828 int result = 0;
829
830 if (tls_io == NULL)
831 {
832 LogError("NULL tls_io handle.");
833 result = MU_FAILURE;
834 }
835 else
836 {
837 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io;
838
839 if ((tls_io_instance->tlsio_state == TLSIO_STATE_NOT_OPEN) ||
840 (tls_io_instance->tlsio_state == TLSIO_STATE_CLOSING))
841 {
842 LogError("Close called while not open.");
843 result = MU_FAILURE;
844 }
845 else
846 {
847 tls_io_instance->tlsio_state = TLSIO_STATE_CLOSING;
848 tls_io_instance->on_io_close_complete = on_io_close_complete;
849 tls_io_instance->on_io_close_complete_context = callback_context;
850
851 if (xio_close(tls_io_instance->socket_io, on_underlying_io_close_complete, tls_io_instance) != 0)
852 {
853 LogError("xio_close failed.");
854 result = MU_FAILURE;
855 }
856 else
857 {
858 result = 0;
859 }
860 }
861 }
862
863 return result;
864 }
865
866 int tlsio_wolfssl_send(CONCRETE_IO_HANDLE tls_io, const void* buffer, size_t size, ON_SEND_COMPLETE on_send_complete, void* callback_context)
867 {
868 int result;
869
870 if (tls_io == NULL || buffer == NULL || size == 0)
871 {
872 LogError("Invalid parameter specified tls_io: %p, buffer: %p, size: %ul", tls_io, buffer, (unsigned int)size);
873 result = MU_FAILURE;
874 }
875 else
876 {
877 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io;
878
879 if (tls_io_instance->tlsio_state != TLSIO_STATE_OPEN)
880 {
881 LogError("send called while not open");
882 result = MU_FAILURE;
883 }
884 else
885 {
886 tls_io_instance->on_send_complete = on_send_complete;
887 tls_io_instance->on_send_complete_callback_context = callback_context;
888
889 int res = wolfSSL_write(tls_io_instance->ssl, buffer, size);
890 if ((res < 0) || ((size_t)res != size)) // Best way I can think of to safely compare an int to a size_t
891 {
892 LogError("Error writing data through WolfSSL");
893 result = MU_FAILURE;
894 }
895 else
896 {
897 result = 0;
898 }
899
900 // remove on send complete and callback context
901 tls_io_instance->on_send_complete = NULL;
902 tls_io_instance->on_send_complete_callback_context = NULL;
903 }
904 }
905
906 return result;
907 }
908
909 void tlsio_wolfssl_dowork(CONCRETE_IO_HANDLE tls_io)
910 {
911 if (tls_io == NULL)
912 {
913 LogError("NULL tls_io");
914 }
915 else
916 {
917 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io;
918 if (tls_io_instance->tlsio_state == TLSIO_STATE_IN_HANDSHAKE ||
919 tls_io_instance->tlsio_state == TLSIO_STATE_OPEN ||
920 tls_io_instance->tlsio_state == TLSIO_STATE_CLOSING)
921 {
922 decode_ssl_received_bytes(tls_io_instance);
923 }
924
925 xio_dowork(tls_io_instance->socket_io);
926 }
927 }
928
929
930 static int process_option(char** destination, const char* name, const char* value)
931 {
932
933 (void) name;
934
935 int result;
936 if (*destination != NULL)
937 {
938 free(*destination);
939 *destination = NULL;
940 }
941 if (mallocAndStrcpy_s(destination, value) != 0)
942 {
943 LogError("unable to process option %s",name);
944 result = MU_FAILURE;
945 }
946 else
947 {
948 result = 0;
949 }
950 return result;
951 }
952
953 #if defined(LIBWOLFSSL_VERSION_HEX) && LIBWOLFSSL_VERSION_HEX >= 0x04000000
954 static void logging_callback(const int logLevel, const char *const logMessage)
955 {
956 if (logLevel == ERROR_LOG)
957 {
958 LogError("tlsio_wolfssl: %s", logMessage);
959 }
960 else
961 {
962 LogInfo("tlsio_wolfssl: %s", logMessage);
963 }
964 }
965 #endif // LIBWOLFSSL_VERSION_HEX >= 0x04000000
966
967 int tlsio_wolfssl_setoption(CONCRETE_IO_HANDLE tls_io, const char* optionName, const void* value)
968 {
969 int result;
970
971 if (tls_io == NULL || optionName == NULL)
972 {
973 LogError("Bad arguments, tls_io = %p, optionName = %p", tls_io, optionName);
974 result = MU_FAILURE;
975 }
976 else
977 {
978 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io;
979
980 if (strcmp(OPTION_TRUSTED_CERT, optionName) == 0)
981 {
982 result = process_option(&tls_io_instance->certificate, optionName, value);
983 }
984 else if (strcmp(SU_OPTION_X509_CERT, optionName) == 0 || strcmp(OPTION_X509_ECC_CERT, optionName) == 0)
985 {
986 result = process_option(&tls_io_instance->x509certificate, optionName, value);
987 }
988 else if (strcmp(SU_OPTION_X509_PRIVATE_KEY, optionName) == 0 || strcmp(OPTION_X509_ECC_KEY, optionName) == 0)
989 {
990 result = process_option(&tls_io_instance->x509privatekey, optionName, value);
991 }
992 else if (strcmp(optionName, OPTION_SET_TLS_RENEGOTIATION) == 0)
993 {
994 // No need to do anything for WolfSSL
995 result = 0;
996 }
997 #ifdef INVALID_DEVID
998 else if (strcmp(OPTION_WOLFSSL_SET_DEVICE_ID, optionName) == 0)
999 {
1000 int device_id = *((int *)value);
1001 if (tls_io_instance->ssl != NULL && wolfSSL_SetDevId(tls_io_instance->ssl, device_id) != WOLFSSL_SUCCESS)
1002 {
1003 LogError("Failure setting device id on ssl");
1004 result = MU_FAILURE;
1005 }
1006 else
1007 {
1008 // Save the device Id even if ssl object not yet created.
1009 tls_io_instance->wolfssl_device_id = device_id;
1010 result = 0;
1011 }
1012 }
1013 #endif
1014 else if (strcmp("ignore_host_name_check", optionName) == 0)
1015 {
1016 bool* server_name_check = (bool*)value;
1017 tls_io_instance->ignore_host_name_check = *server_name_check;
1018 result = 0;
1019 }
1020 #if defined(LIBWOLFSSL_VERSION_HEX) && LIBWOLFSSL_VERSION_HEX >= 0x04000000
1021 else if (strcmp("debug_log", optionName) == 0)
1022 {
1023 bool* enable_debug_logging = (bool*)value;
1024 if (enable_debug_logging)
1025 {
1026 if (!wolfSSL_Debugging_ON())
1027 {
1028 LogError("wolfSSL_Debugging_ON failed.");
1029 result = MU_FAILURE;
1030 }
1031 else if (!wolfSSL_SetLoggingCb(&logging_callback))
1032 {
1033 LogError("wolfSSL_SetLoggingCb failed.");
1034 result = MU_FAILURE;
1035 }
1036 else
1037 {
1038 result = 0;
1039 }
1040 }
1041 else
1042 {
1043 LogError("tlsio WolfSSL layer does not support disabling debug_logs after they have been enabled.");
1044 result = MU_FAILURE;
1045 }
1046 }
1047 #endif // LIBWOLFSSL_VERSION_HEX >= 0x04000000
1048 else
1049 {
1050 if (tls_io_instance->socket_io == NULL)
1051 {
1052 LogError("NULL underlying IO handle");
1053 result = MU_FAILURE;
1054 }
1055 else
1056 {
1057 result = xio_setoption(tls_io_instance->socket_io, optionName, value);
1058 }
1059 }
1060 }
1061
1062 return result;
1063 }
1064 const IO_INTERFACE_DESCRIPTION* tlsio_wolfssl_get_interface_description(void)
1065 {
1066 return &tlsio_wolfssl_interface_description;
1067 }