[453] | 1 | // Copyright (c) Microsoft. All rights reserved.
|
---|
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
---|
| 3 |
|
---|
| 4 | #include <stdlib.h>
|
---|
| 5 | #include "azure_c_shared_utility/gballoc.h"
|
---|
| 6 | #include "azure_macro_utils/macro_utils.h"
|
---|
| 7 | #include "umock_c/umock_c_prod.h"
|
---|
| 8 | #include "azure_c_shared_utility/crt_abstractions.h"
|
---|
| 9 | #include "azure_c_shared_utility/agenttime.h"
|
---|
| 10 | #include "azure_c_shared_utility/xlogging.h"
|
---|
| 11 | #include "azure_c_shared_utility/strings.h"
|
---|
| 12 | #include "azure_c_shared_utility/sastoken.h"
|
---|
| 13 | #include "azure_c_shared_utility/shared_util_options.h"
|
---|
| 14 | #include "azure_c_shared_utility/azure_base64.h"
|
---|
| 15 | #include "azure_c_shared_utility/buffer_.h"
|
---|
| 16 |
|
---|
| 17 | #ifdef USE_PROV_MODULE
|
---|
| 18 | #include "azure_prov_client/internal/iothub_auth_client.h"
|
---|
| 19 | #endif
|
---|
| 20 |
|
---|
| 21 | #include "internal/iothub_client_authorization.h"
|
---|
| 22 |
|
---|
| 23 | #define DEFAULT_SAS_TOKEN_EXPIRY_TIME_SECS 3600
|
---|
| 24 | #define INDEFINITE_TIME ((time_t)(-1))
|
---|
| 25 | #define MIN_SAS_EXPIRY_TIME 5 // 5 seconds
|
---|
| 26 |
|
---|
| 27 | typedef struct IOTHUB_AUTHORIZATION_DATA_TAG
|
---|
| 28 | {
|
---|
| 29 | char* device_sas_token;
|
---|
| 30 | char* device_key;
|
---|
| 31 | char* device_id;
|
---|
| 32 | char* module_id;
|
---|
| 33 | size_t token_expiry_time_sec;
|
---|
| 34 | IOTHUB_CREDENTIAL_TYPE cred_type;
|
---|
| 35 | #ifdef USE_PROV_MODULE
|
---|
| 36 | IOTHUB_SECURITY_HANDLE device_auth_handle;
|
---|
| 37 | #endif
|
---|
| 38 | } IOTHUB_AUTHORIZATION_DATA;
|
---|
| 39 |
|
---|
| 40 | static int get_seconds_since_epoch(size_t* seconds)
|
---|
| 41 | {
|
---|
| 42 | int result;
|
---|
| 43 | time_t current_time;
|
---|
| 44 | if ((current_time = get_time(NULL)) == INDEFINITE_TIME)
|
---|
| 45 | {
|
---|
| 46 | LogError("Failed getting the current local time (get_time() failed)");
|
---|
| 47 | result = MU_FAILURE;
|
---|
| 48 | }
|
---|
| 49 | else
|
---|
| 50 | {
|
---|
| 51 | *seconds = (size_t)get_difftime(current_time, (time_t)0);
|
---|
| 52 | result = 0;
|
---|
| 53 | }
|
---|
| 54 | return result;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | static IOTHUB_AUTHORIZATION_DATA* initialize_auth_client(const char* device_id, const char* module_id)
|
---|
| 58 | {
|
---|
| 59 | IOTHUB_AUTHORIZATION_DATA* result;
|
---|
| 60 |
|
---|
| 61 | /* Codes_SRS_IoTHub_Authorization_07_002: [IoTHubClient_Auth_Create shall allocate a IOTHUB_AUTHORIZATION_HANDLE that is needed for subsequent calls. ] */
|
---|
| 62 | result = (IOTHUB_AUTHORIZATION_DATA*)malloc(sizeof(IOTHUB_AUTHORIZATION_DATA) );
|
---|
| 63 | if (result == NULL)
|
---|
| 64 | {
|
---|
| 65 | /* Codes_SRS_IoTHub_Authorization_07_019: [ On error IoTHubClient_Auth_Create shall return NULL. ] */
|
---|
| 66 | LogError("Failed allocating IOTHUB_AUTHORIZATION_DATA");
|
---|
| 67 | result = NULL;
|
---|
| 68 | }
|
---|
| 69 | else
|
---|
| 70 | {
|
---|
| 71 | memset(result, 0, sizeof(IOTHUB_AUTHORIZATION_DATA) );
|
---|
| 72 | if (mallocAndStrcpy_s(&result->device_id, device_id) != 0)
|
---|
| 73 | {
|
---|
| 74 | /* Codes_SRS_IoTHub_Authorization_07_019: [ On error IoTHubClient_Auth_Create shall return NULL. ] */
|
---|
| 75 | LogError("Failed allocating device_key");
|
---|
| 76 | free(result);
|
---|
| 77 | result = NULL;
|
---|
| 78 | }
|
---|
| 79 | else if (module_id != NULL && mallocAndStrcpy_s(&result->module_id, module_id) != 0)
|
---|
| 80 | {
|
---|
| 81 | /* Codes_SRS_IoTHub_Authorization_07_019: [ On error IoTHubClient_Auth_Create shall return NULL. ] */
|
---|
| 82 | LogError("Failed allocating module_id");
|
---|
| 83 | free(result->device_id);
|
---|
| 84 | free(result);
|
---|
| 85 | result = NULL;
|
---|
| 86 | }
|
---|
| 87 | else
|
---|
| 88 | {
|
---|
| 89 | result->token_expiry_time_sec = DEFAULT_SAS_TOKEN_EXPIRY_TIME_SECS;
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | return result;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | IOTHUB_AUTHORIZATION_HANDLE IoTHubClient_Auth_Create(const char* device_key, const char* device_id, const char* device_sas_token, const char *module_id)
|
---|
| 96 | {
|
---|
| 97 | IOTHUB_AUTHORIZATION_DATA* result;
|
---|
| 98 | bool is_key_valid;
|
---|
| 99 |
|
---|
| 100 | if (device_key == NULL)
|
---|
| 101 | {
|
---|
| 102 | is_key_valid = true;
|
---|
| 103 | }
|
---|
| 104 | else
|
---|
| 105 | {
|
---|
| 106 | /* Codes_SRS_IoTHub_Authorization_21_021: [ If the provided key is not base64 encoded, IoTHubClient_Auth_Create shall return NULL. ] */
|
---|
| 107 | BUFFER_HANDLE key = Azure_Base64_Decode(device_key);
|
---|
| 108 | if (key != NULL)
|
---|
| 109 | {
|
---|
| 110 | is_key_valid = true;
|
---|
| 111 | }
|
---|
| 112 | else
|
---|
| 113 | {
|
---|
| 114 | is_key_valid = false;
|
---|
| 115 | }
|
---|
| 116 | BUFFER_delete(key);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | /* Codes_SRS_IoTHub_Authorization_07_001: [if device_id is NULL IoTHubClient_Auth_Create, shall return NULL. ] */
|
---|
| 120 | if ((device_id == NULL) || (!is_key_valid))
|
---|
| 121 | {
|
---|
| 122 | LogError("Invalid Parameter %s", ((device_id == NULL) ? "device_id: NULL" : "key"));
|
---|
| 123 | result = NULL;
|
---|
| 124 | }
|
---|
| 125 | else
|
---|
| 126 | {
|
---|
| 127 | result = initialize_auth_client(device_id, module_id);
|
---|
| 128 | if (result == NULL)
|
---|
| 129 | {
|
---|
| 130 | LogError("Failure initializing auth client");
|
---|
| 131 | }
|
---|
| 132 | else if (device_key != NULL && mallocAndStrcpy_s(&result->device_key, device_key) != 0)
|
---|
| 133 | {
|
---|
| 134 | /* Codes_SRS_IoTHub_Authorization_07_019: [ On error IoTHubClient_Auth_Create shall return NULL. ] */
|
---|
| 135 | LogError("Failed allocating device_key");
|
---|
| 136 | free(result->device_id);
|
---|
| 137 | free(result->module_id);
|
---|
| 138 | free(result);
|
---|
| 139 | result = NULL;
|
---|
| 140 | }
|
---|
| 141 | else
|
---|
| 142 | {
|
---|
| 143 | if (device_key != NULL)
|
---|
| 144 | {
|
---|
| 145 | /* 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. ]*/
|
---|
| 146 | result->cred_type = IOTHUB_CREDENTIAL_TYPE_DEVICE_KEY;
|
---|
| 147 | }
|
---|
| 148 | else if (device_sas_token != NULL)
|
---|
| 149 | {
|
---|
| 150 | /* Codes_SRS_IoTHub_Authorization_07_020: [ else IoTHubClient_Auth_Create shall set the credential type to IOTHUB_CREDENTIAL_TYPE_SAS_TOKEN. ] */
|
---|
| 151 | result->cred_type = IOTHUB_CREDENTIAL_TYPE_SAS_TOKEN;
|
---|
| 152 | if (mallocAndStrcpy_s(&result->device_sas_token, device_sas_token) != 0)
|
---|
| 153 | {
|
---|
| 154 | /* Codes_SRS_IoTHub_Authorization_07_019: [ On error IoTHubClient_Auth_Create shall return NULL. ] */
|
---|
| 155 | LogError("Failed allocating device_key");
|
---|
| 156 | free(result->device_key);
|
---|
| 157 | free(result->device_id);
|
---|
| 158 | free(result->module_id);
|
---|
| 159 | free(result);
|
---|
| 160 | result = NULL;
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 | else
|
---|
| 164 | {
|
---|
| 165 | /* 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. ] */
|
---|
| 166 | result->cred_type = IOTHUB_CREDENTIAL_TYPE_UNKNOWN;
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 | /* Codes_SRS_IoTHub_Authorization_07_004: [ If successful IoTHubClient_Auth_Create shall return a IOTHUB_AUTHORIZATION_HANDLE value. ] */
|
---|
| 171 | return result;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | IOTHUB_AUTHORIZATION_HANDLE IoTHubClient_Auth_CreateFromDeviceAuth(const char* device_id, const char* module_id)
|
---|
| 175 | {
|
---|
| 176 | IOTHUB_AUTHORIZATION_DATA* result;
|
---|
| 177 | if (device_id == NULL)
|
---|
| 178 | {
|
---|
| 179 | LogError("Invalid Parameter device_id: %p", device_id);
|
---|
| 180 | result = NULL;
|
---|
| 181 | }
|
---|
| 182 | else
|
---|
| 183 | {
|
---|
| 184 | #ifdef USE_PROV_MODULE
|
---|
| 185 | result = initialize_auth_client(device_id, module_id);
|
---|
| 186 | if (result == NULL)
|
---|
| 187 | {
|
---|
| 188 | LogError("Failure initializing auth client");
|
---|
| 189 | }
|
---|
| 190 | else
|
---|
| 191 | {
|
---|
| 192 | result->device_auth_handle = iothub_device_auth_create();
|
---|
| 193 | if (result->device_auth_handle == NULL)
|
---|
| 194 | {
|
---|
| 195 | LogError("Failed allocating IOTHUB_AUTHORIZATION_DATA");
|
---|
| 196 | free(result->device_id);
|
---|
| 197 | free(result->module_id);
|
---|
| 198 | free(result);
|
---|
| 199 | result = NULL;
|
---|
| 200 | }
|
---|
| 201 | else
|
---|
| 202 | {
|
---|
| 203 | DEVICE_AUTH_TYPE auth_type = iothub_device_auth_get_type(result->device_auth_handle);
|
---|
| 204 | if (auth_type == AUTH_TYPE_SAS || auth_type == AUTH_TYPE_SYMM_KEY)
|
---|
| 205 | {
|
---|
| 206 | result->cred_type = IOTHUB_CREDENTIAL_TYPE_DEVICE_AUTH;
|
---|
| 207 | }
|
---|
| 208 | else
|
---|
| 209 | {
|
---|
| 210 | result->cred_type = IOTHUB_CREDENTIAL_TYPE_X509_ECC;
|
---|
| 211 | }
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
| 214 | #else
|
---|
| 215 | (void)module_id;
|
---|
| 216 | LogError("Failed HSM module is not supported");
|
---|
| 217 | result = NULL;
|
---|
| 218 | #endif
|
---|
| 219 | }
|
---|
| 220 | return result;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | void IoTHubClient_Auth_Destroy(IOTHUB_AUTHORIZATION_HANDLE handle)
|
---|
| 224 | {
|
---|
| 225 | /* Codes_SRS_IoTHub_Authorization_07_005: [ if handle is NULL IoTHubClient_Auth_Destroy shall do nothing. ] */
|
---|
| 226 | if (handle != NULL)
|
---|
| 227 | {
|
---|
| 228 | /* Codes_SRS_IoTHub_Authorization_07_006: [ IoTHubClient_Auth_Destroy shall free all resources associated with the IOTHUB_AUTHORIZATION_HANDLE handle. ] */
|
---|
| 229 | #ifdef USE_PROV_MODULE
|
---|
| 230 | iothub_device_auth_destroy(handle->device_auth_handle);
|
---|
| 231 | #endif
|
---|
| 232 | free(handle->device_key);
|
---|
| 233 | free(handle->device_id);
|
---|
| 234 | free(handle->module_id);
|
---|
| 235 | free(handle->device_sas_token);
|
---|
| 236 | free(handle);
|
---|
| 237 | }
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | IOTHUB_CREDENTIAL_TYPE IoTHubClient_Auth_Set_x509_Type(IOTHUB_AUTHORIZATION_HANDLE handle, bool enable_x509)
|
---|
| 241 | {
|
---|
| 242 | IOTHUB_CREDENTIAL_TYPE result;
|
---|
| 243 | if (handle != NULL)
|
---|
| 244 | {
|
---|
| 245 | if (enable_x509)
|
---|
| 246 | {
|
---|
| 247 | result = handle->cred_type = IOTHUB_CREDENTIAL_TYPE_X509;
|
---|
| 248 | }
|
---|
| 249 | else
|
---|
| 250 | {
|
---|
| 251 | if (handle->device_sas_token == NULL)
|
---|
| 252 | {
|
---|
| 253 | result = handle->cred_type = IOTHUB_CREDENTIAL_TYPE_DEVICE_KEY;
|
---|
| 254 | }
|
---|
| 255 | else if (handle->device_key == NULL)
|
---|
| 256 | {
|
---|
| 257 | result = handle->cred_type = IOTHUB_CREDENTIAL_TYPE_SAS_TOKEN;
|
---|
| 258 | }
|
---|
| 259 | else
|
---|
| 260 | {
|
---|
| 261 | result = handle->cred_type = IOTHUB_CREDENTIAL_TYPE_UNKNOWN;
|
---|
| 262 | }
|
---|
| 263 | }
|
---|
| 264 | }
|
---|
| 265 | else
|
---|
| 266 | {
|
---|
| 267 | result = IOTHUB_CREDENTIAL_TYPE_UNKNOWN;
|
---|
| 268 | }
|
---|
| 269 | return result;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | int IoTHubClient_Auth_Set_xio_Certificate(IOTHUB_AUTHORIZATION_HANDLE handle, XIO_HANDLE xio)
|
---|
| 273 | {
|
---|
| 274 | int result;
|
---|
| 275 | if (handle == NULL || xio == NULL)
|
---|
| 276 | {
|
---|
| 277 | LogError("Invalid Parameter handle: %p xio: %p", handle, xio);
|
---|
| 278 | result = MU_FAILURE;
|
---|
| 279 | }
|
---|
| 280 | else if (handle->cred_type != IOTHUB_CREDENTIAL_TYPE_X509_ECC)
|
---|
| 281 | {
|
---|
| 282 | LogError("Invalid credential types for this operation");
|
---|
| 283 | result = MU_FAILURE;
|
---|
| 284 | }
|
---|
| 285 | else
|
---|
| 286 | {
|
---|
| 287 | #ifdef USE_PROV_MODULE
|
---|
| 288 | CREDENTIAL_RESULT* cred_result = iothub_device_auth_generate_credentials(handle->device_auth_handle, NULL);
|
---|
| 289 | if (cred_result == NULL)
|
---|
| 290 | {
|
---|
| 291 | LogError("Failure generating credentials");
|
---|
| 292 | result = MU_FAILURE;
|
---|
| 293 | }
|
---|
| 294 | else
|
---|
| 295 | {
|
---|
| 296 | if (xio_setoption(xio, OPTION_X509_ECC_CERT, cred_result->auth_cred_result.x509_result.x509_cert) != 0)
|
---|
| 297 | {
|
---|
| 298 | LogError("Failure setting x509 cert on xio");
|
---|
| 299 | result = MU_FAILURE;
|
---|
| 300 | }
|
---|
| 301 | else if (xio_setoption(xio, OPTION_X509_ECC_KEY, cred_result->auth_cred_result.x509_result.x509_alias_key) != 0)
|
---|
| 302 | {
|
---|
| 303 | LogError("Failure setting x509 key on xio");
|
---|
| 304 | result = MU_FAILURE;
|
---|
| 305 | }
|
---|
| 306 | else
|
---|
| 307 | {
|
---|
| 308 | result = 0;
|
---|
| 309 | }
|
---|
| 310 | free(cred_result);
|
---|
| 311 | }
|
---|
| 312 | #else
|
---|
| 313 | LogError("Failed HSM module is not supported");
|
---|
| 314 | result = MU_FAILURE;
|
---|
| 315 | #endif
|
---|
| 316 | }
|
---|
| 317 | return result;
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 | int IoTHubClient_Auth_Get_x509_info(IOTHUB_AUTHORIZATION_HANDLE handle, char** x509_cert, char** x509_key)
|
---|
| 321 | {
|
---|
| 322 | int result;
|
---|
| 323 | if (handle == NULL || x509_cert == NULL || x509_key == NULL)
|
---|
| 324 | {
|
---|
| 325 | LogError("Invalid Parameter handle: %p, x509_cert: %p, x509_key: %p", handle, x509_cert, x509_key);
|
---|
| 326 | result = MU_FAILURE;
|
---|
| 327 | }
|
---|
| 328 | else if (handle->cred_type != IOTHUB_CREDENTIAL_TYPE_X509_ECC)
|
---|
| 329 | {
|
---|
| 330 | LogError("Invalid credential types for this operation");
|
---|
| 331 | result = MU_FAILURE;
|
---|
| 332 | }
|
---|
| 333 | else
|
---|
| 334 | {
|
---|
| 335 | #ifdef USE_PROV_MODULE
|
---|
| 336 | CREDENTIAL_RESULT* cred_result = iothub_device_auth_generate_credentials(handle->device_auth_handle, NULL);
|
---|
| 337 | if (cred_result == NULL)
|
---|
| 338 | {
|
---|
| 339 | LogError("Failure generating credentials");
|
---|
| 340 | result = MU_FAILURE;
|
---|
| 341 | }
|
---|
| 342 | else
|
---|
| 343 | {
|
---|
| 344 | if (mallocAndStrcpy_s(x509_cert, cred_result->auth_cred_result.x509_result.x509_cert) != 0)
|
---|
| 345 | {
|
---|
| 346 | LogError("Failure copying certificate");
|
---|
| 347 | result = MU_FAILURE;
|
---|
| 348 | }
|
---|
| 349 | else if (mallocAndStrcpy_s(x509_key, cred_result->auth_cred_result.x509_result.x509_alias_key) != 0)
|
---|
| 350 | {
|
---|
| 351 | LogError("Failure copying private key");
|
---|
| 352 | result = MU_FAILURE;
|
---|
| 353 | free(*x509_cert);
|
---|
| 354 | }
|
---|
| 355 | else
|
---|
| 356 | {
|
---|
| 357 | result = 0;
|
---|
| 358 | }
|
---|
| 359 | free(cred_result);
|
---|
| 360 | }
|
---|
| 361 | #else
|
---|
| 362 | LogError("Failed HSM module is not supported");
|
---|
| 363 | result = MU_FAILURE;
|
---|
| 364 | #endif
|
---|
| 365 | }
|
---|
| 366 | return result;
|
---|
| 367 | }
|
---|
| 368 |
|
---|
| 369 | IOTHUB_CREDENTIAL_TYPE IoTHubClient_Auth_Get_Credential_Type(IOTHUB_AUTHORIZATION_HANDLE handle)
|
---|
| 370 | {
|
---|
| 371 | IOTHUB_CREDENTIAL_TYPE result;
|
---|
| 372 | /* Codes_SRS_IoTHub_Authorization_07_007: [ if handle is NULL IoTHub_Auth_Get_Credential_Type shall return IOTHUB_CREDENTIAL_TYPE_UNKNOWN. ] */
|
---|
| 373 | if (handle == NULL)
|
---|
| 374 | {
|
---|
| 375 | LogError("Invalid Parameter handle: %p", handle);
|
---|
| 376 | result = IOTHUB_CREDENTIAL_TYPE_UNKNOWN;
|
---|
| 377 | }
|
---|
| 378 | else
|
---|
| 379 | {
|
---|
| 380 | /* Codes_SRS_IoTHub_Authorization_07_008: [ IoTHub_Auth_Get_Credential_Type shall return the credential type that is set upon creation. ] */
|
---|
| 381 | result = handle->cred_type;
|
---|
| 382 | }
|
---|
| 383 | return result;
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 | char* IoTHubClient_Auth_Get_SasToken(IOTHUB_AUTHORIZATION_HANDLE handle, const char* scope, size_t expiry_time_relative_seconds, const char* key_name)
|
---|
| 387 | {
|
---|
| 388 | char* result;
|
---|
| 389 | (void)expiry_time_relative_seconds;
|
---|
| 390 | /* Codes_SRS_IoTHub_Authorization_07_009: [ if handle or scope are NULL, IoTHubClient_Auth_Get_SasToken shall return NULL. ] */
|
---|
| 391 | if (handle == NULL)
|
---|
| 392 | {
|
---|
| 393 | LogError("Invalid Parameter handle: %p", handle);
|
---|
| 394 | result = NULL;
|
---|
| 395 | }
|
---|
| 396 | else
|
---|
| 397 | {
|
---|
| 398 | if (handle->cred_type == IOTHUB_CREDENTIAL_TYPE_DEVICE_AUTH)
|
---|
| 399 | {
|
---|
| 400 | #ifdef USE_PROV_MODULE
|
---|
| 401 | DEVICE_AUTH_CREDENTIAL_INFO dev_auth_cred;
|
---|
| 402 | size_t sec_since_epoch;
|
---|
| 403 |
|
---|
| 404 | if (get_seconds_since_epoch(&sec_since_epoch) != 0)
|
---|
| 405 | {
|
---|
| 406 | LogError("failure getting seconds from epoch");
|
---|
| 407 | result = NULL;
|
---|
| 408 | }
|
---|
| 409 | else
|
---|
| 410 | {
|
---|
| 411 | memset(&dev_auth_cred, 0, sizeof(DEVICE_AUTH_CREDENTIAL_INFO));
|
---|
| 412 | size_t expiry_time = sec_since_epoch + handle->token_expiry_time_sec;
|
---|
| 413 | dev_auth_cred.sas_info.expiry_seconds = expiry_time;
|
---|
| 414 | dev_auth_cred.sas_info.token_scope = scope;
|
---|
| 415 | dev_auth_cred.sas_info.key_name = key_name;
|
---|
| 416 | dev_auth_cred.dev_auth_type = AUTH_TYPE_SAS;
|
---|
| 417 |
|
---|
| 418 | CREDENTIAL_RESULT* cred_result = iothub_device_auth_generate_credentials(handle->device_auth_handle, &dev_auth_cred);
|
---|
| 419 | if (cred_result == NULL)
|
---|
| 420 | {
|
---|
| 421 | LogError("failure getting credentials from device auth module");
|
---|
| 422 | result = NULL;
|
---|
| 423 | }
|
---|
| 424 | else
|
---|
| 425 | {
|
---|
| 426 | if (mallocAndStrcpy_s(&result, cred_result->auth_cred_result.sas_result.sas_token) != 0)
|
---|
| 427 | {
|
---|
| 428 | LogError("failure allocating Sas Token");
|
---|
| 429 | result = NULL;
|
---|
| 430 | }
|
---|
| 431 | free(cred_result);
|
---|
| 432 | }
|
---|
| 433 | }
|
---|
| 434 | #else
|
---|
| 435 | LogError("Failed HSM module is not supported");
|
---|
| 436 | result = NULL;
|
---|
| 437 | #endif
|
---|
| 438 | }
|
---|
| 439 | else if (handle->cred_type == IOTHUB_CREDENTIAL_TYPE_SAS_TOKEN)
|
---|
| 440 | {
|
---|
| 441 | /* 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. ] */
|
---|
| 442 | if (handle->device_sas_token != NULL)
|
---|
| 443 | {
|
---|
| 444 | if (mallocAndStrcpy_s(&result, handle->device_sas_token) != 0)
|
---|
| 445 | {
|
---|
| 446 | LogError("failure allocating sas token");
|
---|
| 447 | result = NULL;
|
---|
| 448 | }
|
---|
| 449 | }
|
---|
| 450 | else
|
---|
| 451 | {
|
---|
| 452 | LogError("failure device sas token is NULL");
|
---|
| 453 | result = NULL;
|
---|
| 454 | }
|
---|
| 455 | }
|
---|
| 456 | else if (handle->cred_type == IOTHUB_CREDENTIAL_TYPE_DEVICE_KEY)
|
---|
| 457 | {
|
---|
| 458 | /* Codes_SRS_IoTHub_Authorization_07_009: [ if handle or scope are NULL, IoTHubClient_Auth_Get_SasToken shall return NULL. ] */
|
---|
| 459 | if (scope == NULL)
|
---|
| 460 | {
|
---|
| 461 | LogError("Invalid Parameter scope: %p", scope);
|
---|
| 462 | result = NULL;
|
---|
| 463 | }
|
---|
| 464 | else
|
---|
| 465 | {
|
---|
| 466 | STRING_HANDLE sas_token;
|
---|
| 467 | size_t sec_since_epoch;
|
---|
| 468 |
|
---|
| 469 | /* Codes_SRS_IoTHub_Authorization_07_010: [ IoTHubClient_Auth_Get_SasToken` shall construct the expiration time using the handle->token_expiry_time_sec added to epoch time. ] */
|
---|
| 470 | if (get_seconds_since_epoch(&sec_since_epoch) != 0)
|
---|
| 471 | {
|
---|
| 472 | /* Codes_SRS_IoTHub_Authorization_07_020: [ If any error is encountered IoTHubClient_Auth_Get_ConnString shall return NULL. ] */
|
---|
| 473 | LogError("failure getting seconds from epoch");
|
---|
| 474 | result = NULL;
|
---|
| 475 | }
|
---|
| 476 | else
|
---|
| 477 | {
|
---|
| 478 | /* Codes_SRS_IoTHub_Authorization_07_011: [ IoTHubClient_Auth_Get_ConnString shall call SASToken_CreateString to construct the sas token. ] */
|
---|
| 479 | size_t expiry_time = sec_since_epoch + handle->token_expiry_time_sec;
|
---|
| 480 | if ( (sas_token = SASToken_CreateString(handle->device_key, scope, key_name, expiry_time)) == NULL)
|
---|
| 481 | {
|
---|
| 482 | /* Codes_SRS_IoTHub_Authorization_07_020: [ If any error is encountered IoTHubClient_Auth_Get_ConnString shall return NULL. ] */
|
---|
| 483 | LogError("Failed creating sas_token");
|
---|
| 484 | result = NULL;
|
---|
| 485 | }
|
---|
| 486 | else
|
---|
| 487 | {
|
---|
| 488 | /* Codes_SRS_IoTHub_Authorization_07_012: [ On success IoTHubClient_Auth_Get_ConnString shall allocate and return the sas token in a char*. ] */
|
---|
| 489 | if (mallocAndStrcpy_s(&result, STRING_c_str(sas_token) ) != 0)
|
---|
| 490 | {
|
---|
| 491 | /* Codes_SRS_IoTHub_Authorization_07_020: [ If any error is encountered IoTHubClient_Auth_Get_ConnString shall return NULL. ] */
|
---|
| 492 | LogError("Failed copying result");
|
---|
| 493 | result = NULL;
|
---|
| 494 | }
|
---|
| 495 | STRING_delete(sas_token);
|
---|
| 496 | }
|
---|
| 497 | }
|
---|
| 498 | }
|
---|
| 499 | }
|
---|
| 500 | else
|
---|
| 501 | {
|
---|
| 502 | LogError("Failed getting sas token invalid credential type");
|
---|
| 503 | result = NULL;
|
---|
| 504 | }
|
---|
| 505 | }
|
---|
| 506 | return result;
|
---|
| 507 | }
|
---|
| 508 |
|
---|
| 509 | const char* IoTHubClient_Auth_Get_DeviceId(IOTHUB_AUTHORIZATION_HANDLE handle)
|
---|
| 510 | {
|
---|
| 511 | const char* result;
|
---|
| 512 | if (handle == NULL)
|
---|
| 513 | {
|
---|
| 514 | /* Codes_SRS_IoTHub_Authorization_07_013: [ if handle is NULL, IoTHubClient_Auth_Get_DeviceId shall return NULL. ] */
|
---|
| 515 | LogError("Invalid Parameter handle: %p", handle);
|
---|
| 516 | result = NULL;
|
---|
| 517 | }
|
---|
| 518 | else
|
---|
| 519 | {
|
---|
| 520 | /* Codes_SRS_IoTHub_Authorization_07_014: [ IoTHubClient_Auth_Get_DeviceId shall return the device_id specified upon creation. ] */
|
---|
| 521 | result = handle->device_id;
|
---|
| 522 | }
|
---|
| 523 | return result;
|
---|
| 524 | }
|
---|
| 525 |
|
---|
| 526 | const char* IoTHubClient_Auth_Get_ModuleId(IOTHUB_AUTHORIZATION_HANDLE handle)
|
---|
| 527 | {
|
---|
| 528 | const char* result;
|
---|
| 529 | if (handle == NULL)
|
---|
| 530 | {
|
---|
| 531 | /* Codes_SRS_IoTHub_Authorization_31_025: [ if handle is NULL, IoTHubClient_Auth_Get_ModuleId shall return NULL. ] */
|
---|
| 532 | LogError("Invalid Parameter handle: %p", handle);
|
---|
| 533 | result = NULL;
|
---|
| 534 | }
|
---|
| 535 | else
|
---|
| 536 | {
|
---|
| 537 | /* Codes_SRS_IoTHub_Authorization_31_026: [ IoTHubClient_Auth_Get_ModuleId shall return the module_id specified upon creation. ] */
|
---|
| 538 | result = handle->module_id;
|
---|
| 539 | }
|
---|
| 540 | return result;
|
---|
| 541 | }
|
---|
| 542 |
|
---|
| 543 | const char* IoTHubClient_Auth_Get_DeviceKey(IOTHUB_AUTHORIZATION_HANDLE handle)
|
---|
| 544 | {
|
---|
| 545 | const char* result;
|
---|
| 546 | if (handle == NULL)
|
---|
| 547 | {
|
---|
| 548 | /* Codes_SRS_IoTHub_Authorization_07_022: [ if handle is NULL, IoTHubClient_Auth_Get_DeviceKey shall return NULL. ] */
|
---|
| 549 | LogError("Invalid Parameter handle: %p", handle);
|
---|
| 550 | result = NULL;
|
---|
| 551 | }
|
---|
| 552 | else
|
---|
| 553 | {
|
---|
| 554 | /* Codes_SRS_IoTHub_Authorization_07_023: [ IoTHubClient_Auth_Get_DeviceKey shall return the device_key specified upon creation. ] */
|
---|
| 555 | result = handle->device_key;
|
---|
| 556 | }
|
---|
| 557 | return result;
|
---|
| 558 | }
|
---|
| 559 |
|
---|
| 560 | SAS_TOKEN_STATUS IoTHubClient_Auth_Is_SasToken_Valid(IOTHUB_AUTHORIZATION_HANDLE handle)
|
---|
| 561 | {
|
---|
| 562 | SAS_TOKEN_STATUS result;
|
---|
| 563 | if (handle == NULL)
|
---|
| 564 | {
|
---|
| 565 | /* Codes_SRS_IoTHub_Authorization_07_015: [ if handle is NULL, IoTHubClient_Auth_Is_SasToken_Valid shall return false. ] */
|
---|
| 566 | LogError("Invalid Parameter handle: %p", handle);
|
---|
| 567 | result = SAS_TOKEN_STATUS_FAILED;
|
---|
| 568 | }
|
---|
| 569 | else
|
---|
| 570 | {
|
---|
| 571 | if (handle->cred_type == IOTHUB_CREDENTIAL_TYPE_SAS_TOKEN)
|
---|
| 572 | {
|
---|
| 573 | if (handle->device_sas_token == NULL)
|
---|
| 574 | {
|
---|
| 575 | /* Codes_SRS_IoTHub_Authorization_07_017: [ If the sas_token is NULL IoTHubClient_Auth_Is_SasToken_Valid shall return false. ] */
|
---|
| 576 | LogError("Failure: device_sas_toke is NULL");
|
---|
| 577 | result = SAS_TOKEN_STATUS_FAILED;
|
---|
| 578 | }
|
---|
| 579 | else
|
---|
| 580 | {
|
---|
| 581 | /* Codes_SRS_IoTHub_Authorization_07_018: [ otherwise IoTHubClient_Auth_Is_SasToken_Valid shall return the value returned by SASToken_Validate. ] */
|
---|
| 582 | STRING_HANDLE strSasToken = STRING_construct(handle->device_sas_token);
|
---|
| 583 | if (strSasToken != NULL)
|
---|
| 584 | {
|
---|
| 585 | if (!SASToken_Validate(strSasToken))
|
---|
| 586 | {
|
---|
| 587 | result = SAS_TOKEN_STATUS_INVALID;
|
---|
| 588 | }
|
---|
| 589 | else
|
---|
| 590 | {
|
---|
| 591 | result = SAS_TOKEN_STATUS_VALID;
|
---|
| 592 | }
|
---|
| 593 | STRING_delete(strSasToken);
|
---|
| 594 | }
|
---|
| 595 | else
|
---|
| 596 | {
|
---|
| 597 | LogError("Failure constructing SAS Token");
|
---|
| 598 | result = SAS_TOKEN_STATUS_FAILED;
|
---|
| 599 | }
|
---|
| 600 | }
|
---|
| 601 | }
|
---|
| 602 | else
|
---|
| 603 | {
|
---|
| 604 | /* 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. ] */
|
---|
| 605 | result = SAS_TOKEN_STATUS_VALID;
|
---|
| 606 | }
|
---|
| 607 | }
|
---|
| 608 | return result;
|
---|
| 609 | }
|
---|
| 610 |
|
---|
| 611 | #ifdef USE_EDGE_MODULES
|
---|
| 612 |
|
---|
| 613 | // For debugging C modules, the environment can set the environment variable 'EdgeModuleCACertificateFile' to provide
|
---|
| 614 | // trusted certificates. We'd otherwise usually get these from trusted Edge service, but this complicates debugging experience.
|
---|
| 615 | // EdgeModuleCACertificateFile and the related EdgeHubConnectionString can be set either manually or by tooling (e.g. VS Code).
|
---|
| 616 | static char* read_ca_certificate_from_file(const char* certificate_file_name)
|
---|
| 617 | {
|
---|
| 618 | char* result;
|
---|
| 619 | FILE *file_stream = NULL;
|
---|
| 620 |
|
---|
| 621 | if ((file_stream = fopen(certificate_file_name, "r")) == NULL)
|
---|
| 622 | {
|
---|
| 623 | LogError("Cannot read file %s, errno=%d", certificate_file_name, errno);
|
---|
| 624 | result = NULL;
|
---|
| 625 | }
|
---|
| 626 | else if (fseek(file_stream, 0, SEEK_END) != 0)
|
---|
| 627 | {
|
---|
| 628 | LogError("fseek on file %s fails, errno=%d", certificate_file_name, errno);
|
---|
| 629 | result = NULL;
|
---|
| 630 | }
|
---|
| 631 | else
|
---|
| 632 | {
|
---|
| 633 | long int file_size = ftell(file_stream);
|
---|
| 634 | if (file_size < 0)
|
---|
| 635 | {
|
---|
| 636 | LogError("ftell fails reading %s, errno=%d", certificate_file_name, errno);
|
---|
| 637 | result = NULL;
|
---|
| 638 | }
|
---|
| 639 | else if (file_size == 0)
|
---|
| 640 | {
|
---|
| 641 | LogError("file %s is 0 bytes, which is not valid certificate", certificate_file_name);
|
---|
| 642 | result = NULL;
|
---|
| 643 | }
|
---|
| 644 | else
|
---|
| 645 | {
|
---|
| 646 | rewind(file_stream);
|
---|
| 647 |
|
---|
| 648 | if ((result = calloc(1, file_size + 1)) == NULL)
|
---|
| 649 | {
|
---|
| 650 | LogError("Cannot allocate %lu bytes", (unsigned long)file_size);
|
---|
| 651 | }
|
---|
| 652 | else if ((fread(result, 1, file_size, file_stream) == 0) || (ferror(file_stream) != 0))
|
---|
| 653 | {
|
---|
| 654 | LogError("fread failed on file %s, errno=%d", certificate_file_name, errno);
|
---|
| 655 | free(result);
|
---|
| 656 | result = NULL;
|
---|
| 657 | }
|
---|
| 658 | }
|
---|
| 659 | }
|
---|
| 660 |
|
---|
| 661 | if (file_stream != NULL)
|
---|
| 662 | {
|
---|
| 663 | fclose(file_stream);
|
---|
| 664 | }
|
---|
| 665 |
|
---|
| 666 | return result;
|
---|
| 667 | }
|
---|
| 668 |
|
---|
| 669 | // IoTHubClient_Auth_Get_TrustBundle retrieves a trust bundle - namely a PEM indicating the certificates the client should
|
---|
| 670 | // trust as root authorities - to caller. If certificate_file_name, we read this from a local file. This should in general
|
---|
| 671 | // be limited only to debugging modules on Edge. If certificate_file_name is NULL, we invoke into the underlying
|
---|
| 672 | // HSM to retrieve this.
|
---|
| 673 | char* IoTHubClient_Auth_Get_TrustBundle(IOTHUB_AUTHORIZATION_HANDLE handle, const char* certificate_file_name)
|
---|
| 674 | {
|
---|
| 675 | char* result;
|
---|
| 676 | if (handle == NULL)
|
---|
| 677 | {
|
---|
| 678 | LogError("Security Handle is NULL");
|
---|
| 679 | result = NULL;
|
---|
| 680 | }
|
---|
| 681 | else if (certificate_file_name != NULL)
|
---|
| 682 | {
|
---|
| 683 | result = read_ca_certificate_from_file(certificate_file_name);
|
---|
| 684 | }
|
---|
| 685 | else
|
---|
| 686 | {
|
---|
| 687 | result = iothub_device_auth_get_trust_bundle(handle->device_auth_handle);
|
---|
| 688 | }
|
---|
| 689 | return result;
|
---|
| 690 | }
|
---|
| 691 | #endif
|
---|
| 692 |
|
---|
| 693 | int IoTHubClient_Auth_Set_SasToken_Expiry(IOTHUB_AUTHORIZATION_HANDLE handle, size_t expiry_time_seconds)
|
---|
| 694 | {
|
---|
| 695 | int result;
|
---|
| 696 | if (handle == NULL)
|
---|
| 697 | {
|
---|
| 698 | LogError("Invalid handle value handle: NULL");
|
---|
| 699 | result = MU_FAILURE;
|
---|
| 700 | }
|
---|
| 701 | // Validate the expiry_time in seconds
|
---|
| 702 | else if (expiry_time_seconds < MIN_SAS_EXPIRY_TIME)
|
---|
| 703 | {
|
---|
| 704 | LogError("Failure setting expiry time to value %lu min value is %d", (unsigned long)expiry_time_seconds, MIN_SAS_EXPIRY_TIME);
|
---|
| 705 | result = MU_FAILURE;
|
---|
| 706 | }
|
---|
| 707 | else
|
---|
| 708 | {
|
---|
| 709 | handle->token_expiry_time_sec = expiry_time_seconds;
|
---|
| 710 | result = 0;
|
---|
| 711 | }
|
---|
| 712 | return result;
|
---|
| 713 | }
|
---|
| 714 |
|
---|
| 715 | size_t IoTHubClient_Auth_Get_SasToken_Expiry(IOTHUB_AUTHORIZATION_HANDLE handle)
|
---|
| 716 | {
|
---|
| 717 | size_t result;
|
---|
| 718 | if (handle == NULL)
|
---|
| 719 | {
|
---|
| 720 | LogError("Invalid handle value handle: NULL");
|
---|
| 721 | result = 0;
|
---|
| 722 | }
|
---|
| 723 | else
|
---|
| 724 | {
|
---|
| 725 | result = handle->token_expiry_time_sec;
|
---|
| 726 | }
|
---|
| 727 | return result;
|
---|
| 728 | }
|
---|