Azure IoT common library
Dependents: STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more
tlsio_wolfssl.c@21:b92006c5b9ff, 2017-02-24 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Fri Feb 24 14:01:41 2017 -0800
- Revision:
- 21:b92006c5b9ff
- Parent:
- 19:2e0811512ceb
- Child:
- 22:10640b226104
1.1.8
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> |
Azure.IoT Build | 0:fa2de1b79154 | 5 | #include "wolfssl/ssl.h" |
Azure.IoT Build | 0:fa2de1b79154 | 6 | #include "wolfssl/error-ssl.h" |
Azure.IoT Build | 0:fa2de1b79154 | 7 | #include <stdio.h> |
Azure.IoT Build | 0:fa2de1b79154 | 8 | #include <stdbool.h> |
Azure.IoT Build | 0:fa2de1b79154 | 9 | #include <string.h> |
Azure.IoT Build | 0:fa2de1b79154 | 10 | #include "azure_c_shared_utility/tlsio.h" |
Azure.IoT Build | 0:fa2de1b79154 | 11 | #include "azure_c_shared_utility/tlsio_wolfssl.h" |
Azure.IoT Build | 0:fa2de1b79154 | 12 | #include "azure_c_shared_utility/socketio.h" |
Azure.IoT Build | 6:c55b013dfc2a | 13 | #include "azure_c_shared_utility/crt_abstractions.h" |
AzureIoTClient | 21:b92006c5b9ff | 14 | #include "azure_c_shared_utility/optimize_size.h" |
Azure.IoT Build | 6:c55b013dfc2a | 15 | #include "azure_c_shared_utility/xlogging.h" |
AzureIoTClient | 15:956c6d205aa7 | 16 | #include "azure_c_shared_utility/shared_util_options.h" |
AzureIoTClient | 15:956c6d205aa7 | 17 | |
Azure.IoT Build | 0:fa2de1b79154 | 18 | |
Azure.IoT Build | 0:fa2de1b79154 | 19 | typedef enum TLSIO_STATE_ENUM_TAG |
Azure.IoT Build | 0:fa2de1b79154 | 20 | { |
Azure.IoT Build | 0:fa2de1b79154 | 21 | TLSIO_STATE_NOT_OPEN, |
Azure.IoT Build | 0:fa2de1b79154 | 22 | TLSIO_STATE_OPENING_UNDERLYING_IO, |
Azure.IoT Build | 0:fa2de1b79154 | 23 | TLSIO_STATE_IN_HANDSHAKE, |
Azure.IoT Build | 0:fa2de1b79154 | 24 | TLSIO_STATE_OPEN, |
Azure.IoT Build | 0:fa2de1b79154 | 25 | TLSIO_STATE_CLOSING, |
Azure.IoT Build | 0:fa2de1b79154 | 26 | TLSIO_STATE_ERROR |
Azure.IoT Build | 0:fa2de1b79154 | 27 | } TLSIO_STATE_ENUM; |
Azure.IoT Build | 0:fa2de1b79154 | 28 | |
Azure.IoT Build | 0:fa2de1b79154 | 29 | typedef struct TLS_IO_INSTANCE_TAG |
Azure.IoT Build | 0:fa2de1b79154 | 30 | { |
Azure.IoT Build | 0:fa2de1b79154 | 31 | XIO_HANDLE socket_io; |
Azure.IoT Build | 0:fa2de1b79154 | 32 | ON_BYTES_RECEIVED on_bytes_received; |
Azure.IoT Build | 0:fa2de1b79154 | 33 | ON_IO_OPEN_COMPLETE on_io_open_complete; |
Azure.IoT Build | 0:fa2de1b79154 | 34 | ON_IO_CLOSE_COMPLETE on_io_close_complete; |
Azure.IoT Build | 0:fa2de1b79154 | 35 | ON_IO_ERROR on_io_error; |
Azure.IoT Build | 0:fa2de1b79154 | 36 | void* on_bytes_received_context; |
Azure.IoT Build | 0:fa2de1b79154 | 37 | void* on_io_open_complete_context; |
Azure.IoT Build | 0:fa2de1b79154 | 38 | void* on_io_close_complete_context; |
Azure.IoT Build | 0:fa2de1b79154 | 39 | void* on_io_error_context; |
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 | 6:c55b013dfc2a | 47 | char* certificate; |
AzureIoTClient | 15:956c6d205aa7 | 48 | char* x509certificate; |
AzureIoTClient | 15:956c6d205aa7 | 49 | char* x509privatekey; |
Azure.IoT Build | 6:c55b013dfc2a | 50 | char* hostname; |
Azure.IoT Build | 6:c55b013dfc2a | 51 | int port; |
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 | 10:1be0bc9a9deb | 65 | if (strcmp(name, "TrustedCerts") == 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 | 15:956c6d205aa7 | 120 | if ((strcmp(name, "TrustedCerts") == 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 | 19:2e0811512ceb | 172 | (tls_io_instance->x509privatekey != NULL) && |
AzureIoTClient | 19:2e0811512ceb | 173 | (OptionHandler_AddOption(result, "TrustedCerts", 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 | { |
Azure.IoT Build | 0:fa2de1b79154 | 267 | TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context; |
Azure.IoT Build | 0:fa2de1b79154 | 268 | |
Azure.IoT Build | 0:fa2de1b79154 | 269 | 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 | 270 | if (new_socket_io_read_bytes == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 271 | { |
AzureIoTClient | 19:2e0811512ceb | 272 | LogError("Failed allocating memory for received bytes"); |
Azure.IoT Build | 0:fa2de1b79154 | 273 | tls_io_instance->tlsio_state = TLSIO_STATE_ERROR; |
Azure.IoT Build | 0:fa2de1b79154 | 274 | indicate_error(tls_io_instance); |
Azure.IoT Build | 0:fa2de1b79154 | 275 | } |
Azure.IoT Build | 0:fa2de1b79154 | 276 | else |
Azure.IoT Build | 0:fa2de1b79154 | 277 | { |
Azure.IoT Build | 0:fa2de1b79154 | 278 | tls_io_instance->socket_io_read_bytes = new_socket_io_read_bytes; |
Azure.IoT Build | 0:fa2de1b79154 | 279 | (void)memcpy(tls_io_instance->socket_io_read_bytes + tls_io_instance->socket_io_read_byte_count, buffer, size); |
Azure.IoT Build | 0:fa2de1b79154 | 280 | tls_io_instance->socket_io_read_byte_count += size; |
Azure.IoT Build | 0:fa2de1b79154 | 281 | } |
Azure.IoT Build | 0:fa2de1b79154 | 282 | } |
Azure.IoT Build | 0:fa2de1b79154 | 283 | |
Azure.IoT Build | 0:fa2de1b79154 | 284 | static void on_underlying_io_error(void* context) |
Azure.IoT Build | 0:fa2de1b79154 | 285 | { |
Azure.IoT Build | 0:fa2de1b79154 | 286 | TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context; |
Azure.IoT Build | 0:fa2de1b79154 | 287 | |
Azure.IoT Build | 0:fa2de1b79154 | 288 | switch (tls_io_instance->tlsio_state) |
Azure.IoT Build | 0:fa2de1b79154 | 289 | { |
AzureIoTClient | 15:956c6d205aa7 | 290 | default: |
AzureIoTClient | 19:2e0811512ceb | 291 | LogError("Unknown TLS IO WolfSSL state: %d", (int)tls_io_instance->tlsio_state); |
AzureIoTClient | 19:2e0811512ceb | 292 | break; |
AzureIoTClient | 19:2e0811512ceb | 293 | |
AzureIoTClient | 15:956c6d205aa7 | 294 | case TLSIO_STATE_NOT_OPEN: |
AzureIoTClient | 15:956c6d205aa7 | 295 | case TLSIO_STATE_ERROR: |
AzureIoTClient | 15:956c6d205aa7 | 296 | break; |
Azure.IoT Build | 0:fa2de1b79154 | 297 | |
AzureIoTClient | 15:956c6d205aa7 | 298 | case TLSIO_STATE_OPENING_UNDERLYING_IO: |
AzureIoTClient | 15:956c6d205aa7 | 299 | case TLSIO_STATE_IN_HANDSHAKE: |
AzureIoTClient | 15:956c6d205aa7 | 300 | tls_io_instance->tlsio_state = TLSIO_STATE_ERROR; |
AzureIoTClient | 15:956c6d205aa7 | 301 | indicate_open_complete(tls_io_instance, IO_OPEN_ERROR); |
AzureIoTClient | 15:956c6d205aa7 | 302 | break; |
Azure.IoT Build | 0:fa2de1b79154 | 303 | |
AzureIoTClient | 15:956c6d205aa7 | 304 | case TLSIO_STATE_OPEN: |
AzureIoTClient | 15:956c6d205aa7 | 305 | tls_io_instance->tlsio_state = TLSIO_STATE_ERROR; |
AzureIoTClient | 15:956c6d205aa7 | 306 | indicate_error(tls_io_instance); |
AzureIoTClient | 15:956c6d205aa7 | 307 | break; |
Azure.IoT Build | 0:fa2de1b79154 | 308 | } |
Azure.IoT Build | 0:fa2de1b79154 | 309 | } |
Azure.IoT Build | 0:fa2de1b79154 | 310 | |
Azure.IoT Build | 0:fa2de1b79154 | 311 | static void on_underlying_io_close_complete(void* context) |
Azure.IoT Build | 0:fa2de1b79154 | 312 | { |
Azure.IoT Build | 0:fa2de1b79154 | 313 | TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context; |
Azure.IoT Build | 0:fa2de1b79154 | 314 | |
AzureIoTClient | 19:2e0811512ceb | 315 | if (tls_io_instance->tlsio_state != TLSIO_STATE_CLOSING) |
AzureIoTClient | 19:2e0811512ceb | 316 | { |
AzureIoTClient | 19:2e0811512ceb | 317 | LogError("on_underlying_io_close_complete called when not in CLOSING state"); |
AzureIoTClient | 19:2e0811512ceb | 318 | } |
AzureIoTClient | 19:2e0811512ceb | 319 | else |
Azure.IoT Build | 0:fa2de1b79154 | 320 | { |
Azure.IoT Build | 0:fa2de1b79154 | 321 | if (tls_io_instance->on_io_close_complete != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 322 | { |
Azure.IoT Build | 0:fa2de1b79154 | 323 | tls_io_instance->on_io_close_complete(tls_io_instance->on_io_close_complete_context); |
Azure.IoT Build | 0:fa2de1b79154 | 324 | } |
Azure.IoT Build | 6:c55b013dfc2a | 325 | tls_io_instance->tlsio_state = TLSIO_STATE_NOT_OPEN; |
Azure.IoT Build | 0:fa2de1b79154 | 326 | } |
Azure.IoT Build | 0:fa2de1b79154 | 327 | } |
Azure.IoT Build | 0:fa2de1b79154 | 328 | |
Azure.IoT Build | 0:fa2de1b79154 | 329 | static int on_io_recv(WOLFSSL *ssl, char *buf, int sz, void *context) |
Azure.IoT Build | 0:fa2de1b79154 | 330 | { |
Azure.IoT Build | 0:fa2de1b79154 | 331 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 332 | TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context; |
Azure.IoT Build | 0:fa2de1b79154 | 333 | unsigned char* new_socket_io_read_bytes; |
Azure.IoT Build | 0:fa2de1b79154 | 334 | |
AzureIoTClient | 15:956c6d205aa7 | 335 | (void)ssl; |
Azure.IoT Build | 0:fa2de1b79154 | 336 | while (tls_io_instance->socket_io_read_byte_count == 0) |
Azure.IoT Build | 0:fa2de1b79154 | 337 | { |
Azure.IoT Build | 0:fa2de1b79154 | 338 | xio_dowork(tls_io_instance->socket_io); |
Azure.IoT Build | 6:c55b013dfc2a | 339 | if (tls_io_instance->tlsio_state != TLSIO_STATE_IN_HANDSHAKE) |
Azure.IoT Build | 0:fa2de1b79154 | 340 | { |
Azure.IoT Build | 0:fa2de1b79154 | 341 | break; |
Azure.IoT Build | 0:fa2de1b79154 | 342 | } |
Azure.IoT Build | 0:fa2de1b79154 | 343 | } |
Azure.IoT Build | 0:fa2de1b79154 | 344 | |
Azure.IoT Build | 0:fa2de1b79154 | 345 | result = tls_io_instance->socket_io_read_byte_count; |
Azure.IoT Build | 0:fa2de1b79154 | 346 | if (result > sz) |
Azure.IoT Build | 0:fa2de1b79154 | 347 | { |
Azure.IoT Build | 0:fa2de1b79154 | 348 | result = sz; |
Azure.IoT Build | 0:fa2de1b79154 | 349 | } |
Azure.IoT Build | 0:fa2de1b79154 | 350 | |
Azure.IoT Build | 0:fa2de1b79154 | 351 | if (result > 0) |
Azure.IoT Build | 0:fa2de1b79154 | 352 | { |
Azure.IoT Build | 0:fa2de1b79154 | 353 | (void)memcpy(buf, tls_io_instance->socket_io_read_bytes, result); |
Azure.IoT Build | 0:fa2de1b79154 | 354 | (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 | 355 | tls_io_instance->socket_io_read_byte_count -= result; |
Azure.IoT Build | 0:fa2de1b79154 | 356 | if (tls_io_instance->socket_io_read_byte_count > 0) |
Azure.IoT Build | 0:fa2de1b79154 | 357 | { |
Azure.IoT Build | 0:fa2de1b79154 | 358 | 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 | 359 | if (new_socket_io_read_bytes != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 360 | { |
Azure.IoT Build | 0:fa2de1b79154 | 361 | tls_io_instance->socket_io_read_bytes = new_socket_io_read_bytes; |
Azure.IoT Build | 0:fa2de1b79154 | 362 | } |
Azure.IoT Build | 0:fa2de1b79154 | 363 | } |
Azure.IoT Build | 0:fa2de1b79154 | 364 | else |
Azure.IoT Build | 0:fa2de1b79154 | 365 | { |
Azure.IoT Build | 0:fa2de1b79154 | 366 | free(tls_io_instance->socket_io_read_bytes); |
Azure.IoT Build | 0:fa2de1b79154 | 367 | tls_io_instance->socket_io_read_bytes = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 368 | } |
Azure.IoT Build | 0:fa2de1b79154 | 369 | } |
Azure.IoT Build | 0:fa2de1b79154 | 370 | |
Azure.IoT Build | 0:fa2de1b79154 | 371 | if ((result == 0) && (tls_io_instance->tlsio_state == TLSIO_STATE_OPEN)) |
Azure.IoT Build | 0:fa2de1b79154 | 372 | { |
Azure.IoT Build | 0:fa2de1b79154 | 373 | result = WOLFSSL_CBIO_ERR_WANT_READ; |
Azure.IoT Build | 0:fa2de1b79154 | 374 | } |
Azure.IoT Build | 6:c55b013dfc2a | 375 | else if ((result == 0) && tls_io_instance->tlsio_state == TLSIO_STATE_CLOSING) |
Azure.IoT Build | 6:c55b013dfc2a | 376 | { |
Azure.IoT Build | 6:c55b013dfc2a | 377 | result = WOLFSSL_CBIO_ERR_CONN_CLOSE; |
Azure.IoT Build | 6:c55b013dfc2a | 378 | } |
Azure.IoT Build | 0:fa2de1b79154 | 379 | |
Azure.IoT Build | 0:fa2de1b79154 | 380 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 381 | } |
Azure.IoT Build | 0:fa2de1b79154 | 382 | |
Azure.IoT Build | 0:fa2de1b79154 | 383 | static int on_io_send(WOLFSSL *ssl, char *buf, int sz, void *context) |
Azure.IoT Build | 0:fa2de1b79154 | 384 | { |
Azure.IoT Build | 0:fa2de1b79154 | 385 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 386 | TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context; |
Azure.IoT Build | 0:fa2de1b79154 | 387 | |
AzureIoTClient | 15:956c6d205aa7 | 388 | (void)ssl; |
Azure.IoT Build | 0:fa2de1b79154 | 389 | 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 | 390 | { |
AzureIoTClient | 19:2e0811512ceb | 391 | LogError("Failed sending bytes through underlying IO"); |
Azure.IoT Build | 0:fa2de1b79154 | 392 | tls_io_instance->tlsio_state = TLSIO_STATE_ERROR; |
Azure.IoT Build | 0:fa2de1b79154 | 393 | indicate_error(tls_io_instance); |
Azure.IoT Build | 0:fa2de1b79154 | 394 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 395 | } |
Azure.IoT Build | 0:fa2de1b79154 | 396 | else |
Azure.IoT Build | 0:fa2de1b79154 | 397 | { |
Azure.IoT Build | 0:fa2de1b79154 | 398 | result = sz; |
Azure.IoT Build | 0:fa2de1b79154 | 399 | } |
Azure.IoT Build | 0:fa2de1b79154 | 400 | |
Azure.IoT Build | 0:fa2de1b79154 | 401 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 402 | } |
Azure.IoT Build | 0:fa2de1b79154 | 403 | |
Azure.IoT Build | 0:fa2de1b79154 | 404 | static int on_handshake_done(WOLFSSL* ssl, void* context) |
Azure.IoT Build | 0:fa2de1b79154 | 405 | { |
Azure.IoT Build | 0:fa2de1b79154 | 406 | TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context; |
AzureIoTClient | 15:956c6d205aa7 | 407 | (void)ssl; |
AzureIoTClient | 19:2e0811512ceb | 408 | if (tls_io_instance->tlsio_state != TLSIO_STATE_IN_HANDSHAKE) |
AzureIoTClient | 19:2e0811512ceb | 409 | { |
AzureIoTClient | 19:2e0811512ceb | 410 | LogInfo("on_handshake_done called when not in IN_HANDSHAKE state"); |
AzureIoTClient | 19:2e0811512ceb | 411 | } |
AzureIoTClient | 19:2e0811512ceb | 412 | else |
Azure.IoT Build | 0:fa2de1b79154 | 413 | { |
Azure.IoT Build | 0:fa2de1b79154 | 414 | tls_io_instance->tlsio_state = TLSIO_STATE_OPEN; |
Azure.IoT Build | 0:fa2de1b79154 | 415 | indicate_open_complete(tls_io_instance, IO_OPEN_OK); |
Azure.IoT Build | 0:fa2de1b79154 | 416 | } |
Azure.IoT Build | 0:fa2de1b79154 | 417 | |
Azure.IoT Build | 0:fa2de1b79154 | 418 | return 0; |
Azure.IoT Build | 0:fa2de1b79154 | 419 | } |
Azure.IoT Build | 0:fa2de1b79154 | 420 | |
Azure.IoT Build | 6:c55b013dfc2a | 421 | static int add_certificate_to_store(TLS_IO_INSTANCE* tls_io_instance) |
Azure.IoT Build | 6:c55b013dfc2a | 422 | { |
Azure.IoT Build | 6:c55b013dfc2a | 423 | int result; |
Azure.IoT Build | 6:c55b013dfc2a | 424 | if (tls_io_instance->certificate != NULL) |
Azure.IoT Build | 6:c55b013dfc2a | 425 | { |
AzureIoTClient | 15:956c6d205aa7 | 426 | int res = wolfSSL_CTX_load_verify_buffer(tls_io_instance->ssl_context, (const unsigned char*)tls_io_instance->certificate, strlen(tls_io_instance->certificate) + 1, SSL_FILETYPE_PEM); |
Azure.IoT Build | 6:c55b013dfc2a | 427 | if (res != SSL_SUCCESS) |
Azure.IoT Build | 6:c55b013dfc2a | 428 | { |
AzureIoTClient | 19:2e0811512ceb | 429 | LogError("wolfSSL_CTX_load_verify_buffer failed"); |
AzureIoTClient | 21:b92006c5b9ff | 430 | result = __FAILURE__; |
Azure.IoT Build | 6:c55b013dfc2a | 431 | } |
Azure.IoT Build | 6:c55b013dfc2a | 432 | else |
Azure.IoT Build | 6:c55b013dfc2a | 433 | { |
Azure.IoT Build | 6:c55b013dfc2a | 434 | result = 0; |
Azure.IoT Build | 6:c55b013dfc2a | 435 | } |
Azure.IoT Build | 6:c55b013dfc2a | 436 | } |
Azure.IoT Build | 6:c55b013dfc2a | 437 | else |
Azure.IoT Build | 6:c55b013dfc2a | 438 | { |
Azure.IoT Build | 6:c55b013dfc2a | 439 | result = 0; |
Azure.IoT Build | 6:c55b013dfc2a | 440 | } |
Azure.IoT Build | 6:c55b013dfc2a | 441 | return result; |
Azure.IoT Build | 6:c55b013dfc2a | 442 | } |
Azure.IoT Build | 6:c55b013dfc2a | 443 | |
AzureIoTClient | 15:956c6d205aa7 | 444 | static int x509_wolfssl_add_credentials(WOLFSSL* ssl, char* x509certificate, char* x509privatekey) { |
AzureIoTClient | 15:956c6d205aa7 | 445 | |
AzureIoTClient | 15:956c6d205aa7 | 446 | int result; |
AzureIoTClient | 15:956c6d205aa7 | 447 | |
AzureIoTClient | 15:956c6d205aa7 | 448 | if (wolfSSL_use_certificate_buffer(ssl, (unsigned char*)x509certificate, strlen(x509certificate) + 1, SSL_FILETYPE_PEM) != SSL_SUCCESS) |
AzureIoTClient | 15:956c6d205aa7 | 449 | { |
AzureIoTClient | 15:956c6d205aa7 | 450 | LogError("unable to load x509 client certificate"); |
AzureIoTClient | 21:b92006c5b9ff | 451 | result = __FAILURE__; |
AzureIoTClient | 15:956c6d205aa7 | 452 | } |
AzureIoTClient | 15:956c6d205aa7 | 453 | else if (wolfSSL_use_PrivateKey_buffer(ssl, (unsigned char*)x509privatekey, strlen(x509privatekey) + 1, SSL_FILETYPE_PEM) != SSL_SUCCESS) |
AzureIoTClient | 15:956c6d205aa7 | 454 | { |
AzureIoTClient | 15:956c6d205aa7 | 455 | LogError("unable to load x509 client private key"); |
AzureIoTClient | 21:b92006c5b9ff | 456 | result = __FAILURE__; |
AzureIoTClient | 15:956c6d205aa7 | 457 | } |
AzureIoTClient | 15:956c6d205aa7 | 458 | #ifdef HAVE_SECURE_RENEGOTIATION |
AzureIoTClient | 19:2e0811512ceb | 459 | else if (wolfSSL_UseSecureRenegotiation(ssl) != SSL_SUCCESS) |
AzureIoTClient | 19:2e0811512ceb | 460 | { |
AzureIoTClient | 15:956c6d205aa7 | 461 | LogError("unable to enable secure renegotiation"); |
AzureIoTClient | 21:b92006c5b9ff | 462 | result = __FAILURE__; |
AzureIoTClient | 15:956c6d205aa7 | 463 | } |
AzureIoTClient | 15:956c6d205aa7 | 464 | #endif |
AzureIoTClient | 15:956c6d205aa7 | 465 | else |
AzureIoTClient | 15:956c6d205aa7 | 466 | { |
AzureIoTClient | 15:956c6d205aa7 | 467 | result = 0; |
AzureIoTClient | 15:956c6d205aa7 | 468 | } |
AzureIoTClient | 15:956c6d205aa7 | 469 | return result; |
AzureIoTClient | 15:956c6d205aa7 | 470 | } |
AzureIoTClient | 15:956c6d205aa7 | 471 | |
AzureIoTClient | 15:956c6d205aa7 | 472 | static void destroy_wolfssl_instance(TLS_IO_INSTANCE* tls_io_instance) |
AzureIoTClient | 15:956c6d205aa7 | 473 | { |
AzureIoTClient | 15:956c6d205aa7 | 474 | wolfSSL_free(tls_io_instance->ssl); |
AzureIoTClient | 15:956c6d205aa7 | 475 | } |
AzureIoTClient | 15:956c6d205aa7 | 476 | |
Azure.IoT Build | 6:c55b013dfc2a | 477 | static int create_wolfssl_instance(TLS_IO_INSTANCE* tls_io_instance) |
Azure.IoT Build | 6:c55b013dfc2a | 478 | { |
Azure.IoT Build | 6:c55b013dfc2a | 479 | int result; |
Azure.IoT Build | 6:c55b013dfc2a | 480 | |
Azure.IoT Build | 6:c55b013dfc2a | 481 | if (add_certificate_to_store(tls_io_instance) != 0) |
Azure.IoT Build | 6:c55b013dfc2a | 482 | { |
AzureIoTClient | 19:2e0811512ceb | 483 | LogError("Failed to add certificates to store"); |
AzureIoTClient | 21:b92006c5b9ff | 484 | result = __FAILURE__; |
Azure.IoT Build | 6:c55b013dfc2a | 485 | } |
Azure.IoT Build | 6:c55b013dfc2a | 486 | else |
Azure.IoT Build | 6:c55b013dfc2a | 487 | { |
Azure.IoT Build | 6:c55b013dfc2a | 488 | tls_io_instance->ssl = wolfSSL_new(tls_io_instance->ssl_context); |
Azure.IoT Build | 6:c55b013dfc2a | 489 | if (tls_io_instance->ssl == NULL) |
Azure.IoT Build | 6:c55b013dfc2a | 490 | { |
AzureIoTClient | 19:2e0811512ceb | 491 | LogError("Failed to add certificates to store"); |
AzureIoTClient | 21:b92006c5b9ff | 492 | result = __FAILURE__; |
Azure.IoT Build | 6:c55b013dfc2a | 493 | } |
AzureIoTClient | 15:956c6d205aa7 | 494 | /*x509 authentication can only be build before underlying connection is realized*/ |
AzureIoTClient | 15:956c6d205aa7 | 495 | else if ((tls_io_instance->x509certificate != NULL) && |
AzureIoTClient | 15:956c6d205aa7 | 496 | (tls_io_instance->x509privatekey != NULL) && |
AzureIoTClient | 15:956c6d205aa7 | 497 | (x509_wolfssl_add_credentials(tls_io_instance->ssl, tls_io_instance->x509certificate, tls_io_instance->x509privatekey) != 0)) |
AzureIoTClient | 15:956c6d205aa7 | 498 | { |
AzureIoTClient | 15:956c6d205aa7 | 499 | destroy_wolfssl_instance(tls_io_instance); |
AzureIoTClient | 15:956c6d205aa7 | 500 | LogError("unable to use x509 authentication"); |
AzureIoTClient | 21:b92006c5b9ff | 501 | result = __FAILURE__; |
AzureIoTClient | 15:956c6d205aa7 | 502 | } |
Azure.IoT Build | 6:c55b013dfc2a | 503 | else |
Azure.IoT Build | 6:c55b013dfc2a | 504 | { |
Azure.IoT Build | 6:c55b013dfc2a | 505 | tls_io_instance->socket_io_read_bytes = NULL; |
Azure.IoT Build | 6:c55b013dfc2a | 506 | tls_io_instance->socket_io_read_byte_count = 0; |
Azure.IoT Build | 6:c55b013dfc2a | 507 | tls_io_instance->on_send_complete = NULL; |
Azure.IoT Build | 6:c55b013dfc2a | 508 | tls_io_instance->on_send_complete_callback_context = NULL; |
Azure.IoT Build | 6:c55b013dfc2a | 509 | |
Azure.IoT Build | 6:c55b013dfc2a | 510 | wolfSSL_set_using_nonblock(tls_io_instance->ssl, 1); |
Azure.IoT Build | 6:c55b013dfc2a | 511 | wolfSSL_SetIOSend(tls_io_instance->ssl_context, on_io_send); |
Azure.IoT Build | 6:c55b013dfc2a | 512 | wolfSSL_SetIORecv(tls_io_instance->ssl_context, on_io_recv); |
Azure.IoT Build | 6:c55b013dfc2a | 513 | wolfSSL_SetHsDoneCb(tls_io_instance->ssl, on_handshake_done, tls_io_instance); |
Azure.IoT Build | 6:c55b013dfc2a | 514 | wolfSSL_SetIOWriteCtx(tls_io_instance->ssl, tls_io_instance); |
Azure.IoT Build | 6:c55b013dfc2a | 515 | wolfSSL_SetIOReadCtx(tls_io_instance->ssl, tls_io_instance); |
Azure.IoT Build | 6:c55b013dfc2a | 516 | |
Azure.IoT Build | 6:c55b013dfc2a | 517 | tls_io_instance->tlsio_state = TLSIO_STATE_NOT_OPEN; |
Azure.IoT Build | 6:c55b013dfc2a | 518 | result = 0; |
Azure.IoT Build | 6:c55b013dfc2a | 519 | } |
Azure.IoT Build | 6:c55b013dfc2a | 520 | } |
AzureIoTClient | 19:2e0811512ceb | 521 | |
Azure.IoT Build | 6:c55b013dfc2a | 522 | return result; |
Azure.IoT Build | 6:c55b013dfc2a | 523 | } |
Azure.IoT Build | 6:c55b013dfc2a | 524 | |
Azure.IoT Build | 0:fa2de1b79154 | 525 | int tlsio_wolfssl_init(void) |
Azure.IoT Build | 0:fa2de1b79154 | 526 | { |
Azure.IoT Build | 0:fa2de1b79154 | 527 | (void)wolfSSL_library_init(); |
Azure.IoT Build | 0:fa2de1b79154 | 528 | wolfSSL_load_error_strings(); |
Azure.IoT Build | 0:fa2de1b79154 | 529 | |
Azure.IoT Build | 0:fa2de1b79154 | 530 | return 0; |
Azure.IoT Build | 0:fa2de1b79154 | 531 | } |
Azure.IoT Build | 0:fa2de1b79154 | 532 | |
Azure.IoT Build | 0:fa2de1b79154 | 533 | void tlsio_wolfssl_deinit(void) |
Azure.IoT Build | 0:fa2de1b79154 | 534 | { |
Azure.IoT Build | 0:fa2de1b79154 | 535 | } |
Azure.IoT Build | 0:fa2de1b79154 | 536 | |
Azure.IoT Build | 6:c55b013dfc2a | 537 | CONCRETE_IO_HANDLE tlsio_wolfssl_create(void* io_create_parameters) |
Azure.IoT Build | 0:fa2de1b79154 | 538 | { |
Azure.IoT Build | 0:fa2de1b79154 | 539 | TLS_IO_INSTANCE* result; |
Azure.IoT Build | 0:fa2de1b79154 | 540 | |
AzureIoTClient | 19:2e0811512ceb | 541 | if (io_create_parameters == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 542 | { |
AzureIoTClient | 19:2e0811512ceb | 543 | LogError("NULL io_create_parameters"); |
Azure.IoT Build | 0:fa2de1b79154 | 544 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 545 | } |
Azure.IoT Build | 0:fa2de1b79154 | 546 | else |
Azure.IoT Build | 0:fa2de1b79154 | 547 | { |
AzureIoTClient | 19:2e0811512ceb | 548 | TLSIO_CONFIG* tls_io_config = io_create_parameters; |
AzureIoTClient | 19:2e0811512ceb | 549 | |
Azure.IoT Build | 6:c55b013dfc2a | 550 | result = (TLS_IO_INSTANCE*)malloc(sizeof(TLS_IO_INSTANCE)); |
Azure.IoT Build | 0:fa2de1b79154 | 551 | if (result != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 552 | { |
Azure.IoT Build | 6:c55b013dfc2a | 553 | memset(result, 0, sizeof(TLS_IO_INSTANCE)); |
AzureIoTClient | 19:2e0811512ceb | 554 | if (mallocAndStrcpy_s(&result->hostname, tls_io_config->hostname) != 0) |
Azure.IoT Build | 0:fa2de1b79154 | 555 | { |
AzureIoTClient | 19:2e0811512ceb | 556 | LogError("Cannot copy the hostname"); |
Azure.IoT Build | 0:fa2de1b79154 | 557 | free(result); |
Azure.IoT Build | 0:fa2de1b79154 | 558 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 559 | } |
Azure.IoT Build | 0:fa2de1b79154 | 560 | else |
Azure.IoT Build | 0:fa2de1b79154 | 561 | { |
AzureIoTClient | 19:2e0811512ceb | 562 | result->port = tls_io_config->port; |
AzureIoTClient | 19:2e0811512ceb | 563 | |
AzureIoTClient | 19:2e0811512ceb | 564 | result->socket_io_read_bytes = 0; |
AzureIoTClient | 19:2e0811512ceb | 565 | result->socket_io_read_byte_count = 0; |
AzureIoTClient | 19:2e0811512ceb | 566 | result->socket_io = NULL; |
AzureIoTClient | 19:2e0811512ceb | 567 | |
AzureIoTClient | 19:2e0811512ceb | 568 | result->ssl = NULL; |
AzureIoTClient | 19:2e0811512ceb | 569 | result->certificate = NULL; |
AzureIoTClient | 19:2e0811512ceb | 570 | result->x509certificate = NULL; |
AzureIoTClient | 19:2e0811512ceb | 571 | result->x509privatekey = NULL; |
AzureIoTClient | 19:2e0811512ceb | 572 | |
AzureIoTClient | 19:2e0811512ceb | 573 | result->on_bytes_received = NULL; |
AzureIoTClient | 19:2e0811512ceb | 574 | result->on_bytes_received_context = NULL; |
AzureIoTClient | 19:2e0811512ceb | 575 | |
AzureIoTClient | 19:2e0811512ceb | 576 | result->on_io_open_complete = NULL; |
AzureIoTClient | 19:2e0811512ceb | 577 | result->on_io_open_complete_context = NULL; |
AzureIoTClient | 19:2e0811512ceb | 578 | |
AzureIoTClient | 19:2e0811512ceb | 579 | result->on_io_close_complete = NULL; |
AzureIoTClient | 19:2e0811512ceb | 580 | result->on_io_close_complete_context = NULL; |
AzureIoTClient | 19:2e0811512ceb | 581 | |
AzureIoTClient | 19:2e0811512ceb | 582 | result->on_io_error = NULL; |
AzureIoTClient | 19:2e0811512ceb | 583 | result->on_io_error_context = NULL; |
AzureIoTClient | 19:2e0811512ceb | 584 | |
AzureIoTClient | 19:2e0811512ceb | 585 | result->tlsio_state = TLSIO_STATE_NOT_OPEN; |
AzureIoTClient | 19:2e0811512ceb | 586 | |
AzureIoTClient | 19:2e0811512ceb | 587 | result->ssl_context = wolfSSL_CTX_new(wolfTLSv1_client_method()); |
AzureIoTClient | 19:2e0811512ceb | 588 | if (result->ssl_context == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 589 | { |
AzureIoTClient | 19:2e0811512ceb | 590 | LogError("Cannot create the wolfSSL context"); |
AzureIoTClient | 19:2e0811512ceb | 591 | free(result->hostname); |
Azure.IoT Build | 0:fa2de1b79154 | 592 | free(result); |
Azure.IoT Build | 0:fa2de1b79154 | 593 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 594 | } |
Azure.IoT Build | 0:fa2de1b79154 | 595 | else |
Azure.IoT Build | 0:fa2de1b79154 | 596 | { |
AzureIoTClient | 19:2e0811512ceb | 597 | const IO_INTERFACE_DESCRIPTION* socket_io_interface = socketio_get_interface_description(); |
AzureIoTClient | 19:2e0811512ceb | 598 | if (socket_io_interface == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 599 | { |
Azure.IoT Build | 0:fa2de1b79154 | 600 | wolfSSL_CTX_free(result->ssl_context); |
AzureIoTClient | 19:2e0811512ceb | 601 | free(result->hostname); |
Azure.IoT Build | 0:fa2de1b79154 | 602 | free(result); |
Azure.IoT Build | 0:fa2de1b79154 | 603 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 604 | } |
AzureIoTClient | 19:2e0811512ceb | 605 | else |
AzureIoTClient | 19:2e0811512ceb | 606 | { |
AzureIoTClient | 19:2e0811512ceb | 607 | SOCKETIO_CONFIG socketio_config; |
AzureIoTClient | 19:2e0811512ceb | 608 | socketio_config.hostname = result->hostname; |
AzureIoTClient | 19:2e0811512ceb | 609 | socketio_config.port = result->port; |
AzureIoTClient | 19:2e0811512ceb | 610 | socketio_config.accepted_socket = NULL; |
AzureIoTClient | 19:2e0811512ceb | 611 | |
AzureIoTClient | 19:2e0811512ceb | 612 | result->socket_io = xio_create(socket_io_interface, &socketio_config); |
AzureIoTClient | 19:2e0811512ceb | 613 | if (result->socket_io == NULL) |
AzureIoTClient | 19:2e0811512ceb | 614 | { |
AzureIoTClient | 19:2e0811512ceb | 615 | LogError("Failure connecting to underlying socket_io"); |
AzureIoTClient | 19:2e0811512ceb | 616 | wolfSSL_CTX_free(result->ssl_context); |
AzureIoTClient | 19:2e0811512ceb | 617 | free(result->hostname); |
AzureIoTClient | 19:2e0811512ceb | 618 | free(result); |
AzureIoTClient | 19:2e0811512ceb | 619 | result = NULL; |
AzureIoTClient | 19:2e0811512ceb | 620 | } |
AzureIoTClient | 19:2e0811512ceb | 621 | } |
Azure.IoT Build | 0:fa2de1b79154 | 622 | } |
Azure.IoT Build | 0:fa2de1b79154 | 623 | } |
Azure.IoT Build | 0:fa2de1b79154 | 624 | } |
Azure.IoT Build | 0:fa2de1b79154 | 625 | } |
Azure.IoT Build | 0:fa2de1b79154 | 626 | |
Azure.IoT Build | 0:fa2de1b79154 | 627 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 628 | } |
Azure.IoT Build | 0:fa2de1b79154 | 629 | |
Azure.IoT Build | 0:fa2de1b79154 | 630 | void tlsio_wolfssl_destroy(CONCRETE_IO_HANDLE tls_io) |
Azure.IoT Build | 0:fa2de1b79154 | 631 | { |
Azure.IoT Build | 0:fa2de1b79154 | 632 | if (tls_io != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 633 | { |
Azure.IoT Build | 0:fa2de1b79154 | 634 | TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; |
Azure.IoT Build | 0:fa2de1b79154 | 635 | if (tls_io_instance->socket_io_read_bytes != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 636 | { |
Azure.IoT Build | 0:fa2de1b79154 | 637 | free(tls_io_instance->socket_io_read_bytes); |
Azure.IoT Build | 0:fa2de1b79154 | 638 | } |
Azure.IoT Build | 0:fa2de1b79154 | 639 | |
Azure.IoT Build | 6:c55b013dfc2a | 640 | if (tls_io_instance->certificate != NULL) |
Azure.IoT Build | 6:c55b013dfc2a | 641 | { |
Azure.IoT Build | 6:c55b013dfc2a | 642 | free(tls_io_instance->certificate); |
Azure.IoT Build | 6:c55b013dfc2a | 643 | } |
AzureIoTClient | 15:956c6d205aa7 | 644 | if (tls_io_instance->x509certificate != NULL) |
AzureIoTClient | 15:956c6d205aa7 | 645 | { |
AzureIoTClient | 15:956c6d205aa7 | 646 | free(tls_io_instance->x509certificate); |
AzureIoTClient | 15:956c6d205aa7 | 647 | } |
AzureIoTClient | 15:956c6d205aa7 | 648 | if (tls_io_instance->x509privatekey != NULL) |
AzureIoTClient | 15:956c6d205aa7 | 649 | { |
AzureIoTClient | 15:956c6d205aa7 | 650 | free(tls_io_instance->x509privatekey); |
AzureIoTClient | 15:956c6d205aa7 | 651 | } |
AzureIoTClient | 19:2e0811512ceb | 652 | if (tls_io_instance->hostname != NULL) |
AzureIoTClient | 19:2e0811512ceb | 653 | { |
AzureIoTClient | 19:2e0811512ceb | 654 | free(tls_io_instance->hostname); |
AzureIoTClient | 19:2e0811512ceb | 655 | } |
AzureIoTClient | 19:2e0811512ceb | 656 | |
Azure.IoT Build | 6:c55b013dfc2a | 657 | wolfSSL_CTX_free(tls_io_instance->ssl_context); |
Azure.IoT Build | 0:fa2de1b79154 | 658 | xio_destroy(tls_io_instance->socket_io); |
Azure.IoT Build | 0:fa2de1b79154 | 659 | free(tls_io); |
Azure.IoT Build | 0:fa2de1b79154 | 660 | } |
Azure.IoT Build | 0:fa2de1b79154 | 661 | } |
Azure.IoT Build | 0:fa2de1b79154 | 662 | |
Azure.IoT Build | 0:fa2de1b79154 | 663 | 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 | 664 | { |
Azure.IoT Build | 0:fa2de1b79154 | 665 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 666 | |
Azure.IoT Build | 0:fa2de1b79154 | 667 | if (tls_io == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 668 | { |
AzureIoTClient | 19:2e0811512ceb | 669 | LogError("NULL tls_io instance"); |
AzureIoTClient | 21:b92006c5b9ff | 670 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 671 | } |
Azure.IoT Build | 0:fa2de1b79154 | 672 | else |
Azure.IoT Build | 0:fa2de1b79154 | 673 | { |
Azure.IoT Build | 0:fa2de1b79154 | 674 | TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; |
Azure.IoT Build | 0:fa2de1b79154 | 675 | |
Azure.IoT Build | 0:fa2de1b79154 | 676 | if (tls_io_instance->tlsio_state != TLSIO_STATE_NOT_OPEN) |
Azure.IoT Build | 0:fa2de1b79154 | 677 | { |
Azure.IoT Build | 6:c55b013dfc2a | 678 | LogError("Invalid state encountered."); |
AzureIoTClient | 21:b92006c5b9ff | 679 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 680 | } |
Azure.IoT Build | 0:fa2de1b79154 | 681 | else |
Azure.IoT Build | 0:fa2de1b79154 | 682 | { |
Azure.IoT Build | 0:fa2de1b79154 | 683 | tls_io_instance->on_bytes_received = on_bytes_received; |
Azure.IoT Build | 0:fa2de1b79154 | 684 | tls_io_instance->on_bytes_received_context = on_bytes_received_context; |
Azure.IoT Build | 0:fa2de1b79154 | 685 | |
Azure.IoT Build | 0:fa2de1b79154 | 686 | tls_io_instance->on_io_open_complete = on_io_open_complete; |
Azure.IoT Build | 0:fa2de1b79154 | 687 | tls_io_instance->on_io_open_complete_context = on_io_open_complete_context; |
Azure.IoT Build | 0:fa2de1b79154 | 688 | |
Azure.IoT Build | 0:fa2de1b79154 | 689 | tls_io_instance->on_io_error = on_io_error; |
Azure.IoT Build | 0:fa2de1b79154 | 690 | tls_io_instance->on_io_error_context = on_io_error_context; |
Azure.IoT Build | 0:fa2de1b79154 | 691 | |
Azure.IoT Build | 0:fa2de1b79154 | 692 | tls_io_instance->tlsio_state = TLSIO_STATE_OPENING_UNDERLYING_IO; |
Azure.IoT Build | 0:fa2de1b79154 | 693 | |
Azure.IoT Build | 6:c55b013dfc2a | 694 | if (create_wolfssl_instance(tls_io_instance) != 0) |
Azure.IoT Build | 6:c55b013dfc2a | 695 | { |
AzureIoTClient | 19:2e0811512ceb | 696 | LogError("Cannot create wolfssl instance."); |
Azure.IoT Build | 6:c55b013dfc2a | 697 | tls_io_instance->tlsio_state = TLSIO_STATE_NOT_OPEN; |
AzureIoTClient | 21:b92006c5b9ff | 698 | result = __FAILURE__; |
Azure.IoT Build | 6:c55b013dfc2a | 699 | } |
Azure.IoT Build | 6:c55b013dfc2a | 700 | 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 | 701 | { |
AzureIoTClient | 19:2e0811512ceb | 702 | LogError("Cannot open the underlying IO."); |
Azure.IoT Build | 0:fa2de1b79154 | 703 | tls_io_instance->tlsio_state = TLSIO_STATE_NOT_OPEN; |
AzureIoTClient | 21:b92006c5b9ff | 704 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 705 | } |
Azure.IoT Build | 0:fa2de1b79154 | 706 | else |
Azure.IoT Build | 0:fa2de1b79154 | 707 | { |
Azure.IoT Build | 6:c55b013dfc2a | 708 | // The state can get changed in the on_underlying_io_open_complete |
AzureIoTClient | 15:956c6d205aa7 | 709 | if (tls_io_instance->tlsio_state != TLSIO_STATE_OPEN) |
Azure.IoT Build | 0:fa2de1b79154 | 710 | { |
Azure.IoT Build | 6:c55b013dfc2a | 711 | LogError("Failed to connect to server. The certificates may not be correct."); |
AzureIoTClient | 21:b92006c5b9ff | 712 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 713 | } |
Azure.IoT Build | 0:fa2de1b79154 | 714 | else |
Azure.IoT Build | 0:fa2de1b79154 | 715 | { |
AzureIoTClient | 15:956c6d205aa7 | 716 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 717 | } |
Azure.IoT Build | 0:fa2de1b79154 | 718 | } |
Azure.IoT Build | 0:fa2de1b79154 | 719 | } |
Azure.IoT Build | 0:fa2de1b79154 | 720 | } |
Azure.IoT Build | 0:fa2de1b79154 | 721 | |
Azure.IoT Build | 0:fa2de1b79154 | 722 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 723 | } |
Azure.IoT Build | 0:fa2de1b79154 | 724 | |
Azure.IoT Build | 0:fa2de1b79154 | 725 | 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 | 726 | { |
Azure.IoT Build | 0:fa2de1b79154 | 727 | int result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 728 | |
Azure.IoT Build | 0:fa2de1b79154 | 729 | if (tls_io == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 730 | { |
AzureIoTClient | 19:2e0811512ceb | 731 | LogError("NULL tls_io handle."); |
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 | { |
Azure.IoT Build | 0:fa2de1b79154 | 736 | TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; |
Azure.IoT Build | 0:fa2de1b79154 | 737 | |
Azure.IoT Build | 0:fa2de1b79154 | 738 | if ((tls_io_instance->tlsio_state == TLSIO_STATE_NOT_OPEN) || |
Azure.IoT Build | 0:fa2de1b79154 | 739 | (tls_io_instance->tlsio_state == TLSIO_STATE_CLOSING)) |
Azure.IoT Build | 0:fa2de1b79154 | 740 | { |
AzureIoTClient | 19:2e0811512ceb | 741 | LogError("Close called while not open."); |
AzureIoTClient | 21:b92006c5b9ff | 742 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 743 | } |
Azure.IoT Build | 0:fa2de1b79154 | 744 | else |
Azure.IoT Build | 0:fa2de1b79154 | 745 | { |
Azure.IoT Build | 0:fa2de1b79154 | 746 | tls_io_instance->tlsio_state = TLSIO_STATE_CLOSING; |
Azure.IoT Build | 0:fa2de1b79154 | 747 | tls_io_instance->on_io_close_complete = on_io_close_complete; |
Azure.IoT Build | 0:fa2de1b79154 | 748 | tls_io_instance->on_io_close_complete_context = callback_context; |
Azure.IoT Build | 0:fa2de1b79154 | 749 | |
Azure.IoT Build | 0:fa2de1b79154 | 750 | if (xio_close(tls_io_instance->socket_io, on_underlying_io_close_complete, tls_io_instance) != 0) |
Azure.IoT Build | 0:fa2de1b79154 | 751 | { |
AzureIoTClient | 19:2e0811512ceb | 752 | LogError("xio_close failed."); |
AzureIoTClient | 21:b92006c5b9ff | 753 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 754 | } |
Azure.IoT Build | 0:fa2de1b79154 | 755 | else |
Azure.IoT Build | 0:fa2de1b79154 | 756 | { |
Azure.IoT Build | 6:c55b013dfc2a | 757 | destroy_wolfssl_instance(tls_io_instance); |
Azure.IoT Build | 0:fa2de1b79154 | 758 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 759 | } |
Azure.IoT Build | 0:fa2de1b79154 | 760 | } |
Azure.IoT Build | 0:fa2de1b79154 | 761 | } |
Azure.IoT Build | 0:fa2de1b79154 | 762 | |
Azure.IoT Build | 0:fa2de1b79154 | 763 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 764 | } |
Azure.IoT Build | 0:fa2de1b79154 | 765 | |
Azure.IoT Build | 0:fa2de1b79154 | 766 | 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 | 767 | { |
Azure.IoT Build | 0:fa2de1b79154 | 768 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 769 | |
Azure.IoT Build | 0:fa2de1b79154 | 770 | if (tls_io == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 771 | { |
AzureIoTClient | 19:2e0811512ceb | 772 | LogError("NULL tls_io handle"); |
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 | TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; |
Azure.IoT Build | 0:fa2de1b79154 | 778 | |
Azure.IoT Build | 0:fa2de1b79154 | 779 | if (tls_io_instance->tlsio_state != TLSIO_STATE_OPEN) |
Azure.IoT Build | 0:fa2de1b79154 | 780 | { |
AzureIoTClient | 19:2e0811512ceb | 781 | LogError("send called while not open"); |
AzureIoTClient | 21:b92006c5b9ff | 782 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 783 | } |
Azure.IoT Build | 0:fa2de1b79154 | 784 | else |
Azure.IoT Build | 0:fa2de1b79154 | 785 | { |
Azure.IoT Build | 0:fa2de1b79154 | 786 | tls_io_instance->on_send_complete = on_send_complete; |
Azure.IoT Build | 0:fa2de1b79154 | 787 | tls_io_instance->on_send_complete_callback_context = callback_context; |
Azure.IoT Build | 0:fa2de1b79154 | 788 | |
Azure.IoT Build | 0:fa2de1b79154 | 789 | int res = wolfSSL_write(tls_io_instance->ssl, buffer, size); |
AzureIoTClient | 15:956c6d205aa7 | 790 | 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 | 791 | { |
AzureIoTClient | 19:2e0811512ceb | 792 | LogError("Error writing data through WolfSSL"); |
AzureIoTClient | 21:b92006c5b9ff | 793 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 794 | } |
Azure.IoT Build | 0:fa2de1b79154 | 795 | else |
Azure.IoT Build | 0:fa2de1b79154 | 796 | { |
Azure.IoT Build | 0:fa2de1b79154 | 797 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 798 | } |
Azure.IoT Build | 0:fa2de1b79154 | 799 | } |
Azure.IoT Build | 0:fa2de1b79154 | 800 | } |
Azure.IoT Build | 0:fa2de1b79154 | 801 | |
Azure.IoT Build | 0:fa2de1b79154 | 802 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 803 | } |
Azure.IoT Build | 0:fa2de1b79154 | 804 | |
Azure.IoT Build | 0:fa2de1b79154 | 805 | void tlsio_wolfssl_dowork(CONCRETE_IO_HANDLE tls_io) |
Azure.IoT Build | 0:fa2de1b79154 | 806 | { |
AzureIoTClient | 19:2e0811512ceb | 807 | if (tls_io == NULL) |
AzureIoTClient | 19:2e0811512ceb | 808 | { |
AzureIoTClient | 19:2e0811512ceb | 809 | LogError("NULL tls_io"); |
AzureIoTClient | 19:2e0811512ceb | 810 | } |
AzureIoTClient | 19:2e0811512ceb | 811 | else |
Azure.IoT Build | 0:fa2de1b79154 | 812 | { |
Azure.IoT Build | 0:fa2de1b79154 | 813 | TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; |
Azure.IoT Build | 0:fa2de1b79154 | 814 | |
Azure.IoT Build | 0:fa2de1b79154 | 815 | if ((tls_io_instance->tlsio_state != TLSIO_STATE_NOT_OPEN) && |
Azure.IoT Build | 0:fa2de1b79154 | 816 | (tls_io_instance->tlsio_state != TLSIO_STATE_ERROR)) |
Azure.IoT Build | 0:fa2de1b79154 | 817 | { |
Azure.IoT Build | 0:fa2de1b79154 | 818 | decode_ssl_received_bytes(tls_io_instance); |
Azure.IoT Build | 0:fa2de1b79154 | 819 | xio_dowork(tls_io_instance->socket_io); |
Azure.IoT Build | 0:fa2de1b79154 | 820 | } |
Azure.IoT Build | 0:fa2de1b79154 | 821 | } |
Azure.IoT Build | 0:fa2de1b79154 | 822 | } |
Azure.IoT Build | 0:fa2de1b79154 | 823 | |
Azure.IoT Build | 0:fa2de1b79154 | 824 | const IO_INTERFACE_DESCRIPTION* tlsio_wolfssl_get_interface_description(void) |
Azure.IoT Build | 0:fa2de1b79154 | 825 | { |
Azure.IoT Build | 0:fa2de1b79154 | 826 | return &tlsio_wolfssl_interface_description; |
Azure.IoT Build | 0:fa2de1b79154 | 827 | } |
Azure.IoT Build | 0:fa2de1b79154 | 828 | |
AzureIoTClient | 15:956c6d205aa7 | 829 | static int process_option(char** destination, const char* name, const char* value) |
AzureIoTClient | 15:956c6d205aa7 | 830 | { |
AzureIoTClient | 15:956c6d205aa7 | 831 | int result; |
AzureIoTClient | 15:956c6d205aa7 | 832 | if (*destination != NULL) |
AzureIoTClient | 15:956c6d205aa7 | 833 | { |
AzureIoTClient | 15:956c6d205aa7 | 834 | free(*destination); |
AzureIoTClient | 15:956c6d205aa7 | 835 | *destination = NULL; |
AzureIoTClient | 15:956c6d205aa7 | 836 | } |
AzureIoTClient | 15:956c6d205aa7 | 837 | if (mallocAndStrcpy_s(destination, value) != 0) |
AzureIoTClient | 15:956c6d205aa7 | 838 | { |
AzureIoTClient | 15:956c6d205aa7 | 839 | LogError("unable to process option %s",name); |
AzureIoTClient | 21:b92006c5b9ff | 840 | result = __FAILURE__; |
AzureIoTClient | 15:956c6d205aa7 | 841 | } |
AzureIoTClient | 15:956c6d205aa7 | 842 | else |
AzureIoTClient | 15:956c6d205aa7 | 843 | { |
AzureIoTClient | 15:956c6d205aa7 | 844 | result = 0; |
AzureIoTClient | 15:956c6d205aa7 | 845 | } |
AzureIoTClient | 15:956c6d205aa7 | 846 | return result; |
AzureIoTClient | 19:2e0811512ceb | 847 | } |
AzureIoTClient | 15:956c6d205aa7 | 848 | |
Azure.IoT Build | 0:fa2de1b79154 | 849 | int tlsio_wolfssl_setoption(CONCRETE_IO_HANDLE tls_io, const char* optionName, const void* value) |
Azure.IoT Build | 0:fa2de1b79154 | 850 | { |
Azure.IoT Build | 0:fa2de1b79154 | 851 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 852 | |
Azure.IoT Build | 0:fa2de1b79154 | 853 | if (tls_io == NULL || optionName == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 854 | { |
AzureIoTClient | 19:2e0811512ceb | 855 | LogError("Bad arguments, tls_io = %p, optionName = %p", tls_io, optionName); |
AzureIoTClient | 21:b92006c5b9ff | 856 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 857 | } |
Azure.IoT Build | 0:fa2de1b79154 | 858 | else |
Azure.IoT Build | 0:fa2de1b79154 | 859 | { |
Azure.IoT Build | 0:fa2de1b79154 | 860 | TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; |
Azure.IoT Build | 0:fa2de1b79154 | 861 | |
Azure.IoT Build | 0:fa2de1b79154 | 862 | if (strcmp("TrustedCerts", optionName) == 0) |
Azure.IoT Build | 0:fa2de1b79154 | 863 | { |
AzureIoTClient | 15:956c6d205aa7 | 864 | result = process_option(&tls_io_instance->certificate, optionName, value); |
AzureIoTClient | 15:956c6d205aa7 | 865 | } |
AzureIoTClient | 15:956c6d205aa7 | 866 | else if (strcmp(SU_OPTION_X509_CERT, optionName) == 0) |
AzureIoTClient | 15:956c6d205aa7 | 867 | { |
AzureIoTClient | 15:956c6d205aa7 | 868 | result = process_option(&tls_io_instance->x509certificate, optionName, value); |
AzureIoTClient | 15:956c6d205aa7 | 869 | } |
AzureIoTClient | 15:956c6d205aa7 | 870 | else if (strcmp(SU_OPTION_X509_PRIVATE_KEY, optionName) == 0) |
AzureIoTClient | 15:956c6d205aa7 | 871 | { |
AzureIoTClient | 15:956c6d205aa7 | 872 | result = process_option(&tls_io_instance->x509privatekey, optionName, value); |
AzureIoTClient | 15:956c6d205aa7 | 873 | } |
AzureIoTClient | 15:956c6d205aa7 | 874 | else |
AzureIoTClient | 15:956c6d205aa7 | 875 | { |
AzureIoTClient | 15:956c6d205aa7 | 876 | if (tls_io_instance->socket_io == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 877 | { |
AzureIoTClient | 19:2e0811512ceb | 878 | LogError("NULL underlying IO handle"); |
AzureIoTClient | 21:b92006c5b9ff | 879 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 880 | } |
Azure.IoT Build | 0:fa2de1b79154 | 881 | else |
Azure.IoT Build | 0:fa2de1b79154 | 882 | { |
AzureIoTClient | 15:956c6d205aa7 | 883 | result = xio_setoption(tls_io_instance->socket_io, optionName, value); |
Azure.IoT Build | 0:fa2de1b79154 | 884 | } |
Azure.IoT Build | 0:fa2de1b79154 | 885 | } |
Azure.IoT Build | 0:fa2de1b79154 | 886 | } |
Azure.IoT Build | 0:fa2de1b79154 | 887 | |
Azure.IoT Build | 0:fa2de1b79154 | 888 | return result; |
AzureIoTClient | 10:1be0bc9a9deb | 889 | } |