Ignore:
Timestamp:
Jun 22, 2021, 9:00:19 PM (3 years ago)
Author:
coas-nagasima
Message:

WolfSSLとAzure IoT SDKを更新

File:
1 edited

Legend:

Unmodified
Added
Removed
  • azure_iot_hub_f767zi/trunk/azure_iot_sdk/c-utility/adapters/tlsio_wolfssl.c

    r457 r464  
    1919#include "azure_c_shared_utility/xlogging.h"
    2020#include "azure_c_shared_utility/shared_util_options.h"
    21 #include "azure_c_shared_utility/threadapi.h"
    2221
    2322typedef enum TLSIO_STATE_ENUM_TAG
     
    5352    char* x509privatekey;
    5453    int wolfssl_device_id;
    55     size_t socket_reads;
     54    char* hostname;
     55    bool ignore_host_name_check;
    5656} TLS_IO_INSTANCE;
    5757
    5858STATIC_VAR_UNUSED const char* const OPTION_WOLFSSL_SET_DEVICE_ID = "SetDeviceId";
    59 static const size_t SOCKET_READ_LIMIT = 10000; // 10,000 ms ?
     59static const size_t SOCKET_READ_LIMIT = 5;
    6060
    6161/*this function will clone an option given by name and value*/
     
    106106            }
    107107        }
     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
    108125        else
    109126        {
     
    127144        if ((strcmp(name, OPTION_TRUSTED_CERT) == 0) ||
    128145            (strcmp(name, SU_OPTION_X509_CERT) == 0) ||
    129             (strcmp(name, SU_OPTION_X509_PRIVATE_KEY) == 0))
     146            (strcmp(name, SU_OPTION_X509_PRIVATE_KEY) == 0) ||
     147            (strcmp(name, OPTION_WOLFSSL_SET_DEVICE_ID) == 0))
    130148        {
    131149            free((void*)value);
     
    185203                result = NULL;
    186204            }
     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
    187216            else
    188217            {
     
    258287        int res;
    259288        tls_io_instance->tlsio_state = TLSIO_STATE_IN_HANDSHAKE;
    260         tls_io_instance->socket_reads = 0;
    261289
    262290        res = wolfSSL_connect(tls_io_instance->ssl);
    263291        if (res != SSL_SUCCESS)
    264292        {
    265             LogError("WolfSSL connect failed");
     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));
    266295            indicate_open_complete(tls_io_instance, IO_OPEN_ERROR);
    267296            tls_io_instance->tlsio_state = TLSIO_STATE_ERROR;
     
    355384        TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context;
    356385        unsigned char* new_socket_io_read_bytes;
     386        size_t socket_reads = 0;
    357387
    358388        AZURE_UNREFERENCED_PARAMETER(ssl);
    359         if (tls_io_instance->socket_io_read_byte_count == 0)
    360         {
    361             if (tls_io_instance->socket_reads >= SOCKET_READ_LIMIT) {
    362                 return WOLFSSL_CBIO_ERR_TIMEOUT;
    363             }
     389        while (tls_io_instance->socket_io_read_byte_count == 0 && socket_reads < SOCKET_READ_LIMIT)
     390        {
    364391            xio_dowork(tls_io_instance->socket_io);
    365             if (tls_io_instance->tlsio_state == TLSIO_STATE_IN_HANDSHAKE)
    366             {
    367                 tls_io_instance->socket_reads++;
    368                 ThreadAPI_Sleep(1);
    369                 return 0;
    370             }
     392            if (tls_io_instance->tlsio_state != TLSIO_STATE_IN_HANDSHAKE)
     393            {
     394                break;
     395            }
     396            socket_reads++;
    371397        }
    372398
     
    417443}
    418444
    419 static void on_send_complete(void* context, IO_SEND_RESULT send_result)
    420 {
    421         TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context;
    422         if ((tls_io_instance == NULL) || (tls_io_instance->on_send_complete == NULL))
    423                 return;
    424 
    425         tls_io_instance->on_send_complete(tls_io_instance->on_send_complete_callback_context, send_result);
    426 
    427         tls_io_instance->on_send_complete = NULL;
    428         tls_io_instance->on_send_complete_callback_context = NULL;
    429 }
    430 
    431445static int on_io_send(WOLFSSL *ssl, char *buf, int sz, void *context)
    432446{
    433     int result, ret;
     447    int result;
    434448    AZURE_UNREFERENCED_PARAMETER(ssl);
    435449
    436450    TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context;
    437451
    438     if ((ret = xio_send(tls_io_instance->socket_io, buf, sz, on_send_complete, tls_io_instance)) != 0)
    439     {
    440         LogError("Failed sending bytes through underlying IO %d", ret);
     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)
     453    {
     454        LogError("Failed sending bytes through underlying IO");
    441455        tls_io_instance->tlsio_state = TLSIO_STATE_ERROR;
    442456        indicate_error(tls_io_instance);
    443         result = 0;
     457        result = WOLFSSL_CBIO_ERR_GENERAL;
    444458    }
    445459    else
     
    455469    AZURE_UNREFERENCED_PARAMETER(ssl);
    456470    TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context;
    457     if (tls_io_instance->tlsio_state == TLSIO_STATE_OPEN) {
    458         LogInfo("on_handshake_done called in TLSIO_STATE_OPEN state");
    459     }
    460     else if (tls_io_instance->tlsio_state != TLSIO_STATE_IN_HANDSHAKE)
     471    if (tls_io_instance->tlsio_state != TLSIO_STATE_IN_HANDSHAKE)
    461472    {
    462473        LogInfo("on_handshake_done called when not in IN_HANDSHAKE state");
     
    558569}
    559570
     571static 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
    560586static int prepare_wolfssl_open(TLS_IO_INSTANCE* tls_io_instance)
    561587{
    562588    int result;
    563     if (add_certificate_to_store(tls_io_instance) != 0)
     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)
    564596    {
    565597        LogError("Failed to add certificates to store");
     
    575607        result = MU_FAILURE;
    576608    }
    577 #ifdef INVALID_DEVID
    578     else if (tls_io_instance->wolfssl_device_id != INVALID_DEVID && wolfSSL_SetDevId(tls_io_instance->ssl, tls_io_instance->wolfssl_device_id) != WOLFSSL_SUCCESS)
    579     {
    580         LogError("Failure setting device id");
    581         result = MU_FAILURE;
    582     }
    583 #endif
    584609    else
    585610    {
     
    628653            {
    629654                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.");
    630661                free(result);
    631662                result = NULL;
     
    660691                    LogError("Failed getting socket IO interface description.");
    661692                    wolfSSL_CTX_free(result->ssl_context);
     693                    free(result->hostname);
    662694                    free(result);
    663695                    result = NULL;
     
    670702                        LogError("Failure connecting to underlying socket_io");
    671703                        wolfSSL_CTX_free(result->ssl_context);
     704                        free(result->hostname);
    672705                        free(result);
    673706                        result = NULL;
     
    677710                        LogError("Failure connecting to underlying socket_io");
    678711                        wolfSSL_CTX_free(result->ssl_context);
     712                        free(result->hostname);
    679713                        free(result);
    680714                        result = NULL;
     
    719753
    720754        xio_destroy(tls_io_instance->socket_io);
     755        free(tls_io_instance->hostname);
    721756        free(tls_io);
    722757    }
     
    843878            result = MU_FAILURE;
    844879        }
    845         if (tls_io_instance->on_send_complete != NULL)
    846         {
    847             LogError("Error writing data");
    848             result = MU_FAILURE;
    849         }
    850880        else
    851881        {
     
    882912            (tls_io_instance->tlsio_state != TLSIO_STATE_ERROR))
    883913        {
    884             if (tls_io_instance->tlsio_state != TLSIO_STATE_OPENING_UNDERLYING_IO)
    885                 decode_ssl_received_bytes(tls_io_instance);
     914            decode_ssl_received_bytes(tls_io_instance);
    886915            xio_dowork(tls_io_instance->socket_io);
    887916        }
     
    892921static int process_option(char** destination, const char* name, const char* value)
    893922{
     923
     924    (void) name;
     925   
    894926    int result;
    895927    if (*destination != NULL)
     
    944976        {
    945977            int device_id = *((int *)value);
    946             if (tls_io_instance->ssl != NULL)
    947             {
    948                 if (tls_io_instance->ssl != NULL && wolfSSL_SetDevId(tls_io_instance->ssl, device_id) != WOLFSSL_SUCCESS)
    949                 {
    950                     LogError("Failure setting device id on ssl");
    951                     result = MU_FAILURE;
    952                 }
    953                 else
    954                 {
    955                     result = 0;
    956                 }
     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;
    957982            }
    958983            else
    959984            {
    960                 // Save the id till we create the ssl object
     985                // Save the device Id even if ssl object not yet created.
    961986                tls_io_instance->wolfssl_device_id = device_id;
    962987                result = 0;
     
    964989        }
    965990#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        }
    966997        else
    967998        {
Note: See TracChangeset for help on using the changeset viewer.