| 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 |
#include <stdlib.h> |
| 5 |
#include <stddef.h> |
| 6 |
#include <stdio.h> |
| 7 |
#include "azure_c_shared_utility/tlsio.h" |
| 8 |
#include "azure_c_shared_utility/shared_util_options.h" |
| 9 |
#include "azure_c_shared_utility/xlogging.h" |
| 10 |
#include "azure_c_shared_utility/gballoc.h" |
| 11 |
|
| 12 |
#define TLSIO_STATE_VALUES \ |
| 13 |
TLSIO_STATE_NOT_OPEN, \ |
| 14 |
TLSIO_STATE_OPENING_UNDERLYING_IO, \ |
| 15 |
TLSIO_STATE_HANDSHAKE_CLIENT_HELLO_SENT, \ |
| 16 |
TLSIO_STATE_HANDSHAKE_SERVER_HELLO_RECEIVED, \ |
| 17 |
TLSIO_STATE_RENEGOTIATE, \ |
| 18 |
TLSIO_STATE_OPEN, \ |
| 19 |
TLSIO_STATE_CLOSING, \ |
| 20 |
TLSIO_STATE_ERROR |
| 21 |
|
| 22 |
MU_DEFINE_ENUM(TLSIO_STATE, TLSIO_STATE_VALUES); |
| 23 |
MU_DEFINE_ENUM_STRINGS(TLSIO_STATE, TLSIO_STATE_VALUES); |
| 24 |
|
| 25 |
typedef struct TLS_IO_INSTANCE_TAG |
| 26 |
{ |
| 27 |
XIO_HANDLE socket_io; |
| 28 |
ON_IO_OPEN_COMPLETE on_io_open_complete; |
| 29 |
ON_IO_CLOSE_COMPLETE on_io_close_complete; |
| 30 |
ON_BYTES_RECEIVED on_bytes_received; |
| 31 |
ON_IO_ERROR on_io_error; |
| 32 |
|
| 33 |
void* on_io_open_complete_context; |
| 34 |
void* on_io_close_complete_context; |
| 35 |
void* on_bytes_received_context; |
| 36 |
void* on_io_error_context; |
| 37 |
} TLS_IO_INSTANCE; |
| 38 |
|
| 39 |
/*this function will clone an option given by name and value*/ |
| 40 |
static void* tlsio_template_CloneOption(const char* name, const void* value) |
| 41 |
{ |
| 42 |
void* result; |
| 43 |
if (name == NULL || value == NULL) |
| 44 |
{ |
| 45 |
LogError("invalid parameter detected: const char* name = %p, const void* value = %p", name, value); |
| 46 |
result = NULL; |
| 47 |
} |
| 48 |
else |
| 49 |
{ |
| 50 |
result = NULL; |
| 51 |
} |
| 52 |
return result; |
| 53 |
} |
| 54 |
|
| 55 |
/*this function destroys an option previously created*/ |
| 56 |
static void tlsio_template_DestroyOption(const char* name, const void* value) |
| 57 |
{ |
| 58 |
/*since all options for this layer are actually string copies., disposing of one is just calling free*/ |
| 59 |
if (name == NULL || value == NULL) |
| 60 |
{ |
| 61 |
LogError("invalid parameter detected: const char* name = %p, const void* value = %p", name, value); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
static OPTIONHANDLER_HANDLE tlsio_template_retrieveoptions(CONCRETE_IO_HANDLE handle) |
| 66 |
{ |
| 67 |
OPTIONHANDLER_HANDLE result; |
| 68 |
if (handle == NULL) |
| 69 |
{ |
| 70 |
LogError("invalid parameter detected: CONCRETE_IO_HANDLE handle = %p", handle); |
| 71 |
result = NULL; |
| 72 |
} |
| 73 |
else |
| 74 |
{ |
| 75 |
result = OptionHandler_Create(tlsio_template_CloneOption, tlsio_template_DestroyOption, tlsio_template_setoption); |
| 76 |
if (result == NULL) |
| 77 |
{ |
| 78 |
LogError("unable to OptionHandler_Create"); |
| 79 |
/*return as is*/ |
| 80 |
} |
| 81 |
} |
| 82 |
return result; |
| 83 |
} |
| 84 |
|
| 85 |
static const IO_INTERFACE_DESCRIPTION tlsio_template_interface_description = |
| 86 |
{ |
| 87 |
tlsio_template_retrieveoptions, |
| 88 |
tlsio_template_create, |
| 89 |
tlsio_template_destroy, |
| 90 |
tlsio_template_open, |
| 91 |
tlsio_template_close, |
| 92 |
tlsio_template_send, |
| 93 |
tlsio_template_dowork, |
| 94 |
tlsio_template_setoption |
| 95 |
}; |
| 96 |
|
| 97 |
static void indicate_error(TLS_IO_INSTANCE* tls_io_instance) |
| 98 |
{ |
| 99 |
if (tls_io_instance->on_io_error != NULL) |
| 100 |
{ |
| 101 |
tls_io_instance->on_io_error(tls_io_instance->on_io_error_context); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
CONCRETE_IO_HANDLE tlsio_template_create(void* io_create_parameters) |
| 106 |
{ |
| 107 |
TLSIO_CONFIG* tls_io_config = (TLSIO_CONFIG *)io_create_parameters; |
| 108 |
TLS_IO_INSTANCE* result; |
| 109 |
|
| 110 |
if (tls_io_config == NULL) |
| 111 |
{ |
| 112 |
LogError("invalid argument detected: void* io_create_parameters = %p", tls_io_config); |
| 113 |
result = NULL; |
| 114 |
} |
| 115 |
else |
| 116 |
{ |
| 117 |
result = (TLS_IO_INSTANCE*)malloc(sizeof(TLS_IO_INSTANCE)); |
| 118 |
if (result == NULL) |
| 119 |
{ |
| 120 |
LogError("malloc failed"); |
| 121 |
} |
| 122 |
else |
| 123 |
{ |
| 124 |
(void)memset(result, 0, sizeof(TLS_IO_INSTANCE)); |
| 125 |
} |
| 126 |
} |
| 127 |
return result; |
| 128 |
} |
| 129 |
|
| 130 |
void tlsio_template_destroy(CONCRETE_IO_HANDLE tls_io) |
| 131 |
{ |
| 132 |
if (tls_io != NULL) |
| 133 |
{ |
| 134 |
TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; |
| 135 |
free(tls_io_instance); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
int tlsio_template_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) |
| 140 |
{ |
| 141 |
int result; |
| 142 |
(void)on_io_open_complete; |
| 143 |
(void)on_io_open_complete_context; |
| 144 |
(void)on_bytes_received; |
| 145 |
(void)on_bytes_received_context; |
| 146 |
(void)on_io_error; |
| 147 |
(void)on_io_error_context; |
| 148 |
if (tls_io == NULL) |
| 149 |
{ |
| 150 |
LogError("invalid argument detected: CONCRETE_IO_HANDLE tls_io = %p", tls_io); |
| 151 |
result = MU_FAILURE; |
| 152 |
} |
| 153 |
else |
| 154 |
{ |
| 155 |
TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; |
| 156 |
(void)tls_io_instance; |
| 157 |
result = 0; |
| 158 |
} |
| 159 |
return result; |
| 160 |
} |
| 161 |
|
| 162 |
int tlsio_template_close(CONCRETE_IO_HANDLE tls_io, ON_IO_CLOSE_COMPLETE on_io_close_complete, void* callback_context) |
| 163 |
{ |
| 164 |
int result = 0; |
| 165 |
(void)callback_context; |
| 166 |
(void)on_io_close_complete; |
| 167 |
|
| 168 |
if (tls_io == NULL) |
| 169 |
{ |
| 170 |
LogError("invalid argument detected: tls_io = %p", tls_io); |
| 171 |
result = MU_FAILURE; |
| 172 |
} |
| 173 |
else |
| 174 |
{ |
| 175 |
TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; |
| 176 |
(void)tls_io_instance; |
| 177 |
result = 0; |
| 178 |
} |
| 179 |
return result; |
| 180 |
} |
| 181 |
|
| 182 |
int tlsio_template_send(CONCRETE_IO_HANDLE tls_io, const void* buffer, size_t size, ON_SEND_COMPLETE on_send_complete, void* callback_context) |
| 183 |
{ |
| 184 |
int result; |
| 185 |
(void)buffer; |
| 186 |
(void)size; |
| 187 |
(void)on_send_complete; |
| 188 |
(void)callback_context; |
| 189 |
|
| 190 |
TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; |
| 191 |
(void)tls_io_instance; |
| 192 |
result = 0; |
| 193 |
return result; |
| 194 |
} |
| 195 |
|
| 196 |
void tlsio_template_dowork(CONCRETE_IO_HANDLE tls_io) |
| 197 |
{ |
| 198 |
if (tls_io != NULL) |
| 199 |
{ |
| 200 |
TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; |
| 201 |
(void)tls_io_instance; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
int tlsio_template_setoption(CONCRETE_IO_HANDLE tls_io, const char* optionName, const void* value) |
| 206 |
{ |
| 207 |
int result; |
| 208 |
(void)value; |
| 209 |
|
| 210 |
if (tls_io == NULL || optionName == NULL) |
| 211 |
{ |
| 212 |
LogError("invalid argument detected: CONCRETE_IO_HANDLE tls_io = %p, const char* optionName = %p", tls_io, optionName); |
| 213 |
result = MU_FAILURE; |
| 214 |
} |
| 215 |
else |
| 216 |
{ |
| 217 |
result = 0; |
| 218 |
} |
| 219 |
return result; |
| 220 |
} |
| 221 |
|
| 222 |
const IO_INTERFACE_DESCRIPTION* tlsio_template_get_interface_description(void) |
| 223 |
{ |
| 224 |
return &tlsio_template_interface_description; |
| 225 |
} |