A small memory footprint AMQP implimentation
Dependents: iothub_client_sample_amqp remote_monitoring simplesample_amqp
sasl_mechanism.c@20:206846c14c80, 2017-03-10 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Fri Mar 10 11:47:49 2017 -0800
- Revision:
- 20:206846c14c80
- Parent:
- 19:000ab4e6a2c1
- Child:
- 23:1111ee8bcba4
1.1.9
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Azure.IoT Build | 0:6ae2f7bca550 | 1 | // Copyright (c) Microsoft. All rights reserved. |
Azure.IoT Build | 0:6ae2f7bca550 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
Azure.IoT Build | 0:6ae2f7bca550 | 3 | |
Azure.IoT Build | 0:6ae2f7bca550 | 4 | #include <stdlib.h> |
AzureIoTClient | 19:000ab4e6a2c1 | 5 | #include "azure_c_shared_utility/optimize_size.h" |
AzureIoTClient | 20:206846c14c80 | 6 | #include "azure_c_shared_utility/gballoc.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 7 | #include "azure_uamqp_c/sasl_mechanism.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 8 | |
Azure.IoT Build | 0:6ae2f7bca550 | 9 | typedef struct SASL_MECHANISM_INSTANCE_TAG |
Azure.IoT Build | 0:6ae2f7bca550 | 10 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 11 | const SASL_MECHANISM_INTERFACE_DESCRIPTION* sasl_mechanism_interface_description; |
Azure.IoT Build | 0:6ae2f7bca550 | 12 | CONCRETE_SASL_MECHANISM_HANDLE concrete_sasl_mechanism_handle; |
Azure.IoT Build | 0:6ae2f7bca550 | 13 | } SASL_MECHANISM_INSTANCE; |
Azure.IoT Build | 0:6ae2f7bca550 | 14 | |
Azure.IoT Build | 0:6ae2f7bca550 | 15 | SASL_MECHANISM_HANDLE saslmechanism_create(const SASL_MECHANISM_INTERFACE_DESCRIPTION* sasl_mechanism_interface_description, void* sasl_mechanism_create_parameters) |
Azure.IoT Build | 0:6ae2f7bca550 | 16 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 17 | SASL_MECHANISM_INSTANCE* sasl_mechanism_instance; |
Azure.IoT Build | 0:6ae2f7bca550 | 18 | |
Azure.IoT Build | 0:6ae2f7bca550 | 19 | /* Codes_SRS_SASL_MECHANISM_01_004: [If the argument sasl_mechanism_interface_description is NULL, saslmechanism_create shall return NULL.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 20 | if ((sasl_mechanism_interface_description == NULL) || |
Azure.IoT Build | 0:6ae2f7bca550 | 21 | /* Codes_SRS_SASL_MECHANISM_01_005: [If any sasl_mechanism_interface_description member is NULL, sasl_mechanism_create shall fail and return NULL.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 22 | (sasl_mechanism_interface_description->concrete_sasl_mechanism_create == NULL) || |
Azure.IoT Build | 0:6ae2f7bca550 | 23 | (sasl_mechanism_interface_description->concrete_sasl_mechanism_destroy == NULL) || |
Azure.IoT Build | 0:6ae2f7bca550 | 24 | (sasl_mechanism_interface_description->concrete_sasl_mechanism_get_init_bytes == NULL) || |
Azure.IoT Build | 0:6ae2f7bca550 | 25 | (sasl_mechanism_interface_description->concrete_sasl_mechanism_get_mechanism_name == NULL)) |
Azure.IoT Build | 0:6ae2f7bca550 | 26 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 27 | sasl_mechanism_instance = NULL; |
Azure.IoT Build | 0:6ae2f7bca550 | 28 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 29 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 30 | { |
AzureIoTClient | 20:206846c14c80 | 31 | sasl_mechanism_instance = (SASL_MECHANISM_INSTANCE*)malloc(sizeof(SASL_MECHANISM_INSTANCE)); |
Azure.IoT Build | 0:6ae2f7bca550 | 32 | |
Azure.IoT Build | 0:6ae2f7bca550 | 33 | if (sasl_mechanism_instance != NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 34 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 35 | sasl_mechanism_instance->sasl_mechanism_interface_description = sasl_mechanism_interface_description; |
Azure.IoT Build | 0:6ae2f7bca550 | 36 | |
Azure.IoT Build | 0:6ae2f7bca550 | 37 | /* Codes_SRS_SASL_MECHANISM_01_002: [In order to instantiate the concrete SASL mechanism implementation the function concrete_sasl_mechanism_create from the sasl_mechanism_interface_description shall be called, passing the sasl_mechanism_create_parameters to it.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 38 | sasl_mechanism_instance->concrete_sasl_mechanism_handle = sasl_mechanism_instance->sasl_mechanism_interface_description->concrete_sasl_mechanism_create((void*)sasl_mechanism_create_parameters); |
Azure.IoT Build | 0:6ae2f7bca550 | 39 | if (sasl_mechanism_instance->concrete_sasl_mechanism_handle == NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 40 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 41 | /* Codes_SRS_SASL_MECHANISM_01_003: [If the underlying concrete_sasl_mechanism_create call fails, saslmechanism_create shall return NULL.] */ |
AzureIoTClient | 20:206846c14c80 | 42 | free(sasl_mechanism_instance); |
Azure.IoT Build | 0:6ae2f7bca550 | 43 | sasl_mechanism_instance = NULL; |
Azure.IoT Build | 0:6ae2f7bca550 | 44 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 45 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 46 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 47 | |
Azure.IoT Build | 0:6ae2f7bca550 | 48 | /* Codes_SRS_SASL_MECHANISM_01_001: [saslmechanism_create shall return on success a non-NULL handle to a new SASL mechanism interface.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 49 | return (SASL_MECHANISM_HANDLE)sasl_mechanism_instance; |
Azure.IoT Build | 0:6ae2f7bca550 | 50 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 51 | |
Azure.IoT Build | 0:6ae2f7bca550 | 52 | void saslmechanism_destroy(SASL_MECHANISM_HANDLE sasl_mechanism) |
Azure.IoT Build | 0:6ae2f7bca550 | 53 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 54 | if (sasl_mechanism != NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 55 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 56 | SASL_MECHANISM_INSTANCE* sasl_mechanism_instance = (SASL_MECHANISM_INSTANCE*)sasl_mechanism; |
Azure.IoT Build | 0:6ae2f7bca550 | 57 | |
Azure.IoT Build | 0:6ae2f7bca550 | 58 | /* Codes_SRS_SASL_MECHANISM_01_008: [saslmechanism_destroy shall also call the concrete_sasl_mechanism_destroy function that is member of the sasl_mechanism_interface_description argument passed to saslmechanism_create, while passing as argument to concrete_sasl_mechanism_destroy the result of the underlying concrete SASL mechanism handle.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 59 | sasl_mechanism_instance->sasl_mechanism_interface_description->concrete_sasl_mechanism_destroy(sasl_mechanism_instance->concrete_sasl_mechanism_handle); |
Azure.IoT Build | 0:6ae2f7bca550 | 60 | |
Azure.IoT Build | 0:6ae2f7bca550 | 61 | /* Codes_SRS_SASL_MECHANISM_01_007: [saslmechanism_destroy shall free all resources associated with the SASL mechanism handle.] */ |
AzureIoTClient | 20:206846c14c80 | 62 | free(sasl_mechanism_instance); |
Azure.IoT Build | 0:6ae2f7bca550 | 63 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 64 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 65 | |
Azure.IoT Build | 0:6ae2f7bca550 | 66 | int saslmechanism_get_init_bytes(SASL_MECHANISM_HANDLE sasl_mechanism, SASL_MECHANISM_BYTES* init_bytes) |
Azure.IoT Build | 0:6ae2f7bca550 | 67 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 68 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 69 | |
Azure.IoT Build | 0:6ae2f7bca550 | 70 | /* Codes_SRS_SASL_MECHANISM_01_012: [If the argument sasl_mechanism is NULL, saslmechanism_get_init_bytes shall fail and return a non-zero value.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 71 | if (sasl_mechanism == NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 72 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 73 | result = __FAILURE__; |
Azure.IoT Build | 0:6ae2f7bca550 | 74 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 75 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 76 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 77 | SASL_MECHANISM_INSTANCE* sasl_mechanism_instance = (SASL_MECHANISM_INSTANCE*)sasl_mechanism; |
Azure.IoT Build | 0:6ae2f7bca550 | 78 | |
Azure.IoT Build | 0:6ae2f7bca550 | 79 | /* Codes_SRS_SASL_MECHANISM_01_010: [saslmechanism_get_init_bytes shall call the specific concrete_sasl_mechanism_get_init_bytes function specified in saslmechanism_create, passing the init_bytes argument to it.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 80 | if (sasl_mechanism_instance->sasl_mechanism_interface_description->concrete_sasl_mechanism_get_init_bytes(sasl_mechanism_instance->concrete_sasl_mechanism_handle, init_bytes) != 0) |
Azure.IoT Build | 0:6ae2f7bca550 | 81 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 82 | /* Codes_SRS_SASL_MECHANISM_01_013: [If the underlying concrete_sasl_mechanism_get_init_bytes fails, saslmechanism_get_init_bytes shall fail and return a non-zero value.] */ |
AzureIoTClient | 19:000ab4e6a2c1 | 83 | result = __FAILURE__; |
Azure.IoT Build | 0:6ae2f7bca550 | 84 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 85 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 86 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 87 | /* Codes_SRS_SASL_MECHANISM_01_011: [On success, saslmechanism_get_init_bytes shall return 0.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 88 | result = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 89 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 90 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 91 | |
Azure.IoT Build | 0:6ae2f7bca550 | 92 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 93 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 94 | |
Azure.IoT Build | 0:6ae2f7bca550 | 95 | const char* saslmechanism_get_mechanism_name(SASL_MECHANISM_HANDLE sasl_mechanism) |
Azure.IoT Build | 0:6ae2f7bca550 | 96 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 97 | const char* result; |
Azure.IoT Build | 0:6ae2f7bca550 | 98 | |
Azure.IoT Build | 0:6ae2f7bca550 | 99 | /* Codes_SRS_SASL_MECHANISM_01_016: [If the argument sasl_mechanism is NULL, saslmechanism_get_mechanism_name shall fail and return a non-zero value.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 100 | if (sasl_mechanism == NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 101 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 102 | result = NULL; |
Azure.IoT Build | 0:6ae2f7bca550 | 103 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 104 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 105 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 106 | SASL_MECHANISM_INSTANCE* sasl_mechanism_instance = (SASL_MECHANISM_INSTANCE*)sasl_mechanism; |
Azure.IoT Build | 0:6ae2f7bca550 | 107 | |
Azure.IoT Build | 0:6ae2f7bca550 | 108 | /* Codes_SRS_SASL_MECHANISM_01_014: [saslmechanism_get_mechanism_name shall call the specific concrete_sasl_mechanism_get_mechanism_name function specified in saslmechanism_create.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 109 | /* Codes_SRS_SASL_MECHANISM_01_015: [On success, saslmechanism_get_mechanism_name shall return a pointer to a string with the mechanism name.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 110 | /* Codes_SRS_SASL_MECHANISM_01_017: [If the underlying concrete_sasl_mechanism_get_mechanism_name fails, saslmechanism_get_mechanism_name shall return NULL.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 111 | result = sasl_mechanism_instance->sasl_mechanism_interface_description->concrete_sasl_mechanism_get_mechanism_name(sasl_mechanism_instance->concrete_sasl_mechanism_handle); |
Azure.IoT Build | 0:6ae2f7bca550 | 112 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 113 | |
Azure.IoT Build | 0:6ae2f7bca550 | 114 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 115 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 116 | |
Azure.IoT Build | 0:6ae2f7bca550 | 117 | int saslmechanism_challenge(SASL_MECHANISM_HANDLE sasl_mechanism, const SASL_MECHANISM_BYTES* challenge_bytes, SASL_MECHANISM_BYTES* response_bytes) |
Azure.IoT Build | 0:6ae2f7bca550 | 118 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 119 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 120 | |
Azure.IoT Build | 0:6ae2f7bca550 | 121 | /* Codes_SRS_SASL_MECHANISM_01_020: [If the argument sasl_mechanism is NULL, saslmechanism_challenge shall fail and return a non-zero value.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 122 | if (sasl_mechanism == NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 123 | { |
AzureIoTClient | 19:000ab4e6a2c1 | 124 | result = __FAILURE__; |
Azure.IoT Build | 0:6ae2f7bca550 | 125 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 126 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 127 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 128 | /* Codes_SRS_SASL_MECHANISM_01_018: [saslmechanism_challenge shall call the specific concrete_sasl_mechanism_challenge function specified in saslmechanism_create, while passing the challenge_bytes and response_bytes arguments to it.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 129 | if (sasl_mechanism->sasl_mechanism_interface_description->concrete_sasl_mechanism_challenge(sasl_mechanism->concrete_sasl_mechanism_handle, challenge_bytes, response_bytes) != 0) |
Azure.IoT Build | 0:6ae2f7bca550 | 130 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 131 | /* Codes_SRS_SASL_MECHANISM_01_021: [If the underlying concrete_sasl_mechanism_challenge fails, saslmechanism_challenge shall fail and return a non-zero value.] */ |
AzureIoTClient | 19:000ab4e6a2c1 | 132 | result = __FAILURE__; |
Azure.IoT Build | 0:6ae2f7bca550 | 133 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 134 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 135 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 136 | /* Codes_SRS_SASL_MECHANISM_01_019: [On success, saslmechanism_challenge shall return 0.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 137 | result = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 138 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 139 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 140 | |
Azure.IoT Build | 0:6ae2f7bca550 | 141 | return result; |
AzureIoTClient | 6:641a9672db08 | 142 | } |