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

Committer:
AzureIoTClient
Date:
Fri Nov 17 13:57:39 2017 -0800
Revision:
79:bb88037c05e6
Parent:
78:74a8d3068204
Child:
88:248736be106e
1.1.28

Who changed what in which revision?

UserRevisionLine numberNew 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 78:74a8d3068204 16 #include "azure_prov_client/iothub_auth_client.h"
AzureIoTClient 75:86205ca63a59 17 #endif
AzureIoTClient 62:5a4cdacf5090 18
AzureIoTClient 62:5a4cdacf5090 19 #include "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 62:5a4cdacf5090 29 size_t token_expiry_time_sec;
AzureIoTClient 62:5a4cdacf5090 30 IOTHUB_CREDENTIAL_TYPE cred_type;
AzureIoTClient 78:74a8d3068204 31 #ifdef USE_PROV_MODULE
AzureIoTClient 75:86205ca63a59 32 IOTHUB_SECURITY_HANDLE device_auth_handle;
AzureIoTClient 75:86205ca63a59 33 #endif
AzureIoTClient 62:5a4cdacf5090 34 } IOTHUB_AUTHORIZATION_DATA;
AzureIoTClient 62:5a4cdacf5090 35
AzureIoTClient 62:5a4cdacf5090 36 static int get_seconds_since_epoch(size_t* seconds)
AzureIoTClient 62:5a4cdacf5090 37 {
AzureIoTClient 62:5a4cdacf5090 38 int result;
AzureIoTClient 62:5a4cdacf5090 39 time_t current_time;
AzureIoTClient 62:5a4cdacf5090 40 if ((current_time = get_time(NULL)) == INDEFINITE_TIME)
AzureIoTClient 62:5a4cdacf5090 41 {
AzureIoTClient 62:5a4cdacf5090 42 LogError("Failed getting the current local time (get_time() failed)");
AzureIoTClient 75:86205ca63a59 43 result = __FAILURE__;
AzureIoTClient 62:5a4cdacf5090 44 }
AzureIoTClient 62:5a4cdacf5090 45 else
AzureIoTClient 62:5a4cdacf5090 46 {
AzureIoTClient 62:5a4cdacf5090 47 *seconds = (size_t)get_difftime(current_time, (time_t)0);
AzureIoTClient 62:5a4cdacf5090 48 result = 0;
AzureIoTClient 62:5a4cdacf5090 49 }
AzureIoTClient 62:5a4cdacf5090 50 return result;
AzureIoTClient 62:5a4cdacf5090 51 }
AzureIoTClient 62:5a4cdacf5090 52
AzureIoTClient 62:5a4cdacf5090 53 IOTHUB_AUTHORIZATION_HANDLE IoTHubClient_Auth_Create(const char* device_key, const char* device_id, const char* device_sas_token)
AzureIoTClient 62:5a4cdacf5090 54 {
AzureIoTClient 62:5a4cdacf5090 55 IOTHUB_AUTHORIZATION_DATA* result;
AzureIoTClient 62:5a4cdacf5090 56 /* Codes_SRS_IoTHub_Authorization_07_001: [if device_id is NULL IoTHubClient_Auth_Create, shall return NULL. ] */
AzureIoTClient 62:5a4cdacf5090 57 if (device_id == NULL)
AzureIoTClient 62:5a4cdacf5090 58 {
AzureIoTClient 75:86205ca63a59 59 LogError("Invalid Parameter device_id: %p", device_id);
AzureIoTClient 62:5a4cdacf5090 60 result = NULL;
AzureIoTClient 62:5a4cdacf5090 61 }
AzureIoTClient 62:5a4cdacf5090 62 else
AzureIoTClient 62:5a4cdacf5090 63 {
AzureIoTClient 62:5a4cdacf5090 64 /* Codes_SRS_IoTHub_Authorization_07_002: [IoTHubClient_Auth_Create shall allocate a IOTHUB_AUTHORIZATION_HANDLE that is needed for subsequent calls. ] */
AzureIoTClient 62:5a4cdacf5090 65 result = (IOTHUB_AUTHORIZATION_DATA*)malloc(sizeof(IOTHUB_AUTHORIZATION_DATA) );
AzureIoTClient 62:5a4cdacf5090 66 if (result == NULL)
AzureIoTClient 62:5a4cdacf5090 67 {
AzureIoTClient 62:5a4cdacf5090 68 /* Codes_SRS_IoTHub_Authorization_07_019: [ On error IoTHubClient_Auth_Create shall return NULL. ] */
AzureIoTClient 62:5a4cdacf5090 69 LogError("Failed allocating IOTHUB_AUTHORIZATION_DATA");
AzureIoTClient 62:5a4cdacf5090 70 result = NULL;
AzureIoTClient 62:5a4cdacf5090 71 }
AzureIoTClient 62:5a4cdacf5090 72 else
AzureIoTClient 62:5a4cdacf5090 73 {
AzureIoTClient 62:5a4cdacf5090 74 memset(result, 0, sizeof(IOTHUB_AUTHORIZATION_DATA) );
AzureIoTClient 62:5a4cdacf5090 75 result->token_expiry_time_sec = DEFAULT_SAS_TOKEN_EXPIRY_TIME_SECS;
AzureIoTClient 62:5a4cdacf5090 76
AzureIoTClient 62:5a4cdacf5090 77 if (device_key != NULL && mallocAndStrcpy_s(&result->device_key, device_key) != 0)
AzureIoTClient 62:5a4cdacf5090 78 {
AzureIoTClient 62:5a4cdacf5090 79 /* Codes_SRS_IoTHub_Authorization_07_019: [ On error IoTHubClient_Auth_Create shall return NULL. ] */
AzureIoTClient 62:5a4cdacf5090 80 LogError("Failed allocating device_key");
AzureIoTClient 62:5a4cdacf5090 81 free(result);
AzureIoTClient 62:5a4cdacf5090 82 result = NULL;
AzureIoTClient 62:5a4cdacf5090 83 }
AzureIoTClient 62:5a4cdacf5090 84 else if (mallocAndStrcpy_s(&result->device_id, device_id) != 0)
AzureIoTClient 62:5a4cdacf5090 85 {
AzureIoTClient 62:5a4cdacf5090 86 /* Codes_SRS_IoTHub_Authorization_07_019: [ On error IoTHubClient_Auth_Create shall return NULL. ] */
AzureIoTClient 62:5a4cdacf5090 87 LogError("Failed allocating device_key");
AzureIoTClient 62:5a4cdacf5090 88 free(result->device_key);
AzureIoTClient 62:5a4cdacf5090 89 free(result);
AzureIoTClient 62:5a4cdacf5090 90 result = NULL;
AzureIoTClient 62:5a4cdacf5090 91 }
AzureIoTClient 62:5a4cdacf5090 92 else
AzureIoTClient 62:5a4cdacf5090 93 {
AzureIoTClient 63:1bf1c2d60aab 94 if (device_key != NULL)
AzureIoTClient 62:5a4cdacf5090 95 {
AzureIoTClient 62:5a4cdacf5090 96 /* 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 97 result->cred_type = IOTHUB_CREDENTIAL_TYPE_DEVICE_KEY;
AzureIoTClient 62:5a4cdacf5090 98 }
AzureIoTClient 63:1bf1c2d60aab 99 else if (device_sas_token != NULL)
AzureIoTClient 62:5a4cdacf5090 100 {
AzureIoTClient 62:5a4cdacf5090 101 /* Codes_SRS_IoTHub_Authorization_07_020: [ else IoTHubClient_Auth_Create shall set the credential type to IOTHUB_CREDENTIAL_TYPE_SAS_TOKEN. ] */
AzureIoTClient 62:5a4cdacf5090 102 result->cred_type = IOTHUB_CREDENTIAL_TYPE_SAS_TOKEN;
AzureIoTClient 62:5a4cdacf5090 103 if (mallocAndStrcpy_s(&result->device_sas_token, device_sas_token) != 0)
AzureIoTClient 62:5a4cdacf5090 104 {
AzureIoTClient 62:5a4cdacf5090 105 /* Codes_SRS_IoTHub_Authorization_07_019: [ On error IoTHubClient_Auth_Create shall return NULL. ] */
AzureIoTClient 62:5a4cdacf5090 106 LogError("Failed allocating device_key");
AzureIoTClient 62:5a4cdacf5090 107 free(result->device_key);
AzureIoTClient 62:5a4cdacf5090 108 free(result->device_id);
AzureIoTClient 62:5a4cdacf5090 109 free(result);
AzureIoTClient 62:5a4cdacf5090 110 result = NULL;
AzureIoTClient 62:5a4cdacf5090 111 }
AzureIoTClient 62:5a4cdacf5090 112 }
AzureIoTClient 63:1bf1c2d60aab 113 else
AzureIoTClient 63:1bf1c2d60aab 114 {
AzureIoTClient 63:1bf1c2d60aab 115 /* 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 116 result->cred_type = IOTHUB_CREDENTIAL_TYPE_UNKNOWN;
AzureIoTClient 63:1bf1c2d60aab 117 }
AzureIoTClient 62:5a4cdacf5090 118 }
AzureIoTClient 62:5a4cdacf5090 119 }
AzureIoTClient 62:5a4cdacf5090 120 }
AzureIoTClient 62:5a4cdacf5090 121 /* Codes_SRS_IoTHub_Authorization_07_004: [ If successful IoTHubClient_Auth_Create shall return a IOTHUB_AUTHORIZATION_HANDLE value. ] */
AzureIoTClient 62:5a4cdacf5090 122 return result;
AzureIoTClient 62:5a4cdacf5090 123 }
AzureIoTClient 62:5a4cdacf5090 124
AzureIoTClient 75:86205ca63a59 125 IOTHUB_AUTHORIZATION_HANDLE IoTHubClient_Auth_CreateFromDeviceAuth(const char* device_id)
AzureIoTClient 75:86205ca63a59 126 {
AzureIoTClient 75:86205ca63a59 127 IOTHUB_AUTHORIZATION_DATA* result;
AzureIoTClient 75:86205ca63a59 128 if (device_id == NULL)
AzureIoTClient 75:86205ca63a59 129 {
AzureIoTClient 75:86205ca63a59 130 LogError("Invalid Parameter device_id: %p", device_id);
AzureIoTClient 75:86205ca63a59 131 result = NULL;
AzureIoTClient 75:86205ca63a59 132 }
AzureIoTClient 75:86205ca63a59 133 else
AzureIoTClient 75:86205ca63a59 134 {
AzureIoTClient 78:74a8d3068204 135 #ifdef USE_PROV_MODULE
AzureIoTClient 75:86205ca63a59 136 result = (IOTHUB_AUTHORIZATION_DATA*)malloc(sizeof(IOTHUB_AUTHORIZATION_DATA));
AzureIoTClient 75:86205ca63a59 137 if (result == NULL)
AzureIoTClient 75:86205ca63a59 138 {
AzureIoTClient 75:86205ca63a59 139 LogError("Failed allocating IOTHUB_AUTHORIZATION_DATA");
AzureIoTClient 75:86205ca63a59 140 result = NULL;
AzureIoTClient 75:86205ca63a59 141 }
AzureIoTClient 75:86205ca63a59 142 else
AzureIoTClient 75:86205ca63a59 143 {
AzureIoTClient 75:86205ca63a59 144 memset(result, 0, sizeof(IOTHUB_AUTHORIZATION_DATA));
AzureIoTClient 75:86205ca63a59 145
AzureIoTClient 75:86205ca63a59 146 result->device_auth_handle = iothub_device_auth_create();
AzureIoTClient 75:86205ca63a59 147 if (result->device_auth_handle == NULL)
AzureIoTClient 75:86205ca63a59 148 {
AzureIoTClient 75:86205ca63a59 149 LogError("Failed allocating IOTHUB_AUTHORIZATION_DATA");
AzureIoTClient 75:86205ca63a59 150 free(result);
AzureIoTClient 75:86205ca63a59 151 result = NULL;
AzureIoTClient 75:86205ca63a59 152 }
AzureIoTClient 75:86205ca63a59 153 else if (mallocAndStrcpy_s(&result->device_id, device_id) != 0)
AzureIoTClient 75:86205ca63a59 154 {
AzureIoTClient 75:86205ca63a59 155 LogError("Failed allocating device_key");
AzureIoTClient 75:86205ca63a59 156 iothub_device_auth_destroy(result->device_auth_handle);
AzureIoTClient 75:86205ca63a59 157 free(result);
AzureIoTClient 75:86205ca63a59 158 result = NULL;
AzureIoTClient 75:86205ca63a59 159 }
AzureIoTClient 75:86205ca63a59 160 else
AzureIoTClient 75:86205ca63a59 161 {
AzureIoTClient 78:74a8d3068204 162 if (iothub_device_auth_get_type(result->device_auth_handle) == AUTH_TYPE_SAS)
AzureIoTClient 75:86205ca63a59 163 {
AzureIoTClient 75:86205ca63a59 164 result->cred_type = IOTHUB_CREDENTIAL_TYPE_DEVICE_AUTH;
AzureIoTClient 75:86205ca63a59 165 }
AzureIoTClient 75:86205ca63a59 166 else
AzureIoTClient 75:86205ca63a59 167 {
AzureIoTClient 75:86205ca63a59 168 result->cred_type = IOTHUB_CREDENTIAL_TYPE_X509_ECC;
AzureIoTClient 75:86205ca63a59 169 }
AzureIoTClient 75:86205ca63a59 170 }
AzureIoTClient 75:86205ca63a59 171 }
AzureIoTClient 75:86205ca63a59 172 #else
AzureIoTClient 78:74a8d3068204 173 LogError("Failed HSM module is not supported");
AzureIoTClient 75:86205ca63a59 174 result = NULL;
AzureIoTClient 75:86205ca63a59 175 #endif
AzureIoTClient 75:86205ca63a59 176 }
AzureIoTClient 75:86205ca63a59 177 return result;
AzureIoTClient 75:86205ca63a59 178 }
AzureIoTClient 62:5a4cdacf5090 179
AzureIoTClient 62:5a4cdacf5090 180 void IoTHubClient_Auth_Destroy(IOTHUB_AUTHORIZATION_HANDLE handle)
AzureIoTClient 62:5a4cdacf5090 181 {
AzureIoTClient 62:5a4cdacf5090 182 /* Codes_SRS_IoTHub_Authorization_07_005: [ if handle is NULL IoTHubClient_Auth_Destroy shall do nothing. ] */
AzureIoTClient 62:5a4cdacf5090 183 if (handle != NULL)
AzureIoTClient 62:5a4cdacf5090 184 {
AzureIoTClient 62:5a4cdacf5090 185 /* Codes_SRS_IoTHub_Authorization_07_006: [ IoTHubClient_Auth_Destroy shall free all resources associated with the IOTHUB_AUTHORIZATION_HANDLE handle. ] */
AzureIoTClient 78:74a8d3068204 186 #ifdef USE_PROV_MODULE
AzureIoTClient 75:86205ca63a59 187 iothub_device_auth_destroy(handle->device_auth_handle);
AzureIoTClient 75:86205ca63a59 188 #endif
AzureIoTClient 62:5a4cdacf5090 189 free(handle->device_key);
AzureIoTClient 62:5a4cdacf5090 190 free(handle->device_id);
AzureIoTClient 62:5a4cdacf5090 191 free(handle->device_sas_token);
AzureIoTClient 62:5a4cdacf5090 192 free(handle);
AzureIoTClient 62:5a4cdacf5090 193 }
AzureIoTClient 62:5a4cdacf5090 194 }
AzureIoTClient 62:5a4cdacf5090 195
AzureIoTClient 63:1bf1c2d60aab 196 IOTHUB_CREDENTIAL_TYPE IoTHubClient_Auth_Set_x509_Type(IOTHUB_AUTHORIZATION_HANDLE handle, bool enable_x509)
AzureIoTClient 63:1bf1c2d60aab 197 {
AzureIoTClient 63:1bf1c2d60aab 198 IOTHUB_CREDENTIAL_TYPE result;
AzureIoTClient 63:1bf1c2d60aab 199 if (handle != NULL)
AzureIoTClient 63:1bf1c2d60aab 200 {
AzureIoTClient 63:1bf1c2d60aab 201 if (enable_x509)
AzureIoTClient 63:1bf1c2d60aab 202 {
AzureIoTClient 63:1bf1c2d60aab 203 result = handle->cred_type = IOTHUB_CREDENTIAL_TYPE_X509;
AzureIoTClient 63:1bf1c2d60aab 204 }
AzureIoTClient 63:1bf1c2d60aab 205 else
AzureIoTClient 63:1bf1c2d60aab 206 {
AzureIoTClient 63:1bf1c2d60aab 207 if (handle->device_sas_token == NULL)
AzureIoTClient 63:1bf1c2d60aab 208 {
AzureIoTClient 63:1bf1c2d60aab 209 result = handle->cred_type = IOTHUB_CREDENTIAL_TYPE_DEVICE_KEY;
AzureIoTClient 63:1bf1c2d60aab 210 }
AzureIoTClient 63:1bf1c2d60aab 211 else if (handle->device_key == NULL)
AzureIoTClient 63:1bf1c2d60aab 212 {
AzureIoTClient 63:1bf1c2d60aab 213 result = handle->cred_type = IOTHUB_CREDENTIAL_TYPE_SAS_TOKEN;
AzureIoTClient 63:1bf1c2d60aab 214 }
AzureIoTClient 63:1bf1c2d60aab 215 else
AzureIoTClient 63:1bf1c2d60aab 216 {
AzureIoTClient 63:1bf1c2d60aab 217 result = handle->cred_type = IOTHUB_CREDENTIAL_TYPE_UNKNOWN;
AzureIoTClient 63:1bf1c2d60aab 218 }
AzureIoTClient 63:1bf1c2d60aab 219 }
AzureIoTClient 63:1bf1c2d60aab 220 }
AzureIoTClient 63:1bf1c2d60aab 221 else
AzureIoTClient 63:1bf1c2d60aab 222 {
AzureIoTClient 63:1bf1c2d60aab 223 result = IOTHUB_CREDENTIAL_TYPE_UNKNOWN;
AzureIoTClient 63:1bf1c2d60aab 224 }
AzureIoTClient 63:1bf1c2d60aab 225 return result;
AzureIoTClient 63:1bf1c2d60aab 226 }
AzureIoTClient 63:1bf1c2d60aab 227
AzureIoTClient 75:86205ca63a59 228 int IoTHubClient_Auth_Set_xio_Certificate(IOTHUB_AUTHORIZATION_HANDLE handle, XIO_HANDLE xio)
AzureIoTClient 75:86205ca63a59 229 {
AzureIoTClient 75:86205ca63a59 230 int result;
AzureIoTClient 75:86205ca63a59 231 if (handle == NULL || xio == NULL)
AzureIoTClient 75:86205ca63a59 232 {
AzureIoTClient 75:86205ca63a59 233 LogError("Invalid Parameter handle: %p xio: %p", handle, xio);
AzureIoTClient 75:86205ca63a59 234 result = __FAILURE__;
AzureIoTClient 75:86205ca63a59 235 }
AzureIoTClient 75:86205ca63a59 236 else if (handle->cred_type != IOTHUB_CREDENTIAL_TYPE_X509_ECC)
AzureIoTClient 75:86205ca63a59 237 {
AzureIoTClient 75:86205ca63a59 238 LogError("Invalid credential types for this operation");
AzureIoTClient 75:86205ca63a59 239 result = __FAILURE__;
AzureIoTClient 75:86205ca63a59 240 }
AzureIoTClient 75:86205ca63a59 241 else
AzureIoTClient 75:86205ca63a59 242 {
AzureIoTClient 78:74a8d3068204 243 #ifdef USE_PROV_MODULE
AzureIoTClient 75:86205ca63a59 244 CREDENTIAL_RESULT* cred_result = iothub_device_auth_generate_credentials(handle->device_auth_handle, NULL);
AzureIoTClient 75:86205ca63a59 245 if (cred_result == NULL)
AzureIoTClient 75:86205ca63a59 246 {
AzureIoTClient 75:86205ca63a59 247 LogError("Failure generating credentials");
AzureIoTClient 75:86205ca63a59 248 result = __FAILURE__;
AzureIoTClient 75:86205ca63a59 249 }
AzureIoTClient 75:86205ca63a59 250 else
AzureIoTClient 75:86205ca63a59 251 {
AzureIoTClient 75:86205ca63a59 252 if (xio_setoption(xio, OPTION_X509_ECC_CERT, cred_result->auth_cred_result.x509_result.x509_cert) != 0)
AzureIoTClient 75:86205ca63a59 253 {
AzureIoTClient 75:86205ca63a59 254 LogError("Failure setting x509 cert on xio");
AzureIoTClient 75:86205ca63a59 255 result = __FAILURE__;
AzureIoTClient 75:86205ca63a59 256 }
AzureIoTClient 75:86205ca63a59 257 else if (xio_setoption(xio, OPTION_X509_ECC_KEY, cred_result->auth_cred_result.x509_result.x509_alias_key) != 0)
AzureIoTClient 75:86205ca63a59 258 {
AzureIoTClient 75:86205ca63a59 259 LogError("Failure setting x509 key on xio");
AzureIoTClient 75:86205ca63a59 260 result = __FAILURE__;
AzureIoTClient 75:86205ca63a59 261 }
AzureIoTClient 75:86205ca63a59 262 else
AzureIoTClient 75:86205ca63a59 263 {
AzureIoTClient 75:86205ca63a59 264 result = 0;
AzureIoTClient 75:86205ca63a59 265 }
AzureIoTClient 75:86205ca63a59 266 free(cred_result);
AzureIoTClient 75:86205ca63a59 267 }
AzureIoTClient 75:86205ca63a59 268 #else
AzureIoTClient 78:74a8d3068204 269 LogError("Failed HSM module is not supported");
AzureIoTClient 75:86205ca63a59 270 result = __FAILURE__;
AzureIoTClient 75:86205ca63a59 271 #endif
AzureIoTClient 75:86205ca63a59 272 }
AzureIoTClient 75:86205ca63a59 273 return result;
AzureIoTClient 75:86205ca63a59 274 }
AzureIoTClient 75:86205ca63a59 275
AzureIoTClient 62:5a4cdacf5090 276 IOTHUB_CREDENTIAL_TYPE IoTHubClient_Auth_Get_Credential_Type(IOTHUB_AUTHORIZATION_HANDLE handle)
AzureIoTClient 62:5a4cdacf5090 277 {
AzureIoTClient 62:5a4cdacf5090 278 IOTHUB_CREDENTIAL_TYPE result;
AzureIoTClient 62:5a4cdacf5090 279 /* Codes_SRS_IoTHub_Authorization_07_007: [ if handle is NULL IoTHub_Auth_Get_Credential_Type shall return IOTHUB_CREDENTIAL_TYPE_UNKNOWN. ] */
AzureIoTClient 62:5a4cdacf5090 280 if (handle == NULL)
AzureIoTClient 62:5a4cdacf5090 281 {
AzureIoTClient 62:5a4cdacf5090 282 LogError("Invalid Parameter handle: %p", handle);
AzureIoTClient 62:5a4cdacf5090 283 result = IOTHUB_CREDENTIAL_TYPE_UNKNOWN;
AzureIoTClient 62:5a4cdacf5090 284 }
AzureIoTClient 62:5a4cdacf5090 285 else
AzureIoTClient 62:5a4cdacf5090 286 {
AzureIoTClient 62:5a4cdacf5090 287 /* Codes_SRS_IoTHub_Authorization_07_008: [ IoTHub_Auth_Get_Credential_Type shall return the credential type that is set upon creation. ] */
AzureIoTClient 62:5a4cdacf5090 288 result = handle->cred_type;
AzureIoTClient 62:5a4cdacf5090 289 }
AzureIoTClient 62:5a4cdacf5090 290 return result;
AzureIoTClient 62:5a4cdacf5090 291 }
AzureIoTClient 62:5a4cdacf5090 292
AzureIoTClient 76:943524fee0b7 293 char* IoTHubClient_Auth_Get_SasToken(IOTHUB_AUTHORIZATION_HANDLE handle, const char* scope, size_t expiry_time_relative_seconds)
AzureIoTClient 62:5a4cdacf5090 294 {
AzureIoTClient 62:5a4cdacf5090 295 char* result;
AzureIoTClient 63:1bf1c2d60aab 296 /* Codes_SRS_IoTHub_Authorization_07_009: [ if handle or scope are NULL, IoTHubClient_Auth_Get_SasToken shall return NULL. ] */
AzureIoTClient 63:1bf1c2d60aab 297 if (handle == NULL)
AzureIoTClient 62:5a4cdacf5090 298 {
AzureIoTClient 63:1bf1c2d60aab 299 LogError("Invalid Parameter handle: %p", handle);
AzureIoTClient 62:5a4cdacf5090 300 result = NULL;
AzureIoTClient 62:5a4cdacf5090 301 }
AzureIoTClient 62:5a4cdacf5090 302 else
AzureIoTClient 62:5a4cdacf5090 303 {
AzureIoTClient 75:86205ca63a59 304 if (handle->cred_type == IOTHUB_CREDENTIAL_TYPE_DEVICE_AUTH)
AzureIoTClient 62:5a4cdacf5090 305 {
AzureIoTClient 78:74a8d3068204 306 #ifdef USE_PROV_MODULE
AzureIoTClient 75:86205ca63a59 307 DEVICE_AUTH_CREDENTIAL_INFO dev_auth_cred;
AzureIoTClient 63:1bf1c2d60aab 308 size_t sec_since_epoch;
AzureIoTClient 63:1bf1c2d60aab 309
AzureIoTClient 63:1bf1c2d60aab 310 if (get_seconds_since_epoch(&sec_since_epoch) != 0)
AzureIoTClient 62:5a4cdacf5090 311 {
AzureIoTClient 63:1bf1c2d60aab 312 LogError("failure getting seconds from epoch");
AzureIoTClient 62:5a4cdacf5090 313 result = NULL;
AzureIoTClient 62:5a4cdacf5090 314 }
AzureIoTClient 63:1bf1c2d60aab 315 else
AzureIoTClient 62:5a4cdacf5090 316 {
AzureIoTClient 76:943524fee0b7 317 size_t expiry_time = sec_since_epoch+expiry_time_relative_seconds;
AzureIoTClient 75:86205ca63a59 318 dev_auth_cred.sas_info.expiry_seconds = expiry_time;
AzureIoTClient 75:86205ca63a59 319 dev_auth_cred.sas_info.token_scope = scope;
AzureIoTClient 75:86205ca63a59 320 dev_auth_cred.dev_auth_type = AUTH_TYPE_SAS;
AzureIoTClient 75:86205ca63a59 321
AzureIoTClient 75:86205ca63a59 322 CREDENTIAL_RESULT* cred_result = iothub_device_auth_generate_credentials(handle->device_auth_handle, &dev_auth_cred);
AzureIoTClient 75:86205ca63a59 323 if (cred_result == NULL)
AzureIoTClient 62:5a4cdacf5090 324 {
AzureIoTClient 75:86205ca63a59 325 LogError("failure getting credentials from device auth module");
AzureIoTClient 62:5a4cdacf5090 326 result = NULL;
AzureIoTClient 62:5a4cdacf5090 327 }
AzureIoTClient 63:1bf1c2d60aab 328 else
AzureIoTClient 63:1bf1c2d60aab 329 {
AzureIoTClient 75:86205ca63a59 330 if (mallocAndStrcpy_s(&result, cred_result->auth_cred_result.sas_result.sas_token) != 0)
AzureIoTClient 75:86205ca63a59 331 {
AzureIoTClient 75:86205ca63a59 332 LogError("failure allocating Sas Token");
AzureIoTClient 75:86205ca63a59 333 result = NULL;
AzureIoTClient 75:86205ca63a59 334 }
AzureIoTClient 75:86205ca63a59 335 free(cred_result);
AzureIoTClient 75:86205ca63a59 336 }
AzureIoTClient 75:86205ca63a59 337 }
AzureIoTClient 75:86205ca63a59 338 #else
AzureIoTClient 78:74a8d3068204 339 LogError("Failed HSM module is not supported");
AzureIoTClient 75:86205ca63a59 340 result = NULL;
AzureIoTClient 75:86205ca63a59 341 #endif
AzureIoTClient 75:86205ca63a59 342 }
AzureIoTClient 75:86205ca63a59 343 else if (handle->cred_type == IOTHUB_CREDENTIAL_TYPE_SAS_TOKEN)
AzureIoTClient 75:86205ca63a59 344 {
AzureIoTClient 75:86205ca63a59 345 /* 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 346 if (handle->device_sas_token != NULL)
AzureIoTClient 75:86205ca63a59 347 {
AzureIoTClient 75:86205ca63a59 348 if (mallocAndStrcpy_s(&result, handle->device_sas_token) != 0)
AzureIoTClient 75:86205ca63a59 349 {
AzureIoTClient 75:86205ca63a59 350 LogError("failure allocating sas token");
AzureIoTClient 75:86205ca63a59 351 result = NULL;
AzureIoTClient 75:86205ca63a59 352 }
AzureIoTClient 75:86205ca63a59 353 }
AzureIoTClient 75:86205ca63a59 354 else
AzureIoTClient 75:86205ca63a59 355 {
AzureIoTClient 75:86205ca63a59 356 LogError("failure device sas token is NULL");
AzureIoTClient 75:86205ca63a59 357 result = NULL;
AzureIoTClient 75:86205ca63a59 358 }
AzureIoTClient 75:86205ca63a59 359 }
AzureIoTClient 75:86205ca63a59 360 else if (handle->cred_type == IOTHUB_CREDENTIAL_TYPE_DEVICE_KEY)
AzureIoTClient 75:86205ca63a59 361 {
AzureIoTClient 75:86205ca63a59 362 /* Codes_SRS_IoTHub_Authorization_07_009: [ if handle or scope are NULL, IoTHubClient_Auth_Get_SasToken shall return NULL. ] */
AzureIoTClient 75:86205ca63a59 363 if (scope == NULL)
AzureIoTClient 75:86205ca63a59 364 {
AzureIoTClient 75:86205ca63a59 365 LogError("Invalid Parameter scope: %p", scope);
AzureIoTClient 75:86205ca63a59 366 result = NULL;
AzureIoTClient 75:86205ca63a59 367 }
AzureIoTClient 75:86205ca63a59 368 else
AzureIoTClient 75:86205ca63a59 369 {
AzureIoTClient 75:86205ca63a59 370 const char* key_name = "";
AzureIoTClient 75:86205ca63a59 371 STRING_HANDLE sas_token;
AzureIoTClient 75:86205ca63a59 372 size_t sec_since_epoch;
AzureIoTClient 75:86205ca63a59 373
AzureIoTClient 76:943524fee0b7 374 /* 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 375 if (get_seconds_since_epoch(&sec_since_epoch) != 0)
AzureIoTClient 75:86205ca63a59 376 {
AzureIoTClient 75:86205ca63a59 377 /* Codes_SRS_IoTHub_Authorization_07_020: [ If any error is encountered IoTHubClient_Auth_Get_ConnString shall return NULL. ] */
AzureIoTClient 75:86205ca63a59 378 LogError("failure getting seconds from epoch");
AzureIoTClient 75:86205ca63a59 379 result = NULL;
AzureIoTClient 75:86205ca63a59 380 }
AzureIoTClient 75:86205ca63a59 381 else
AzureIoTClient 75:86205ca63a59 382 {
AzureIoTClient 75:86205ca63a59 383 /* Codes_SRS_IoTHub_Authorization_07_011: [ IoTHubClient_Auth_Get_ConnString shall call SASToken_CreateString to construct the sas token. ] */
AzureIoTClient 76:943524fee0b7 384 size_t expiry_time = sec_since_epoch+expiry_time_relative_seconds;
AzureIoTClient 75:86205ca63a59 385 if ( (sas_token = SASToken_CreateString(handle->device_key, scope, key_name, expiry_time)) == NULL)
AzureIoTClient 63:1bf1c2d60aab 386 {
AzureIoTClient 63:1bf1c2d60aab 387 /* Codes_SRS_IoTHub_Authorization_07_020: [ If any error is encountered IoTHubClient_Auth_Get_ConnString shall return NULL. ] */
AzureIoTClient 75:86205ca63a59 388 LogError("Failed creating sas_token");
AzureIoTClient 63:1bf1c2d60aab 389 result = NULL;
AzureIoTClient 63:1bf1c2d60aab 390 }
AzureIoTClient 75:86205ca63a59 391 else
AzureIoTClient 75:86205ca63a59 392 {
AzureIoTClient 75:86205ca63a59 393 /* 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 394 if (mallocAndStrcpy_s(&result, STRING_c_str(sas_token) ) != 0)
AzureIoTClient 75:86205ca63a59 395 {
AzureIoTClient 75:86205ca63a59 396 /* Codes_SRS_IoTHub_Authorization_07_020: [ If any error is encountered IoTHubClient_Auth_Get_ConnString shall return NULL. ] */
AzureIoTClient 75:86205ca63a59 397 LogError("Failed copying result");
AzureIoTClient 75:86205ca63a59 398 result = NULL;
AzureIoTClient 75:86205ca63a59 399 }
AzureIoTClient 75:86205ca63a59 400 STRING_delete(sas_token);
AzureIoTClient 75:86205ca63a59 401 }
AzureIoTClient 63:1bf1c2d60aab 402 }
AzureIoTClient 62:5a4cdacf5090 403 }
AzureIoTClient 62:5a4cdacf5090 404 }
AzureIoTClient 75:86205ca63a59 405 else
AzureIoTClient 75:86205ca63a59 406 {
AzureIoTClient 75:86205ca63a59 407 LogError("Failed getting sas token invalid credential type");
AzureIoTClient 75:86205ca63a59 408 result = NULL;
AzureIoTClient 75:86205ca63a59 409 }
AzureIoTClient 62:5a4cdacf5090 410 }
AzureIoTClient 62:5a4cdacf5090 411 return result;
AzureIoTClient 62:5a4cdacf5090 412 }
AzureIoTClient 62:5a4cdacf5090 413
AzureIoTClient 62:5a4cdacf5090 414 const char* IoTHubClient_Auth_Get_DeviceId(IOTHUB_AUTHORIZATION_HANDLE handle)
AzureIoTClient 62:5a4cdacf5090 415 {
AzureIoTClient 62:5a4cdacf5090 416 const char* result;
AzureIoTClient 62:5a4cdacf5090 417 if (handle == NULL)
AzureIoTClient 62:5a4cdacf5090 418 {
AzureIoTClient 62:5a4cdacf5090 419 /* Codes_SRS_IoTHub_Authorization_07_013: [ if handle is NULL, IoTHubClient_Auth_Get_DeviceId shall return NULL. ] */
AzureIoTClient 62:5a4cdacf5090 420 LogError("Invalid Parameter handle: %p", handle);
AzureIoTClient 62:5a4cdacf5090 421 result = NULL;
AzureIoTClient 62:5a4cdacf5090 422 }
AzureIoTClient 62:5a4cdacf5090 423 else
AzureIoTClient 62:5a4cdacf5090 424 {
AzureIoTClient 62:5a4cdacf5090 425 /* Codes_SRS_IoTHub_Authorization_07_014: [ IoTHubClient_Auth_Get_DeviceId shall return the device_id specified upon creation. ] */
AzureIoTClient 62:5a4cdacf5090 426 result = handle->device_id;
AzureIoTClient 62:5a4cdacf5090 427 }
AzureIoTClient 62:5a4cdacf5090 428 return result;
AzureIoTClient 62:5a4cdacf5090 429 }
AzureIoTClient 62:5a4cdacf5090 430
AzureIoTClient 63:1bf1c2d60aab 431 const char* IoTHubClient_Auth_Get_DeviceKey(IOTHUB_AUTHORIZATION_HANDLE handle)
AzureIoTClient 62:5a4cdacf5090 432 {
AzureIoTClient 63:1bf1c2d60aab 433 const char* result;
AzureIoTClient 63:1bf1c2d60aab 434 if (handle == NULL)
AzureIoTClient 63:1bf1c2d60aab 435 {
AzureIoTClient 63:1bf1c2d60aab 436 /* Codes_SRS_IoTHub_Authorization_07_022: [ if handle is NULL, IoTHubClient_Auth_Get_DeviceKey shall return NULL. ] */
AzureIoTClient 63:1bf1c2d60aab 437 LogError("Invalid Parameter handle: %p", handle);
AzureIoTClient 63:1bf1c2d60aab 438 result = NULL;
AzureIoTClient 63:1bf1c2d60aab 439 }
AzureIoTClient 63:1bf1c2d60aab 440 else
AzureIoTClient 63:1bf1c2d60aab 441 {
AzureIoTClient 63:1bf1c2d60aab 442 /* Codes_SRS_IoTHub_Authorization_07_023: [ IoTHubClient_Auth_Get_DeviceKey shall return the device_key specified upon creation. ] */
AzureIoTClient 63:1bf1c2d60aab 443 result = handle->device_key;
AzureIoTClient 63:1bf1c2d60aab 444 }
AzureIoTClient 63:1bf1c2d60aab 445 return result;
AzureIoTClient 63:1bf1c2d60aab 446 }
AzureIoTClient 63:1bf1c2d60aab 447
AzureIoTClient 63:1bf1c2d60aab 448 SAS_TOKEN_STATUS IoTHubClient_Auth_Is_SasToken_Valid(IOTHUB_AUTHORIZATION_HANDLE handle)
AzureIoTClient 63:1bf1c2d60aab 449 {
AzureIoTClient 63:1bf1c2d60aab 450 SAS_TOKEN_STATUS result;
AzureIoTClient 62:5a4cdacf5090 451 if (handle == NULL)
AzureIoTClient 62:5a4cdacf5090 452 {
AzureIoTClient 62:5a4cdacf5090 453 /* Codes_SRS_IoTHub_Authorization_07_015: [ if handle is NULL, IoTHubClient_Auth_Is_SasToken_Valid shall return false. ] */
AzureIoTClient 62:5a4cdacf5090 454 LogError("Invalid Parameter handle: %p", handle);
AzureIoTClient 63:1bf1c2d60aab 455 result = SAS_TOKEN_STATUS_FAILED;
AzureIoTClient 62:5a4cdacf5090 456 }
AzureIoTClient 62:5a4cdacf5090 457 else
AzureIoTClient 62:5a4cdacf5090 458 {
AzureIoTClient 62:5a4cdacf5090 459 if (handle->cred_type == IOTHUB_CREDENTIAL_TYPE_SAS_TOKEN)
AzureIoTClient 62:5a4cdacf5090 460 {
AzureIoTClient 62:5a4cdacf5090 461 if (handle->device_sas_token == NULL)
AzureIoTClient 62:5a4cdacf5090 462 {
AzureIoTClient 62:5a4cdacf5090 463 /* Codes_SRS_IoTHub_Authorization_07_017: [ If the sas_token is NULL IoTHubClient_Auth_Is_SasToken_Valid shall return false. ] */
AzureIoTClient 63:1bf1c2d60aab 464 LogError("Failure: device_sas_toke is NULL");
AzureIoTClient 63:1bf1c2d60aab 465 result = SAS_TOKEN_STATUS_FAILED;
AzureIoTClient 62:5a4cdacf5090 466 }
AzureIoTClient 62:5a4cdacf5090 467 else
AzureIoTClient 62:5a4cdacf5090 468 {
AzureIoTClient 62:5a4cdacf5090 469 /* Codes_SRS_IoTHub_Authorization_07_018: [ otherwise IoTHubClient_Auth_Is_SasToken_Valid shall return the value returned by SASToken_Validate. ] */
AzureIoTClient 62:5a4cdacf5090 470 STRING_HANDLE strSasToken = STRING_construct(handle->device_sas_token);
AzureIoTClient 63:1bf1c2d60aab 471 if (strSasToken != NULL)
AzureIoTClient 63:1bf1c2d60aab 472 {
AzureIoTClient 63:1bf1c2d60aab 473 if (!SASToken_Validate(strSasToken))
AzureIoTClient 63:1bf1c2d60aab 474 {
AzureIoTClient 63:1bf1c2d60aab 475 result = SAS_TOKEN_STATUS_INVALID;
AzureIoTClient 63:1bf1c2d60aab 476 }
AzureIoTClient 63:1bf1c2d60aab 477 else
AzureIoTClient 63:1bf1c2d60aab 478 {
AzureIoTClient 63:1bf1c2d60aab 479 result = SAS_TOKEN_STATUS_VALID;
AzureIoTClient 63:1bf1c2d60aab 480 }
AzureIoTClient 63:1bf1c2d60aab 481 STRING_delete(strSasToken);
AzureIoTClient 63:1bf1c2d60aab 482 }
AzureIoTClient 63:1bf1c2d60aab 483 else
AzureIoTClient 63:1bf1c2d60aab 484 {
AzureIoTClient 63:1bf1c2d60aab 485 LogError("Failure constructing SAS Token");
AzureIoTClient 63:1bf1c2d60aab 486 result = SAS_TOKEN_STATUS_FAILED;
AzureIoTClient 63:1bf1c2d60aab 487 }
AzureIoTClient 62:5a4cdacf5090 488 }
AzureIoTClient 62:5a4cdacf5090 489 }
AzureIoTClient 62:5a4cdacf5090 490 else
AzureIoTClient 62:5a4cdacf5090 491 {
AzureIoTClient 62:5a4cdacf5090 492 /* Codes_SRS_IoTHub_Authorization_07_016: [ if credential type is not IOTHUB_CREDENTIAL_TYPE_SAS_TOKEN IoTHubClient_Auth_Is_SasToken_Valid shall return true. ] */
AzureIoTClient 62:5a4cdacf5090 493 result = true;
AzureIoTClient 62:5a4cdacf5090 494 }
AzureIoTClient 62:5a4cdacf5090 495 }
AzureIoTClient 62:5a4cdacf5090 496 return result;
AzureIoTClient 62:5a4cdacf5090 497 }