Changeset 471 for azure_iot_hub_f767zi
- Timestamp:
- Jun 27, 2021, 10:17:43 PM (2 years ago)
- Location:
- azure_iot_hub_f767zi/trunk/azure_iot_sdk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
azure_iot_hub_f767zi/trunk/azure_iot_sdk/c-utility/adapters/httpapi_compact.c
r464 r471 7 7 #include <string.h> 8 8 #include <limits.h> 9 10 #include "azure_macro_utils/macro_utils.h"11 9 #include "azure_c_shared_utility/gballoc.h" 12 10 #include "azure_c_shared_utility/httpheaders.h" … … 51 49 char* x509ClientCertificate; 52 50 char* x509ClientPrivateKey; 51 const char* proxy_host; 52 int proxy_port; 53 const char* proxy_username; 54 const char* proxy_password; 53 55 XIO_HANDLE xio_handle; 54 56 size_t received_bytes_count; … … 201 203 { 202 204 HTTP_HANDLE_DATA* http_instance; 203 TLSIO_CONFIG tlsio_config;204 205 205 206 if (hostName == NULL) … … 223 224 LogError("There is no memory to control the http connection"); 224 225 } 225 else if (mallocAndStrcpy_s(&http_instance->hostName, hostName) != 0)226 { 227 LogError(" Failed copying hostname");226 else if(mallocAndStrcpy_s((char**)&(http_instance->hostName), hostName) != 0) 227 { 228 LogError("failure allocate host name"); 228 229 free(http_instance); 229 230 http_instance = NULL; … … 231 232 else 232 233 { 233 tlsio_config.hostname = http_instance->hostName; 234 tlsio_config.port = 443; 235 tlsio_config.underlying_io_interface = NULL; 236 tlsio_config.underlying_io_parameters = NULL; 237 238 http_instance->xio_handle = xio_create(platform_get_default_tlsio(), (void*)&tlsio_config); 239 240 /*Codes_SRS_HTTPAPI_COMPACT_21_016: [ If the HTTPAPI_CreateConnection failed to create the connection, it shall return NULL as the handle. ]*/ 241 if (http_instance->xio_handle == NULL) 242 { 243 LogError("Create connection failed"); 244 free(http_instance->hostName); 245 free(http_instance); 246 http_instance = NULL; 247 } 248 else 249 { 250 http_instance->is_connected = 0; 251 http_instance->is_io_error = 0; 252 http_instance->received_bytes_count = 0; 253 http_instance->received_bytes = NULL; 254 http_instance->certificate = NULL; 255 http_instance->x509ClientCertificate = NULL; 256 http_instance->x509ClientPrivateKey = NULL; 257 } 234 http_instance->xio_handle = NULL; 235 http_instance->is_connected = 0; 236 http_instance->is_io_error = 0; 237 http_instance->received_bytes_count = 0; 238 http_instance->received_bytes = NULL; 239 http_instance->certificate = NULL; 240 http_instance->x509ClientCertificate = NULL; 241 http_instance->x509ClientPrivateKey = NULL; 242 http_instance->proxy_host = NULL; 243 http_instance->proxy_port = 0; 244 http_instance->proxy_username = NULL; 245 http_instance->proxy_password = NULL; 258 246 } 259 247 } … … 321 309 } 322 310 311 if (http_instance->hostName != NULL) 312 { 313 free((void*)http_instance->hostName); 314 } 315 323 316 #ifndef DO_NOT_COPY_TRUSTED_CERTS_STRING 324 317 /*Codes_SRS_HTTPAPI_COMPACT_21_018: [ If there is a certificate associated to this connection, the HTTPAPI_CloseConnection shall free all allocated memory for the certificate. ]*/ … … 341 334 } 342 335 343 if (http_instance->hostName) 344 { 345 free(http_instance->hostName); 336 if (http_instance->proxy_host != NULL) 337 { 338 free((void*)http_instance->proxy_host); 339 } 340 if (http_instance->proxy_username != NULL) 341 { 342 free((void*)http_instance->proxy_username); 343 } 344 if (http_instance->proxy_password != NULL) 345 { 346 free((void*)http_instance->proxy_password); 346 347 } 347 348 … … 711 712 { 712 713 HTTPAPI_RESULT result; 714 TLSIO_CONFIG tlsio_config; 715 HTTP_PROXY_IO_CONFIG http_proxy_io_config; 713 716 714 717 if (http_instance->is_connected != 0) … … 720 723 { 721 724 http_instance->is_io_error = 0; 722 725 tlsio_config.hostname = http_instance->hostName; 726 tlsio_config.port = 443; 727 728 if (http_instance->proxy_host != NULL) { 729 tlsio_config.underlying_io_interface = http_proxy_io_get_interface_description(); 730 } 731 else { 732 tlsio_config.underlying_io_interface = NULL; 733 } 734 735 if (tlsio_config.underlying_io_interface != NULL) { 736 tlsio_config.underlying_io_parameters = (void*)&http_proxy_io_config; 737 738 http_proxy_io_config.proxy_hostname = http_instance->proxy_host; 739 http_proxy_io_config.proxy_port = http_instance->proxy_port; 740 http_proxy_io_config.username = http_instance->proxy_username; 741 http_proxy_io_config.password = http_instance->proxy_password; 742 http_proxy_io_config.hostname = http_instance->hostName; 743 http_proxy_io_config.port = 443; 744 } 745 else { 746 tlsio_config.underlying_io_parameters = NULL; 747 } 748 749 http_instance->xio_handle = xio_create(platform_get_default_tlsio(), (void*)&tlsio_config); 750 751 if (http_instance->xio_handle == NULL) 752 { 753 LogError("Create connection failed"); 754 free(http_instance); 755 http_instance = NULL; 756 } 723 757 /*Codes_SRS_HTTPAPI_COMPACT_21_022: [ If a Certificate was provided, the HTTPAPI_ExecuteRequest shall set this option on the transport layer. ]*/ 724 if ((http_instance->certificate != NULL) &&758 else if ((http_instance->certificate != NULL) && 725 759 (xio_setoption(http_instance->xio_handle, OPTION_TRUSTED_CERT, http_instance->certificate) != 0)) 726 760 { … … 740 774 (xio_setoption(http_instance->xio_handle, SU_OPTION_X509_PRIVATE_KEY, http_instance->x509ClientPrivateKey) != 0)) 741 775 { 742 743 776 /*Codes_SRS_HTTPAPI_COMPACT_06_006: [ If the transport failed setting the client certificate private key, the HTTPAPI_ExecuteRequest shall not send any request and return HTTPAPI_SET_OPTION_FAILED. ] */ 744 777 result = HTTPAPI_SET_OPTION_FAILED; … … 764 797 { 765 798 xio_dowork(http_instance->xio_handle); 766 LogInfo("Waiting for TLS connection");799 //LogInfo("Waiting for TLS connection"); 767 800 if ((countRetry--) < 0) 768 801 { … … 838 871 const char* get_request_type(HTTPAPI_REQUEST_TYPE requestType) 839 872 { 840 return (const char*)httpapiRequestString[requestType - HTTPAPI_REQUEST_GET];873 return (const char*)httpapiRequestString[requestType]; 841 874 } 842 875 … … 1220 1253 { 1221 1254 result = HTTPAPI_INVALID_ARG; 1222 LogError("(result = % " PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));1255 LogError("(result = %s)", MU_ENUM_TO_STRING(HTTPAPI_RESULT, result)); 1223 1256 } 1224 1257 /*Codes_SRS_HTTPAPI_COMPACT_21_024: [ The HTTPAPI_ExecuteRequest shall open the transport connection with the host to send the request. ]*/ 1225 1258 else if ((result = OpenXIOConnection(http_instance)) != HTTPAPI_OK) 1226 1259 { 1227 LogError("Open HTTP connection failed (result = % " PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));1260 LogError("Open HTTP connection failed (result = %s)", MU_ENUM_TO_STRING(HTTPAPI_RESULT, result)); 1228 1261 } 1229 1262 /*Codes_SRS_HTTPAPI_COMPACT_21_026: [ If the open process succeed, the HTTPAPI_ExecuteRequest shall send the request message to the host. ]*/ 1230 1263 else if ((result = SendHeadsToXIO(http_instance, requestType, relativePath, httpHeadersHandle, headersCount)) != HTTPAPI_OK) 1231 1264 { 1232 LogError("Send heads to HTTP failed (result = % " PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));1265 LogError("Send heads to HTTP failed (result = %s)", MU_ENUM_TO_STRING(HTTPAPI_RESULT, result)); 1233 1266 } 1234 1267 /*Codes_SRS_HTTPAPI_COMPACT_21_042: [ The request can contain the a content message, provided in content parameter. ]*/ 1235 1268 else if ((result = SendContentToXIO(http_instance, content, contentLength)) != HTTPAPI_OK) 1236 1269 { 1237 LogError("Send content to HTTP failed (result = % " PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));1270 LogError("Send content to HTTP failed (result = %s)", MU_ENUM_TO_STRING(HTTPAPI_RESULT, result)); 1238 1271 } 1239 1272 /*Codes_SRS_HTTPAPI_COMPACT_21_030: [ At the end of the transmission, the HTTPAPI_ExecuteRequest shall receive the response from the host. ]*/ … … 1241 1274 else if ((result = ReceiveHeaderFromXIO(http_instance, statusCode)) != HTTPAPI_OK) 1242 1275 { 1243 LogError("Receive header from HTTP failed (result = % " PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));1276 LogError("Receive header from HTTP failed (result = %s)", MU_ENUM_TO_STRING(HTTPAPI_RESULT, result)); 1244 1277 } 1245 1278 /*Codes_SRS_HTTPAPI_COMPACT_21_074: [ After the header, the message received by the HTTPAPI_ExecuteRequest can contain addition information about the content. ]*/ 1246 1279 else if ((result = ReceiveContentInfoFromXIO(http_instance, responseHeadersHandle, &bodyLength, &chunked)) != HTTPAPI_OK) 1247 1280 { 1248 LogError("Receive content information from HTTP failed (result = % " PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));1281 LogError("Receive content information from HTTP failed (result = %s)", MU_ENUM_TO_STRING(HTTPAPI_RESULT, result)); 1249 1282 } 1250 1283 /*Codes_SRS_HTTPAPI_COMPACT_42_084: [ The message received by the HTTPAPI_ExecuteRequest should not contain http body. ]*/ … … 1254 1287 if ((result = ReadHTTPResponseBodyFromXIO(http_instance, bodyLength, chunked, responseContent)) != HTTPAPI_OK) 1255 1288 { 1256 LogError("Read HTTP response body from HTTP failed (result = % " PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));1289 LogError("Read HTTP response body from HTTP failed (result = %s)", MU_ENUM_TO_STRING(HTTPAPI_RESULT, result)); 1257 1290 } 1258 1291 } … … 1311 1344 #endif // DO_NOT_COPY_TRUSTED_CERTS_STRING 1312 1345 } 1313 else if (strcmp(SU_OPTION_X509_CERT, optionName) == 0 || strcmp(OPTION_X509_ECC_CERT, optionName) == 0)1346 else if (strcmp(SU_OPTION_X509_CERT, optionName) == 0) 1314 1347 { 1315 1348 int len; … … 1334 1367 } 1335 1368 } 1336 else if (strcmp(SU_OPTION_X509_PRIVATE_KEY, optionName) == 0 || strcmp(OPTION_X509_ECC_KEY, optionName) == 0)1369 else if (strcmp(SU_OPTION_X509_PRIVATE_KEY, optionName) == 0) 1337 1370 { 1338 1371 int len; … … 1359 1392 else if (strcmp(OPTION_HTTP_PROXY, optionName) == 0) 1360 1393 { 1361 TLSIO_CONFIG tlsio_config; 1362 HTTP_PROXY_IO_CONFIG proxy_config; 1363 HTTP_PROXY_OPTIONS* proxy_options = (HTTP_PROXY_OPTIONS*)value; 1364 1365 if (proxy_options->host_address == NULL) 1366 { 1367 LogError("NULL host_address in proxy options"); 1394 HTTP_PROXY_OPTIONS* proxy_data = (HTTP_PROXY_OPTIONS*)value; 1395 if (proxy_data->host_address == NULL || proxy_data->port <= 0) 1396 { 1397 LogError("invalid proxy_data values ( host_address = %p, port = %d)", proxy_data->host_address, proxy_data->port); 1368 1398 result = HTTPAPI_ERROR; 1369 1399 } 1370 else if (((proxy_options->username == NULL) || (proxy_options->password == NULL)) &&1371 (proxy_options->username != proxy_options->password))1372 {1373 LogError("Only one of username and password for proxy settings was NULL");1374 result = HTTPAPI_ERROR;1375 }1376 1400 else 1377 1401 { 1378 1379 /* Workaround: xio interface is already created when HTTPAPI_CreateConnection is call without proxy support1380 * need to destroy the interface and create a new one with proxy information1381 */1382 OPTIONHANDLER_HANDLE xio_options;1383 if ((xio_options = xio_retrieveoptions(http_instance->xio_handle)) == NULL)1384 { 1385 LogError("fail ed saving underlying I/O transport options");1402 if(http_instance->proxy_host != NULL) 1403 { 1404 free((void*)http_instance->proxy_host); 1405 http_instance->proxy_host = NULL; 1406 } 1407 if(mallocAndStrcpy_s((char**)&(http_instance->proxy_host), (const char*)proxy_data->host_address) != 0) 1408 { 1409 LogError("failure allocate proxy host"); 1386 1410 result = HTTPAPI_ERROR; 1387 1411 } 1388 1412 else 1389 1413 { 1390 xio_destroy(http_instance->xio_handle); 1391 1392 proxy_config.hostname = http_instance->hostName; 1393 proxy_config.proxy_hostname = proxy_options->host_address; 1394 proxy_config.password = proxy_options->password; 1395 proxy_config.username = proxy_options->username; 1396 proxy_config.proxy_port = proxy_options->port; 1397 proxy_config.port = 443; 1398 1399 tlsio_config.hostname = http_instance->hostName; 1400 tlsio_config.port = 443; 1401 tlsio_config.underlying_io_interface = http_proxy_io_get_interface_description(); 1402 tlsio_config.underlying_io_parameters = &proxy_config; 1403 1404 http_instance->xio_handle = xio_create(platform_get_default_tlsio(), (void*)&tlsio_config); 1405 1406 if (http_instance->xio_handle == NULL) 1407 { 1408 LogError("Failed to create xio handle with proxy configuration"); 1409 result = HTTPAPI_ERROR; 1414 http_instance->proxy_port = proxy_data->port; 1415 1416 if (proxy_data->username != NULL && proxy_data->password != NULL) 1417 { 1418 if(http_instance->proxy_username != NULL) 1419 { 1420 free((void*)http_instance->proxy_username); 1421 http_instance->proxy_username = NULL; 1422 } 1423 if(mallocAndStrcpy_s((char**)&(http_instance->proxy_username), (const char*)proxy_data->username) != 0) 1424 { 1425 LogError("failure allocate proxy username"); 1426 free((void*)http_instance->proxy_host); 1427 http_instance->proxy_host = NULL; 1428 result = HTTPAPI_ERROR; 1429 } 1430 else 1431 { 1432 if(http_instance->proxy_password != NULL) 1433 { 1434 free((void*)http_instance->proxy_password); 1435 http_instance->proxy_password = NULL; 1436 } 1437 if(mallocAndStrcpy_s((char**)&(http_instance->proxy_password), (const char*)proxy_data->password) != 0) 1438 { 1439 LogError("failure allocate proxy password"); 1440 free((void*)http_instance->proxy_host); 1441 http_instance->proxy_host = NULL; 1442 free((void*)http_instance->proxy_username); 1443 http_instance->proxy_username = NULL; 1444 result = HTTPAPI_ERROR; 1445 } 1446 else 1447 { 1448 result = HTTPAPI_OK; 1449 } 1450 } 1410 1451 } 1411 1452 else 1412 1453 { 1413 if (OptionHandler_FeedOptions(xio_options, http_instance->xio_handle) != OPTIONHANDLER_OK) 1414 { 1415 LogError("Failed feeding existing options to new xio instance."); 1416 result = HTTPAPI_ERROR; 1417 } 1418 else 1419 { 1420 result = HTTPAPI_OK; 1421 } 1422 } 1423 1424 OptionHandler_Destroy(xio_options); 1454 result = HTTPAPI_OK; 1455 } 1425 1456 } 1426 1457 } … … 1432 1463 LogInfo("unknown option %s", optionName); 1433 1464 } 1434 1435 1465 return result; 1436 1466 } … … 1477 1507 #endif // DO_NOT_COPY_TRUSTED_CERTS_STRING 1478 1508 } 1479 else if (strcmp(SU_OPTION_X509_CERT, optionName) == 0 || strcmp(OPTION_X509_ECC_CERT, optionName) == 0)1509 else if (strcmp(SU_OPTION_X509_CERT, optionName) == 0) 1480 1510 { 1481 1511 certLen = strlen((const char*)value); … … 1494 1524 } 1495 1525 } 1496 else if (strcmp(SU_OPTION_X509_PRIVATE_KEY, optionName) == 0 || strcmp(OPTION_X509_ECC_KEY, optionName) == 0)1526 else if (strcmp(SU_OPTION_X509_PRIVATE_KEY, optionName) == 0) 1497 1527 { 1498 1528 certLen = strlen((const char*)value); -
azure_iot_hub_f767zi/trunk/azure_iot_sdk/c-utility/adapters/tlsio_wolfssl.c
r464 r471 52 52 char* x509privatekey; 53 53 int wolfssl_device_id; 54 char* hostname; 55 bool ignore_host_name_check; 54 size_t socket_reads; 56 55 } TLS_IO_INSTANCE; 57 56 58 57 STATIC_VAR_UNUSED const char* const OPTION_WOLFSSL_SET_DEVICE_ID = "SetDeviceId"; 59 static const size_t SOCKET_READ_LIMIT = 5;58 static const size_t SOCKET_READ_LIMIT = 10000; // 10,000 ms ? 60 59 61 60 /*this function will clone an option given by name and value*/ … … 106 105 } 107 106 } 108 #ifdef INVALID_DEVID109 else if(strcmp(name, OPTION_WOLFSSL_SET_DEVICE_ID) == 0 )110 {111 int* value_clone;112 113 if ((value_clone = malloc(sizeof(int))) == NULL)114 {115 LogError("unable to clone device id option");116 }117 else118 {119 *value_clone = *(int*)value;120 }121 122 result = value_clone;123 }124 #endif125 107 else 126 108 { … … 144 126 if ((strcmp(name, OPTION_TRUSTED_CERT) == 0) || 145 127 (strcmp(name, SU_OPTION_X509_CERT) == 0) || 146 (strcmp(name, SU_OPTION_X509_PRIVATE_KEY) == 0) || 147 (strcmp(name, OPTION_WOLFSSL_SET_DEVICE_ID) == 0)) 128 (strcmp(name, SU_OPTION_X509_PRIVATE_KEY) == 0)) 148 129 { 149 130 free((void*)value); … … 203 184 result = NULL; 204 185 } 205 #ifdef INVALID_DEVID206 else if (207 (tls_io_instance->wolfssl_device_id != INVALID_DEVID) &&208 (OptionHandler_AddOption(result, OPTION_WOLFSSL_SET_DEVICE_ID, &tls_io_instance->wolfssl_device_id) != OPTIONHANDLER_OK)209 )210 {211 LogError("unable to save deviceid option");212 OptionHandler_Destroy(result);213 result = NULL;214 }215 #endif216 186 else 217 187 { … … 287 257 int res; 288 258 tls_io_instance->tlsio_state = TLSIO_STATE_IN_HANDSHAKE; 259 tls_io_instance->socket_reads = 0; 289 260 290 261 res = wolfSSL_connect(tls_io_instance->ssl); 291 262 if (res != SSL_SUCCESS) 292 263 { 293 // Error codes explained in https://www.wolfssl.com/docs/wolfssl-manual/appendix-c/ 294 LogError("WolfSSL connect failed (%d)", wolfSSL_get_error(tls_io_instance->ssl, res)); 264 LogError("WolfSSL connect failed"); 295 265 indicate_open_complete(tls_io_instance, IO_OPEN_ERROR); 296 266 tls_io_instance->tlsio_state = TLSIO_STATE_ERROR; … … 384 354 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context; 385 355 unsigned char* new_socket_io_read_bytes; 386 size_t socket_reads = 0;387 356 388 357 AZURE_UNREFERENCED_PARAMETER(ssl); 389 while (tls_io_instance->socket_io_read_byte_count == 0 && socket_reads < SOCKET_READ_LIMIT) 390 { 358 if (tls_io_instance->socket_io_read_byte_count == 0) 359 { 360 if (tls_io_instance->socket_reads >= SOCKET_READ_LIMIT) { 361 return WOLFSSL_CBIO_ERR_TIMEOUT; 362 } 391 363 xio_dowork(tls_io_instance->socket_io); 392 if (tls_io_instance->tlsio_state != TLSIO_STATE_IN_HANDSHAKE) 393 { 394 break; 395 } 396 socket_reads++; 364 if (tls_io_instance->tlsio_state == TLSIO_STATE_IN_HANDSHAKE) 365 { 366 tls_io_instance->socket_reads++; 367 ThreadAPI_Sleep(1); 368 return 0; 369 } 397 370 } 398 371 … … 443 416 } 444 417 418 static void on_send_complete(void* context, IO_SEND_RESULT send_result) 419 { 420 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context; 421 if ((tls_io_instance == NULL) || (tls_io_instance->on_send_complete == NULL)) 422 return; 423 424 tls_io_instance->on_send_complete(tls_io_instance->on_send_complete_callback_context, send_result); 425 426 tls_io_instance->on_send_complete = NULL; 427 tls_io_instance->on_send_complete_callback_context = NULL; 428 } 429 445 430 static int on_io_send(WOLFSSL *ssl, char *buf, int sz, void *context) 446 431 { … … 450 435 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context; 451 436 452 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)437 if (xio_send(tls_io_instance->socket_io, buf, sz, on_send_complete, tls_io_instance) != 0) 453 438 { 454 439 LogError("Failed sending bytes through underlying IO"); 455 440 tls_io_instance->tlsio_state = TLSIO_STATE_ERROR; 456 441 indicate_error(tls_io_instance); 457 result = WOLFSSL_CBIO_ERR_GENERAL;442 result = -1; 458 443 } 459 444 else … … 469 454 AZURE_UNREFERENCED_PARAMETER(ssl); 470 455 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context; 471 if (tls_io_instance->tlsio_state != TLSIO_STATE_IN_HANDSHAKE) 456 if (tls_io_instance->tlsio_state == TLSIO_STATE_OPEN) { 457 } 458 else if (tls_io_instance->tlsio_state != TLSIO_STATE_IN_HANDSHAKE) 472 459 { 473 460 LogInfo("on_handshake_done called when not in IN_HANDSHAKE state"); … … 569 556 } 570 557 571 static int enable_domain_check(TLS_IO_INSTANCE* tls_io_instance)572 {573 int result = 0;574 575 if (!tls_io_instance->ignore_host_name_check)576 {577 if (wolfSSL_check_domain_name(tls_io_instance->ssl, tls_io_instance->hostname) != WOLFSSL_SUCCESS)578 {579 result = MU_FAILURE;580 }581 }582 583 return result;584 }585 586 558 static int prepare_wolfssl_open(TLS_IO_INSTANCE* tls_io_instance) 587 559 { 588 560 int result; 589 590 if (enable_domain_check(tls_io_instance)) 591 { 592 LogError("Failed to configure domain name verification"); 593 result = MU_FAILURE; 594 } 595 else if (add_certificate_to_store(tls_io_instance) != 0) 561 if (add_certificate_to_store(tls_io_instance) != 0) 596 562 { 597 563 LogError("Failed to add certificates to store"); … … 607 573 result = MU_FAILURE; 608 574 } 575 #ifdef INVALID_DEVID 576 else if (tls_io_instance->wolfssl_device_id != INVALID_DEVID && wolfSSL_SetDevId(tls_io_instance->ssl, tls_io_instance->wolfssl_device_id) != WOLFSSL_SUCCESS) 577 { 578 LogError("Failure setting device id"); 579 result = MU_FAILURE; 580 } 581 #endif 609 582 else 610 583 { … … 653 626 { 654 627 LogError("Cannot create the wolfSSL context"); 655 free(result);656 result = NULL;657 }658 else if (mallocAndStrcpy_s(&result->hostname, tls_io_config->hostname) != 0)659 {660 LogError("Failed copying the target hostname.");661 628 free(result); 662 629 result = NULL; … … 691 658 LogError("Failed getting socket IO interface description."); 692 659 wolfSSL_CTX_free(result->ssl_context); 693 free(result->hostname);694 660 free(result); 695 661 result = NULL; … … 702 668 LogError("Failure connecting to underlying socket_io"); 703 669 wolfSSL_CTX_free(result->ssl_context); 704 free(result->hostname);705 670 free(result); 706 671 result = NULL; … … 710 675 LogError("Failure connecting to underlying socket_io"); 711 676 wolfSSL_CTX_free(result->ssl_context); 712 free(result->hostname);713 677 free(result); 714 678 result = NULL; … … 753 717 754 718 xio_destroy(tls_io_instance->socket_io); 755 free(tls_io_instance->hostname);756 719 free(tls_io); 757 720 } … … 803 766 else 804 767 { 805 // The state can get changed in the on_underlying_io_open_complete 806 if (tls_io_instance->tlsio_state != TLSIO_STATE_OPEN) 768 result = 0; 769 } 770 } 771 } 772 773 return result; 774 } 775 776 int tlsio_wolfssl_close(CONCRETE_IO_HANDLE tls_io, ON_IO_CLOSE_COMPLETE on_io_close_complete, void* callback_context) 777 { 778 int result = 0; 779 780 if (tls_io == NULL) 781 { 782 LogError("NULL tls_io handle."); 783 result = MU_FAILURE; 784 } 785 else 786 { 787 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; 788 789 if ((tls_io_instance->tlsio_state == TLSIO_STATE_NOT_OPEN) || 790 (tls_io_instance->tlsio_state == TLSIO_STATE_CLOSING)) 791 { 792 LogError("Close called while not open."); 793 result = MU_FAILURE; 794 } 795 else 796 { 797 tls_io_instance->tlsio_state = TLSIO_STATE_CLOSING; 798 tls_io_instance->on_io_close_complete = on_io_close_complete; 799 tls_io_instance->on_io_close_complete_context = callback_context; 800 801 if (xio_close(tls_io_instance->socket_io, on_underlying_io_close_complete, tls_io_instance) != 0) 802 { 803 LogError("xio_close failed."); 804 result = MU_FAILURE; 805 } 806 else 807 { 808 result = 0; 809 } 810 } 811 } 812 813 return result; 814 } 815 816 int tlsio_wolfssl_send(CONCRETE_IO_HANDLE tls_io, const void* buffer, size_t size, ON_SEND_COMPLETE on_send_complete, void* callback_context) 817 { 818 int result; 819 820 if (tls_io == NULL || buffer == NULL || size == 0) 821 { 822 LogError("Invalid parameter specified tls_io: %p, buffer: %p, size: %ul", tls_io, buffer, (unsigned int)size); 823 result = MU_FAILURE; 824 } 825 else 826 { 827 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; 828 829 if (tls_io_instance->tlsio_state != TLSIO_STATE_OPEN) 830 { 831 LogError("send called while not open"); 832 result = MU_FAILURE; 833 } 834 if (tls_io_instance->on_send_complete != NULL) 835 { 836 LogError("Error writing data"); 837 result = MU_FAILURE; 838 } 839 else 840 { 841 tls_io_instance->on_send_complete = on_send_complete; 842 tls_io_instance->on_send_complete_callback_context = callback_context; 843 844 int res = wolfSSL_write(tls_io_instance->ssl, buffer, size); 845 if ((res < 0) || ((size_t)res != size)) // Best way I can think of to safely compare an int to a size_t 846 { 847 LogError("Error writing data through WolfSSL"); 848 result = MU_FAILURE; 849 } 850 else 851 { 852 result = 0; 853 } 854 } 855 } 856 857 return result; 858 } 859 860 void tlsio_wolfssl_dowork(CONCRETE_IO_HANDLE tls_io) 861 { 862 if (tls_io == NULL) 863 { 864 LogError("NULL tls_io"); 865 } 866 else 867 { 868 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; 869 870 if ((tls_io_instance->tlsio_state != TLSIO_STATE_NOT_OPEN) && 871 (tls_io_instance->tlsio_state != TLSIO_STATE_ERROR)) 872 { 873 if (tls_io_instance->tlsio_state != TLSIO_STATE_OPENING_UNDERLYING_IO) 874 decode_ssl_received_bytes(tls_io_instance); 875 xio_dowork(tls_io_instance->socket_io); 876 } 877 } 878 } 879 880 881 static int process_option(char** destination, const char* name, const char* value) 882 { 883 int result; 884 if (*destination != NULL) 885 { 886 free(*destination); 887 *destination = NULL; 888 } 889 if (mallocAndStrcpy_s(destination, value) != 0) 890 { 891 LogError("unable to process option %s",name); 892 result = MU_FAILURE; 893 } 894 else 895 { 896 result = 0; 897 } 898 return result; 899 } 900 901 int tlsio_wolfssl_setoption(CONCRETE_IO_HANDLE tls_io, const char* optionName, const void* value) 902 { 903 int result; 904 905 if (tls_io == NULL || optionName == NULL) 906 { 907 LogError("Bad arguments, tls_io = %p, optionName = %p", tls_io, optionName); 908 result = MU_FAILURE; 909 } 910 else 911 { 912 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; 913 914 if (strcmp(OPTION_TRUSTED_CERT, optionName) == 0) 915 { 916 result = process_option(&tls_io_instance->certificate, optionName, value); 917 } 918 else if (strcmp(SU_OPTION_X509_CERT, optionName) == 0 || strcmp(OPTION_X509_ECC_CERT, optionName) == 0) 919 { 920 result = process_option(&tls_io_instance->x509certificate, optionName, value); 921 } 922 else if (strcmp(SU_OPTION_X509_PRIVATE_KEY, optionName) == 0 || strcmp(OPTION_X509_ECC_KEY, optionName) == 0) 923 { 924 result = process_option(&tls_io_instance->x509privatekey, optionName, value); 925 } 926 else if (strcmp(optionName, OPTION_SET_TLS_RENEGOTIATION) == 0) 927 { 928 // No need to do anything for WolfSSL 929 result = 0; 930 } 931 #ifdef INVALID_DEVID 932 else if (strcmp(OPTION_WOLFSSL_SET_DEVICE_ID, optionName) == 0) 933 { 934 int device_id = *((int *)value); 935 if (tls_io_instance->ssl != NULL) 936 { 937 if (tls_io_instance->ssl != NULL && wolfSSL_SetDevId(tls_io_instance->ssl, device_id) != WOLFSSL_SUCCESS) 807 938 { 808 LogError("Fail ed to connect to server. The certificates may not be correct.");939 LogError("Failure setting device id on ssl"); 809 940 result = MU_FAILURE; 810 941 } … … 814 945 } 815 946 } 816 } 817 } 818 819 return result; 820 } 821 822 int tlsio_wolfssl_close(CONCRETE_IO_HANDLE tls_io, ON_IO_CLOSE_COMPLETE on_io_close_complete, void* callback_context) 823 { 824 int result = 0; 825 826 if (tls_io == NULL) 827 { 828 LogError("NULL tls_io handle."); 829 result = MU_FAILURE; 830 } 831 else 832 { 833 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; 834 835 if ((tls_io_instance->tlsio_state == TLSIO_STATE_NOT_OPEN) || 836 (tls_io_instance->tlsio_state == TLSIO_STATE_CLOSING)) 837 { 838 LogError("Close called while not open."); 839 result = MU_FAILURE; 840 } 841 else 842 { 843 tls_io_instance->tlsio_state = TLSIO_STATE_CLOSING; 844 tls_io_instance->on_io_close_complete = on_io_close_complete; 845 tls_io_instance->on_io_close_complete_context = callback_context; 846 847 if (xio_close(tls_io_instance->socket_io, on_underlying_io_close_complete, tls_io_instance) != 0) 848 { 849 LogError("xio_close failed."); 850 result = MU_FAILURE; 851 } 852 else 853 { 854 result = 0; 855 } 856 } 857 } 858 859 return result; 860 } 861 862 int tlsio_wolfssl_send(CONCRETE_IO_HANDLE tls_io, const void* buffer, size_t size, ON_SEND_COMPLETE on_send_complete, void* callback_context) 863 { 864 int result; 865 866 if (tls_io == NULL || buffer == NULL || size == 0) 867 { 868 LogError("Invalid parameter specified tls_io: %p, buffer: %p, size: %ul", tls_io, buffer, (unsigned int)size); 869 result = MU_FAILURE; 870 } 871 else 872 { 873 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; 874 875 if (tls_io_instance->tlsio_state != TLSIO_STATE_OPEN) 876 { 877 LogError("send called while not open"); 878 result = MU_FAILURE; 879 } 880 else 881 { 882 tls_io_instance->on_send_complete = on_send_complete; 883 tls_io_instance->on_send_complete_callback_context = callback_context; 884 885 int res = wolfSSL_write(tls_io_instance->ssl, buffer, size); 886 if ((res < 0) || ((size_t)res != size)) // Best way I can think of to safely compare an int to a size_t 887 { 888 LogError("Error writing data through WolfSSL"); 889 result = MU_FAILURE; 890 } 891 else 892 { 893 result = 0; 894 } 895 } 896 } 897 898 return result; 899 } 900 901 void tlsio_wolfssl_dowork(CONCRETE_IO_HANDLE tls_io) 902 { 903 if (tls_io == NULL) 904 { 905 LogError("NULL tls_io"); 906 } 907 else 908 { 909 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; 910 911 if ((tls_io_instance->tlsio_state != TLSIO_STATE_NOT_OPEN) && 912 (tls_io_instance->tlsio_state != TLSIO_STATE_ERROR)) 913 { 914 decode_ssl_received_bytes(tls_io_instance); 915 xio_dowork(tls_io_instance->socket_io); 916 } 917 } 918 } 919 920 921 static int process_option(char** destination, const char* name, const char* value) 922 { 923 924 (void) name; 925 926 int result; 927 if (*destination != NULL) 928 { 929 free(*destination); 930 *destination = NULL; 931 } 932 if (mallocAndStrcpy_s(destination, value) != 0) 933 { 934 LogError("unable to process option %s",name); 935 result = MU_FAILURE; 936 } 937 else 938 { 939 result = 0; 940 } 941 return result; 942 } 943 944 int tlsio_wolfssl_setoption(CONCRETE_IO_HANDLE tls_io, const char* optionName, const void* value) 945 { 946 int result; 947 948 if (tls_io == NULL || optionName == NULL) 949 { 950 LogError("Bad arguments, tls_io = %p, optionName = %p", tls_io, optionName); 951 result = MU_FAILURE; 952 } 953 else 954 { 955 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; 956 957 if (strcmp(OPTION_TRUSTED_CERT, optionName) == 0) 958 { 959 result = process_option(&tls_io_instance->certificate, optionName, value); 960 } 961 else if (strcmp(SU_OPTION_X509_CERT, optionName) == 0 || strcmp(OPTION_X509_ECC_CERT, optionName) == 0) 962 { 963 result = process_option(&tls_io_instance->x509certificate, optionName, value); 964 } 965 else if (strcmp(SU_OPTION_X509_PRIVATE_KEY, optionName) == 0 || strcmp(OPTION_X509_ECC_KEY, optionName) == 0) 966 { 967 result = process_option(&tls_io_instance->x509privatekey, optionName, value); 968 } 969 else if (strcmp(optionName, OPTION_SET_TLS_RENEGOTIATION) == 0) 970 { 971 // No need to do anything for WolfSSL 972 result = 0; 973 } 974 #ifdef INVALID_DEVID 975 else if (strcmp(OPTION_WOLFSSL_SET_DEVICE_ID, optionName) == 0) 976 { 977 int device_id = *((int *)value); 978 if (tls_io_instance->ssl != NULL && wolfSSL_SetDevId(tls_io_instance->ssl, device_id) != WOLFSSL_SUCCESS) 979 { 980 LogError("Failure setting device id on ssl"); 981 result = MU_FAILURE; 982 } 983 else 984 { 985 // Save the device Id even if ssl object not yet created. 947 else 948 { 949 // Save the id till we create the ssl object 986 950 tls_io_instance->wolfssl_device_id = device_id; 987 951 result = 0; … … 989 953 } 990 954 #endif 991 else if (strcmp("ignore_host_name_check", optionName) == 0)992 {993 bool* server_name_check = (bool*)value;994 tls_io_instance->ignore_host_name_check = *server_name_check;995 result = 0;996 }997 955 else 998 956 { -
azure_iot_hub_f767zi/trunk/azure_iot_sdk/serializer/src/commanddecoder.c
r457 r471 731 731 else 732 732 { 733 //printf("%s %s\n", fullMethodName, methodJSON); 734 if (strcmp(methodJSON, "null") == 0) { 735 strcpy(methodJSON, "{}"); 736 } 733 737 MULTITREE_HANDLE methodTree; 734 738 if (JSONDecoder_JSON_To_MultiTree(methodJSON, &methodTree) != JSON_DECODER_OK) -
azure_iot_hub_f767zi/trunk/azure_iot_sdk/serializer/src/jsondecoder.c
r457 r471 598 598 result = JSON_DECODER_MULTITREE_FAILED; 599 599 } 600 else if (strcmp(json, "null") == 0) { 601 result = JSON_DECODER_OK; 602 } 600 603 else 601 604 {
Note:
See TracChangeset
for help on using the changeset viewer.