Azure IoT common library

Dependents:   STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more

Committer:
Azure.IoT Build
Date:
Fri Jul 01 10:43:23 2016 -0700
Revision:
6:c55b013dfc2a
Parent:
1:9190c0f4d23a
Child:
10:1be0bc9a9deb
1.0.10

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>
Azure.IoT Build 0:fa2de1b79154 5 #ifdef _CRTDBG_MAP_ALLOC
Azure.IoT Build 0:fa2de1b79154 6 #include <crtdbg.h>
Azure.IoT Build 0:fa2de1b79154 7 #endif
Azure.IoT Build 0:fa2de1b79154 8
Azure.IoT Build 0:fa2de1b79154 9 #include "wolfssl/ssl.h"
Azure.IoT Build 0:fa2de1b79154 10 #include "wolfssl/error-ssl.h"
Azure.IoT Build 0:fa2de1b79154 11 #include <stdio.h>
Azure.IoT Build 0:fa2de1b79154 12 #include <stdbool.h>
Azure.IoT Build 0:fa2de1b79154 13 #include <string.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"
Azure.IoT Build 6:c55b013dfc2a 18 #include "azure_c_shared_utility/xlogging.h"
Azure.IoT Build 0:fa2de1b79154 19
Azure.IoT Build 0:fa2de1b79154 20 typedef enum TLSIO_STATE_ENUM_TAG
Azure.IoT Build 0:fa2de1b79154 21 {
Azure.IoT Build 0:fa2de1b79154 22 TLSIO_STATE_NOT_OPEN,
Azure.IoT Build 0:fa2de1b79154 23 TLSIO_STATE_OPENING_UNDERLYING_IO,
Azure.IoT Build 0:fa2de1b79154 24 TLSIO_STATE_IN_HANDSHAKE,
Azure.IoT Build 0:fa2de1b79154 25 TLSIO_STATE_OPEN,
Azure.IoT Build 0:fa2de1b79154 26 TLSIO_STATE_CLOSING,
Azure.IoT Build 0:fa2de1b79154 27 TLSIO_STATE_ERROR
Azure.IoT Build 0:fa2de1b79154 28 } TLSIO_STATE_ENUM;
Azure.IoT Build 0:fa2de1b79154 29
Azure.IoT Build 0:fa2de1b79154 30 typedef struct TLS_IO_INSTANCE_TAG
Azure.IoT Build 0:fa2de1b79154 31 {
Azure.IoT Build 0:fa2de1b79154 32 XIO_HANDLE socket_io;
Azure.IoT Build 0:fa2de1b79154 33 ON_BYTES_RECEIVED on_bytes_received;
Azure.IoT Build 0:fa2de1b79154 34 ON_IO_OPEN_COMPLETE on_io_open_complete;
Azure.IoT Build 0:fa2de1b79154 35 ON_IO_CLOSE_COMPLETE on_io_close_complete;
Azure.IoT Build 0:fa2de1b79154 36 ON_IO_ERROR on_io_error;
Azure.IoT Build 0:fa2de1b79154 37 void* on_bytes_received_context;
Azure.IoT Build 0:fa2de1b79154 38 void* on_io_open_complete_context;
Azure.IoT Build 0:fa2de1b79154 39 void* on_io_close_complete_context;
Azure.IoT Build 0:fa2de1b79154 40 void* on_io_error_context;
Azure.IoT Build 0:fa2de1b79154 41 WOLFSSL* ssl;
Azure.IoT Build 0:fa2de1b79154 42 WOLFSSL_CTX* ssl_context;
Azure.IoT Build 0:fa2de1b79154 43 TLSIO_STATE_ENUM tlsio_state;
Azure.IoT Build 0:fa2de1b79154 44 unsigned char* socket_io_read_bytes;
Azure.IoT Build 0:fa2de1b79154 45 size_t socket_io_read_byte_count;
Azure.IoT Build 0:fa2de1b79154 46 ON_SEND_COMPLETE on_send_complete;
Azure.IoT Build 0:fa2de1b79154 47 void* on_send_complete_callback_context;
Azure.IoT Build 6:c55b013dfc2a 48 char* certificate;
Azure.IoT Build 6:c55b013dfc2a 49 char* hostname;
Azure.IoT Build 6:c55b013dfc2a 50 int port;
Azure.IoT Build 0:fa2de1b79154 51 } TLS_IO_INSTANCE;
Azure.IoT Build 0:fa2de1b79154 52
Azure.IoT Build 0:fa2de1b79154 53 static const IO_INTERFACE_DESCRIPTION tlsio_wolfssl_interface_description =
Azure.IoT Build 0:fa2de1b79154 54 {
Azure.IoT Build 0:fa2de1b79154 55 tlsio_wolfssl_create,
Azure.IoT Build 0:fa2de1b79154 56 tlsio_wolfssl_destroy,
Azure.IoT Build 0:fa2de1b79154 57 tlsio_wolfssl_open,
Azure.IoT Build 0:fa2de1b79154 58 tlsio_wolfssl_close,
Azure.IoT Build 0:fa2de1b79154 59 tlsio_wolfssl_send,
Azure.IoT Build 0:fa2de1b79154 60 tlsio_wolfssl_dowork,
Azure.IoT Build 0:fa2de1b79154 61 tlsio_wolfssl_setoption
Azure.IoT Build 0:fa2de1b79154 62 };
Azure.IoT Build 0:fa2de1b79154 63
Azure.IoT Build 0:fa2de1b79154 64 static void indicate_error(TLS_IO_INSTANCE* tls_io_instance)
Azure.IoT Build 0:fa2de1b79154 65 {
Azure.IoT Build 0:fa2de1b79154 66 if (tls_io_instance->on_io_error != NULL)
Azure.IoT Build 0:fa2de1b79154 67 {
Azure.IoT Build 0:fa2de1b79154 68 tls_io_instance->on_io_error(tls_io_instance->on_io_error_context);
Azure.IoT Build 0:fa2de1b79154 69 }
Azure.IoT Build 0:fa2de1b79154 70 }
Azure.IoT Build 0:fa2de1b79154 71
Azure.IoT Build 0:fa2de1b79154 72 static void indicate_open_complete(TLS_IO_INSTANCE* tls_io_instance, IO_OPEN_RESULT open_result)
Azure.IoT Build 0:fa2de1b79154 73 {
Azure.IoT Build 0:fa2de1b79154 74 if (tls_io_instance->on_io_open_complete != NULL)
Azure.IoT Build 0:fa2de1b79154 75 {
Azure.IoT Build 0:fa2de1b79154 76 tls_io_instance->on_io_open_complete(tls_io_instance->on_io_open_complete_context, open_result);
Azure.IoT Build 0:fa2de1b79154 77 }
Azure.IoT Build 0:fa2de1b79154 78 }
Azure.IoT Build 0:fa2de1b79154 79
Azure.IoT Build 0:fa2de1b79154 80 static int decode_ssl_received_bytes(TLS_IO_INSTANCE* tls_io_instance)
Azure.IoT Build 0:fa2de1b79154 81 {
Azure.IoT Build 0:fa2de1b79154 82 int result = 0;
Azure.IoT Build 0:fa2de1b79154 83 unsigned char buffer[64];
Azure.IoT Build 0:fa2de1b79154 84
Azure.IoT Build 0:fa2de1b79154 85 int rcv_bytes = 1;
Azure.IoT Build 0:fa2de1b79154 86 while (rcv_bytes > 0)
Azure.IoT Build 0:fa2de1b79154 87 {
Azure.IoT Build 0:fa2de1b79154 88 rcv_bytes = wolfSSL_read(tls_io_instance->ssl, buffer, sizeof(buffer));
Azure.IoT Build 0:fa2de1b79154 89 if (rcv_bytes > 0)
Azure.IoT Build 0:fa2de1b79154 90 {
Azure.IoT Build 0:fa2de1b79154 91 if (tls_io_instance->on_bytes_received != NULL)
Azure.IoT Build 0:fa2de1b79154 92 {
Azure.IoT Build 0:fa2de1b79154 93 tls_io_instance->on_bytes_received(tls_io_instance->on_bytes_received_context, buffer, rcv_bytes);
Azure.IoT Build 0:fa2de1b79154 94 }
Azure.IoT Build 0:fa2de1b79154 95 }
Azure.IoT Build 0:fa2de1b79154 96 }
Azure.IoT Build 0:fa2de1b79154 97
Azure.IoT Build 0:fa2de1b79154 98 return result;
Azure.IoT Build 0:fa2de1b79154 99 }
Azure.IoT Build 0:fa2de1b79154 100
Azure.IoT Build 0:fa2de1b79154 101 static void on_underlying_io_open_complete(void* context, IO_OPEN_RESULT open_result)
Azure.IoT Build 0:fa2de1b79154 102 {
Azure.IoT Build 0:fa2de1b79154 103 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context;
Azure.IoT Build 0:fa2de1b79154 104
Azure.IoT Build 0:fa2de1b79154 105 if (open_result != IO_OPEN_OK)
Azure.IoT Build 0:fa2de1b79154 106 {
Azure.IoT Build 0:fa2de1b79154 107 tls_io_instance->tlsio_state = TLSIO_STATE_ERROR;
Azure.IoT Build 0:fa2de1b79154 108 indicate_open_complete(tls_io_instance, IO_OPEN_ERROR);
Azure.IoT Build 0:fa2de1b79154 109 }
Azure.IoT Build 0:fa2de1b79154 110 else
Azure.IoT Build 0:fa2de1b79154 111 {
Azure.IoT Build 0:fa2de1b79154 112 int res;
Azure.IoT Build 0:fa2de1b79154 113 tls_io_instance->tlsio_state = TLSIO_STATE_IN_HANDSHAKE;
Azure.IoT Build 0:fa2de1b79154 114
Azure.IoT Build 0:fa2de1b79154 115 res = wolfSSL_connect(tls_io_instance->ssl);
Azure.IoT Build 0:fa2de1b79154 116 if (res != SSL_SUCCESS)
Azure.IoT Build 0:fa2de1b79154 117 {
Azure.IoT Build 0:fa2de1b79154 118 indicate_open_complete(tls_io_instance, IO_OPEN_ERROR);
Azure.IoT Build 6:c55b013dfc2a 119 tls_io_instance->tlsio_state = TLSIO_STATE_ERROR;
Azure.IoT Build 0:fa2de1b79154 120 }
Azure.IoT Build 0:fa2de1b79154 121 }
Azure.IoT Build 0:fa2de1b79154 122 }
Azure.IoT Build 0:fa2de1b79154 123
Azure.IoT Build 0:fa2de1b79154 124 static void on_underlying_io_bytes_received(void* context, const unsigned char* buffer, size_t size)
Azure.IoT Build 0:fa2de1b79154 125 {
Azure.IoT Build 0:fa2de1b79154 126 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context;
Azure.IoT Build 0:fa2de1b79154 127
Azure.IoT Build 0:fa2de1b79154 128 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);
Azure.IoT Build 0:fa2de1b79154 129 if (new_socket_io_read_bytes == NULL)
Azure.IoT Build 0:fa2de1b79154 130 {
Azure.IoT Build 0:fa2de1b79154 131 tls_io_instance->tlsio_state = TLSIO_STATE_ERROR;
Azure.IoT Build 0:fa2de1b79154 132 indicate_error(tls_io_instance);
Azure.IoT Build 0:fa2de1b79154 133 }
Azure.IoT Build 0:fa2de1b79154 134 else
Azure.IoT Build 0:fa2de1b79154 135 {
Azure.IoT Build 0:fa2de1b79154 136 tls_io_instance->socket_io_read_bytes = new_socket_io_read_bytes;
Azure.IoT Build 0:fa2de1b79154 137 (void)memcpy(tls_io_instance->socket_io_read_bytes + tls_io_instance->socket_io_read_byte_count, buffer, size);
Azure.IoT Build 0:fa2de1b79154 138 tls_io_instance->socket_io_read_byte_count += size;
Azure.IoT Build 0:fa2de1b79154 139 }
Azure.IoT Build 0:fa2de1b79154 140 }
Azure.IoT Build 0:fa2de1b79154 141
Azure.IoT Build 0:fa2de1b79154 142 static void on_underlying_io_error(void* context)
Azure.IoT Build 0:fa2de1b79154 143 {
Azure.IoT Build 0:fa2de1b79154 144 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context;
Azure.IoT Build 0:fa2de1b79154 145
Azure.IoT Build 0:fa2de1b79154 146 switch (tls_io_instance->tlsio_state)
Azure.IoT Build 0:fa2de1b79154 147 {
Azure.IoT Build 6:c55b013dfc2a 148 default:
Azure.IoT Build 6:c55b013dfc2a 149 case TLSIO_STATE_NOT_OPEN:
Azure.IoT Build 6:c55b013dfc2a 150 case TLSIO_STATE_ERROR:
Azure.IoT Build 6:c55b013dfc2a 151 break;
Azure.IoT Build 0:fa2de1b79154 152
Azure.IoT Build 6:c55b013dfc2a 153 case TLSIO_STATE_OPENING_UNDERLYING_IO:
Azure.IoT Build 6:c55b013dfc2a 154 case TLSIO_STATE_IN_HANDSHAKE:
Azure.IoT Build 6:c55b013dfc2a 155 tls_io_instance->tlsio_state = TLSIO_STATE_ERROR;
Azure.IoT Build 6:c55b013dfc2a 156 indicate_open_complete(tls_io_instance, IO_OPEN_ERROR);
Azure.IoT Build 6:c55b013dfc2a 157 break;
Azure.IoT Build 0:fa2de1b79154 158
Azure.IoT Build 6:c55b013dfc2a 159 case TLSIO_STATE_OPEN:
Azure.IoT Build 6:c55b013dfc2a 160 tls_io_instance->tlsio_state = TLSIO_STATE_ERROR;
Azure.IoT Build 6:c55b013dfc2a 161 indicate_error(tls_io_instance);
Azure.IoT Build 6:c55b013dfc2a 162 break;
Azure.IoT Build 0:fa2de1b79154 163 }
Azure.IoT Build 0:fa2de1b79154 164 }
Azure.IoT Build 0:fa2de1b79154 165
Azure.IoT Build 0:fa2de1b79154 166 static void on_underlying_io_close_complete(void* context)
Azure.IoT Build 0:fa2de1b79154 167 {
Azure.IoT Build 0:fa2de1b79154 168 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context;
Azure.IoT Build 0:fa2de1b79154 169
Azure.IoT Build 0:fa2de1b79154 170 if (tls_io_instance->tlsio_state == TLSIO_STATE_CLOSING)
Azure.IoT Build 0:fa2de1b79154 171 {
Azure.IoT Build 0:fa2de1b79154 172 if (tls_io_instance->on_io_close_complete != NULL)
Azure.IoT Build 0:fa2de1b79154 173 {
Azure.IoT Build 0:fa2de1b79154 174 tls_io_instance->on_io_close_complete(tls_io_instance->on_io_close_complete_context);
Azure.IoT Build 0:fa2de1b79154 175 }
Azure.IoT Build 6:c55b013dfc2a 176 tls_io_instance->tlsio_state = TLSIO_STATE_NOT_OPEN;
Azure.IoT Build 0:fa2de1b79154 177 }
Azure.IoT Build 0:fa2de1b79154 178 }
Azure.IoT Build 0:fa2de1b79154 179
Azure.IoT Build 0:fa2de1b79154 180 static int on_io_recv(WOLFSSL *ssl, char *buf, int sz, void *context)
Azure.IoT Build 0:fa2de1b79154 181 {
Azure.IoT Build 0:fa2de1b79154 182 int result;
Azure.IoT Build 0:fa2de1b79154 183 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context;
Azure.IoT Build 0:fa2de1b79154 184 unsigned char* new_socket_io_read_bytes;
Azure.IoT Build 0:fa2de1b79154 185
Azure.IoT Build 0:fa2de1b79154 186 while (tls_io_instance->socket_io_read_byte_count == 0)
Azure.IoT Build 0:fa2de1b79154 187 {
Azure.IoT Build 0:fa2de1b79154 188 xio_dowork(tls_io_instance->socket_io);
Azure.IoT Build 6:c55b013dfc2a 189 if (tls_io_instance->tlsio_state != TLSIO_STATE_IN_HANDSHAKE)
Azure.IoT Build 0:fa2de1b79154 190 {
Azure.IoT Build 0:fa2de1b79154 191 break;
Azure.IoT Build 0:fa2de1b79154 192 }
Azure.IoT Build 0:fa2de1b79154 193 }
Azure.IoT Build 0:fa2de1b79154 194
Azure.IoT Build 0:fa2de1b79154 195 result = tls_io_instance->socket_io_read_byte_count;
Azure.IoT Build 0:fa2de1b79154 196 if (result > sz)
Azure.IoT Build 0:fa2de1b79154 197 {
Azure.IoT Build 0:fa2de1b79154 198 result = sz;
Azure.IoT Build 0:fa2de1b79154 199 }
Azure.IoT Build 0:fa2de1b79154 200
Azure.IoT Build 0:fa2de1b79154 201 if (result > 0)
Azure.IoT Build 0:fa2de1b79154 202 {
Azure.IoT Build 0:fa2de1b79154 203 (void)memcpy(buf, tls_io_instance->socket_io_read_bytes, result);
Azure.IoT Build 0:fa2de1b79154 204 (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);
Azure.IoT Build 0:fa2de1b79154 205 tls_io_instance->socket_io_read_byte_count -= result;
Azure.IoT Build 0:fa2de1b79154 206 if (tls_io_instance->socket_io_read_byte_count > 0)
Azure.IoT Build 0:fa2de1b79154 207 {
Azure.IoT Build 0:fa2de1b79154 208 new_socket_io_read_bytes = (unsigned char*)realloc(tls_io_instance->socket_io_read_bytes, tls_io_instance->socket_io_read_byte_count);
Azure.IoT Build 0:fa2de1b79154 209 if (new_socket_io_read_bytes != NULL)
Azure.IoT Build 0:fa2de1b79154 210 {
Azure.IoT Build 0:fa2de1b79154 211 tls_io_instance->socket_io_read_bytes = new_socket_io_read_bytes;
Azure.IoT Build 0:fa2de1b79154 212 }
Azure.IoT Build 0:fa2de1b79154 213 }
Azure.IoT Build 0:fa2de1b79154 214 else
Azure.IoT Build 0:fa2de1b79154 215 {
Azure.IoT Build 0:fa2de1b79154 216 free(tls_io_instance->socket_io_read_bytes);
Azure.IoT Build 0:fa2de1b79154 217 tls_io_instance->socket_io_read_bytes = NULL;
Azure.IoT Build 0:fa2de1b79154 218 }
Azure.IoT Build 0:fa2de1b79154 219 }
Azure.IoT Build 0:fa2de1b79154 220
Azure.IoT Build 0:fa2de1b79154 221 if ((result == 0) && (tls_io_instance->tlsio_state == TLSIO_STATE_OPEN))
Azure.IoT Build 0:fa2de1b79154 222 {
Azure.IoT Build 0:fa2de1b79154 223 result = WOLFSSL_CBIO_ERR_WANT_READ;
Azure.IoT Build 0:fa2de1b79154 224 }
Azure.IoT Build 6:c55b013dfc2a 225 else if ((result == 0) && tls_io_instance->tlsio_state == TLSIO_STATE_CLOSING)
Azure.IoT Build 6:c55b013dfc2a 226 {
Azure.IoT Build 6:c55b013dfc2a 227 result = WOLFSSL_CBIO_ERR_CONN_CLOSE;
Azure.IoT Build 6:c55b013dfc2a 228 }
Azure.IoT Build 0:fa2de1b79154 229
Azure.IoT Build 0:fa2de1b79154 230 return result;
Azure.IoT Build 0:fa2de1b79154 231 }
Azure.IoT Build 0:fa2de1b79154 232
Azure.IoT Build 0:fa2de1b79154 233 static int on_io_send(WOLFSSL *ssl, char *buf, int sz, void *context)
Azure.IoT Build 0:fa2de1b79154 234 {
Azure.IoT Build 0:fa2de1b79154 235 int result;
Azure.IoT Build 0:fa2de1b79154 236 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context;
Azure.IoT Build 0:fa2de1b79154 237
Azure.IoT Build 0:fa2de1b79154 238 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 239 {
Azure.IoT Build 0:fa2de1b79154 240 tls_io_instance->tlsio_state = TLSIO_STATE_ERROR;
Azure.IoT Build 0:fa2de1b79154 241 indicate_error(tls_io_instance);
Azure.IoT Build 0:fa2de1b79154 242 result = 0;
Azure.IoT Build 0:fa2de1b79154 243 }
Azure.IoT Build 0:fa2de1b79154 244 else
Azure.IoT Build 0:fa2de1b79154 245 {
Azure.IoT Build 0:fa2de1b79154 246 result = sz;
Azure.IoT Build 0:fa2de1b79154 247 }
Azure.IoT Build 0:fa2de1b79154 248
Azure.IoT Build 0:fa2de1b79154 249 return result;
Azure.IoT Build 0:fa2de1b79154 250 }
Azure.IoT Build 0:fa2de1b79154 251
Azure.IoT Build 0:fa2de1b79154 252 static int on_handshake_done(WOLFSSL* ssl, void* context)
Azure.IoT Build 0:fa2de1b79154 253 {
Azure.IoT Build 0:fa2de1b79154 254 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context;
Azure.IoT Build 0:fa2de1b79154 255 if (tls_io_instance->tlsio_state == TLSIO_STATE_IN_HANDSHAKE)
Azure.IoT Build 0:fa2de1b79154 256 {
Azure.IoT Build 0:fa2de1b79154 257 tls_io_instance->tlsio_state = TLSIO_STATE_OPEN;
Azure.IoT Build 0:fa2de1b79154 258 indicate_open_complete(tls_io_instance, IO_OPEN_OK);
Azure.IoT Build 0:fa2de1b79154 259 }
Azure.IoT Build 0:fa2de1b79154 260
Azure.IoT Build 0:fa2de1b79154 261 return 0;
Azure.IoT Build 0:fa2de1b79154 262 }
Azure.IoT Build 0:fa2de1b79154 263
Azure.IoT Build 6:c55b013dfc2a 264 static int add_certificate_to_store(TLS_IO_INSTANCE* tls_io_instance)
Azure.IoT Build 6:c55b013dfc2a 265 {
Azure.IoT Build 6:c55b013dfc2a 266 int result;
Azure.IoT Build 6:c55b013dfc2a 267 if (tls_io_instance->certificate != NULL)
Azure.IoT Build 6:c55b013dfc2a 268 {
Azure.IoT Build 6:c55b013dfc2a 269 int res = wolfSSL_CTX_load_verify_buffer(tls_io_instance->ssl_context, (const unsigned char*)tls_io_instance->certificate, strlen(tls_io_instance->certificate)+1, SSL_FILETYPE_PEM);
Azure.IoT Build 6:c55b013dfc2a 270 if (res != SSL_SUCCESS)
Azure.IoT Build 6:c55b013dfc2a 271 {
Azure.IoT Build 6:c55b013dfc2a 272 result = __LINE__;
Azure.IoT Build 6:c55b013dfc2a 273 }
Azure.IoT Build 6:c55b013dfc2a 274 else
Azure.IoT Build 6:c55b013dfc2a 275 {
Azure.IoT Build 6:c55b013dfc2a 276 result = 0;
Azure.IoT Build 6:c55b013dfc2a 277 }
Azure.IoT Build 6:c55b013dfc2a 278 }
Azure.IoT Build 6:c55b013dfc2a 279 else
Azure.IoT Build 6:c55b013dfc2a 280 {
Azure.IoT Build 6:c55b013dfc2a 281 result = 0;
Azure.IoT Build 6:c55b013dfc2a 282 }
Azure.IoT Build 6:c55b013dfc2a 283 return result;
Azure.IoT Build 6:c55b013dfc2a 284 }
Azure.IoT Build 6:c55b013dfc2a 285
Azure.IoT Build 6:c55b013dfc2a 286 static int create_wolfssl_instance(TLS_IO_INSTANCE* tls_io_instance)
Azure.IoT Build 6:c55b013dfc2a 287 {
Azure.IoT Build 6:c55b013dfc2a 288 int result;
Azure.IoT Build 6:c55b013dfc2a 289
Azure.IoT Build 6:c55b013dfc2a 290 if (add_certificate_to_store(tls_io_instance) != 0)
Azure.IoT Build 6:c55b013dfc2a 291 {
Azure.IoT Build 6:c55b013dfc2a 292 wolfSSL_CTX_free(tls_io_instance->ssl_context);
Azure.IoT Build 6:c55b013dfc2a 293 result = __LINE__;
Azure.IoT Build 6:c55b013dfc2a 294 }
Azure.IoT Build 6:c55b013dfc2a 295 else
Azure.IoT Build 6:c55b013dfc2a 296 {
Azure.IoT Build 6:c55b013dfc2a 297 tls_io_instance->ssl = wolfSSL_new(tls_io_instance->ssl_context);
Azure.IoT Build 6:c55b013dfc2a 298 if (tls_io_instance->ssl == NULL)
Azure.IoT Build 6:c55b013dfc2a 299 {
Azure.IoT Build 6:c55b013dfc2a 300 wolfSSL_CTX_free(tls_io_instance->ssl_context);
Azure.IoT Build 6:c55b013dfc2a 301 result = __LINE__;
Azure.IoT Build 6:c55b013dfc2a 302 }
Azure.IoT Build 6:c55b013dfc2a 303 else
Azure.IoT Build 6:c55b013dfc2a 304 {
Azure.IoT Build 6:c55b013dfc2a 305 tls_io_instance->socket_io_read_bytes = NULL;
Azure.IoT Build 6:c55b013dfc2a 306 tls_io_instance->socket_io_read_byte_count = 0;
Azure.IoT Build 6:c55b013dfc2a 307 tls_io_instance->on_send_complete = NULL;
Azure.IoT Build 6:c55b013dfc2a 308 tls_io_instance->on_send_complete_callback_context = NULL;
Azure.IoT Build 6:c55b013dfc2a 309
Azure.IoT Build 6:c55b013dfc2a 310 wolfSSL_set_using_nonblock(tls_io_instance->ssl, 1);
Azure.IoT Build 6:c55b013dfc2a 311 wolfSSL_SetIOSend(tls_io_instance->ssl_context, on_io_send);
Azure.IoT Build 6:c55b013dfc2a 312 wolfSSL_SetIORecv(tls_io_instance->ssl_context, on_io_recv);
Azure.IoT Build 6:c55b013dfc2a 313 wolfSSL_SetHsDoneCb(tls_io_instance->ssl, on_handshake_done, tls_io_instance);
Azure.IoT Build 6:c55b013dfc2a 314 wolfSSL_SetIOWriteCtx(tls_io_instance->ssl, tls_io_instance);
Azure.IoT Build 6:c55b013dfc2a 315 wolfSSL_SetIOReadCtx(tls_io_instance->ssl, tls_io_instance);
Azure.IoT Build 6:c55b013dfc2a 316
Azure.IoT Build 6:c55b013dfc2a 317 tls_io_instance->tlsio_state = TLSIO_STATE_NOT_OPEN;
Azure.IoT Build 6:c55b013dfc2a 318 result = 0;
Azure.IoT Build 6:c55b013dfc2a 319 }
Azure.IoT Build 6:c55b013dfc2a 320 }
Azure.IoT Build 6:c55b013dfc2a 321 return result;
Azure.IoT Build 6:c55b013dfc2a 322 }
Azure.IoT Build 6:c55b013dfc2a 323
Azure.IoT Build 6:c55b013dfc2a 324 static void destroy_wolfssl_instance(TLS_IO_INSTANCE* tls_io_instance)
Azure.IoT Build 6:c55b013dfc2a 325 {
Azure.IoT Build 6:c55b013dfc2a 326 wolfSSL_free(tls_io_instance->ssl);
Azure.IoT Build 6:c55b013dfc2a 327 }
Azure.IoT Build 6:c55b013dfc2a 328
Azure.IoT Build 0:fa2de1b79154 329 int tlsio_wolfssl_init(void)
Azure.IoT Build 0:fa2de1b79154 330 {
Azure.IoT Build 0:fa2de1b79154 331 (void)wolfSSL_library_init();
Azure.IoT Build 0:fa2de1b79154 332 wolfSSL_load_error_strings();
Azure.IoT Build 0:fa2de1b79154 333
Azure.IoT Build 0:fa2de1b79154 334 return 0;
Azure.IoT Build 0:fa2de1b79154 335 }
Azure.IoT Build 0:fa2de1b79154 336
Azure.IoT Build 0:fa2de1b79154 337 void tlsio_wolfssl_deinit(void)
Azure.IoT Build 0:fa2de1b79154 338 {
Azure.IoT Build 0:fa2de1b79154 339 }
Azure.IoT Build 0:fa2de1b79154 340
Azure.IoT Build 6:c55b013dfc2a 341 CONCRETE_IO_HANDLE tlsio_wolfssl_create(void* io_create_parameters)
Azure.IoT Build 0:fa2de1b79154 342 {
Azure.IoT Build 0:fa2de1b79154 343 TLSIO_CONFIG* tls_io_config = io_create_parameters;
Azure.IoT Build 0:fa2de1b79154 344 TLS_IO_INSTANCE* result;
Azure.IoT Build 0:fa2de1b79154 345
Azure.IoT Build 0:fa2de1b79154 346 if (tls_io_config == NULL)
Azure.IoT Build 0:fa2de1b79154 347 {
Azure.IoT Build 0:fa2de1b79154 348 result = NULL;
Azure.IoT Build 0:fa2de1b79154 349 }
Azure.IoT Build 0:fa2de1b79154 350 else
Azure.IoT Build 0:fa2de1b79154 351 {
Azure.IoT Build 6:c55b013dfc2a 352 result = (TLS_IO_INSTANCE*)malloc(sizeof(TLS_IO_INSTANCE));
Azure.IoT Build 0:fa2de1b79154 353 if (result != NULL)
Azure.IoT Build 0:fa2de1b79154 354 {
Azure.IoT Build 6:c55b013dfc2a 355 memset(result, 0, sizeof(TLS_IO_INSTANCE));
Azure.IoT Build 6:c55b013dfc2a 356 mallocAndStrcpy_s(&result->hostname, tls_io_config->hostname);
Azure.IoT Build 6:c55b013dfc2a 357 result->port = tls_io_config->port;
Azure.IoT Build 0:fa2de1b79154 358
Azure.IoT Build 6:c55b013dfc2a 359 result->socket_io_read_bytes = 0;
Azure.IoT Build 6:c55b013dfc2a 360 result->socket_io_read_byte_count = 0;
Azure.IoT Build 6:c55b013dfc2a 361 result->socket_io = NULL;
Azure.IoT Build 6:c55b013dfc2a 362
Azure.IoT Build 6:c55b013dfc2a 363 result->ssl = NULL;
Azure.IoT Build 6:c55b013dfc2a 364 result->ssl_context = NULL;
Azure.IoT Build 6:c55b013dfc2a 365 result->certificate = NULL;
Azure.IoT Build 0:fa2de1b79154 366
Azure.IoT Build 0:fa2de1b79154 367 result->on_bytes_received = NULL;
Azure.IoT Build 0:fa2de1b79154 368 result->on_bytes_received_context = NULL;
Azure.IoT Build 0:fa2de1b79154 369
Azure.IoT Build 0:fa2de1b79154 370 result->on_io_open_complete = NULL;
Azure.IoT Build 0:fa2de1b79154 371 result->on_io_open_complete_context = NULL;
Azure.IoT Build 0:fa2de1b79154 372
Azure.IoT Build 0:fa2de1b79154 373 result->on_io_close_complete = NULL;
Azure.IoT Build 0:fa2de1b79154 374 result->on_io_close_complete_context = NULL;
Azure.IoT Build 0:fa2de1b79154 375
Azure.IoT Build 0:fa2de1b79154 376 result->on_io_error = NULL;
Azure.IoT Build 0:fa2de1b79154 377 result->on_io_error_context = NULL;
Azure.IoT Build 0:fa2de1b79154 378
Azure.IoT Build 6:c55b013dfc2a 379 result->tlsio_state = TLSIO_STATE_NOT_OPEN;
Azure.IoT Build 0:fa2de1b79154 380
Azure.IoT Build 0:fa2de1b79154 381 result->ssl_context = wolfSSL_CTX_new(wolfTLSv1_client_method());
Azure.IoT Build 0:fa2de1b79154 382 if (result->ssl_context == NULL)
Azure.IoT Build 0:fa2de1b79154 383 {
Azure.IoT Build 0:fa2de1b79154 384 free(result);
Azure.IoT Build 0:fa2de1b79154 385 result = NULL;
Azure.IoT Build 0:fa2de1b79154 386 }
Azure.IoT Build 0:fa2de1b79154 387 else
Azure.IoT Build 0:fa2de1b79154 388 {
Azure.IoT Build 0:fa2de1b79154 389 const IO_INTERFACE_DESCRIPTION* socket_io_interface = socketio_get_interface_description();
Azure.IoT Build 0:fa2de1b79154 390 if (socket_io_interface == NULL)
Azure.IoT Build 0:fa2de1b79154 391 {
Azure.IoT Build 0:fa2de1b79154 392 wolfSSL_CTX_free(result->ssl_context);
Azure.IoT Build 0:fa2de1b79154 393 free(result);
Azure.IoT Build 0:fa2de1b79154 394 result = NULL;
Azure.IoT Build 0:fa2de1b79154 395 }
Azure.IoT Build 0:fa2de1b79154 396 else
Azure.IoT Build 0:fa2de1b79154 397 {
Azure.IoT Build 6:c55b013dfc2a 398 SOCKETIO_CONFIG socketio_config;
Azure.IoT Build 6:c55b013dfc2a 399 socketio_config.hostname = result->hostname;
Azure.IoT Build 6:c55b013dfc2a 400 socketio_config.port = result->port;
Azure.IoT Build 6:c55b013dfc2a 401 socketio_config.accepted_socket = NULL;
Azure.IoT Build 6:c55b013dfc2a 402
Azure.IoT Build 6:c55b013dfc2a 403 result->socket_io = xio_create(socket_io_interface, &socketio_config);
Azure.IoT Build 0:fa2de1b79154 404 if (result->socket_io == NULL)
Azure.IoT Build 0:fa2de1b79154 405 {
Azure.IoT Build 6:c55b013dfc2a 406 LogError("Failure connecting to underlying socket_io");
Azure.IoT Build 0:fa2de1b79154 407 wolfSSL_CTX_free(result->ssl_context);
Azure.IoT Build 0:fa2de1b79154 408 free(result);
Azure.IoT Build 0:fa2de1b79154 409 result = NULL;
Azure.IoT Build 0:fa2de1b79154 410 }
Azure.IoT Build 0:fa2de1b79154 411 }
Azure.IoT Build 0:fa2de1b79154 412 }
Azure.IoT Build 6:c55b013dfc2a 413
Azure.IoT Build 6:c55b013dfc2a 414
Azure.IoT Build 0:fa2de1b79154 415 }
Azure.IoT Build 0:fa2de1b79154 416 }
Azure.IoT Build 0:fa2de1b79154 417
Azure.IoT Build 0:fa2de1b79154 418 return result;
Azure.IoT Build 0:fa2de1b79154 419 }
Azure.IoT Build 0:fa2de1b79154 420
Azure.IoT Build 0:fa2de1b79154 421 void tlsio_wolfssl_destroy(CONCRETE_IO_HANDLE tls_io)
Azure.IoT Build 0:fa2de1b79154 422 {
Azure.IoT Build 0:fa2de1b79154 423 if (tls_io != NULL)
Azure.IoT Build 0:fa2de1b79154 424 {
Azure.IoT Build 0:fa2de1b79154 425 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io;
Azure.IoT Build 0:fa2de1b79154 426 if (tls_io_instance->socket_io_read_bytes != NULL)
Azure.IoT Build 0:fa2de1b79154 427 {
Azure.IoT Build 0:fa2de1b79154 428 free(tls_io_instance->socket_io_read_bytes);
Azure.IoT Build 0:fa2de1b79154 429 }
Azure.IoT Build 0:fa2de1b79154 430
Azure.IoT Build 6:c55b013dfc2a 431 if (tls_io_instance->certificate != NULL)
Azure.IoT Build 6:c55b013dfc2a 432 {
Azure.IoT Build 6:c55b013dfc2a 433 free(tls_io_instance->certificate);
Azure.IoT Build 6:c55b013dfc2a 434 tls_io_instance->certificate = NULL;
Azure.IoT Build 6:c55b013dfc2a 435 }
Azure.IoT Build 6:c55b013dfc2a 436 wolfSSL_CTX_free(tls_io_instance->ssl_context);
Azure.IoT Build 0:fa2de1b79154 437 xio_destroy(tls_io_instance->socket_io);
Azure.IoT Build 0:fa2de1b79154 438 free(tls_io);
Azure.IoT Build 0:fa2de1b79154 439 }
Azure.IoT Build 0:fa2de1b79154 440 }
Azure.IoT Build 0:fa2de1b79154 441
Azure.IoT Build 0:fa2de1b79154 442 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 443 {
Azure.IoT Build 0:fa2de1b79154 444 int result;
Azure.IoT Build 0:fa2de1b79154 445
Azure.IoT Build 0:fa2de1b79154 446 if (tls_io == NULL)
Azure.IoT Build 0:fa2de1b79154 447 {
Azure.IoT Build 0:fa2de1b79154 448 result = __LINE__;
Azure.IoT Build 0:fa2de1b79154 449 }
Azure.IoT Build 0:fa2de1b79154 450 else
Azure.IoT Build 0:fa2de1b79154 451 {
Azure.IoT Build 0:fa2de1b79154 452 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io;
Azure.IoT Build 0:fa2de1b79154 453
Azure.IoT Build 0:fa2de1b79154 454 if (tls_io_instance->tlsio_state != TLSIO_STATE_NOT_OPEN)
Azure.IoT Build 0:fa2de1b79154 455 {
Azure.IoT Build 6:c55b013dfc2a 456 LogError("Invalid state encountered.");
Azure.IoT Build 0:fa2de1b79154 457 result = __LINE__;
Azure.IoT Build 0:fa2de1b79154 458 }
Azure.IoT Build 0:fa2de1b79154 459 else
Azure.IoT Build 0:fa2de1b79154 460 {
Azure.IoT Build 0:fa2de1b79154 461 tls_io_instance->on_bytes_received = on_bytes_received;
Azure.IoT Build 0:fa2de1b79154 462 tls_io_instance->on_bytes_received_context = on_bytes_received_context;
Azure.IoT Build 0:fa2de1b79154 463
Azure.IoT Build 0:fa2de1b79154 464 tls_io_instance->on_io_open_complete = on_io_open_complete;
Azure.IoT Build 0:fa2de1b79154 465 tls_io_instance->on_io_open_complete_context = on_io_open_complete_context;
Azure.IoT Build 0:fa2de1b79154 466
Azure.IoT Build 0:fa2de1b79154 467 tls_io_instance->on_io_error = on_io_error;
Azure.IoT Build 0:fa2de1b79154 468 tls_io_instance->on_io_error_context = on_io_error_context;
Azure.IoT Build 0:fa2de1b79154 469
Azure.IoT Build 0:fa2de1b79154 470 tls_io_instance->tlsio_state = TLSIO_STATE_OPENING_UNDERLYING_IO;
Azure.IoT Build 0:fa2de1b79154 471
Azure.IoT Build 6:c55b013dfc2a 472 if (create_wolfssl_instance(tls_io_instance) != 0)
Azure.IoT Build 6:c55b013dfc2a 473 {
Azure.IoT Build 6:c55b013dfc2a 474 tls_io_instance->tlsio_state = TLSIO_STATE_NOT_OPEN;
Azure.IoT Build 6:c55b013dfc2a 475 result = __LINE__;
Azure.IoT Build 6:c55b013dfc2a 476 }
Azure.IoT Build 6:c55b013dfc2a 477 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 478 {
Azure.IoT Build 0:fa2de1b79154 479 tls_io_instance->tlsio_state = TLSIO_STATE_NOT_OPEN;
Azure.IoT Build 0:fa2de1b79154 480 result = __LINE__;
Azure.IoT Build 0:fa2de1b79154 481 }
Azure.IoT Build 0:fa2de1b79154 482 else
Azure.IoT Build 0:fa2de1b79154 483 {
Azure.IoT Build 6:c55b013dfc2a 484 // The state can get changed in the on_underlying_io_open_complete
Azure.IoT Build 6:c55b013dfc2a 485 if (tls_io_instance->tlsio_state == TLSIO_STATE_ERROR)
Azure.IoT Build 0:fa2de1b79154 486 {
Azure.IoT Build 6:c55b013dfc2a 487 LogError("Failed to connect to server. The certificates may not be correct.");
Azure.IoT Build 0:fa2de1b79154 488 result = __LINE__;
Azure.IoT Build 0:fa2de1b79154 489 }
Azure.IoT Build 0:fa2de1b79154 490 else
Azure.IoT Build 0:fa2de1b79154 491 {
Azure.IoT Build 6:c55b013dfc2a 492 int res;
Azure.IoT Build 6:c55b013dfc2a 493 tls_io_instance->tlsio_state = TLSIO_STATE_IN_HANDSHAKE;
Azure.IoT Build 6:c55b013dfc2a 494 res = wolfSSL_connect(tls_io_instance->ssl);
Azure.IoT Build 6:c55b013dfc2a 495 if (res != SSL_SUCCESS)
Azure.IoT Build 6:c55b013dfc2a 496 {
Azure.IoT Build 6:c55b013dfc2a 497 LogError("Failed to connect to server.");
Azure.IoT Build 6:c55b013dfc2a 498 tls_io_instance->tlsio_state = TLSIO_STATE_NOT_OPEN;
Azure.IoT Build 6:c55b013dfc2a 499 result = __LINE__;
Azure.IoT Build 6:c55b013dfc2a 500 }
Azure.IoT Build 6:c55b013dfc2a 501 else
Azure.IoT Build 6:c55b013dfc2a 502 {
Azure.IoT Build 6:c55b013dfc2a 503 result = 0;
Azure.IoT Build 6:c55b013dfc2a 504 }
Azure.IoT Build 0:fa2de1b79154 505 }
Azure.IoT Build 0:fa2de1b79154 506 }
Azure.IoT Build 0:fa2de1b79154 507 }
Azure.IoT Build 0:fa2de1b79154 508 }
Azure.IoT Build 0:fa2de1b79154 509
Azure.IoT Build 0:fa2de1b79154 510 return result;
Azure.IoT Build 0:fa2de1b79154 511 }
Azure.IoT Build 0:fa2de1b79154 512
Azure.IoT Build 0:fa2de1b79154 513 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 514 {
Azure.IoT Build 0:fa2de1b79154 515 int result = 0;
Azure.IoT Build 0:fa2de1b79154 516
Azure.IoT Build 0:fa2de1b79154 517 if (tls_io == NULL)
Azure.IoT Build 0:fa2de1b79154 518 {
Azure.IoT Build 0:fa2de1b79154 519 result = __LINE__;
Azure.IoT Build 0:fa2de1b79154 520 }
Azure.IoT Build 0:fa2de1b79154 521 else
Azure.IoT Build 0:fa2de1b79154 522 {
Azure.IoT Build 0:fa2de1b79154 523 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io;
Azure.IoT Build 0:fa2de1b79154 524
Azure.IoT Build 0:fa2de1b79154 525 if ((tls_io_instance->tlsio_state == TLSIO_STATE_NOT_OPEN) ||
Azure.IoT Build 0:fa2de1b79154 526 (tls_io_instance->tlsio_state == TLSIO_STATE_CLOSING))
Azure.IoT Build 0:fa2de1b79154 527 {
Azure.IoT Build 0:fa2de1b79154 528 result = __LINE__;
Azure.IoT Build 0:fa2de1b79154 529 }
Azure.IoT Build 0:fa2de1b79154 530 else
Azure.IoT Build 0:fa2de1b79154 531 {
Azure.IoT Build 0:fa2de1b79154 532 tls_io_instance->tlsio_state = TLSIO_STATE_CLOSING;
Azure.IoT Build 0:fa2de1b79154 533 tls_io_instance->on_io_close_complete = on_io_close_complete;
Azure.IoT Build 0:fa2de1b79154 534 tls_io_instance->on_io_close_complete_context = callback_context;
Azure.IoT Build 0:fa2de1b79154 535
Azure.IoT Build 0:fa2de1b79154 536 if (xio_close(tls_io_instance->socket_io, on_underlying_io_close_complete, tls_io_instance) != 0)
Azure.IoT Build 0:fa2de1b79154 537 {
Azure.IoT Build 0:fa2de1b79154 538 result = __LINE__;
Azure.IoT Build 0:fa2de1b79154 539 }
Azure.IoT Build 0:fa2de1b79154 540 else
Azure.IoT Build 0:fa2de1b79154 541 {
Azure.IoT Build 6:c55b013dfc2a 542 destroy_wolfssl_instance(tls_io_instance);
Azure.IoT Build 0:fa2de1b79154 543 result = 0;
Azure.IoT Build 0:fa2de1b79154 544 }
Azure.IoT Build 0:fa2de1b79154 545 }
Azure.IoT Build 0:fa2de1b79154 546 }
Azure.IoT Build 0:fa2de1b79154 547
Azure.IoT Build 0:fa2de1b79154 548 return result;
Azure.IoT Build 0:fa2de1b79154 549 }
Azure.IoT Build 0:fa2de1b79154 550
Azure.IoT Build 0:fa2de1b79154 551 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 552 {
Azure.IoT Build 0:fa2de1b79154 553 int result;
Azure.IoT Build 0:fa2de1b79154 554
Azure.IoT Build 0:fa2de1b79154 555 if (tls_io == NULL)
Azure.IoT Build 0:fa2de1b79154 556 {
Azure.IoT Build 0:fa2de1b79154 557 result = __LINE__;
Azure.IoT Build 0:fa2de1b79154 558 }
Azure.IoT Build 0:fa2de1b79154 559 else
Azure.IoT Build 0:fa2de1b79154 560 {
Azure.IoT Build 0:fa2de1b79154 561 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io;
Azure.IoT Build 0:fa2de1b79154 562
Azure.IoT Build 0:fa2de1b79154 563 if (tls_io_instance->tlsio_state != TLSIO_STATE_OPEN)
Azure.IoT Build 0:fa2de1b79154 564 {
Azure.IoT Build 0:fa2de1b79154 565 result = __LINE__;
Azure.IoT Build 0:fa2de1b79154 566 }
Azure.IoT Build 0:fa2de1b79154 567 else
Azure.IoT Build 0:fa2de1b79154 568 {
Azure.IoT Build 0:fa2de1b79154 569 tls_io_instance->on_send_complete = on_send_complete;
Azure.IoT Build 0:fa2de1b79154 570 tls_io_instance->on_send_complete_callback_context = callback_context;
Azure.IoT Build 0:fa2de1b79154 571
Azure.IoT Build 0:fa2de1b79154 572 int res = wolfSSL_write(tls_io_instance->ssl, buffer, size);
Azure.IoT Build 0:fa2de1b79154 573 if (res != size)
Azure.IoT Build 0:fa2de1b79154 574 {
Azure.IoT Build 0:fa2de1b79154 575 result = __LINE__;
Azure.IoT Build 0:fa2de1b79154 576 }
Azure.IoT Build 0:fa2de1b79154 577 else
Azure.IoT Build 0:fa2de1b79154 578 {
Azure.IoT Build 0:fa2de1b79154 579 result = 0;
Azure.IoT Build 0:fa2de1b79154 580 }
Azure.IoT Build 0:fa2de1b79154 581 }
Azure.IoT Build 0:fa2de1b79154 582 }
Azure.IoT Build 0:fa2de1b79154 583
Azure.IoT Build 0:fa2de1b79154 584 return result;
Azure.IoT Build 0:fa2de1b79154 585 }
Azure.IoT Build 0:fa2de1b79154 586
Azure.IoT Build 0:fa2de1b79154 587 void tlsio_wolfssl_dowork(CONCRETE_IO_HANDLE tls_io)
Azure.IoT Build 0:fa2de1b79154 588 {
Azure.IoT Build 0:fa2de1b79154 589 if (tls_io != NULL)
Azure.IoT Build 0:fa2de1b79154 590 {
Azure.IoT Build 0:fa2de1b79154 591 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io;
Azure.IoT Build 0:fa2de1b79154 592
Azure.IoT Build 0:fa2de1b79154 593 if ((tls_io_instance->tlsio_state != TLSIO_STATE_NOT_OPEN) &&
Azure.IoT Build 0:fa2de1b79154 594 (tls_io_instance->tlsio_state != TLSIO_STATE_ERROR))
Azure.IoT Build 0:fa2de1b79154 595 {
Azure.IoT Build 0:fa2de1b79154 596 decode_ssl_received_bytes(tls_io_instance);
Azure.IoT Build 0:fa2de1b79154 597 xio_dowork(tls_io_instance->socket_io);
Azure.IoT Build 0:fa2de1b79154 598 }
Azure.IoT Build 0:fa2de1b79154 599 }
Azure.IoT Build 0:fa2de1b79154 600 }
Azure.IoT Build 0:fa2de1b79154 601
Azure.IoT Build 0:fa2de1b79154 602 const IO_INTERFACE_DESCRIPTION* tlsio_wolfssl_get_interface_description(void)
Azure.IoT Build 0:fa2de1b79154 603 {
Azure.IoT Build 0:fa2de1b79154 604 return &tlsio_wolfssl_interface_description;
Azure.IoT Build 0:fa2de1b79154 605 }
Azure.IoT Build 0:fa2de1b79154 606
Azure.IoT Build 0:fa2de1b79154 607 int tlsio_wolfssl_setoption(CONCRETE_IO_HANDLE tls_io, const char* optionName, const void* value)
Azure.IoT Build 0:fa2de1b79154 608 {
Azure.IoT Build 0:fa2de1b79154 609 int result;
Azure.IoT Build 0:fa2de1b79154 610
Azure.IoT Build 0:fa2de1b79154 611 if (tls_io == NULL || optionName == NULL)
Azure.IoT Build 0:fa2de1b79154 612 {
Azure.IoT Build 0:fa2de1b79154 613 result = __LINE__;
Azure.IoT Build 0:fa2de1b79154 614 }
Azure.IoT Build 0:fa2de1b79154 615 else
Azure.IoT Build 0:fa2de1b79154 616 {
Azure.IoT Build 0:fa2de1b79154 617 TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io;
Azure.IoT Build 0:fa2de1b79154 618
Azure.IoT Build 0:fa2de1b79154 619 if (strcmp("TrustedCerts", optionName) == 0)
Azure.IoT Build 0:fa2de1b79154 620 {
Azure.IoT Build 6:c55b013dfc2a 621 const char* cert = (const char*)value;
Azure.IoT Build 6:c55b013dfc2a 622 if (tls_io_instance->certificate != NULL)
Azure.IoT Build 6:c55b013dfc2a 623 {
Azure.IoT Build 6:c55b013dfc2a 624 // Free the memory if it has been previously allocated
Azure.IoT Build 6:c55b013dfc2a 625 free(tls_io_instance->certificate);
Azure.IoT Build 6:c55b013dfc2a 626 }
Azure.IoT Build 6:c55b013dfc2a 627
Azure.IoT Build 6:c55b013dfc2a 628 // Store the certificate
Azure.IoT Build 6:c55b013dfc2a 629 size_t len = strlen(cert);
Azure.IoT Build 6:c55b013dfc2a 630 tls_io_instance->certificate = (const char*)malloc(len+1);
Azure.IoT Build 6:c55b013dfc2a 631 if (tls_io_instance->certificate == NULL)
Azure.IoT Build 0:fa2de1b79154 632 {
Azure.IoT Build 0:fa2de1b79154 633 result = __LINE__;
Azure.IoT Build 0:fa2de1b79154 634 }
Azure.IoT Build 0:fa2de1b79154 635 else
Azure.IoT Build 0:fa2de1b79154 636 {
Azure.IoT Build 6:c55b013dfc2a 637 (void)strcpy(tls_io_instance->certificate, cert);
Azure.IoT Build 0:fa2de1b79154 638 result = 0;
Azure.IoT Build 0:fa2de1b79154 639 }
Azure.IoT Build 0:fa2de1b79154 640 }
AzureIoTClient 1:9190c0f4d23a 641 else if (tls_io_instance->socket_io == NULL)
AzureIoTClient 1:9190c0f4d23a 642 {
AzureIoTClient 1:9190c0f4d23a 643 result = __LINE__;
AzureIoTClient 1:9190c0f4d23a 644 }
Azure.IoT Build 0:fa2de1b79154 645 else
Azure.IoT Build 0:fa2de1b79154 646 {
Azure.IoT Build 0:fa2de1b79154 647 result = xio_setoption(tls_io_instance->socket_io, optionName, value);
Azure.IoT Build 0:fa2de1b79154 648 }
Azure.IoT Build 0:fa2de1b79154 649 }
Azure.IoT Build 0:fa2de1b79154 650
Azure.IoT Build 0:fa2de1b79154 651 return result;
Azure.IoT Build 0:fa2de1b79154 652 }