A small memory footprint AMQP implimentation
Dependents: iothub_client_sample_amqp remote_monitoring simplesample_amqp
sasl_anonymous.c@27:d74f1cea23e1, 2017-06-02 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Fri Jun 02 15:53:07 2017 -0700
- Revision:
- 27:d74f1cea23e1
- Parent:
- 23:1111ee8bcba4
- Child:
- 28:add19eb7defa
1.1.16
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> |
Azure.IoT Build | 0:6ae2f7bca550 | 5 | #include <string.h> |
AzureIoTClient | 20:206846c14c80 | 6 | #include "azure_c_shared_utility/optimize_size.h" |
AzureIoTClient | 20:206846c14c80 | 7 | #include "azure_c_shared_utility/gballoc.h" |
AzureIoTClient | 27:d74f1cea23e1 | 8 | #include "azure_c_shared_utility/xlogging.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 9 | #include "azure_uamqp_c/sasl_anonymous.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 10 | |
Azure.IoT Build | 0:6ae2f7bca550 | 11 | typedef struct SASL_ANONYMOUS_INSTANCE_TAG |
Azure.IoT Build | 0:6ae2f7bca550 | 12 | { |
AzureIoTClient | 6:641a9672db08 | 13 | unsigned char dummy; |
Azure.IoT Build | 0:6ae2f7bca550 | 14 | } SASL_ANONYMOUS_INSTANCE; |
Azure.IoT Build | 0:6ae2f7bca550 | 15 | |
AzureIoTClient | 23:1111ee8bcba4 | 16 | /* Codes_SRS_SASL_ANONYMOUS_01_001: [`saslanonymous_create` shall return on success a non-NULL handle to a new SASL anonymous mechanism.]*/ |
AzureIoTClient | 23:1111ee8bcba4 | 17 | static CONCRETE_SASL_MECHANISM_HANDLE saslanonymous_create(void* config) |
Azure.IoT Build | 0:6ae2f7bca550 | 18 | { |
AzureIoTClient | 23:1111ee8bcba4 | 19 | CONCRETE_SASL_MECHANISM_HANDLE result; |
Azure.IoT Build | 0:6ae2f7bca550 | 20 | |
AzureIoTClient | 23:1111ee8bcba4 | 21 | /* Codes_SRS_SASL_ANONYMOUS_01_003: [Since this is the ANONYMOUS SASL mechanism, `config` shall be ignored.]*/ |
Azure.IoT Build | 0:6ae2f7bca550 | 22 | (void)config; |
Azure.IoT Build | 0:6ae2f7bca550 | 23 | |
AzureIoTClient | 23:1111ee8bcba4 | 24 | result = malloc(sizeof(SASL_ANONYMOUS_INSTANCE)); |
AzureIoTClient | 23:1111ee8bcba4 | 25 | if (result == NULL) |
AzureIoTClient | 23:1111ee8bcba4 | 26 | { |
AzureIoTClient | 23:1111ee8bcba4 | 27 | /* Codes_SRS_SASL_ANONYMOUS_01_002: [If allocating the memory needed for the SASL anonymous instance fails then `saslanonymous_create` shall return NULL.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 28 | LogError("Cannot allocate memory for SASL anonymous instance"); |
AzureIoTClient | 23:1111ee8bcba4 | 29 | } |
AzureIoTClient | 23:1111ee8bcba4 | 30 | |
AzureIoTClient | 23:1111ee8bcba4 | 31 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 32 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 33 | |
AzureIoTClient | 23:1111ee8bcba4 | 34 | static void saslanonymous_destroy(CONCRETE_SASL_MECHANISM_HANDLE sasl_mechanism_concrete_handle) |
Azure.IoT Build | 0:6ae2f7bca550 | 35 | { |
AzureIoTClient | 23:1111ee8bcba4 | 36 | /* Codes_SRS_SASL_ANONYMOUS_01_005: [If the argument `concrete_sasl_mechanism` is NULL, `saslanonymous_destroy` shall do nothing.]*/ |
AzureIoTClient | 23:1111ee8bcba4 | 37 | if (sasl_mechanism_concrete_handle == NULL) |
AzureIoTClient | 23:1111ee8bcba4 | 38 | { |
AzureIoTClient | 23:1111ee8bcba4 | 39 | LogError("NULL sasl_mechanism_concrete_handle"); |
AzureIoTClient | 23:1111ee8bcba4 | 40 | } |
AzureIoTClient | 23:1111ee8bcba4 | 41 | else |
AzureIoTClient | 23:1111ee8bcba4 | 42 | { |
AzureIoTClient | 23:1111ee8bcba4 | 43 | /* Codes_SRS_SASL_ANONYMOUS_01_004: [`saslanonymous_destroy` shall free all resources associated with the SASL mechanism.] */ |
AzureIoTClient | 20:206846c14c80 | 44 | free(sasl_mechanism_concrete_handle); |
Azure.IoT Build | 0:6ae2f7bca550 | 45 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 46 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 47 | |
AzureIoTClient | 23:1111ee8bcba4 | 48 | static int saslanonymous_get_init_bytes(CONCRETE_SASL_MECHANISM_HANDLE sasl_mechanism_concrete_handle, SASL_MECHANISM_BYTES* init_bytes) |
Azure.IoT Build | 0:6ae2f7bca550 | 49 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 50 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 51 | |
AzureIoTClient | 23:1111ee8bcba4 | 52 | /* Codes_SRS_SASL_ANONYMOUS_01_007: [If any argument is NULL, `saslanonymous_get_init_bytes` shall return a non-zero value.]*/ |
Azure.IoT Build | 0:6ae2f7bca550 | 53 | if ((sasl_mechanism_concrete_handle == NULL) || |
Azure.IoT Build | 0:6ae2f7bca550 | 54 | (init_bytes == NULL)) |
Azure.IoT Build | 0:6ae2f7bca550 | 55 | { |
AzureIoTClient | 23:1111ee8bcba4 | 56 | LogError("Bad arguments: sasl_mechanism_concrete_handle = %p, init_bytes = %p", |
AzureIoTClient | 23:1111ee8bcba4 | 57 | sasl_mechanism_concrete_handle, init_bytes); |
AzureIoTClient | 23:1111ee8bcba4 | 58 | result = __FAILURE__; |
Azure.IoT Build | 0:6ae2f7bca550 | 59 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 60 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 61 | { |
AzureIoTClient | 23:1111ee8bcba4 | 62 | /* Codes_SRS_SASL_ANONYMOUS_01_012: [The bytes field of `init_buffer` shall be set to NULL.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 63 | init_bytes->bytes = NULL; |
AzureIoTClient | 23:1111ee8bcba4 | 64 | /* Codes_SRS_SASL_ANONYMOUS_01_006: [`saslanonymous_get_init_bytes` shall validate the `concrete_sasl_mechanism` argument and set the length of the `init_bytes` argument to be zero.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 65 | init_bytes->length = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 66 | |
AzureIoTClient | 23:1111ee8bcba4 | 67 | /* Codes_SRS_SASL_ANONYMOUS_01_011: [On success `saslanonymous_get_init_bytes` shall return zero.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 68 | result = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 69 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 70 | |
Azure.IoT Build | 0:6ae2f7bca550 | 71 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 72 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 73 | |
AzureIoTClient | 23:1111ee8bcba4 | 74 | static const char* saslanonymous_get_mechanism_name(CONCRETE_SASL_MECHANISM_HANDLE sasl_mechanism) |
Azure.IoT Build | 0:6ae2f7bca550 | 75 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 76 | const char* result; |
Azure.IoT Build | 0:6ae2f7bca550 | 77 | |
AzureIoTClient | 23:1111ee8bcba4 | 78 | /* Codes_SRS_SASL_ANONYMOUS_01_009: [If the argument `concrete_sasl_mechanism` is NULL, `saslanonymous_get_mechanism_name` shall return NULL.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 79 | if (sasl_mechanism == NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 80 | { |
AzureIoTClient | 23:1111ee8bcba4 | 81 | LogError("NULL sasl_mechanism"); |
AzureIoTClient | 23:1111ee8bcba4 | 82 | result = NULL; |
Azure.IoT Build | 0:6ae2f7bca550 | 83 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 84 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 85 | { |
AzureIoTClient | 23:1111ee8bcba4 | 86 | /* Codes_SRS_SASL_ANONYMOUS_01_008: [`saslanonymous_get_mechanism_name` shall validate the argument `concrete_sasl_mechanism` and on success it shall return a pointer to the string `ANONYMOUS`.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 87 | result = "ANONYMOUS"; |
Azure.IoT Build | 0:6ae2f7bca550 | 88 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 89 | |
Azure.IoT Build | 0:6ae2f7bca550 | 90 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 91 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 92 | |
AzureIoTClient | 23:1111ee8bcba4 | 93 | static int saslanonymous_challenge(CONCRETE_SASL_MECHANISM_HANDLE concrete_sasl_mechanism, const SASL_MECHANISM_BYTES* challenge_bytes, SASL_MECHANISM_BYTES* response_bytes) |
Azure.IoT Build | 0:6ae2f7bca550 | 94 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 95 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 96 | |
Azure.IoT Build | 0:6ae2f7bca550 | 97 | (void)challenge_bytes; |
Azure.IoT Build | 0:6ae2f7bca550 | 98 | |
AzureIoTClient | 23:1111ee8bcba4 | 99 | /* Codes_SRS_SASL_ANONYMOUS_01_015: [If the `concrete_sasl_mechanism` or `response_bytes` argument is NULL then `saslanonymous_challenge` shall fail and return a non-zero value.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 100 | if ((concrete_sasl_mechanism == NULL) || |
Azure.IoT Build | 0:6ae2f7bca550 | 101 | (response_bytes == NULL)) |
Azure.IoT Build | 0:6ae2f7bca550 | 102 | { |
AzureIoTClient | 23:1111ee8bcba4 | 103 | LogError("Bad arguments: concrete_sasl_mechanism = %p, response_bytes = %p", |
AzureIoTClient | 23:1111ee8bcba4 | 104 | concrete_sasl_mechanism, response_bytes); |
AzureIoTClient | 23:1111ee8bcba4 | 105 | result = __FAILURE__; |
Azure.IoT Build | 0:6ae2f7bca550 | 106 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 107 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 108 | { |
AzureIoTClient | 23:1111ee8bcba4 | 109 | /* Codes_SRS_SASL_ANONYMOUS_01_013: [`saslanonymous_challenge` shall set the `buffer` field to NULL and `size` to 0 in the `response_bytes` argument as the ANONYMOUS SASL mechanism does not implement challenge/response.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 110 | response_bytes->bytes = NULL; |
Azure.IoT Build | 0:6ae2f7bca550 | 111 | response_bytes->length = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 112 | |
AzureIoTClient | 23:1111ee8bcba4 | 113 | /* Codes_SRS_SASL_ANONYMOUS_01_014: [On success, `saslanonymous_challenge` shall return 0.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 114 | result = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 115 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 116 | |
Azure.IoT Build | 0:6ae2f7bca550 | 117 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 118 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 119 | |
AzureIoTClient | 23:1111ee8bcba4 | 120 | /* Codes_SRS_SASL_ANONYMOUS_01_010: [`saslanonymous_get_interface` shall return a pointer to a `SASL_MECHANISM_INTERFACE_DESCRIPTION` structure that contains pointers to the functions: `saslanonymous_create`, `saslanonymous_destroy`, `saslanonymous_get_init_bytes`, `saslanonymous_get_mechanism_name`, `saslanonymous_challenge`.] */ |
AzureIoTClient | 23:1111ee8bcba4 | 121 | static const SASL_MECHANISM_INTERFACE_DESCRIPTION saslanonymous_interface = |
AzureIoTClient | 23:1111ee8bcba4 | 122 | { |
AzureIoTClient | 23:1111ee8bcba4 | 123 | saslanonymous_create, |
AzureIoTClient | 23:1111ee8bcba4 | 124 | saslanonymous_destroy, |
AzureIoTClient | 23:1111ee8bcba4 | 125 | saslanonymous_get_init_bytes, |
AzureIoTClient | 23:1111ee8bcba4 | 126 | saslanonymous_get_mechanism_name, |
AzureIoTClient | 23:1111ee8bcba4 | 127 | saslanonymous_challenge |
AzureIoTClient | 23:1111ee8bcba4 | 128 | }; |
AzureIoTClient | 23:1111ee8bcba4 | 129 | |
Azure.IoT Build | 0:6ae2f7bca550 | 130 | const SASL_MECHANISM_INTERFACE_DESCRIPTION* saslanonymous_get_interface(void) |
Azure.IoT Build | 0:6ae2f7bca550 | 131 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 132 | return &saslanonymous_interface; |
Azure.IoT Build | 0:6ae2f7bca550 | 133 | } |