Azure IoT common library

Fork of azure_c_shared_utility by Azure IoT

Committer:
AzureIoTClient
Date:
Sun Apr 24 16:41:14 2016 -0700
Revision:
1:9190c0f4d23a
Parent:
0:fa2de1b79154
Child:
6:c55b013dfc2a
1.0.5

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