A small memory footprint AMQP implimentation
Dependents: iothub_client_sample_amqp remote_monitoring simplesample_amqp
saslclientio.c@47:365a93fdb5bb, 2018-10-04 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Thu Oct 04 09:16:13 2018 -0700
- Revision:
- 47:365a93fdb5bb
- Parent:
- 43:4c1e4e94cdd3
1.2.10
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Azure.IoT Build | 0:6ae2f7bca550 | 1 | // Copyright (c) Microsoft. All rights reserved. |
Azure.IoT Build | 0:6ae2f7bca550 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
Azure.IoT Build | 0:6ae2f7bca550 | 3 | |
Azure.IoT Build | 0:6ae2f7bca550 | 4 | #include <stdlib.h> |
Azure.IoT Build | 0:6ae2f7bca550 | 5 | #include <stddef.h> |
Azure.IoT Build | 0:6ae2f7bca550 | 6 | #include <stdio.h> |
Azure.IoT Build | 0:6ae2f7bca550 | 7 | #include <string.h> |
AzureIoTClient | 6:641a9672db08 | 8 | #include <stdbool.h> |
AzureIoTClient | 23:1111ee8bcba4 | 9 | |
AzureIoTClient | 21:f9c433d8e6ca | 10 | #include "azure_c_shared_utility/gballoc.h" |
AzureIoTClient | 19:000ab4e6a2c1 | 11 | #include "azure_c_shared_utility/optimize_size.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 12 | #include "azure_uamqp_c/saslclientio.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 13 | #include "azure_c_shared_utility/xio.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 14 | #include "azure_c_shared_utility/xlogging.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 15 | #include "azure_uamqp_c/frame_codec.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 16 | #include "azure_uamqp_c/sasl_frame_codec.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 17 | #include "azure_uamqp_c/amqp_definitions.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 18 | #include "azure_uamqp_c/amqpvalue_to_string.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 19 | |
Azure.IoT Build | 0:6ae2f7bca550 | 20 | typedef enum IO_STATE_TAG |
Azure.IoT Build | 0:6ae2f7bca550 | 21 | { |
AzureIoTClient | 6:641a9672db08 | 22 | IO_STATE_NOT_OPEN, |
AzureIoTClient | 6:641a9672db08 | 23 | IO_STATE_OPENING_UNDERLYING_IO, |
AzureIoTClient | 6:641a9672db08 | 24 | IO_STATE_SASL_HANDSHAKE, |
AzureIoTClient | 6:641a9672db08 | 25 | IO_STATE_OPEN, |
AzureIoTClient | 6:641a9672db08 | 26 | IO_STATE_CLOSING, |
AzureIoTClient | 6:641a9672db08 | 27 | IO_STATE_ERROR |
Azure.IoT Build | 0:6ae2f7bca550 | 28 | } IO_STATE; |
Azure.IoT Build | 0:6ae2f7bca550 | 29 | |
AzureIoTClient | 23:1111ee8bcba4 | 30 | #define SASL_HEADER_EXCHANGE_STATE_VALUES \ |
AzureIoTClient | 23:1111ee8bcba4 | 31 | SASL_HEADER_EXCHANGE_IDLE, \ |
AzureIoTClient | 23:1111ee8bcba4 | 32 | SASL_HEADER_EXCHANGE_HEADER_SENT, \ |
AzureIoTClient | 23:1111ee8bcba4 | 33 | SASL_HEADER_EXCHANGE_HEADER_RCVD, \ |
AzureIoTClient | 6:641a9672db08 | 34 | SASL_HEADER_EXCHANGE_HEADER_EXCH |
AzureIoTClient | 23:1111ee8bcba4 | 35 | |
AzureIoTClient | 43:4c1e4e94cdd3 | 36 | // Suppress unused function warning for SASL_HEADER_EXCHANGE_STATEstrings |
AzureIoTClient | 43:4c1e4e94cdd3 | 37 | #ifdef NO_LOGGING |
AzureIoTClient | 43:4c1e4e94cdd3 | 38 | #define ENUM_TO_STRING_UNUSED |
AzureIoTClient | 43:4c1e4e94cdd3 | 39 | #include "azure_c_shared_utility/macro_utils.h" |
AzureIoTClient | 43:4c1e4e94cdd3 | 40 | #endif |
AzureIoTClient | 43:4c1e4e94cdd3 | 41 | |
AzureIoTClient | 23:1111ee8bcba4 | 42 | DEFINE_LOCAL_ENUM(SASL_HEADER_EXCHANGE_STATE, SASL_HEADER_EXCHANGE_STATE_VALUES) |
Azure.IoT Build | 0:6ae2f7bca550 | 43 | |
AzureIoTClient | 23:1111ee8bcba4 | 44 | #define SASL_CLIENT_NEGOTIATION_STATE_VALUES \ |
AzureIoTClient | 23:1111ee8bcba4 | 45 | SASL_CLIENT_NEGOTIATION_NOT_STARTED, \ |
AzureIoTClient | 23:1111ee8bcba4 | 46 | SASL_CLIENT_NEGOTIATION_MECH_RCVD, \ |
AzureIoTClient | 23:1111ee8bcba4 | 47 | SASL_CLIENT_NEGOTIATION_INIT_SENT, \ |
AzureIoTClient | 23:1111ee8bcba4 | 48 | SASL_CLIENT_NEGOTIATION_CHALLENGE_RCVD, \ |
AzureIoTClient | 23:1111ee8bcba4 | 49 | SASL_CLIENT_NEGOTIATION_RESPONSE_SENT, \ |
AzureIoTClient | 23:1111ee8bcba4 | 50 | SASL_CLIENT_NEGOTIATION_OUTCOME_RCVD, \ |
AzureIoTClient | 6:641a9672db08 | 51 | SASL_CLIENT_NEGOTIATION_ERROR |
AzureIoTClient | 23:1111ee8bcba4 | 52 | |
AzureIoTClient | 23:1111ee8bcba4 | 53 | DEFINE_LOCAL_ENUM(SASL_CLIENT_NEGOTIATION_STATE, SASL_CLIENT_NEGOTIATION_STATE_VALUES) |
Azure.IoT Build | 0:6ae2f7bca550 | 54 | |
Azure.IoT Build | 0:6ae2f7bca550 | 55 | typedef struct SASL_CLIENT_IO_INSTANCE_TAG |
Azure.IoT Build | 0:6ae2f7bca550 | 56 | { |
AzureIoTClient | 6:641a9672db08 | 57 | XIO_HANDLE underlying_io; |
AzureIoTClient | 6:641a9672db08 | 58 | ON_BYTES_RECEIVED on_bytes_received; |
AzureIoTClient | 6:641a9672db08 | 59 | ON_IO_OPEN_COMPLETE on_io_open_complete; |
AzureIoTClient | 6:641a9672db08 | 60 | ON_IO_CLOSE_COMPLETE on_io_close_complete; |
AzureIoTClient | 6:641a9672db08 | 61 | ON_IO_ERROR on_io_error; |
Azure.IoT Build | 0:6ae2f7bca550 | 62 | void* on_bytes_received_context; |
AzureIoTClient | 6:641a9672db08 | 63 | void* on_io_open_complete_context; |
AzureIoTClient | 6:641a9672db08 | 64 | void* on_io_close_complete_context; |
Azure.IoT Build | 0:6ae2f7bca550 | 65 | void* on_io_error_context; |
AzureIoTClient | 6:641a9672db08 | 66 | SASL_HEADER_EXCHANGE_STATE sasl_header_exchange_state; |
AzureIoTClient | 6:641a9672db08 | 67 | SASL_CLIENT_NEGOTIATION_STATE sasl_client_negotiation_state; |
AzureIoTClient | 6:641a9672db08 | 68 | size_t header_bytes_received; |
AzureIoTClient | 6:641a9672db08 | 69 | SASL_FRAME_CODEC_HANDLE sasl_frame_codec; |
AzureIoTClient | 6:641a9672db08 | 70 | FRAME_CODEC_HANDLE frame_codec; |
AzureIoTClient | 6:641a9672db08 | 71 | IO_STATE io_state; |
AzureIoTClient | 6:641a9672db08 | 72 | SASL_MECHANISM_HANDLE sasl_mechanism; |
AzureIoTClient | 6:641a9672db08 | 73 | unsigned int is_trace_on : 1; |
AzureIoTClient | 30:0407b2db334c | 74 | unsigned int is_trace_on_set : 1; |
Azure.IoT Build | 0:6ae2f7bca550 | 75 | } SASL_CLIENT_IO_INSTANCE; |
Azure.IoT Build | 0:6ae2f7bca550 | 76 | |
AzureIoTClient | 16:22a72cf8e416 | 77 | /* Codes_SRS_SASLCLIENTIO_01_002: [The protocol header consists of the upper case ASCII letters "AMQP" followed by a protocol id of three, followed by three unsigned bytes representing the major, minor, and revision of the specification version (currently 1 (SASL-MAJOR), 0 (SASLMINOR), 0 (SASL-REVISION)).] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 78 | /* Codes_SRS_SASLCLIENTIO_01_124: [SASL-MAJOR 1 major protocol version.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 79 | /* Codes_SRS_SASLCLIENTIO_01_125: [SASL-MINOR 0 minor protocol version.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 80 | /* Codes_SRS_SASLCLIENTIO_01_126: [SASL-REVISION 0 protocol revision.] */ |
AzureIoTClient | 21:f9c433d8e6ca | 81 | static const unsigned char sasl_header[] = { 'A', 'M', 'Q', 'P', 3, 1, 0, 0 }; |
Azure.IoT Build | 0:6ae2f7bca550 | 82 | |
Azure.IoT Build | 0:6ae2f7bca550 | 83 | static void indicate_error(SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance) |
Azure.IoT Build | 0:6ae2f7bca550 | 84 | { |
AzureIoTClient | 6:641a9672db08 | 85 | if (sasl_client_io_instance->on_io_error != NULL) |
AzureIoTClient | 6:641a9672db08 | 86 | { |
AzureIoTClient | 6:641a9672db08 | 87 | sasl_client_io_instance->on_io_error(sasl_client_io_instance->on_io_error_context); |
AzureIoTClient | 6:641a9672db08 | 88 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 89 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 90 | |
Azure.IoT Build | 0:6ae2f7bca550 | 91 | static void indicate_open_complete(SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance, IO_OPEN_RESULT open_result) |
Azure.IoT Build | 0:6ae2f7bca550 | 92 | { |
AzureIoTClient | 6:641a9672db08 | 93 | if (sasl_client_io_instance->on_io_open_complete != NULL) |
AzureIoTClient | 6:641a9672db08 | 94 | { |
AzureIoTClient | 6:641a9672db08 | 95 | sasl_client_io_instance->on_io_open_complete(sasl_client_io_instance->on_io_open_complete_context, open_result); |
AzureIoTClient | 6:641a9672db08 | 96 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 97 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 98 | |
Azure.IoT Build | 0:6ae2f7bca550 | 99 | static void indicate_close_complete(SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance) |
Azure.IoT Build | 0:6ae2f7bca550 | 100 | { |
AzureIoTClient | 6:641a9672db08 | 101 | if (sasl_client_io_instance->on_io_close_complete != NULL) |
AzureIoTClient | 6:641a9672db08 | 102 | { |
AzureIoTClient | 6:641a9672db08 | 103 | sasl_client_io_instance->on_io_close_complete(sasl_client_io_instance->on_io_close_complete_context); |
AzureIoTClient | 6:641a9672db08 | 104 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 105 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 106 | |
Azure.IoT Build | 0:6ae2f7bca550 | 107 | static void on_underlying_io_close_complete(void* context) |
Azure.IoT Build | 0:6ae2f7bca550 | 108 | { |
AzureIoTClient | 6:641a9672db08 | 109 | SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance = (SASL_CLIENT_IO_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 110 | |
AzureIoTClient | 6:641a9672db08 | 111 | switch (sasl_client_io_instance->io_state) |
AzureIoTClient | 6:641a9672db08 | 112 | { |
AzureIoTClient | 6:641a9672db08 | 113 | default: |
AzureIoTClient | 6:641a9672db08 | 114 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 115 | |
AzureIoTClient | 6:641a9672db08 | 116 | case IO_STATE_OPENING_UNDERLYING_IO: |
AzureIoTClient | 6:641a9672db08 | 117 | case IO_STATE_SASL_HANDSHAKE: |
AzureIoTClient | 6:641a9672db08 | 118 | sasl_client_io_instance->io_state = IO_STATE_NOT_OPEN; |
AzureIoTClient | 6:641a9672db08 | 119 | indicate_open_complete(sasl_client_io_instance, IO_OPEN_ERROR); |
AzureIoTClient | 6:641a9672db08 | 120 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 121 | |
AzureIoTClient | 6:641a9672db08 | 122 | case IO_STATE_CLOSING: |
AzureIoTClient | 6:641a9672db08 | 123 | sasl_client_io_instance->io_state = IO_STATE_NOT_OPEN; |
AzureIoTClient | 6:641a9672db08 | 124 | indicate_close_complete(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 125 | break; |
AzureIoTClient | 6:641a9672db08 | 126 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 127 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 128 | |
Azure.IoT Build | 0:6ae2f7bca550 | 129 | static void handle_error(SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance) |
Azure.IoT Build | 0:6ae2f7bca550 | 130 | { |
AzureIoTClient | 6:641a9672db08 | 131 | switch (sasl_client_io_instance->io_state) |
AzureIoTClient | 6:641a9672db08 | 132 | { |
AzureIoTClient | 6:641a9672db08 | 133 | default: |
AzureIoTClient | 6:641a9672db08 | 134 | case IO_STATE_NOT_OPEN: |
AzureIoTClient | 6:641a9672db08 | 135 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 136 | |
AzureIoTClient | 6:641a9672db08 | 137 | case IO_STATE_OPENING_UNDERLYING_IO: |
AzureIoTClient | 6:641a9672db08 | 138 | case IO_STATE_SASL_HANDSHAKE: |
AzureIoTClient | 6:641a9672db08 | 139 | if (xio_close(sasl_client_io_instance->underlying_io, on_underlying_io_close_complete, sasl_client_io_instance) != 0) |
AzureIoTClient | 6:641a9672db08 | 140 | { |
AzureIoTClient | 6:641a9672db08 | 141 | sasl_client_io_instance->io_state = IO_STATE_NOT_OPEN; |
AzureIoTClient | 6:641a9672db08 | 142 | indicate_open_complete(sasl_client_io_instance, IO_OPEN_ERROR); |
AzureIoTClient | 6:641a9672db08 | 143 | } |
AzureIoTClient | 6:641a9672db08 | 144 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 145 | |
AzureIoTClient | 6:641a9672db08 | 146 | case IO_STATE_OPEN: |
AzureIoTClient | 6:641a9672db08 | 147 | sasl_client_io_instance->io_state = IO_STATE_ERROR; |
AzureIoTClient | 6:641a9672db08 | 148 | indicate_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 149 | break; |
AzureIoTClient | 6:641a9672db08 | 150 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 151 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 152 | |
AzureIoTClient | 43:4c1e4e94cdd3 | 153 | // This callback usage needs to be either verified and commented or integrated into |
AzureIoTClient | 38:7631b92cc772 | 154 | // the state machine. |
AzureIoTClient | 38:7631b92cc772 | 155 | static void unchecked_on_send_complete(void* context, IO_SEND_RESULT send_result) |
AzureIoTClient | 38:7631b92cc772 | 156 | { |
AzureIoTClient | 38:7631b92cc772 | 157 | (void)context; |
AzureIoTClient | 38:7631b92cc772 | 158 | (void)send_result; |
AzureIoTClient | 38:7631b92cc772 | 159 | } |
AzureIoTClient | 38:7631b92cc772 | 160 | |
Azure.IoT Build | 0:6ae2f7bca550 | 161 | static int send_sasl_header(SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance) |
Azure.IoT Build | 0:6ae2f7bca550 | 162 | { |
AzureIoTClient | 6:641a9672db08 | 163 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 164 | |
AzureIoTClient | 6:641a9672db08 | 165 | /* Codes_SRS_SASLCLIENTIO_01_078: [SASL client IO shall start the header exchange by sending the SASL header.] */ |
AzureIoTClient | 30:0407b2db334c | 166 | /* Codes_SRS_SASLCLIENTIO_01_095: [Sending the header shall be done by using `xio_send`.]*/ |
AzureIoTClient | 38:7631b92cc772 | 167 | if (xio_send(sasl_client_io_instance->underlying_io, sasl_header, sizeof(sasl_header), unchecked_on_send_complete, NULL) != 0) |
AzureIoTClient | 6:641a9672db08 | 168 | { |
AzureIoTClient | 23:1111ee8bcba4 | 169 | LogError("Sending SASL header failed"); |
AzureIoTClient | 19:000ab4e6a2c1 | 170 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 171 | } |
AzureIoTClient | 6:641a9672db08 | 172 | else |
AzureIoTClient | 6:641a9672db08 | 173 | { |
AzureIoTClient | 30:0407b2db334c | 174 | if (sasl_client_io_instance->is_trace_on != 0) |
AzureIoTClient | 6:641a9672db08 | 175 | { |
AzureIoTClient | 16:22a72cf8e416 | 176 | LOG(AZ_LOG_TRACE, LOG_LINE, "-> Header (AMQP 3.1.0.0)"); |
AzureIoTClient | 6:641a9672db08 | 177 | } |
AzureIoTClient | 23:1111ee8bcba4 | 178 | |
AzureIoTClient | 6:641a9672db08 | 179 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 180 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 181 | |
AzureIoTClient | 6:641a9672db08 | 182 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 183 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 184 | |
Azure.IoT Build | 0:6ae2f7bca550 | 185 | static void on_underlying_io_open_complete(void* context, IO_OPEN_RESULT open_result) |
Azure.IoT Build | 0:6ae2f7bca550 | 186 | { |
AzureIoTClient | 6:641a9672db08 | 187 | SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance = (SASL_CLIENT_IO_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 188 | |
AzureIoTClient | 6:641a9672db08 | 189 | switch (sasl_client_io_instance->io_state) |
AzureIoTClient | 6:641a9672db08 | 190 | { |
AzureIoTClient | 6:641a9672db08 | 191 | default: |
AzureIoTClient | 23:1111ee8bcba4 | 192 | LogError("Open complete received in unexpected state"); |
AzureIoTClient | 6:641a9672db08 | 193 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 194 | |
AzureIoTClient | 30:0407b2db334c | 195 | case IO_STATE_SASL_HANDSHAKE: |
AzureIoTClient | 30:0407b2db334c | 196 | /* Codes_SRS_SASLCLIENTIO_01_110: [raise ERROR] */ |
AzureIoTClient | 30:0407b2db334c | 197 | case IO_STATE_OPEN: |
AzureIoTClient | 30:0407b2db334c | 198 | /* Codes_SRS_SASLCLIENTIO_01_106: [raise error] */ |
AzureIoTClient | 30:0407b2db334c | 199 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 30:0407b2db334c | 200 | break; |
AzureIoTClient | 30:0407b2db334c | 201 | |
AzureIoTClient | 6:641a9672db08 | 202 | case IO_STATE_OPENING_UNDERLYING_IO: |
AzureIoTClient | 6:641a9672db08 | 203 | if (open_result == IO_OPEN_OK) |
AzureIoTClient | 6:641a9672db08 | 204 | { |
AzureIoTClient | 6:641a9672db08 | 205 | sasl_client_io_instance->io_state = IO_STATE_SASL_HANDSHAKE; |
AzureIoTClient | 6:641a9672db08 | 206 | if (sasl_client_io_instance->sasl_header_exchange_state != SASL_HEADER_EXCHANGE_IDLE) |
AzureIoTClient | 6:641a9672db08 | 207 | { |
AzureIoTClient | 30:0407b2db334c | 208 | /* Codes_SRS_SASLCLIENTIO_01_116: [If the underlying IO indicates another open while the after the header exchange has been started an error shall be indicated by calling `on_io_error`.]*/ |
AzureIoTClient | 6:641a9672db08 | 209 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 210 | } |
AzureIoTClient | 6:641a9672db08 | 211 | else |
AzureIoTClient | 6:641a9672db08 | 212 | { |
AzureIoTClient | 6:641a9672db08 | 213 | /* Codes_SRS_SASLCLIENTIO_01_105: [start header exchange] */ |
AzureIoTClient | 6:641a9672db08 | 214 | /* Codes_SRS_SASLCLIENTIO_01_001: [To establish a SASL layer, each peer MUST start by sending a protocol header.] */ |
AzureIoTClient | 6:641a9672db08 | 215 | if (send_sasl_header(sasl_client_io_instance) != 0) |
AzureIoTClient | 6:641a9672db08 | 216 | { |
AzureIoTClient | 30:0407b2db334c | 217 | /* Codes_SRS_SASLCLIENTIO_01_073: [If the handshake fails (i.e. the outcome is an error) the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/ |
AzureIoTClient | 30:0407b2db334c | 218 | /* Codes_SRS_SASLCLIENTIO_01_077: [If sending the SASL header fails, the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/ |
AzureIoTClient | 6:641a9672db08 | 219 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 220 | } |
AzureIoTClient | 6:641a9672db08 | 221 | else |
AzureIoTClient | 6:641a9672db08 | 222 | { |
AzureIoTClient | 6:641a9672db08 | 223 | sasl_client_io_instance->sasl_header_exchange_state = SASL_HEADER_EXCHANGE_HEADER_SENT; |
AzureIoTClient | 6:641a9672db08 | 224 | } |
AzureIoTClient | 6:641a9672db08 | 225 | } |
AzureIoTClient | 6:641a9672db08 | 226 | } |
AzureIoTClient | 6:641a9672db08 | 227 | else |
AzureIoTClient | 6:641a9672db08 | 228 | { |
AzureIoTClient | 6:641a9672db08 | 229 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 230 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 231 | |
AzureIoTClient | 6:641a9672db08 | 232 | break; |
AzureIoTClient | 6:641a9672db08 | 233 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 234 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 235 | |
Azure.IoT Build | 0:6ae2f7bca550 | 236 | static void on_underlying_io_error(void* context) |
Azure.IoT Build | 0:6ae2f7bca550 | 237 | { |
AzureIoTClient | 6:641a9672db08 | 238 | SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance = (SASL_CLIENT_IO_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 239 | |
AzureIoTClient | 6:641a9672db08 | 240 | switch (sasl_client_io_instance->io_state) |
AzureIoTClient | 6:641a9672db08 | 241 | { |
AzureIoTClient | 6:641a9672db08 | 242 | default: |
AzureIoTClient | 23:1111ee8bcba4 | 243 | LogError("Error callback received in unexpected state"); |
AzureIoTClient | 6:641a9672db08 | 244 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 245 | |
AzureIoTClient | 6:641a9672db08 | 246 | case IO_STATE_OPENING_UNDERLYING_IO: |
AzureIoTClient | 6:641a9672db08 | 247 | case IO_STATE_SASL_HANDSHAKE: |
AzureIoTClient | 30:0407b2db334c | 248 | /* Codes_SRS_SASLCLIENTIO_01_101: [`on_open_complete` with `IO_OPEN_ERROR`]*/ |
AzureIoTClient | 30:0407b2db334c | 249 | if (xio_close(sasl_client_io_instance->underlying_io, on_underlying_io_close_complete, sasl_client_io_instance) != 0) |
AzureIoTClient | 30:0407b2db334c | 250 | { |
AzureIoTClient | 30:0407b2db334c | 251 | sasl_client_io_instance->io_state = IO_STATE_NOT_OPEN; |
AzureIoTClient | 30:0407b2db334c | 252 | indicate_open_complete(sasl_client_io_instance, IO_OPEN_ERROR); |
AzureIoTClient | 30:0407b2db334c | 253 | } |
AzureIoTClient | 30:0407b2db334c | 254 | |
AzureIoTClient | 6:641a9672db08 | 255 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 256 | |
AzureIoTClient | 6:641a9672db08 | 257 | case IO_STATE_OPEN: |
AzureIoTClient | 6:641a9672db08 | 258 | sasl_client_io_instance->io_state = IO_STATE_ERROR; |
AzureIoTClient | 6:641a9672db08 | 259 | indicate_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 260 | break; |
AzureIoTClient | 6:641a9672db08 | 261 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 262 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 263 | |
AzureIoTClient | 43:4c1e4e94cdd3 | 264 | #ifndef NO_LOGGING |
Azure.IoT Build | 0:6ae2f7bca550 | 265 | static const char* get_frame_type_as_string(AMQP_VALUE descriptor) |
Azure.IoT Build | 0:6ae2f7bca550 | 266 | { |
AzureIoTClient | 6:641a9672db08 | 267 | const char* result; |
Azure.IoT Build | 0:6ae2f7bca550 | 268 | |
AzureIoTClient | 6:641a9672db08 | 269 | if (is_sasl_mechanisms_type_by_descriptor(descriptor)) |
AzureIoTClient | 6:641a9672db08 | 270 | { |
AzureIoTClient | 6:641a9672db08 | 271 | result = "[SASL MECHANISMS]"; |
AzureIoTClient | 6:641a9672db08 | 272 | } |
AzureIoTClient | 6:641a9672db08 | 273 | else if (is_sasl_init_type_by_descriptor(descriptor)) |
AzureIoTClient | 6:641a9672db08 | 274 | { |
AzureIoTClient | 6:641a9672db08 | 275 | result = "[SASL INIT]"; |
AzureIoTClient | 6:641a9672db08 | 276 | } |
AzureIoTClient | 6:641a9672db08 | 277 | else if (is_sasl_challenge_type_by_descriptor(descriptor)) |
AzureIoTClient | 6:641a9672db08 | 278 | { |
AzureIoTClient | 6:641a9672db08 | 279 | result = "[SASL CHALLENGE]"; |
AzureIoTClient | 6:641a9672db08 | 280 | } |
AzureIoTClient | 6:641a9672db08 | 281 | else if (is_sasl_response_type_by_descriptor(descriptor)) |
AzureIoTClient | 6:641a9672db08 | 282 | { |
AzureIoTClient | 6:641a9672db08 | 283 | result = "[SASL RESPONSE]"; |
AzureIoTClient | 6:641a9672db08 | 284 | } |
AzureIoTClient | 6:641a9672db08 | 285 | else if (is_sasl_outcome_type_by_descriptor(descriptor)) |
AzureIoTClient | 6:641a9672db08 | 286 | { |
AzureIoTClient | 6:641a9672db08 | 287 | result = "[SASL OUTCOME]"; |
AzureIoTClient | 6:641a9672db08 | 288 | } |
AzureIoTClient | 6:641a9672db08 | 289 | else |
AzureIoTClient | 6:641a9672db08 | 290 | { |
AzureIoTClient | 6:641a9672db08 | 291 | result = "[Unknown]"; |
AzureIoTClient | 6:641a9672db08 | 292 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 293 | |
AzureIoTClient | 6:641a9672db08 | 294 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 295 | } |
AzureIoTClient | 43:4c1e4e94cdd3 | 296 | #endif // NO_LOGGING |
Azure.IoT Build | 0:6ae2f7bca550 | 297 | |
AzureIoTClient | 6:641a9672db08 | 298 | static void log_incoming_frame(AMQP_VALUE performative) |
Azure.IoT Build | 0:6ae2f7bca550 | 299 | { |
AzureIoTClient | 9:c22db038556c | 300 | #ifdef NO_LOGGING |
AzureIoTClient | 9:c22db038556c | 301 | UNUSED(performative); |
AzureIoTClient | 9:c22db038556c | 302 | #else |
AzureIoTClient | 6:641a9672db08 | 303 | if (xlogging_get_log_function() != NULL) |
AzureIoTClient | 6:641a9672db08 | 304 | { |
AzureIoTClient | 6:641a9672db08 | 305 | AMQP_VALUE descriptor = amqpvalue_get_inplace_descriptor(performative); |
AzureIoTClient | 6:641a9672db08 | 306 | if (descriptor != NULL) |
AzureIoTClient | 6:641a9672db08 | 307 | { |
AzureIoTClient | 28:add19eb7defa | 308 | char* performative_as_string; |
AzureIoTClient | 16:22a72cf8e416 | 309 | LOG(AZ_LOG_TRACE, 0, "<- "); |
AzureIoTClient | 16:22a72cf8e416 | 310 | LOG(AZ_LOG_TRACE, 0, (char*)get_frame_type_as_string(descriptor)); |
AzureIoTClient | 25:1101516ee67d | 311 | performative_as_string = NULL; |
AzureIoTClient | 16:22a72cf8e416 | 312 | LOG(AZ_LOG_TRACE, LOG_LINE, (performative_as_string = amqpvalue_to_string(performative))); |
AzureIoTClient | 6:641a9672db08 | 313 | if (performative_as_string != NULL) |
AzureIoTClient | 6:641a9672db08 | 314 | { |
AzureIoTClient | 21:f9c433d8e6ca | 315 | free(performative_as_string); |
AzureIoTClient | 6:641a9672db08 | 316 | } |
AzureIoTClient | 6:641a9672db08 | 317 | } |
AzureIoTClient | 6:641a9672db08 | 318 | } |
AzureIoTClient | 9:c22db038556c | 319 | #endif |
Azure.IoT Build | 0:6ae2f7bca550 | 320 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 321 | |
AzureIoTClient | 6:641a9672db08 | 322 | static void log_outgoing_frame(AMQP_VALUE performative) |
Azure.IoT Build | 0:6ae2f7bca550 | 323 | { |
AzureIoTClient | 9:c22db038556c | 324 | #ifdef NO_LOGGING |
AzureIoTClient | 9:c22db038556c | 325 | UNUSED(performative); |
AzureIoTClient | 9:c22db038556c | 326 | #else |
AzureIoTClient | 6:641a9672db08 | 327 | if (xlogging_get_log_function() != NULL) |
AzureIoTClient | 6:641a9672db08 | 328 | { |
AzureIoTClient | 6:641a9672db08 | 329 | AMQP_VALUE descriptor = amqpvalue_get_inplace_descriptor(performative); |
AzureIoTClient | 6:641a9672db08 | 330 | if (descriptor != NULL) |
AzureIoTClient | 6:641a9672db08 | 331 | { |
AzureIoTClient | 28:add19eb7defa | 332 | char* performative_as_string; |
AzureIoTClient | 16:22a72cf8e416 | 333 | LOG(AZ_LOG_TRACE, 0, "-> "); |
AzureIoTClient | 16:22a72cf8e416 | 334 | LOG(AZ_LOG_TRACE, 0, (char*)get_frame_type_as_string(descriptor)); |
AzureIoTClient | 25:1101516ee67d | 335 | performative_as_string = NULL; |
AzureIoTClient | 16:22a72cf8e416 | 336 | LOG(AZ_LOG_TRACE, LOG_LINE, (performative_as_string = amqpvalue_to_string(performative))); |
AzureIoTClient | 6:641a9672db08 | 337 | if (performative_as_string != NULL) |
AzureIoTClient | 6:641a9672db08 | 338 | { |
AzureIoTClient | 21:f9c433d8e6ca | 339 | free(performative_as_string); |
AzureIoTClient | 6:641a9672db08 | 340 | } |
AzureIoTClient | 6:641a9672db08 | 341 | } |
AzureIoTClient | 6:641a9672db08 | 342 | } |
AzureIoTClient | 9:c22db038556c | 343 | #endif |
Azure.IoT Build | 0:6ae2f7bca550 | 344 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 345 | |
Azure.IoT Build | 0:6ae2f7bca550 | 346 | static int saslclientio_receive_byte(SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance, unsigned char b) |
Azure.IoT Build | 0:6ae2f7bca550 | 347 | { |
AzureIoTClient | 6:641a9672db08 | 348 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 349 | |
AzureIoTClient | 6:641a9672db08 | 350 | switch (sasl_client_io_instance->sasl_header_exchange_state) |
AzureIoTClient | 6:641a9672db08 | 351 | { |
AzureIoTClient | 6:641a9672db08 | 352 | default: |
AzureIoTClient | 23:1111ee8bcba4 | 353 | LogError("Byte being received in unexpected state: %s", ENUM_TO_STRING(SASL_HEADER_EXCHANGE_STATE, sasl_client_io_instance->sasl_header_exchange_state)); |
AzureIoTClient | 19:000ab4e6a2c1 | 354 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 355 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 356 | |
AzureIoTClient | 6:641a9672db08 | 357 | case SASL_HEADER_EXCHANGE_HEADER_EXCH: |
AzureIoTClient | 6:641a9672db08 | 358 | switch (sasl_client_io_instance->sasl_client_negotiation_state) |
AzureIoTClient | 6:641a9672db08 | 359 | { |
AzureIoTClient | 6:641a9672db08 | 360 | case SASL_CLIENT_NEGOTIATION_ERROR: |
AzureIoTClient | 23:1111ee8bcba4 | 361 | LogError("Byte being received in unexpected state: %s", ENUM_TO_STRING(SASL_CLIENT_NEGOTIATION_STATE, SASL_CLIENT_NEGOTIATION_ERROR)); |
AzureIoTClient | 19:000ab4e6a2c1 | 362 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 363 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 364 | |
AzureIoTClient | 6:641a9672db08 | 365 | default: |
AzureIoTClient | 30:0407b2db334c | 366 | /* Codes_SRS_SASLCLIENTIO_01_068: [During the SASL frame exchange that constitutes the handshake the received bytes from the underlying IO shall be fed to the frame codec instance created in `saslclientio_create` by calling `frame_codec_receive_bytes`.]*/ |
AzureIoTClient | 6:641a9672db08 | 367 | if (frame_codec_receive_bytes(sasl_client_io_instance->frame_codec, &b, 1) != 0) |
AzureIoTClient | 6:641a9672db08 | 368 | { |
AzureIoTClient | 30:0407b2db334c | 369 | /* Codes_SRS_SASLCLIENTIO_01_088: [If `frame_codec_receive_bytes` fails, the `on_io_error` callback shall be triggered.]*/ |
AzureIoTClient | 19:000ab4e6a2c1 | 370 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 371 | } |
AzureIoTClient | 6:641a9672db08 | 372 | else |
AzureIoTClient | 6:641a9672db08 | 373 | { |
AzureIoTClient | 6:641a9672db08 | 374 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 375 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 376 | |
AzureIoTClient | 6:641a9672db08 | 377 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 378 | |
AzureIoTClient | 6:641a9672db08 | 379 | case SASL_CLIENT_NEGOTIATION_OUTCOME_RCVD: |
AzureIoTClient | 6:641a9672db08 | 380 | sasl_client_io_instance->on_bytes_received(sasl_client_io_instance->on_bytes_received_context, &b, 1); |
AzureIoTClient | 6:641a9672db08 | 381 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 382 | break; |
AzureIoTClient | 6:641a9672db08 | 383 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 384 | |
AzureIoTClient | 6:641a9672db08 | 385 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 386 | |
AzureIoTClient | 6:641a9672db08 | 387 | /* Codes_SRS_SASLCLIENTIO_01_003: [Other than using a protocol id of three, the exchange of SASL layer headers follows the same rules specified in the version negotiation section of the transport specification (See Part 2: section 2.2).] */ |
AzureIoTClient | 6:641a9672db08 | 388 | case SASL_HEADER_EXCHANGE_IDLE: |
AzureIoTClient | 6:641a9672db08 | 389 | case SASL_HEADER_EXCHANGE_HEADER_SENT: |
AzureIoTClient | 6:641a9672db08 | 390 | if (b != sasl_header[sasl_client_io_instance->header_bytes_received]) |
AzureIoTClient | 6:641a9672db08 | 391 | { |
AzureIoTClient | 23:1111ee8bcba4 | 392 | LogError("Mismatched SASL header"); |
AzureIoTClient | 19:000ab4e6a2c1 | 393 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 394 | } |
AzureIoTClient | 6:641a9672db08 | 395 | else |
AzureIoTClient | 6:641a9672db08 | 396 | { |
AzureIoTClient | 6:641a9672db08 | 397 | sasl_client_io_instance->header_bytes_received++; |
AzureIoTClient | 6:641a9672db08 | 398 | if (sasl_client_io_instance->header_bytes_received == sizeof(sasl_header)) |
AzureIoTClient | 6:641a9672db08 | 399 | { |
AzureIoTClient | 30:0407b2db334c | 400 | if (sasl_client_io_instance->is_trace_on != 0) |
AzureIoTClient | 6:641a9672db08 | 401 | { |
AzureIoTClient | 16:22a72cf8e416 | 402 | LOG(AZ_LOG_TRACE, LOG_LINE, "<- Header (AMQP 3.1.0.0)"); |
AzureIoTClient | 6:641a9672db08 | 403 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 404 | |
AzureIoTClient | 6:641a9672db08 | 405 | switch (sasl_client_io_instance->sasl_header_exchange_state) |
AzureIoTClient | 6:641a9672db08 | 406 | { |
AzureIoTClient | 6:641a9672db08 | 407 | default: |
AzureIoTClient | 23:1111ee8bcba4 | 408 | LogError("Invalid SASL header exchange state: %s", ENUM_TO_STRING(SASL_HEADER_EXCHANGE_STATE, sasl_client_io_instance->sasl_header_exchange_state)); |
AzureIoTClient | 19:000ab4e6a2c1 | 409 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 410 | break; |
AzureIoTClient | 43:4c1e4e94cdd3 | 411 | |
AzureIoTClient | 6:641a9672db08 | 412 | case SASL_HEADER_EXCHANGE_HEADER_SENT: |
AzureIoTClient | 6:641a9672db08 | 413 | /* from this point on we need to decode SASL frames */ |
AzureIoTClient | 6:641a9672db08 | 414 | sasl_client_io_instance->sasl_header_exchange_state = SASL_HEADER_EXCHANGE_HEADER_EXCH; |
AzureIoTClient | 6:641a9672db08 | 415 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 416 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 417 | |
AzureIoTClient | 6:641a9672db08 | 418 | case SASL_HEADER_EXCHANGE_IDLE: |
AzureIoTClient | 6:641a9672db08 | 419 | sasl_client_io_instance->sasl_header_exchange_state = SASL_HEADER_EXCHANGE_HEADER_RCVD; |
AzureIoTClient | 6:641a9672db08 | 420 | if (send_sasl_header(sasl_client_io_instance) != 0) |
AzureIoTClient | 6:641a9672db08 | 421 | { |
AzureIoTClient | 30:0407b2db334c | 422 | /* Codes_SRS_SASLCLIENTIO_01_077: [If sending the SASL header fails, the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/ |
AzureIoTClient | 23:1111ee8bcba4 | 423 | LogError("Could not send SASL header"); |
AzureIoTClient | 19:000ab4e6a2c1 | 424 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 425 | } |
AzureIoTClient | 6:641a9672db08 | 426 | else |
AzureIoTClient | 6:641a9672db08 | 427 | { |
AzureIoTClient | 6:641a9672db08 | 428 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 429 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 430 | |
AzureIoTClient | 6:641a9672db08 | 431 | break; |
AzureIoTClient | 6:641a9672db08 | 432 | } |
AzureIoTClient | 6:641a9672db08 | 433 | } |
AzureIoTClient | 6:641a9672db08 | 434 | else |
AzureIoTClient | 6:641a9672db08 | 435 | { |
AzureIoTClient | 6:641a9672db08 | 436 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 437 | } |
AzureIoTClient | 6:641a9672db08 | 438 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 439 | |
AzureIoTClient | 6:641a9672db08 | 440 | break; |
AzureIoTClient | 6:641a9672db08 | 441 | } |
AzureIoTClient | 6:641a9672db08 | 442 | |
AzureIoTClient | 6:641a9672db08 | 443 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 444 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 445 | |
Azure.IoT Build | 0:6ae2f7bca550 | 446 | static void on_underlying_io_bytes_received(void* context, const unsigned char* buffer, size_t size) |
Azure.IoT Build | 0:6ae2f7bca550 | 447 | { |
AzureIoTClient | 6:641a9672db08 | 448 | SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance = (SASL_CLIENT_IO_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 449 | |
AzureIoTClient | 30:0407b2db334c | 450 | /* Codes_SRS_SASLCLIENTIO_01_028: [If `buffer` is NULL or `size` is zero, nothing should be indicated as received, the state shall be switched to ERROR and the `on_io_error` callback shall be triggered.]*/ |
AzureIoTClient | 6:641a9672db08 | 451 | if ((buffer == NULL) || |
AzureIoTClient | 6:641a9672db08 | 452 | (size == 0)) |
AzureIoTClient | 6:641a9672db08 | 453 | { |
AzureIoTClient | 23:1111ee8bcba4 | 454 | LogError("Bad buffer received from the underlying IO, buffer = %p, size = %u", |
AzureIoTClient | 23:1111ee8bcba4 | 455 | buffer, (unsigned int)size); |
AzureIoTClient | 6:641a9672db08 | 456 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 457 | } |
AzureIoTClient | 6:641a9672db08 | 458 | else |
AzureIoTClient | 6:641a9672db08 | 459 | { |
AzureIoTClient | 6:641a9672db08 | 460 | switch (sasl_client_io_instance->io_state) |
AzureIoTClient | 6:641a9672db08 | 461 | { |
AzureIoTClient | 6:641a9672db08 | 462 | default: |
AzureIoTClient | 6:641a9672db08 | 463 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 464 | |
AzureIoTClient | 6:641a9672db08 | 465 | case IO_STATE_OPEN: |
AzureIoTClient | 30:0407b2db334c | 466 | /* Codes_SRS_SASLCLIENTIO_01_027: [When the `on_underlying_io_bytes_received` callback passed to the underlying IO is called and the SASL client IO state is `IO_STATE_OPEN`, the bytes shall be indicated to the user of SASL client IO by calling the `on_bytes_received` callback that was passed in `saslclientio_open`.]*/ |
AzureIoTClient | 30:0407b2db334c | 467 | /* Codes_SRS_SASLCLIENTIO_01_029: [The `context` argument for `on_io_error` shall be set to the `on_io_error_context` passed in `saslclientio_open`.]*/ |
AzureIoTClient | 6:641a9672db08 | 468 | sasl_client_io_instance->on_bytes_received(sasl_client_io_instance->on_bytes_received_context, buffer, size); |
AzureIoTClient | 6:641a9672db08 | 469 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 470 | |
AzureIoTClient | 6:641a9672db08 | 471 | case IO_STATE_SASL_HANDSHAKE: |
AzureIoTClient | 6:641a9672db08 | 472 | { |
AzureIoTClient | 6:641a9672db08 | 473 | size_t i; |
Azure.IoT Build | 0:6ae2f7bca550 | 474 | |
AzureIoTClient | 30:0407b2db334c | 475 | /* Codes_SRS_SASLCLIENTIO_01_030: [If bytes are received when the SASL client IO state is `IO_STATE_OPENING`, the bytes shall be consumed by the SASL client IO to satisfy the SASL handshake.]*/ |
AzureIoTClient | 6:641a9672db08 | 476 | for (i = 0; i < size; i++) |
AzureIoTClient | 6:641a9672db08 | 477 | { |
AzureIoTClient | 6:641a9672db08 | 478 | if (saslclientio_receive_byte(sasl_client_io_instance, buffer[i]) != 0) |
AzureIoTClient | 6:641a9672db08 | 479 | { |
AzureIoTClient | 6:641a9672db08 | 480 | break; |
AzureIoTClient | 6:641a9672db08 | 481 | } |
AzureIoTClient | 6:641a9672db08 | 482 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 483 | |
AzureIoTClient | 6:641a9672db08 | 484 | if (i < size) |
AzureIoTClient | 6:641a9672db08 | 485 | { |
AzureIoTClient | 30:0407b2db334c | 486 | /* Codes_SRS_SASLCLIENTIO_01_073: [If the handshake fails (i.e. the outcome is an error) the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/ |
AzureIoTClient | 6:641a9672db08 | 487 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 488 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 489 | |
AzureIoTClient | 6:641a9672db08 | 490 | break; |
AzureIoTClient | 6:641a9672db08 | 491 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 492 | |
AzureIoTClient | 6:641a9672db08 | 493 | case IO_STATE_ERROR: |
AzureIoTClient | 30:0407b2db334c | 494 | /* Codes_SRS_SASLCLIENTIO_01_031: [If bytes are received when the SASL client IO state is `IO_STATE_ERROR`, SASL client IO shall do nothing.]*/ |
AzureIoTClient | 6:641a9672db08 | 495 | break; |
AzureIoTClient | 6:641a9672db08 | 496 | } |
AzureIoTClient | 6:641a9672db08 | 497 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 498 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 499 | |
Azure.IoT Build | 0:6ae2f7bca550 | 500 | static void on_bytes_encoded(void* context, const unsigned char* bytes, size_t length, bool encode_complete) |
Azure.IoT Build | 0:6ae2f7bca550 | 501 | { |
AzureIoTClient | 25:1101516ee67d | 502 | SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance = (SASL_CLIENT_IO_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 503 | |
AzureIoTClient | 25:1101516ee67d | 504 | (void)encode_complete; |
Azure.IoT Build | 0:6ae2f7bca550 | 505 | |
AzureIoTClient | 30:0407b2db334c | 506 | /* Codes_SRS_SASLCLIENTIO_01_120: [When SASL client IO is notified by `sasl_frame_codec` of bytes that have been encoded via the `on_bytes_encoded` callback and SASL client IO is in the state OPENING, SASL client IO shall send these bytes by using `xio_send`.]*/ |
AzureIoTClient | 38:7631b92cc772 | 507 | if (xio_send(sasl_client_io_instance->underlying_io, bytes, length, unchecked_on_send_complete, NULL) != 0) |
AzureIoTClient | 6:641a9672db08 | 508 | { |
AzureIoTClient | 30:0407b2db334c | 509 | /* Codes_SRS_SASLCLIENTIO_01_121: [If `xio_send` fails, the `on_io_error` callback shall be triggered.]*/ |
AzureIoTClient | 23:1111ee8bcba4 | 510 | LogError("xio_send failed"); |
AzureIoTClient | 6:641a9672db08 | 511 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 512 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 513 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 514 | |
Azure.IoT Build | 0:6ae2f7bca550 | 515 | static int send_sasl_init(SASL_CLIENT_IO_INSTANCE* sasl_client_io, const char* sasl_mechanism_name) |
Azure.IoT Build | 0:6ae2f7bca550 | 516 | { |
AzureIoTClient | 6:641a9672db08 | 517 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 518 | |
AzureIoTClient | 6:641a9672db08 | 519 | SASL_INIT_HANDLE sasl_init; |
AzureIoTClient | 6:641a9672db08 | 520 | SASL_MECHANISM_BYTES init_bytes; |
Azure.IoT Build | 0:6ae2f7bca550 | 521 | |
AzureIoTClient | 30:0407b2db334c | 522 | init_bytes.length = 0; |
AzureIoTClient | 30:0407b2db334c | 523 | init_bytes.bytes = NULL; |
AzureIoTClient | 30:0407b2db334c | 524 | |
AzureIoTClient | 6:641a9672db08 | 525 | /* Codes_SRS_SASLCLIENTIO_01_045: [The name of the SASL mechanism used for the SASL exchange.] */ |
AzureIoTClient | 6:641a9672db08 | 526 | sasl_init = sasl_init_create(sasl_mechanism_name); |
AzureIoTClient | 6:641a9672db08 | 527 | if (sasl_init == NULL) |
AzureIoTClient | 6:641a9672db08 | 528 | { |
AzureIoTClient | 30:0407b2db334c | 529 | /* Codes_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/ |
AzureIoTClient | 23:1111ee8bcba4 | 530 | LogError("Could not create sasl_init"); |
AzureIoTClient | 19:000ab4e6a2c1 | 531 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 532 | } |
AzureIoTClient | 6:641a9672db08 | 533 | else |
AzureIoTClient | 6:641a9672db08 | 534 | { |
AzureIoTClient | 6:641a9672db08 | 535 | /* Codes_SRS_SASLCLIENTIO_01_048: [The contents of this data are defined by the SASL security mechanism.] */ |
AzureIoTClient | 6:641a9672db08 | 536 | if (saslmechanism_get_init_bytes(sasl_client_io->sasl_mechanism, &init_bytes) != 0) |
AzureIoTClient | 6:641a9672db08 | 537 | { |
AzureIoTClient | 30:0407b2db334c | 538 | /* Codes_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/ |
AzureIoTClient | 23:1111ee8bcba4 | 539 | LogError("Could not get SASL init bytes"); |
AzureIoTClient | 19:000ab4e6a2c1 | 540 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 541 | } |
AzureIoTClient | 6:641a9672db08 | 542 | else |
AzureIoTClient | 6:641a9672db08 | 543 | { |
AzureIoTClient | 6:641a9672db08 | 544 | amqp_binary creds; |
AzureIoTClient | 6:641a9672db08 | 545 | creds.bytes = init_bytes.bytes; |
AzureIoTClient | 6:641a9672db08 | 546 | creds.length = init_bytes.length; |
AzureIoTClient | 6:641a9672db08 | 547 | if ((init_bytes.length > 0) && |
AzureIoTClient | 6:641a9672db08 | 548 | /* Codes_SRS_SASLCLIENTIO_01_047: [A block of opaque data passed to the security mechanism.] */ |
AzureIoTClient | 6:641a9672db08 | 549 | (sasl_init_set_initial_response(sasl_init, creds) != 0)) |
AzureIoTClient | 6:641a9672db08 | 550 | { |
AzureIoTClient | 30:0407b2db334c | 551 | /* Codes_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/ |
AzureIoTClient | 23:1111ee8bcba4 | 552 | LogError("Could not set initial response"); |
AzureIoTClient | 19:000ab4e6a2c1 | 553 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 554 | } |
AzureIoTClient | 6:641a9672db08 | 555 | else |
AzureIoTClient | 6:641a9672db08 | 556 | { |
AzureIoTClient | 6:641a9672db08 | 557 | AMQP_VALUE sasl_init_value = amqpvalue_create_sasl_init(sasl_init); |
AzureIoTClient | 6:641a9672db08 | 558 | if (sasl_init_value == NULL) |
AzureIoTClient | 6:641a9672db08 | 559 | { |
AzureIoTClient | 30:0407b2db334c | 560 | /* Codes_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/ |
AzureIoTClient | 23:1111ee8bcba4 | 561 | LogError("Could not create SASL init"); |
AzureIoTClient | 19:000ab4e6a2c1 | 562 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 563 | } |
AzureIoTClient | 6:641a9672db08 | 564 | else |
AzureIoTClient | 6:641a9672db08 | 565 | { |
AzureIoTClient | 30:0407b2db334c | 566 | /* Codes_SRS_SASLCLIENTIO_01_070: [When a frame needs to be sent as part of the SASL handshake frame exchange, the send shall be done by calling `sasl_frame_codec_encode_frame`.]*/ |
AzureIoTClient | 6:641a9672db08 | 567 | if (sasl_frame_codec_encode_frame(sasl_client_io->sasl_frame_codec, sasl_init_value, on_bytes_encoded, sasl_client_io) != 0) |
AzureIoTClient | 6:641a9672db08 | 568 | { |
AzureIoTClient | 30:0407b2db334c | 569 | /* Codes_SRS_SASLCLIENTIO_01_071: [If `sasl_frame_codec_encode_frame` fails, then the `on_io_error` callback shall be triggered.]*/ |
AzureIoTClient | 23:1111ee8bcba4 | 570 | LogError("Could not encode SASL init value"); |
AzureIoTClient | 19:000ab4e6a2c1 | 571 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 572 | } |
AzureIoTClient | 6:641a9672db08 | 573 | else |
AzureIoTClient | 6:641a9672db08 | 574 | { |
AzureIoTClient | 30:0407b2db334c | 575 | if (sasl_client_io->is_trace_on != 0) |
AzureIoTClient | 6:641a9672db08 | 576 | { |
AzureIoTClient | 6:641a9672db08 | 577 | log_outgoing_frame(sasl_init_value); |
AzureIoTClient | 6:641a9672db08 | 578 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 579 | |
AzureIoTClient | 6:641a9672db08 | 580 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 581 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 582 | |
AzureIoTClient | 6:641a9672db08 | 583 | amqpvalue_destroy(sasl_init_value); |
AzureIoTClient | 6:641a9672db08 | 584 | } |
AzureIoTClient | 6:641a9672db08 | 585 | } |
AzureIoTClient | 6:641a9672db08 | 586 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 587 | |
AzureIoTClient | 6:641a9672db08 | 588 | sasl_init_destroy(sasl_init); |
AzureIoTClient | 6:641a9672db08 | 589 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 590 | |
AzureIoTClient | 6:641a9672db08 | 591 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 592 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 593 | |
Azure.IoT Build | 0:6ae2f7bca550 | 594 | static int send_sasl_response(SASL_CLIENT_IO_INSTANCE* sasl_client_io, SASL_MECHANISM_BYTES sasl_response) |
Azure.IoT Build | 0:6ae2f7bca550 | 595 | { |
AzureIoTClient | 6:641a9672db08 | 596 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 597 | |
AzureIoTClient | 6:641a9672db08 | 598 | SASL_RESPONSE_HANDLE sasl_response_handle; |
AzureIoTClient | 6:641a9672db08 | 599 | amqp_binary response_binary_value; |
AzureIoTClient | 6:641a9672db08 | 600 | |
AzureIoTClient | 6:641a9672db08 | 601 | response_binary_value.bytes = sasl_response.bytes; |
AzureIoTClient | 6:641a9672db08 | 602 | response_binary_value.length = sasl_response.length; |
Azure.IoT Build | 0:6ae2f7bca550 | 603 | |
AzureIoTClient | 6:641a9672db08 | 604 | /* Codes_SRS_SASLCLIENTIO_01_055: [Send the SASL response data as defined by the SASL specification.] */ |
AzureIoTClient | 6:641a9672db08 | 605 | /* Codes_SRS_SASLCLIENTIO_01_056: [A block of opaque data passed to the security mechanism.] */ |
AzureIoTClient | 6:641a9672db08 | 606 | if ((sasl_response_handle = sasl_response_create(response_binary_value)) == NULL) |
AzureIoTClient | 6:641a9672db08 | 607 | { |
AzureIoTClient | 23:1111ee8bcba4 | 608 | LogError("Could not create SASL response"); |
AzureIoTClient | 19:000ab4e6a2c1 | 609 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 610 | } |
AzureIoTClient | 6:641a9672db08 | 611 | else |
AzureIoTClient | 6:641a9672db08 | 612 | { |
AzureIoTClient | 6:641a9672db08 | 613 | AMQP_VALUE sasl_response_value = amqpvalue_create_sasl_response(sasl_response_handle); |
AzureIoTClient | 6:641a9672db08 | 614 | if (sasl_response_value == NULL) |
AzureIoTClient | 6:641a9672db08 | 615 | { |
AzureIoTClient | 23:1111ee8bcba4 | 616 | LogError("Could not create SASL response AMQP value"); |
AzureIoTClient | 19:000ab4e6a2c1 | 617 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 618 | } |
AzureIoTClient | 6:641a9672db08 | 619 | else |
AzureIoTClient | 6:641a9672db08 | 620 | { |
AzureIoTClient | 30:0407b2db334c | 621 | /* Codes_SRS_SASLCLIENTIO_01_070: [When a frame needs to be sent as part of the SASL handshake frame exchange, the send shall be done by calling `sasl_frame_codec_encode_frame`.]*/ |
AzureIoTClient | 6:641a9672db08 | 622 | if (sasl_frame_codec_encode_frame(sasl_client_io->sasl_frame_codec, sasl_response_value, on_bytes_encoded, sasl_client_io) != 0) |
AzureIoTClient | 6:641a9672db08 | 623 | { |
AzureIoTClient | 23:1111ee8bcba4 | 624 | LogError("Could not encode SASL response in the frame"); |
AzureIoTClient | 19:000ab4e6a2c1 | 625 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 626 | } |
AzureIoTClient | 6:641a9672db08 | 627 | else |
AzureIoTClient | 6:641a9672db08 | 628 | { |
AzureIoTClient | 30:0407b2db334c | 629 | if (sasl_client_io->is_trace_on != 0) |
AzureIoTClient | 6:641a9672db08 | 630 | { |
AzureIoTClient | 6:641a9672db08 | 631 | log_outgoing_frame(sasl_response_value); |
AzureIoTClient | 6:641a9672db08 | 632 | } |
AzureIoTClient | 23:1111ee8bcba4 | 633 | |
AzureIoTClient | 6:641a9672db08 | 634 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 635 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 636 | |
AzureIoTClient | 6:641a9672db08 | 637 | amqpvalue_destroy(sasl_response_value); |
AzureIoTClient | 6:641a9672db08 | 638 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 639 | |
AzureIoTClient | 6:641a9672db08 | 640 | sasl_response_destroy(sasl_response_handle); |
AzureIoTClient | 6:641a9672db08 | 641 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 642 | |
AzureIoTClient | 6:641a9672db08 | 643 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 644 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 645 | |
AzureIoTClient | 30:0407b2db334c | 646 | static void on_sasl_frame_received_callback(void* context, AMQP_VALUE sasl_frame) |
Azure.IoT Build | 0:6ae2f7bca550 | 647 | { |
AzureIoTClient | 6:641a9672db08 | 648 | SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance = (SASL_CLIENT_IO_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 649 | |
AzureIoTClient | 6:641a9672db08 | 650 | /* Codes_SRS_SASLCLIENTIO_01_067: [The SASL frame exchange shall be started as soon as the SASL header handshake is done.] */ |
AzureIoTClient | 6:641a9672db08 | 651 | switch (sasl_client_io_instance->io_state) |
AzureIoTClient | 6:641a9672db08 | 652 | { |
AzureIoTClient | 6:641a9672db08 | 653 | default: |
AzureIoTClient | 23:1111ee8bcba4 | 654 | LogError("SASL frame received while in state %d", (int)sasl_client_io_instance->io_state); |
AzureIoTClient | 6:641a9672db08 | 655 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 656 | |
AzureIoTClient | 6:641a9672db08 | 657 | case IO_STATE_OPEN: |
AzureIoTClient | 6:641a9672db08 | 658 | case IO_STATE_OPENING_UNDERLYING_IO: |
AzureIoTClient | 6:641a9672db08 | 659 | case IO_STATE_CLOSING: |
AzureIoTClient | 30:0407b2db334c | 660 | /* Codes_SRS_SASLCLIENTIO_01_117: [If `on_sasl_frame_received_callback` is called when the state of the IO is OPEN then the `on_io_error` callback shall be triggered.]*/ |
AzureIoTClient | 6:641a9672db08 | 661 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 662 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 663 | |
AzureIoTClient | 6:641a9672db08 | 664 | case IO_STATE_SASL_HANDSHAKE: |
AzureIoTClient | 6:641a9672db08 | 665 | if (sasl_client_io_instance->sasl_header_exchange_state != SASL_HEADER_EXCHANGE_HEADER_EXCH) |
AzureIoTClient | 6:641a9672db08 | 666 | { |
AzureIoTClient | 30:0407b2db334c | 667 | /* Codes_SRS_SASLCLIENTIO_01_118: [If `on_sasl_frame_received_callback` is called in the OPENING state but the header exchange has not yet been completed, then the `on_io_error` callback shall be triggered.]*/ |
AzureIoTClient | 6:641a9672db08 | 668 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 669 | } |
AzureIoTClient | 6:641a9672db08 | 670 | else |
AzureIoTClient | 6:641a9672db08 | 671 | { |
AzureIoTClient | 6:641a9672db08 | 672 | AMQP_VALUE descriptor = amqpvalue_get_inplace_descriptor(sasl_frame); |
AzureIoTClient | 6:641a9672db08 | 673 | if (descriptor == NULL) |
AzureIoTClient | 6:641a9672db08 | 674 | { |
AzureIoTClient | 30:0407b2db334c | 675 | /* Codes_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/ |
AzureIoTClient | 23:1111ee8bcba4 | 676 | LogError("Could not obtain SASL frame descriptor"); |
AzureIoTClient | 6:641a9672db08 | 677 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 678 | } |
AzureIoTClient | 6:641a9672db08 | 679 | else |
AzureIoTClient | 6:641a9672db08 | 680 | { |
AzureIoTClient | 30:0407b2db334c | 681 | if (sasl_client_io_instance->is_trace_on != 0) |
AzureIoTClient | 6:641a9672db08 | 682 | { |
AzureIoTClient | 6:641a9672db08 | 683 | log_incoming_frame(sasl_frame); |
AzureIoTClient | 6:641a9672db08 | 684 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 685 | |
AzureIoTClient | 6:641a9672db08 | 686 | /* Codes_SRS_SASLCLIENTIO_01_032: [The peer acting as the SASL server MUST announce supported authentication mechanisms using the sasl-mechanisms frame.] */ |
AzureIoTClient | 6:641a9672db08 | 687 | /* Codes_SRS_SASLCLIENTIO_01_040: [The peer playing the role of the SASL client and the peer playing the role of the SASL server MUST correspond to the TCP client and server respectively.] */ |
AzureIoTClient | 6:641a9672db08 | 688 | /* Codes_SRS_SASLCLIENTIO_01_034: [<-- SASL-MECHANISMS] */ |
AzureIoTClient | 6:641a9672db08 | 689 | if (is_sasl_mechanisms_type_by_descriptor(descriptor)) |
AzureIoTClient | 6:641a9672db08 | 690 | { |
AzureIoTClient | 6:641a9672db08 | 691 | switch (sasl_client_io_instance->sasl_client_negotiation_state) |
AzureIoTClient | 6:641a9672db08 | 692 | { |
AzureIoTClient | 17:923575db8b2d | 693 | default: |
AzureIoTClient | 23:1111ee8bcba4 | 694 | LogError("SASL mechanisms frame received in %s state", ENUM_TO_STRING(SASL_CLIENT_NEGOTIATION_STATE, sasl_client_io_instance->sasl_client_negotiation_state)); |
AzureIoTClient | 17:923575db8b2d | 695 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 17:923575db8b2d | 696 | break; |
AzureIoTClient | 17:923575db8b2d | 697 | |
AzureIoTClient | 6:641a9672db08 | 698 | case SASL_CLIENT_NEGOTIATION_NOT_STARTED: |
AzureIoTClient | 6:641a9672db08 | 699 | { |
AzureIoTClient | 6:641a9672db08 | 700 | SASL_MECHANISMS_HANDLE sasl_mechanisms_handle; |
Azure.IoT Build | 0:6ae2f7bca550 | 701 | |
AzureIoTClient | 6:641a9672db08 | 702 | if (amqpvalue_get_sasl_mechanisms(sasl_frame, &sasl_mechanisms_handle) != 0) |
AzureIoTClient | 6:641a9672db08 | 703 | { |
AzureIoTClient | 30:0407b2db334c | 704 | /* Codes_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/ |
AzureIoTClient | 23:1111ee8bcba4 | 705 | LogError("Could not get SASL mechanisms"); |
AzureIoTClient | 6:641a9672db08 | 706 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 707 | } |
AzureIoTClient | 6:641a9672db08 | 708 | else |
AzureIoTClient | 6:641a9672db08 | 709 | { |
AzureIoTClient | 6:641a9672db08 | 710 | AMQP_VALUE sasl_server_mechanisms; |
AzureIoTClient | 6:641a9672db08 | 711 | uint32_t mechanisms_count; |
Azure.IoT Build | 0:6ae2f7bca550 | 712 | |
AzureIoTClient | 6:641a9672db08 | 713 | if ((sasl_mechanisms_get_sasl_server_mechanisms(sasl_mechanisms_handle, &sasl_server_mechanisms) != 0) || |
AzureIoTClient | 6:641a9672db08 | 714 | (amqpvalue_get_array_item_count(sasl_server_mechanisms, &mechanisms_count) != 0) || |
AzureIoTClient | 6:641a9672db08 | 715 | (mechanisms_count == 0)) |
AzureIoTClient | 6:641a9672db08 | 716 | { |
AzureIoTClient | 6:641a9672db08 | 717 | /* Codes_SRS_SASLCLIENTIO_01_042: [It is invalid for this list to be null or empty.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 718 | LogError("Invalid SASL mechanisms list"); |
AzureIoTClient | 6:641a9672db08 | 719 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 720 | } |
AzureIoTClient | 6:641a9672db08 | 721 | else |
AzureIoTClient | 6:641a9672db08 | 722 | { |
AzureIoTClient | 6:641a9672db08 | 723 | const char* sasl_mechanism_name = saslmechanism_get_mechanism_name(sasl_client_io_instance->sasl_mechanism); |
AzureIoTClient | 6:641a9672db08 | 724 | if (sasl_mechanism_name == NULL) |
AzureIoTClient | 6:641a9672db08 | 725 | { |
AzureIoTClient | 30:0407b2db334c | 726 | /* Codes_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/ |
AzureIoTClient | 23:1111ee8bcba4 | 727 | LogError("Cannot get the mechanism name"); |
AzureIoTClient | 6:641a9672db08 | 728 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 729 | } |
AzureIoTClient | 6:641a9672db08 | 730 | else |
AzureIoTClient | 6:641a9672db08 | 731 | { |
AzureIoTClient | 6:641a9672db08 | 732 | uint32_t i; |
Azure.IoT Build | 0:6ae2f7bca550 | 733 | |
AzureIoTClient | 6:641a9672db08 | 734 | for (i = 0; i < mechanisms_count; i++) |
AzureIoTClient | 6:641a9672db08 | 735 | { |
AzureIoTClient | 6:641a9672db08 | 736 | AMQP_VALUE sasl_server_mechanism; |
AzureIoTClient | 6:641a9672db08 | 737 | sasl_server_mechanism = amqpvalue_get_array_item(sasl_server_mechanisms, i); |
AzureIoTClient | 6:641a9672db08 | 738 | if (sasl_server_mechanism == NULL) |
AzureIoTClient | 6:641a9672db08 | 739 | { |
AzureIoTClient | 23:1111ee8bcba4 | 740 | LogError("Cannot get SASL mechanisms array item for index %u", (unsigned int)i); |
AzureIoTClient | 6:641a9672db08 | 741 | i = mechanisms_count; |
AzureIoTClient | 6:641a9672db08 | 742 | } |
AzureIoTClient | 6:641a9672db08 | 743 | else |
AzureIoTClient | 6:641a9672db08 | 744 | { |
AzureIoTClient | 6:641a9672db08 | 745 | const char* sasl_server_mechanism_name; |
AzureIoTClient | 6:641a9672db08 | 746 | if (amqpvalue_get_symbol(sasl_server_mechanism, &sasl_server_mechanism_name) != 0) |
AzureIoTClient | 6:641a9672db08 | 747 | { |
AzureIoTClient | 23:1111ee8bcba4 | 748 | LogError("Error getting server SASL mechanism from array item"); |
AzureIoTClient | 6:641a9672db08 | 749 | i = mechanisms_count; |
AzureIoTClient | 6:641a9672db08 | 750 | } |
AzureIoTClient | 6:641a9672db08 | 751 | else |
AzureIoTClient | 6:641a9672db08 | 752 | { |
AzureIoTClient | 6:641a9672db08 | 753 | if (strcmp(sasl_mechanism_name, sasl_server_mechanism_name) == 0) |
AzureIoTClient | 6:641a9672db08 | 754 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 755 | amqpvalue_destroy(sasl_server_mechanism); |
Azure.IoT Build | 0:6ae2f7bca550 | 756 | break; |
AzureIoTClient | 6:641a9672db08 | 757 | } |
AzureIoTClient | 6:641a9672db08 | 758 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 759 | |
Azure.IoT Build | 0:6ae2f7bca550 | 760 | amqpvalue_destroy(sasl_server_mechanism); |
Azure.IoT Build | 0:6ae2f7bca550 | 761 | } |
AzureIoTClient | 6:641a9672db08 | 762 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 763 | |
AzureIoTClient | 6:641a9672db08 | 764 | if (i == mechanisms_count) |
AzureIoTClient | 6:641a9672db08 | 765 | { |
AzureIoTClient | 30:0407b2db334c | 766 | /* Codes_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/ |
AzureIoTClient | 23:1111ee8bcba4 | 767 | LogError("Could not find desired SASL mechanism in the list presented by server"); |
AzureIoTClient | 6:641a9672db08 | 768 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 769 | } |
AzureIoTClient | 6:641a9672db08 | 770 | else |
AzureIoTClient | 6:641a9672db08 | 771 | { |
AzureIoTClient | 6:641a9672db08 | 772 | sasl_client_io_instance->sasl_client_negotiation_state = SASL_CLIENT_NEGOTIATION_MECH_RCVD; |
Azure.IoT Build | 0:6ae2f7bca550 | 773 | |
AzureIoTClient | 6:641a9672db08 | 774 | /* Codes_SRS_SASLCLIENTIO_01_035: [SASL-INIT -->] */ |
AzureIoTClient | 6:641a9672db08 | 775 | /* Codes_SRS_SASLCLIENTIO_01_033: [The partner MUST then choose one of the supported mechanisms and initiate a sasl exchange.] */ |
AzureIoTClient | 6:641a9672db08 | 776 | /* Codes_SRS_SASLCLIENTIO_01_054: [Selects the sasl mechanism and provides the initial response if needed.] */ |
AzureIoTClient | 6:641a9672db08 | 777 | if (send_sasl_init(sasl_client_io_instance, sasl_mechanism_name) != 0) |
AzureIoTClient | 6:641a9672db08 | 778 | { |
AzureIoTClient | 30:0407b2db334c | 779 | /* Codes_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/ |
AzureIoTClient | 23:1111ee8bcba4 | 780 | LogError("Could not send SASL init"); |
AzureIoTClient | 6:641a9672db08 | 781 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 782 | } |
AzureIoTClient | 6:641a9672db08 | 783 | else |
AzureIoTClient | 6:641a9672db08 | 784 | { |
AzureIoTClient | 6:641a9672db08 | 785 | sasl_client_io_instance->sasl_client_negotiation_state = SASL_CLIENT_NEGOTIATION_INIT_SENT; |
AzureIoTClient | 6:641a9672db08 | 786 | } |
AzureIoTClient | 6:641a9672db08 | 787 | } |
AzureIoTClient | 6:641a9672db08 | 788 | } |
AzureIoTClient | 6:641a9672db08 | 789 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 790 | |
AzureIoTClient | 6:641a9672db08 | 791 | sasl_mechanisms_destroy(sasl_mechanisms_handle); |
AzureIoTClient | 6:641a9672db08 | 792 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 793 | |
AzureIoTClient | 6:641a9672db08 | 794 | break; |
AzureIoTClient | 6:641a9672db08 | 795 | } |
AzureIoTClient | 6:641a9672db08 | 796 | } |
AzureIoTClient | 6:641a9672db08 | 797 | } |
AzureIoTClient | 6:641a9672db08 | 798 | /* Codes_SRS_SASLCLIENTIO_01_052: [Send the SASL challenge data as defined by the SASL specification.] */ |
AzureIoTClient | 6:641a9672db08 | 799 | /* Codes_SRS_SASLCLIENTIO_01_036: [<-- SASL-CHALLENGE *] */ |
AzureIoTClient | 6:641a9672db08 | 800 | /* Codes_SRS_SASLCLIENTIO_01_039: [the SASL challenge/response step can occur zero or more times depending on the details of the SASL mechanism chosen.] */ |
AzureIoTClient | 6:641a9672db08 | 801 | else if (is_sasl_challenge_type_by_descriptor(descriptor)) |
AzureIoTClient | 6:641a9672db08 | 802 | { |
AzureIoTClient | 6:641a9672db08 | 803 | /* Codes_SRS_SASLCLIENTIO_01_032: [The peer acting as the SASL server MUST announce supported authentication mechanisms using the sasl-mechanisms frame.] */ |
AzureIoTClient | 6:641a9672db08 | 804 | if ((sasl_client_io_instance->sasl_client_negotiation_state != SASL_CLIENT_NEGOTIATION_INIT_SENT) && |
AzureIoTClient | 6:641a9672db08 | 805 | (sasl_client_io_instance->sasl_client_negotiation_state != SASL_CLIENT_NEGOTIATION_RESPONSE_SENT)) |
AzureIoTClient | 6:641a9672db08 | 806 | { |
AzureIoTClient | 23:1111ee8bcba4 | 807 | LogError("SASL challenge received in a bad state: %s", ENUM_TO_STRING(SASL_CLIENT_NEGOTIATION_STATE, sasl_client_io_instance->sasl_client_negotiation_state)); |
AzureIoTClient | 6:641a9672db08 | 808 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 809 | } |
AzureIoTClient | 6:641a9672db08 | 810 | else |
AzureIoTClient | 6:641a9672db08 | 811 | { |
AzureIoTClient | 6:641a9672db08 | 812 | SASL_CHALLENGE_HANDLE sasl_challenge_handle; |
Azure.IoT Build | 0:6ae2f7bca550 | 813 | |
AzureIoTClient | 6:641a9672db08 | 814 | if (amqpvalue_get_sasl_challenge(sasl_frame, &sasl_challenge_handle) != 0) |
AzureIoTClient | 6:641a9672db08 | 815 | { |
AzureIoTClient | 30:0407b2db334c | 816 | /* Codes_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/ |
AzureIoTClient | 23:1111ee8bcba4 | 817 | LogError("Cannot get SASL challenge values"); |
AzureIoTClient | 6:641a9672db08 | 818 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 819 | } |
AzureIoTClient | 6:641a9672db08 | 820 | else |
AzureIoTClient | 6:641a9672db08 | 821 | { |
AzureIoTClient | 6:641a9672db08 | 822 | amqp_binary challenge_binary_value; |
AzureIoTClient | 30:0407b2db334c | 823 | |
AzureIoTClient | 30:0407b2db334c | 824 | challenge_binary_value.bytes = NULL; |
AzureIoTClient | 30:0407b2db334c | 825 | challenge_binary_value.length = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 826 | |
AzureIoTClient | 6:641a9672db08 | 827 | /* Codes_SRS_SASLCLIENTIO_01_053: [Challenge information, a block of opaque binary data passed to the security mechanism.] */ |
AzureIoTClient | 6:641a9672db08 | 828 | if (sasl_challenge_get_challenge(sasl_challenge_handle, &challenge_binary_value) != 0) |
AzureIoTClient | 6:641a9672db08 | 829 | { |
AzureIoTClient | 30:0407b2db334c | 830 | /* Codes_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/ |
AzureIoTClient | 23:1111ee8bcba4 | 831 | LogError("Cannot get SASL challenge binary value"); |
AzureIoTClient | 6:641a9672db08 | 832 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 833 | } |
AzureIoTClient | 6:641a9672db08 | 834 | else |
AzureIoTClient | 6:641a9672db08 | 835 | { |
AzureIoTClient | 6:641a9672db08 | 836 | SASL_MECHANISM_BYTES challenge; |
AzureIoTClient | 30:0407b2db334c | 837 | SASL_MECHANISM_BYTES response_bytes; |
AzureIoTClient | 6:641a9672db08 | 838 | |
AzureIoTClient | 6:641a9672db08 | 839 | challenge.bytes = challenge_binary_value.bytes; |
AzureIoTClient | 6:641a9672db08 | 840 | challenge.length = challenge_binary_value.length; |
AzureIoTClient | 30:0407b2db334c | 841 | response_bytes.bytes = NULL; |
AzureIoTClient | 30:0407b2db334c | 842 | response_bytes.length = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 843 | |
AzureIoTClient | 6:641a9672db08 | 844 | /* Codes_SRS_SASLCLIENTIO_01_057: [The contents of this data are defined by the SASL security mechanism.] */ |
AzureIoTClient | 6:641a9672db08 | 845 | /* Codes_SRS_SASLCLIENTIO_01_037: [SASL-RESPONSE -->] */ |
AzureIoTClient | 23:1111ee8bcba4 | 846 | if (saslmechanism_challenge(sasl_client_io_instance->sasl_mechanism, &challenge, &response_bytes) != 0) |
AzureIoTClient | 6:641a9672db08 | 847 | { |
AzureIoTClient | 30:0407b2db334c | 848 | /* Codes_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/ |
AzureIoTClient | 23:1111ee8bcba4 | 849 | LogError("SASL Challenge failed"); |
AzureIoTClient | 23:1111ee8bcba4 | 850 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 23:1111ee8bcba4 | 851 | } |
AzureIoTClient | 23:1111ee8bcba4 | 852 | else if (send_sasl_response(sasl_client_io_instance, response_bytes) != 0) |
AzureIoTClient | 23:1111ee8bcba4 | 853 | { |
AzureIoTClient | 30:0407b2db334c | 854 | /* Codes_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/ |
AzureIoTClient | 23:1111ee8bcba4 | 855 | LogError("Cannot send SASL reponse"); |
AzureIoTClient | 6:641a9672db08 | 856 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 857 | } |
AzureIoTClient | 6:641a9672db08 | 858 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 859 | |
AzureIoTClient | 6:641a9672db08 | 860 | sasl_challenge_destroy(sasl_challenge_handle); |
AzureIoTClient | 6:641a9672db08 | 861 | } |
AzureIoTClient | 6:641a9672db08 | 862 | } |
AzureIoTClient | 6:641a9672db08 | 863 | } |
AzureIoTClient | 6:641a9672db08 | 864 | /* Codes_SRS_SASLCLIENTIO_01_058: [This frame indicates the outcome of the SASL dialog.] */ |
AzureIoTClient | 6:641a9672db08 | 865 | /* Codes_SRS_SASLCLIENTIO_01_038: [<-- SASL-OUTCOME] */ |
AzureIoTClient | 6:641a9672db08 | 866 | else if (is_sasl_outcome_type_by_descriptor(descriptor)) |
AzureIoTClient | 6:641a9672db08 | 867 | { |
AzureIoTClient | 6:641a9672db08 | 868 | /* Codes_SRS_SASLCLIENTIO_01_032: [The peer acting as the SASL server MUST announce supported authentication mechanisms using the sasl-mechanisms frame.] */ |
AzureIoTClient | 6:641a9672db08 | 869 | if ((sasl_client_io_instance->sasl_client_negotiation_state != SASL_CLIENT_NEGOTIATION_INIT_SENT) && |
AzureIoTClient | 6:641a9672db08 | 870 | (sasl_client_io_instance->sasl_client_negotiation_state != SASL_CLIENT_NEGOTIATION_RESPONSE_SENT)) |
AzureIoTClient | 6:641a9672db08 | 871 | { |
AzureIoTClient | 23:1111ee8bcba4 | 872 | LogError("SASL outcome received in a bad state: %s", ENUM_TO_STRING(SASL_CLIENT_NEGOTIATION_STATE, sasl_client_io_instance->sasl_client_negotiation_state)); |
AzureIoTClient | 6:641a9672db08 | 873 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 874 | } |
AzureIoTClient | 6:641a9672db08 | 875 | else |
AzureIoTClient | 6:641a9672db08 | 876 | { |
AzureIoTClient | 6:641a9672db08 | 877 | SASL_OUTCOME_HANDLE sasl_outcome; |
Azure.IoT Build | 0:6ae2f7bca550 | 878 | |
AzureIoTClient | 6:641a9672db08 | 879 | sasl_client_io_instance->sasl_client_negotiation_state = SASL_CLIENT_NEGOTIATION_OUTCOME_RCVD; |
Azure.IoT Build | 0:6ae2f7bca550 | 880 | |
AzureIoTClient | 6:641a9672db08 | 881 | if (amqpvalue_get_sasl_outcome(sasl_frame, &sasl_outcome) != 0) |
AzureIoTClient | 6:641a9672db08 | 882 | { |
AzureIoTClient | 23:1111ee8bcba4 | 883 | LogError("Cannot get SASL outcome"); |
AzureIoTClient | 6:641a9672db08 | 884 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 885 | } |
AzureIoTClient | 6:641a9672db08 | 886 | else |
AzureIoTClient | 6:641a9672db08 | 887 | { |
AzureIoTClient | 6:641a9672db08 | 888 | sasl_code sasl_code; |
Azure.IoT Build | 0:6ae2f7bca550 | 889 | |
AzureIoTClient | 6:641a9672db08 | 890 | /* Codes_SRS_SASLCLIENTIO_01_060: [A reply-code indicating the outcome of the SASL dialog.] */ |
AzureIoTClient | 6:641a9672db08 | 891 | if (sasl_outcome_get_code(sasl_outcome, &sasl_code) != 0) |
AzureIoTClient | 6:641a9672db08 | 892 | { |
AzureIoTClient | 23:1111ee8bcba4 | 893 | LogError("Cannot get SASL outcome code"); |
AzureIoTClient | 6:641a9672db08 | 894 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 895 | } |
AzureIoTClient | 6:641a9672db08 | 896 | else |
AzureIoTClient | 6:641a9672db08 | 897 | { |
AzureIoTClient | 6:641a9672db08 | 898 | switch (sasl_code) |
AzureIoTClient | 6:641a9672db08 | 899 | { |
AzureIoTClient | 6:641a9672db08 | 900 | default: |
AzureIoTClient | 6:641a9672db08 | 901 | case sasl_code_auth: |
AzureIoTClient | 6:641a9672db08 | 902 | /* Codes_SRS_SASLCLIENTIO_01_063: [1 Connection authentication failed due to an unspecified problem with the supplied credentials.] */ |
AzureIoTClient | 6:641a9672db08 | 903 | case sasl_code_sys: |
AzureIoTClient | 6:641a9672db08 | 904 | /* Codes_SRS_SASLCLIENTIO_01_064: [2 Connection authentication failed due to a system error.] */ |
AzureIoTClient | 6:641a9672db08 | 905 | case sasl_code_sys_perm: |
AzureIoTClient | 6:641a9672db08 | 906 | /* Codes_SRS_SASLCLIENTIO_01_065: [3 Connection authentication failed due to a system error that is unlikely to be corrected without intervention.] */ |
AzureIoTClient | 6:641a9672db08 | 907 | case sasl_code_sys_temp: |
AzureIoTClient | 6:641a9672db08 | 908 | /* Codes_SRS_SASLCLIENTIO_01_066: [4 Connection authentication failed due to a transient system error.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 909 | LogError("SASL handshake failed with code %02X", (unsigned char)sasl_code); |
AzureIoTClient | 6:641a9672db08 | 910 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 911 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 912 | |
AzureIoTClient | 6:641a9672db08 | 913 | case sasl_code_ok: |
AzureIoTClient | 6:641a9672db08 | 914 | /* Codes_SRS_SASLCLIENTIO_01_059: [Upon successful completion of the SASL dialog the security layer has been established] */ |
AzureIoTClient | 6:641a9672db08 | 915 | /* Codes_SRS_SASLCLIENTIO_01_062: [0 Connection authentication succeeded.] */ |
AzureIoTClient | 6:641a9672db08 | 916 | sasl_client_io_instance->io_state = IO_STATE_OPEN; |
AzureIoTClient | 30:0407b2db334c | 917 | |
AzureIoTClient | 30:0407b2db334c | 918 | /* Codes_SRS_SASLCLIENTIO_01_072: [When the SASL handshake is complete, if the handshake is successful, the SASL client IO state shall be switched to `IO_STATE_OPEN` and the `on_io_open_complete` callback shall be called with `IO_OPEN_OK`.]*/ |
AzureIoTClient | 6:641a9672db08 | 919 | indicate_open_complete(sasl_client_io_instance, IO_OPEN_OK); |
AzureIoTClient | 6:641a9672db08 | 920 | break; |
AzureIoTClient | 6:641a9672db08 | 921 | } |
AzureIoTClient | 6:641a9672db08 | 922 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 923 | |
AzureIoTClient | 6:641a9672db08 | 924 | sasl_outcome_destroy(sasl_outcome); |
AzureIoTClient | 6:641a9672db08 | 925 | } |
AzureIoTClient | 6:641a9672db08 | 926 | } |
AzureIoTClient | 6:641a9672db08 | 927 | } |
AzureIoTClient | 6:641a9672db08 | 928 | else |
AzureIoTClient | 6:641a9672db08 | 929 | { |
AzureIoTClient | 6:641a9672db08 | 930 | LogError("Bad SASL frame"); |
AzureIoTClient | 6:641a9672db08 | 931 | } |
AzureIoTClient | 6:641a9672db08 | 932 | } |
AzureIoTClient | 6:641a9672db08 | 933 | } |
AzureIoTClient | 6:641a9672db08 | 934 | break; |
AzureIoTClient | 6:641a9672db08 | 935 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 936 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 937 | |
Azure.IoT Build | 0:6ae2f7bca550 | 938 | static void on_frame_codec_error(void* context) |
Azure.IoT Build | 0:6ae2f7bca550 | 939 | { |
AzureIoTClient | 6:641a9672db08 | 940 | SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance = (SASL_CLIENT_IO_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 941 | |
AzureIoTClient | 30:0407b2db334c | 942 | /* Codes_SRS_SASLCLIENTIO_01_122: [When `on_frame_codec_error` is called while in the OPENING state the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/ |
AzureIoTClient | 30:0407b2db334c | 943 | /* Codes_SRS_SASLCLIENTIO_01_143: [ When `on_frame_codec_error` is called while in the OPEN state the `on_io_error` callback shall be triggered. ]*/ |
AzureIoTClient | 30:0407b2db334c | 944 | /* Codes_SRS_SASLCLIENTIO_01_123: [When `on_frame_codec_error` is called in the ERROR state nothing shall be done.]*/ |
AzureIoTClient | 23:1111ee8bcba4 | 945 | LogError("Error encoding frame (on_frame_codec_error)"); |
AzureIoTClient | 6:641a9672db08 | 946 | handle_error(sasl_client_io_instance); |
Azure.IoT Build | 0:6ae2f7bca550 | 947 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 948 | |
Azure.IoT Build | 0:6ae2f7bca550 | 949 | static void on_sasl_frame_codec_error(void* context) |
Azure.IoT Build | 0:6ae2f7bca550 | 950 | { |
AzureIoTClient | 6:641a9672db08 | 951 | SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance = (SASL_CLIENT_IO_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 952 | |
AzureIoTClient | 30:0407b2db334c | 953 | /* Codes_SRS_SASLCLIENTIO_01_141: [ When `on_sasl_frame_codec_error` is called while in the OPENING state the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`. ]*/ |
AzureIoTClient | 30:0407b2db334c | 954 | /* Codes_SRS_SASLCLIENTIO_01_144: [ When `on_sasl_frame_codec_error` is called while OPEN state the `on_io_error` callback shall be triggered. ]*/ |
AzureIoTClient | 30:0407b2db334c | 955 | /* Codes_SRS_SASLCLIENTIO_01_142: [ When `on_sasl_frame_codec_error` is called in the ERROR state nothing shall be done. ]*/ |
AzureIoTClient | 23:1111ee8bcba4 | 956 | LogError("Error encoding SASL frame (on_sasl_frame_codec_error)"); |
AzureIoTClient | 6:641a9672db08 | 957 | handle_error(sasl_client_io_instance); |
Azure.IoT Build | 0:6ae2f7bca550 | 958 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 959 | |
Azure.IoT Build | 5:ae49385aff34 | 960 | CONCRETE_IO_HANDLE saslclientio_create(void* io_create_parameters) |
Azure.IoT Build | 0:6ae2f7bca550 | 961 | { |
AzureIoTClient | 23:1111ee8bcba4 | 962 | SASLCLIENTIO_CONFIG* sasl_client_io_config = (SASLCLIENTIO_CONFIG*)io_create_parameters; |
AzureIoTClient | 6:641a9672db08 | 963 | SASL_CLIENT_IO_INSTANCE* result; |
Azure.IoT Build | 0:6ae2f7bca550 | 964 | |
AzureIoTClient | 30:0407b2db334c | 965 | /* Codes_SRS_SASLCLIENTIO_01_005: [If `io_create_parameters` is NULL, `saslclientio_create` shall fail and return NULL.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 966 | if (sasl_client_io_config == NULL) |
AzureIoTClient | 23:1111ee8bcba4 | 967 | { |
AzureIoTClient | 23:1111ee8bcba4 | 968 | LogError("NULL io_create_parameters"); |
AzureIoTClient | 23:1111ee8bcba4 | 969 | result = NULL; |
AzureIoTClient | 23:1111ee8bcba4 | 970 | } |
AzureIoTClient | 30:0407b2db334c | 971 | /* Codes_SRS_SASLCLIENTIO_01_092: [If any of the `sasl_mechanism` or `underlying_io` members of the configuration structure are NULL, `saslclientio_create` shall fail and return NULL.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 972 | else if ((sasl_client_io_config->underlying_io == NULL) || |
AzureIoTClient | 6:641a9672db08 | 973 | (sasl_client_io_config->sasl_mechanism == NULL)) |
AzureIoTClient | 6:641a9672db08 | 974 | { |
AzureIoTClient | 23:1111ee8bcba4 | 975 | LogError("Bad parameters: underlying_io = %p, sasl_mechanism = %p", |
AzureIoTClient | 23:1111ee8bcba4 | 976 | sasl_client_io_config->underlying_io, sasl_client_io_config->sasl_mechanism); |
AzureIoTClient | 6:641a9672db08 | 977 | result = NULL; |
AzureIoTClient | 6:641a9672db08 | 978 | } |
AzureIoTClient | 6:641a9672db08 | 979 | else |
AzureIoTClient | 6:641a9672db08 | 980 | { |
AzureIoTClient | 23:1111ee8bcba4 | 981 | result = (SASL_CLIENT_IO_INSTANCE*)malloc(sizeof(SASL_CLIENT_IO_INSTANCE)); |
AzureIoTClient | 23:1111ee8bcba4 | 982 | if (result == NULL) |
AzureIoTClient | 23:1111ee8bcba4 | 983 | { |
AzureIoTClient | 30:0407b2db334c | 984 | /* Codes_SRS_SASLCLIENTIO_01_006: [If memory cannot be allocated for the new instance, `saslclientio_create` shall fail and return NULL.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 985 | LogError("Cannot allocate sasl client IO instance"); |
AzureIoTClient | 23:1111ee8bcba4 | 986 | } |
AzureIoTClient | 23:1111ee8bcba4 | 987 | else |
AzureIoTClient | 6:641a9672db08 | 988 | { |
AzureIoTClient | 6:641a9672db08 | 989 | result->underlying_io = sasl_client_io_config->underlying_io; |
AzureIoTClient | 30:0407b2db334c | 990 | /* Codes_SRS_SASLCLIENTIO_01_089: [`saslclientio_create` shall create a frame codec to be used for encoding/decoding frames by calling `frame_codec_create` and passing `on_frame_codec_error` and a context as arguments.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 991 | result->frame_codec = frame_codec_create(on_frame_codec_error, result); |
AzureIoTClient | 23:1111ee8bcba4 | 992 | if (result->frame_codec == NULL) |
AzureIoTClient | 6:641a9672db08 | 993 | { |
AzureIoTClient | 30:0407b2db334c | 994 | /* Codes_SRS_SASLCLIENTIO_01_090: [If `frame_codec_create` fails, then `saslclientio_create` shall fail and return NULL.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 995 | LogError("frame_codec_create failed"); |
AzureIoTClient | 21:f9c433d8e6ca | 996 | free(result); |
AzureIoTClient | 6:641a9672db08 | 997 | result = NULL; |
AzureIoTClient | 6:641a9672db08 | 998 | } |
AzureIoTClient | 6:641a9672db08 | 999 | else |
AzureIoTClient | 6:641a9672db08 | 1000 | { |
AzureIoTClient | 30:0407b2db334c | 1001 | /* Codes_SRS_SASLCLIENTIO_01_084: [`saslclientio_create` shall create a SASL frame codec to be used for SASL frame encoding/decoding by calling `sasl_frame_codec_create` and passing the just created frame codec as argument.] */ |
AzureIoTClient | 30:0407b2db334c | 1002 | result->sasl_frame_codec = sasl_frame_codec_create(result->frame_codec, on_sasl_frame_received_callback, on_sasl_frame_codec_error, result); |
AzureIoTClient | 23:1111ee8bcba4 | 1003 | if (result->sasl_frame_codec == NULL) |
AzureIoTClient | 6:641a9672db08 | 1004 | { |
AzureIoTClient | 30:0407b2db334c | 1005 | /* Codes_SRS_SASLCLIENTIO_01_085: [If `sasl_frame_codec_create` fails, then `saslclientio_create` shall fail and return NULL.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 1006 | LogError("sasl_frame_codec_create failed"); |
AzureIoTClient | 23:1111ee8bcba4 | 1007 | frame_codec_destroy(result->frame_codec); |
AzureIoTClient | 21:f9c433d8e6ca | 1008 | free(result); |
AzureIoTClient | 6:641a9672db08 | 1009 | result = NULL; |
AzureIoTClient | 6:641a9672db08 | 1010 | } |
AzureIoTClient | 6:641a9672db08 | 1011 | else |
AzureIoTClient | 6:641a9672db08 | 1012 | { |
AzureIoTClient | 30:0407b2db334c | 1013 | /* Codes_SRS_SASLCLIENTIO_01_004: [`saslclientio_create` shall return on success a non-NULL handle to a new SASL client IO instance.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 1014 | result->on_bytes_received = NULL; |
AzureIoTClient | 23:1111ee8bcba4 | 1015 | result->on_io_open_complete = NULL; |
AzureIoTClient | 23:1111ee8bcba4 | 1016 | result->on_io_error = NULL; |
AzureIoTClient | 23:1111ee8bcba4 | 1017 | result->on_io_close_complete = NULL; |
AzureIoTClient | 23:1111ee8bcba4 | 1018 | result->on_bytes_received_context = NULL; |
AzureIoTClient | 23:1111ee8bcba4 | 1019 | result->on_io_open_complete_context = NULL; |
AzureIoTClient | 23:1111ee8bcba4 | 1020 | result->on_io_close_complete_context = NULL; |
AzureIoTClient | 23:1111ee8bcba4 | 1021 | result->on_io_error_context = NULL; |
AzureIoTClient | 23:1111ee8bcba4 | 1022 | result->sasl_mechanism = sasl_client_io_config->sasl_mechanism; |
Azure.IoT Build | 0:6ae2f7bca550 | 1023 | |
AzureIoTClient | 23:1111ee8bcba4 | 1024 | result->io_state = IO_STATE_NOT_OPEN; |
AzureIoTClient | 6:641a9672db08 | 1025 | } |
AzureIoTClient | 6:641a9672db08 | 1026 | } |
AzureIoTClient | 6:641a9672db08 | 1027 | } |
AzureIoTClient | 6:641a9672db08 | 1028 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1029 | |
AzureIoTClient | 6:641a9672db08 | 1030 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1031 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1032 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1033 | void saslclientio_destroy(CONCRETE_IO_HANDLE sasl_client_io) |
Azure.IoT Build | 0:6ae2f7bca550 | 1034 | { |
AzureIoTClient | 23:1111ee8bcba4 | 1035 | if (sasl_client_io == NULL) |
AzureIoTClient | 23:1111ee8bcba4 | 1036 | { |
AzureIoTClient | 30:0407b2db334c | 1037 | /* Codes_SRS_SASLCLIENTIO_01_008: [If the argument `sasl_client_io` is NULL, `saslclientio_destroy` shall do nothing.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 1038 | LogError("NULL sasl_client_io"); |
AzureIoTClient | 23:1111ee8bcba4 | 1039 | } |
AzureIoTClient | 23:1111ee8bcba4 | 1040 | else |
AzureIoTClient | 6:641a9672db08 | 1041 | { |
AzureIoTClient | 6:641a9672db08 | 1042 | SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance = (SASL_CLIENT_IO_INSTANCE*)sasl_client_io; |
Azure.IoT Build | 0:6ae2f7bca550 | 1043 | |
AzureIoTClient | 30:0407b2db334c | 1044 | /* Codes_SRS_SASLCLIENTIO_01_007: [`saslclientio_destroy` shall free all resources associated with the SASL client IO handle.] */ |
AzureIoTClient | 30:0407b2db334c | 1045 | /* Codes_SRS_SASLCLIENTIO_01_086: [`saslclientio_destroy` shall destroy the SASL frame codec created in `saslclientio_create` by calling `sasl_frame_codec_destroy`.] */ |
AzureIoTClient | 6:641a9672db08 | 1046 | sasl_frame_codec_destroy(sasl_client_io_instance->sasl_frame_codec); |
Azure.IoT Build | 0:6ae2f7bca550 | 1047 | |
AzureIoTClient | 30:0407b2db334c | 1048 | /* Codes_SRS_SASLCLIENTIO_01_091: [`saslclientio_destroy` shall destroy the frame codec created in `saslclientio_create` by calling `frame_codec_destroy`.] */ |
AzureIoTClient | 6:641a9672db08 | 1049 | frame_codec_destroy(sasl_client_io_instance->frame_codec); |
AzureIoTClient | 21:f9c433d8e6ca | 1050 | free(sasl_client_io); |
AzureIoTClient | 6:641a9672db08 | 1051 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1052 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1053 | |
AzureIoTClient | 30:0407b2db334c | 1054 | int saslclientio_open_async(CONCRETE_IO_HANDLE sasl_client_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:6ae2f7bca550 | 1055 | { |
AzureIoTClient | 6:641a9672db08 | 1056 | int result = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 1057 | |
AzureIoTClient | 6:641a9672db08 | 1058 | if ((sasl_client_io == NULL) || |
AzureIoTClient | 30:0407b2db334c | 1059 | (on_io_open_complete == NULL) || |
AzureIoTClient | 30:0407b2db334c | 1060 | (on_bytes_received == NULL) || |
AzureIoTClient | 30:0407b2db334c | 1061 | (on_io_error == NULL)) |
AzureIoTClient | 6:641a9672db08 | 1062 | { |
AzureIoTClient | 30:0407b2db334c | 1063 | /* Codes_SRS_SASLCLIENTIO_01_011: [If any of the `sasl_client_io`, `on_io_open_complete`, `on_bytes_received` or `on_io_error` arguments is NULL, `saslclientio_open` shall fail and return a non-zero value.] */ |
AzureIoTClient | 30:0407b2db334c | 1064 | LogError("Bad arguments: sasl_client_io = %p, on_io_open_complete = %p, on_bytes_received = %p, on_io_error = %p", |
AzureIoTClient | 30:0407b2db334c | 1065 | sasl_client_io, on_io_open_complete, on_bytes_received, on_io_error); |
AzureIoTClient | 19:000ab4e6a2c1 | 1066 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 1067 | } |
AzureIoTClient | 6:641a9672db08 | 1068 | else |
AzureIoTClient | 6:641a9672db08 | 1069 | { |
AzureIoTClient | 6:641a9672db08 | 1070 | SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance = (SASL_CLIENT_IO_INSTANCE*)sasl_client_io; |
Azure.IoT Build | 0:6ae2f7bca550 | 1071 | |
AzureIoTClient | 6:641a9672db08 | 1072 | if (sasl_client_io_instance->io_state != IO_STATE_NOT_OPEN) |
AzureIoTClient | 6:641a9672db08 | 1073 | { |
AzureIoTClient | 23:1111ee8bcba4 | 1074 | LogError("Open called while already OPEN"); |
AzureIoTClient | 19:000ab4e6a2c1 | 1075 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 1076 | } |
AzureIoTClient | 6:641a9672db08 | 1077 | else |
AzureIoTClient | 6:641a9672db08 | 1078 | { |
AzureIoTClient | 6:641a9672db08 | 1079 | sasl_client_io_instance->on_bytes_received = on_bytes_received; |
AzureIoTClient | 6:641a9672db08 | 1080 | sasl_client_io_instance->on_io_open_complete = on_io_open_complete; |
AzureIoTClient | 6:641a9672db08 | 1081 | sasl_client_io_instance->on_io_error = on_io_error; |
AzureIoTClient | 6:641a9672db08 | 1082 | sasl_client_io_instance->on_bytes_received_context = on_bytes_received_context; |
Azure.IoT Build | 0:6ae2f7bca550 | 1083 | sasl_client_io_instance->on_io_open_complete_context = on_io_open_complete_context; |
Azure.IoT Build | 0:6ae2f7bca550 | 1084 | sasl_client_io_instance->on_io_error_context = on_io_error_context; |
AzureIoTClient | 6:641a9672db08 | 1085 | sasl_client_io_instance->sasl_header_exchange_state = SASL_HEADER_EXCHANGE_IDLE; |
AzureIoTClient | 6:641a9672db08 | 1086 | sasl_client_io_instance->sasl_client_negotiation_state = SASL_CLIENT_NEGOTIATION_NOT_STARTED; |
AzureIoTClient | 6:641a9672db08 | 1087 | sasl_client_io_instance->header_bytes_received = 0; |
AzureIoTClient | 6:641a9672db08 | 1088 | sasl_client_io_instance->io_state = IO_STATE_OPENING_UNDERLYING_IO; |
AzureIoTClient | 6:641a9672db08 | 1089 | sasl_client_io_instance->is_trace_on = 0; |
AzureIoTClient | 30:0407b2db334c | 1090 | sasl_client_io_instance->is_trace_on_set = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 1091 | |
AzureIoTClient | 30:0407b2db334c | 1092 | /* Codes_SRS_SASLCLIENTIO_01_009: [`saslclientio_open` shall call `xio_open` on the `underlying_io` passed to `saslclientio_create`.] */ |
AzureIoTClient | 30:0407b2db334c | 1093 | /* Codes_SRS_SASLCLIENTIO_01_013: [`saslclientio_open_async` shall pass to `xio_open` the `on_underlying_io_open_complete` as `on_io_open_complete` argument, `on_underlying_io_bytes_received` as `on_bytes_received` argument and `on_underlying_io_error` as `on_io_error` argument.] */ |
AzureIoTClient | 6:641a9672db08 | 1094 | if (xio_open(sasl_client_io_instance->underlying_io, on_underlying_io_open_complete, sasl_client_io_instance, on_underlying_io_bytes_received, sasl_client_io_instance, on_underlying_io_error, sasl_client_io_instance) != 0) |
AzureIoTClient | 6:641a9672db08 | 1095 | { |
AzureIoTClient | 30:0407b2db334c | 1096 | /* Codes_SRS_SASLCLIENTIO_01_012: [If the open of the `underlying_io` fails, `saslclientio_open_async` shall fail and return non-zero value.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 1097 | LogError("xio_open failed"); |
AzureIoTClient | 19:000ab4e6a2c1 | 1098 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 1099 | } |
AzureIoTClient | 6:641a9672db08 | 1100 | else |
AzureIoTClient | 6:641a9672db08 | 1101 | { |
AzureIoTClient | 30:0407b2db334c | 1102 | /* Codes_SRS_SASLCLIENTIO_01_010: [On success, `saslclientio_open_async` shall return 0.]*/ |
AzureIoTClient | 6:641a9672db08 | 1103 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 1104 | } |
AzureIoTClient | 6:641a9672db08 | 1105 | } |
AzureIoTClient | 6:641a9672db08 | 1106 | } |
AzureIoTClient | 43:4c1e4e94cdd3 | 1107 | |
AzureIoTClient | 6:641a9672db08 | 1108 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1109 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1110 | |
AzureIoTClient | 30:0407b2db334c | 1111 | int saslclientio_close_async(CONCRETE_IO_HANDLE sasl_client_io, ON_IO_CLOSE_COMPLETE on_io_close_complete, void* on_io_close_complete_context) |
Azure.IoT Build | 0:6ae2f7bca550 | 1112 | { |
AzureIoTClient | 6:641a9672db08 | 1113 | int result = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 1114 | |
AzureIoTClient | 30:0407b2db334c | 1115 | /* Codes_SRS_SASLCLIENTIO_01_017: [If `sasl_client_io` is NULL, `saslclientio_close_async` shall fail and return a non-zero value.] */ |
AzureIoTClient | 6:641a9672db08 | 1116 | if (sasl_client_io == NULL) |
AzureIoTClient | 6:641a9672db08 | 1117 | { |
AzureIoTClient | 23:1111ee8bcba4 | 1118 | LogError("NULL saslclientio_close"); |
AzureIoTClient | 19:000ab4e6a2c1 | 1119 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 1120 | } |
AzureIoTClient | 6:641a9672db08 | 1121 | else |
AzureIoTClient | 6:641a9672db08 | 1122 | { |
AzureIoTClient | 6:641a9672db08 | 1123 | SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance = (SASL_CLIENT_IO_INSTANCE*)sasl_client_io; |
Azure.IoT Build | 0:6ae2f7bca550 | 1124 | |
AzureIoTClient | 30:0407b2db334c | 1125 | /* Codes_SRS_SASLCLIENTIO_01_098: [`saslclientio_close_async` shall only perform the close if the state is OPEN, OPENING or ERROR.] */ |
AzureIoTClient | 6:641a9672db08 | 1126 | if ((sasl_client_io_instance->io_state == IO_STATE_NOT_OPEN) || |
AzureIoTClient | 6:641a9672db08 | 1127 | (sasl_client_io_instance->io_state == IO_STATE_CLOSING)) |
AzureIoTClient | 6:641a9672db08 | 1128 | { |
AzureIoTClient | 30:0407b2db334c | 1129 | /* Codes_SRS_SASLCLIENTIO_01_097: [If `saslclientio_close_async` is called when the IO is in the `IO_STATE_NOT_OPEN` state, `saslclientio_close_async` shall fail and return a non zero value.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 1130 | LogError("saslclientio_close called while not open"); |
AzureIoTClient | 19:000ab4e6a2c1 | 1131 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 1132 | } |
AzureIoTClient | 6:641a9672db08 | 1133 | else |
AzureIoTClient | 6:641a9672db08 | 1134 | { |
AzureIoTClient | 6:641a9672db08 | 1135 | sasl_client_io_instance->io_state = IO_STATE_CLOSING; |
Azure.IoT Build | 0:6ae2f7bca550 | 1136 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1137 | sasl_client_io_instance->on_io_close_complete = on_io_close_complete; |
Azure.IoT Build | 0:6ae2f7bca550 | 1138 | sasl_client_io_instance->on_io_close_complete_context = on_io_close_complete_context; |
Azure.IoT Build | 0:6ae2f7bca550 | 1139 | |
AzureIoTClient | 30:0407b2db334c | 1140 | /* Codes_SRS_SASLCLIENTIO_01_015: [`saslclientio_close_async` shall close the underlying io handle passed in `saslclientio_create` by calling `xio_close`.] */ |
AzureIoTClient | 6:641a9672db08 | 1141 | if (xio_close(sasl_client_io_instance->underlying_io, on_underlying_io_close_complete, sasl_client_io_instance) != 0) |
AzureIoTClient | 6:641a9672db08 | 1142 | { |
AzureIoTClient | 30:0407b2db334c | 1143 | /* Codes_SRS_SASLCLIENTIO_01_018: [If `xio_close` fails, then `saslclientio_close_async` shall return a non-zero value.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 1144 | LogError("xio_close failed"); |
AzureIoTClient | 19:000ab4e6a2c1 | 1145 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 1146 | } |
AzureIoTClient | 6:641a9672db08 | 1147 | else |
AzureIoTClient | 6:641a9672db08 | 1148 | { |
AzureIoTClient | 30:0407b2db334c | 1149 | /* Codes_SRS_SASLCLIENTIO_01_016: [On success, `saslclientio_close_async` shall return 0.] */ |
AzureIoTClient | 6:641a9672db08 | 1150 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 1151 | } |
AzureIoTClient | 6:641a9672db08 | 1152 | } |
AzureIoTClient | 6:641a9672db08 | 1153 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1154 | |
AzureIoTClient | 6:641a9672db08 | 1155 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1156 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1157 | |
AzureIoTClient | 30:0407b2db334c | 1158 | int saslclientio_send_async(CONCRETE_IO_HANDLE sasl_client_io, const void* buffer, size_t size, ON_SEND_COMPLETE on_send_complete, void* callback_context) |
Azure.IoT Build | 0:6ae2f7bca550 | 1159 | { |
AzureIoTClient | 6:641a9672db08 | 1160 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1161 | |
AzureIoTClient | 30:0407b2db334c | 1162 | /* Codes_SRS_SASLCLIENTIO_01_022: [If the `sasl_client_io` or `buffer` argument is NULL, `saslclientio_send_async` shall fail and return a non-zero value.]*/ |
AzureIoTClient | 30:0407b2db334c | 1163 | /* Codes_SRS_SASLCLIENTIO_01_127: [ `on_send_complete` shall be allowed to be NULL. ]*/ |
AzureIoTClient | 6:641a9672db08 | 1164 | if ((sasl_client_io == NULL) || |
AzureIoTClient | 6:641a9672db08 | 1165 | (buffer == NULL) || |
AzureIoTClient | 30:0407b2db334c | 1166 | /* Codes_SRS_SASLCLIENTIO_01_023: [If `size` is 0, `saslclientio_send_async` shall fail and return a non-zero value.]*/ |
AzureIoTClient | 6:641a9672db08 | 1167 | (size == 0)) |
AzureIoTClient | 6:641a9672db08 | 1168 | { |
AzureIoTClient | 6:641a9672db08 | 1169 | /* Invalid arguments */ |
AzureIoTClient | 23:1111ee8bcba4 | 1170 | LogError("Bad arguments: sasl_client_io = %p, buffer = %p, size = %u", |
AzureIoTClient | 23:1111ee8bcba4 | 1171 | sasl_client_io, buffer, (unsigned int)size); |
AzureIoTClient | 19:000ab4e6a2c1 | 1172 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 1173 | } |
AzureIoTClient | 6:641a9672db08 | 1174 | else |
AzureIoTClient | 6:641a9672db08 | 1175 | { |
AzureIoTClient | 6:641a9672db08 | 1176 | SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance = (SASL_CLIENT_IO_INSTANCE*)sasl_client_io; |
Azure.IoT Build | 0:6ae2f7bca550 | 1177 | |
AzureIoTClient | 30:0407b2db334c | 1178 | /* Codes_SRS_SASLCLIENTIO_01_019: [If `saslclientio_send_async` is called while the SASL client IO state is not `IO_STATE_OPEN`, `saslclientio_send_async` shall fail and return a non-zero value.]*/ |
AzureIoTClient | 6:641a9672db08 | 1179 | if (sasl_client_io_instance->io_state != IO_STATE_OPEN) |
AzureIoTClient | 6:641a9672db08 | 1180 | { |
AzureIoTClient | 23:1111ee8bcba4 | 1181 | LogError("send called while not open"); |
AzureIoTClient | 19:000ab4e6a2c1 | 1182 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 1183 | } |
AzureIoTClient | 6:641a9672db08 | 1184 | else |
AzureIoTClient | 6:641a9672db08 | 1185 | { |
AzureIoTClient | 30:0407b2db334c | 1186 | /* Codes_SRS_SASLCLIENTIO_01_020: [If the SASL client IO state is `IO_STATE_OPEN`, `saslclientio_send_async` shall call `xio_send` on the `underlying_io` passed to `saslclientio_create`, while passing as arguments the `buffer`,`size`, `on_send_complete` and `callback_context`.]*/ |
AzureIoTClient | 6:641a9672db08 | 1187 | if (xio_send(sasl_client_io_instance->underlying_io, buffer, size, on_send_complete, callback_context) != 0) |
AzureIoTClient | 6:641a9672db08 | 1188 | { |
AzureIoTClient | 30:0407b2db334c | 1189 | /* Codes_SRS_SASLCLIENTIO_01_024: [If the call to `xio_send` fails, then `saslclientio_send_async` shall fail and return a non-zero value.]*/ |
AzureIoTClient | 23:1111ee8bcba4 | 1190 | LogError("xio_send failed"); |
AzureIoTClient | 19:000ab4e6a2c1 | 1191 | result = __FAILURE__; |
AzureIoTClient | 6:641a9672db08 | 1192 | } |
AzureIoTClient | 6:641a9672db08 | 1193 | else |
AzureIoTClient | 6:641a9672db08 | 1194 | { |
AzureIoTClient | 30:0407b2db334c | 1195 | /* Codes_SRS_SASLCLIENTIO_01_021: [On success, `saslclientio_send_async` shall return 0.]*/ |
AzureIoTClient | 6:641a9672db08 | 1196 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 1197 | } |
AzureIoTClient | 6:641a9672db08 | 1198 | } |
AzureIoTClient | 6:641a9672db08 | 1199 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1200 | |
AzureIoTClient | 6:641a9672db08 | 1201 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1202 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1203 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1204 | void saslclientio_dowork(CONCRETE_IO_HANDLE sasl_client_io) |
Azure.IoT Build | 0:6ae2f7bca550 | 1205 | { |
AzureIoTClient | 30:0407b2db334c | 1206 | /* Codes_SRS_SASLCLIENTIO_01_026: [If the `sasl_client_io` argument is NULL, `saslclientio_dowork` shall do nothing.]*/ |
AzureIoTClient | 23:1111ee8bcba4 | 1207 | if (sasl_client_io == NULL) |
AzureIoTClient | 23:1111ee8bcba4 | 1208 | { |
AzureIoTClient | 23:1111ee8bcba4 | 1209 | LogError("NULL sasl_client_io"); |
AzureIoTClient | 23:1111ee8bcba4 | 1210 | } |
AzureIoTClient | 23:1111ee8bcba4 | 1211 | else |
AzureIoTClient | 6:641a9672db08 | 1212 | { |
AzureIoTClient | 6:641a9672db08 | 1213 | SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance = (SASL_CLIENT_IO_INSTANCE*)sasl_client_io; |
Azure.IoT Build | 0:6ae2f7bca550 | 1214 | |
AzureIoTClient | 30:0407b2db334c | 1215 | /* Codes_SRS_SASLCLIENTIO_01_099: [If the state of the IO is `IO_NOT_OPEN`, `saslclientio_dowork` shall make no calls to the underlying IO.]*/ |
AzureIoTClient | 6:641a9672db08 | 1216 | if (sasl_client_io_instance->io_state != IO_STATE_NOT_OPEN) |
AzureIoTClient | 6:641a9672db08 | 1217 | { |
AzureIoTClient | 30:0407b2db334c | 1218 | /* Codes_SRS_SASLCLIENTIO_01_025: [`saslclientio_dowork` shall call the `xio_dowork` on the `underlying_io` passed in `saslclientio_create`.]*/ |
AzureIoTClient | 6:641a9672db08 | 1219 | xio_dowork(sasl_client_io_instance->underlying_io); |
AzureIoTClient | 6:641a9672db08 | 1220 | } |
AzureIoTClient | 6:641a9672db08 | 1221 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1222 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1223 | |
AzureIoTClient | 30:0407b2db334c | 1224 | int saslclientio_setoption(CONCRETE_IO_HANDLE sasl_client_io, const char* option_name, const void* value) |
Azure.IoT Build | 0:6ae2f7bca550 | 1225 | { |
AzureIoTClient | 1:eab586236bfe | 1226 | int result; |
AzureIoTClient | 1:eab586236bfe | 1227 | |
AzureIoTClient | 30:0407b2db334c | 1228 | if ((sasl_client_io == NULL) || |
AzureIoTClient | 30:0407b2db334c | 1229 | (option_name == NULL)) |
AzureIoTClient | 1:eab586236bfe | 1230 | { |
AzureIoTClient | 30:0407b2db334c | 1231 | /* Codes_SRS_SASLCLIENTIO_01_130: [ If `sasl_client_io` or `option_name` is NULL, `saslclientio_setoption` shall fail and return a non-zero value. ]*/ |
AzureIoTClient | 30:0407b2db334c | 1232 | LogError("Bad arguments: sasl_client_io = %p, option_name = %p", |
AzureIoTClient | 30:0407b2db334c | 1233 | sasl_client_io, option_name); |
AzureIoTClient | 19:000ab4e6a2c1 | 1234 | result = __FAILURE__; |
AzureIoTClient | 1:eab586236bfe | 1235 | } |
AzureIoTClient | 1:eab586236bfe | 1236 | else |
AzureIoTClient | 1:eab586236bfe | 1237 | { |
AzureIoTClient | 1:eab586236bfe | 1238 | SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance = (SASL_CLIENT_IO_INSTANCE*)sasl_client_io; |
AzureIoTClient | 1:eab586236bfe | 1239 | |
AzureIoTClient | 1:eab586236bfe | 1240 | if (sasl_client_io_instance->underlying_io == NULL) |
AzureIoTClient | 1:eab586236bfe | 1241 | { |
AzureIoTClient | 23:1111ee8bcba4 | 1242 | LogError("NULL underlying_io"); |
AzureIoTClient | 19:000ab4e6a2c1 | 1243 | result = __FAILURE__; |
AzureIoTClient | 1:eab586236bfe | 1244 | } |
AzureIoTClient | 30:0407b2db334c | 1245 | /* Codes_SRS_SASLCLIENTIO_01_131: [ SASL client IO shall handle the following options: ]*/ |
AzureIoTClient | 30:0407b2db334c | 1246 | /* Codes_SRS_SASLCLIENTIO_01_132: [ - logtrace - bool. ]*/ |
AzureIoTClient | 30:0407b2db334c | 1247 | else if (strcmp("logtrace", option_name) == 0) |
AzureIoTClient | 6:641a9672db08 | 1248 | { |
AzureIoTClient | 6:641a9672db08 | 1249 | sasl_client_io_instance->is_trace_on = *((bool*)value) == true ? 1 : 0; |
AzureIoTClient | 30:0407b2db334c | 1250 | sasl_client_io_instance->is_trace_on_set = 1; |
AzureIoTClient | 30:0407b2db334c | 1251 | |
AzureIoTClient | 30:0407b2db334c | 1252 | /* Codes_SRS_SASLCLIENTIO_01_128: [ On success, `saslclientio_setoption` shall return 0. ]*/ |
AzureIoTClient | 6:641a9672db08 | 1253 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 1254 | } |
AzureIoTClient | 1:eab586236bfe | 1255 | else |
AzureIoTClient | 1:eab586236bfe | 1256 | { |
AzureIoTClient | 30:0407b2db334c | 1257 | /* Codes_SRS_SASLCLIENTIO_03_001: [`saslclientio_setoption` shall forward all unhandled options to underlying io by calling `xio_setoption`.]*/ |
AzureIoTClient | 30:0407b2db334c | 1258 | if (xio_setoption(sasl_client_io_instance->underlying_io, option_name, value) != 0) |
AzureIoTClient | 30:0407b2db334c | 1259 | { |
AzureIoTClient | 30:0407b2db334c | 1260 | LogError("Error executing xio_setoption"); |
AzureIoTClient | 30:0407b2db334c | 1261 | result = __FAILURE__; |
AzureIoTClient | 30:0407b2db334c | 1262 | } |
AzureIoTClient | 30:0407b2db334c | 1263 | else |
AzureIoTClient | 30:0407b2db334c | 1264 | { |
AzureIoTClient | 30:0407b2db334c | 1265 | /* Codes_SRS_SASLCLIENTIO_01_128: [ On success, `saslclientio_setoption` shall return 0. ]*/ |
AzureIoTClient | 30:0407b2db334c | 1266 | result = 0; |
AzureIoTClient | 30:0407b2db334c | 1267 | } |
AzureIoTClient | 1:eab586236bfe | 1268 | } |
AzureIoTClient | 1:eab586236bfe | 1269 | } |
AzureIoTClient | 23:1111ee8bcba4 | 1270 | |
AzureIoTClient | 6:641a9672db08 | 1271 | return result; |
AzureIoTClient | 6:641a9672db08 | 1272 | } |
AzureIoTClient | 1:eab586236bfe | 1273 | |
AzureIoTClient | 6:641a9672db08 | 1274 | /*this function will clone an option given by name and value*/ |
AzureIoTClient | 30:0407b2db334c | 1275 | static void* saslclientio_clone_option(const char* name, const void* value) |
AzureIoTClient | 6:641a9672db08 | 1276 | { |
AzureIoTClient | 17:923575db8b2d | 1277 | (void)name; |
AzureIoTClient | 17:923575db8b2d | 1278 | (void)value; |
AzureIoTClient | 6:641a9672db08 | 1279 | return NULL; |
AzureIoTClient | 6:641a9672db08 | 1280 | } |
AzureIoTClient | 6:641a9672db08 | 1281 | |
AzureIoTClient | 6:641a9672db08 | 1282 | /*this function destroys an option previously created*/ |
AzureIoTClient | 30:0407b2db334c | 1283 | static void saslclientio_destroy_option(const char* name, const void* value) |
AzureIoTClient | 6:641a9672db08 | 1284 | { |
AzureIoTClient | 17:923575db8b2d | 1285 | (void)name; |
AzureIoTClient | 17:923575db8b2d | 1286 | (void)value; |
AzureIoTClient | 6:641a9672db08 | 1287 | } |
AzureIoTClient | 6:641a9672db08 | 1288 | |
AzureIoTClient | 30:0407b2db334c | 1289 | static OPTIONHANDLER_HANDLE saslclientio_retrieveoptions(CONCRETE_IO_HANDLE sasl_client_io) |
AzureIoTClient | 6:641a9672db08 | 1290 | { |
AzureIoTClient | 6:641a9672db08 | 1291 | OPTIONHANDLER_HANDLE result; |
AzureIoTClient | 30:0407b2db334c | 1292 | |
AzureIoTClient | 30:0407b2db334c | 1293 | if (sasl_client_io == NULL) |
AzureIoTClient | 6:641a9672db08 | 1294 | { |
AzureIoTClient | 30:0407b2db334c | 1295 | /* Codes_SRS_SASLCLIENTIO_01_139: [ When `saslclientio_retrieveoptions` is called with NULL `sasl_client_io` it shall fail and return NULL. ]*/ |
AzureIoTClient | 30:0407b2db334c | 1296 | result = NULL; |
AzureIoTClient | 6:641a9672db08 | 1297 | } |
AzureIoTClient | 6:641a9672db08 | 1298 | else |
AzureIoTClient | 6:641a9672db08 | 1299 | { |
AzureIoTClient | 30:0407b2db334c | 1300 | /* Codes_SRS_SASLCLIENTIO_01_133: [ `saslclientio_retrieveoptions` shall create an option handler by calling `OptionHandler_Create`. ]*/ |
AzureIoTClient | 30:0407b2db334c | 1301 | result = OptionHandler_Create(saslclientio_clone_option, saslclientio_destroy_option, saslclientio_setoption); |
AzureIoTClient | 30:0407b2db334c | 1302 | if (result == NULL) |
AzureIoTClient | 30:0407b2db334c | 1303 | { |
AzureIoTClient | 30:0407b2db334c | 1304 | /* Codes_SRS_SASLCLIENTIO_01_138: [ If `OptionHandler_AddOption` or `OptionHandler_Create` fails then `saslclientio_retrieveoptions` shall fail and return NULL. ]*/ |
AzureIoTClient | 30:0407b2db334c | 1305 | LogError("unable to OptionHandler_Create"); |
AzureIoTClient | 30:0407b2db334c | 1306 | /*return as is*/ |
AzureIoTClient | 30:0407b2db334c | 1307 | } |
AzureIoTClient | 30:0407b2db334c | 1308 | else |
AzureIoTClient | 30:0407b2db334c | 1309 | { |
AzureIoTClient | 30:0407b2db334c | 1310 | SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance = (SASL_CLIENT_IO_INSTANCE*)sasl_client_io; |
AzureIoTClient | 30:0407b2db334c | 1311 | |
AzureIoTClient | 30:0407b2db334c | 1312 | /*insert here work to add the options to "result" handle*/ |
AzureIoTClient | 30:0407b2db334c | 1313 | if (sasl_client_io_instance->is_trace_on_set) |
AzureIoTClient | 30:0407b2db334c | 1314 | { |
AzureIoTClient | 30:0407b2db334c | 1315 | bool logtrace = sasl_client_io_instance->is_trace_on ? true : false; |
AzureIoTClient | 30:0407b2db334c | 1316 | /* Codes_SRS_SASLCLIENTIO_01_137: [ The options shall be added by calling `OptionHandler_AddOption`. ]*/ |
AzureIoTClient | 30:0407b2db334c | 1317 | if (OptionHandler_AddOption(result, "logtrace", &logtrace) != 0) |
AzureIoTClient | 30:0407b2db334c | 1318 | { |
AzureIoTClient | 30:0407b2db334c | 1319 | /* Codes_SRS_SASLCLIENTIO_01_138: [ If `OptionHandler_AddOption` or `OptionHandler_Create` fails then `saslclientio_retrieveoptions` shall fail and return NULL. ]*/ |
AzureIoTClient | 30:0407b2db334c | 1320 | LogError("unable to add logtrace option"); |
AzureIoTClient | 30:0407b2db334c | 1321 | OptionHandler_Destroy(result); |
AzureIoTClient | 30:0407b2db334c | 1322 | result = NULL; |
AzureIoTClient | 30:0407b2db334c | 1323 | } |
AzureIoTClient | 30:0407b2db334c | 1324 | } |
AzureIoTClient | 30:0407b2db334c | 1325 | } |
AzureIoTClient | 6:641a9672db08 | 1326 | } |
AzureIoTClient | 23:1111ee8bcba4 | 1327 | |
AzureIoTClient | 1:eab586236bfe | 1328 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1329 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1330 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1331 | static const IO_INTERFACE_DESCRIPTION sasl_client_io_interface_description = |
Azure.IoT Build | 0:6ae2f7bca550 | 1332 | { |
AzureIoTClient | 6:641a9672db08 | 1333 | saslclientio_retrieveoptions, |
AzureIoTClient | 6:641a9672db08 | 1334 | saslclientio_create, |
AzureIoTClient | 6:641a9672db08 | 1335 | saslclientio_destroy, |
AzureIoTClient | 30:0407b2db334c | 1336 | saslclientio_open_async, |
AzureIoTClient | 30:0407b2db334c | 1337 | saslclientio_close_async, |
AzureIoTClient | 30:0407b2db334c | 1338 | saslclientio_send_async, |
AzureIoTClient | 6:641a9672db08 | 1339 | saslclientio_dowork, |
Azure.IoT Build | 0:6ae2f7bca550 | 1340 | saslclientio_setoption |
Azure.IoT Build | 0:6ae2f7bca550 | 1341 | }; |
Azure.IoT Build | 0:6ae2f7bca550 | 1342 | |
AzureIoTClient | 30:0407b2db334c | 1343 | /* Codes_SRS_SASLCLIENTIO_01_087: [`saslclientio_get_interface_description` shall return a pointer to an `IO_INTERFACE_DESCRIPTION` structure that contains pointers to the functions: `saslclientio_create`, `saslclientio_destroy`, `saslclientio_open_async`, `saslclientio_close_async`, `saslclientio_send_async`, `saslclientio_setoption`, `saslclientio_retrieveoptions` and `saslclientio_dowork`.]*/ |
Azure.IoT Build | 0:6ae2f7bca550 | 1344 | const IO_INTERFACE_DESCRIPTION* saslclientio_get_interface_description(void) |
Azure.IoT Build | 0:6ae2f7bca550 | 1345 | { |
AzureIoTClient | 6:641a9672db08 | 1346 | return &sasl_client_io_interface_description; |
Azure.IoT Build | 0:6ae2f7bca550 | 1347 | } |