Microsoft Azure IoTHub client AMQP transport
Dependents: sht15_remote_monitoring RobotArmDemo iothub_client_sample_amqp iothub_client_sample_amqp ... more
This library implements the AMQP transport for Microsoft Azure IoTHub client. The code is replicated from https://github.com/Azure/azure-iot-sdks
iothubtransport_amqp_cbs_auth.c@57:56ac1346c70d, 2018-10-04 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Thu Oct 04 09:14:47 2018 -0700
- Revision:
- 57:56ac1346c70d
- Parent:
- 56:8704100b3b54
1.2.10
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AzureIoTClient | 30:20a85b733111 | 1 | // Copyright (c) Microsoft. All rights reserved. |
AzureIoTClient | 30:20a85b733111 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
AzureIoTClient | 30:20a85b733111 | 3 | |
AzureIoTClient | 30:20a85b733111 | 4 | #include <stdlib.h> |
AzureIoTClient | 53:e21e1e88460f | 5 | #include "internal/iothubtransport_amqp_cbs_auth.h" |
AzureIoTClient | 30:20a85b733111 | 6 | #include "azure_c_shared_utility/optimize_size.h" |
AzureIoTClient | 30:20a85b733111 | 7 | #include "azure_c_shared_utility/gballoc.h" |
AzureIoTClient | 56:8704100b3b54 | 8 | #include "azure_c_shared_utility/agenttime.h" |
AzureIoTClient | 30:20a85b733111 | 9 | #include "azure_c_shared_utility/xlogging.h" |
AzureIoTClient | 30:20a85b733111 | 10 | #include "azure_c_shared_utility/sastoken.h" |
AzureIoTClient | 30:20a85b733111 | 11 | |
AzureIoTClient | 30:20a85b733111 | 12 | #define RESULT_OK 0 |
AzureIoTClient | 30:20a85b733111 | 13 | #define INDEFINITE_TIME ((time_t)(-1)) |
AzureIoTClient | 30:20a85b733111 | 14 | #define SAS_TOKEN_TYPE "servicebus.windows.net:sastoken" |
AzureIoTClient | 30:20a85b733111 | 15 | #define IOTHUB_DEVICES_PATH_FMT "%s/devices/%s" |
AzureIoTClient | 54:830550fef7ea | 16 | #define IOTHUB_DEVICES_MODULE_PATH_FMT "%s/devices/%s/modules/%s" |
AzureIoTClient | 30:20a85b733111 | 17 | #define DEFAULT_CBS_REQUEST_TIMEOUT_SECS UINT32_MAX |
AzureIoTClient | 30:20a85b733111 | 18 | #define DEFAULT_SAS_TOKEN_LIFETIME_SECS 3600 |
AzureIoTClient | 30:20a85b733111 | 19 | #define DEFAULT_SAS_TOKEN_REFRESH_TIME_SECS 1800 |
AzureIoTClient | 30:20a85b733111 | 20 | |
AzureIoTClient | 56:8704100b3b54 | 21 | typedef struct AUTHENTICATION_INSTANCE_TAG |
AzureIoTClient | 30:20a85b733111 | 22 | { |
AzureIoTClient | 34:51d158b409d2 | 23 | const char* device_id; |
AzureIoTClient | 54:830550fef7ea | 24 | const char* module_id; |
AzureIoTClient | 34:51d158b409d2 | 25 | STRING_HANDLE iothub_host_fqdn; |
AzureIoTClient | 56:8704100b3b54 | 26 | |
AzureIoTClient | 34:51d158b409d2 | 27 | ON_AUTHENTICATION_STATE_CHANGED_CALLBACK on_state_changed_callback; |
AzureIoTClient | 34:51d158b409d2 | 28 | void* on_state_changed_callback_context; |
AzureIoTClient | 30:20a85b733111 | 29 | |
AzureIoTClient | 34:51d158b409d2 | 30 | ON_AUTHENTICATION_ERROR_CALLBACK on_error_callback; |
AzureIoTClient | 34:51d158b409d2 | 31 | void* on_error_callback_context; |
AzureIoTClient | 56:8704100b3b54 | 32 | |
AzureIoTClient | 34:51d158b409d2 | 33 | size_t cbs_request_timeout_secs; |
AzureIoTClient | 34:51d158b409d2 | 34 | size_t sas_token_lifetime_secs; |
AzureIoTClient | 34:51d158b409d2 | 35 | size_t sas_token_refresh_time_secs; |
AzureIoTClient | 30:20a85b733111 | 36 | |
AzureIoTClient | 34:51d158b409d2 | 37 | AUTHENTICATION_STATE state; |
AzureIoTClient | 34:51d158b409d2 | 38 | CBS_HANDLE cbs_handle; |
AzureIoTClient | 34:51d158b409d2 | 39 | |
AzureIoTClient | 34:51d158b409d2 | 40 | bool is_cbs_put_token_in_progress; |
AzureIoTClient | 34:51d158b409d2 | 41 | bool is_sas_token_refresh_in_progress; |
AzureIoTClient | 30:20a85b733111 | 42 | |
AzureIoTClient | 34:51d158b409d2 | 43 | time_t current_sas_token_put_time; |
AzureIoTClient | 30:20a85b733111 | 44 | |
AzureIoTClient | 34:51d158b409d2 | 45 | // Auth module used to generating handle authorization |
AzureIoTClient | 34:51d158b409d2 | 46 | // with either SAS Token, x509 Certs, and Device SAS Token |
AzureIoTClient | 34:51d158b409d2 | 47 | IOTHUB_AUTHORIZATION_HANDLE authorization_module; |
AzureIoTClient | 30:20a85b733111 | 48 | } AUTHENTICATION_INSTANCE; |
AzureIoTClient | 30:20a85b733111 | 49 | |
AzureIoTClient | 30:20a85b733111 | 50 | |
AzureIoTClient | 30:20a85b733111 | 51 | // Helper functions: |
AzureIoTClient | 30:20a85b733111 | 52 | |
AzureIoTClient | 30:20a85b733111 | 53 | static void update_state(AUTHENTICATION_INSTANCE* instance, AUTHENTICATION_STATE new_state) |
AzureIoTClient | 30:20a85b733111 | 54 | { |
AzureIoTClient | 34:51d158b409d2 | 55 | if (new_state != instance->state) |
AzureIoTClient | 34:51d158b409d2 | 56 | { |
AzureIoTClient | 34:51d158b409d2 | 57 | AUTHENTICATION_STATE previous_state = instance->state; |
AzureIoTClient | 34:51d158b409d2 | 58 | instance->state = new_state; |
AzureIoTClient | 30:20a85b733111 | 59 | |
AzureIoTClient | 34:51d158b409d2 | 60 | if (instance->on_state_changed_callback != NULL) |
AzureIoTClient | 34:51d158b409d2 | 61 | { |
AzureIoTClient | 34:51d158b409d2 | 62 | instance->on_state_changed_callback(instance->on_state_changed_callback_context, previous_state, new_state); |
AzureIoTClient | 34:51d158b409d2 | 63 | } |
AzureIoTClient | 34:51d158b409d2 | 64 | } |
AzureIoTClient | 30:20a85b733111 | 65 | } |
AzureIoTClient | 30:20a85b733111 | 66 | |
AzureIoTClient | 30:20a85b733111 | 67 | static void notify_error(AUTHENTICATION_INSTANCE* instance, AUTHENTICATION_ERROR_CODE error_code) |
AzureIoTClient | 30:20a85b733111 | 68 | { |
AzureIoTClient | 34:51d158b409d2 | 69 | if (instance->on_error_callback != NULL) |
AzureIoTClient | 34:51d158b409d2 | 70 | { |
AzureIoTClient | 34:51d158b409d2 | 71 | instance->on_error_callback(instance->on_error_callback_context, error_code); |
AzureIoTClient | 34:51d158b409d2 | 72 | } |
AzureIoTClient | 30:20a85b733111 | 73 | } |
AzureIoTClient | 30:20a85b733111 | 74 | |
AzureIoTClient | 30:20a85b733111 | 75 | static int verify_cbs_put_token_timeout(AUTHENTICATION_INSTANCE* instance, bool* is_timed_out) |
AzureIoTClient | 30:20a85b733111 | 76 | { |
AzureIoTClient | 34:51d158b409d2 | 77 | int result; |
AzureIoTClient | 30:20a85b733111 | 78 | |
AzureIoTClient | 34:51d158b409d2 | 79 | if (instance->current_sas_token_put_time == INDEFINITE_TIME) |
AzureIoTClient | 34:51d158b409d2 | 80 | { |
AzureIoTClient | 34:51d158b409d2 | 81 | result = __FAILURE__; |
AzureIoTClient | 34:51d158b409d2 | 82 | LogError("Failed verifying if cbs_put_token has timed out (current_sas_token_put_time is not set)"); |
AzureIoTClient | 34:51d158b409d2 | 83 | } |
AzureIoTClient | 34:51d158b409d2 | 84 | else |
AzureIoTClient | 34:51d158b409d2 | 85 | { |
AzureIoTClient | 34:51d158b409d2 | 86 | time_t current_time; |
AzureIoTClient | 30:20a85b733111 | 87 | |
AzureIoTClient | 34:51d158b409d2 | 88 | if ((current_time = get_time(NULL)) == INDEFINITE_TIME) |
AzureIoTClient | 34:51d158b409d2 | 89 | { |
AzureIoTClient | 34:51d158b409d2 | 90 | result = __FAILURE__; |
AzureIoTClient | 34:51d158b409d2 | 91 | LogError("Failed verifying if cbs_put_token has timed out (get_time failed)"); |
AzureIoTClient | 34:51d158b409d2 | 92 | } |
AzureIoTClient | 34:51d158b409d2 | 93 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_083: [authentication_do_work() shall check for authentication timeout comparing the current time since `instance->current_sas_token_put_time` to `instance->cbs_request_timeout_secs`] |
AzureIoTClient | 34:51d158b409d2 | 94 | else if ((uint32_t)get_difftime(current_time, instance->current_sas_token_put_time) >= instance->cbs_request_timeout_secs) |
AzureIoTClient | 34:51d158b409d2 | 95 | { |
AzureIoTClient | 34:51d158b409d2 | 96 | *is_timed_out = true; |
AzureIoTClient | 34:51d158b409d2 | 97 | result = RESULT_OK; |
AzureIoTClient | 34:51d158b409d2 | 98 | } |
AzureIoTClient | 34:51d158b409d2 | 99 | else |
AzureIoTClient | 34:51d158b409d2 | 100 | { |
AzureIoTClient | 34:51d158b409d2 | 101 | *is_timed_out = false; |
AzureIoTClient | 34:51d158b409d2 | 102 | result = RESULT_OK; |
AzureIoTClient | 34:51d158b409d2 | 103 | } |
AzureIoTClient | 34:51d158b409d2 | 104 | } |
AzureIoTClient | 30:20a85b733111 | 105 | |
AzureIoTClient | 34:51d158b409d2 | 106 | return result; |
AzureIoTClient | 30:20a85b733111 | 107 | } |
AzureIoTClient | 30:20a85b733111 | 108 | |
AzureIoTClient | 30:20a85b733111 | 109 | static int verify_sas_token_refresh_timeout(AUTHENTICATION_INSTANCE* instance, bool* is_timed_out) |
AzureIoTClient | 30:20a85b733111 | 110 | { |
AzureIoTClient | 34:51d158b409d2 | 111 | int result; |
AzureIoTClient | 30:20a85b733111 | 112 | |
AzureIoTClient | 34:51d158b409d2 | 113 | if (instance->current_sas_token_put_time == INDEFINITE_TIME) |
AzureIoTClient | 34:51d158b409d2 | 114 | { |
AzureIoTClient | 34:51d158b409d2 | 115 | result = __FAILURE__; |
AzureIoTClient | 34:51d158b409d2 | 116 | LogError("Failed verifying if SAS token refresh timed out (current_sas_token_put_time is not set)"); |
AzureIoTClient | 34:51d158b409d2 | 117 | } |
AzureIoTClient | 30:20a85b733111 | 118 | else |
AzureIoTClient | 30:20a85b733111 | 119 | { |
AzureIoTClient | 34:51d158b409d2 | 120 | time_t current_time; |
AzureIoTClient | 34:51d158b409d2 | 121 | |
AzureIoTClient | 34:51d158b409d2 | 122 | if ((current_time = get_time(NULL)) == INDEFINITE_TIME) |
AzureIoTClient | 30:20a85b733111 | 123 | { |
AzureIoTClient | 34:51d158b409d2 | 124 | result = __FAILURE__; |
AzureIoTClient | 34:51d158b409d2 | 125 | LogError("Failed verifying if SAS token refresh timed out (get_time failed)"); |
AzureIoTClient | 34:51d158b409d2 | 126 | } |
AzureIoTClient | 34:51d158b409d2 | 127 | else if ((uint32_t)get_difftime(current_time, instance->current_sas_token_put_time) >= instance->sas_token_refresh_time_secs) |
AzureIoTClient | 34:51d158b409d2 | 128 | { |
AzureIoTClient | 34:51d158b409d2 | 129 | *is_timed_out = true; |
AzureIoTClient | 34:51d158b409d2 | 130 | result = RESULT_OK; |
AzureIoTClient | 34:51d158b409d2 | 131 | } |
AzureIoTClient | 34:51d158b409d2 | 132 | else |
AzureIoTClient | 34:51d158b409d2 | 133 | { |
AzureIoTClient | 34:51d158b409d2 | 134 | *is_timed_out = false; |
AzureIoTClient | 34:51d158b409d2 | 135 | result = RESULT_OK; |
AzureIoTClient | 30:20a85b733111 | 136 | } |
AzureIoTClient | 30:20a85b733111 | 137 | } |
AzureIoTClient | 30:20a85b733111 | 138 | |
AzureIoTClient | 34:51d158b409d2 | 139 | return result; |
AzureIoTClient | 30:20a85b733111 | 140 | } |
AzureIoTClient | 30:20a85b733111 | 141 | |
AzureIoTClient | 54:830550fef7ea | 142 | static STRING_HANDLE create_device_and_module_path(STRING_HANDLE iothub_host_fqdn, const char* device_id, const char* module_id) |
AzureIoTClient | 30:20a85b733111 | 143 | { |
AzureIoTClient | 54:830550fef7ea | 144 | STRING_HANDLE devices_and_modules_path; |
AzureIoTClient | 54:830550fef7ea | 145 | |
AzureIoTClient | 56:8704100b3b54 | 146 | if (module_id == NULL) |
AzureIoTClient | 34:51d158b409d2 | 147 | { |
AzureIoTClient | 54:830550fef7ea | 148 | if ((devices_and_modules_path = STRING_construct_sprintf(IOTHUB_DEVICES_PATH_FMT, STRING_c_str(iothub_host_fqdn), device_id)) == NULL) |
AzureIoTClient | 54:830550fef7ea | 149 | { |
AzureIoTClient | 54:830550fef7ea | 150 | LogError("Failed creating devices_and_modules_path (STRING_new failed)"); |
AzureIoTClient | 54:830550fef7ea | 151 | } |
AzureIoTClient | 34:51d158b409d2 | 152 | } |
AzureIoTClient | 54:830550fef7ea | 153 | else |
AzureIoTClient | 54:830550fef7ea | 154 | { |
AzureIoTClient | 54:830550fef7ea | 155 | if ((devices_and_modules_path = STRING_construct_sprintf(IOTHUB_DEVICES_MODULE_PATH_FMT, STRING_c_str(iothub_host_fqdn), device_id, module_id)) == NULL) |
AzureIoTClient | 54:830550fef7ea | 156 | { |
AzureIoTClient | 54:830550fef7ea | 157 | LogError("Failed creating devices_and_modules_path (STRING_new failed)"); |
AzureIoTClient | 54:830550fef7ea | 158 | } |
AzureIoTClient | 54:830550fef7ea | 159 | } |
AzureIoTClient | 54:830550fef7ea | 160 | return devices_and_modules_path; |
AzureIoTClient | 30:20a85b733111 | 161 | } |
AzureIoTClient | 30:20a85b733111 | 162 | |
AzureIoTClient | 30:20a85b733111 | 163 | static void on_cbs_put_token_complete_callback(void* context, CBS_OPERATION_RESULT operation_result, unsigned int status_code, const char* status_description) |
AzureIoTClient | 30:20a85b733111 | 164 | { |
AzureIoTClient | 30:20a85b733111 | 165 | #ifdef NO_LOGGING |
AzureIoTClient | 34:51d158b409d2 | 166 | UNUSED(status_code); |
AzureIoTClient | 34:51d158b409d2 | 167 | UNUSED(status_description); |
AzureIoTClient | 30:20a85b733111 | 168 | #endif |
AzureIoTClient | 34:51d158b409d2 | 169 | AUTHENTICATION_INSTANCE* instance = (AUTHENTICATION_INSTANCE*)context; |
AzureIoTClient | 30:20a85b733111 | 170 | |
AzureIoTClient | 34:51d158b409d2 | 171 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_095: [`instance->is_sas_token_refresh_in_progress` and `instance->is_cbs_put_token_in_progress` shall be set to FALSE] |
AzureIoTClient | 34:51d158b409d2 | 172 | instance->is_cbs_put_token_in_progress = false; |
AzureIoTClient | 30:20a85b733111 | 173 | |
AzureIoTClient | 34:51d158b409d2 | 174 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_091: [If `result` is CBS_OPERATION_RESULT_OK `instance->state` shall be set to AUTHENTICATION_STATE_STARTED and `instance->on_state_changed_callback` invoked] |
AzureIoTClient | 34:51d158b409d2 | 175 | if (operation_result == CBS_OPERATION_RESULT_OK) |
AzureIoTClient | 34:51d158b409d2 | 176 | { |
AzureIoTClient | 34:51d158b409d2 | 177 | update_state(instance, AUTHENTICATION_STATE_STARTED); |
AzureIoTClient | 34:51d158b409d2 | 178 | } |
AzureIoTClient | 34:51d158b409d2 | 179 | else |
AzureIoTClient | 34:51d158b409d2 | 180 | { |
AzureIoTClient | 34:51d158b409d2 | 181 | LogError("CBS reported status code %u, error: '%s' for put-token operation for device '%s'", status_code, status_description, instance->device_id); |
AzureIoTClient | 30:20a85b733111 | 182 | |
AzureIoTClient | 34:51d158b409d2 | 183 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_092: [If `result` is not CBS_OPERATION_RESULT_OK `instance->state` shall be set to AUTHENTICATION_STATE_ERROR and `instance->on_state_changed_callback` invoked] |
AzureIoTClient | 34:51d158b409d2 | 184 | update_state(instance, AUTHENTICATION_STATE_ERROR); |
AzureIoTClient | 30:20a85b733111 | 185 | |
AzureIoTClient | 34:51d158b409d2 | 186 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_094: [If `result` is not CBS_OPERATION_RESULT_OK and `instance->is_sas_token_refresh_in_progress` is TRUE, `instance->on_error_callback`shall be invoked with AUTHENTICATION_ERROR_SAS_REFRESH_FAILED] |
AzureIoTClient | 34:51d158b409d2 | 187 | if (instance->is_sas_token_refresh_in_progress) |
AzureIoTClient | 34:51d158b409d2 | 188 | { |
AzureIoTClient | 34:51d158b409d2 | 189 | notify_error(instance, AUTHENTICATION_ERROR_SAS_REFRESH_FAILED); |
AzureIoTClient | 34:51d158b409d2 | 190 | } |
AzureIoTClient | 34:51d158b409d2 | 191 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_093: [If `result` is not CBS_OPERATION_RESULT_OK and `instance->is_sas_token_refresh_in_progress` is FALSE, `instance->on_error_callback`shall be invoked with AUTHENTICATION_ERROR_AUTH_FAILED] |
AzureIoTClient | 34:51d158b409d2 | 192 | else |
AzureIoTClient | 34:51d158b409d2 | 193 | { |
AzureIoTClient | 34:51d158b409d2 | 194 | notify_error(instance, AUTHENTICATION_ERROR_AUTH_FAILED); |
AzureIoTClient | 34:51d158b409d2 | 195 | } |
AzureIoTClient | 34:51d158b409d2 | 196 | } |
AzureIoTClient | 30:20a85b733111 | 197 | |
AzureIoTClient | 34:51d158b409d2 | 198 | instance->is_sas_token_refresh_in_progress = false; |
AzureIoTClient | 30:20a85b733111 | 199 | } |
AzureIoTClient | 30:20a85b733111 | 200 | |
AzureIoTClient | 54:830550fef7ea | 201 | static int put_SAS_token_to_cbs(AUTHENTICATION_INSTANCE* instance, STRING_HANDLE cbs_audience, const char* sas_token) |
AzureIoTClient | 30:20a85b733111 | 202 | { |
AzureIoTClient | 34:51d158b409d2 | 203 | int result; |
AzureIoTClient | 30:20a85b733111 | 204 | |
AzureIoTClient | 34:51d158b409d2 | 205 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_043: [authentication_do_work() shall set `instance->is_cbs_put_token_in_progress` to TRUE] |
AzureIoTClient | 34:51d158b409d2 | 206 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_057: [authentication_do_work() shall set `instance->is_cbs_put_token_in_progress` to TRUE] |
AzureIoTClient | 34:51d158b409d2 | 207 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_075: [authentication_do_work() shall set `instance->is_cbs_put_token_in_progress` to TRUE] |
AzureIoTClient | 34:51d158b409d2 | 208 | instance->is_cbs_put_token_in_progress = true; |
AzureIoTClient | 30:20a85b733111 | 209 | |
AzureIoTClient | 54:830550fef7ea | 210 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_046: [The SAS token provided shall be sent to CBS using cbs_put_token(), using `servicebus.windows.net:sastoken` as token type, `devices_and_modules_path` as audience and passing on_cbs_put_token_complete_callback] |
AzureIoTClient | 54:830550fef7ea | 211 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_058: [The SAS token shall be sent to CBS using cbs_put_token(), using `servicebus.windows.net:sastoken` as token type, `devices_and_modules_path` as audience and passing on_cbs_put_token_complete_callback] |
AzureIoTClient | 54:830550fef7ea | 212 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_076: [The SAS token shall be sent to CBS using cbs_put_token(), using `servicebus.windows.net:sastoken` as token type, `devices_and_modules_path` as audience and passing on_cbs_put_token_complete_callback] |
AzureIoTClient | 30:20a85b733111 | 213 | const char* cbs_audience_c_str = STRING_c_str(cbs_audience); |
AzureIoTClient | 34:51d158b409d2 | 214 | if (cbs_put_token_async(instance->cbs_handle, SAS_TOKEN_TYPE, cbs_audience_c_str, sas_token, on_cbs_put_token_complete_callback, instance) != RESULT_OK) |
AzureIoTClient | 34:51d158b409d2 | 215 | { |
AzureIoTClient | 54:830550fef7ea | 216 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_048: [If cbs_put_token() failed, authentication_do_work() shall set `instance->is_cbs_put_token_in_progress` to FALSE, destroy `devices_and_modules_path` and return] |
AzureIoTClient | 34:51d158b409d2 | 217 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_060: [If cbs_put_token() fails, `instance->is_cbs_put_token_in_progress` shall be set to FALSE] |
AzureIoTClient | 34:51d158b409d2 | 218 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_078: [If cbs_put_token() fails, `instance->is_cbs_put_token_in_progress` shall be set to FALSE] |
AzureIoTClient | 34:51d158b409d2 | 219 | instance->is_cbs_put_token_in_progress = false; |
AzureIoTClient | 34:51d158b409d2 | 220 | result = __FAILURE__; |
AzureIoTClient | 34:51d158b409d2 | 221 | LogError("Failed putting SAS token to CBS for device '%s' (cbs_put_token failed)", instance->device_id); |
AzureIoTClient | 34:51d158b409d2 | 222 | } |
AzureIoTClient | 34:51d158b409d2 | 223 | else |
AzureIoTClient | 34:51d158b409d2 | 224 | { |
AzureIoTClient | 34:51d158b409d2 | 225 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_047: [If cbs_put_token() succeeds, authentication_do_work() shall set `instance->current_sas_token_put_time` with current time] |
AzureIoTClient | 34:51d158b409d2 | 226 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_059: [If cbs_put_token() succeeds, authentication_do_work() shall set `instance->current_sas_token_put_time` with current time] |
AzureIoTClient | 34:51d158b409d2 | 227 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_077: [If cbs_put_token() succeeds, authentication_do_work() shall set `instance->current_sas_token_put_time` with the current time] |
AzureIoTClient | 34:51d158b409d2 | 228 | time_t current_time; |
AzureIoTClient | 30:20a85b733111 | 229 | |
AzureIoTClient | 34:51d158b409d2 | 230 | if ((current_time = get_time(NULL)) == INDEFINITE_TIME) |
AzureIoTClient | 34:51d158b409d2 | 231 | { |
AzureIoTClient | 34:51d158b409d2 | 232 | LogError("Failed setting current_sas_token_put_time for device '%s' (get_time() failed)", instance->device_id); |
AzureIoTClient | 34:51d158b409d2 | 233 | } |
AzureIoTClient | 30:20a85b733111 | 234 | |
AzureIoTClient | 34:51d158b409d2 | 235 | instance->current_sas_token_put_time = current_time; // If it failed, fear not. `current_sas_token_put_time` shall be checked for INDEFINITE_TIME wherever it is used. |
AzureIoTClient | 30:20a85b733111 | 236 | |
AzureIoTClient | 34:51d158b409d2 | 237 | result = RESULT_OK; |
AzureIoTClient | 34:51d158b409d2 | 238 | } |
AzureIoTClient | 30:20a85b733111 | 239 | |
AzureIoTClient | 34:51d158b409d2 | 240 | return result; |
AzureIoTClient | 30:20a85b733111 | 241 | } |
AzureIoTClient | 30:20a85b733111 | 242 | |
AzureIoTClient | 34:51d158b409d2 | 243 | static int create_and_put_SAS_token_to_cbs(AUTHENTICATION_INSTANCE* instance) |
AzureIoTClient | 30:20a85b733111 | 244 | { |
AzureIoTClient | 34:51d158b409d2 | 245 | int result; |
AzureIoTClient | 34:51d158b409d2 | 246 | char* sas_token; |
AzureIoTClient | 54:830550fef7ea | 247 | STRING_HANDLE device_and_module_path; |
AzureIoTClient | 30:20a85b733111 | 248 | |
AzureIoTClient | 54:830550fef7ea | 249 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_053: [A STRING_HANDLE, referred to as `devices_and_modules_path`, shall be created from: iothub_host_fqdn + "/devices/" + device_id (+ "/modules/" + module_id if a module)] |
AzureIoTClient | 54:830550fef7ea | 250 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_071: [A STRING_HANDLE, referred to as `devices_and_modules_path`, shall be created from: iothub_host_fqdn + "/devices/" + device_id (+ "/modules/" + module_id if a module)] |
AzureIoTClient | 54:830550fef7ea | 251 | if ((device_and_module_path = create_device_and_module_path(instance->iothub_host_fqdn, instance->device_id, instance->module_id)) == NULL) |
AzureIoTClient | 34:51d158b409d2 | 252 | { |
AzureIoTClient | 54:830550fef7ea | 253 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_054: [If `devices_and_modules_path` failed to be created, authentication_do_work() shall fail and return] |
AzureIoTClient | 54:830550fef7ea | 254 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_072: [If `devices_and_modules_path` failed to be created, authentication_do_work() shall fail and return] |
AzureIoTClient | 34:51d158b409d2 | 255 | result = __FAILURE__; |
AzureIoTClient | 34:51d158b409d2 | 256 | sas_token = NULL; |
AzureIoTClient | 54:830550fef7ea | 257 | LogError("Failed creating a SAS token (create_device_and_module_path() failed)"); |
AzureIoTClient | 34:51d158b409d2 | 258 | } |
AzureIoTClient | 34:51d158b409d2 | 259 | else |
AzureIoTClient | 34:51d158b409d2 | 260 | { |
AzureIoTClient | 34:51d158b409d2 | 261 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_07_001: [ authentication_do_work() shall determine what credential type is used SAS_TOKEN or DEVICE_KEY by calling IoTHubClient_Auth_Get_Credential_Type ] */ |
AzureIoTClient | 34:51d158b409d2 | 262 | IOTHUB_CREDENTIAL_TYPE cred_type = IoTHubClient_Auth_Get_Credential_Type(instance->authorization_module); |
AzureIoTClient | 41:71c01aa3df1a | 263 | if (cred_type == IOTHUB_CREDENTIAL_TYPE_DEVICE_KEY || cred_type == IOTHUB_CREDENTIAL_TYPE_DEVICE_AUTH) |
AzureIoTClient | 34:51d158b409d2 | 264 | { |
AzureIoTClient | 42:c2eaa912a28c | 265 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_049: [authentication_do_work() shall create a SAS token using IoTHubClient_Auth_Get_SasToken, unless it has failed previously] */ |
AzureIoTClient | 54:830550fef7ea | 266 | sas_token = IoTHubClient_Auth_Get_SasToken(instance->authorization_module, STRING_c_str(device_and_module_path), instance->sas_token_lifetime_secs, NULL); |
AzureIoTClient | 42:c2eaa912a28c | 267 | if (sas_token == NULL) |
AzureIoTClient | 34:51d158b409d2 | 268 | { |
AzureIoTClient | 42:c2eaa912a28c | 269 | LogError("failure getting sas token."); |
AzureIoTClient | 34:51d158b409d2 | 270 | result = __FAILURE__; |
AzureIoTClient | 34:51d158b409d2 | 271 | } |
AzureIoTClient | 34:51d158b409d2 | 272 | else |
AzureIoTClient | 34:51d158b409d2 | 273 | { |
AzureIoTClient | 42:c2eaa912a28c | 274 | result = RESULT_OK; |
AzureIoTClient | 34:51d158b409d2 | 275 | } |
AzureIoTClient | 34:51d158b409d2 | 276 | } |
AzureIoTClient | 34:51d158b409d2 | 277 | else if (cred_type == IOTHUB_CREDENTIAL_TYPE_SAS_TOKEN) |
AzureIoTClient | 34:51d158b409d2 | 278 | { |
AzureIoTClient | 34:51d158b409d2 | 279 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_07_002: [ If credential Type is SAS_TOKEN authentication_do_work() shall validate the sas_token, and fail if it's not valid. ] */ |
AzureIoTClient | 34:51d158b409d2 | 280 | SAS_TOKEN_STATUS token_status = IoTHubClient_Auth_Is_SasToken_Valid(instance->authorization_module); |
AzureIoTClient | 34:51d158b409d2 | 281 | if (token_status == SAS_TOKEN_STATUS_INVALID) |
AzureIoTClient | 34:51d158b409d2 | 282 | { |
AzureIoTClient | 34:51d158b409d2 | 283 | LogError("sas token is invalid."); |
AzureIoTClient | 34:51d158b409d2 | 284 | sas_token = NULL; |
AzureIoTClient | 34:51d158b409d2 | 285 | result = __FAILURE__; |
AzureIoTClient | 34:51d158b409d2 | 286 | } |
AzureIoTClient | 34:51d158b409d2 | 287 | else if (token_status == SAS_TOKEN_STATUS_FAILED) |
AzureIoTClient | 34:51d158b409d2 | 288 | { |
AzureIoTClient | 34:51d158b409d2 | 289 | LogError("testing Sas Token failed."); |
AzureIoTClient | 34:51d158b409d2 | 290 | sas_token = NULL; |
AzureIoTClient | 34:51d158b409d2 | 291 | result = __FAILURE__; |
AzureIoTClient | 34:51d158b409d2 | 292 | } |
AzureIoTClient | 34:51d158b409d2 | 293 | else |
AzureIoTClient | 34:51d158b409d2 | 294 | { |
AzureIoTClient | 34:51d158b409d2 | 295 | /* Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_049: [authentication_do_work() shall create a SAS token using IoTHubClient_Auth_Get_SasToken, unless it has failed previously] */ |
AzureIoTClient | 53:e21e1e88460f | 296 | sas_token = IoTHubClient_Auth_Get_SasToken(instance->authorization_module, NULL, 0, NULL); |
AzureIoTClient | 34:51d158b409d2 | 297 | if (sas_token == NULL) |
AzureIoTClient | 34:51d158b409d2 | 298 | { |
AzureIoTClient | 34:51d158b409d2 | 299 | LogError("failure getting sas Token."); |
AzureIoTClient | 34:51d158b409d2 | 300 | result = __FAILURE__; |
AzureIoTClient | 34:51d158b409d2 | 301 | } |
AzureIoTClient | 34:51d158b409d2 | 302 | else |
AzureIoTClient | 34:51d158b409d2 | 303 | { |
AzureIoTClient | 34:51d158b409d2 | 304 | result = RESULT_OK; |
AzureIoTClient | 34:51d158b409d2 | 305 | } |
AzureIoTClient | 34:51d158b409d2 | 306 | } |
AzureIoTClient | 34:51d158b409d2 | 307 | } |
AzureIoTClient | 41:71c01aa3df1a | 308 | else if (cred_type == IOTHUB_CREDENTIAL_TYPE_X509 || cred_type == IOTHUB_CREDENTIAL_TYPE_X509_ECC) |
AzureIoTClient | 34:51d158b409d2 | 309 | { |
AzureIoTClient | 34:51d158b409d2 | 310 | sas_token = NULL; |
AzureIoTClient | 34:51d158b409d2 | 311 | result = RESULT_OK; |
AzureIoTClient | 34:51d158b409d2 | 312 | } |
AzureIoTClient | 34:51d158b409d2 | 313 | else |
AzureIoTClient | 34:51d158b409d2 | 314 | { |
AzureIoTClient | 34:51d158b409d2 | 315 | LogError("failure unknown credential type found."); |
AzureIoTClient | 34:51d158b409d2 | 316 | sas_token = NULL; |
AzureIoTClient | 34:51d158b409d2 | 317 | result = __FAILURE__; |
AzureIoTClient | 34:51d158b409d2 | 318 | } |
AzureIoTClient | 30:20a85b733111 | 319 | |
AzureIoTClient | 30:20a85b733111 | 320 | |
AzureIoTClient | 34:51d158b409d2 | 321 | if (sas_token != NULL) |
AzureIoTClient | 34:51d158b409d2 | 322 | { |
AzureIoTClient | 54:830550fef7ea | 323 | if (put_SAS_token_to_cbs(instance, device_and_module_path, sas_token) != RESULT_OK) |
AzureIoTClient | 34:51d158b409d2 | 324 | { |
AzureIoTClient | 34:51d158b409d2 | 325 | result = __FAILURE__; |
AzureIoTClient | 34:51d158b409d2 | 326 | LogError("Failed putting SAS token to CBS"); |
AzureIoTClient | 34:51d158b409d2 | 327 | } |
AzureIoTClient | 34:51d158b409d2 | 328 | else |
AzureIoTClient | 34:51d158b409d2 | 329 | { |
AzureIoTClient | 34:51d158b409d2 | 330 | result = RESULT_OK; |
AzureIoTClient | 34:51d158b409d2 | 331 | } |
AzureIoTClient | 34:51d158b409d2 | 332 | free(sas_token); |
AzureIoTClient | 34:51d158b409d2 | 333 | } |
AzureIoTClient | 34:51d158b409d2 | 334 | |
AzureIoTClient | 54:830550fef7ea | 335 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_081: [authentication_do_work() shall free the memory it allocated for `devices_and_modules_path`, `sasTokenKeyName` and SAS token] |
AzureIoTClient | 54:830550fef7ea | 336 | STRING_delete(device_and_module_path); |
AzureIoTClient | 34:51d158b409d2 | 337 | } |
AzureIoTClient | 34:51d158b409d2 | 338 | return result; |
AzureIoTClient | 30:20a85b733111 | 339 | } |
AzureIoTClient | 30:20a85b733111 | 340 | |
AzureIoTClient | 30:20a85b733111 | 341 | // ---------- Set/Retrieve Options Helpers ----------// |
AzureIoTClient | 30:20a85b733111 | 342 | static void* authentication_clone_option(const char* name, const void* value) |
AzureIoTClient | 30:20a85b733111 | 343 | { |
AzureIoTClient | 34:51d158b409d2 | 344 | void* result; |
AzureIoTClient | 30:20a85b733111 | 345 | |
AzureIoTClient | 34:51d158b409d2 | 346 | if (name == NULL) |
AzureIoTClient | 34:51d158b409d2 | 347 | { |
AzureIoTClient | 56:8704100b3b54 | 348 | LogError("Failed to clone authentication option (name is NULL)"); |
AzureIoTClient | 34:51d158b409d2 | 349 | result = NULL; |
AzureIoTClient | 34:51d158b409d2 | 350 | } |
AzureIoTClient | 34:51d158b409d2 | 351 | else if (value == NULL) |
AzureIoTClient | 34:51d158b409d2 | 352 | { |
AzureIoTClient | 34:51d158b409d2 | 353 | LogError("Failed to clone authentication option (value is NULL)"); |
AzureIoTClient | 34:51d158b409d2 | 354 | result = NULL; |
AzureIoTClient | 34:51d158b409d2 | 355 | } |
AzureIoTClient | 34:51d158b409d2 | 356 | else |
AzureIoTClient | 34:51d158b409d2 | 357 | { |
AzureIoTClient | 34:51d158b409d2 | 358 | if (strcmp(AUTHENTICATION_OPTION_CBS_REQUEST_TIMEOUT_SECS, name) == 0 || |
AzureIoTClient | 34:51d158b409d2 | 359 | strcmp(AUTHENTICATION_OPTION_SAS_TOKEN_REFRESH_TIME_SECS, name) == 0 || |
AzureIoTClient | 34:51d158b409d2 | 360 | strcmp(AUTHENTICATION_OPTION_SAS_TOKEN_LIFETIME_SECS, name) == 0 || |
AzureIoTClient | 34:51d158b409d2 | 361 | strcmp(AUTHENTICATION_OPTION_SAVED_OPTIONS, name) == 0) |
AzureIoTClient | 34:51d158b409d2 | 362 | { |
AzureIoTClient | 34:51d158b409d2 | 363 | result = (void*)value; |
AzureIoTClient | 34:51d158b409d2 | 364 | } |
AzureIoTClient | 34:51d158b409d2 | 365 | else |
AzureIoTClient | 34:51d158b409d2 | 366 | { |
AzureIoTClient | 34:51d158b409d2 | 367 | LogError("Failed to clone authentication option (option with name '%s' is not suppported)", name); |
AzureIoTClient | 34:51d158b409d2 | 368 | result = NULL; |
AzureIoTClient | 34:51d158b409d2 | 369 | } |
AzureIoTClient | 34:51d158b409d2 | 370 | } |
AzureIoTClient | 30:20a85b733111 | 371 | |
AzureIoTClient | 34:51d158b409d2 | 372 | return result; |
AzureIoTClient | 30:20a85b733111 | 373 | } |
AzureIoTClient | 30:20a85b733111 | 374 | |
AzureIoTClient | 30:20a85b733111 | 375 | static void authentication_destroy_option(const char* name, const void* value) |
AzureIoTClient | 30:20a85b733111 | 376 | { |
AzureIoTClient | 34:51d158b409d2 | 377 | if (name == NULL) |
AzureIoTClient | 34:51d158b409d2 | 378 | { |
AzureIoTClient | 34:51d158b409d2 | 379 | LogError("Failed to destroy authentication option (name is NULL)"); |
AzureIoTClient | 34:51d158b409d2 | 380 | } |
AzureIoTClient | 34:51d158b409d2 | 381 | else if (value == NULL) |
AzureIoTClient | 34:51d158b409d2 | 382 | { |
AzureIoTClient | 34:51d158b409d2 | 383 | LogError("Failed to destroy authentication option (value is NULL)"); |
AzureIoTClient | 34:51d158b409d2 | 384 | } |
AzureIoTClient | 34:51d158b409d2 | 385 | else |
AzureIoTClient | 34:51d158b409d2 | 386 | { |
AzureIoTClient | 34:51d158b409d2 | 387 | if (strcmp(name, AUTHENTICATION_OPTION_SAVED_OPTIONS) == 0) |
AzureIoTClient | 34:51d158b409d2 | 388 | { |
AzureIoTClient | 34:51d158b409d2 | 389 | OptionHandler_Destroy((OPTIONHANDLER_HANDLE)value); |
AzureIoTClient | 34:51d158b409d2 | 390 | } |
AzureIoTClient | 34:51d158b409d2 | 391 | } |
AzureIoTClient | 30:20a85b733111 | 392 | } |
AzureIoTClient | 30:20a85b733111 | 393 | |
AzureIoTClient | 30:20a85b733111 | 394 | // Public APIs: |
AzureIoTClient | 30:20a85b733111 | 395 | int authentication_start(AUTHENTICATION_HANDLE authentication_handle, const CBS_HANDLE cbs_handle) |
AzureIoTClient | 30:20a85b733111 | 396 | { |
AzureIoTClient | 34:51d158b409d2 | 397 | int result; |
AzureIoTClient | 30:20a85b733111 | 398 | |
AzureIoTClient | 34:51d158b409d2 | 399 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_025: [If authentication_handle is NULL, authentication_start() shall fail and return __FAILURE__ as error code] |
AzureIoTClient | 34:51d158b409d2 | 400 | if (authentication_handle == NULL) |
AzureIoTClient | 34:51d158b409d2 | 401 | { |
AzureIoTClient | 34:51d158b409d2 | 402 | result = __FAILURE__; |
AzureIoTClient | 34:51d158b409d2 | 403 | LogError("authentication_start failed (authentication_handle is NULL)"); |
AzureIoTClient | 34:51d158b409d2 | 404 | } |
AzureIoTClient | 34:51d158b409d2 | 405 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_026: [If `cbs_handle` is NULL, authentication_start() shall fail and return __FAILURE__ as error code] |
AzureIoTClient | 34:51d158b409d2 | 406 | else if (cbs_handle == NULL) |
AzureIoTClient | 34:51d158b409d2 | 407 | { |
AzureIoTClient | 34:51d158b409d2 | 408 | result = __FAILURE__; |
AzureIoTClient | 34:51d158b409d2 | 409 | LogError("authentication_start failed (cbs_handle is NULL)"); |
AzureIoTClient | 34:51d158b409d2 | 410 | } |
AzureIoTClient | 34:51d158b409d2 | 411 | else |
AzureIoTClient | 34:51d158b409d2 | 412 | { |
AzureIoTClient | 34:51d158b409d2 | 413 | AUTHENTICATION_INSTANCE* instance = (AUTHENTICATION_INSTANCE*)authentication_handle; |
AzureIoTClient | 56:8704100b3b54 | 414 | |
AzureIoTClient | 34:51d158b409d2 | 415 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_027: [If authenticate state has been started already, authentication_start() shall fail and return __FAILURE__ as error code] |
AzureIoTClient | 34:51d158b409d2 | 416 | if (instance->state != AUTHENTICATION_STATE_STOPPED) |
AzureIoTClient | 34:51d158b409d2 | 417 | { |
AzureIoTClient | 34:51d158b409d2 | 418 | result = __FAILURE__; |
AzureIoTClient | 34:51d158b409d2 | 419 | LogError("authentication_start failed (messenger has already been started; current state: %d)", instance->state); |
AzureIoTClient | 34:51d158b409d2 | 420 | } |
AzureIoTClient | 34:51d158b409d2 | 421 | else |
AzureIoTClient | 34:51d158b409d2 | 422 | { |
AzureIoTClient | 34:51d158b409d2 | 423 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_028: [authentication_start() shall save `cbs_handle` on `instance->cbs_handle`] |
AzureIoTClient | 34:51d158b409d2 | 424 | instance->cbs_handle = cbs_handle; |
AzureIoTClient | 30:20a85b733111 | 425 | |
AzureIoTClient | 34:51d158b409d2 | 426 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_029: [If no failures occur, `instance->state` shall be set to AUTHENTICATION_STATE_STARTING and `instance->on_state_changed_callback` invoked] |
AzureIoTClient | 34:51d158b409d2 | 427 | update_state(instance, AUTHENTICATION_STATE_STARTING); |
AzureIoTClient | 30:20a85b733111 | 428 | |
AzureIoTClient | 34:51d158b409d2 | 429 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_030: [If no failures occur, authentication_start() shall return 0] |
AzureIoTClient | 34:51d158b409d2 | 430 | result = RESULT_OK; |
AzureIoTClient | 34:51d158b409d2 | 431 | } |
AzureIoTClient | 34:51d158b409d2 | 432 | } |
AzureIoTClient | 30:20a85b733111 | 433 | |
AzureIoTClient | 34:51d158b409d2 | 434 | return result; |
AzureIoTClient | 30:20a85b733111 | 435 | } |
AzureIoTClient | 30:20a85b733111 | 436 | |
AzureIoTClient | 30:20a85b733111 | 437 | int authentication_stop(AUTHENTICATION_HANDLE authentication_handle) |
AzureIoTClient | 30:20a85b733111 | 438 | { |
AzureIoTClient | 34:51d158b409d2 | 439 | int result; |
AzureIoTClient | 56:8704100b3b54 | 440 | |
AzureIoTClient | 34:51d158b409d2 | 441 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_031: [If `authentication_handle` is NULL, authentication_stop() shall fail and return __FAILURE__] |
AzureIoTClient | 34:51d158b409d2 | 442 | if (authentication_handle == NULL) |
AzureIoTClient | 34:51d158b409d2 | 443 | { |
AzureIoTClient | 34:51d158b409d2 | 444 | result = __FAILURE__; |
AzureIoTClient | 34:51d158b409d2 | 445 | LogError("authentication_stop failed (authentication_handle is NULL)"); |
AzureIoTClient | 34:51d158b409d2 | 446 | } |
AzureIoTClient | 34:51d158b409d2 | 447 | else |
AzureIoTClient | 34:51d158b409d2 | 448 | { |
AzureIoTClient | 34:51d158b409d2 | 449 | AUTHENTICATION_INSTANCE* instance = (AUTHENTICATION_INSTANCE*)authentication_handle; |
AzureIoTClient | 30:20a85b733111 | 450 | |
AzureIoTClient | 34:51d158b409d2 | 451 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_032: [If `instance->state` is AUTHENTICATION_STATE_STOPPED, authentication_stop() shall fail and return __FAILURE__] |
AzureIoTClient | 34:51d158b409d2 | 452 | if (instance->state == AUTHENTICATION_STATE_STOPPED) |
AzureIoTClient | 34:51d158b409d2 | 453 | { |
AzureIoTClient | 34:51d158b409d2 | 454 | result = __FAILURE__; |
AzureIoTClient | 34:51d158b409d2 | 455 | LogError("authentication_stop failed (messenger is already stopped)"); |
AzureIoTClient | 34:51d158b409d2 | 456 | } |
AzureIoTClient | 34:51d158b409d2 | 457 | else |
AzureIoTClient | 34:51d158b409d2 | 458 | { |
AzureIoTClient | 34:51d158b409d2 | 459 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_033: [`instance->cbs_handle` shall be set to NULL] |
AzureIoTClient | 34:51d158b409d2 | 460 | instance->cbs_handle = NULL; |
AzureIoTClient | 30:20a85b733111 | 461 | |
AzureIoTClient | 34:51d158b409d2 | 462 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_034: [`instance->state` shall be set to AUTHENTICATION_STATE_STOPPED and `instance->on_state_changed_callback` invoked] |
AzureIoTClient | 34:51d158b409d2 | 463 | update_state(instance, AUTHENTICATION_STATE_STOPPED); |
AzureIoTClient | 30:20a85b733111 | 464 | |
AzureIoTClient | 34:51d158b409d2 | 465 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_035: [authentication_stop() shall return success code 0] |
AzureIoTClient | 34:51d158b409d2 | 466 | result = RESULT_OK; |
AzureIoTClient | 34:51d158b409d2 | 467 | } |
AzureIoTClient | 34:51d158b409d2 | 468 | } |
AzureIoTClient | 30:20a85b733111 | 469 | |
AzureIoTClient | 34:51d158b409d2 | 470 | return result; |
AzureIoTClient | 30:20a85b733111 | 471 | } |
AzureIoTClient | 30:20a85b733111 | 472 | |
AzureIoTClient | 30:20a85b733111 | 473 | void authentication_destroy(AUTHENTICATION_HANDLE authentication_handle) |
AzureIoTClient | 30:20a85b733111 | 474 | { |
AzureIoTClient | 34:51d158b409d2 | 475 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_106: [If authentication_handle is NULL, authentication_destroy() shall return] |
AzureIoTClient | 34:51d158b409d2 | 476 | if (authentication_handle == NULL) |
AzureIoTClient | 34:51d158b409d2 | 477 | { |
AzureIoTClient | 34:51d158b409d2 | 478 | LogError("authentication_destroy failed (authentication_handle is NULL)"); |
AzureIoTClient | 34:51d158b409d2 | 479 | } |
AzureIoTClient | 34:51d158b409d2 | 480 | else |
AzureIoTClient | 34:51d158b409d2 | 481 | { |
AzureIoTClient | 34:51d158b409d2 | 482 | AUTHENTICATION_INSTANCE* instance = (AUTHENTICATION_INSTANCE*)authentication_handle; |
AzureIoTClient | 30:20a85b733111 | 483 | |
AzureIoTClient | 34:51d158b409d2 | 484 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_107: [If `instance->state` is AUTHENTICATION_STATE_STARTING or AUTHENTICATION_STATE_STARTED, authentication_stop() shall be invoked and its result ignored] |
AzureIoTClient | 34:51d158b409d2 | 485 | if (instance->state != AUTHENTICATION_STATE_STOPPED) |
AzureIoTClient | 34:51d158b409d2 | 486 | { |
AzureIoTClient | 34:51d158b409d2 | 487 | (void)authentication_stop(authentication_handle); |
AzureIoTClient | 34:51d158b409d2 | 488 | } |
AzureIoTClient | 34:51d158b409d2 | 489 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_108: [authentication_destroy() shall destroy all resouces used by this module] |
AzureIoTClient | 34:51d158b409d2 | 490 | if (instance->iothub_host_fqdn != NULL) |
AzureIoTClient | 34:51d158b409d2 | 491 | STRING_delete(instance->iothub_host_fqdn); |
AzureIoTClient | 34:51d158b409d2 | 492 | |
AzureIoTClient | 34:51d158b409d2 | 493 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_108: [authentication_destroy() shall destroy all resouces used by this module] |
AzureIoTClient | 34:51d158b409d2 | 494 | free(instance); |
AzureIoTClient | 34:51d158b409d2 | 495 | } |
AzureIoTClient | 30:20a85b733111 | 496 | } |
AzureIoTClient | 30:20a85b733111 | 497 | |
AzureIoTClient | 30:20a85b733111 | 498 | AUTHENTICATION_HANDLE authentication_create(const AUTHENTICATION_CONFIG* config) |
AzureIoTClient | 30:20a85b733111 | 499 | { |
AzureIoTClient | 34:51d158b409d2 | 500 | AUTHENTICATION_HANDLE result; |
AzureIoTClient | 30:20a85b733111 | 501 | |
AzureIoTClient | 34:51d158b409d2 | 502 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_001: [If parameter `config` is NULL, authentication_create() shall fail and return NULL.] |
AzureIoTClient | 34:51d158b409d2 | 503 | if (config == NULL) |
AzureIoTClient | 34:51d158b409d2 | 504 | { |
AzureIoTClient | 34:51d158b409d2 | 505 | result = NULL; |
AzureIoTClient | 34:51d158b409d2 | 506 | LogError("authentication_create failed (config is NULL)"); |
AzureIoTClient | 34:51d158b409d2 | 507 | } |
AzureIoTClient | 34:51d158b409d2 | 508 | else if (config->authorization_module == NULL) |
AzureIoTClient | 34:51d158b409d2 | 509 | { |
AzureIoTClient | 34:51d158b409d2 | 510 | result = NULL; |
AzureIoTClient | 34:51d158b409d2 | 511 | LogError("authentication_create failed (config->authorization_module is NULL)"); |
AzureIoTClient | 34:51d158b409d2 | 512 | } |
AzureIoTClient | 34:51d158b409d2 | 513 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_004: [If `config->iothub_host_fqdn` is NULL, authentication_create() shall fail and return NULL.] |
AzureIoTClient | 34:51d158b409d2 | 514 | else if (config->iothub_host_fqdn == NULL) |
AzureIoTClient | 34:51d158b409d2 | 515 | { |
AzureIoTClient | 34:51d158b409d2 | 516 | result = NULL; |
AzureIoTClient | 34:51d158b409d2 | 517 | LogError("authentication_create failed (config->iothub_host_fqdn is NULL)"); |
AzureIoTClient | 34:51d158b409d2 | 518 | } |
AzureIoTClient | 34:51d158b409d2 | 519 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_005: [If `config->on_state_changed_callback` is NULL, authentication_create() shall fail and return NULL] |
AzureIoTClient | 34:51d158b409d2 | 520 | else if (config->on_state_changed_callback == NULL) |
AzureIoTClient | 34:51d158b409d2 | 521 | { |
AzureIoTClient | 34:51d158b409d2 | 522 | result = NULL; |
AzureIoTClient | 34:51d158b409d2 | 523 | LogError("authentication_create failed (config->on_state_changed_callback is NULL)"); |
AzureIoTClient | 34:51d158b409d2 | 524 | } |
AzureIoTClient | 34:51d158b409d2 | 525 | else |
AzureIoTClient | 34:51d158b409d2 | 526 | { |
AzureIoTClient | 34:51d158b409d2 | 527 | AUTHENTICATION_INSTANCE* instance; |
AzureIoTClient | 30:20a85b733111 | 528 | |
AzureIoTClient | 34:51d158b409d2 | 529 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_006: [authentication_create() shall allocate memory for a new authenticate state structure AUTHENTICATION_INSTANCE.] |
AzureIoTClient | 34:51d158b409d2 | 530 | if ((instance = (AUTHENTICATION_INSTANCE*)malloc(sizeof(AUTHENTICATION_INSTANCE))) == NULL) |
AzureIoTClient | 34:51d158b409d2 | 531 | { |
AzureIoTClient | 34:51d158b409d2 | 532 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_007: [If malloc() fails, authentication_create() shall fail and return NULL.] |
AzureIoTClient | 34:51d158b409d2 | 533 | result = NULL; |
AzureIoTClient | 34:51d158b409d2 | 534 | LogError("authentication_create failed (malloc failed)"); |
AzureIoTClient | 34:51d158b409d2 | 535 | } |
AzureIoTClient | 34:51d158b409d2 | 536 | else |
AzureIoTClient | 34:51d158b409d2 | 537 | { |
AzureIoTClient | 34:51d158b409d2 | 538 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_123: [authentication_create() shall initialize all fields of `instance` with 0 using memset().] |
AzureIoTClient | 34:51d158b409d2 | 539 | memset(instance, 0, sizeof(AUTHENTICATION_INSTANCE)); |
AzureIoTClient | 30:20a85b733111 | 540 | |
AzureIoTClient | 34:51d158b409d2 | 541 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_008: [authentication_create() shall save the device_id into the `instance->device_id`] |
AzureIoTClient | 34:51d158b409d2 | 542 | if ((instance->device_id = IoTHubClient_Auth_Get_DeviceId(config->authorization_module) ) == NULL) |
AzureIoTClient | 34:51d158b409d2 | 543 | { |
AzureIoTClient | 34:51d158b409d2 | 544 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_009: [If STRING_construct() fails, authentication_create() shall fail and return NULL] |
AzureIoTClient | 34:51d158b409d2 | 545 | result = NULL; |
AzureIoTClient | 34:51d158b409d2 | 546 | LogError("authentication_create failed (config->device_id could not be copied; STRING_construct failed)"); |
AzureIoTClient | 34:51d158b409d2 | 547 | } |
AzureIoTClient | 34:51d158b409d2 | 548 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_016: [If provided, authentication_create() shall save a copy of `config->iothub_host_fqdn` into `instance->iothub_host_fqdn`] |
AzureIoTClient | 34:51d158b409d2 | 549 | else if ((instance->iothub_host_fqdn = STRING_construct(config->iothub_host_fqdn)) == NULL) |
AzureIoTClient | 34:51d158b409d2 | 550 | { |
AzureIoTClient | 34:51d158b409d2 | 551 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_017: [If STRING_clone() fails to copy `config->iothub_host_fqdn`, authentication_create() shall fail and return NULL] |
AzureIoTClient | 34:51d158b409d2 | 552 | result = NULL; |
AzureIoTClient | 34:51d158b409d2 | 553 | LogError("authentication_create failed (config->iothub_host_fqdn could not be copied; STRING_construct failed)"); |
AzureIoTClient | 34:51d158b409d2 | 554 | } |
AzureIoTClient | 34:51d158b409d2 | 555 | else |
AzureIoTClient | 34:51d158b409d2 | 556 | { |
AzureIoTClient | 34:51d158b409d2 | 557 | instance->state = AUTHENTICATION_STATE_STOPPED; |
AzureIoTClient | 34:51d158b409d2 | 558 | |
AzureIoTClient | 54:830550fef7ea | 559 | instance->module_id = IoTHubClient_Auth_Get_ModuleId(config->authorization_module); |
AzureIoTClient | 54:830550fef7ea | 560 | |
AzureIoTClient | 34:51d158b409d2 | 561 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_018: [authentication_create() shall save `config->on_state_changed_callback` and `config->on_state_changed_callback_context` into `instance->on_state_changed_callback` and `instance->on_state_changed_callback_context`.] |
AzureIoTClient | 34:51d158b409d2 | 562 | instance->on_state_changed_callback = config->on_state_changed_callback; |
AzureIoTClient | 34:51d158b409d2 | 563 | instance->on_state_changed_callback_context = config->on_state_changed_callback_context; |
AzureIoTClient | 30:20a85b733111 | 564 | |
AzureIoTClient | 34:51d158b409d2 | 565 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_019: [authentication_create() shall save `config->on_error_callback` and `config->on_error_callback_context` into `instance->on_error_callback` and `instance->on_error_callback_context`.] |
AzureIoTClient | 34:51d158b409d2 | 566 | instance->on_error_callback = config->on_error_callback; |
AzureIoTClient | 34:51d158b409d2 | 567 | instance->on_error_callback_context = config->on_error_callback_context; |
AzureIoTClient | 30:20a85b733111 | 568 | |
AzureIoTClient | 34:51d158b409d2 | 569 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_021: [authentication_create() shall set `instance->cbs_request_timeout_secs` with the default value of UINT32_MAX] |
AzureIoTClient | 34:51d158b409d2 | 570 | instance->cbs_request_timeout_secs = DEFAULT_CBS_REQUEST_TIMEOUT_SECS; |
AzureIoTClient | 34:51d158b409d2 | 571 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_022: [authentication_create() shall set `instance->sas_token_lifetime_secs` with the default value of one hour] |
AzureIoTClient | 34:51d158b409d2 | 572 | instance->sas_token_lifetime_secs = DEFAULT_SAS_TOKEN_LIFETIME_SECS; |
AzureIoTClient | 34:51d158b409d2 | 573 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_023: [authentication_create() shall set `instance->sas_token_refresh_time_secs` with the default value of 30 minutes] |
AzureIoTClient | 34:51d158b409d2 | 574 | instance->sas_token_refresh_time_secs = DEFAULT_SAS_TOKEN_REFRESH_TIME_SECS; |
AzureIoTClient | 30:20a85b733111 | 575 | |
AzureIoTClient | 34:51d158b409d2 | 576 | instance->authorization_module = config->authorization_module; |
AzureIoTClient | 30:20a85b733111 | 577 | |
AzureIoTClient | 34:51d158b409d2 | 578 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_024: [If no failure occurs, authentication_create() shall return a reference to the AUTHENTICATION_INSTANCE handle] |
AzureIoTClient | 34:51d158b409d2 | 579 | result = (AUTHENTICATION_HANDLE)instance; |
AzureIoTClient | 34:51d158b409d2 | 580 | } |
AzureIoTClient | 30:20a85b733111 | 581 | |
AzureIoTClient | 34:51d158b409d2 | 582 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_020: [If any failure occurs, authentication_create() shall free any memory it allocated previously] |
AzureIoTClient | 34:51d158b409d2 | 583 | if (result == NULL) |
AzureIoTClient | 34:51d158b409d2 | 584 | { |
AzureIoTClient | 34:51d158b409d2 | 585 | authentication_destroy((AUTHENTICATION_HANDLE)instance); |
AzureIoTClient | 34:51d158b409d2 | 586 | } |
AzureIoTClient | 34:51d158b409d2 | 587 | } |
AzureIoTClient | 34:51d158b409d2 | 588 | } |
AzureIoTClient | 56:8704100b3b54 | 589 | |
AzureIoTClient | 30:20a85b733111 | 590 | return result; |
AzureIoTClient | 30:20a85b733111 | 591 | } |
AzureIoTClient | 30:20a85b733111 | 592 | |
AzureIoTClient | 30:20a85b733111 | 593 | void authentication_do_work(AUTHENTICATION_HANDLE authentication_handle) |
AzureIoTClient | 30:20a85b733111 | 594 | { |
AzureIoTClient | 34:51d158b409d2 | 595 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_036: [If authentication_handle is NULL, authentication_do_work() shall fail and return] |
AzureIoTClient | 34:51d158b409d2 | 596 | if (authentication_handle == NULL) |
AzureIoTClient | 34:51d158b409d2 | 597 | { |
AzureIoTClient | 34:51d158b409d2 | 598 | LogError("authentication_do_work failed (authentication_handle is NULL)"); |
AzureIoTClient | 34:51d158b409d2 | 599 | } |
AzureIoTClient | 34:51d158b409d2 | 600 | else |
AzureIoTClient | 34:51d158b409d2 | 601 | { |
AzureIoTClient | 34:51d158b409d2 | 602 | AUTHENTICATION_INSTANCE* instance = (AUTHENTICATION_INSTANCE*)authentication_handle; |
AzureIoTClient | 56:8704100b3b54 | 603 | |
AzureIoTClient | 34:51d158b409d2 | 604 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_038: [If `instance->is_cbs_put_token_in_progress` is TRUE, authentication_do_work() shall only verify the authentication timeout] |
AzureIoTClient | 34:51d158b409d2 | 605 | if (instance->is_cbs_put_token_in_progress) |
AzureIoTClient | 34:51d158b409d2 | 606 | { |
AzureIoTClient | 34:51d158b409d2 | 607 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_084: [If no timeout has occurred, authentication_do_work() shall return] |
AzureIoTClient | 30:20a85b733111 | 608 | |
AzureIoTClient | 34:51d158b409d2 | 609 | bool is_timed_out; |
AzureIoTClient | 34:51d158b409d2 | 610 | if (verify_cbs_put_token_timeout(instance, &is_timed_out) == RESULT_OK && is_timed_out) |
AzureIoTClient | 34:51d158b409d2 | 611 | { |
AzureIoTClient | 34:51d158b409d2 | 612 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_085: [`instance->is_cbs_put_token_in_progress` shall be set to FALSE] |
AzureIoTClient | 34:51d158b409d2 | 613 | instance->is_cbs_put_token_in_progress = false; |
AzureIoTClient | 56:8704100b3b54 | 614 | |
AzureIoTClient | 34:51d158b409d2 | 615 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_086: [`instance->state` shall be updated to AUTHENTICATION_STATE_ERROR and `instance->on_state_changed_callback` invoked] |
AzureIoTClient | 34:51d158b409d2 | 616 | update_state(instance, AUTHENTICATION_STATE_ERROR); |
AzureIoTClient | 30:20a85b733111 | 617 | |
AzureIoTClient | 34:51d158b409d2 | 618 | if (instance->is_sas_token_refresh_in_progress) |
AzureIoTClient | 34:51d158b409d2 | 619 | { |
AzureIoTClient | 34:51d158b409d2 | 620 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_087: [If `instance->is_sas_token_refresh_in_progress` is TRUE, `instance->on_error_callback` shall be invoked with AUTHENTICATION_ERROR_SAS_REFRESH_TIMEOUT] |
AzureIoTClient | 34:51d158b409d2 | 621 | notify_error(instance, AUTHENTICATION_ERROR_SAS_REFRESH_TIMEOUT); |
AzureIoTClient | 34:51d158b409d2 | 622 | } |
AzureIoTClient | 34:51d158b409d2 | 623 | else |
AzureIoTClient | 34:51d158b409d2 | 624 | { |
AzureIoTClient | 34:51d158b409d2 | 625 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_088: [If `instance->is_sas_token_refresh_in_progress` is FALSE, `instance->on_error_callback` shall be invoked with AUTHENTICATION_ERROR_AUTH_TIMEOUT] |
AzureIoTClient | 34:51d158b409d2 | 626 | notify_error(instance, AUTHENTICATION_ERROR_AUTH_TIMEOUT); |
AzureIoTClient | 34:51d158b409d2 | 627 | } |
AzureIoTClient | 30:20a85b733111 | 628 | |
AzureIoTClient | 34:51d158b409d2 | 629 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_089: [`instance->is_sas_token_refresh_in_progress` shall be set to FALSE] |
AzureIoTClient | 34:51d158b409d2 | 630 | instance->is_sas_token_refresh_in_progress = false; |
AzureIoTClient | 34:51d158b409d2 | 631 | } |
AzureIoTClient | 34:51d158b409d2 | 632 | } |
AzureIoTClient | 34:51d158b409d2 | 633 | else if (instance->state == AUTHENTICATION_STATE_STARTED) |
AzureIoTClient | 34:51d158b409d2 | 634 | { |
AzureIoTClient | 34:51d158b409d2 | 635 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_040: [If `instance->state` is AUTHENTICATION_STATE_STARTED and user-provided SAS token was used, authentication_do_work() shall return] |
AzureIoTClient | 34:51d158b409d2 | 636 | if (IoTHubClient_Auth_Get_Credential_Type(instance->authorization_module) == IOTHUB_CREDENTIAL_TYPE_DEVICE_KEY) |
AzureIoTClient | 34:51d158b409d2 | 637 | { |
AzureIoTClient | 34:51d158b409d2 | 638 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_039: [If `instance->state` is AUTHENTICATION_STATE_STARTED and device keys were used, authentication_do_work() shall only verify the SAS token refresh time] |
AzureIoTClient | 34:51d158b409d2 | 639 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_065: [The SAS token shall be refreshed if the current time minus `instance->current_sas_token_put_time` equals or exceeds `instance->sas_token_refresh_time_secs`] |
AzureIoTClient | 34:51d158b409d2 | 640 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_066: [If SAS token does not need to be refreshed, authentication_do_work() shall return] |
AzureIoTClient | 34:51d158b409d2 | 641 | bool is_timed_out; |
AzureIoTClient | 34:51d158b409d2 | 642 | if (verify_sas_token_refresh_timeout(instance, &is_timed_out) == RESULT_OK && is_timed_out) |
AzureIoTClient | 34:51d158b409d2 | 643 | { |
AzureIoTClient | 34:51d158b409d2 | 644 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_119: [authentication_do_work() shall set `instance->is_sas_token_refresh_in_progress` to TRUE] |
AzureIoTClient | 34:51d158b409d2 | 645 | instance->is_sas_token_refresh_in_progress = true; |
AzureIoTClient | 30:20a85b733111 | 646 | |
AzureIoTClient | 34:51d158b409d2 | 647 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_067: [authentication_do_work() shall create a SAS token using `instance->device_primary_key`, unless it has failed previously] |
AzureIoTClient | 34:51d158b409d2 | 648 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_069: [If using `instance->device_primary_key` has failed previously, a SAS token shall be created using `instance->device_secondary_key`] |
AzureIoTClient | 34:51d158b409d2 | 649 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_068: [If using `instance->device_primary_key` has failed previously and `instance->device_secondary_key` is not provided, authentication_do_work() shall fail and return] |
AzureIoTClient | 34:51d158b409d2 | 650 | if (create_and_put_SAS_token_to_cbs(instance) != RESULT_OK) |
AzureIoTClient | 34:51d158b409d2 | 651 | { |
AzureIoTClient | 34:51d158b409d2 | 652 | LogError("Failed refreshing SAS token '%'", instance->device_id); |
AzureIoTClient | 34:51d158b409d2 | 653 | } |
AzureIoTClient | 30:20a85b733111 | 654 | |
AzureIoTClient | 34:51d158b409d2 | 655 | if (!instance->is_cbs_put_token_in_progress) |
AzureIoTClient | 34:51d158b409d2 | 656 | { |
AzureIoTClient | 34:51d158b409d2 | 657 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_120: [If cbs_put_token() fails, `instance->is_sas_token_refresh_in_progress` shall be set to FALSE] |
AzureIoTClient | 34:51d158b409d2 | 658 | instance->is_sas_token_refresh_in_progress = false; |
AzureIoTClient | 30:20a85b733111 | 659 | |
AzureIoTClient | 34:51d158b409d2 | 660 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_079: [If cbs_put_token() fails, `instance->state` shall be updated to AUTHENTICATION_STATE_ERROR and `instance->on_state_changed_callback` invoked] |
AzureIoTClient | 34:51d158b409d2 | 661 | update_state(instance, AUTHENTICATION_STATE_ERROR); |
AzureIoTClient | 30:20a85b733111 | 662 | |
AzureIoTClient | 34:51d158b409d2 | 663 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_080: [If cbs_put_token() fails, `instance->on_error_callback` shall be invoked with AUTHENTICATION_ERROR_SAS_REFRESH_FAILED] |
AzureIoTClient | 34:51d158b409d2 | 664 | notify_error(instance, AUTHENTICATION_ERROR_SAS_REFRESH_FAILED); |
AzureIoTClient | 34:51d158b409d2 | 665 | } |
AzureIoTClient | 34:51d158b409d2 | 666 | } |
AzureIoTClient | 34:51d158b409d2 | 667 | } |
AzureIoTClient | 34:51d158b409d2 | 668 | } |
AzureIoTClient | 34:51d158b409d2 | 669 | else if (instance->state == AUTHENTICATION_STATE_STARTING) |
AzureIoTClient | 34:51d158b409d2 | 670 | { |
AzureIoTClient | 34:51d158b409d2 | 671 | if (create_and_put_SAS_token_to_cbs(instance) != RESULT_OK) |
AzureIoTClient | 34:51d158b409d2 | 672 | { |
AzureIoTClient | 34:51d158b409d2 | 673 | LogError("Failed authenticating device '%s' using device keys", instance->device_id); |
AzureIoTClient | 34:51d158b409d2 | 674 | } |
AzureIoTClient | 30:20a85b733111 | 675 | |
AzureIoTClient | 34:51d158b409d2 | 676 | if (!instance->is_cbs_put_token_in_progress) |
AzureIoTClient | 34:51d158b409d2 | 677 | { |
AzureIoTClient | 34:51d158b409d2 | 678 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_061: [If cbs_put_token() fails, `instance->state` shall be updated to AUTHENTICATION_STATE_ERROR and `instance->on_state_changed_callback` invoked] |
AzureIoTClient | 34:51d158b409d2 | 679 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_121: [If cbs_put_token() fails, `instance->state` shall be updated to AUTHENTICATION_STATE_ERROR and `instance->on_state_changed_callback` invoked] |
AzureIoTClient | 34:51d158b409d2 | 680 | update_state(instance, AUTHENTICATION_STATE_ERROR); |
AzureIoTClient | 30:20a85b733111 | 681 | |
AzureIoTClient | 34:51d158b409d2 | 682 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_062: [If cbs_put_token() fails, `instance->on_error_callback` shall be invoked with AUTHENTICATION_ERROR_AUTH_FAILED] |
AzureIoTClient | 34:51d158b409d2 | 683 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_122: [If cbs_put_token() fails, `instance->on_error_callback` shall be invoked with AUTHENTICATION_ERROR_AUTH_FAILED] |
AzureIoTClient | 34:51d158b409d2 | 684 | notify_error(instance, AUTHENTICATION_ERROR_AUTH_FAILED); |
AzureIoTClient | 34:51d158b409d2 | 685 | } |
AzureIoTClient | 34:51d158b409d2 | 686 | } |
AzureIoTClient | 34:51d158b409d2 | 687 | else |
AzureIoTClient | 34:51d158b409d2 | 688 | { |
AzureIoTClient | 34:51d158b409d2 | 689 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_037: [If `instance->state` is not AUTHENTICATION_STATE_STARTING or AUTHENTICATION_STATE_STARTED, authentication_do_work() shall fail and return] |
AzureIoTClient | 34:51d158b409d2 | 690 | // Nothing to be done. |
AzureIoTClient | 34:51d158b409d2 | 691 | } |
AzureIoTClient | 34:51d158b409d2 | 692 | } |
AzureIoTClient | 30:20a85b733111 | 693 | } |
AzureIoTClient | 30:20a85b733111 | 694 | |
AzureIoTClient | 30:20a85b733111 | 695 | int authentication_set_option(AUTHENTICATION_HANDLE authentication_handle, const char* name, void* value) |
AzureIoTClient | 30:20a85b733111 | 696 | { |
AzureIoTClient | 34:51d158b409d2 | 697 | int result; |
AzureIoTClient | 30:20a85b733111 | 698 | |
AzureIoTClient | 34:51d158b409d2 | 699 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_097: [If `authentication_handle` or `name` or `value` is NULL, authentication_set_option shall fail and return a non-zero value] |
AzureIoTClient | 34:51d158b409d2 | 700 | if (authentication_handle == NULL || name == NULL || value == NULL) |
AzureIoTClient | 34:51d158b409d2 | 701 | { |
AzureIoTClient | 56:8704100b3b54 | 702 | LogError("authentication_set_option failed (one of the followin are NULL: authentication_handle=%p, name=%p, value=%p)", |
AzureIoTClient | 34:51d158b409d2 | 703 | authentication_handle, name, value); |
AzureIoTClient | 34:51d158b409d2 | 704 | result = __FAILURE__; |
AzureIoTClient | 34:51d158b409d2 | 705 | } |
AzureIoTClient | 34:51d158b409d2 | 706 | else |
AzureIoTClient | 34:51d158b409d2 | 707 | { |
AzureIoTClient | 34:51d158b409d2 | 708 | AUTHENTICATION_INSTANCE* instance = (AUTHENTICATION_INSTANCE*)authentication_handle; |
AzureIoTClient | 30:20a85b733111 | 709 | |
AzureIoTClient | 34:51d158b409d2 | 710 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_098: [If name matches AUTHENTICATION_OPTION_CBS_REQUEST_TIMEOUT_SECS, `value` shall be saved on `instance->cbs_request_timeout_secs`] |
AzureIoTClient | 34:51d158b409d2 | 711 | if (strcmp(AUTHENTICATION_OPTION_CBS_REQUEST_TIMEOUT_SECS, name) == 0) |
AzureIoTClient | 34:51d158b409d2 | 712 | { |
AzureIoTClient | 34:51d158b409d2 | 713 | instance->cbs_request_timeout_secs = *((size_t*)value); |
AzureIoTClient | 34:51d158b409d2 | 714 | result = RESULT_OK; |
AzureIoTClient | 34:51d158b409d2 | 715 | } |
AzureIoTClient | 34:51d158b409d2 | 716 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_124: [If name matches AUTHENTICATION_OPTION_SAS_TOKEN_REFRESH_TIME_SECS, `value` shall be saved on `instance->sas_token_refresh_time_secs`] |
AzureIoTClient | 34:51d158b409d2 | 717 | else if (strcmp(AUTHENTICATION_OPTION_SAS_TOKEN_REFRESH_TIME_SECS, name) == 0) |
AzureIoTClient | 34:51d158b409d2 | 718 | { |
AzureIoTClient | 34:51d158b409d2 | 719 | instance->sas_token_refresh_time_secs = *((size_t*)value); |
AzureIoTClient | 34:51d158b409d2 | 720 | result = RESULT_OK; |
AzureIoTClient | 34:51d158b409d2 | 721 | } |
AzureIoTClient | 34:51d158b409d2 | 722 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_125: [If name matches AUTHENTICATION_OPTION_SAS_TOKEN_LIFETIME_SECS, `value` shall be saved on `instance->sas_token_lifetime_secs`] |
AzureIoTClient | 34:51d158b409d2 | 723 | else if (strcmp(AUTHENTICATION_OPTION_SAS_TOKEN_LIFETIME_SECS, name) == 0) |
AzureIoTClient | 34:51d158b409d2 | 724 | { |
AzureIoTClient | 34:51d158b409d2 | 725 | instance->sas_token_lifetime_secs = *((size_t*)value); |
AzureIoTClient | 34:51d158b409d2 | 726 | result = RESULT_OK; |
AzureIoTClient | 34:51d158b409d2 | 727 | } |
AzureIoTClient | 34:51d158b409d2 | 728 | else if (strcmp(AUTHENTICATION_OPTION_SAVED_OPTIONS, name) == 0) |
AzureIoTClient | 34:51d158b409d2 | 729 | { |
AzureIoTClient | 34:51d158b409d2 | 730 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_098: [If name matches AUTHENTICATION_OPTION_SAVED_OPTIONS, `value` shall be applied using OptionHandler_FeedOptions] |
AzureIoTClient | 34:51d158b409d2 | 731 | if (OptionHandler_FeedOptions((OPTIONHANDLER_HANDLE)value, authentication_handle) != OPTIONHANDLER_OK) |
AzureIoTClient | 34:51d158b409d2 | 732 | { |
AzureIoTClient | 34:51d158b409d2 | 733 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_126: [If OptionHandler_FeedOptions fails, authentication_set_option shall fail and return a non-zero value] |
AzureIoTClient | 34:51d158b409d2 | 734 | LogError("authentication_set_option failed (OptionHandler_FeedOptions failed)"); |
AzureIoTClient | 34:51d158b409d2 | 735 | result = __FAILURE__; |
AzureIoTClient | 34:51d158b409d2 | 736 | } |
AzureIoTClient | 34:51d158b409d2 | 737 | else |
AzureIoTClient | 34:51d158b409d2 | 738 | { |
AzureIoTClient | 34:51d158b409d2 | 739 | result = RESULT_OK; |
AzureIoTClient | 34:51d158b409d2 | 740 | } |
AzureIoTClient | 34:51d158b409d2 | 741 | } |
AzureIoTClient | 34:51d158b409d2 | 742 | else |
AzureIoTClient | 34:51d158b409d2 | 743 | { |
AzureIoTClient | 34:51d158b409d2 | 744 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_128: [If name does not match any supported option, authentication_set_option shall fail and return a non-zero value] |
AzureIoTClient | 34:51d158b409d2 | 745 | LogError("authentication_set_option failed (option with name '%s' is not suppported)", name); |
AzureIoTClient | 34:51d158b409d2 | 746 | result = __FAILURE__; |
AzureIoTClient | 34:51d158b409d2 | 747 | } |
AzureIoTClient | 34:51d158b409d2 | 748 | } |
AzureIoTClient | 30:20a85b733111 | 749 | |
AzureIoTClient | 34:51d158b409d2 | 750 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_099: [If no errors occur, authentication_set_option shall return 0] |
AzureIoTClient | 34:51d158b409d2 | 751 | return result; |
AzureIoTClient | 30:20a85b733111 | 752 | } |
AzureIoTClient | 30:20a85b733111 | 753 | |
AzureIoTClient | 30:20a85b733111 | 754 | OPTIONHANDLER_HANDLE authentication_retrieve_options(AUTHENTICATION_HANDLE authentication_handle) |
AzureIoTClient | 30:20a85b733111 | 755 | { |
AzureIoTClient | 34:51d158b409d2 | 756 | OPTIONHANDLER_HANDLE result; |
AzureIoTClient | 30:20a85b733111 | 757 | |
AzureIoTClient | 34:51d158b409d2 | 758 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_100: [If `authentication_handle` is NULL, authentication_retrieve_options shall fail and return NULL] |
AzureIoTClient | 34:51d158b409d2 | 759 | if (authentication_handle == NULL) |
AzureIoTClient | 34:51d158b409d2 | 760 | { |
AzureIoTClient | 34:51d158b409d2 | 761 | LogError("Failed to retrieve options from authentication instance (authentication_handle is NULL)"); |
AzureIoTClient | 34:51d158b409d2 | 762 | result = NULL; |
AzureIoTClient | 34:51d158b409d2 | 763 | } |
AzureIoTClient | 34:51d158b409d2 | 764 | else |
AzureIoTClient | 34:51d158b409d2 | 765 | { |
AzureIoTClient | 34:51d158b409d2 | 766 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_101: [An OPTIONHANDLER_HANDLE instance shall be created using OptionHandler_Create] |
AzureIoTClient | 34:51d158b409d2 | 767 | OPTIONHANDLER_HANDLE options = OptionHandler_Create(authentication_clone_option, authentication_destroy_option, (pfSetOption)authentication_set_option); |
AzureIoTClient | 30:20a85b733111 | 768 | |
AzureIoTClient | 34:51d158b409d2 | 769 | if (options == NULL) |
AzureIoTClient | 34:51d158b409d2 | 770 | { |
AzureIoTClient | 34:51d158b409d2 | 771 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_102: [If an OPTIONHANDLER_HANDLE instance fails to be created, authentication_retrieve_options shall fail and return NULL] |
AzureIoTClient | 34:51d158b409d2 | 772 | LogError("Failed to retrieve options from authentication instance (OptionHandler_Create failed)"); |
AzureIoTClient | 34:51d158b409d2 | 773 | result = NULL; |
AzureIoTClient | 34:51d158b409d2 | 774 | } |
AzureIoTClient | 34:51d158b409d2 | 775 | else |
AzureIoTClient | 34:51d158b409d2 | 776 | { |
AzureIoTClient | 34:51d158b409d2 | 777 | AUTHENTICATION_INSTANCE* instance = (AUTHENTICATION_INSTANCE*)authentication_handle; |
AzureIoTClient | 30:20a85b733111 | 778 | |
AzureIoTClient | 34:51d158b409d2 | 779 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_103: [Each option of `instance` shall be added to the OPTIONHANDLER_HANDLE instance using OptionHandler_AddOption] |
AzureIoTClient | 34:51d158b409d2 | 780 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_104: [If OptionHandler_AddOption fails, authentication_retrieve_options shall fail and return NULL] |
AzureIoTClient | 34:51d158b409d2 | 781 | if (OptionHandler_AddOption(options, AUTHENTICATION_OPTION_CBS_REQUEST_TIMEOUT_SECS, (void*)&instance->cbs_request_timeout_secs) != OPTIONHANDLER_OK) |
AzureIoTClient | 34:51d158b409d2 | 782 | { |
AzureIoTClient | 34:51d158b409d2 | 783 | LogError("Failed to retrieve options from authentication instance (OptionHandler_Create failed for option '%s')", AUTHENTICATION_OPTION_CBS_REQUEST_TIMEOUT_SECS); |
AzureIoTClient | 34:51d158b409d2 | 784 | result = NULL; |
AzureIoTClient | 34:51d158b409d2 | 785 | } |
AzureIoTClient | 34:51d158b409d2 | 786 | else if (OptionHandler_AddOption(options, AUTHENTICATION_OPTION_SAS_TOKEN_REFRESH_TIME_SECS, (void*)&instance->sas_token_refresh_time_secs) != OPTIONHANDLER_OK) |
AzureIoTClient | 34:51d158b409d2 | 787 | { |
AzureIoTClient | 34:51d158b409d2 | 788 | LogError("Failed to retrieve options from authentication instance (OptionHandler_Create failed for option '%s')", AUTHENTICATION_OPTION_SAS_TOKEN_REFRESH_TIME_SECS); |
AzureIoTClient | 34:51d158b409d2 | 789 | result = NULL; |
AzureIoTClient | 34:51d158b409d2 | 790 | } |
AzureIoTClient | 34:51d158b409d2 | 791 | else if (OptionHandler_AddOption(options, AUTHENTICATION_OPTION_SAS_TOKEN_LIFETIME_SECS, (void*)&instance->sas_token_lifetime_secs) != OPTIONHANDLER_OK) |
AzureIoTClient | 34:51d158b409d2 | 792 | { |
AzureIoTClient | 34:51d158b409d2 | 793 | LogError("Failed to retrieve options from authentication instance (OptionHandler_Create failed for option '%s')", AUTHENTICATION_OPTION_SAS_TOKEN_LIFETIME_SECS); |
AzureIoTClient | 34:51d158b409d2 | 794 | result = NULL; |
AzureIoTClient | 34:51d158b409d2 | 795 | } |
AzureIoTClient | 34:51d158b409d2 | 796 | else |
AzureIoTClient | 34:51d158b409d2 | 797 | { |
AzureIoTClient | 34:51d158b409d2 | 798 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_127: [If no failures occur, authentication_retrieve_options shall return the OPTIONHANDLER_HANDLE instance] |
AzureIoTClient | 34:51d158b409d2 | 799 | result = options; |
AzureIoTClient | 34:51d158b409d2 | 800 | } |
AzureIoTClient | 30:20a85b733111 | 801 | |
AzureIoTClient | 34:51d158b409d2 | 802 | if (result == NULL) |
AzureIoTClient | 34:51d158b409d2 | 803 | { |
AzureIoTClient | 34:51d158b409d2 | 804 | // Codes_SRS_IOTHUBTRANSPORT_AMQP_AUTH_09_105: [If authentication_retrieve_options fails, any allocated memory shall be freed] |
AzureIoTClient | 34:51d158b409d2 | 805 | OptionHandler_Destroy(options); |
AzureIoTClient | 34:51d158b409d2 | 806 | } |
AzureIoTClient | 34:51d158b409d2 | 807 | } |
AzureIoTClient | 34:51d158b409d2 | 808 | } |
AzureIoTClient | 34:51d158b409d2 | 809 | return result; |
AzureIoTClient | 30:20a85b733111 | 810 | } |