Changeset 471


Ignore:
Timestamp:
Jun 27, 2021, 10:17:43 PM (3 years ago)
Author:
coas-nagasima
Message:

バージョンを上げたときに失われたAzure IoT SDKの変更部分を適用

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  
    77#include <string.h>
    88#include <limits.h>
    9 
    10 #include "azure_macro_utils/macro_utils.h"
    119#include "azure_c_shared_utility/gballoc.h"
    1210#include "azure_c_shared_utility/httpheaders.h"
     
    5149    char*           x509ClientCertificate;
    5250    char*           x509ClientPrivateKey;
     51    const char*     proxy_host;
     52    int             proxy_port;
     53    const char*     proxy_username;
     54    const char*     proxy_password;
    5355    XIO_HANDLE      xio_handle;
    5456    size_t          received_bytes_count;
     
    201203{
    202204    HTTP_HANDLE_DATA* http_instance;
    203     TLSIO_CONFIG tlsio_config;
    204205
    205206    if (hostName == NULL)
     
    223224            LogError("There is no memory to control the http connection");
    224225        }
    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");
    228229            free(http_instance);
    229230            http_instance = NULL;
     
    231232        else
    232233        {
    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;
    258246        }
    259247    }
     
    321309        }
    322310
     311        if (http_instance->hostName != NULL)
     312        {
     313            free((void*)http_instance->hostName);
     314        }
     315
    323316#ifndef DO_NOT_COPY_TRUSTED_CERTS_STRING
    324317        /*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. ]*/
     
    341334        }
    342335
    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);
    346347        }
    347348
     
    711712{
    712713    HTTPAPI_RESULT result;
     714    TLSIO_CONFIG tlsio_config;
     715    HTTP_PROXY_IO_CONFIG http_proxy_io_config;
    713716
    714717    if (http_instance->is_connected != 0)
     
    720723    {
    721724        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        }
    723757        /*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) &&
    725759            (xio_setoption(http_instance->xio_handle, OPTION_TRUSTED_CERT, http_instance->certificate) != 0))
    726760        {
     
    740774            (xio_setoption(http_instance->xio_handle, SU_OPTION_X509_PRIVATE_KEY, http_instance->x509ClientPrivateKey) != 0))
    741775        {
    742 
    743776            /*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. ] */
    744777            result = HTTPAPI_SET_OPTION_FAILED;
     
    764797                {
    765798                    xio_dowork(http_instance->xio_handle);
    766                     LogInfo("Waiting for TLS connection");
     799                    //LogInfo("Waiting for TLS connection");
    767800                    if ((countRetry--) < 0)
    768801                    {
     
    838871const char* get_request_type(HTTPAPI_REQUEST_TYPE requestType)
    839872{
    840     return (const char*)httpapiRequestString[requestType - HTTPAPI_REQUEST_GET];
     873    return (const char*)httpapiRequestString[requestType];
    841874}
    842875
     
    12201253    {
    12211254        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));
    12231256    }
    12241257    /*Codes_SRS_HTTPAPI_COMPACT_21_024: [ The HTTPAPI_ExecuteRequest shall open the transport connection with the host to send the request. ]*/
    12251258    else if ((result = OpenXIOConnection(http_instance)) != HTTPAPI_OK)
    12261259    {
    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));
    12281261    }
    12291262    /*Codes_SRS_HTTPAPI_COMPACT_21_026: [ If the open process succeed, the HTTPAPI_ExecuteRequest shall send the request message to the host. ]*/
    12301263    else if ((result = SendHeadsToXIO(http_instance, requestType, relativePath, httpHeadersHandle, headersCount)) != HTTPAPI_OK)
    12311264    {
    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));
    12331266    }
    12341267    /*Codes_SRS_HTTPAPI_COMPACT_21_042: [ The request can contain the a content message, provided in content parameter. ]*/
    12351268    else if ((result = SendContentToXIO(http_instance, content, contentLength)) != HTTPAPI_OK)
    12361269    {
    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));
    12381271    }
    12391272    /*Codes_SRS_HTTPAPI_COMPACT_21_030: [ At the end of the transmission, the HTTPAPI_ExecuteRequest shall receive the response from the host. ]*/
     
    12411274    else if ((result = ReceiveHeaderFromXIO(http_instance, statusCode)) != HTTPAPI_OK)
    12421275    {
    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));
    12441277    }
    12451278    /*Codes_SRS_HTTPAPI_COMPACT_21_074: [ After the header, the message received by the HTTPAPI_ExecuteRequest can contain addition information about the content. ]*/
    12461279    else if ((result = ReceiveContentInfoFromXIO(http_instance, responseHeadersHandle, &bodyLength, &chunked)) != HTTPAPI_OK)
    12471280    {
    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));
    12491282    }
    12501283    /*Codes_SRS_HTTPAPI_COMPACT_42_084: [ The message received by the HTTPAPI_ExecuteRequest should not contain http body. ]*/
     
    12541287        if ((result = ReadHTTPResponseBodyFromXIO(http_instance, bodyLength, chunked, responseContent)) != HTTPAPI_OK)
    12551288        {
    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));
    12571290        }
    12581291    }
     
    13111344#endif // DO_NOT_COPY_TRUSTED_CERTS_STRING
    13121345    }
    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)
    13141347    {
    13151348        int len;
     
    13341367        }
    13351368    }
    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)
    13371370    {
    13381371        int len;
     
    13591392    else if (strcmp(OPTION_HTTP_PROXY, optionName) == 0)
    13601393    {
    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);
    13681398            result = HTTPAPI_ERROR;
    13691399        }
    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         }
    13761400        else
    13771401        {
    1378 
    1379             /* Workaround: xio interface is already created when HTTPAPI_CreateConnection is call without proxy support
    1380              * need to destroy the interface and create a new one with proxy information
    1381              */
    1382             OPTIONHANDLER_HANDLE xio_options;
    1383             if ((xio_options = xio_retrieveoptions(http_instance->xio_handle)) == NULL)
    1384             {
    1385                 LogError("failed 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");
    13861410                result = HTTPAPI_ERROR;
    13871411            }
    13881412            else
    13891413            {
    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                    }
    14101451                }
    14111452                else
    14121453                {
    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                }
    14251456            }
    14261457        }
     
    14321463        LogInfo("unknown option %s", optionName);
    14331464    }
    1434 
    14351465    return result;
    14361466}
     
    14771507#endif // DO_NOT_COPY_TRUSTED_CERTS_STRING
    14781508    }
    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)
    14801510    {
    14811511        certLen = strlen((const char*)value);
     
    14941524        }
    14951525    }
    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)
    14971527    {
    14981528        certLen = strlen((const char*)value);
  • azure_iot_hub_f767zi/trunk/azure_iot_sdk/c-utility/adapters/tlsio_wolfssl.c

    r464 r471  
    5252    char* x509privatekey;
    5353    int wolfssl_device_id;
    54     char* hostname;
    55     bool ignore_host_name_check;
     54    size_t socket_reads;
    5655} TLS_IO_INSTANCE;
    5756
    5857STATIC_VAR_UNUSED const char* const OPTION_WOLFSSL_SET_DEVICE_ID = "SetDeviceId";
    59 static const size_t SOCKET_READ_LIMIT = 5;
     58static const size_t SOCKET_READ_LIMIT = 10000; // 10,000 ms ?
    6059
    6160/*this function will clone an option given by name and value*/
     
    106105            }
    107106        }
    108         #ifdef INVALID_DEVID
    109         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              else
    118              {
    119                  *value_clone = *(int*)value;
    120              }
    121 
    122              result = value_clone;
    123         }
    124         #endif
    125107        else
    126108        {
     
    144126        if ((strcmp(name, OPTION_TRUSTED_CERT) == 0) ||
    145127            (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))
    148129        {
    149130            free((void*)value);
     
    203184                result = NULL;
    204185            }
    205             #ifdef INVALID_DEVID
    206             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             #endif
    216186            else
    217187            {
     
    287257        int res;
    288258        tls_io_instance->tlsio_state = TLSIO_STATE_IN_HANDSHAKE;
     259        tls_io_instance->socket_reads = 0;
    289260
    290261        res = wolfSSL_connect(tls_io_instance->ssl);
    291262        if (res != SSL_SUCCESS)
    292263        {
    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");
    295265            indicate_open_complete(tls_io_instance, IO_OPEN_ERROR);
    296266            tls_io_instance->tlsio_state = TLSIO_STATE_ERROR;
     
    384354        TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context;
    385355        unsigned char* new_socket_io_read_bytes;
    386         size_t socket_reads = 0;
    387356
    388357        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            }
    391363            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            }
    397370        }
    398371
     
    443416}
    444417
     418static 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
    445430static int on_io_send(WOLFSSL *ssl, char *buf, int sz, void *context)
    446431{
     
    450435    TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context;
    451436
    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)
    453438    {
    454439        LogError("Failed sending bytes through underlying IO");
    455440        tls_io_instance->tlsio_state = TLSIO_STATE_ERROR;
    456441        indicate_error(tls_io_instance);
    457         result = WOLFSSL_CBIO_ERR_GENERAL;
     442        result = -1;
    458443    }
    459444    else
     
    469454    AZURE_UNREFERENCED_PARAMETER(ssl);
    470455    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)
    472459    {
    473460        LogInfo("on_handshake_done called when not in IN_HANDSHAKE state");
     
    569556}
    570557
    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 
    586558static int prepare_wolfssl_open(TLS_IO_INSTANCE* tls_io_instance)
    587559{
    588560    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)
    596562    {
    597563        LogError("Failed to add certificates to store");
     
    607573        result = MU_FAILURE;
    608574    }
     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
    609582    else
    610583    {
     
    653626            {
    654627                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.");
    661628                free(result);
    662629                result = NULL;
     
    691658                    LogError("Failed getting socket IO interface description.");
    692659                    wolfSSL_CTX_free(result->ssl_context);
    693                     free(result->hostname);
    694660                    free(result);
    695661                    result = NULL;
     
    702668                        LogError("Failure connecting to underlying socket_io");
    703669                        wolfSSL_CTX_free(result->ssl_context);
    704                         free(result->hostname);
    705670                        free(result);
    706671                        result = NULL;
     
    710675                        LogError("Failure connecting to underlying socket_io");
    711676                        wolfSSL_CTX_free(result->ssl_context);
    712                         free(result->hostname);
    713677                        free(result);
    714678                        result = NULL;
     
    753717
    754718        xio_destroy(tls_io_instance->socket_io);
    755         free(tls_io_instance->hostname);
    756719        free(tls_io);
    757720    }
     
    803766            else
    804767            {
    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
     776int 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
     816int 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
     860void 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
     881static 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
     901int 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)
    807938                {
    808                     LogError("Failed to connect to server.  The certificates may not be correct.");
     939                    LogError("Failure setting device id on ssl");
    809940                    result = MU_FAILURE;
    810941                }
     
    814945                }
    815946            }
    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
    986950                tls_io_instance->wolfssl_device_id = device_id;
    987951                result = 0;
     
    989953        }
    990954#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         }
    997955        else
    998956        {
  • azure_iot_hub_f767zi/trunk/azure_iot_sdk/serializer/src/commanddecoder.c

    r457 r471  
    731731                else
    732732                {
     733                    //printf("%s %s\n", fullMethodName, methodJSON);
     734                    if (strcmp(methodJSON, "null") == 0) {
     735                        strcpy(methodJSON, "{}");
     736                    }
    733737                    MULTITREE_HANDLE methodTree;
    734738                    if (JSONDecoder_JSON_To_MultiTree(methodJSON, &methodTree) != JSON_DECODER_OK)
  • azure_iot_hub_f767zi/trunk/azure_iot_sdk/serializer/src/jsondecoder.c

    r457 r471  
    598598            result = JSON_DECODER_MULTITREE_FAILED;
    599599        }
     600        else if (strcmp(json, "null") == 0) {
     601            result = JSON_DECODER_OK;
     602        }
    600603        else
    601604        {
Note: See TracChangeset for help on using the changeset viewer.