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