A small memory footprint AMQP implimentation
Dependents: iothub_client_sample_amqp remote_monitoring simplesample_amqp
saslclientio.c@7:9e9ab3b0efef, 2016-08-12 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Fri Aug 12 10:04:13 2016 -0700
- Revision:
- 7:9e9ab3b0efef
- Parent:
- 6:641a9672db08
- Child:
- 9:c22db038556c
1.0.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 | #ifdef _CRTDBG_MAP_ALLOC |
Azure.IoT Build | 0:6ae2f7bca550 | 6 | #include <crtdbg.h> |
Azure.IoT Build | 0:6ae2f7bca550 | 7 | #endif |
Azure.IoT Build | 0:6ae2f7bca550 | 8 | #include <stddef.h> |
Azure.IoT Build | 0:6ae2f7bca550 | 9 | #include <stdio.h> |
Azure.IoT Build | 0:6ae2f7bca550 | 10 | #include <string.h> |
AzureIoTClient | 6:641a9672db08 | 11 | #include <stdbool.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/amqpalloc.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 16 | #include "azure_uamqp_c/frame_codec.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 17 | #include "azure_uamqp_c/sasl_frame_codec.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 18 | #include "azure_uamqp_c/amqp_definitions.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 19 | #include "azure_uamqp_c/amqpvalue_to_string.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 20 | |
Azure.IoT Build | 0:6ae2f7bca550 | 21 | typedef enum IO_STATE_TAG |
Azure.IoT Build | 0:6ae2f7bca550 | 22 | { |
AzureIoTClient | 6:641a9672db08 | 23 | IO_STATE_NOT_OPEN, |
AzureIoTClient | 6:641a9672db08 | 24 | IO_STATE_OPENING_UNDERLYING_IO, |
AzureIoTClient | 6:641a9672db08 | 25 | IO_STATE_SASL_HANDSHAKE, |
AzureIoTClient | 6:641a9672db08 | 26 | IO_STATE_OPEN, |
AzureIoTClient | 6:641a9672db08 | 27 | IO_STATE_CLOSING, |
AzureIoTClient | 6:641a9672db08 | 28 | IO_STATE_ERROR |
Azure.IoT Build | 0:6ae2f7bca550 | 29 | } IO_STATE; |
Azure.IoT Build | 0:6ae2f7bca550 | 30 | |
Azure.IoT Build | 0:6ae2f7bca550 | 31 | typedef enum SASL_HEADER_EXCHANGE_STATE_TAG |
Azure.IoT Build | 0:6ae2f7bca550 | 32 | { |
AzureIoTClient | 6:641a9672db08 | 33 | SASL_HEADER_EXCHANGE_IDLE, |
AzureIoTClient | 6:641a9672db08 | 34 | SASL_HEADER_EXCHANGE_HEADER_SENT, |
AzureIoTClient | 6:641a9672db08 | 35 | SASL_HEADER_EXCHANGE_HEADER_RCVD, |
AzureIoTClient | 6:641a9672db08 | 36 | SASL_HEADER_EXCHANGE_HEADER_EXCH |
Azure.IoT Build | 0:6ae2f7bca550 | 37 | } SASL_HEADER_EXCHANGE_STATE; |
Azure.IoT Build | 0:6ae2f7bca550 | 38 | |
Azure.IoT Build | 0:6ae2f7bca550 | 39 | typedef enum SASL_CLIENT_NEGOTIATION_STATE_TAG |
Azure.IoT Build | 0:6ae2f7bca550 | 40 | { |
AzureIoTClient | 6:641a9672db08 | 41 | SASL_CLIENT_NEGOTIATION_NOT_STARTED, |
AzureIoTClient | 6:641a9672db08 | 42 | SASL_CLIENT_NEGOTIATION_MECH_RCVD, |
AzureIoTClient | 6:641a9672db08 | 43 | SASL_CLIENT_NEGOTIATION_INIT_SENT, |
AzureIoTClient | 6:641a9672db08 | 44 | SASL_CLIENT_NEGOTIATION_CHALLENGE_RCVD, |
AzureIoTClient | 6:641a9672db08 | 45 | SASL_CLIENT_NEGOTIATION_RESPONSE_SENT, |
AzureIoTClient | 6:641a9672db08 | 46 | SASL_CLIENT_NEGOTIATION_OUTCOME_RCVD, |
AzureIoTClient | 6:641a9672db08 | 47 | SASL_CLIENT_NEGOTIATION_ERROR |
Azure.IoT Build | 0:6ae2f7bca550 | 48 | } SASL_CLIENT_NEGOTIATION_STATE; |
Azure.IoT Build | 0:6ae2f7bca550 | 49 | |
Azure.IoT Build | 0:6ae2f7bca550 | 50 | typedef struct SASL_CLIENT_IO_INSTANCE_TAG |
Azure.IoT Build | 0:6ae2f7bca550 | 51 | { |
AzureIoTClient | 6:641a9672db08 | 52 | XIO_HANDLE underlying_io; |
AzureIoTClient | 6:641a9672db08 | 53 | ON_BYTES_RECEIVED on_bytes_received; |
AzureIoTClient | 6:641a9672db08 | 54 | ON_IO_OPEN_COMPLETE on_io_open_complete; |
AzureIoTClient | 6:641a9672db08 | 55 | ON_IO_CLOSE_COMPLETE on_io_close_complete; |
AzureIoTClient | 6:641a9672db08 | 56 | ON_IO_ERROR on_io_error; |
Azure.IoT Build | 0:6ae2f7bca550 | 57 | void* on_bytes_received_context; |
AzureIoTClient | 6:641a9672db08 | 58 | void* on_io_open_complete_context; |
AzureIoTClient | 6:641a9672db08 | 59 | void* on_io_close_complete_context; |
Azure.IoT Build | 0:6ae2f7bca550 | 60 | void* on_io_error_context; |
AzureIoTClient | 6:641a9672db08 | 61 | SASL_HEADER_EXCHANGE_STATE sasl_header_exchange_state; |
AzureIoTClient | 6:641a9672db08 | 62 | SASL_CLIENT_NEGOTIATION_STATE sasl_client_negotiation_state; |
AzureIoTClient | 6:641a9672db08 | 63 | size_t header_bytes_received; |
AzureIoTClient | 6:641a9672db08 | 64 | SASL_FRAME_CODEC_HANDLE sasl_frame_codec; |
AzureIoTClient | 6:641a9672db08 | 65 | FRAME_CODEC_HANDLE frame_codec; |
AzureIoTClient | 6:641a9672db08 | 66 | IO_STATE io_state; |
AzureIoTClient | 6:641a9672db08 | 67 | SASL_MECHANISM_HANDLE sasl_mechanism; |
AzureIoTClient | 6:641a9672db08 | 68 | unsigned int is_trace_on : 1; |
Azure.IoT Build | 0:6ae2f7bca550 | 69 | } SASL_CLIENT_IO_INSTANCE; |
Azure.IoT Build | 0:6ae2f7bca550 | 70 | |
Azure.IoT Build | 0:6ae2f7bca550 | 71 | /* 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 | 72 | /* Codes_SRS_SASLCLIENTIO_01_124: [SASL-MAJOR 1 major protocol version.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 73 | /* Codes_SRS_SASLCLIENTIO_01_125: [SASL-MINOR 0 minor protocol version.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 74 | /* Codes_SRS_SASLCLIENTIO_01_126: [SASL-REVISION 0 protocol revision.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 75 | const unsigned char sasl_header[] = { 'A', 'M', 'Q', 'P', 3, 1, 0, 0 }; |
Azure.IoT Build | 0:6ae2f7bca550 | 76 | |
Azure.IoT Build | 0:6ae2f7bca550 | 77 | static void indicate_error(SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance) |
Azure.IoT Build | 0:6ae2f7bca550 | 78 | { |
AzureIoTClient | 6:641a9672db08 | 79 | if (sasl_client_io_instance->on_io_error != NULL) |
AzureIoTClient | 6:641a9672db08 | 80 | { |
AzureIoTClient | 6:641a9672db08 | 81 | sasl_client_io_instance->on_io_error(sasl_client_io_instance->on_io_error_context); |
AzureIoTClient | 6:641a9672db08 | 82 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 83 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 84 | |
Azure.IoT Build | 0:6ae2f7bca550 | 85 | static void indicate_open_complete(SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance, IO_OPEN_RESULT open_result) |
Azure.IoT Build | 0:6ae2f7bca550 | 86 | { |
AzureIoTClient | 6:641a9672db08 | 87 | if (sasl_client_io_instance->on_io_open_complete != NULL) |
AzureIoTClient | 6:641a9672db08 | 88 | { |
AzureIoTClient | 6:641a9672db08 | 89 | sasl_client_io_instance->on_io_open_complete(sasl_client_io_instance->on_io_open_complete_context, open_result); |
AzureIoTClient | 6:641a9672db08 | 90 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 91 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 92 | |
Azure.IoT Build | 0:6ae2f7bca550 | 93 | static void indicate_close_complete(SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance) |
Azure.IoT Build | 0:6ae2f7bca550 | 94 | { |
AzureIoTClient | 6:641a9672db08 | 95 | if (sasl_client_io_instance->on_io_close_complete != NULL) |
AzureIoTClient | 6:641a9672db08 | 96 | { |
AzureIoTClient | 6:641a9672db08 | 97 | sasl_client_io_instance->on_io_close_complete(sasl_client_io_instance->on_io_close_complete_context); |
AzureIoTClient | 6:641a9672db08 | 98 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 99 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 100 | |
Azure.IoT Build | 0:6ae2f7bca550 | 101 | static void on_underlying_io_close_complete(void* context) |
Azure.IoT Build | 0:6ae2f7bca550 | 102 | { |
AzureIoTClient | 6:641a9672db08 | 103 | SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance = (SASL_CLIENT_IO_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 104 | |
AzureIoTClient | 6:641a9672db08 | 105 | switch (sasl_client_io_instance->io_state) |
AzureIoTClient | 6:641a9672db08 | 106 | { |
AzureIoTClient | 6:641a9672db08 | 107 | default: |
AzureIoTClient | 6:641a9672db08 | 108 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 109 | |
AzureIoTClient | 6:641a9672db08 | 110 | case IO_STATE_OPENING_UNDERLYING_IO: |
AzureIoTClient | 6:641a9672db08 | 111 | case IO_STATE_SASL_HANDSHAKE: |
AzureIoTClient | 6:641a9672db08 | 112 | sasl_client_io_instance->io_state = IO_STATE_NOT_OPEN; |
AzureIoTClient | 6:641a9672db08 | 113 | indicate_open_complete(sasl_client_io_instance, IO_OPEN_ERROR); |
AzureIoTClient | 6:641a9672db08 | 114 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 115 | |
AzureIoTClient | 6:641a9672db08 | 116 | case IO_STATE_CLOSING: |
AzureIoTClient | 6:641a9672db08 | 117 | sasl_client_io_instance->io_state = IO_STATE_NOT_OPEN; |
AzureIoTClient | 6:641a9672db08 | 118 | indicate_close_complete(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 119 | break; |
AzureIoTClient | 6:641a9672db08 | 120 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 121 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 122 | |
Azure.IoT Build | 0:6ae2f7bca550 | 123 | static void handle_error(SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance) |
Azure.IoT Build | 0:6ae2f7bca550 | 124 | { |
AzureIoTClient | 6:641a9672db08 | 125 | switch (sasl_client_io_instance->io_state) |
AzureIoTClient | 6:641a9672db08 | 126 | { |
AzureIoTClient | 6:641a9672db08 | 127 | default: |
AzureIoTClient | 6:641a9672db08 | 128 | case IO_STATE_NOT_OPEN: |
AzureIoTClient | 6:641a9672db08 | 129 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 130 | |
AzureIoTClient | 6:641a9672db08 | 131 | case IO_STATE_OPENING_UNDERLYING_IO: |
AzureIoTClient | 6:641a9672db08 | 132 | case IO_STATE_SASL_HANDSHAKE: |
AzureIoTClient | 6:641a9672db08 | 133 | if (xio_close(sasl_client_io_instance->underlying_io, on_underlying_io_close_complete, sasl_client_io_instance) != 0) |
AzureIoTClient | 6:641a9672db08 | 134 | { |
AzureIoTClient | 6:641a9672db08 | 135 | sasl_client_io_instance->io_state = IO_STATE_NOT_OPEN; |
AzureIoTClient | 6:641a9672db08 | 136 | indicate_open_complete(sasl_client_io_instance, IO_OPEN_ERROR); |
AzureIoTClient | 6:641a9672db08 | 137 | } |
AzureIoTClient | 6:641a9672db08 | 138 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 139 | |
AzureIoTClient | 6:641a9672db08 | 140 | case IO_STATE_OPEN: |
AzureIoTClient | 6:641a9672db08 | 141 | sasl_client_io_instance->io_state = IO_STATE_ERROR; |
AzureIoTClient | 6:641a9672db08 | 142 | indicate_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 143 | break; |
AzureIoTClient | 6:641a9672db08 | 144 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 145 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 146 | |
Azure.IoT Build | 0:6ae2f7bca550 | 147 | static int send_sasl_header(SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance) |
Azure.IoT Build | 0:6ae2f7bca550 | 148 | { |
AzureIoTClient | 6:641a9672db08 | 149 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 150 | |
AzureIoTClient | 6:641a9672db08 | 151 | /* Codes_SRS_SASLCLIENTIO_01_078: [SASL client IO shall start the header exchange by sending the SASL header.] */ |
AzureIoTClient | 6:641a9672db08 | 152 | /* Codes_SRS_SASLCLIENTIO_01_095: [Sending the header shall be done by using xio_send.] */ |
AzureIoTClient | 6:641a9672db08 | 153 | if (xio_send(sasl_client_io_instance->underlying_io, sasl_header, sizeof(sasl_header), NULL, NULL) != 0) |
AzureIoTClient | 6:641a9672db08 | 154 | { |
AzureIoTClient | 6:641a9672db08 | 155 | result = __LINE__; |
AzureIoTClient | 6:641a9672db08 | 156 | } |
AzureIoTClient | 6:641a9672db08 | 157 | else |
AzureIoTClient | 6:641a9672db08 | 158 | { |
AzureIoTClient | 6:641a9672db08 | 159 | if (sasl_client_io_instance->is_trace_on == 1) |
AzureIoTClient | 6:641a9672db08 | 160 | { |
AzureIoTClient | 6:641a9672db08 | 161 | LOG(LOG_TRACE, LOG_LINE, "-> Header (AMQP 3.1.0.0)"); |
AzureIoTClient | 6:641a9672db08 | 162 | } |
AzureIoTClient | 6:641a9672db08 | 163 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 164 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 165 | |
AzureIoTClient | 6:641a9672db08 | 166 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 167 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 168 | |
Azure.IoT Build | 0:6ae2f7bca550 | 169 | static void on_underlying_io_open_complete(void* context, IO_OPEN_RESULT open_result) |
Azure.IoT Build | 0:6ae2f7bca550 | 170 | { |
AzureIoTClient | 6:641a9672db08 | 171 | SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance = (SASL_CLIENT_IO_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 172 | |
AzureIoTClient | 6:641a9672db08 | 173 | switch (sasl_client_io_instance->io_state) |
AzureIoTClient | 6:641a9672db08 | 174 | { |
AzureIoTClient | 6:641a9672db08 | 175 | default: |
AzureIoTClient | 6:641a9672db08 | 176 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 177 | |
AzureIoTClient | 6:641a9672db08 | 178 | case IO_STATE_OPENING_UNDERLYING_IO: |
AzureIoTClient | 6:641a9672db08 | 179 | if (open_result == IO_OPEN_OK) |
AzureIoTClient | 6:641a9672db08 | 180 | { |
AzureIoTClient | 6:641a9672db08 | 181 | sasl_client_io_instance->io_state = IO_STATE_SASL_HANDSHAKE; |
AzureIoTClient | 6:641a9672db08 | 182 | if (sasl_client_io_instance->sasl_header_exchange_state != SASL_HEADER_EXCHANGE_IDLE) |
AzureIoTClient | 6:641a9672db08 | 183 | { |
AzureIoTClient | 6:641a9672db08 | 184 | /* Codes_SRS_SASLCLIENTIO_01_116: [Any underlying IO state changes to state OPEN after the header exchange has been started shall trigger no action.] */ |
AzureIoTClient | 6:641a9672db08 | 185 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 186 | } |
AzureIoTClient | 6:641a9672db08 | 187 | else |
AzureIoTClient | 6:641a9672db08 | 188 | { |
AzureIoTClient | 6:641a9672db08 | 189 | /* Codes_SRS_SASLCLIENTIO_01_105: [start header exchange] */ |
AzureIoTClient | 6:641a9672db08 | 190 | /* Codes_SRS_SASLCLIENTIO_01_001: [To establish a SASL layer, each peer MUST start by sending a protocol header.] */ |
AzureIoTClient | 6:641a9672db08 | 191 | if (send_sasl_header(sasl_client_io_instance) != 0) |
AzureIoTClient | 6:641a9672db08 | 192 | { |
AzureIoTClient | 6:641a9672db08 | 193 | /* Codes_SRS_SASLCLIENTIO_01_073: [If the handshake fails (i.e. the outcome is an error) the SASL client IO state shall be switched to IO_STATE_ERROR and the on_state_changed callback shall be triggered.] */ |
AzureIoTClient | 6:641a9672db08 | 194 | /* Codes_SRS_SASLCLIENTIO_01_077: [If sending the SASL header fails, the SASL client IO state shall be set to IO_STATE_ERROR and the on_state_changed callback shall be triggered.] */ |
AzureIoTClient | 6:641a9672db08 | 195 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 196 | } |
AzureIoTClient | 6:641a9672db08 | 197 | else |
AzureIoTClient | 6:641a9672db08 | 198 | { |
AzureIoTClient | 6:641a9672db08 | 199 | sasl_client_io_instance->sasl_header_exchange_state = SASL_HEADER_EXCHANGE_HEADER_SENT; |
AzureIoTClient | 6:641a9672db08 | 200 | } |
AzureIoTClient | 6:641a9672db08 | 201 | } |
AzureIoTClient | 6:641a9672db08 | 202 | } |
AzureIoTClient | 6:641a9672db08 | 203 | else |
AzureIoTClient | 6:641a9672db08 | 204 | { |
AzureIoTClient | 6:641a9672db08 | 205 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 206 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 207 | |
AzureIoTClient | 6:641a9672db08 | 208 | break; |
AzureIoTClient | 6:641a9672db08 | 209 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 210 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 211 | |
Azure.IoT Build | 0:6ae2f7bca550 | 212 | static void on_underlying_io_error(void* context) |
Azure.IoT Build | 0:6ae2f7bca550 | 213 | { |
AzureIoTClient | 6:641a9672db08 | 214 | SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance = (SASL_CLIENT_IO_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 215 | |
AzureIoTClient | 6:641a9672db08 | 216 | switch (sasl_client_io_instance->io_state) |
AzureIoTClient | 6:641a9672db08 | 217 | { |
AzureIoTClient | 6:641a9672db08 | 218 | default: |
AzureIoTClient | 6:641a9672db08 | 219 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 220 | |
AzureIoTClient | 6:641a9672db08 | 221 | case IO_STATE_OPENING_UNDERLYING_IO: |
AzureIoTClient | 6:641a9672db08 | 222 | case IO_STATE_SASL_HANDSHAKE: |
AzureIoTClient | 6:641a9672db08 | 223 | sasl_client_io_instance->io_state = IO_STATE_NOT_OPEN; |
AzureIoTClient | 6:641a9672db08 | 224 | indicate_open_complete(sasl_client_io_instance, IO_OPEN_ERROR); |
AzureIoTClient | 6:641a9672db08 | 225 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 226 | |
AzureIoTClient | 6:641a9672db08 | 227 | case IO_STATE_OPEN: |
AzureIoTClient | 6:641a9672db08 | 228 | sasl_client_io_instance->io_state = IO_STATE_ERROR; |
AzureIoTClient | 6:641a9672db08 | 229 | indicate_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 230 | break; |
AzureIoTClient | 6:641a9672db08 | 231 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 232 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 233 | |
Azure.IoT Build | 0:6ae2f7bca550 | 234 | static const char* get_frame_type_as_string(AMQP_VALUE descriptor) |
Azure.IoT Build | 0:6ae2f7bca550 | 235 | { |
AzureIoTClient | 6:641a9672db08 | 236 | const char* result; |
Azure.IoT Build | 0:6ae2f7bca550 | 237 | |
AzureIoTClient | 6:641a9672db08 | 238 | if (is_sasl_mechanisms_type_by_descriptor(descriptor)) |
AzureIoTClient | 6:641a9672db08 | 239 | { |
AzureIoTClient | 6:641a9672db08 | 240 | result = "[SASL MECHANISMS]"; |
AzureIoTClient | 6:641a9672db08 | 241 | } |
AzureIoTClient | 6:641a9672db08 | 242 | else if (is_sasl_init_type_by_descriptor(descriptor)) |
AzureIoTClient | 6:641a9672db08 | 243 | { |
AzureIoTClient | 6:641a9672db08 | 244 | result = "[SASL INIT]"; |
AzureIoTClient | 6:641a9672db08 | 245 | } |
AzureIoTClient | 6:641a9672db08 | 246 | else if (is_sasl_challenge_type_by_descriptor(descriptor)) |
AzureIoTClient | 6:641a9672db08 | 247 | { |
AzureIoTClient | 6:641a9672db08 | 248 | result = "[SASL CHALLENGE]"; |
AzureIoTClient | 6:641a9672db08 | 249 | } |
AzureIoTClient | 6:641a9672db08 | 250 | else if (is_sasl_response_type_by_descriptor(descriptor)) |
AzureIoTClient | 6:641a9672db08 | 251 | { |
AzureIoTClient | 6:641a9672db08 | 252 | result = "[SASL RESPONSE]"; |
AzureIoTClient | 6:641a9672db08 | 253 | } |
AzureIoTClient | 6:641a9672db08 | 254 | else if (is_sasl_outcome_type_by_descriptor(descriptor)) |
AzureIoTClient | 6:641a9672db08 | 255 | { |
AzureIoTClient | 6:641a9672db08 | 256 | result = "[SASL OUTCOME]"; |
AzureIoTClient | 6:641a9672db08 | 257 | } |
AzureIoTClient | 6:641a9672db08 | 258 | else |
AzureIoTClient | 6:641a9672db08 | 259 | { |
AzureIoTClient | 6:641a9672db08 | 260 | result = "[Unknown]"; |
AzureIoTClient | 6:641a9672db08 | 261 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 262 | |
AzureIoTClient | 6:641a9672db08 | 263 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 264 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 265 | |
AzureIoTClient | 7:9e9ab3b0efef | 266 | #ifndef NO_LOGGING |
AzureIoTClient | 6:641a9672db08 | 267 | static void log_incoming_frame(AMQP_VALUE performative) |
Azure.IoT Build | 0:6ae2f7bca550 | 268 | { |
AzureIoTClient | 6:641a9672db08 | 269 | if (xlogging_get_log_function() != NULL) |
AzureIoTClient | 6:641a9672db08 | 270 | { |
AzureIoTClient | 6:641a9672db08 | 271 | AMQP_VALUE descriptor = amqpvalue_get_inplace_descriptor(performative); |
AzureIoTClient | 6:641a9672db08 | 272 | if (descriptor != NULL) |
AzureIoTClient | 6:641a9672db08 | 273 | { |
AzureIoTClient | 6:641a9672db08 | 274 | LOG(LOG_TRACE, 0, "<- "); |
AzureIoTClient | 6:641a9672db08 | 275 | LOG(LOG_TRACE, 0, (char*)get_frame_type_as_string(descriptor)); |
AzureIoTClient | 6:641a9672db08 | 276 | char* performative_as_string = NULL; |
AzureIoTClient | 6:641a9672db08 | 277 | LOG(LOG_TRACE, LOG_LINE, (performative_as_string = amqpvalue_to_string(performative))); |
AzureIoTClient | 6:641a9672db08 | 278 | if (performative_as_string != NULL) |
AzureIoTClient | 6:641a9672db08 | 279 | { |
AzureIoTClient | 6:641a9672db08 | 280 | amqpalloc_free(performative_as_string); |
AzureIoTClient | 6:641a9672db08 | 281 | } |
AzureIoTClient | 6:641a9672db08 | 282 | } |
AzureIoTClient | 6:641a9672db08 | 283 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 284 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 285 | |
AzureIoTClient | 6:641a9672db08 | 286 | static void log_outgoing_frame(AMQP_VALUE performative) |
Azure.IoT Build | 0:6ae2f7bca550 | 287 | { |
AzureIoTClient | 6:641a9672db08 | 288 | if (xlogging_get_log_function() != NULL) |
AzureIoTClient | 6:641a9672db08 | 289 | { |
AzureIoTClient | 6:641a9672db08 | 290 | AMQP_VALUE descriptor = amqpvalue_get_inplace_descriptor(performative); |
AzureIoTClient | 6:641a9672db08 | 291 | if (descriptor != NULL) |
AzureIoTClient | 6:641a9672db08 | 292 | { |
AzureIoTClient | 6:641a9672db08 | 293 | LOG(LOG_TRACE, 0, "-> "); |
AzureIoTClient | 6:641a9672db08 | 294 | LOG(LOG_TRACE, 0, (char*)get_frame_type_as_string(descriptor)); |
AzureIoTClient | 6:641a9672db08 | 295 | char* performative_as_string = NULL; |
AzureIoTClient | 6:641a9672db08 | 296 | LOG(LOG_TRACE, LOG_LINE, (performative_as_string = amqpvalue_to_string(performative))); |
AzureIoTClient | 6:641a9672db08 | 297 | if (performative_as_string != NULL) |
AzureIoTClient | 6:641a9672db08 | 298 | { |
AzureIoTClient | 6:641a9672db08 | 299 | amqpalloc_free(performative_as_string); |
AzureIoTClient | 6:641a9672db08 | 300 | } |
AzureIoTClient | 6:641a9672db08 | 301 | } |
AzureIoTClient | 6:641a9672db08 | 302 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 303 | } |
AzureIoTClient | 7:9e9ab3b0efef | 304 | #endif |
Azure.IoT Build | 0:6ae2f7bca550 | 305 | |
Azure.IoT Build | 0:6ae2f7bca550 | 306 | static int saslclientio_receive_byte(SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance, unsigned char b) |
Azure.IoT Build | 0:6ae2f7bca550 | 307 | { |
AzureIoTClient | 6:641a9672db08 | 308 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 309 | |
AzureIoTClient | 6:641a9672db08 | 310 | switch (sasl_client_io_instance->sasl_header_exchange_state) |
AzureIoTClient | 6:641a9672db08 | 311 | { |
AzureIoTClient | 6:641a9672db08 | 312 | default: |
AzureIoTClient | 6:641a9672db08 | 313 | result = __LINE__; |
AzureIoTClient | 6:641a9672db08 | 314 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 315 | |
AzureIoTClient | 6:641a9672db08 | 316 | case SASL_HEADER_EXCHANGE_HEADER_EXCH: |
AzureIoTClient | 6:641a9672db08 | 317 | switch (sasl_client_io_instance->sasl_client_negotiation_state) |
AzureIoTClient | 6:641a9672db08 | 318 | { |
AzureIoTClient | 6:641a9672db08 | 319 | case SASL_CLIENT_NEGOTIATION_ERROR: |
AzureIoTClient | 6:641a9672db08 | 320 | result = __LINE__; |
AzureIoTClient | 6:641a9672db08 | 321 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 322 | |
AzureIoTClient | 6:641a9672db08 | 323 | default: |
AzureIoTClient | 6:641a9672db08 | 324 | /* 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 | 325 | if (frame_codec_receive_bytes(sasl_client_io_instance->frame_codec, &b, 1) != 0) |
AzureIoTClient | 6:641a9672db08 | 326 | { |
AzureIoTClient | 6:641a9672db08 | 327 | /* Codes_SRS_SASLCLIENTIO_01_088: [If frame_codec_receive_bytes fails, the state of SASL client IO shall be switched to IO_STATE_ERROR and the on_state_changed callback shall be triggered.] */ |
AzureIoTClient | 6:641a9672db08 | 328 | result = __LINE__; |
AzureIoTClient | 6:641a9672db08 | 329 | } |
AzureIoTClient | 6:641a9672db08 | 330 | else |
AzureIoTClient | 6:641a9672db08 | 331 | { |
AzureIoTClient | 6:641a9672db08 | 332 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 333 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 334 | |
AzureIoTClient | 6:641a9672db08 | 335 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 336 | |
AzureIoTClient | 6:641a9672db08 | 337 | case SASL_CLIENT_NEGOTIATION_OUTCOME_RCVD: |
AzureIoTClient | 6:641a9672db08 | 338 | sasl_client_io_instance->on_bytes_received(sasl_client_io_instance->on_bytes_received_context, &b, 1); |
AzureIoTClient | 6:641a9672db08 | 339 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 340 | break; |
AzureIoTClient | 6:641a9672db08 | 341 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 342 | |
AzureIoTClient | 6:641a9672db08 | 343 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 344 | |
AzureIoTClient | 6:641a9672db08 | 345 | /* 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 | 346 | case SASL_HEADER_EXCHANGE_IDLE: |
AzureIoTClient | 6:641a9672db08 | 347 | case SASL_HEADER_EXCHANGE_HEADER_SENT: |
AzureIoTClient | 6:641a9672db08 | 348 | if (b != sasl_header[sasl_client_io_instance->header_bytes_received]) |
AzureIoTClient | 6:641a9672db08 | 349 | { |
AzureIoTClient | 6:641a9672db08 | 350 | result = __LINE__; |
AzureIoTClient | 6:641a9672db08 | 351 | } |
AzureIoTClient | 6:641a9672db08 | 352 | else |
AzureIoTClient | 6:641a9672db08 | 353 | { |
AzureIoTClient | 6:641a9672db08 | 354 | sasl_client_io_instance->header_bytes_received++; |
AzureIoTClient | 6:641a9672db08 | 355 | if (sasl_client_io_instance->header_bytes_received == sizeof(sasl_header)) |
AzureIoTClient | 6:641a9672db08 | 356 | { |
AzureIoTClient | 6:641a9672db08 | 357 | if (sasl_client_io_instance->is_trace_on == 1) |
AzureIoTClient | 6:641a9672db08 | 358 | { |
AzureIoTClient | 6:641a9672db08 | 359 | LOG(LOG_TRACE, LOG_LINE, "<- Header (AMQP 3.1.0.0)"); |
AzureIoTClient | 6:641a9672db08 | 360 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 361 | |
AzureIoTClient | 6:641a9672db08 | 362 | switch (sasl_client_io_instance->sasl_header_exchange_state) |
AzureIoTClient | 6:641a9672db08 | 363 | { |
AzureIoTClient | 6:641a9672db08 | 364 | default: |
AzureIoTClient | 6:641a9672db08 | 365 | result = __LINE__; |
AzureIoTClient | 6:641a9672db08 | 366 | break; |
AzureIoTClient | 6:641a9672db08 | 367 | |
AzureIoTClient | 6:641a9672db08 | 368 | case SASL_HEADER_EXCHANGE_HEADER_SENT: |
AzureIoTClient | 6:641a9672db08 | 369 | /* from this point on we need to decode SASL frames */ |
AzureIoTClient | 6:641a9672db08 | 370 | sasl_client_io_instance->sasl_header_exchange_state = SASL_HEADER_EXCHANGE_HEADER_EXCH; |
AzureIoTClient | 6:641a9672db08 | 371 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 372 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 373 | |
AzureIoTClient | 6:641a9672db08 | 374 | case SASL_HEADER_EXCHANGE_IDLE: |
AzureIoTClient | 6:641a9672db08 | 375 | sasl_client_io_instance->sasl_header_exchange_state = SASL_HEADER_EXCHANGE_HEADER_RCVD; |
AzureIoTClient | 6:641a9672db08 | 376 | if (send_sasl_header(sasl_client_io_instance) != 0) |
AzureIoTClient | 6:641a9672db08 | 377 | { |
AzureIoTClient | 6:641a9672db08 | 378 | /* Codes_SRS_SASLCLIENTIO_01_077: [If sending the SASL header fails, the SASL client IO state shall be set to IO_STATE_ERROR and the on_state_changed callback shall be triggered.] */ |
AzureIoTClient | 6:641a9672db08 | 379 | result = __LINE__; |
AzureIoTClient | 6:641a9672db08 | 380 | } |
AzureIoTClient | 6:641a9672db08 | 381 | else |
AzureIoTClient | 6:641a9672db08 | 382 | { |
AzureIoTClient | 6:641a9672db08 | 383 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 384 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 385 | |
AzureIoTClient | 6:641a9672db08 | 386 | break; |
AzureIoTClient | 6:641a9672db08 | 387 | } |
AzureIoTClient | 6:641a9672db08 | 388 | } |
AzureIoTClient | 6:641a9672db08 | 389 | else |
AzureIoTClient | 6:641a9672db08 | 390 | { |
AzureIoTClient | 6:641a9672db08 | 391 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 392 | } |
AzureIoTClient | 6:641a9672db08 | 393 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 394 | |
AzureIoTClient | 6:641a9672db08 | 395 | break; |
AzureIoTClient | 6:641a9672db08 | 396 | } |
AzureIoTClient | 6:641a9672db08 | 397 | |
AzureIoTClient | 6:641a9672db08 | 398 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 399 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 400 | |
Azure.IoT Build | 0:6ae2f7bca550 | 401 | static void on_underlying_io_bytes_received(void* context, const unsigned char* buffer, size_t size) |
Azure.IoT Build | 0:6ae2f7bca550 | 402 | { |
AzureIoTClient | 6:641a9672db08 | 403 | SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance = (SASL_CLIENT_IO_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 404 | |
AzureIoTClient | 6:641a9672db08 | 405 | /* Codes_SRS_SASLCLIENTIO_01_028: [If buffer is NULL or size is zero, nothing should be indicated as received and the saslio state shall be switched to ERROR the on_state_changed callback shall be triggered.] */ |
AzureIoTClient | 6:641a9672db08 | 406 | if ((buffer == NULL) || |
AzureIoTClient | 6:641a9672db08 | 407 | (size == 0)) |
AzureIoTClient | 6:641a9672db08 | 408 | { |
AzureIoTClient | 6:641a9672db08 | 409 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 410 | } |
AzureIoTClient | 6:641a9672db08 | 411 | else |
AzureIoTClient | 6:641a9672db08 | 412 | { |
AzureIoTClient | 6:641a9672db08 | 413 | switch (sasl_client_io_instance->io_state) |
AzureIoTClient | 6:641a9672db08 | 414 | { |
AzureIoTClient | 6:641a9672db08 | 415 | default: |
AzureIoTClient | 6:641a9672db08 | 416 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 417 | |
AzureIoTClient | 6:641a9672db08 | 418 | case IO_STATE_OPEN: |
AzureIoTClient | 6:641a9672db08 | 419 | /* Codes_SRS_SASLCLIENTIO_01_027: [When the on_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 that was passed in saslclientio_open.] */ |
AzureIoTClient | 6:641a9672db08 | 420 | /* Codes_SRS_SASLCLIENTIO_01_029: [The context argument shall be set to the callback_context passed in saslclientio_open.] */ |
AzureIoTClient | 6:641a9672db08 | 421 | sasl_client_io_instance->on_bytes_received(sasl_client_io_instance->on_bytes_received_context, buffer, size); |
AzureIoTClient | 6:641a9672db08 | 422 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 423 | |
AzureIoTClient | 6:641a9672db08 | 424 | case IO_STATE_SASL_HANDSHAKE: |
AzureIoTClient | 6:641a9672db08 | 425 | { |
AzureIoTClient | 6:641a9672db08 | 426 | size_t i; |
Azure.IoT Build | 0:6ae2f7bca550 | 427 | |
AzureIoTClient | 6:641a9672db08 | 428 | for (i = 0; i < size; i++) |
AzureIoTClient | 6:641a9672db08 | 429 | { |
AzureIoTClient | 6:641a9672db08 | 430 | if (saslclientio_receive_byte(sasl_client_io_instance, buffer[i]) != 0) |
AzureIoTClient | 6:641a9672db08 | 431 | { |
AzureIoTClient | 6:641a9672db08 | 432 | break; |
AzureIoTClient | 6:641a9672db08 | 433 | } |
AzureIoTClient | 6:641a9672db08 | 434 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 435 | |
AzureIoTClient | 6:641a9672db08 | 436 | if (i < size) |
AzureIoTClient | 6:641a9672db08 | 437 | { |
AzureIoTClient | 6:641a9672db08 | 438 | /* Codes_SRS_SASLCLIENTIO_01_073: [If the handshake fails (i.e. the outcome is an error) the SASL client IO state shall be switched to IO_STATE_ERROR and the on_state_changed callback shall be triggered.] */ |
AzureIoTClient | 6:641a9672db08 | 439 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 440 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 441 | |
AzureIoTClient | 6:641a9672db08 | 442 | break; |
AzureIoTClient | 6:641a9672db08 | 443 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 444 | |
AzureIoTClient | 6:641a9672db08 | 445 | case IO_STATE_ERROR: |
AzureIoTClient | 6:641a9672db08 | 446 | /* 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 | 447 | break; |
AzureIoTClient | 6:641a9672db08 | 448 | } |
AzureIoTClient | 6:641a9672db08 | 449 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 450 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 451 | |
Azure.IoT Build | 0:6ae2f7bca550 | 452 | static void on_bytes_encoded(void* context, const unsigned char* bytes, size_t length, bool encode_complete) |
Azure.IoT Build | 0:6ae2f7bca550 | 453 | { |
AzureIoTClient | 6:641a9672db08 | 454 | (void)encode_complete; |
Azure.IoT Build | 0:6ae2f7bca550 | 455 | |
AzureIoTClient | 6:641a9672db08 | 456 | SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance = (SASL_CLIENT_IO_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 457 | |
AzureIoTClient | 6:641a9672db08 | 458 | /* 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 | 6:641a9672db08 | 459 | if (xio_send(sasl_client_io_instance->underlying_io, bytes, length, NULL, NULL) != 0) |
AzureIoTClient | 6:641a9672db08 | 460 | { |
AzureIoTClient | 6:641a9672db08 | 461 | /* Codes_SRS_SASLCLIENTIO_01_121: [If xio_send fails, the SASL client IO state shall be switched to IO_STATE_ERROR and the on_state_changed callback shall be triggered.] */ |
AzureIoTClient | 6:641a9672db08 | 462 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 463 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 464 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 465 | |
Azure.IoT Build | 0:6ae2f7bca550 | 466 | static int send_sasl_init(SASL_CLIENT_IO_INSTANCE* sasl_client_io, const char* sasl_mechanism_name) |
Azure.IoT Build | 0:6ae2f7bca550 | 467 | { |
AzureIoTClient | 6:641a9672db08 | 468 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 469 | |
AzureIoTClient | 6:641a9672db08 | 470 | SASL_INIT_HANDLE sasl_init; |
AzureIoTClient | 6:641a9672db08 | 471 | SASL_MECHANISM_BYTES init_bytes; |
Azure.IoT Build | 0:6ae2f7bca550 | 472 | |
AzureIoTClient | 6:641a9672db08 | 473 | /* Codes_SRS_SASLCLIENTIO_01_045: [The name of the SASL mechanism used for the SASL exchange.] */ |
AzureIoTClient | 6:641a9672db08 | 474 | sasl_init = sasl_init_create(sasl_mechanism_name); |
AzureIoTClient | 6:641a9672db08 | 475 | if (sasl_init == NULL) |
AzureIoTClient | 6:641a9672db08 | 476 | { |
AzureIoTClient | 6:641a9672db08 | 477 | /* Codes_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the SASL client IO state shall be switched to IO_STATE_ERROR and the on_state_changed callback shall be triggered.] */ |
AzureIoTClient | 6:641a9672db08 | 478 | result = __LINE__; |
AzureIoTClient | 6:641a9672db08 | 479 | } |
AzureIoTClient | 6:641a9672db08 | 480 | else |
AzureIoTClient | 6:641a9672db08 | 481 | { |
AzureIoTClient | 6:641a9672db08 | 482 | /* Codes_SRS_SASLCLIENTIO_01_048: [The contents of this data are defined by the SASL security mechanism.] */ |
AzureIoTClient | 6:641a9672db08 | 483 | if (saslmechanism_get_init_bytes(sasl_client_io->sasl_mechanism, &init_bytes) != 0) |
AzureIoTClient | 6:641a9672db08 | 484 | { |
AzureIoTClient | 6:641a9672db08 | 485 | /* Codes_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the SASL client IO state shall be switched to IO_STATE_ERROR and the on_state_changed callback shall be triggered.] */ |
AzureIoTClient | 6:641a9672db08 | 486 | result = __LINE__; |
AzureIoTClient | 6:641a9672db08 | 487 | } |
AzureIoTClient | 6:641a9672db08 | 488 | else |
AzureIoTClient | 6:641a9672db08 | 489 | { |
AzureIoTClient | 6:641a9672db08 | 490 | amqp_binary creds; |
AzureIoTClient | 6:641a9672db08 | 491 | creds.bytes = init_bytes.bytes; |
AzureIoTClient | 6:641a9672db08 | 492 | creds.length = init_bytes.length; |
AzureIoTClient | 6:641a9672db08 | 493 | if ((init_bytes.length > 0) && |
AzureIoTClient | 6:641a9672db08 | 494 | /* Codes_SRS_SASLCLIENTIO_01_047: [A block of opaque data passed to the security mechanism.] */ |
AzureIoTClient | 6:641a9672db08 | 495 | (sasl_init_set_initial_response(sasl_init, creds) != 0)) |
AzureIoTClient | 6:641a9672db08 | 496 | { |
AzureIoTClient | 6:641a9672db08 | 497 | /* Codes_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the SASL client IO state shall be switched to IO_STATE_ERROR and the on_state_changed callback shall be triggered.] */ |
AzureIoTClient | 6:641a9672db08 | 498 | result = __LINE__; |
AzureIoTClient | 6:641a9672db08 | 499 | } |
AzureIoTClient | 6:641a9672db08 | 500 | else |
AzureIoTClient | 6:641a9672db08 | 501 | { |
AzureIoTClient | 6:641a9672db08 | 502 | AMQP_VALUE sasl_init_value = amqpvalue_create_sasl_init(sasl_init); |
AzureIoTClient | 6:641a9672db08 | 503 | if (sasl_init_value == NULL) |
AzureIoTClient | 6:641a9672db08 | 504 | { |
AzureIoTClient | 6:641a9672db08 | 505 | /* Codes_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the SASL client IO state shall be switched to IO_STATE_ERROR and the on_state_changed callback shall be triggered.] */ |
AzureIoTClient | 6:641a9672db08 | 506 | result = __LINE__; |
AzureIoTClient | 6:641a9672db08 | 507 | } |
AzureIoTClient | 6:641a9672db08 | 508 | else |
AzureIoTClient | 6:641a9672db08 | 509 | { |
AzureIoTClient | 6:641a9672db08 | 510 | /* 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 | 511 | 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 | 512 | { |
AzureIoTClient | 6:641a9672db08 | 513 | /* Codes_SRS_SASLCLIENTIO_01_071: [If sasl_frame_codec_encode_frame fails, then the state of SASL client IO shall be switched to IO_STATE_ERROR and the on_state_changed callback shall be triggered.] */ |
AzureIoTClient | 6:641a9672db08 | 514 | result = __LINE__; |
AzureIoTClient | 6:641a9672db08 | 515 | } |
AzureIoTClient | 6:641a9672db08 | 516 | else |
AzureIoTClient | 6:641a9672db08 | 517 | { |
AzureIoTClient | 6:641a9672db08 | 518 | if (sasl_client_io->is_trace_on == 1) |
AzureIoTClient | 6:641a9672db08 | 519 | { |
AzureIoTClient | 6:641a9672db08 | 520 | log_outgoing_frame(sasl_init_value); |
AzureIoTClient | 6:641a9672db08 | 521 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 522 | |
AzureIoTClient | 6:641a9672db08 | 523 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 524 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 525 | |
AzureIoTClient | 6:641a9672db08 | 526 | amqpvalue_destroy(sasl_init_value); |
AzureIoTClient | 6:641a9672db08 | 527 | } |
AzureIoTClient | 6:641a9672db08 | 528 | } |
AzureIoTClient | 6:641a9672db08 | 529 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 530 | |
AzureIoTClient | 6:641a9672db08 | 531 | sasl_init_destroy(sasl_init); |
AzureIoTClient | 6:641a9672db08 | 532 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 533 | |
AzureIoTClient | 6:641a9672db08 | 534 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 535 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 536 | |
Azure.IoT Build | 0:6ae2f7bca550 | 537 | static int send_sasl_response(SASL_CLIENT_IO_INSTANCE* sasl_client_io, SASL_MECHANISM_BYTES sasl_response) |
Azure.IoT Build | 0:6ae2f7bca550 | 538 | { |
AzureIoTClient | 6:641a9672db08 | 539 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 540 | |
AzureIoTClient | 6:641a9672db08 | 541 | SASL_RESPONSE_HANDLE sasl_response_handle; |
AzureIoTClient | 6:641a9672db08 | 542 | amqp_binary response_binary_value; |
AzureIoTClient | 6:641a9672db08 | 543 | |
AzureIoTClient | 6:641a9672db08 | 544 | response_binary_value.bytes = sasl_response.bytes; |
AzureIoTClient | 6:641a9672db08 | 545 | response_binary_value.length = sasl_response.length; |
Azure.IoT Build | 0:6ae2f7bca550 | 546 | |
AzureIoTClient | 6:641a9672db08 | 547 | /* Codes_SRS_SASLCLIENTIO_01_055: [Send the SASL response data as defined by the SASL specification.] */ |
AzureIoTClient | 6:641a9672db08 | 548 | /* Codes_SRS_SASLCLIENTIO_01_056: [A block of opaque data passed to the security mechanism.] */ |
AzureIoTClient | 6:641a9672db08 | 549 | if ((sasl_response_handle = sasl_response_create(response_binary_value)) == NULL) |
AzureIoTClient | 6:641a9672db08 | 550 | { |
AzureIoTClient | 6:641a9672db08 | 551 | result = __LINE__; |
AzureIoTClient | 6:641a9672db08 | 552 | } |
AzureIoTClient | 6:641a9672db08 | 553 | else |
AzureIoTClient | 6:641a9672db08 | 554 | { |
AzureIoTClient | 6:641a9672db08 | 555 | AMQP_VALUE sasl_response_value = amqpvalue_create_sasl_response(sasl_response_handle); |
AzureIoTClient | 6:641a9672db08 | 556 | if (sasl_response_value == NULL) |
AzureIoTClient | 6:641a9672db08 | 557 | { |
AzureIoTClient | 6:641a9672db08 | 558 | result = __LINE__; |
AzureIoTClient | 6:641a9672db08 | 559 | } |
AzureIoTClient | 6:641a9672db08 | 560 | else |
AzureIoTClient | 6:641a9672db08 | 561 | { |
AzureIoTClient | 6:641a9672db08 | 562 | /* 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 | 563 | 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 | 564 | { |
AzureIoTClient | 6:641a9672db08 | 565 | result = __LINE__; |
AzureIoTClient | 6:641a9672db08 | 566 | } |
AzureIoTClient | 6:641a9672db08 | 567 | else |
AzureIoTClient | 6:641a9672db08 | 568 | { |
AzureIoTClient | 6:641a9672db08 | 569 | if (sasl_client_io->is_trace_on == 1) |
AzureIoTClient | 6:641a9672db08 | 570 | { |
AzureIoTClient | 6:641a9672db08 | 571 | log_outgoing_frame(sasl_response_value); |
AzureIoTClient | 6:641a9672db08 | 572 | } |
AzureIoTClient | 6:641a9672db08 | 573 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 574 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 575 | |
AzureIoTClient | 6:641a9672db08 | 576 | amqpvalue_destroy(sasl_response_value); |
AzureIoTClient | 6:641a9672db08 | 577 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 578 | |
AzureIoTClient | 6:641a9672db08 | 579 | sasl_response_destroy(sasl_response_handle); |
AzureIoTClient | 6:641a9672db08 | 580 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 581 | |
AzureIoTClient | 6:641a9672db08 | 582 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 583 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 584 | |
Azure.IoT Build | 0:6ae2f7bca550 | 585 | static void sasl_frame_received_callback(void* context, AMQP_VALUE sasl_frame) |
Azure.IoT Build | 0:6ae2f7bca550 | 586 | { |
AzureIoTClient | 6:641a9672db08 | 587 | SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance = (SASL_CLIENT_IO_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 588 | |
AzureIoTClient | 6:641a9672db08 | 589 | /* Codes_SRS_SASLCLIENTIO_01_067: [The SASL frame exchange shall be started as soon as the SASL header handshake is done.] */ |
AzureIoTClient | 6:641a9672db08 | 590 | switch (sasl_client_io_instance->io_state) |
AzureIoTClient | 6:641a9672db08 | 591 | { |
AzureIoTClient | 6:641a9672db08 | 592 | default: |
AzureIoTClient | 6:641a9672db08 | 593 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 594 | |
AzureIoTClient | 6:641a9672db08 | 595 | case IO_STATE_OPEN: |
AzureIoTClient | 6:641a9672db08 | 596 | case IO_STATE_OPENING_UNDERLYING_IO: |
AzureIoTClient | 6:641a9672db08 | 597 | case IO_STATE_CLOSING: |
AzureIoTClient | 6:641a9672db08 | 598 | /* Codes_SRS_SASLCLIENTIO_01_117: [If on_sasl_frame_received_callback is called when the state of the IO is OPEN then the SASL client IO state shall be switched to IO_STATE_ERROR and the on_state_changed callback shall be triggered.] */ |
AzureIoTClient | 6:641a9672db08 | 599 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 600 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 601 | |
AzureIoTClient | 6:641a9672db08 | 602 | case IO_STATE_SASL_HANDSHAKE: |
AzureIoTClient | 6:641a9672db08 | 603 | if (sasl_client_io_instance->sasl_header_exchange_state != SASL_HEADER_EXCHANGE_HEADER_EXCH) |
AzureIoTClient | 6:641a9672db08 | 604 | { |
AzureIoTClient | 6:641a9672db08 | 605 | /* 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 SASL client IO state shall be switched to IO_STATE_ERROR and the on_state_changed callback shall be triggered.] */ |
AzureIoTClient | 6:641a9672db08 | 606 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 607 | } |
AzureIoTClient | 6:641a9672db08 | 608 | else |
AzureIoTClient | 6:641a9672db08 | 609 | { |
AzureIoTClient | 6:641a9672db08 | 610 | AMQP_VALUE descriptor = amqpvalue_get_inplace_descriptor(sasl_frame); |
AzureIoTClient | 6:641a9672db08 | 611 | if (descriptor == NULL) |
AzureIoTClient | 6:641a9672db08 | 612 | { |
AzureIoTClient | 6:641a9672db08 | 613 | /* Codes_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the SASL client IO state shall be switched to IO_STATE_ERROR and the on_state_changed callback shall be triggered.] */ |
AzureIoTClient | 6:641a9672db08 | 614 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 615 | } |
AzureIoTClient | 6:641a9672db08 | 616 | else |
AzureIoTClient | 6:641a9672db08 | 617 | { |
AzureIoTClient | 6:641a9672db08 | 618 | if (sasl_client_io_instance->is_trace_on == 1) |
AzureIoTClient | 6:641a9672db08 | 619 | { |
AzureIoTClient | 6:641a9672db08 | 620 | log_incoming_frame(sasl_frame); |
AzureIoTClient | 6:641a9672db08 | 621 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 622 | |
AzureIoTClient | 6:641a9672db08 | 623 | /* 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 | 624 | /* 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 | 625 | /* Codes_SRS_SASLCLIENTIO_01_034: [<-- SASL-MECHANISMS] */ |
AzureIoTClient | 6:641a9672db08 | 626 | if (is_sasl_mechanisms_type_by_descriptor(descriptor)) |
AzureIoTClient | 6:641a9672db08 | 627 | { |
AzureIoTClient | 6:641a9672db08 | 628 | switch (sasl_client_io_instance->sasl_client_negotiation_state) |
AzureIoTClient | 6:641a9672db08 | 629 | { |
AzureIoTClient | 6:641a9672db08 | 630 | case SASL_CLIENT_NEGOTIATION_NOT_STARTED: |
AzureIoTClient | 6:641a9672db08 | 631 | { |
AzureIoTClient | 6:641a9672db08 | 632 | SASL_MECHANISMS_HANDLE sasl_mechanisms_handle; |
Azure.IoT Build | 0:6ae2f7bca550 | 633 | |
AzureIoTClient | 6:641a9672db08 | 634 | if (amqpvalue_get_sasl_mechanisms(sasl_frame, &sasl_mechanisms_handle) != 0) |
AzureIoTClient | 6:641a9672db08 | 635 | { |
AzureIoTClient | 6:641a9672db08 | 636 | /* Codes_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the SASL client IO state shall be switched to IO_STATE_ERROR and the on_state_changed callback shall be triggered.] */ |
AzureIoTClient | 6:641a9672db08 | 637 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 638 | } |
AzureIoTClient | 6:641a9672db08 | 639 | else |
AzureIoTClient | 6:641a9672db08 | 640 | { |
AzureIoTClient | 6:641a9672db08 | 641 | AMQP_VALUE sasl_server_mechanisms; |
AzureIoTClient | 6:641a9672db08 | 642 | uint32_t mechanisms_count; |
Azure.IoT Build | 0:6ae2f7bca550 | 643 | |
AzureIoTClient | 6:641a9672db08 | 644 | if ((sasl_mechanisms_get_sasl_server_mechanisms(sasl_mechanisms_handle, &sasl_server_mechanisms) != 0) || |
AzureIoTClient | 6:641a9672db08 | 645 | (amqpvalue_get_array_item_count(sasl_server_mechanisms, &mechanisms_count) != 0) || |
AzureIoTClient | 6:641a9672db08 | 646 | (mechanisms_count == 0)) |
AzureIoTClient | 6:641a9672db08 | 647 | { |
AzureIoTClient | 6:641a9672db08 | 648 | /* Codes_SRS_SASLCLIENTIO_01_042: [It is invalid for this list to be null or empty.] */ |
AzureIoTClient | 6:641a9672db08 | 649 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 650 | } |
AzureIoTClient | 6:641a9672db08 | 651 | else |
AzureIoTClient | 6:641a9672db08 | 652 | { |
AzureIoTClient | 6:641a9672db08 | 653 | const char* sasl_mechanism_name = saslmechanism_get_mechanism_name(sasl_client_io_instance->sasl_mechanism); |
AzureIoTClient | 6:641a9672db08 | 654 | if (sasl_mechanism_name == NULL) |
AzureIoTClient | 6:641a9672db08 | 655 | { |
AzureIoTClient | 6:641a9672db08 | 656 | /* Codes_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the SASL client IO state shall be switched to IO_STATE_ERROR and the on_state_changed callback shall be triggered.] */ |
AzureIoTClient | 6:641a9672db08 | 657 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 658 | } |
AzureIoTClient | 6:641a9672db08 | 659 | else |
AzureIoTClient | 6:641a9672db08 | 660 | { |
AzureIoTClient | 6:641a9672db08 | 661 | uint32_t i; |
Azure.IoT Build | 0:6ae2f7bca550 | 662 | |
AzureIoTClient | 6:641a9672db08 | 663 | for (i = 0; i < mechanisms_count; i++) |
AzureIoTClient | 6:641a9672db08 | 664 | { |
AzureIoTClient | 6:641a9672db08 | 665 | AMQP_VALUE sasl_server_mechanism; |
AzureIoTClient | 6:641a9672db08 | 666 | sasl_server_mechanism = amqpvalue_get_array_item(sasl_server_mechanisms, i); |
AzureIoTClient | 6:641a9672db08 | 667 | if (sasl_server_mechanism == NULL) |
AzureIoTClient | 6:641a9672db08 | 668 | { |
AzureIoTClient | 6:641a9672db08 | 669 | i = mechanisms_count; |
AzureIoTClient | 6:641a9672db08 | 670 | } |
AzureIoTClient | 6:641a9672db08 | 671 | else |
AzureIoTClient | 6:641a9672db08 | 672 | { |
AzureIoTClient | 6:641a9672db08 | 673 | const char* sasl_server_mechanism_name; |
AzureIoTClient | 6:641a9672db08 | 674 | if (amqpvalue_get_symbol(sasl_server_mechanism, &sasl_server_mechanism_name) != 0) |
AzureIoTClient | 6:641a9672db08 | 675 | { |
AzureIoTClient | 6:641a9672db08 | 676 | i = mechanisms_count; |
AzureIoTClient | 6:641a9672db08 | 677 | } |
AzureIoTClient | 6:641a9672db08 | 678 | else |
AzureIoTClient | 6:641a9672db08 | 679 | { |
AzureIoTClient | 6:641a9672db08 | 680 | if (strcmp(sasl_mechanism_name, sasl_server_mechanism_name) == 0) |
AzureIoTClient | 6:641a9672db08 | 681 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 682 | amqpvalue_destroy(sasl_server_mechanism); |
Azure.IoT Build | 0:6ae2f7bca550 | 683 | break; |
AzureIoTClient | 6:641a9672db08 | 684 | } |
AzureIoTClient | 6:641a9672db08 | 685 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 686 | |
Azure.IoT Build | 0:6ae2f7bca550 | 687 | amqpvalue_destroy(sasl_server_mechanism); |
Azure.IoT Build | 0:6ae2f7bca550 | 688 | } |
AzureIoTClient | 6:641a9672db08 | 689 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 690 | |
AzureIoTClient | 6:641a9672db08 | 691 | if (i == mechanisms_count) |
AzureIoTClient | 6:641a9672db08 | 692 | { |
AzureIoTClient | 6:641a9672db08 | 693 | /* Codes_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the SASL client IO state shall be switched to IO_STATE_ERROR and the on_state_changed callback shall be triggered.] */ |
AzureIoTClient | 6:641a9672db08 | 694 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 695 | } |
AzureIoTClient | 6:641a9672db08 | 696 | else |
AzureIoTClient | 6:641a9672db08 | 697 | { |
AzureIoTClient | 6:641a9672db08 | 698 | sasl_client_io_instance->sasl_client_negotiation_state = SASL_CLIENT_NEGOTIATION_MECH_RCVD; |
Azure.IoT Build | 0:6ae2f7bca550 | 699 | |
AzureIoTClient | 6:641a9672db08 | 700 | /* Codes_SRS_SASLCLIENTIO_01_035: [SASL-INIT -->] */ |
AzureIoTClient | 6:641a9672db08 | 701 | /* Codes_SRS_SASLCLIENTIO_01_033: [The partner MUST then choose one of the supported mechanisms and initiate a sasl exchange.] */ |
AzureIoTClient | 6:641a9672db08 | 702 | /* Codes_SRS_SASLCLIENTIO_01_054: [Selects the sasl mechanism and provides the initial response if needed.] */ |
AzureIoTClient | 6:641a9672db08 | 703 | if (send_sasl_init(sasl_client_io_instance, sasl_mechanism_name) != 0) |
AzureIoTClient | 6:641a9672db08 | 704 | { |
AzureIoTClient | 6:641a9672db08 | 705 | /* Codes_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the SASL client IO state shall be switched to IO_STATE_ERROR and the on_state_changed callback shall be triggered.] */ |
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 | sasl_client_io_instance->sasl_client_negotiation_state = SASL_CLIENT_NEGOTIATION_INIT_SENT; |
AzureIoTClient | 6:641a9672db08 | 711 | } |
AzureIoTClient | 6:641a9672db08 | 712 | } |
AzureIoTClient | 6:641a9672db08 | 713 | } |
AzureIoTClient | 6:641a9672db08 | 714 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 715 | |
AzureIoTClient | 6:641a9672db08 | 716 | sasl_mechanisms_destroy(sasl_mechanisms_handle); |
AzureIoTClient | 6:641a9672db08 | 717 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 718 | |
AzureIoTClient | 6:641a9672db08 | 719 | break; |
AzureIoTClient | 6:641a9672db08 | 720 | } |
AzureIoTClient | 6:641a9672db08 | 721 | } |
AzureIoTClient | 6:641a9672db08 | 722 | } |
AzureIoTClient | 6:641a9672db08 | 723 | /* Codes_SRS_SASLCLIENTIO_01_052: [Send the SASL challenge data as defined by the SASL specification.] */ |
AzureIoTClient | 6:641a9672db08 | 724 | /* Codes_SRS_SASLCLIENTIO_01_036: [<-- SASL-CHALLENGE *] */ |
AzureIoTClient | 6:641a9672db08 | 725 | /* 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 | 726 | else if (is_sasl_challenge_type_by_descriptor(descriptor)) |
AzureIoTClient | 6:641a9672db08 | 727 | { |
AzureIoTClient | 6:641a9672db08 | 728 | /* 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 | 729 | if ((sasl_client_io_instance->sasl_client_negotiation_state != SASL_CLIENT_NEGOTIATION_INIT_SENT) && |
AzureIoTClient | 6:641a9672db08 | 730 | (sasl_client_io_instance->sasl_client_negotiation_state != SASL_CLIENT_NEGOTIATION_RESPONSE_SENT)) |
AzureIoTClient | 6:641a9672db08 | 731 | { |
AzureIoTClient | 6:641a9672db08 | 732 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 733 | } |
AzureIoTClient | 6:641a9672db08 | 734 | else |
AzureIoTClient | 6:641a9672db08 | 735 | { |
AzureIoTClient | 6:641a9672db08 | 736 | SASL_CHALLENGE_HANDLE sasl_challenge_handle; |
Azure.IoT Build | 0:6ae2f7bca550 | 737 | |
AzureIoTClient | 6:641a9672db08 | 738 | if (amqpvalue_get_sasl_challenge(sasl_frame, &sasl_challenge_handle) != 0) |
AzureIoTClient | 6:641a9672db08 | 739 | { |
AzureIoTClient | 6:641a9672db08 | 740 | /* Codes_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the SASL client IO state shall be switched to IO_STATE_ERROR and the on_state_changed callback shall be triggered.] */ |
AzureIoTClient | 6:641a9672db08 | 741 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 742 | } |
AzureIoTClient | 6:641a9672db08 | 743 | else |
AzureIoTClient | 6:641a9672db08 | 744 | { |
AzureIoTClient | 6:641a9672db08 | 745 | amqp_binary challenge_binary_value; |
AzureIoTClient | 6:641a9672db08 | 746 | SASL_MECHANISM_BYTES response_bytes; |
Azure.IoT Build | 0:6ae2f7bca550 | 747 | |
AzureIoTClient | 6:641a9672db08 | 748 | /* Codes_SRS_SASLCLIENTIO_01_053: [Challenge information, a block of opaque binary data passed to the security mechanism.] */ |
AzureIoTClient | 6:641a9672db08 | 749 | if (sasl_challenge_get_challenge(sasl_challenge_handle, &challenge_binary_value) != 0) |
AzureIoTClient | 6:641a9672db08 | 750 | { |
AzureIoTClient | 6:641a9672db08 | 751 | /* Codes_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the SASL client IO state shall be switched to IO_STATE_ERROR and the on_state_changed callback shall be triggered.] */ |
AzureIoTClient | 6:641a9672db08 | 752 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 753 | } |
AzureIoTClient | 6:641a9672db08 | 754 | else |
AzureIoTClient | 6:641a9672db08 | 755 | { |
AzureIoTClient | 6:641a9672db08 | 756 | SASL_MECHANISM_BYTES challenge; |
AzureIoTClient | 6:641a9672db08 | 757 | |
AzureIoTClient | 6:641a9672db08 | 758 | challenge.bytes = challenge_binary_value.bytes; |
AzureIoTClient | 6:641a9672db08 | 759 | challenge.length = challenge_binary_value.length; |
Azure.IoT Build | 0:6ae2f7bca550 | 760 | |
AzureIoTClient | 6:641a9672db08 | 761 | /* Codes_SRS_SASLCLIENTIO_01_057: [The contents of this data are defined by the SASL security mechanism.] */ |
AzureIoTClient | 6:641a9672db08 | 762 | /* Codes_SRS_SASLCLIENTIO_01_037: [SASL-RESPONSE -->] */ |
AzureIoTClient | 6:641a9672db08 | 763 | if ((saslmechanism_challenge(sasl_client_io_instance->sasl_mechanism, &challenge, &response_bytes) != 0) || |
AzureIoTClient | 6:641a9672db08 | 764 | (send_sasl_response(sasl_client_io_instance, response_bytes) != 0)) |
AzureIoTClient | 6:641a9672db08 | 765 | { |
AzureIoTClient | 6:641a9672db08 | 766 | /* Codes_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the SASL client IO state shall be switched to IO_STATE_ERROR and the on_state_changed callback shall be triggered.] */ |
AzureIoTClient | 6:641a9672db08 | 767 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 768 | } |
AzureIoTClient | 6:641a9672db08 | 769 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 770 | |
AzureIoTClient | 6:641a9672db08 | 771 | sasl_challenge_destroy(sasl_challenge_handle); |
AzureIoTClient | 6:641a9672db08 | 772 | } |
AzureIoTClient | 6:641a9672db08 | 773 | } |
AzureIoTClient | 6:641a9672db08 | 774 | } |
AzureIoTClient | 6:641a9672db08 | 775 | /* Codes_SRS_SASLCLIENTIO_01_058: [This frame indicates the outcome of the SASL dialog.] */ |
AzureIoTClient | 6:641a9672db08 | 776 | /* Codes_SRS_SASLCLIENTIO_01_038: [<-- SASL-OUTCOME] */ |
AzureIoTClient | 6:641a9672db08 | 777 | else if (is_sasl_outcome_type_by_descriptor(descriptor)) |
AzureIoTClient | 6:641a9672db08 | 778 | { |
AzureIoTClient | 6:641a9672db08 | 779 | /* 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 | 780 | if ((sasl_client_io_instance->sasl_client_negotiation_state != SASL_CLIENT_NEGOTIATION_INIT_SENT) && |
AzureIoTClient | 6:641a9672db08 | 781 | (sasl_client_io_instance->sasl_client_negotiation_state != SASL_CLIENT_NEGOTIATION_RESPONSE_SENT)) |
AzureIoTClient | 6:641a9672db08 | 782 | { |
AzureIoTClient | 6:641a9672db08 | 783 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 784 | } |
AzureIoTClient | 6:641a9672db08 | 785 | else |
AzureIoTClient | 6:641a9672db08 | 786 | { |
AzureIoTClient | 6:641a9672db08 | 787 | SASL_OUTCOME_HANDLE sasl_outcome; |
Azure.IoT Build | 0:6ae2f7bca550 | 788 | |
AzureIoTClient | 6:641a9672db08 | 789 | sasl_client_io_instance->sasl_client_negotiation_state = SASL_CLIENT_NEGOTIATION_OUTCOME_RCVD; |
Azure.IoT Build | 0:6ae2f7bca550 | 790 | |
AzureIoTClient | 6:641a9672db08 | 791 | if (amqpvalue_get_sasl_outcome(sasl_frame, &sasl_outcome) != 0) |
AzureIoTClient | 6:641a9672db08 | 792 | { |
AzureIoTClient | 6:641a9672db08 | 793 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 794 | } |
AzureIoTClient | 6:641a9672db08 | 795 | else |
AzureIoTClient | 6:641a9672db08 | 796 | { |
AzureIoTClient | 6:641a9672db08 | 797 | sasl_code sasl_code; |
Azure.IoT Build | 0:6ae2f7bca550 | 798 | |
AzureIoTClient | 6:641a9672db08 | 799 | /* Codes_SRS_SASLCLIENTIO_01_060: [A reply-code indicating the outcome of the SASL dialog.] */ |
AzureIoTClient | 6:641a9672db08 | 800 | if (sasl_outcome_get_code(sasl_outcome, &sasl_code) != 0) |
AzureIoTClient | 6:641a9672db08 | 801 | { |
AzureIoTClient | 6:641a9672db08 | 802 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 803 | } |
AzureIoTClient | 6:641a9672db08 | 804 | else |
AzureIoTClient | 6:641a9672db08 | 805 | { |
AzureIoTClient | 6:641a9672db08 | 806 | switch (sasl_code) |
AzureIoTClient | 6:641a9672db08 | 807 | { |
AzureIoTClient | 6:641a9672db08 | 808 | default: |
AzureIoTClient | 6:641a9672db08 | 809 | case sasl_code_auth: |
AzureIoTClient | 6:641a9672db08 | 810 | /* Codes_SRS_SASLCLIENTIO_01_063: [1 Connection authentication failed due to an unspecified problem with the supplied credentials.] */ |
AzureIoTClient | 6:641a9672db08 | 811 | case sasl_code_sys: |
AzureIoTClient | 6:641a9672db08 | 812 | /* Codes_SRS_SASLCLIENTIO_01_064: [2 Connection authentication failed due to a system error.] */ |
AzureIoTClient | 6:641a9672db08 | 813 | case sasl_code_sys_perm: |
AzureIoTClient | 6:641a9672db08 | 814 | /* 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 | 815 | case sasl_code_sys_temp: |
AzureIoTClient | 6:641a9672db08 | 816 | /* Codes_SRS_SASLCLIENTIO_01_066: [4 Connection authentication failed due to a transient system error.] */ |
AzureIoTClient | 6:641a9672db08 | 817 | handle_error(sasl_client_io_instance); |
AzureIoTClient | 6:641a9672db08 | 818 | break; |
Azure.IoT Build | 0:6ae2f7bca550 | 819 | |
AzureIoTClient | 6:641a9672db08 | 820 | case sasl_code_ok: |
AzureIoTClient | 6:641a9672db08 | 821 | /* Codes_SRS_SASLCLIENTIO_01_059: [Upon successful completion of the SASL dialog the security layer has been established] */ |
AzureIoTClient | 6:641a9672db08 | 822 | /* Codes_SRS_SASLCLIENTIO_01_062: [0 Connection authentication succeeded.] */ |
AzureIoTClient | 6:641a9672db08 | 823 | sasl_client_io_instance->io_state = IO_STATE_OPEN; |
AzureIoTClient | 6:641a9672db08 | 824 | indicate_open_complete(sasl_client_io_instance, IO_OPEN_OK); |
AzureIoTClient | 6:641a9672db08 | 825 | break; |
AzureIoTClient | 6:641a9672db08 | 826 | } |
AzureIoTClient | 6:641a9672db08 | 827 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 828 | |
AzureIoTClient | 6:641a9672db08 | 829 | sasl_outcome_destroy(sasl_outcome); |
AzureIoTClient | 6:641a9672db08 | 830 | } |
AzureIoTClient | 6:641a9672db08 | 831 | } |
AzureIoTClient | 6:641a9672db08 | 832 | } |
AzureIoTClient | 6:641a9672db08 | 833 | else |
AzureIoTClient | 6:641a9672db08 | 834 | { |
AzureIoTClient | 6:641a9672db08 | 835 | LogError("Bad SASL frame"); |
AzureIoTClient | 6:641a9672db08 | 836 | } |
AzureIoTClient | 6:641a9672db08 | 837 | } |
AzureIoTClient | 6:641a9672db08 | 838 | } |
AzureIoTClient | 6:641a9672db08 | 839 | break; |
AzureIoTClient | 6:641a9672db08 | 840 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 841 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 842 | |
Azure.IoT Build | 0:6ae2f7bca550 | 843 | static void on_frame_codec_error(void* context) |
Azure.IoT Build | 0:6ae2f7bca550 | 844 | { |
AzureIoTClient | 6:641a9672db08 | 845 | SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance = (SASL_CLIENT_IO_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 846 | |
AzureIoTClient | 6:641a9672db08 | 847 | /* Codes_SRS_SASLCLIENTIO_01_122: [When on_frame_codec_error is called while in the OPENING or OPEN state the SASL client IO state shall be switched to IO_STATE_ERROR and the on_state_changed callback shall be triggered.] */ |
AzureIoTClient | 6:641a9672db08 | 848 | /* Codes_SRS_SASLCLIENTIO_01_123: [When on_frame_codec_error is called in the ERROR state nothing shall be done.] */ |
AzureIoTClient | 6:641a9672db08 | 849 | handle_error(sasl_client_io_instance); |
Azure.IoT Build | 0:6ae2f7bca550 | 850 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 851 | |
Azure.IoT Build | 0:6ae2f7bca550 | 852 | static void on_sasl_frame_codec_error(void* context) |
Azure.IoT Build | 0:6ae2f7bca550 | 853 | { |
AzureIoTClient | 6:641a9672db08 | 854 | SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance = (SASL_CLIENT_IO_INSTANCE*)context; |
Azure.IoT Build | 0:6ae2f7bca550 | 855 | |
AzureIoTClient | 6:641a9672db08 | 856 | /* Codes_SRS_SASLCLIENTIO_01_124: [**When on_sasl_frame_codec_error is called while in the OPENING or OPEN state the SASL client IO state shall be switched to IO_STATE_ERROR and the on_state_changed callback shall be triggered.] */ |
AzureIoTClient | 6:641a9672db08 | 857 | /* Codes_SRS_SASLCLIENTIO_01_125: [When on_sasl_frame_codec_error is called in the ERROR state nothing shall be done.] */ |
AzureIoTClient | 6:641a9672db08 | 858 | handle_error(sasl_client_io_instance); |
Azure.IoT Build | 0:6ae2f7bca550 | 859 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 860 | |
Azure.IoT Build | 5:ae49385aff34 | 861 | CONCRETE_IO_HANDLE saslclientio_create(void* io_create_parameters) |
Azure.IoT Build | 0:6ae2f7bca550 | 862 | { |
AzureIoTClient | 6:641a9672db08 | 863 | SASLCLIENTIO_CONFIG* sasl_client_io_config = io_create_parameters; |
AzureIoTClient | 6:641a9672db08 | 864 | SASL_CLIENT_IO_INSTANCE* result; |
Azure.IoT Build | 0:6ae2f7bca550 | 865 | |
AzureIoTClient | 6:641a9672db08 | 866 | /* Codes_SRS_SASLCLIENTIO_01_005: [If xio_create_parameters is NULL, saslclientio_create shall fail and return NULL.] */ |
AzureIoTClient | 6:641a9672db08 | 867 | if ((sasl_client_io_config == NULL) || |
AzureIoTClient | 6:641a9672db08 | 868 | /* 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 | 6:641a9672db08 | 869 | (sasl_client_io_config->underlying_io == NULL) || |
AzureIoTClient | 6:641a9672db08 | 870 | (sasl_client_io_config->sasl_mechanism == NULL)) |
AzureIoTClient | 6:641a9672db08 | 871 | { |
AzureIoTClient | 6:641a9672db08 | 872 | result = NULL; |
AzureIoTClient | 6:641a9672db08 | 873 | } |
AzureIoTClient | 6:641a9672db08 | 874 | else |
AzureIoTClient | 6:641a9672db08 | 875 | { |
AzureIoTClient | 6:641a9672db08 | 876 | result = amqpalloc_malloc(sizeof(SASL_CLIENT_IO_INSTANCE)); |
AzureIoTClient | 6:641a9672db08 | 877 | /* Codes_SRS_SASLCLIENTIO_01_006: [If memory cannot be allocated for the new instance, saslclientio_create shall fail and return NULL.] */ |
AzureIoTClient | 6:641a9672db08 | 878 | if (result != NULL) |
AzureIoTClient | 6:641a9672db08 | 879 | { |
AzureIoTClient | 6:641a9672db08 | 880 | result->underlying_io = sasl_client_io_config->underlying_io; |
AzureIoTClient | 6:641a9672db08 | 881 | if (result->underlying_io == NULL) |
AzureIoTClient | 6:641a9672db08 | 882 | { |
AzureIoTClient | 6:641a9672db08 | 883 | amqpalloc_free(result); |
AzureIoTClient | 6:641a9672db08 | 884 | result = NULL; |
AzureIoTClient | 6:641a9672db08 | 885 | } |
AzureIoTClient | 6:641a9672db08 | 886 | else |
AzureIoTClient | 6:641a9672db08 | 887 | { |
AzureIoTClient | 6:641a9672db08 | 888 | /* Codes_SRS_SASLCLIENTIO_01_089: [saslclientio_create shall create a frame_codec to be used for encoding/decoding frames bycalling frame_codec_create and passing the underlying_io as argument.] */ |
AzureIoTClient | 6:641a9672db08 | 889 | result->frame_codec = frame_codec_create(on_frame_codec_error, result); |
AzureIoTClient | 6:641a9672db08 | 890 | if (result->frame_codec == NULL) |
AzureIoTClient | 6:641a9672db08 | 891 | { |
AzureIoTClient | 6:641a9672db08 | 892 | /* Codes_SRS_SASLCLIENTIO_01_090: [If frame_codec_create fails, then saslclientio_create shall fail and return NULL.] */ |
AzureIoTClient | 6:641a9672db08 | 893 | amqpalloc_free(result); |
AzureIoTClient | 6:641a9672db08 | 894 | result = NULL; |
AzureIoTClient | 6:641a9672db08 | 895 | } |
AzureIoTClient | 6:641a9672db08 | 896 | else |
AzureIoTClient | 6:641a9672db08 | 897 | { |
AzureIoTClient | 6:641a9672db08 | 898 | /* 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 | 6:641a9672db08 | 899 | result->sasl_frame_codec = sasl_frame_codec_create(result->frame_codec, sasl_frame_received_callback, on_sasl_frame_codec_error, result); |
AzureIoTClient | 6:641a9672db08 | 900 | if (result->sasl_frame_codec == NULL) |
AzureIoTClient | 6:641a9672db08 | 901 | { |
AzureIoTClient | 6:641a9672db08 | 902 | frame_codec_destroy(result->frame_codec); |
AzureIoTClient | 6:641a9672db08 | 903 | amqpalloc_free(result); |
AzureIoTClient | 6:641a9672db08 | 904 | result = NULL; |
AzureIoTClient | 6:641a9672db08 | 905 | } |
AzureIoTClient | 6:641a9672db08 | 906 | else |
AzureIoTClient | 6:641a9672db08 | 907 | { |
AzureIoTClient | 6:641a9672db08 | 908 | /* Codes_SRS_SASLCLIENTIO_01_004: [saslclientio_create shall return on success a non-NULL handle to a new SASL client IO instance.] */ |
AzureIoTClient | 6:641a9672db08 | 909 | result->on_bytes_received = NULL; |
AzureIoTClient | 6:641a9672db08 | 910 | result->on_io_open_complete = NULL; |
AzureIoTClient | 6:641a9672db08 | 911 | result->on_io_error = NULL; |
AzureIoTClient | 6:641a9672db08 | 912 | result->on_io_close_complete = NULL; |
Azure.IoT Build | 0:6ae2f7bca550 | 913 | result->on_bytes_received_context = NULL; |
AzureIoTClient | 6:641a9672db08 | 914 | result->on_io_open_complete_context = NULL; |
AzureIoTClient | 6:641a9672db08 | 915 | result->on_io_close_complete_context = NULL; |
Azure.IoT Build | 0:6ae2f7bca550 | 916 | result->on_io_error_context = NULL; |
AzureIoTClient | 6:641a9672db08 | 917 | result->sasl_mechanism = sasl_client_io_config->sasl_mechanism; |
Azure.IoT Build | 0:6ae2f7bca550 | 918 | |
AzureIoTClient | 6:641a9672db08 | 919 | result->io_state = IO_STATE_NOT_OPEN; |
AzureIoTClient | 6:641a9672db08 | 920 | } |
AzureIoTClient | 6:641a9672db08 | 921 | } |
AzureIoTClient | 6:641a9672db08 | 922 | } |
AzureIoTClient | 6:641a9672db08 | 923 | } |
AzureIoTClient | 6:641a9672db08 | 924 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 925 | |
AzureIoTClient | 6:641a9672db08 | 926 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 927 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 928 | |
Azure.IoT Build | 0:6ae2f7bca550 | 929 | void saslclientio_destroy(CONCRETE_IO_HANDLE sasl_client_io) |
Azure.IoT Build | 0:6ae2f7bca550 | 930 | { |
AzureIoTClient | 6:641a9672db08 | 931 | if (sasl_client_io != NULL) |
AzureIoTClient | 6:641a9672db08 | 932 | { |
AzureIoTClient | 6:641a9672db08 | 933 | SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance = (SASL_CLIENT_IO_INSTANCE*)sasl_client_io; |
Azure.IoT Build | 0:6ae2f7bca550 | 934 | |
AzureIoTClient | 6:641a9672db08 | 935 | /* Codes_SRS_SASLCLIENTIO_01_007: [saslclientio_destroy shall free all resources associated with the SASL client IO handle.] */ |
AzureIoTClient | 6:641a9672db08 | 936 | /* 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 | 937 | sasl_frame_codec_destroy(sasl_client_io_instance->sasl_frame_codec); |
Azure.IoT Build | 0:6ae2f7bca550 | 938 | |
AzureIoTClient | 6:641a9672db08 | 939 | /* Codes_SRS_SASLCLIENTIO_01_091: [saslclientio_destroy shall destroy the frame_codec created in saslclientio_create by calling frame_codec_destroy.] */ |
AzureIoTClient | 6:641a9672db08 | 940 | frame_codec_destroy(sasl_client_io_instance->frame_codec); |
AzureIoTClient | 6:641a9672db08 | 941 | amqpalloc_free(sasl_client_io); |
AzureIoTClient | 6:641a9672db08 | 942 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 943 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 944 | |
Azure.IoT Build | 0:6ae2f7bca550 | 945 | int saslclientio_open(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 | 946 | { |
AzureIoTClient | 6:641a9672db08 | 947 | int result = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 948 | |
AzureIoTClient | 6:641a9672db08 | 949 | /* Codes_SRS_SASLCLIENTIO_01_011: [If any of the sasl_client_io or on_bytes_received arguments is NULL, saslclientio_open shall fail and return a non-zero value.] */ |
AzureIoTClient | 6:641a9672db08 | 950 | if ((sasl_client_io == NULL) || |
AzureIoTClient | 6:641a9672db08 | 951 | (on_bytes_received == NULL)) |
AzureIoTClient | 6:641a9672db08 | 952 | { |
AzureIoTClient | 6:641a9672db08 | 953 | result = __LINE__; |
AzureIoTClient | 6:641a9672db08 | 954 | } |
AzureIoTClient | 6:641a9672db08 | 955 | else |
AzureIoTClient | 6:641a9672db08 | 956 | { |
AzureIoTClient | 6:641a9672db08 | 957 | SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance = (SASL_CLIENT_IO_INSTANCE*)sasl_client_io; |
Azure.IoT Build | 0:6ae2f7bca550 | 958 | |
AzureIoTClient | 6:641a9672db08 | 959 | if (sasl_client_io_instance->io_state != IO_STATE_NOT_OPEN) |
AzureIoTClient | 6:641a9672db08 | 960 | { |
AzureIoTClient | 6:641a9672db08 | 961 | result = __LINE__; |
AzureIoTClient | 6:641a9672db08 | 962 | } |
AzureIoTClient | 6:641a9672db08 | 963 | else |
AzureIoTClient | 6:641a9672db08 | 964 | { |
AzureIoTClient | 6:641a9672db08 | 965 | sasl_client_io_instance->on_bytes_received = on_bytes_received; |
AzureIoTClient | 6:641a9672db08 | 966 | sasl_client_io_instance->on_io_open_complete = on_io_open_complete; |
AzureIoTClient | 6:641a9672db08 | 967 | sasl_client_io_instance->on_io_error = on_io_error; |
AzureIoTClient | 6:641a9672db08 | 968 | sasl_client_io_instance->on_bytes_received_context = on_bytes_received_context; |
Azure.IoT Build | 0:6ae2f7bca550 | 969 | sasl_client_io_instance->on_io_open_complete_context = on_io_open_complete_context; |
Azure.IoT Build | 0:6ae2f7bca550 | 970 | sasl_client_io_instance->on_io_error_context = on_io_error_context; |
AzureIoTClient | 6:641a9672db08 | 971 | sasl_client_io_instance->sasl_header_exchange_state = SASL_HEADER_EXCHANGE_IDLE; |
AzureIoTClient | 6:641a9672db08 | 972 | sasl_client_io_instance->sasl_client_negotiation_state = SASL_CLIENT_NEGOTIATION_NOT_STARTED; |
AzureIoTClient | 6:641a9672db08 | 973 | sasl_client_io_instance->header_bytes_received = 0; |
AzureIoTClient | 6:641a9672db08 | 974 | sasl_client_io_instance->io_state = IO_STATE_OPENING_UNDERLYING_IO; |
AzureIoTClient | 6:641a9672db08 | 975 | sasl_client_io_instance->is_trace_on = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 976 | |
AzureIoTClient | 6:641a9672db08 | 977 | /* Codes_SRS_SASLCLIENTIO_01_009: [saslclientio_open shall call xio_open on the underlying_io passed to saslclientio_create.] */ |
AzureIoTClient | 6:641a9672db08 | 978 | /* Codes_SRS_SASLCLIENTIO_01_013: [saslclientio_open shall pass to xio_open a callback for receiving bytes and a state changed callback for the underlying_io state changes.] */ |
AzureIoTClient | 6:641a9672db08 | 979 | 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 | 980 | { |
AzureIoTClient | 6:641a9672db08 | 981 | /* Codes_SRS_SASLCLIENTIO_01_012: [If the open of the underlying_io fails, saslclientio_open shall fail and return non-zero value.] */ |
AzureIoTClient | 6:641a9672db08 | 982 | result = __LINE__; |
AzureIoTClient | 6:641a9672db08 | 983 | } |
AzureIoTClient | 6:641a9672db08 | 984 | else |
AzureIoTClient | 6:641a9672db08 | 985 | { |
AzureIoTClient | 6:641a9672db08 | 986 | /* Codes_SRS_SASLCLIENTIO_01_010: [On success, saslclientio_open shall return 0.] */ |
AzureIoTClient | 6:641a9672db08 | 987 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 988 | } |
AzureIoTClient | 6:641a9672db08 | 989 | } |
AzureIoTClient | 6:641a9672db08 | 990 | } |
AzureIoTClient | 6:641a9672db08 | 991 | |
AzureIoTClient | 6:641a9672db08 | 992 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 993 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 994 | |
Azure.IoT Build | 0:6ae2f7bca550 | 995 | int saslclientio_close(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 | 996 | { |
AzureIoTClient | 6:641a9672db08 | 997 | int result = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 998 | |
AzureIoTClient | 6:641a9672db08 | 999 | /* Codes_SRS_SASLCLIENTIO_01_017: [If sasl_client_io is NULL, saslclientio_close shall fail and return a non-zero value.] */ |
AzureIoTClient | 6:641a9672db08 | 1000 | if (sasl_client_io == NULL) |
AzureIoTClient | 6:641a9672db08 | 1001 | { |
AzureIoTClient | 6:641a9672db08 | 1002 | result = __LINE__; |
AzureIoTClient | 6:641a9672db08 | 1003 | } |
AzureIoTClient | 6:641a9672db08 | 1004 | else |
AzureIoTClient | 6:641a9672db08 | 1005 | { |
AzureIoTClient | 6:641a9672db08 | 1006 | SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance = (SASL_CLIENT_IO_INSTANCE*)sasl_client_io; |
Azure.IoT Build | 0:6ae2f7bca550 | 1007 | |
AzureIoTClient | 6:641a9672db08 | 1008 | /* Codes_SRS_SASLCLIENTIO_01_098: [saslclientio_close shall only perform the close if the state is OPEN, OPENING or ERROR.] */ |
AzureIoTClient | 6:641a9672db08 | 1009 | if ((sasl_client_io_instance->io_state == IO_STATE_NOT_OPEN) || |
AzureIoTClient | 6:641a9672db08 | 1010 | (sasl_client_io_instance->io_state == IO_STATE_CLOSING)) |
AzureIoTClient | 6:641a9672db08 | 1011 | { |
AzureIoTClient | 6:641a9672db08 | 1012 | result = __LINE__; |
AzureIoTClient | 6:641a9672db08 | 1013 | } |
AzureIoTClient | 6:641a9672db08 | 1014 | else |
AzureIoTClient | 6:641a9672db08 | 1015 | { |
AzureIoTClient | 6:641a9672db08 | 1016 | sasl_client_io_instance->io_state = IO_STATE_CLOSING; |
Azure.IoT Build | 0:6ae2f7bca550 | 1017 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1018 | sasl_client_io_instance->on_io_close_complete = on_io_close_complete; |
Azure.IoT Build | 0:6ae2f7bca550 | 1019 | sasl_client_io_instance->on_io_close_complete_context = on_io_close_complete_context; |
Azure.IoT Build | 0:6ae2f7bca550 | 1020 | |
AzureIoTClient | 6:641a9672db08 | 1021 | /* Codes_SRS_SASLCLIENTIO_01_015: [saslclientio_close shall close the underlying io handle passed in saslclientio_create by calling xio_close.] */ |
AzureIoTClient | 6:641a9672db08 | 1022 | if (xio_close(sasl_client_io_instance->underlying_io, on_underlying_io_close_complete, sasl_client_io_instance) != 0) |
AzureIoTClient | 6:641a9672db08 | 1023 | { |
AzureIoTClient | 6:641a9672db08 | 1024 | /* Codes_SRS_SASLCLIENTIO_01_018: [If xio_close fails, then saslclientio_close shall return a non-zero value.] */ |
AzureIoTClient | 6:641a9672db08 | 1025 | result = __LINE__; |
AzureIoTClient | 6:641a9672db08 | 1026 | } |
AzureIoTClient | 6:641a9672db08 | 1027 | else |
AzureIoTClient | 6:641a9672db08 | 1028 | { |
AzureIoTClient | 6:641a9672db08 | 1029 | /* Codes_SRS_SASLCLIENTIO_01_016: [On success, saslclientio_close shall return 0.] */ |
AzureIoTClient | 6:641a9672db08 | 1030 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 1031 | } |
AzureIoTClient | 6:641a9672db08 | 1032 | } |
AzureIoTClient | 6:641a9672db08 | 1033 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1034 | |
AzureIoTClient | 6:641a9672db08 | 1035 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1036 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1037 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1038 | int saslclientio_send(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 | 1039 | { |
AzureIoTClient | 6:641a9672db08 | 1040 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1041 | |
AzureIoTClient | 6:641a9672db08 | 1042 | /* Codes_SRS_SASLCLIENTIO_01_022: [If the saslio or buffer argument is NULL, saslclientio_send shall fail and return a non-zero value.] */ |
AzureIoTClient | 6:641a9672db08 | 1043 | if ((sasl_client_io == NULL) || |
AzureIoTClient | 6:641a9672db08 | 1044 | (buffer == NULL) || |
AzureIoTClient | 6:641a9672db08 | 1045 | /* Codes_SRS_SASLCLIENTIO_01_023: [If size is 0, saslclientio_send shall fail and return a non-zero value.] */ |
AzureIoTClient | 6:641a9672db08 | 1046 | (size == 0)) |
AzureIoTClient | 6:641a9672db08 | 1047 | { |
AzureIoTClient | 6:641a9672db08 | 1048 | /* Invalid arguments */ |
AzureIoTClient | 6:641a9672db08 | 1049 | result = __LINE__; |
AzureIoTClient | 6:641a9672db08 | 1050 | } |
AzureIoTClient | 6:641a9672db08 | 1051 | else |
AzureIoTClient | 6:641a9672db08 | 1052 | { |
AzureIoTClient | 6:641a9672db08 | 1053 | SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance = (SASL_CLIENT_IO_INSTANCE*)sasl_client_io; |
Azure.IoT Build | 0:6ae2f7bca550 | 1054 | |
AzureIoTClient | 6:641a9672db08 | 1055 | /* Codes_SRS_SASLCLIENTIO_01_019: [If saslclientio_send is called while the SASL client IO state is not IO_STATE_OPEN, saslclientio_send shall fail and return a non-zero value.] */ |
AzureIoTClient | 6:641a9672db08 | 1056 | if (sasl_client_io_instance->io_state != IO_STATE_OPEN) |
AzureIoTClient | 6:641a9672db08 | 1057 | { |
AzureIoTClient | 6:641a9672db08 | 1058 | result = __LINE__; |
AzureIoTClient | 6:641a9672db08 | 1059 | } |
AzureIoTClient | 6:641a9672db08 | 1060 | else |
AzureIoTClient | 6:641a9672db08 | 1061 | { |
AzureIoTClient | 6:641a9672db08 | 1062 | /* Codes_SRS_SASLCLIENTIO_01_020: [If the SASL client IO state is IO_STATE_OPEN, saslclientio_send 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 | 1063 | if (xio_send(sasl_client_io_instance->underlying_io, buffer, size, on_send_complete, callback_context) != 0) |
AzureIoTClient | 6:641a9672db08 | 1064 | { |
AzureIoTClient | 6:641a9672db08 | 1065 | /* Codes_SRS_SASLCLIENTIO_01_024: [If the call to xio_send fails, then saslclientio_send shall fail and return a non-zero value.] */ |
AzureIoTClient | 6:641a9672db08 | 1066 | result = __LINE__; |
AzureIoTClient | 6:641a9672db08 | 1067 | } |
AzureIoTClient | 6:641a9672db08 | 1068 | else |
AzureIoTClient | 6:641a9672db08 | 1069 | { |
AzureIoTClient | 6:641a9672db08 | 1070 | /* Codes_SRS_SASLCLIENTIO_01_021: [On success, saslclientio_send shall return 0.] */ |
AzureIoTClient | 6:641a9672db08 | 1071 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 1072 | } |
AzureIoTClient | 6:641a9672db08 | 1073 | } |
AzureIoTClient | 6:641a9672db08 | 1074 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1075 | |
AzureIoTClient | 6:641a9672db08 | 1076 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1077 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1078 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1079 | void saslclientio_dowork(CONCRETE_IO_HANDLE sasl_client_io) |
Azure.IoT Build | 0:6ae2f7bca550 | 1080 | { |
AzureIoTClient | 6:641a9672db08 | 1081 | /* Codes_SRS_SASLCLIENTIO_01_026: [If the sasl_client_io argument is NULL, saslclientio_dowork shall do nothing.] */ |
AzureIoTClient | 6:641a9672db08 | 1082 | if (sasl_client_io != NULL) |
AzureIoTClient | 6:641a9672db08 | 1083 | { |
AzureIoTClient | 6:641a9672db08 | 1084 | SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance = (SASL_CLIENT_IO_INSTANCE*)sasl_client_io; |
Azure.IoT Build | 0:6ae2f7bca550 | 1085 | |
AzureIoTClient | 6:641a9672db08 | 1086 | /* Codes_SRS_SASLCLIENTIO_01_025: [saslclientio_dowork shall call the xio_dowork on the underlying_io passed in saslclientio_create.] */ |
AzureIoTClient | 6:641a9672db08 | 1087 | if (sasl_client_io_instance->io_state != IO_STATE_NOT_OPEN) |
AzureIoTClient | 6:641a9672db08 | 1088 | { |
AzureIoTClient | 6:641a9672db08 | 1089 | /* Codes_SRS_SASLCLIENTIO_01_025: [saslclientio_dowork shall call the xio_dowork on the underlying_io passed in saslclientio_create.] */ |
AzureIoTClient | 6:641a9672db08 | 1090 | xio_dowork(sasl_client_io_instance->underlying_io); |
AzureIoTClient | 6:641a9672db08 | 1091 | } |
AzureIoTClient | 6:641a9672db08 | 1092 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1093 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1094 | |
AzureIoTClient | 1:eab586236bfe | 1095 | /* Codes_SRS_SASLCLIENTIO_03_001: [saslclientio_setoption shall forward options to underlying io.]*/ |
AzureIoTClient | 1:eab586236bfe | 1096 | int saslclientio_setoption(CONCRETE_IO_HANDLE sasl_client_io, const char* optionName, const void* value) |
Azure.IoT Build | 0:6ae2f7bca550 | 1097 | { |
AzureIoTClient | 1:eab586236bfe | 1098 | int result; |
AzureIoTClient | 1:eab586236bfe | 1099 | |
AzureIoTClient | 1:eab586236bfe | 1100 | if (sasl_client_io == NULL) |
AzureIoTClient | 1:eab586236bfe | 1101 | { |
AzureIoTClient | 1:eab586236bfe | 1102 | result = __LINE__; |
AzureIoTClient | 1:eab586236bfe | 1103 | } |
AzureIoTClient | 1:eab586236bfe | 1104 | else |
AzureIoTClient | 1:eab586236bfe | 1105 | { |
AzureIoTClient | 1:eab586236bfe | 1106 | SASL_CLIENT_IO_INSTANCE* sasl_client_io_instance = (SASL_CLIENT_IO_INSTANCE*)sasl_client_io; |
AzureIoTClient | 1:eab586236bfe | 1107 | |
AzureIoTClient | 1:eab586236bfe | 1108 | if (sasl_client_io_instance->underlying_io == NULL) |
AzureIoTClient | 1:eab586236bfe | 1109 | { |
AzureIoTClient | 1:eab586236bfe | 1110 | result = __LINE__; |
AzureIoTClient | 1:eab586236bfe | 1111 | } |
AzureIoTClient | 6:641a9672db08 | 1112 | else if (strcmp("logtrace", optionName) == 0) |
AzureIoTClient | 6:641a9672db08 | 1113 | { |
AzureIoTClient | 6:641a9672db08 | 1114 | sasl_client_io_instance->is_trace_on = *((bool*)value) == true ? 1 : 0; |
AzureIoTClient | 6:641a9672db08 | 1115 | result = 0; |
AzureIoTClient | 6:641a9672db08 | 1116 | } |
AzureIoTClient | 1:eab586236bfe | 1117 | else |
AzureIoTClient | 1:eab586236bfe | 1118 | { |
AzureIoTClient | 1:eab586236bfe | 1119 | result = xio_setoption(sasl_client_io_instance->underlying_io, optionName, value); |
AzureIoTClient | 1:eab586236bfe | 1120 | } |
AzureIoTClient | 1:eab586236bfe | 1121 | } |
AzureIoTClient | 6:641a9672db08 | 1122 | return result; |
AzureIoTClient | 6:641a9672db08 | 1123 | } |
AzureIoTClient | 1:eab586236bfe | 1124 | |
AzureIoTClient | 6:641a9672db08 | 1125 | /*this function will clone an option given by name and value*/ |
AzureIoTClient | 6:641a9672db08 | 1126 | static void* saslclientio_CloneOption(const char* name, const void* value) |
AzureIoTClient | 6:641a9672db08 | 1127 | { |
AzureIoTClient | 6:641a9672db08 | 1128 | (void)(name, value); |
AzureIoTClient | 6:641a9672db08 | 1129 | return NULL; |
AzureIoTClient | 6:641a9672db08 | 1130 | } |
AzureIoTClient | 6:641a9672db08 | 1131 | |
AzureIoTClient | 6:641a9672db08 | 1132 | /*this function destroys an option previously created*/ |
AzureIoTClient | 6:641a9672db08 | 1133 | static void saslclientio_DestroyOption(const char* name, const void* value) |
AzureIoTClient | 6:641a9672db08 | 1134 | { |
AzureIoTClient | 6:641a9672db08 | 1135 | (void)(name, value); |
AzureIoTClient | 6:641a9672db08 | 1136 | } |
AzureIoTClient | 6:641a9672db08 | 1137 | |
AzureIoTClient | 6:641a9672db08 | 1138 | static OPTIONHANDLER_HANDLE saslclientio_retrieveoptions(CONCRETE_IO_HANDLE handle) |
AzureIoTClient | 6:641a9672db08 | 1139 | { |
AzureIoTClient | 6:641a9672db08 | 1140 | OPTIONHANDLER_HANDLE result; |
AzureIoTClient | 6:641a9672db08 | 1141 | (void)handle; |
AzureIoTClient | 6:641a9672db08 | 1142 | result = OptionHandler_Create(saslclientio_CloneOption, saslclientio_DestroyOption, saslclientio_setoption); |
AzureIoTClient | 6:641a9672db08 | 1143 | if (result == NULL) |
AzureIoTClient | 6:641a9672db08 | 1144 | { |
AzureIoTClient | 6:641a9672db08 | 1145 | LogError("unable to OptionHandler_Create"); |
AzureIoTClient | 6:641a9672db08 | 1146 | /*return as is*/ |
AzureIoTClient | 6:641a9672db08 | 1147 | } |
AzureIoTClient | 6:641a9672db08 | 1148 | else |
AzureIoTClient | 6:641a9672db08 | 1149 | { |
AzureIoTClient | 6:641a9672db08 | 1150 | /*insert here work to add the options to "result" handle*/ |
AzureIoTClient | 6:641a9672db08 | 1151 | } |
AzureIoTClient | 1:eab586236bfe | 1152 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 1153 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 1154 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1155 | static const IO_INTERFACE_DESCRIPTION sasl_client_io_interface_description = |
Azure.IoT Build | 0:6ae2f7bca550 | 1156 | { |
AzureIoTClient | 6:641a9672db08 | 1157 | saslclientio_retrieveoptions, |
AzureIoTClient | 6:641a9672db08 | 1158 | saslclientio_create, |
AzureIoTClient | 6:641a9672db08 | 1159 | saslclientio_destroy, |
AzureIoTClient | 6:641a9672db08 | 1160 | saslclientio_open, |
AzureIoTClient | 6:641a9672db08 | 1161 | saslclientio_close, |
AzureIoTClient | 6:641a9672db08 | 1162 | saslclientio_send, |
AzureIoTClient | 6:641a9672db08 | 1163 | saslclientio_dowork, |
Azure.IoT Build | 0:6ae2f7bca550 | 1164 | saslclientio_setoption |
Azure.IoT Build | 0:6ae2f7bca550 | 1165 | }; |
Azure.IoT Build | 0:6ae2f7bca550 | 1166 | |
Azure.IoT Build | 0:6ae2f7bca550 | 1167 | /* 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, saslclientio_close, saslclientio_send and saslclientio_dowork.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 1168 | const IO_INTERFACE_DESCRIPTION* saslclientio_get_interface_description(void) |
Azure.IoT Build | 0:6ae2f7bca550 | 1169 | { |
AzureIoTClient | 6:641a9672db08 | 1170 | return &sasl_client_io_interface_description; |
Azure.IoT Build | 0:6ae2f7bca550 | 1171 | } |