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