| 1 |
// Copyright (c) Microsoft. All rights reserved. |
| 2 |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 |
|
| 4 |
#error CycloneSSL is NOT supported for the Azure C shared utilities. |
| 5 |
#error The code here is provided for reference purposes. |
| 6 |
#error |
| 7 |
#error A security audit is required if you attempt to bring this code back. |
| 8 |
|
| 9 |
#include <stdlib.h> |
| 10 |
|
| 11 |
/* Using tlsio_cyclonessl requires: |
| 12 |
- Downloading the source code for CycloneSSL. |
| 13 |
- If using cmake, defining the environment variable CycloneSSLDir |
| 14 |
to point to the location of the unpacked CycloneSSL source |
| 15 |
- Using the use_cyclonessl cmake option |
| 16 |
|
| 17 |
Alternately if cmake is not used, one has to make sure that the |
| 18 |
CycloneSSL headers are in the include directlroy list. */ |
| 19 |
|
| 20 |
#if _WIN32 |
| 21 |
#define _WINERROR_ |
| 22 |
#include <winsock2.h> |
| 23 |
#endif |
| 24 |
|
| 25 |
#include <stdio.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <stdbool.h> |
| 28 |
#include <stdint.h> |
| 29 |
#include "azure_c_shared_utility/gballoc.h" |
| 30 |
#include "azure_c_shared_utility/tlsio.h" |
| 31 |
#include "azure_c_shared_utility/tlsio_cyclonessl.h" |
| 32 |
#include "azure_c_shared_utility/xlogging.h" |
| 33 |
#include "azure_c_shared_utility/crt_abstractions.h" |
| 34 |
#include "error.h" |
| 35 |
#include "yarrow.h" |
| 36 |
#include "tls.h" |
| 37 |
#include "azure_c_shared_utility/tlsio_cyclonessl_socket.h" |
| 38 |
|
| 39 |
typedef enum TLSIO_STATE_TAG |
| 40 |
{ |
| 41 |
TLSIO_STATE_NOT_OPEN, |
| 42 |
TLSIO_STATE_OPEN, |
| 43 |
TLSIO_STATE_ERROR |
| 44 |
} TLSIO_STATE; |
| 45 |
|
| 46 |
typedef struct TLS_IO_INSTANCE_TAG |
| 47 |
{ |
| 48 |
ON_BYTES_RECEIVED on_bytes_received; |
| 49 |
ON_IO_ERROR on_io_error; |
| 50 |
void* on_bytes_received_context; |
| 51 |
void* on_io_error_context; |
| 52 |
TLSIO_STATE tlsio_state; |
| 53 |
char* hostname; |
| 54 |
int port; |
| 55 |
char* certificate; |
| 56 |
YarrowContext yarrowContext; |
| 57 |
TlsContext *tlsContext; |
| 58 |
TlsSocket socket; |
| 59 |
} TLS_IO_INSTANCE; |
| 60 |
|
| 61 |
static int tlsio_cyclonessl_close(CONCRETE_IO_HANDLE tls_io, ON_IO_CLOSE_COMPLETE on_io_close_complete, void* on_io_close_complete_context); |
| 62 |
|
| 63 |
/*this function will clone an option given by name and value*/ |
| 64 |
static void* tlsio_cyclonessl_clone_option(const char* name, const void* value) |
| 65 |
{ |
| 66 |
void* result; |
| 67 |
|
| 68 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_070: [ If the name or value arguments are NULL, tlsio_cyclonessl_clone_option shall return NULL. ]*/ |
| 69 |
if((name == NULL) || (value == NULL)) |
| 70 |
{ |
| 71 |
LogError("invalid parameter detected: const char* name=%p, const void* value=%p", name, value); |
| 72 |
result = NULL; |
| 73 |
} |
| 74 |
else |
| 75 |
{ |
| 76 |
if (strcmp(name, "TrustedCerts") == 0) |
| 77 |
{ |
| 78 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_071: [ tlsio_cyclonessl_clone_option shall clone the option named TrustedCerts by calling mallocAndStrcpy_s. ]*/ |
| 79 |
if(mallocAndStrcpy_s((char**)&result, value) != 0) |
| 80 |
{ |
| 81 |
LogError("unable to mallocAndStrcpy_s TrustedCerts value"); |
| 82 |
result = NULL; |
| 83 |
} |
| 84 |
else |
| 85 |
{ |
| 86 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_072: [ On success it shall return a non-NULL pointer to the cloned option. ]*/ |
| 87 |
/*return as is*/ |
| 88 |
} |
| 89 |
} |
| 90 |
else |
| 91 |
{ |
| 92 |
LogError("not handled option : %s", name); |
| 93 |
result = NULL; |
| 94 |
} |
| 95 |
} |
| 96 |
return result; |
| 97 |
} |
| 98 |
|
| 99 |
/*this function destroys an option previously created*/ |
| 100 |
static void tlsio_cyclonessl_destroy_option(const char* name, const void* value) |
| 101 |
{ |
| 102 |
/*since all options for this layer are actually string copies., disposing of one is just calling free*/ |
| 103 |
|
| 104 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_074: [ If any of the arguments is NULL, tlsio_cyclonessl_destroy_option shall do nothing. ]*/ |
| 105 |
if ((name == NULL) || (value == NULL)) |
| 106 |
{ |
| 107 |
LogError("invalid parameter detected: const char* name=%p, const void* value=%p", name, value); |
| 108 |
} |
| 109 |
else |
| 110 |
{ |
| 111 |
if (strcmp(name, "TrustedCerts") == 0) |
| 112 |
{ |
| 113 |
free((void*)value); |
| 114 |
} |
| 115 |
else |
| 116 |
{ |
| 117 |
LogError("not handled option : %s", name); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_001: [ tlsio_cyclonessl_create shall create a new instance of the tlsio for Cyclone SSL. ]*/ |
| 123 |
static CONCRETE_IO_HANDLE tlsio_cyclonessl_create(void* io_create_parameters) |
| 124 |
{ |
| 125 |
TLS_IO_INSTANCE* result; |
| 126 |
|
| 127 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_002: [ If io_create_parameters is NULL, tlsio_cyclonessl_create shall fail and return NULL. ]*/ |
| 128 |
if (io_create_parameters == NULL) |
| 129 |
{ |
| 130 |
result = NULL; |
| 131 |
LogError("NULL tls_io_config."); |
| 132 |
} |
| 133 |
else |
| 134 |
{ |
| 135 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_003: [ io_create_parameters shall be used as a TLSIO_CONFIG\*. ]*/ |
| 136 |
TLSIO_CONFIG* tls_io_config = io_create_parameters; |
| 137 |
|
| 138 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_004: [ If the hostname member is NULL, then tlsio_cyclonessl_create shall fail and return NULL. ]*/ |
| 139 |
if (tls_io_config->hostname == NULL) |
| 140 |
{ |
| 141 |
result = NULL; |
| 142 |
LogError("NULL hostname in the TLS IO configuration."); |
| 143 |
} |
| 144 |
else |
| 145 |
{ |
| 146 |
result = malloc(sizeof(TLS_IO_INSTANCE)); |
| 147 |
if (result == NULL) |
| 148 |
{ |
| 149 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_076: [ If allocating memory for the TLS IO instance fails then tlsio_cyclonessl_create shall fail and return NULL. ]*/ |
| 150 |
LogError("Failed allocating TLSIO instance."); |
| 151 |
} |
| 152 |
else |
| 153 |
{ |
| 154 |
uint8_t seed[32]; |
| 155 |
size_t i; |
| 156 |
|
| 157 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_005: [ tlsio_cyclonessl_create shall copy the hostname and port values for later use when the open of the underlying socket is needed. ]*/ |
| 158 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_006: [ hostname shall be copied by calling mallocAndStrcpy_s. ]*/ |
| 159 |
if (mallocAndStrcpy_s(&result->hostname, tls_io_config->hostname) != 0) |
| 160 |
{ |
| 161 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_007: [ If mallocAndStrcpy_s fails then tlsio_cyclonessl_create shall fail and return NULL. ]*/ |
| 162 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_018: [ When tlsio_cyclonessl_create fails, all allocated resources up to that point shall be freed. ]*/ |
| 163 |
LogError("Failed to copy the hostname."); |
| 164 |
free(result); |
| 165 |
result = NULL; |
| 166 |
} |
| 167 |
else |
| 168 |
{ |
| 169 |
result->port = tls_io_config->port; |
| 170 |
result->certificate = NULL; |
| 171 |
result->on_bytes_received = NULL; |
| 172 |
result->on_bytes_received_context = NULL; |
| 173 |
result->on_io_error = NULL; |
| 174 |
result->on_io_error_context = NULL; |
| 175 |
result->tlsio_state = TLSIO_STATE_NOT_OPEN; |
| 176 |
result->socket = (TlsSocket)NULL; |
| 177 |
|
| 178 |
/* seed should be initialized with some random seed ... */ |
| 179 |
for (i = 0; i < 32; i++) |
| 180 |
{ |
| 181 |
seed[i] = rand() * 255 / RAND_MAX; |
| 182 |
} |
| 183 |
|
| 184 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_008: [ tlsio_cyclonessl_create shall initialize the yarrow context by calling yarrowInit. ]*/ |
| 185 |
if (yarrowInit(&result->yarrowContext) != NO_ERROR) |
| 186 |
{ |
| 187 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_009: [ If yarrowInit fails then tlsio_cyclonessl_create shall return NULL. ]*/ |
| 188 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_018: [ When tlsio_cyclonessl_create fails, all allocated resources up to that point shall be freed. ]*/ |
| 189 |
LogError("yarrowInit failed"); |
| 190 |
free(result->hostname); |
| 191 |
free(result); |
| 192 |
result = NULL; |
| 193 |
} |
| 194 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_010: [ The yarrow context shall be seeded with 32 bytes of randomly chosen data by calling yarrowSeed. ]*/ |
| 195 |
else if (yarrowSeed(&result->yarrowContext, seed, sizeof(seed)) != NO_ERROR) |
| 196 |
{ |
| 197 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_011: [ If yarrowSeed fails then tlsio_cyclonessl_create shall return NULL. ]*/ |
| 198 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_018: [ When tlsio_cyclonessl_create fails, all allocated resources up to that point shall be freed. ]*/ |
| 199 |
LogError("yarrowInit seed failed"); |
| 200 |
yarrowRelease(&result->yarrowContext); |
| 201 |
free(result->hostname); |
| 202 |
free(result); |
| 203 |
result = NULL; |
| 204 |
} |
| 205 |
else |
| 206 |
{ |
| 207 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_012: [ tlsio_cyclonessl_create shall create a TLS context by calling tlsInit. ]*/ |
| 208 |
result->tlsContext = tlsInit(); |
| 209 |
if (result->tlsContext == NULL) |
| 210 |
{ |
| 211 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_013: [ If tlsInit fails then tlsio_cyclonessl_create shall return NULL. ]*/ |
| 212 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_018: [ When tlsio_cyclonessl_create fails, all allocated resources up to that point shall be freed. ]*/ |
| 213 |
tlsFree(result->tlsContext); |
| 214 |
yarrowRelease(&result->yarrowContext); |
| 215 |
free(result->hostname); |
| 216 |
free(result); |
| 217 |
LogError("Creating the TLS context failed"); |
| 218 |
result = NULL; |
| 219 |
} |
| 220 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_014: [ The TLS context shall be setup to operate as a client by calling tlsSetConnectionEnd with TLS_CONNECTION_END_CLIENT. ]*/ |
| 221 |
else if (tlsSetConnectionEnd(result->tlsContext, TLS_CONNECTION_END_CLIENT)) |
| 222 |
{ |
| 223 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_015: [ If tlsSetConnectionEnd fails then tlsio_cyclonessl_create shall return NULL. ]*/ |
| 224 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_018: [ When tlsio_cyclonessl_create fails, all allocated resources up to that point shall be freed. ]*/ |
| 225 |
tlsFree(result->tlsContext); |
| 226 |
yarrowRelease(&result->yarrowContext); |
| 227 |
free(result->hostname); |
| 228 |
free(result); |
| 229 |
LogError("tlsSetConnectionEnd failed"); |
| 230 |
result = NULL; |
| 231 |
} |
| 232 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_016: [ The pseudo random number generator to be used shall be set by calling tlsSetPrng with the yarrow instance as argument. ]*/ |
| 233 |
else if (tlsSetPrng(result->tlsContext, YARROW_PRNG_ALGO, &result->yarrowContext) != NO_ERROR) |
| 234 |
{ |
| 235 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_017: [ If tlsSetPrng fails then tlsio_cyclonessl_create shall return NULL. ]*/ |
| 236 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_018: [ When tlsio_cyclonessl_create fails, all allocated resources up to that point shall be freed. ]*/ |
| 237 |
tlsFree(result->tlsContext); |
| 238 |
yarrowRelease(&result->yarrowContext); |
| 239 |
free(result->hostname); |
| 240 |
free(result); |
| 241 |
LogError("tlsSetPrng failed"); |
| 242 |
result = NULL; |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
return result; |
| 251 |
} |
| 252 |
|
| 253 |
static void tlsio_cyclonessl_destroy(CONCRETE_IO_HANDLE tls_io) |
| 254 |
{ |
| 255 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_020: [ If tls_io is NULL, tlsio_cyclonessl_destroy shall do nothing. ]*/ |
| 256 |
if (tls_io == NULL) |
| 257 |
{ |
| 258 |
LogError("NULL tls_io."); |
| 259 |
} |
| 260 |
else |
| 261 |
{ |
| 262 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_019: [ tlsio_cyclonessl_destroy shall free the tlsio CycloneSSL instance. ]*/ |
| 263 |
TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; |
| 264 |
|
| 265 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_081: [ tlsio_cyclonessl_destroy should close the IO if it was open before freeing all the resources. ]*/ |
| 266 |
tlsio_cyclonessl_close(tls_io, NULL, NULL); |
| 267 |
|
| 268 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_022: [ The TLS context shall be freed by calling tlsFree. ]*/ |
| 269 |
tlsFree(tls_io_instance->tlsContext); |
| 270 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_021: [ tlsio_cyclonessl_destroy shall deinitialize the yarrow context by calling yarrowRelease. ]*/ |
| 271 |
yarrowRelease(&tls_io_instance->yarrowContext); |
| 272 |
|
| 273 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_077: [ All options cached via tlsio_cyclonessl_set_option shall also be freed. ]*/ |
| 274 |
if (tls_io_instance->certificate != NULL) |
| 275 |
{ |
| 276 |
free(tls_io_instance->certificate); |
| 277 |
} |
| 278 |
free(tls_io_instance->hostname); |
| 279 |
free(tls_io); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
static int tlsio_cyclonessl_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) |
| 284 |
{ |
| 285 |
int result; |
| 286 |
|
| 287 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_024: [ If any of the arguments tls_io, on_io_open_complete, on_bytes_received or on_io_error are NULL then tlsio_cyclonessl_open shall return a non-zero value. ]*/ |
| 288 |
if ((tls_io == NULL) || |
| 289 |
(on_io_open_complete == NULL) || |
| 290 |
(on_bytes_received == NULL) || |
| 291 |
(on_io_error == NULL)) |
| 292 |
{ |
| 293 |
result = MU_FAILURE; |
| 294 |
LogError("NULL tls_io."); |
| 295 |
} |
| 296 |
else |
| 297 |
{ |
| 298 |
TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; |
| 299 |
|
| 300 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_034: [ If tlsio_cyclonessl_open is called while the IO is open, tlsio_cyclonessl_open shall fail and return a non-zero value without performing any work to open the IO. ]*/ |
| 301 |
if (tls_io_instance->tlsio_state != TLSIO_STATE_NOT_OPEN) |
| 302 |
{ |
| 303 |
result = MU_FAILURE; |
| 304 |
LogError("Invalid tlsio_state. Expected state is TLSIO_STATE_NOT_OPEN."); |
| 305 |
} |
| 306 |
else |
| 307 |
{ |
| 308 |
tls_io_instance->on_bytes_received = on_bytes_received; |
| 309 |
tls_io_instance->on_bytes_received_context = on_bytes_received_context; |
| 310 |
|
| 311 |
tls_io_instance->on_io_error = on_io_error; |
| 312 |
tls_io_instance->on_io_error_context = on_io_error_context; |
| 313 |
|
| 314 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_025: [ tlsio_cyclonessl_open shall create a socket by calling tlsio_cyclonessl_socket_create, while passing to it the hostname and port that were saved in the tlsio_cyclonessl_create. ]*/ |
| 315 |
if (tlsio_cyclonessl_socket_create(tls_io_instance->hostname, tls_io_instance->port, &tls_io_instance->socket) != 0) |
| 316 |
{ |
| 317 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_026: [ If tlsio_cyclonessl_socket_create fails, then tlsio_cyclonessl_open shall return a non-zero value. ]*/ |
| 318 |
LogError("Error: Cannot open socket"); |
| 319 |
result = MU_FAILURE; |
| 320 |
} |
| 321 |
else |
| 322 |
{ |
| 323 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_027: [ The socket created by tlsio_cyclonessl_socket_create shall be assigned to the TLS context by calling tlsSetSocket. ]*/ |
| 324 |
if (tlsSetSocket(tls_io_instance->tlsContext, tls_io_instance->socket)) |
| 325 |
{ |
| 326 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_028: [ If tlsSetSocket fails then tlsio_cyclonessl_open shall return a non-zero value. ]*/ |
| 327 |
tlsio_cyclonessl_socket_destroy(tls_io_instance->socket); |
| 328 |
tls_io_instance->socket = (TlsSocket)NULL; |
| 329 |
LogError("Error: tlsSetSocket"); |
| 330 |
result = MU_FAILURE; |
| 331 |
} |
| 332 |
else |
| 333 |
{ |
| 334 |
unsigned char is_error = 0; |
| 335 |
|
| 336 |
if (tls_io_instance->certificate != NULL) |
| 337 |
{ |
| 338 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_078: [ If certificates have been set by using tlsio_cyclonessl_set_option then a call to tlsSetTrustedCaList shall be made to pass the certificates to CycloneSSL. ]*/ |
| 339 |
if (tlsSetTrustedCaList(tls_io_instance->tlsContext, tls_io_instance->certificate, strlen(tls_io_instance->certificate))) |
| 340 |
{ |
| 341 |
is_error = 1; |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
if (is_error) |
| 346 |
{ |
| 347 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_079: [ If tlsSetTrustedCaList fails then tlsio_cyclonessl_open shall return a non-zero value. ]*/ |
| 348 |
tlsio_cyclonessl_socket_destroy(tls_io_instance->socket); |
| 349 |
tls_io_instance->socket = (TlsSocket)NULL; |
| 350 |
LogError("tlsSetTrustedCaList failed"); |
| 351 |
result = MU_FAILURE; |
| 352 |
} |
| 353 |
else |
| 354 |
{ |
| 355 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_031: [ tlsio_cyclonessl_open shall start the TLS handshake by calling tlsConnect. ]*/ |
| 356 |
if (tlsConnect(tls_io_instance->tlsContext)) |
| 357 |
{ |
| 358 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_032: [ If tlsConnect fails then tlsio_cyclonessl_open shall return a non-zero value. ]*/ |
| 359 |
tlsio_cyclonessl_socket_destroy(tls_io_instance->socket); |
| 360 |
tls_io_instance->socket = (TlsSocket)NULL; |
| 361 |
LogError("tlsConnect failed"); |
| 362 |
result = MU_FAILURE; |
| 363 |
} |
| 364 |
else |
| 365 |
{ |
| 366 |
tls_io_instance->tlsio_state = TLSIO_STATE_OPEN; |
| 367 |
|
| 368 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_033: [ If tlsConnect succeeds, the callback on_io_open_complete shall be called, while passing on_io_open_complete_context and IO_OPEN_OK as arguments. ]*/ |
| 369 |
on_io_open_complete(on_io_open_complete_context, IO_OPEN_OK); |
| 370 |
|
| 371 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_023: [ tlsio_cyclonessl_open shall open the TLS io and on success it shall return 0. ]*/ |
| 372 |
result = 0; |
| 373 |
} |
| 374 |
} |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
return result; |
| 381 |
} |
| 382 |
|
| 383 |
static int tlsio_cyclonessl_close(CONCRETE_IO_HANDLE tls_io, ON_IO_CLOSE_COMPLETE on_io_close_complete, void* on_io_close_complete_context) |
| 384 |
{ |
| 385 |
int result = 0; |
| 386 |
|
| 387 |
if (tls_io == NULL) |
| 388 |
{ |
| 389 |
LogError("NULL tls_io."); |
| 390 |
result = MU_FAILURE; |
| 391 |
} |
| 392 |
else |
| 393 |
{ |
| 394 |
TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; |
| 395 |
|
| 396 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_041: [ If tlsio_cyclonessl_close is called when not open, tlsio_cyclonessl_close shall fail and return a non-zero value. ]*/ |
| 397 |
if (tls_io_instance->tlsio_state == TLSIO_STATE_NOT_OPEN) |
| 398 |
{ |
| 399 |
result = MU_FAILURE; |
| 400 |
LogError("Invalid tlsio_state. Expected state is TLSIO_STATE_NOT_OPEN or TLSIO_STATE_CLOSING."); |
| 401 |
} |
| 402 |
else |
| 403 |
{ |
| 404 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_037: [ tlsio_cyclonessl_close shall close the TLS connection by calling tlsShutdown. ]*/ |
| 405 |
if (tlsShutdown(tls_io_instance->tlsContext)) |
| 406 |
{ |
| 407 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_038: [ If tlsShutdown fails, tlsio_cyclonessl_close shall fail and return a non-zero value. ]*/ |
| 408 |
LogError("tlsShutdown failed\r\n"); |
| 409 |
result = MU_FAILURE; |
| 410 |
} |
| 411 |
else |
| 412 |
{ |
| 413 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_039: [ tlsio_cyclonessl_destroy shall destroy the underlying socket by calling tlsio_cyclonessl_socket_destroy. ]*/ |
| 414 |
tlsio_cyclonessl_socket_destroy(tls_io_instance->socket); |
| 415 |
tls_io_instance->socket = (TlsSocket)NULL; |
| 416 |
tls_io_instance->tlsio_state = TLSIO_STATE_NOT_OPEN; |
| 417 |
|
| 418 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_040: [ On success, on_io_close_complete shall be called while passing as argument on_io_close_complete_context. ]*/ |
| 419 |
if (on_io_close_complete != NULL) |
| 420 |
{ |
| 421 |
on_io_close_complete(on_io_close_complete_context); |
| 422 |
} |
| 423 |
|
| 424 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_035: [ tlsio_cyclonessl_close shall close the TLS IO and on success it shall return 0. ]*/ |
| 425 |
result = 0; |
| 426 |
} |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
return result; |
| 431 |
} |
| 432 |
|
| 433 |
static int tlsio_cyclonessl_send(CONCRETE_IO_HANDLE tls_io, const void* buffer, size_t size, ON_SEND_COMPLETE on_send_complete, void* on_send_complete_context) |
| 434 |
{ |
| 435 |
int result; |
| 436 |
|
| 437 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_043: [ If any of the arguments tls_io or buffer is NULL, tlsio_cyclonessl_send shall fail and return a non-zero value. ]*/ |
| 438 |
if ((tls_io == NULL) || |
| 439 |
(buffer == NULL) || |
| 440 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_044: [ If size is 0, tlsio_cyclonessl_send shall fail and return a non-zero value. ]*/ |
| 441 |
(size == 0)) |
| 442 |
{ |
| 443 |
LogError("NULL tls_io."); |
| 444 |
result = MU_FAILURE; |
| 445 |
} |
| 446 |
else |
| 447 |
{ |
| 448 |
TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; |
| 449 |
|
| 450 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_048: [ If tlsio_cyclonessl_send is called when the IO is not open, tlsio_cyclonessl_send shall fail and return a non-zero value. ]*/ |
| 451 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_049: [ If the IO is in an error state (an error was reported through the on_io_error callback), tlsio_cyclonessl_send shall fail and return a non-zero value. ]*/ |
| 452 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_056: [ Also the IO shall be considered in error and any subsequent calls to tlsio_cyclonessl_send shall fail. ]*/ |
| 453 |
if (tls_io_instance->tlsio_state != TLSIO_STATE_OPEN) |
| 454 |
{ |
| 455 |
LogError("Invalid tlsio_state. Expected state is TLSIO_STATE_OPEN."); |
| 456 |
result = MU_FAILURE; |
| 457 |
} |
| 458 |
else |
| 459 |
{ |
| 460 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_047: [ tlsio_cyclonessl_send shall send the bytes by calling tlsWrite and passing buffer and size as arguments. 0 shall be passed for the flags argument. ]*/ |
| 461 |
if (tlsWrite(tls_io_instance->tlsContext, buffer, size, 0) != 0) |
| 462 |
{ |
| 463 |
LogError("SSL_write error."); |
| 464 |
result = MU_FAILURE; |
| 465 |
} |
| 466 |
else |
| 467 |
{ |
| 468 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_045: [ on_send_complete shall be allowed to be NULL. ]*/ |
| 469 |
if (on_send_complete != NULL) |
| 470 |
{ |
| 471 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_046: [ On success, if a non-NULL value was passed for on_send_complete, then on_send_complete shall be called while passing to it the on_send_complete_context value. ]*/ |
| 472 |
on_send_complete(on_send_complete_context, IO_SEND_OK); |
| 473 |
} |
| 474 |
|
| 475 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_042: [ tlsio_cyclonessl_send shall send the size bytes pointed to by buffer and on success it shall return 0. ]*/ |
| 476 |
result = 0; |
| 477 |
} |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
return result; |
| 482 |
} |
| 483 |
|
| 484 |
static void tlsio_cyclonessl_dowork(CONCRETE_IO_HANDLE tls_io) |
| 485 |
{ |
| 486 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_051: [ If the tls_io argument is NULL, tlsio_cyclonessl_dowork shall do nothing. ]*/ |
| 487 |
if (tls_io == NULL) |
| 488 |
{ |
| 489 |
LogError("NULL tls_io."); |
| 490 |
} |
| 491 |
else |
| 492 |
{ |
| 493 |
TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; |
| 494 |
|
| 495 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_052: [ If the IO is not open (no open has been called or the IO has been closed) then tlsio_cyclonessl_dowork shall do nothing. ]*/ |
| 496 |
if ((tls_io_instance->tlsio_state != TLSIO_STATE_NOT_OPEN) && |
| 497 |
(tls_io_instance->tlsio_state != TLSIO_STATE_ERROR)) |
| 498 |
{ |
| 499 |
unsigned char buffer[64]; |
| 500 |
|
| 501 |
size_t received; |
| 502 |
|
| 503 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_050: [ tlsio_cyclonessl_dowork shall check if any bytes are available to be read from the CycloneSSL library and indicate those bytes as received. ]*/ |
| 504 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_053: [ If the IO is open, tlsio_cyclonessl_dowork shall attempt to read 64 bytes from the TLS library by calling tlsRead. ]*/ |
| 505 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_054: [ The flags argument for tlsRead shall be 0. ]*/ |
| 506 |
if (tlsRead(tls_io_instance->tlsContext, buffer, sizeof(buffer), &received, 0) != 0) |
| 507 |
{ |
| 508 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_055: [ If tlsRead fails, the error shall be indicated by calling the on_io_error callback passed in tlsio_cyclonessl_open, while passing the on_io_error_context to the callback. ]*/ |
| 509 |
LogError("Error received bytes"); |
| 510 |
|
| 511 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_055: [ If tlsRead fails, the error shall be indicated by calling the on_io_error callback passed in tlsio_cyclonessl_open, while passing the on_io_error_context to the callback. ]*/ |
| 512 |
tls_io_instance->tlsio_state = TLSIO_STATE_ERROR; |
| 513 |
tls_io_instance->on_io_error(tls_io_instance->on_io_error_context); |
| 514 |
} |
| 515 |
else |
| 516 |
{ |
| 517 |
if (received > 0) |
| 518 |
{ |
| 519 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_080: [ If any bytes are read from CycloneSSL they should be indicated via the on_bytes_received callback passed to tlsio_cyclonessl_open. ]*/ |
| 520 |
tls_io_instance->on_bytes_received(tls_io_instance->on_bytes_received_context, buffer, received); |
| 521 |
} |
| 522 |
} |
| 523 |
} |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
static int tlsio_cyclonessl_setoption(CONCRETE_IO_HANDLE tls_io, const char* optionName, const void* value) |
| 528 |
{ |
| 529 |
int result; |
| 530 |
|
| 531 |
/* Tests_SRS_TLSIO_CYCLONESSL_01_057: [ If any of the arguments tls_io or option_name is NULL tlsio_cyclonessl_setoption shall return a non-zero value. ]*/ |
| 532 |
if ((tls_io == NULL) || (optionName == NULL)) |
| 533 |
{ |
| 534 |
LogError("NULL tls_io"); |
| 535 |
result = MU_FAILURE; |
| 536 |
} |
| 537 |
else |
| 538 |
{ |
| 539 |
TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; |
| 540 |
|
| 541 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_060: [ - "TrustedCerts" - a char\* that shall be saved by tlsio_cyclonessl as it shall be given to the underlying CycloneSSL TLS context when the IO is open. ]*/ |
| 542 |
if (strcmp("TrustedCerts", optionName) == 0) |
| 543 |
{ |
| 544 |
const char* cert = (const char*)value; |
| 545 |
|
| 546 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_062: [ If a previous TrustedCerts option was saved, then the previous value shall be freed. ]*/ |
| 547 |
if (tls_io_instance->certificate != NULL) |
| 548 |
{ |
| 549 |
// Free the memory if it has been previously allocated |
| 550 |
free(tls_io_instance->certificate); |
| 551 |
tls_io_instance->certificate = NULL; |
| 552 |
} |
| 553 |
|
| 554 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_063: [ A NULL value shall be allowed for TrustedCerts, in which case the previously stored TrustedCerts option value shall be cleared. ]*/ |
| 555 |
if (cert == NULL) |
| 556 |
{ |
| 557 |
result = 0; |
| 558 |
} |
| 559 |
else |
| 560 |
{ |
| 561 |
// Store the certificate |
| 562 |
if (mallocAndStrcpy_s(&tls_io_instance->certificate, cert) != 0) |
| 563 |
{ |
| 564 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_061: [ If copying the char\* passed in value fails then tlsio_cyclonessl_setoption shall return a non-zero value. ]*/ |
| 565 |
LogError("Error allocating memory for certificates"); |
| 566 |
result = MU_FAILURE; |
| 567 |
} |
| 568 |
else |
| 569 |
{ |
| 570 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_059: [ If the option was handled by tlsio_cyclonessl, then tlsio_cyclonessl_setoption shall return 0. ]*/ |
| 571 |
result = 0; |
| 572 |
} |
| 573 |
} |
| 574 |
} |
| 575 |
else |
| 576 |
{ |
| 577 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_058: [ If the option_name argument indicates an option that is not handled by tlsio_cyclonessl, then tlsio_cyclonessl_setoption shall return a non-zero value. ]*/ |
| 578 |
LogError("Unrecognized option"); |
| 579 |
result = MU_FAILURE; |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
return result; |
| 584 |
} |
| 585 |
|
| 586 |
static OPTIONHANDLER_HANDLE tlsio_cyclonessl_retrieve_options(CONCRETE_IO_HANDLE handle) |
| 587 |
{ |
| 588 |
OPTIONHANDLER_HANDLE result; |
| 589 |
|
| 590 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_064: [ If parameter handle is NULL then tlsio_cyclonessl_retrieve_options shall fail and return NULL. ]*/ |
| 591 |
if (handle == NULL) |
| 592 |
{ |
| 593 |
LogError("invalid parameter detected: CONCRETE_IO_HANDLE handle=%p", handle); |
| 594 |
result = NULL; |
| 595 |
} |
| 596 |
else |
| 597 |
{ |
| 598 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_065: [ tlsio_cyclonessl_retrieve_options shall produce an OPTIONHANDLER_HANDLE. ]*/ |
| 599 |
result = OptionHandler_Create(tlsio_cyclonessl_clone_option, tlsio_cyclonessl_destroy_option, tlsio_cyclonessl_setoption); |
| 600 |
if (result == NULL) |
| 601 |
{ |
| 602 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_068: [ If producing the OPTIONHANDLER_HANDLE fails then tlsio_cyclonessl_retrieve_options shall fail and return NULL. ]*/ |
| 603 |
LogError("unable to OptionHandler_Create"); |
| 604 |
/*return as is*/ |
| 605 |
} |
| 606 |
else |
| 607 |
{ |
| 608 |
TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)handle; |
| 609 |
|
| 610 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_066: [ tlsio_cyclonessl_retrieve_options shall add to it the options: ]*/ |
| 611 |
if ( |
| 612 |
(tls_io_instance->certificate != NULL) && |
| 613 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_067: [ - TrustedCerts ]*/ |
| 614 |
(OptionHandler_AddOption(result, "TrustedCerts", tls_io_instance->certificate) != OPTIONHANDLER_OK) |
| 615 |
) |
| 616 |
{ |
| 617 |
LogError("unable to save TrustedCerts option"); |
| 618 |
OptionHandler_Destroy(result); |
| 619 |
result = NULL; |
| 620 |
} |
| 621 |
else |
| 622 |
{ |
| 623 |
/*all is fine, all interesting options have been saved*/ |
| 624 |
/*return as is*/ |
| 625 |
} |
| 626 |
} |
| 627 |
} |
| 628 |
return result; |
| 629 |
} |
| 630 |
|
| 631 |
static const IO_INTERFACE_DESCRIPTION tlsio_cyclonessl_interface_description = |
| 632 |
{ |
| 633 |
tlsio_cyclonessl_retrieve_options, |
| 634 |
tlsio_cyclonessl_create, |
| 635 |
tlsio_cyclonessl_destroy, |
| 636 |
tlsio_cyclonessl_open, |
| 637 |
tlsio_cyclonessl_close, |
| 638 |
tlsio_cyclonessl_send, |
| 639 |
tlsio_cyclonessl_dowork, |
| 640 |
tlsio_cyclonessl_setoption |
| 641 |
}; |
| 642 |
|
| 643 |
/* Codes_SRS_TLSIO_CYCLONESSL_01_069: [ tlsio_cyclonessl_get_interface_description shall return a pointer to an IO_INTERFACE_DESCRIPTION structure that contains pointers to the functions: tlsio_cyclonessl_retrieve_options, tlsio_cyclonessl_create, tlsio_cyclonessl_destroy, tlsio_cyclonessl_open, tlsio_cyclonessl_close, tlsio_cyclonessl_send and tlsio_cyclonessl_dowork. ]*/ |
| 644 |
const IO_INTERFACE_DESCRIPTION* tlsio_cyclonessl_get_interface_description(void) |
| 645 |
{ |
| 646 |
return &tlsio_cyclonessl_interface_description; |
| 647 |
} |