Microsoft Azure IoTHub client libraries
Dependents: sht15_remote_monitoring RobotArmDemo iothub_client_sample_amqp f767zi_mqtt ... more
This library implements the Microsoft Azure IoTHub client library. The code is replicated from https://github.com/Azure/azure-iot-sdks
iothub_client_authorization.c@89:a2ed767a532e, 2018-06-26 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Tue Jun 26 19:14:28 2018 -0700
- Revision:
- 89:a2ed767a532e
- Parent:
- 88:248736be106e
- Child:
- 92:97148cf9aa2a
1.2.6
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AzureIoTClient | 62:5a4cdacf5090 | 1 | // Copyright (c) Microsoft. All rights reserved. |
AzureIoTClient | 62:5a4cdacf5090 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
AzureIoTClient | 62:5a4cdacf5090 | 3 | |
AzureIoTClient | 62:5a4cdacf5090 | 4 | #include <stdlib.h> |
AzureIoTClient | 62:5a4cdacf5090 | 5 | #include "azure_c_shared_utility/gballoc.h" |
AzureIoTClient | 62:5a4cdacf5090 | 6 | #include "azure_c_shared_utility/macro_utils.h" |
AzureIoTClient | 62:5a4cdacf5090 | 7 | #include "azure_c_shared_utility/umock_c_prod.h" |
AzureIoTClient | 62:5a4cdacf5090 | 8 | #include "azure_c_shared_utility/crt_abstractions.h" |
AzureIoTClient | 62:5a4cdacf5090 | 9 | #include "azure_c_shared_utility/agenttime.h" |
AzureIoTClient | 62:5a4cdacf5090 | 10 | #include "azure_c_shared_utility/xlogging.h" |
AzureIoTClient | 62:5a4cdacf5090 | 11 | #include "azure_c_shared_utility/strings.h" |
AzureIoTClient | 62:5a4cdacf5090 | 12 | #include "azure_c_shared_utility/sastoken.h" |
AzureIoTClient | 75:86205ca63a59 | 13 | #include "azure_c_shared_utility/shared_util_options.h" |
AzureIoTClient | 75:86205ca63a59 | 14 | |
AzureIoTClient | 78:74a8d3068204 | 15 | #ifdef USE_PROV_MODULE |
AzureIoTClient | 88:248736be106e | 16 | #include "azure_prov_client/internal/iothub_auth_client.h" |
AzureIoTClient | 75:86205ca63a59 | 17 | #endif |
AzureIoTClient | 62:5a4cdacf5090 | 18 | |
AzureIoTClient | 88:248736be106e | 19 | #include "internal/iothub_client_authorization.h" |
AzureIoTClient | 62:5a4cdacf5090 | 20 | |
AzureIoTClient | 62:5a4cdacf5090 | 21 | #define DEFAULT_SAS_TOKEN_EXPIRY_TIME_SECS 3600 |
AzureIoTClient | 62:5a4cdacf5090 | 22 | #define INDEFINITE_TIME ((time_t)(-1)) |
AzureIoTClient | 62:5a4cdacf5090 | 23 | |
AzureIoTClient | 62:5a4cdacf5090 | 24 | typedef struct IOTHUB_AUTHORIZATION_DATA_TAG |
AzureIoTClient | 62:5a4cdacf5090 | 25 | { |
AzureIoTClient | 62:5a4cdacf5090 | 26 | char* device_sas_token; |
AzureIoTClient | 62:5a4cdacf5090 | 27 | char* device_key; |
AzureIoTClient | 62:5a4cdacf5090 | 28 | char* device_id; |
AzureIoTClient | 89:a2ed767a532e | 29 | char* module_id; |
AzureIoTClient | 62:5a4cdacf5090 | 30 | size_t token_expiry_time_sec; |
AzureIoTClient | 62:5a4cdacf5090 | 31 | IOTHUB_CREDENTIAL_TYPE cred_type; |
AzureIoTClient | 78:74a8d3068204 | 32 | #ifdef USE_PROV_MODULE |
AzureIoTClient | 75:86205ca63a59 | 33 | IOTHUB_SECURITY_HANDLE device_auth_handle; |
AzureIoTClient | 75:86205ca63a59 | 34 | #endif |
AzureIoTClient | 62:5a4cdacf5090 | 35 | } IOTHUB_AUTHORIZATION_DATA; |
AzureIoTClient | 62:5a4cdacf5090 | 36 | |
AzureIoTClient | 62:5a4cdacf5090 | 37 | static int get_seconds_since_epoch(size_t* seconds) |
AzureIoTClient | 62:5a4cdacf5090 | 38 | { |
AzureIoTClient | 62:5a4cdacf5090 | 39 | int result; |
AzureIoTClient | 62:5a4cdacf5090 | 40 | time_t current_time; |
AzureIoTClient | 62:5a4cdacf5090 | 41 | if ((current_time = get_time(NULL)) == INDEFINITE_TIME) |
AzureIoTClient | 62:5a4cdacf5090 | 42 | { |
AzureIoTClient | 62:5a4cdacf5090 | 43 | LogError("Failed getting the current local time (get_time() failed)"); |
AzureIoTClient | 75:86205ca63a59 | 44 | result = __FAILURE__; |
AzureIoTClient | 62:5a4cdacf5090 | 45 | } |
AzureIoTClient | 62:5a4cdacf5090 | 46 | else |
AzureIoTClient | 62:5a4cdacf5090 | 47 | { |
AzureIoTClient | 62:5a4cdacf5090 | 48 | *seconds = (size_t)get_difftime(current_time, (time_t)0); |
AzureIoTClient | 62:5a4cdacf5090 | 49 | result = 0; |
AzureIoTClient | 62:5a4cdacf5090 | 50 | } |
AzureIoTClient | 62:5a4cdacf5090 | 51 | return result; |
AzureIoTClient | 62:5a4cdacf5090 | 52 | } |
AzureIoTClient | 62:5a4cdacf5090 | 53 | |
AzureIoTClient | 89:a2ed767a532e | 54 | IOTHUB_AUTHORIZATION_HANDLE IoTHubClient_Auth_Create(const char* device_key, const char* device_id, const char* device_sas_token, const char *module_id) |
AzureIoTClient | 62:5a4cdacf5090 | 55 | { |
AzureIoTClient | 62:5a4cdacf5090 | 56 | IOTHUB_AUTHORIZATION_DATA* result; |
AzureIoTClient | 62:5a4cdacf5090 | 57 | /* Codes_SRS_IoTHub_Authorization_07_001: [if device_id is NULL IoTHubClient_Auth_Create, shall return NULL. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 58 | if (device_id == NULL) |
AzureIoTClient | 62:5a4cdacf5090 | 59 | { |
AzureIoTClient | 75:86205ca63a59 | 60 | LogError("Invalid Parameter device_id: %p", device_id); |
AzureIoTClient | 62:5a4cdacf5090 | 61 | result = NULL; |
AzureIoTClient | 62:5a4cdacf5090 | 62 | } |
AzureIoTClient | 62:5a4cdacf5090 | 63 | else |
AzureIoTClient | 62:5a4cdacf5090 | 64 | { |
AzureIoTClient | 62:5a4cdacf5090 | 65 | /* Codes_SRS_IoTHub_Authorization_07_002: [IoTHubClient_Auth_Create shall allocate a IOTHUB_AUTHORIZATION_HANDLE that is needed for subsequent calls. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 66 | result = (IOTHUB_AUTHORIZATION_DATA*)malloc(sizeof(IOTHUB_AUTHORIZATION_DATA) ); |
AzureIoTClient | 62:5a4cdacf5090 | 67 | if (result == NULL) |
AzureIoTClient | 62:5a4cdacf5090 | 68 | { |
AzureIoTClient | 62:5a4cdacf5090 | 69 | /* Codes_SRS_IoTHub_Authorization_07_019: [ On error IoTHubClient_Auth_Create shall return NULL. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 70 | LogError("Failed allocating IOTHUB_AUTHORIZATION_DATA"); |
AzureIoTClient | 62:5a4cdacf5090 | 71 | result = NULL; |
AzureIoTClient | 62:5a4cdacf5090 | 72 | } |
AzureIoTClient | 62:5a4cdacf5090 | 73 | else |
AzureIoTClient | 62:5a4cdacf5090 | 74 | { |
AzureIoTClient | 62:5a4cdacf5090 | 75 | memset(result, 0, sizeof(IOTHUB_AUTHORIZATION_DATA) ); |
AzureIoTClient | 62:5a4cdacf5090 | 76 | result->token_expiry_time_sec = DEFAULT_SAS_TOKEN_EXPIRY_TIME_SECS; |
AzureIoTClient | 62:5a4cdacf5090 | 77 | |
AzureIoTClient | 62:5a4cdacf5090 | 78 | if (device_key != NULL && mallocAndStrcpy_s(&result->device_key, device_key) != 0) |
AzureIoTClient | 62:5a4cdacf5090 | 79 | { |
AzureIoTClient | 62:5a4cdacf5090 | 80 | /* Codes_SRS_IoTHub_Authorization_07_019: [ On error IoTHubClient_Auth_Create shall return NULL. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 81 | LogError("Failed allocating device_key"); |
AzureIoTClient | 62:5a4cdacf5090 | 82 | free(result); |
AzureIoTClient | 62:5a4cdacf5090 | 83 | result = NULL; |
AzureIoTClient | 62:5a4cdacf5090 | 84 | } |
AzureIoTClient | 62:5a4cdacf5090 | 85 | else if (mallocAndStrcpy_s(&result->device_id, device_id) != 0) |
AzureIoTClient | 62:5a4cdacf5090 | 86 | { |
AzureIoTClient | 62:5a4cdacf5090 | 87 | /* Codes_SRS_IoTHub_Authorization_07_019: [ On error IoTHubClient_Auth_Create shall return NULL. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 88 | LogError("Failed allocating device_key"); |
AzureIoTClient | 62:5a4cdacf5090 | 89 | free(result->device_key); |
AzureIoTClient | 62:5a4cdacf5090 | 90 | free(result); |
AzureIoTClient | 62:5a4cdacf5090 | 91 | result = NULL; |
AzureIoTClient | 62:5a4cdacf5090 | 92 | } |
AzureIoTClient | 89:a2ed767a532e | 93 | else if (module_id != NULL && mallocAndStrcpy_s(&result->module_id, module_id) != 0) |
AzureIoTClient | 89:a2ed767a532e | 94 | { |
AzureIoTClient | 89:a2ed767a532e | 95 | /* Codes_SRS_IoTHub_Authorization_07_019: [ On error IoTHubClient_Auth_Create shall return NULL. ] */ |
AzureIoTClient | 89:a2ed767a532e | 96 | LogError("Failed allocating module_id"); |
AzureIoTClient | 89:a2ed767a532e | 97 | free(result->device_id); |
AzureIoTClient | 89:a2ed767a532e | 98 | free(result->device_key); |
AzureIoTClient | 89:a2ed767a532e | 99 | free(result); |
AzureIoTClient | 89:a2ed767a532e | 100 | result = NULL; |
AzureIoTClient | 89:a2ed767a532e | 101 | } |
AzureIoTClient | 62:5a4cdacf5090 | 102 | else |
AzureIoTClient | 62:5a4cdacf5090 | 103 | { |
AzureIoTClient | 63:1bf1c2d60aab | 104 | if (device_key != NULL) |
AzureIoTClient | 62:5a4cdacf5090 | 105 | { |
AzureIoTClient | 62:5a4cdacf5090 | 106 | /* Codes_SRS_IoTHub_Authorization_07_003: [ IoTHubClient_Auth_Create shall set the credential type to IOTHUB_CREDENTIAL_TYPE_DEVICE_KEY if the device_sas_token is NULL. ]*/ |
AzureIoTClient | 62:5a4cdacf5090 | 107 | result->cred_type = IOTHUB_CREDENTIAL_TYPE_DEVICE_KEY; |
AzureIoTClient | 62:5a4cdacf5090 | 108 | } |
AzureIoTClient | 63:1bf1c2d60aab | 109 | else if (device_sas_token != NULL) |
AzureIoTClient | 62:5a4cdacf5090 | 110 | { |
AzureIoTClient | 62:5a4cdacf5090 | 111 | /* Codes_SRS_IoTHub_Authorization_07_020: [ else IoTHubClient_Auth_Create shall set the credential type to IOTHUB_CREDENTIAL_TYPE_SAS_TOKEN. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 112 | result->cred_type = IOTHUB_CREDENTIAL_TYPE_SAS_TOKEN; |
AzureIoTClient | 62:5a4cdacf5090 | 113 | if (mallocAndStrcpy_s(&result->device_sas_token, device_sas_token) != 0) |
AzureIoTClient | 62:5a4cdacf5090 | 114 | { |
AzureIoTClient | 62:5a4cdacf5090 | 115 | /* Codes_SRS_IoTHub_Authorization_07_019: [ On error IoTHubClient_Auth_Create shall return NULL. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 116 | LogError("Failed allocating device_key"); |
AzureIoTClient | 62:5a4cdacf5090 | 117 | free(result->device_key); |
AzureIoTClient | 62:5a4cdacf5090 | 118 | free(result->device_id); |
AzureIoTClient | 89:a2ed767a532e | 119 | free(result->module_id); |
AzureIoTClient | 62:5a4cdacf5090 | 120 | free(result); |
AzureIoTClient | 62:5a4cdacf5090 | 121 | result = NULL; |
AzureIoTClient | 62:5a4cdacf5090 | 122 | } |
AzureIoTClient | 62:5a4cdacf5090 | 123 | } |
AzureIoTClient | 63:1bf1c2d60aab | 124 | else |
AzureIoTClient | 63:1bf1c2d60aab | 125 | { |
AzureIoTClient | 63:1bf1c2d60aab | 126 | /* Codes_SRS_IoTHub_Authorization_07_024: [ if device_sas_token and device_key are NULL IoTHubClient_Auth_Create shall set the credential type to IOTHUB_CREDENTIAL_TYPE_UNKNOWN. ] */ |
AzureIoTClient | 63:1bf1c2d60aab | 127 | result->cred_type = IOTHUB_CREDENTIAL_TYPE_UNKNOWN; |
AzureIoTClient | 63:1bf1c2d60aab | 128 | } |
AzureIoTClient | 62:5a4cdacf5090 | 129 | } |
AzureIoTClient | 62:5a4cdacf5090 | 130 | } |
AzureIoTClient | 62:5a4cdacf5090 | 131 | } |
AzureIoTClient | 62:5a4cdacf5090 | 132 | /* Codes_SRS_IoTHub_Authorization_07_004: [ If successful IoTHubClient_Auth_Create shall return a IOTHUB_AUTHORIZATION_HANDLE value. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 133 | return result; |
AzureIoTClient | 62:5a4cdacf5090 | 134 | } |
AzureIoTClient | 62:5a4cdacf5090 | 135 | |
AzureIoTClient | 89:a2ed767a532e | 136 | IOTHUB_AUTHORIZATION_HANDLE IoTHubClient_Auth_CreateFromDeviceAuth(const char* device_id, const char* module_id) |
AzureIoTClient | 75:86205ca63a59 | 137 | { |
AzureIoTClient | 75:86205ca63a59 | 138 | IOTHUB_AUTHORIZATION_DATA* result; |
AzureIoTClient | 75:86205ca63a59 | 139 | if (device_id == NULL) |
AzureIoTClient | 75:86205ca63a59 | 140 | { |
AzureIoTClient | 75:86205ca63a59 | 141 | LogError("Invalid Parameter device_id: %p", device_id); |
AzureIoTClient | 75:86205ca63a59 | 142 | result = NULL; |
AzureIoTClient | 75:86205ca63a59 | 143 | } |
AzureIoTClient | 75:86205ca63a59 | 144 | else |
AzureIoTClient | 75:86205ca63a59 | 145 | { |
AzureIoTClient | 78:74a8d3068204 | 146 | #ifdef USE_PROV_MODULE |
AzureIoTClient | 75:86205ca63a59 | 147 | result = (IOTHUB_AUTHORIZATION_DATA*)malloc(sizeof(IOTHUB_AUTHORIZATION_DATA)); |
AzureIoTClient | 75:86205ca63a59 | 148 | if (result == NULL) |
AzureIoTClient | 75:86205ca63a59 | 149 | { |
AzureIoTClient | 75:86205ca63a59 | 150 | LogError("Failed allocating IOTHUB_AUTHORIZATION_DATA"); |
AzureIoTClient | 75:86205ca63a59 | 151 | result = NULL; |
AzureIoTClient | 75:86205ca63a59 | 152 | } |
AzureIoTClient | 75:86205ca63a59 | 153 | else |
AzureIoTClient | 75:86205ca63a59 | 154 | { |
AzureIoTClient | 75:86205ca63a59 | 155 | memset(result, 0, sizeof(IOTHUB_AUTHORIZATION_DATA)); |
AzureIoTClient | 75:86205ca63a59 | 156 | |
AzureIoTClient | 75:86205ca63a59 | 157 | result->device_auth_handle = iothub_device_auth_create(); |
AzureIoTClient | 75:86205ca63a59 | 158 | if (result->device_auth_handle == NULL) |
AzureIoTClient | 75:86205ca63a59 | 159 | { |
AzureIoTClient | 75:86205ca63a59 | 160 | LogError("Failed allocating IOTHUB_AUTHORIZATION_DATA"); |
AzureIoTClient | 75:86205ca63a59 | 161 | free(result); |
AzureIoTClient | 75:86205ca63a59 | 162 | result = NULL; |
AzureIoTClient | 75:86205ca63a59 | 163 | } |
AzureIoTClient | 75:86205ca63a59 | 164 | else if (mallocAndStrcpy_s(&result->device_id, device_id) != 0) |
AzureIoTClient | 75:86205ca63a59 | 165 | { |
AzureIoTClient | 89:a2ed767a532e | 166 | LogError("Failed allocating device_id"); |
AzureIoTClient | 75:86205ca63a59 | 167 | iothub_device_auth_destroy(result->device_auth_handle); |
AzureIoTClient | 75:86205ca63a59 | 168 | free(result); |
AzureIoTClient | 75:86205ca63a59 | 169 | result = NULL; |
AzureIoTClient | 75:86205ca63a59 | 170 | } |
AzureIoTClient | 89:a2ed767a532e | 171 | else if ((module_id != NULL) && (mallocAndStrcpy_s(&result->module_id, module_id) != 0)) |
AzureIoTClient | 89:a2ed767a532e | 172 | { |
AzureIoTClient | 89:a2ed767a532e | 173 | LogError("Failed allocating module_id"); |
AzureIoTClient | 89:a2ed767a532e | 174 | iothub_device_auth_destroy(result->device_auth_handle); |
AzureIoTClient | 89:a2ed767a532e | 175 | free(result->device_id); |
AzureIoTClient | 89:a2ed767a532e | 176 | free(result); |
AzureIoTClient | 89:a2ed767a532e | 177 | result = NULL; |
AzureIoTClient | 89:a2ed767a532e | 178 | } |
AzureIoTClient | 75:86205ca63a59 | 179 | else |
AzureIoTClient | 75:86205ca63a59 | 180 | { |
AzureIoTClient | 78:74a8d3068204 | 181 | if (iothub_device_auth_get_type(result->device_auth_handle) == AUTH_TYPE_SAS) |
AzureIoTClient | 75:86205ca63a59 | 182 | { |
AzureIoTClient | 75:86205ca63a59 | 183 | result->cred_type = IOTHUB_CREDENTIAL_TYPE_DEVICE_AUTH; |
AzureIoTClient | 75:86205ca63a59 | 184 | } |
AzureIoTClient | 75:86205ca63a59 | 185 | else |
AzureIoTClient | 75:86205ca63a59 | 186 | { |
AzureIoTClient | 75:86205ca63a59 | 187 | result->cred_type = IOTHUB_CREDENTIAL_TYPE_X509_ECC; |
AzureIoTClient | 75:86205ca63a59 | 188 | } |
AzureIoTClient | 75:86205ca63a59 | 189 | } |
AzureIoTClient | 75:86205ca63a59 | 190 | } |
AzureIoTClient | 75:86205ca63a59 | 191 | #else |
AzureIoTClient | 89:a2ed767a532e | 192 | (void)module_id; |
AzureIoTClient | 78:74a8d3068204 | 193 | LogError("Failed HSM module is not supported"); |
AzureIoTClient | 75:86205ca63a59 | 194 | result = NULL; |
AzureIoTClient | 75:86205ca63a59 | 195 | #endif |
AzureIoTClient | 75:86205ca63a59 | 196 | } |
AzureIoTClient | 75:86205ca63a59 | 197 | return result; |
AzureIoTClient | 75:86205ca63a59 | 198 | } |
AzureIoTClient | 62:5a4cdacf5090 | 199 | |
AzureIoTClient | 62:5a4cdacf5090 | 200 | void IoTHubClient_Auth_Destroy(IOTHUB_AUTHORIZATION_HANDLE handle) |
AzureIoTClient | 62:5a4cdacf5090 | 201 | { |
AzureIoTClient | 62:5a4cdacf5090 | 202 | /* Codes_SRS_IoTHub_Authorization_07_005: [ if handle is NULL IoTHubClient_Auth_Destroy shall do nothing. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 203 | if (handle != NULL) |
AzureIoTClient | 62:5a4cdacf5090 | 204 | { |
AzureIoTClient | 62:5a4cdacf5090 | 205 | /* Codes_SRS_IoTHub_Authorization_07_006: [ IoTHubClient_Auth_Destroy shall free all resources associated with the IOTHUB_AUTHORIZATION_HANDLE handle. ] */ |
AzureIoTClient | 78:74a8d3068204 | 206 | #ifdef USE_PROV_MODULE |
AzureIoTClient | 75:86205ca63a59 | 207 | iothub_device_auth_destroy(handle->device_auth_handle); |
AzureIoTClient | 75:86205ca63a59 | 208 | #endif |
AzureIoTClient | 62:5a4cdacf5090 | 209 | free(handle->device_key); |
AzureIoTClient | 62:5a4cdacf5090 | 210 | free(handle->device_id); |
AzureIoTClient | 89:a2ed767a532e | 211 | free(handle->module_id); |
AzureIoTClient | 62:5a4cdacf5090 | 212 | free(handle->device_sas_token); |
AzureIoTClient | 62:5a4cdacf5090 | 213 | free(handle); |
AzureIoTClient | 62:5a4cdacf5090 | 214 | } |
AzureIoTClient | 62:5a4cdacf5090 | 215 | } |
AzureIoTClient | 62:5a4cdacf5090 | 216 | |
AzureIoTClient | 63:1bf1c2d60aab | 217 | IOTHUB_CREDENTIAL_TYPE IoTHubClient_Auth_Set_x509_Type(IOTHUB_AUTHORIZATION_HANDLE handle, bool enable_x509) |
AzureIoTClient | 63:1bf1c2d60aab | 218 | { |
AzureIoTClient | 63:1bf1c2d60aab | 219 | IOTHUB_CREDENTIAL_TYPE result; |
AzureIoTClient | 63:1bf1c2d60aab | 220 | if (handle != NULL) |
AzureIoTClient | 63:1bf1c2d60aab | 221 | { |
AzureIoTClient | 63:1bf1c2d60aab | 222 | if (enable_x509) |
AzureIoTClient | 63:1bf1c2d60aab | 223 | { |
AzureIoTClient | 63:1bf1c2d60aab | 224 | result = handle->cred_type = IOTHUB_CREDENTIAL_TYPE_X509; |
AzureIoTClient | 63:1bf1c2d60aab | 225 | } |
AzureIoTClient | 63:1bf1c2d60aab | 226 | else |
AzureIoTClient | 63:1bf1c2d60aab | 227 | { |
AzureIoTClient | 63:1bf1c2d60aab | 228 | if (handle->device_sas_token == NULL) |
AzureIoTClient | 63:1bf1c2d60aab | 229 | { |
AzureIoTClient | 63:1bf1c2d60aab | 230 | result = handle->cred_type = IOTHUB_CREDENTIAL_TYPE_DEVICE_KEY; |
AzureIoTClient | 63:1bf1c2d60aab | 231 | } |
AzureIoTClient | 63:1bf1c2d60aab | 232 | else if (handle->device_key == NULL) |
AzureIoTClient | 63:1bf1c2d60aab | 233 | { |
AzureIoTClient | 63:1bf1c2d60aab | 234 | result = handle->cred_type = IOTHUB_CREDENTIAL_TYPE_SAS_TOKEN; |
AzureIoTClient | 63:1bf1c2d60aab | 235 | } |
AzureIoTClient | 63:1bf1c2d60aab | 236 | else |
AzureIoTClient | 63:1bf1c2d60aab | 237 | { |
AzureIoTClient | 63:1bf1c2d60aab | 238 | result = handle->cred_type = IOTHUB_CREDENTIAL_TYPE_UNKNOWN; |
AzureIoTClient | 63:1bf1c2d60aab | 239 | } |
AzureIoTClient | 63:1bf1c2d60aab | 240 | } |
AzureIoTClient | 63:1bf1c2d60aab | 241 | } |
AzureIoTClient | 63:1bf1c2d60aab | 242 | else |
AzureIoTClient | 63:1bf1c2d60aab | 243 | { |
AzureIoTClient | 63:1bf1c2d60aab | 244 | result = IOTHUB_CREDENTIAL_TYPE_UNKNOWN; |
AzureIoTClient | 63:1bf1c2d60aab | 245 | } |
AzureIoTClient | 63:1bf1c2d60aab | 246 | return result; |
AzureIoTClient | 63:1bf1c2d60aab | 247 | } |
AzureIoTClient | 63:1bf1c2d60aab | 248 | |
AzureIoTClient | 75:86205ca63a59 | 249 | int IoTHubClient_Auth_Set_xio_Certificate(IOTHUB_AUTHORIZATION_HANDLE handle, XIO_HANDLE xio) |
AzureIoTClient | 75:86205ca63a59 | 250 | { |
AzureIoTClient | 75:86205ca63a59 | 251 | int result; |
AzureIoTClient | 75:86205ca63a59 | 252 | if (handle == NULL || xio == NULL) |
AzureIoTClient | 75:86205ca63a59 | 253 | { |
AzureIoTClient | 75:86205ca63a59 | 254 | LogError("Invalid Parameter handle: %p xio: %p", handle, xio); |
AzureIoTClient | 75:86205ca63a59 | 255 | result = __FAILURE__; |
AzureIoTClient | 75:86205ca63a59 | 256 | } |
AzureIoTClient | 75:86205ca63a59 | 257 | else if (handle->cred_type != IOTHUB_CREDENTIAL_TYPE_X509_ECC) |
AzureIoTClient | 75:86205ca63a59 | 258 | { |
AzureIoTClient | 75:86205ca63a59 | 259 | LogError("Invalid credential types for this operation"); |
AzureIoTClient | 75:86205ca63a59 | 260 | result = __FAILURE__; |
AzureIoTClient | 75:86205ca63a59 | 261 | } |
AzureIoTClient | 75:86205ca63a59 | 262 | else |
AzureIoTClient | 75:86205ca63a59 | 263 | { |
AzureIoTClient | 78:74a8d3068204 | 264 | #ifdef USE_PROV_MODULE |
AzureIoTClient | 75:86205ca63a59 | 265 | CREDENTIAL_RESULT* cred_result = iothub_device_auth_generate_credentials(handle->device_auth_handle, NULL); |
AzureIoTClient | 75:86205ca63a59 | 266 | if (cred_result == NULL) |
AzureIoTClient | 75:86205ca63a59 | 267 | { |
AzureIoTClient | 75:86205ca63a59 | 268 | LogError("Failure generating credentials"); |
AzureIoTClient | 75:86205ca63a59 | 269 | result = __FAILURE__; |
AzureIoTClient | 75:86205ca63a59 | 270 | } |
AzureIoTClient | 75:86205ca63a59 | 271 | else |
AzureIoTClient | 75:86205ca63a59 | 272 | { |
AzureIoTClient | 75:86205ca63a59 | 273 | if (xio_setoption(xio, OPTION_X509_ECC_CERT, cred_result->auth_cred_result.x509_result.x509_cert) != 0) |
AzureIoTClient | 75:86205ca63a59 | 274 | { |
AzureIoTClient | 75:86205ca63a59 | 275 | LogError("Failure setting x509 cert on xio"); |
AzureIoTClient | 75:86205ca63a59 | 276 | result = __FAILURE__; |
AzureIoTClient | 75:86205ca63a59 | 277 | } |
AzureIoTClient | 75:86205ca63a59 | 278 | else if (xio_setoption(xio, OPTION_X509_ECC_KEY, cred_result->auth_cred_result.x509_result.x509_alias_key) != 0) |
AzureIoTClient | 75:86205ca63a59 | 279 | { |
AzureIoTClient | 75:86205ca63a59 | 280 | LogError("Failure setting x509 key on xio"); |
AzureIoTClient | 75:86205ca63a59 | 281 | result = __FAILURE__; |
AzureIoTClient | 75:86205ca63a59 | 282 | } |
AzureIoTClient | 75:86205ca63a59 | 283 | else |
AzureIoTClient | 75:86205ca63a59 | 284 | { |
AzureIoTClient | 75:86205ca63a59 | 285 | result = 0; |
AzureIoTClient | 75:86205ca63a59 | 286 | } |
AzureIoTClient | 75:86205ca63a59 | 287 | free(cred_result); |
AzureIoTClient | 75:86205ca63a59 | 288 | } |
AzureIoTClient | 75:86205ca63a59 | 289 | #else |
AzureIoTClient | 78:74a8d3068204 | 290 | LogError("Failed HSM module is not supported"); |
AzureIoTClient | 75:86205ca63a59 | 291 | result = __FAILURE__; |
AzureIoTClient | 75:86205ca63a59 | 292 | #endif |
AzureIoTClient | 75:86205ca63a59 | 293 | } |
AzureIoTClient | 75:86205ca63a59 | 294 | return result; |
AzureIoTClient | 75:86205ca63a59 | 295 | } |
AzureIoTClient | 75:86205ca63a59 | 296 | |
AzureIoTClient | 62:5a4cdacf5090 | 297 | IOTHUB_CREDENTIAL_TYPE IoTHubClient_Auth_Get_Credential_Type(IOTHUB_AUTHORIZATION_HANDLE handle) |
AzureIoTClient | 62:5a4cdacf5090 | 298 | { |
AzureIoTClient | 62:5a4cdacf5090 | 299 | IOTHUB_CREDENTIAL_TYPE result; |
AzureIoTClient | 62:5a4cdacf5090 | 300 | /* Codes_SRS_IoTHub_Authorization_07_007: [ if handle is NULL IoTHub_Auth_Get_Credential_Type shall return IOTHUB_CREDENTIAL_TYPE_UNKNOWN. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 301 | if (handle == NULL) |
AzureIoTClient | 62:5a4cdacf5090 | 302 | { |
AzureIoTClient | 62:5a4cdacf5090 | 303 | LogError("Invalid Parameter handle: %p", handle); |
AzureIoTClient | 62:5a4cdacf5090 | 304 | result = IOTHUB_CREDENTIAL_TYPE_UNKNOWN; |
AzureIoTClient | 62:5a4cdacf5090 | 305 | } |
AzureIoTClient | 62:5a4cdacf5090 | 306 | else |
AzureIoTClient | 62:5a4cdacf5090 | 307 | { |
AzureIoTClient | 62:5a4cdacf5090 | 308 | /* Codes_SRS_IoTHub_Authorization_07_008: [ IoTHub_Auth_Get_Credential_Type shall return the credential type that is set upon creation. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 309 | result = handle->cred_type; |
AzureIoTClient | 62:5a4cdacf5090 | 310 | } |
AzureIoTClient | 62:5a4cdacf5090 | 311 | return result; |
AzureIoTClient | 62:5a4cdacf5090 | 312 | } |
AzureIoTClient | 62:5a4cdacf5090 | 313 | |
AzureIoTClient | 88:248736be106e | 314 | char* IoTHubClient_Auth_Get_SasToken(IOTHUB_AUTHORIZATION_HANDLE handle, const char* scope, size_t expiry_time_relative_seconds, const char* key_name) |
AzureIoTClient | 62:5a4cdacf5090 | 315 | { |
AzureIoTClient | 62:5a4cdacf5090 | 316 | char* result; |
AzureIoTClient | 63:1bf1c2d60aab | 317 | /* Codes_SRS_IoTHub_Authorization_07_009: [ if handle or scope are NULL, IoTHubClient_Auth_Get_SasToken shall return NULL. ] */ |
AzureIoTClient | 63:1bf1c2d60aab | 318 | if (handle == NULL) |
AzureIoTClient | 62:5a4cdacf5090 | 319 | { |
AzureIoTClient | 63:1bf1c2d60aab | 320 | LogError("Invalid Parameter handle: %p", handle); |
AzureIoTClient | 62:5a4cdacf5090 | 321 | result = NULL; |
AzureIoTClient | 62:5a4cdacf5090 | 322 | } |
AzureIoTClient | 62:5a4cdacf5090 | 323 | else |
AzureIoTClient | 62:5a4cdacf5090 | 324 | { |
AzureIoTClient | 75:86205ca63a59 | 325 | if (handle->cred_type == IOTHUB_CREDENTIAL_TYPE_DEVICE_AUTH) |
AzureIoTClient | 62:5a4cdacf5090 | 326 | { |
AzureIoTClient | 78:74a8d3068204 | 327 | #ifdef USE_PROV_MODULE |
AzureIoTClient | 75:86205ca63a59 | 328 | DEVICE_AUTH_CREDENTIAL_INFO dev_auth_cred; |
AzureIoTClient | 63:1bf1c2d60aab | 329 | size_t sec_since_epoch; |
AzureIoTClient | 63:1bf1c2d60aab | 330 | |
AzureIoTClient | 63:1bf1c2d60aab | 331 | if (get_seconds_since_epoch(&sec_since_epoch) != 0) |
AzureIoTClient | 62:5a4cdacf5090 | 332 | { |
AzureIoTClient | 63:1bf1c2d60aab | 333 | LogError("failure getting seconds from epoch"); |
AzureIoTClient | 62:5a4cdacf5090 | 334 | result = NULL; |
AzureIoTClient | 62:5a4cdacf5090 | 335 | } |
AzureIoTClient | 63:1bf1c2d60aab | 336 | else |
AzureIoTClient | 62:5a4cdacf5090 | 337 | { |
AzureIoTClient | 88:248736be106e | 338 | memset(&dev_auth_cred, 0, sizeof(DEVICE_AUTH_CREDENTIAL_INFO)); |
AzureIoTClient | 76:943524fee0b7 | 339 | size_t expiry_time = sec_since_epoch+expiry_time_relative_seconds; |
AzureIoTClient | 75:86205ca63a59 | 340 | dev_auth_cred.sas_info.expiry_seconds = expiry_time; |
AzureIoTClient | 75:86205ca63a59 | 341 | dev_auth_cred.sas_info.token_scope = scope; |
AzureIoTClient | 88:248736be106e | 342 | dev_auth_cred.sas_info.key_name = key_name; |
AzureIoTClient | 75:86205ca63a59 | 343 | dev_auth_cred.dev_auth_type = AUTH_TYPE_SAS; |
AzureIoTClient | 75:86205ca63a59 | 344 | |
AzureIoTClient | 75:86205ca63a59 | 345 | CREDENTIAL_RESULT* cred_result = iothub_device_auth_generate_credentials(handle->device_auth_handle, &dev_auth_cred); |
AzureIoTClient | 75:86205ca63a59 | 346 | if (cred_result == NULL) |
AzureIoTClient | 62:5a4cdacf5090 | 347 | { |
AzureIoTClient | 75:86205ca63a59 | 348 | LogError("failure getting credentials from device auth module"); |
AzureIoTClient | 62:5a4cdacf5090 | 349 | result = NULL; |
AzureIoTClient | 62:5a4cdacf5090 | 350 | } |
AzureIoTClient | 63:1bf1c2d60aab | 351 | else |
AzureIoTClient | 63:1bf1c2d60aab | 352 | { |
AzureIoTClient | 75:86205ca63a59 | 353 | if (mallocAndStrcpy_s(&result, cred_result->auth_cred_result.sas_result.sas_token) != 0) |
AzureIoTClient | 75:86205ca63a59 | 354 | { |
AzureIoTClient | 75:86205ca63a59 | 355 | LogError("failure allocating Sas Token"); |
AzureIoTClient | 75:86205ca63a59 | 356 | result = NULL; |
AzureIoTClient | 75:86205ca63a59 | 357 | } |
AzureIoTClient | 75:86205ca63a59 | 358 | free(cred_result); |
AzureIoTClient | 75:86205ca63a59 | 359 | } |
AzureIoTClient | 75:86205ca63a59 | 360 | } |
AzureIoTClient | 75:86205ca63a59 | 361 | #else |
AzureIoTClient | 78:74a8d3068204 | 362 | LogError("Failed HSM module is not supported"); |
AzureIoTClient | 75:86205ca63a59 | 363 | result = NULL; |
AzureIoTClient | 75:86205ca63a59 | 364 | #endif |
AzureIoTClient | 75:86205ca63a59 | 365 | } |
AzureIoTClient | 75:86205ca63a59 | 366 | else if (handle->cred_type == IOTHUB_CREDENTIAL_TYPE_SAS_TOKEN) |
AzureIoTClient | 75:86205ca63a59 | 367 | { |
AzureIoTClient | 75:86205ca63a59 | 368 | /* Codes_SRS_IoTHub_Authorization_07_021: [If the device_sas_token is NOT NULL IoTHubClient_Auth_Get_SasToken shall return a copy of the device_sas_token. ] */ |
AzureIoTClient | 75:86205ca63a59 | 369 | if (handle->device_sas_token != NULL) |
AzureIoTClient | 75:86205ca63a59 | 370 | { |
AzureIoTClient | 75:86205ca63a59 | 371 | if (mallocAndStrcpy_s(&result, handle->device_sas_token) != 0) |
AzureIoTClient | 75:86205ca63a59 | 372 | { |
AzureIoTClient | 75:86205ca63a59 | 373 | LogError("failure allocating sas token"); |
AzureIoTClient | 75:86205ca63a59 | 374 | result = NULL; |
AzureIoTClient | 75:86205ca63a59 | 375 | } |
AzureIoTClient | 75:86205ca63a59 | 376 | } |
AzureIoTClient | 75:86205ca63a59 | 377 | else |
AzureIoTClient | 75:86205ca63a59 | 378 | { |
AzureIoTClient | 75:86205ca63a59 | 379 | LogError("failure device sas token is NULL"); |
AzureIoTClient | 75:86205ca63a59 | 380 | result = NULL; |
AzureIoTClient | 75:86205ca63a59 | 381 | } |
AzureIoTClient | 75:86205ca63a59 | 382 | } |
AzureIoTClient | 75:86205ca63a59 | 383 | else if (handle->cred_type == IOTHUB_CREDENTIAL_TYPE_DEVICE_KEY) |
AzureIoTClient | 75:86205ca63a59 | 384 | { |
AzureIoTClient | 75:86205ca63a59 | 385 | /* Codes_SRS_IoTHub_Authorization_07_009: [ if handle or scope are NULL, IoTHubClient_Auth_Get_SasToken shall return NULL. ] */ |
AzureIoTClient | 75:86205ca63a59 | 386 | if (scope == NULL) |
AzureIoTClient | 75:86205ca63a59 | 387 | { |
AzureIoTClient | 75:86205ca63a59 | 388 | LogError("Invalid Parameter scope: %p", scope); |
AzureIoTClient | 75:86205ca63a59 | 389 | result = NULL; |
AzureIoTClient | 75:86205ca63a59 | 390 | } |
AzureIoTClient | 75:86205ca63a59 | 391 | else |
AzureIoTClient | 75:86205ca63a59 | 392 | { |
AzureIoTClient | 75:86205ca63a59 | 393 | STRING_HANDLE sas_token; |
AzureIoTClient | 75:86205ca63a59 | 394 | size_t sec_since_epoch; |
AzureIoTClient | 75:86205ca63a59 | 395 | |
AzureIoTClient | 76:943524fee0b7 | 396 | /* Codes_SRS_IoTHub_Authorization_07_010: [ IoTHubClient_Auth_Get_SasToken` shall construct the expiration time using the expiry_time_relative_seconds added to epoch time. ] */ |
AzureIoTClient | 75:86205ca63a59 | 397 | if (get_seconds_since_epoch(&sec_since_epoch) != 0) |
AzureIoTClient | 75:86205ca63a59 | 398 | { |
AzureIoTClient | 75:86205ca63a59 | 399 | /* Codes_SRS_IoTHub_Authorization_07_020: [ If any error is encountered IoTHubClient_Auth_Get_ConnString shall return NULL. ] */ |
AzureIoTClient | 75:86205ca63a59 | 400 | LogError("failure getting seconds from epoch"); |
AzureIoTClient | 75:86205ca63a59 | 401 | result = NULL; |
AzureIoTClient | 75:86205ca63a59 | 402 | } |
AzureIoTClient | 75:86205ca63a59 | 403 | else |
AzureIoTClient | 75:86205ca63a59 | 404 | { |
AzureIoTClient | 75:86205ca63a59 | 405 | /* Codes_SRS_IoTHub_Authorization_07_011: [ IoTHubClient_Auth_Get_ConnString shall call SASToken_CreateString to construct the sas token. ] */ |
AzureIoTClient | 76:943524fee0b7 | 406 | size_t expiry_time = sec_since_epoch+expiry_time_relative_seconds; |
AzureIoTClient | 75:86205ca63a59 | 407 | if ( (sas_token = SASToken_CreateString(handle->device_key, scope, key_name, expiry_time)) == NULL) |
AzureIoTClient | 63:1bf1c2d60aab | 408 | { |
AzureIoTClient | 63:1bf1c2d60aab | 409 | /* Codes_SRS_IoTHub_Authorization_07_020: [ If any error is encountered IoTHubClient_Auth_Get_ConnString shall return NULL. ] */ |
AzureIoTClient | 75:86205ca63a59 | 410 | LogError("Failed creating sas_token"); |
AzureIoTClient | 63:1bf1c2d60aab | 411 | result = NULL; |
AzureIoTClient | 63:1bf1c2d60aab | 412 | } |
AzureIoTClient | 75:86205ca63a59 | 413 | else |
AzureIoTClient | 75:86205ca63a59 | 414 | { |
AzureIoTClient | 75:86205ca63a59 | 415 | /* Codes_SRS_IoTHub_Authorization_07_012: [ On success IoTHubClient_Auth_Get_ConnString shall allocate and return the sas token in a char*. ] */ |
AzureIoTClient | 75:86205ca63a59 | 416 | if (mallocAndStrcpy_s(&result, STRING_c_str(sas_token) ) != 0) |
AzureIoTClient | 75:86205ca63a59 | 417 | { |
AzureIoTClient | 75:86205ca63a59 | 418 | /* Codes_SRS_IoTHub_Authorization_07_020: [ If any error is encountered IoTHubClient_Auth_Get_ConnString shall return NULL. ] */ |
AzureIoTClient | 75:86205ca63a59 | 419 | LogError("Failed copying result"); |
AzureIoTClient | 75:86205ca63a59 | 420 | result = NULL; |
AzureIoTClient | 75:86205ca63a59 | 421 | } |
AzureIoTClient | 75:86205ca63a59 | 422 | STRING_delete(sas_token); |
AzureIoTClient | 75:86205ca63a59 | 423 | } |
AzureIoTClient | 63:1bf1c2d60aab | 424 | } |
AzureIoTClient | 62:5a4cdacf5090 | 425 | } |
AzureIoTClient | 62:5a4cdacf5090 | 426 | } |
AzureIoTClient | 75:86205ca63a59 | 427 | else |
AzureIoTClient | 75:86205ca63a59 | 428 | { |
AzureIoTClient | 75:86205ca63a59 | 429 | LogError("Failed getting sas token invalid credential type"); |
AzureIoTClient | 75:86205ca63a59 | 430 | result = NULL; |
AzureIoTClient | 75:86205ca63a59 | 431 | } |
AzureIoTClient | 62:5a4cdacf5090 | 432 | } |
AzureIoTClient | 62:5a4cdacf5090 | 433 | return result; |
AzureIoTClient | 62:5a4cdacf5090 | 434 | } |
AzureIoTClient | 62:5a4cdacf5090 | 435 | |
AzureIoTClient | 62:5a4cdacf5090 | 436 | const char* IoTHubClient_Auth_Get_DeviceId(IOTHUB_AUTHORIZATION_HANDLE handle) |
AzureIoTClient | 62:5a4cdacf5090 | 437 | { |
AzureIoTClient | 62:5a4cdacf5090 | 438 | const char* result; |
AzureIoTClient | 62:5a4cdacf5090 | 439 | if (handle == NULL) |
AzureIoTClient | 62:5a4cdacf5090 | 440 | { |
AzureIoTClient | 62:5a4cdacf5090 | 441 | /* Codes_SRS_IoTHub_Authorization_07_013: [ if handle is NULL, IoTHubClient_Auth_Get_DeviceId shall return NULL. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 442 | LogError("Invalid Parameter handle: %p", handle); |
AzureIoTClient | 62:5a4cdacf5090 | 443 | result = NULL; |
AzureIoTClient | 62:5a4cdacf5090 | 444 | } |
AzureIoTClient | 62:5a4cdacf5090 | 445 | else |
AzureIoTClient | 62:5a4cdacf5090 | 446 | { |
AzureIoTClient | 62:5a4cdacf5090 | 447 | /* Codes_SRS_IoTHub_Authorization_07_014: [ IoTHubClient_Auth_Get_DeviceId shall return the device_id specified upon creation. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 448 | result = handle->device_id; |
AzureIoTClient | 62:5a4cdacf5090 | 449 | } |
AzureIoTClient | 62:5a4cdacf5090 | 450 | return result; |
AzureIoTClient | 62:5a4cdacf5090 | 451 | } |
AzureIoTClient | 62:5a4cdacf5090 | 452 | |
AzureIoTClient | 89:a2ed767a532e | 453 | const char* IoTHubClient_Auth_Get_ModuleId(IOTHUB_AUTHORIZATION_HANDLE handle) |
AzureIoTClient | 89:a2ed767a532e | 454 | { |
AzureIoTClient | 89:a2ed767a532e | 455 | const char* result; |
AzureIoTClient | 89:a2ed767a532e | 456 | if (handle == NULL) |
AzureIoTClient | 89:a2ed767a532e | 457 | { |
AzureIoTClient | 89:a2ed767a532e | 458 | /* Codes_SRS_IoTHub_Authorization_31_025: [ if handle is NULL, IoTHubClient_Auth_Get_ModuleId shall return NULL. ] */ |
AzureIoTClient | 89:a2ed767a532e | 459 | LogError("Invalid Parameter handle: %p", handle); |
AzureIoTClient | 89:a2ed767a532e | 460 | result = NULL; |
AzureIoTClient | 89:a2ed767a532e | 461 | } |
AzureIoTClient | 89:a2ed767a532e | 462 | else |
AzureIoTClient | 89:a2ed767a532e | 463 | { |
AzureIoTClient | 89:a2ed767a532e | 464 | /* Codes_SRS_IoTHub_Authorization_31_026: [ IoTHubClient_Auth_Get_ModuleId shall return the module_id specified upon creation. ] */ |
AzureIoTClient | 89:a2ed767a532e | 465 | result = handle->module_id; |
AzureIoTClient | 89:a2ed767a532e | 466 | } |
AzureIoTClient | 89:a2ed767a532e | 467 | return result; |
AzureIoTClient | 89:a2ed767a532e | 468 | } |
AzureIoTClient | 89:a2ed767a532e | 469 | |
AzureIoTClient | 63:1bf1c2d60aab | 470 | const char* IoTHubClient_Auth_Get_DeviceKey(IOTHUB_AUTHORIZATION_HANDLE handle) |
AzureIoTClient | 62:5a4cdacf5090 | 471 | { |
AzureIoTClient | 63:1bf1c2d60aab | 472 | const char* result; |
AzureIoTClient | 63:1bf1c2d60aab | 473 | if (handle == NULL) |
AzureIoTClient | 63:1bf1c2d60aab | 474 | { |
AzureIoTClient | 63:1bf1c2d60aab | 475 | /* Codes_SRS_IoTHub_Authorization_07_022: [ if handle is NULL, IoTHubClient_Auth_Get_DeviceKey shall return NULL. ] */ |
AzureIoTClient | 63:1bf1c2d60aab | 476 | LogError("Invalid Parameter handle: %p", handle); |
AzureIoTClient | 63:1bf1c2d60aab | 477 | result = NULL; |
AzureIoTClient | 63:1bf1c2d60aab | 478 | } |
AzureIoTClient | 63:1bf1c2d60aab | 479 | else |
AzureIoTClient | 63:1bf1c2d60aab | 480 | { |
AzureIoTClient | 63:1bf1c2d60aab | 481 | /* Codes_SRS_IoTHub_Authorization_07_023: [ IoTHubClient_Auth_Get_DeviceKey shall return the device_key specified upon creation. ] */ |
AzureIoTClient | 63:1bf1c2d60aab | 482 | result = handle->device_key; |
AzureIoTClient | 63:1bf1c2d60aab | 483 | } |
AzureIoTClient | 63:1bf1c2d60aab | 484 | return result; |
AzureIoTClient | 63:1bf1c2d60aab | 485 | } |
AzureIoTClient | 63:1bf1c2d60aab | 486 | |
AzureIoTClient | 63:1bf1c2d60aab | 487 | SAS_TOKEN_STATUS IoTHubClient_Auth_Is_SasToken_Valid(IOTHUB_AUTHORIZATION_HANDLE handle) |
AzureIoTClient | 63:1bf1c2d60aab | 488 | { |
AzureIoTClient | 63:1bf1c2d60aab | 489 | SAS_TOKEN_STATUS result; |
AzureIoTClient | 62:5a4cdacf5090 | 490 | if (handle == NULL) |
AzureIoTClient | 62:5a4cdacf5090 | 491 | { |
AzureIoTClient | 62:5a4cdacf5090 | 492 | /* Codes_SRS_IoTHub_Authorization_07_015: [ if handle is NULL, IoTHubClient_Auth_Is_SasToken_Valid shall return false. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 493 | LogError("Invalid Parameter handle: %p", handle); |
AzureIoTClient | 63:1bf1c2d60aab | 494 | result = SAS_TOKEN_STATUS_FAILED; |
AzureIoTClient | 62:5a4cdacf5090 | 495 | } |
AzureIoTClient | 62:5a4cdacf5090 | 496 | else |
AzureIoTClient | 62:5a4cdacf5090 | 497 | { |
AzureIoTClient | 62:5a4cdacf5090 | 498 | if (handle->cred_type == IOTHUB_CREDENTIAL_TYPE_SAS_TOKEN) |
AzureIoTClient | 62:5a4cdacf5090 | 499 | { |
AzureIoTClient | 62:5a4cdacf5090 | 500 | if (handle->device_sas_token == NULL) |
AzureIoTClient | 62:5a4cdacf5090 | 501 | { |
AzureIoTClient | 62:5a4cdacf5090 | 502 | /* Codes_SRS_IoTHub_Authorization_07_017: [ If the sas_token is NULL IoTHubClient_Auth_Is_SasToken_Valid shall return false. ] */ |
AzureIoTClient | 63:1bf1c2d60aab | 503 | LogError("Failure: device_sas_toke is NULL"); |
AzureIoTClient | 63:1bf1c2d60aab | 504 | result = SAS_TOKEN_STATUS_FAILED; |
AzureIoTClient | 62:5a4cdacf5090 | 505 | } |
AzureIoTClient | 62:5a4cdacf5090 | 506 | else |
AzureIoTClient | 62:5a4cdacf5090 | 507 | { |
AzureIoTClient | 62:5a4cdacf5090 | 508 | /* Codes_SRS_IoTHub_Authorization_07_018: [ otherwise IoTHubClient_Auth_Is_SasToken_Valid shall return the value returned by SASToken_Validate. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 509 | STRING_HANDLE strSasToken = STRING_construct(handle->device_sas_token); |
AzureIoTClient | 63:1bf1c2d60aab | 510 | if (strSasToken != NULL) |
AzureIoTClient | 63:1bf1c2d60aab | 511 | { |
AzureIoTClient | 63:1bf1c2d60aab | 512 | if (!SASToken_Validate(strSasToken)) |
AzureIoTClient | 63:1bf1c2d60aab | 513 | { |
AzureIoTClient | 63:1bf1c2d60aab | 514 | result = SAS_TOKEN_STATUS_INVALID; |
AzureIoTClient | 63:1bf1c2d60aab | 515 | } |
AzureIoTClient | 63:1bf1c2d60aab | 516 | else |
AzureIoTClient | 63:1bf1c2d60aab | 517 | { |
AzureIoTClient | 63:1bf1c2d60aab | 518 | result = SAS_TOKEN_STATUS_VALID; |
AzureIoTClient | 63:1bf1c2d60aab | 519 | } |
AzureIoTClient | 63:1bf1c2d60aab | 520 | STRING_delete(strSasToken); |
AzureIoTClient | 63:1bf1c2d60aab | 521 | } |
AzureIoTClient | 63:1bf1c2d60aab | 522 | else |
AzureIoTClient | 63:1bf1c2d60aab | 523 | { |
AzureIoTClient | 63:1bf1c2d60aab | 524 | LogError("Failure constructing SAS Token"); |
AzureIoTClient | 63:1bf1c2d60aab | 525 | result = SAS_TOKEN_STATUS_FAILED; |
AzureIoTClient | 63:1bf1c2d60aab | 526 | } |
AzureIoTClient | 62:5a4cdacf5090 | 527 | } |
AzureIoTClient | 62:5a4cdacf5090 | 528 | } |
AzureIoTClient | 62:5a4cdacf5090 | 529 | else |
AzureIoTClient | 62:5a4cdacf5090 | 530 | { |
AzureIoTClient | 88:248736be106e | 531 | /* Codes_SRS_IoTHub_Authorization_07_016: [ if credential type is not IOTHUB_CREDENTIAL_TYPE_SAS_TOKEN IoTHubClient_Auth_Is_SasToken_Valid shall return SAS_TOKEN_STATUS_VALID. ] */ |
AzureIoTClient | 88:248736be106e | 532 | result = SAS_TOKEN_STATUS_VALID; |
AzureIoTClient | 62:5a4cdacf5090 | 533 | } |
AzureIoTClient | 62:5a4cdacf5090 | 534 | } |
AzureIoTClient | 62:5a4cdacf5090 | 535 | return result; |
AzureIoTClient | 62:5a4cdacf5090 | 536 | } |
AzureIoTClient | 89:a2ed767a532e | 537 | |
AzureIoTClient | 89:a2ed767a532e | 538 | |
AzureIoTClient | 89:a2ed767a532e | 539 | #ifdef USE_EDGE_MODULES |
AzureIoTClient | 89:a2ed767a532e | 540 | char* IoTHubClient_Auth_Get_TrustBundle(IOTHUB_AUTHORIZATION_HANDLE handle) |
AzureIoTClient | 89:a2ed767a532e | 541 | { |
AzureIoTClient | 89:a2ed767a532e | 542 | return iothub_device_auth_get_trust_bundle(handle->device_auth_handle); |
AzureIoTClient | 89:a2ed767a532e | 543 | } |
AzureIoTClient | 89:a2ed767a532e | 544 | #endif |
AzureIoTClient | 89:a2ed767a532e | 545 |