Azure IoT common library

Dependents:   STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more

Committer:
AzureIoTClient
Date:
Thu Jul 12 18:10:56 2018 -0700
Revision:
47:e59b74c745bc
Parent:
46:fde927ea2e72
Child:
48:81866008bba4
1.2.8

Who changed what in which revision?

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