A small memory footprint AMQP implimentation
Dependents: iothub_client_sample_amqp remote_monitoring simplesample_amqp
sasl_mechanism.c@23:1111ee8bcba4, 2017-04-21 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Fri Apr 21 14:50:32 2017 -0700
- Revision:
- 23:1111ee8bcba4
- Parent:
- 20:206846c14c80
- Child:
- 28:add19eb7defa
1.1.13
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" |
AzureIoTClient | 23:1111ee8bcba4 | 7 | #include "azure_c_shared_utility/xlogging.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 8 | #include "azure_uamqp_c/sasl_mechanism.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 9 | |
Azure.IoT Build | 0:6ae2f7bca550 | 10 | typedef struct SASL_MECHANISM_INSTANCE_TAG |
Azure.IoT Build | 0:6ae2f7bca550 | 11 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 12 | const SASL_MECHANISM_INTERFACE_DESCRIPTION* sasl_mechanism_interface_description; |
Azure.IoT Build | 0:6ae2f7bca550 | 13 | CONCRETE_SASL_MECHANISM_HANDLE concrete_sasl_mechanism_handle; |
Azure.IoT Build | 0:6ae2f7bca550 | 14 | } SASL_MECHANISM_INSTANCE; |
Azure.IoT Build | 0:6ae2f7bca550 | 15 | |
Azure.IoT Build | 0:6ae2f7bca550 | 16 | SASL_MECHANISM_HANDLE saslmechanism_create(const SASL_MECHANISM_INTERFACE_DESCRIPTION* sasl_mechanism_interface_description, void* sasl_mechanism_create_parameters) |
Azure.IoT Build | 0:6ae2f7bca550 | 17 | { |
AzureIoTClient | 23:1111ee8bcba4 | 18 | SASL_MECHANISM_HANDLE result; |
Azure.IoT Build | 0:6ae2f7bca550 | 19 | |
AzureIoTClient | 23:1111ee8bcba4 | 20 | /* Codes_SRS_SASL_MECHANISM_01_004: [If the argument `sasl_mechanism_interface_description` is NULL, `saslmechanism_create` shall return NULL.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 21 | if (sasl_mechanism_interface_description == NULL) |
AzureIoTClient | 23:1111ee8bcba4 | 22 | { |
AzureIoTClient | 23:1111ee8bcba4 | 23 | LogError("NULL sasl_mechanism_interface_description"); |
AzureIoTClient | 23:1111ee8bcba4 | 24 | result = NULL; |
AzureIoTClient | 23:1111ee8bcba4 | 25 | } |
AzureIoTClient | 23:1111ee8bcba4 | 26 | /* Codes_SRS_SASL_MECHANISM_01_005: [If any `sasl_mechanism_interface_description` member is NULL, `sasl_mechanism_create` shall fail and return NULL.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 27 | else if ((sasl_mechanism_interface_description->concrete_sasl_mechanism_create == NULL) || |
Azure.IoT Build | 0:6ae2f7bca550 | 28 | (sasl_mechanism_interface_description->concrete_sasl_mechanism_destroy == NULL) || |
Azure.IoT Build | 0:6ae2f7bca550 | 29 | (sasl_mechanism_interface_description->concrete_sasl_mechanism_get_init_bytes == NULL) || |
Azure.IoT Build | 0:6ae2f7bca550 | 30 | (sasl_mechanism_interface_description->concrete_sasl_mechanism_get_mechanism_name == NULL)) |
Azure.IoT Build | 0:6ae2f7bca550 | 31 | { |
AzureIoTClient | 23:1111ee8bcba4 | 32 | LogError("Bad interface, concrete_sasl_mechanism_create = %p, concrete_sasl_mechanism_destroy = %p, concrete_sasl_mechanism_get_init_bytes = %p, concrete_sasl_mechanism_get_mechanism_name = %p", |
AzureIoTClient | 23:1111ee8bcba4 | 33 | sasl_mechanism_interface_description->concrete_sasl_mechanism_create, |
AzureIoTClient | 23:1111ee8bcba4 | 34 | sasl_mechanism_interface_description->concrete_sasl_mechanism_destroy, |
AzureIoTClient | 23:1111ee8bcba4 | 35 | sasl_mechanism_interface_description->concrete_sasl_mechanism_get_init_bytes, |
AzureIoTClient | 23:1111ee8bcba4 | 36 | sasl_mechanism_interface_description->concrete_sasl_mechanism_get_mechanism_name); |
AzureIoTClient | 23:1111ee8bcba4 | 37 | result = NULL; |
Azure.IoT Build | 0:6ae2f7bca550 | 38 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 39 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 40 | { |
AzureIoTClient | 23:1111ee8bcba4 | 41 | result = (SASL_MECHANISM_HANDLE)malloc(sizeof(SASL_MECHANISM_INSTANCE)); |
AzureIoTClient | 23:1111ee8bcba4 | 42 | if (result == NULL) |
AzureIoTClient | 23:1111ee8bcba4 | 43 | { |
AzureIoTClient | 23:1111ee8bcba4 | 44 | /* Codes_SRS_SASL_MECHANISM_01_006: [If allocating the memory needed for the SASL mechanism interface fails then `saslmechanism_create` shall fail and return NULL.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 45 | LogError("Could not allocate memory for SASL mechanism"); |
AzureIoTClient | 23:1111ee8bcba4 | 46 | } |
AzureIoTClient | 23:1111ee8bcba4 | 47 | else |
AzureIoTClient | 23:1111ee8bcba4 | 48 | { |
AzureIoTClient | 23:1111ee8bcba4 | 49 | result->sasl_mechanism_interface_description = sasl_mechanism_interface_description; |
Azure.IoT Build | 0:6ae2f7bca550 | 50 | |
AzureIoTClient | 23:1111ee8bcba4 | 51 | /* 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.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 52 | result->concrete_sasl_mechanism_handle = result->sasl_mechanism_interface_description->concrete_sasl_mechanism_create((void*)sasl_mechanism_create_parameters); |
AzureIoTClient | 23:1111ee8bcba4 | 53 | if (result->concrete_sasl_mechanism_handle == NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 54 | { |
AzureIoTClient | 23:1111ee8bcba4 | 55 | /* Codes_SRS_SASL_MECHANISM_01_003: [If the underlying `concrete_sasl_mechanism_create` call fails, `saslmechanism_create` shall return NULL.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 56 | LogError("concrete_sasl_mechanism_create failed"); |
AzureIoTClient | 23:1111ee8bcba4 | 57 | free(result); |
AzureIoTClient | 23:1111ee8bcba4 | 58 | result = NULL; |
Azure.IoT Build | 0:6ae2f7bca550 | 59 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 60 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 61 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 62 | |
AzureIoTClient | 23:1111ee8bcba4 | 63 | /* Codes_SRS_SASL_MECHANISM_01_001: [`saslmechanism_create` shall return on success a non-NULL handle to a new SASL mechanism interface.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 64 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 65 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 66 | |
Azure.IoT Build | 0:6ae2f7bca550 | 67 | void saslmechanism_destroy(SASL_MECHANISM_HANDLE sasl_mechanism) |
Azure.IoT Build | 0:6ae2f7bca550 | 68 | { |
AzureIoTClient | 23:1111ee8bcba4 | 69 | if (sasl_mechanism == NULL) |
AzureIoTClient | 23:1111ee8bcba4 | 70 | { |
AzureIoTClient | 23:1111ee8bcba4 | 71 | /* Codes_SRS_SASL_MECHANISM_01_009: [If the argument `sasl_mechanism` is NULL, `saslmechanism_destroy` shall do nothing.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 72 | LogError("NULL sasl_mechanism"); |
AzureIoTClient | 23:1111ee8bcba4 | 73 | } |
AzureIoTClient | 23:1111ee8bcba4 | 74 | else |
AzureIoTClient | 23:1111ee8bcba4 | 75 | { |
AzureIoTClient | 23:1111ee8bcba4 | 76 | /* 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.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 77 | sasl_mechanism->sasl_mechanism_interface_description->concrete_sasl_mechanism_destroy(sasl_mechanism->concrete_sasl_mechanism_handle); |
Azure.IoT Build | 0:6ae2f7bca550 | 78 | |
AzureIoTClient | 23:1111ee8bcba4 | 79 | /* Codes_SRS_SASL_MECHANISM_01_007: [`saslmechanism_destroy` shall free all resources associated with the SASL mechanism handle.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 80 | free(sasl_mechanism); |
Azure.IoT Build | 0:6ae2f7bca550 | 81 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 82 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 83 | |
Azure.IoT Build | 0:6ae2f7bca550 | 84 | int saslmechanism_get_init_bytes(SASL_MECHANISM_HANDLE sasl_mechanism, SASL_MECHANISM_BYTES* init_bytes) |
Azure.IoT Build | 0:6ae2f7bca550 | 85 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 86 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 87 | |
AzureIoTClient | 23:1111ee8bcba4 | 88 | /* 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 | 89 | if (sasl_mechanism == NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 90 | { |
AzureIoTClient | 23:1111ee8bcba4 | 91 | LogError("NULL sasl_mechanism"); |
AzureIoTClient | 23:1111ee8bcba4 | 92 | result = __FAILURE__; |
Azure.IoT Build | 0:6ae2f7bca550 | 93 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 94 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 95 | { |
AzureIoTClient | 23:1111ee8bcba4 | 96 | /* 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.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 97 | if (sasl_mechanism->sasl_mechanism_interface_description->concrete_sasl_mechanism_get_init_bytes(sasl_mechanism->concrete_sasl_mechanism_handle, init_bytes) != 0) |
Azure.IoT Build | 0:6ae2f7bca550 | 98 | { |
AzureIoTClient | 23:1111ee8bcba4 | 99 | /* 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 | 23:1111ee8bcba4 | 100 | LogError("concrete_sasl_mechanism_get_init_bytes failed"); |
AzureIoTClient | 23:1111ee8bcba4 | 101 | result = __FAILURE__; |
Azure.IoT Build | 0:6ae2f7bca550 | 102 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 103 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 104 | { |
AzureIoTClient | 23:1111ee8bcba4 | 105 | /* Codes_SRS_SASL_MECHANISM_01_011: [On success, `saslmechanism_get_init_bytes` shall return 0.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 106 | result = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 107 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 108 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 109 | |
Azure.IoT Build | 0:6ae2f7bca550 | 110 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 111 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 112 | |
Azure.IoT Build | 0:6ae2f7bca550 | 113 | const char* saslmechanism_get_mechanism_name(SASL_MECHANISM_HANDLE sasl_mechanism) |
Azure.IoT Build | 0:6ae2f7bca550 | 114 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 115 | const char* result; |
Azure.IoT Build | 0:6ae2f7bca550 | 116 | |
AzureIoTClient | 23:1111ee8bcba4 | 117 | /* 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 | 118 | if (sasl_mechanism == NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 119 | { |
AzureIoTClient | 23:1111ee8bcba4 | 120 | LogError("NULL sasl_mechanism"); |
AzureIoTClient | 23:1111ee8bcba4 | 121 | result = NULL; |
Azure.IoT Build | 0:6ae2f7bca550 | 122 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 123 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 124 | { |
AzureIoTClient | 23:1111ee8bcba4 | 125 | /* 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`.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 126 | /* Codes_SRS_SASL_MECHANISM_01_015: [On success, `saslmechanism_get_mechanism_name` shall return a pointer to a string with the mechanism name.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 127 | /* Codes_SRS_SASL_MECHANISM_01_017: [If the underlying `concrete_sasl_mechanism_get_mechanism_name` fails, `saslmechanism_get_mechanism_name` shall return NULL.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 128 | result = sasl_mechanism->sasl_mechanism_interface_description->concrete_sasl_mechanism_get_mechanism_name(sasl_mechanism->concrete_sasl_mechanism_handle); |
AzureIoTClient | 23:1111ee8bcba4 | 129 | if (result == NULL) |
AzureIoTClient | 23:1111ee8bcba4 | 130 | { |
AzureIoTClient | 23:1111ee8bcba4 | 131 | LogError("concrete_sasl_mechanism_get_mechanism_name failed"); |
AzureIoTClient | 23:1111ee8bcba4 | 132 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 133 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 134 | |
Azure.IoT Build | 0:6ae2f7bca550 | 135 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 136 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 137 | |
Azure.IoT Build | 0:6ae2f7bca550 | 138 | int saslmechanism_challenge(SASL_MECHANISM_HANDLE sasl_mechanism, const SASL_MECHANISM_BYTES* challenge_bytes, SASL_MECHANISM_BYTES* response_bytes) |
Azure.IoT Build | 0:6ae2f7bca550 | 139 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 140 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 141 | |
AzureIoTClient | 23:1111ee8bcba4 | 142 | /* 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 | 143 | if (sasl_mechanism == NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 144 | { |
AzureIoTClient | 23:1111ee8bcba4 | 145 | LogError("NULL sasl_mechanism"); |
AzureIoTClient | 23:1111ee8bcba4 | 146 | result = __FAILURE__; |
Azure.IoT Build | 0:6ae2f7bca550 | 147 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 148 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 149 | { |
AzureIoTClient | 23:1111ee8bcba4 | 150 | /* 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 | 151 | 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 | 152 | { |
AzureIoTClient | 23:1111ee8bcba4 | 153 | /* 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 | 23:1111ee8bcba4 | 154 | LogError("concrete_sasl_mechanism_challenge failed"); |
AzureIoTClient | 23:1111ee8bcba4 | 155 | result = __FAILURE__; |
Azure.IoT Build | 0:6ae2f7bca550 | 156 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 157 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 158 | { |
AzureIoTClient | 23:1111ee8bcba4 | 159 | /* Codes_SRS_SASL_MECHANISM_01_019: [On success, `saslmechanism_challenge` shall return 0.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 160 | result = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 161 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 162 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 163 | |
Azure.IoT Build | 0:6ae2f7bca550 | 164 | return result; |
AzureIoTClient | 6:641a9672db08 | 165 | } |