A small memory footprint AMQP implimentation
Dependents: iothub_client_sample_amqp remote_monitoring simplesample_amqp
sasl_mechanism.c@28:add19eb7defa, 2017-06-30 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Fri Jun 30 10:41:22 2017 -0700
- Revision:
- 28:add19eb7defa
- Parent:
- 23:1111ee8bcba4
- Child:
- 31:cafc87baef79
1.1.18
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 | { |
AzureIoTClient | 28:add19eb7defa | 12 | const SASL_MECHANISM_INTERFACE_DESCRIPTION* sasl_mechanism_interface_description; |
AzureIoTClient | 28:add19eb7defa | 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 | 28:add19eb7defa | 18 | SASL_MECHANISM_HANDLE result; |
Azure.IoT Build | 0:6ae2f7bca550 | 19 | |
AzureIoTClient | 28:add19eb7defa | 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 | 28:add19eb7defa | 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) || |
AzureIoTClient | 28:add19eb7defa | 28 | (sasl_mechanism_interface_description->concrete_sasl_mechanism_destroy == NULL) || |
AzureIoTClient | 28:add19eb7defa | 29 | (sasl_mechanism_interface_description->concrete_sasl_mechanism_get_init_bytes == NULL) || |
AzureIoTClient | 28:add19eb7defa | 30 | (sasl_mechanism_interface_description->concrete_sasl_mechanism_get_mechanism_name == NULL)) |
AzureIoTClient | 28:add19eb7defa | 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 | 28:add19eb7defa | 37 | result = NULL; |
AzureIoTClient | 28:add19eb7defa | 38 | } |
AzureIoTClient | 28:add19eb7defa | 39 | else |
AzureIoTClient | 28:add19eb7defa | 40 | { |
AzureIoTClient | 28:add19eb7defa | 41 | result = (SASL_MECHANISM_HANDLE)malloc(sizeof(SASL_MECHANISM_INSTANCE)); |
AzureIoTClient | 23:1111ee8bcba4 | 42 | if (result == NULL) |
AzureIoTClient | 23:1111ee8bcba4 | 43 | { |
AzureIoTClient | 28:add19eb7defa | 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 | 28:add19eb7defa | 45 | LogError("Could not allocate memory for SASL mechanism"); |
AzureIoTClient | 23:1111ee8bcba4 | 46 | } |
AzureIoTClient | 23:1111ee8bcba4 | 47 | else |
AzureIoTClient | 23:1111ee8bcba4 | 48 | { |
AzureIoTClient | 28:add19eb7defa | 49 | result->sasl_mechanism_interface_description = sasl_mechanism_interface_description; |
Azure.IoT Build | 0:6ae2f7bca550 | 50 | |
AzureIoTClient | 28:add19eb7defa | 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 | 28:add19eb7defa | 52 | result->concrete_sasl_mechanism_handle = result->sasl_mechanism_interface_description->concrete_sasl_mechanism_create((void*)sasl_mechanism_create_parameters); |
AzureIoTClient | 28:add19eb7defa | 53 | if (result->concrete_sasl_mechanism_handle == NULL) |
AzureIoTClient | 28:add19eb7defa | 54 | { |
AzureIoTClient | 28:add19eb7defa | 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 | 28:add19eb7defa | 58 | result = NULL; |
AzureIoTClient | 28:add19eb7defa | 59 | } |
AzureIoTClient | 28:add19eb7defa | 60 | } |
AzureIoTClient | 28:add19eb7defa | 61 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 62 | |
AzureIoTClient | 28:add19eb7defa | 63 | /* Codes_SRS_SASL_MECHANISM_01_001: [`saslmechanism_create` shall return on success a non-NULL handle to a new SASL mechanism interface.] */ |
AzureIoTClient | 28:add19eb7defa | 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 | 28:add19eb7defa | 71 | /* Codes_SRS_SASL_MECHANISM_01_009: [If the argument `sasl_mechanism` is NULL, `saslmechanism_destroy` shall do nothing.] */ |
AzureIoTClient | 28:add19eb7defa | 72 | LogError("NULL sasl_mechanism"); |
AzureIoTClient | 23:1111ee8bcba4 | 73 | } |
AzureIoTClient | 23:1111ee8bcba4 | 74 | else |
AzureIoTClient | 23:1111ee8bcba4 | 75 | { |
AzureIoTClient | 28:add19eb7defa | 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 | 28:add19eb7defa | 77 | sasl_mechanism->sasl_mechanism_interface_description->concrete_sasl_mechanism_destroy(sasl_mechanism->concrete_sasl_mechanism_handle); |
Azure.IoT Build | 0:6ae2f7bca550 | 78 | |
AzureIoTClient | 28:add19eb7defa | 79 | /* Codes_SRS_SASL_MECHANISM_01_007: [`saslmechanism_destroy` shall free all resources associated with the SASL mechanism handle.] */ |
AzureIoTClient | 28:add19eb7defa | 80 | free(sasl_mechanism); |
AzureIoTClient | 28:add19eb7defa | 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 | { |
AzureIoTClient | 28:add19eb7defa | 86 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 87 | |
AzureIoTClient | 28:add19eb7defa | 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.] */ |
AzureIoTClient | 28:add19eb7defa | 89 | if (sasl_mechanism == NULL) |
AzureIoTClient | 28:add19eb7defa | 90 | { |
AzureIoTClient | 23:1111ee8bcba4 | 91 | LogError("NULL sasl_mechanism"); |
AzureIoTClient | 23:1111ee8bcba4 | 92 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 93 | } |
AzureIoTClient | 28:add19eb7defa | 94 | else |
AzureIoTClient | 28:add19eb7defa | 95 | { |
AzureIoTClient | 28:add19eb7defa | 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 | 28:add19eb7defa | 97 | if (sasl_mechanism->sasl_mechanism_interface_description->concrete_sasl_mechanism_get_init_bytes(sasl_mechanism->concrete_sasl_mechanism_handle, init_bytes) != 0) |
AzureIoTClient | 28:add19eb7defa | 98 | { |
AzureIoTClient | 28:add19eb7defa | 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__; |
AzureIoTClient | 28:add19eb7defa | 102 | } |
AzureIoTClient | 28:add19eb7defa | 103 | else |
AzureIoTClient | 28:add19eb7defa | 104 | { |
AzureIoTClient | 28:add19eb7defa | 105 | /* Codes_SRS_SASL_MECHANISM_01_011: [On success, `saslmechanism_get_init_bytes` shall return 0.] */ |
AzureIoTClient | 28:add19eb7defa | 106 | result = 0; |
AzureIoTClient | 28:add19eb7defa | 107 | } |
AzureIoTClient | 28:add19eb7defa | 108 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 109 | |
AzureIoTClient | 28:add19eb7defa | 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 | { |
AzureIoTClient | 28:add19eb7defa | 115 | const char* result; |
Azure.IoT Build | 0:6ae2f7bca550 | 116 | |
AzureIoTClient | 28:add19eb7defa | 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.] */ |
AzureIoTClient | 28:add19eb7defa | 118 | if (sasl_mechanism == NULL) |
AzureIoTClient | 28:add19eb7defa | 119 | { |
AzureIoTClient | 23:1111ee8bcba4 | 120 | LogError("NULL sasl_mechanism"); |
AzureIoTClient | 23:1111ee8bcba4 | 121 | result = NULL; |
AzureIoTClient | 28:add19eb7defa | 122 | } |
AzureIoTClient | 28:add19eb7defa | 123 | else |
AzureIoTClient | 28:add19eb7defa | 124 | { |
AzureIoTClient | 28:add19eb7defa | 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 | 28:add19eb7defa | 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 | 28:add19eb7defa | 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 | 28:add19eb7defa | 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 | } |
AzureIoTClient | 28:add19eb7defa | 133 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 134 | |
AzureIoTClient | 28:add19eb7defa | 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 | { |
AzureIoTClient | 28:add19eb7defa | 140 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 141 | |
AzureIoTClient | 28:add19eb7defa | 142 | /* Codes_SRS_SASL_MECHANISM_01_020: [If the argument `sasl_mechanism` is NULL, `saslmechanism_challenge` shall fail and return a non-zero value.] */ |
AzureIoTClient | 28:add19eb7defa | 143 | if (sasl_mechanism == NULL) |
AzureIoTClient | 28:add19eb7defa | 144 | { |
AzureIoTClient | 23:1111ee8bcba4 | 145 | LogError("NULL sasl_mechanism"); |
AzureIoTClient | 23:1111ee8bcba4 | 146 | result = __FAILURE__; |
AzureIoTClient | 28:add19eb7defa | 147 | } |
AzureIoTClient | 28:add19eb7defa | 148 | else |
AzureIoTClient | 28:add19eb7defa | 149 | { |
AzureIoTClient | 28:add19eb7defa | 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.] */ |
AzureIoTClient | 28:add19eb7defa | 151 | if (sasl_mechanism->sasl_mechanism_interface_description->concrete_sasl_mechanism_challenge(sasl_mechanism->concrete_sasl_mechanism_handle, challenge_bytes, response_bytes) != 0) |
AzureIoTClient | 28:add19eb7defa | 152 | { |
AzureIoTClient | 28:add19eb7defa | 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__; |
AzureIoTClient | 28:add19eb7defa | 156 | } |
AzureIoTClient | 28:add19eb7defa | 157 | else |
AzureIoTClient | 28:add19eb7defa | 158 | { |
AzureIoTClient | 28:add19eb7defa | 159 | /* Codes_SRS_SASL_MECHANISM_01_019: [On success, `saslmechanism_challenge` shall return 0.] */ |
AzureIoTClient | 28:add19eb7defa | 160 | result = 0; |
AzureIoTClient | 28:add19eb7defa | 161 | } |
AzureIoTClient | 28:add19eb7defa | 162 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 163 | |
AzureIoTClient | 28:add19eb7defa | 164 | return result; |
AzureIoTClient | 6:641a9672db08 | 165 | } |