A small memory footprint AMQP implimentation
Dependents: iothub_client_sample_amqp remote_monitoring simplesample_amqp
sasl_mechanism.c@17:923575db8b2d, 2017-01-24 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Tue Jan 24 15:23:52 2017 -0800
- Revision:
- 17:923575db8b2d
- Parent:
- 6:641a9672db08
- Child:
- 19:000ab4e6a2c1
1.1.5
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 "azure_uamqp_c/sasl_mechanism.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 6 | #include "azure_uamqp_c/amqpalloc.h" |
Azure.IoT Build | 0:6ae2f7bca550 | 7 | |
Azure.IoT Build | 0:6ae2f7bca550 | 8 | typedef struct SASL_MECHANISM_INSTANCE_TAG |
Azure.IoT Build | 0:6ae2f7bca550 | 9 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 10 | const SASL_MECHANISM_INTERFACE_DESCRIPTION* sasl_mechanism_interface_description; |
Azure.IoT Build | 0:6ae2f7bca550 | 11 | CONCRETE_SASL_MECHANISM_HANDLE concrete_sasl_mechanism_handle; |
Azure.IoT Build | 0:6ae2f7bca550 | 12 | } SASL_MECHANISM_INSTANCE; |
Azure.IoT Build | 0:6ae2f7bca550 | 13 | |
Azure.IoT Build | 0:6ae2f7bca550 | 14 | SASL_MECHANISM_HANDLE saslmechanism_create(const SASL_MECHANISM_INTERFACE_DESCRIPTION* sasl_mechanism_interface_description, void* sasl_mechanism_create_parameters) |
Azure.IoT Build | 0:6ae2f7bca550 | 15 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 16 | SASL_MECHANISM_INSTANCE* sasl_mechanism_instance; |
Azure.IoT Build | 0:6ae2f7bca550 | 17 | |
Azure.IoT Build | 0:6ae2f7bca550 | 18 | /* 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 | 19 | if ((sasl_mechanism_interface_description == NULL) || |
Azure.IoT Build | 0:6ae2f7bca550 | 20 | /* 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 | 21 | (sasl_mechanism_interface_description->concrete_sasl_mechanism_create == NULL) || |
Azure.IoT Build | 0:6ae2f7bca550 | 22 | (sasl_mechanism_interface_description->concrete_sasl_mechanism_destroy == NULL) || |
Azure.IoT Build | 0:6ae2f7bca550 | 23 | (sasl_mechanism_interface_description->concrete_sasl_mechanism_get_init_bytes == NULL) || |
Azure.IoT Build | 0:6ae2f7bca550 | 24 | (sasl_mechanism_interface_description->concrete_sasl_mechanism_get_mechanism_name == NULL)) |
Azure.IoT Build | 0:6ae2f7bca550 | 25 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 26 | sasl_mechanism_instance = NULL; |
Azure.IoT Build | 0:6ae2f7bca550 | 27 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 28 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 29 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 30 | sasl_mechanism_instance = (SASL_MECHANISM_INSTANCE*)amqpalloc_malloc(sizeof(SASL_MECHANISM_INSTANCE)); |
Azure.IoT Build | 0:6ae2f7bca550 | 31 | |
Azure.IoT Build | 0:6ae2f7bca550 | 32 | if (sasl_mechanism_instance != NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 33 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 34 | sasl_mechanism_instance->sasl_mechanism_interface_description = sasl_mechanism_interface_description; |
Azure.IoT Build | 0:6ae2f7bca550 | 35 | |
Azure.IoT Build | 0:6ae2f7bca550 | 36 | /* 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 | 37 | 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 | 38 | if (sasl_mechanism_instance->concrete_sasl_mechanism_handle == NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 39 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 40 | /* Codes_SRS_SASL_MECHANISM_01_003: [If the underlying concrete_sasl_mechanism_create call fails, saslmechanism_create shall return NULL.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 41 | amqpalloc_free(sasl_mechanism_instance); |
Azure.IoT Build | 0:6ae2f7bca550 | 42 | sasl_mechanism_instance = NULL; |
Azure.IoT Build | 0:6ae2f7bca550 | 43 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 44 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 45 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 46 | |
Azure.IoT Build | 0:6ae2f7bca550 | 47 | /* 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 | 48 | return (SASL_MECHANISM_HANDLE)sasl_mechanism_instance; |
Azure.IoT Build | 0:6ae2f7bca550 | 49 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 50 | |
Azure.IoT Build | 0:6ae2f7bca550 | 51 | void saslmechanism_destroy(SASL_MECHANISM_HANDLE sasl_mechanism) |
Azure.IoT Build | 0:6ae2f7bca550 | 52 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 53 | if (sasl_mechanism != NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 54 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 55 | SASL_MECHANISM_INSTANCE* sasl_mechanism_instance = (SASL_MECHANISM_INSTANCE*)sasl_mechanism; |
Azure.IoT Build | 0:6ae2f7bca550 | 56 | |
Azure.IoT Build | 0:6ae2f7bca550 | 57 | /* 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 | 58 | sasl_mechanism_instance->sasl_mechanism_interface_description->concrete_sasl_mechanism_destroy(sasl_mechanism_instance->concrete_sasl_mechanism_handle); |
Azure.IoT Build | 0:6ae2f7bca550 | 59 | |
Azure.IoT Build | 0:6ae2f7bca550 | 60 | /* Codes_SRS_SASL_MECHANISM_01_007: [saslmechanism_destroy shall free all resources associated with the SASL mechanism handle.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 61 | amqpalloc_free(sasl_mechanism_instance); |
Azure.IoT Build | 0:6ae2f7bca550 | 62 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 63 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 64 | |
Azure.IoT Build | 0:6ae2f7bca550 | 65 | int saslmechanism_get_init_bytes(SASL_MECHANISM_HANDLE sasl_mechanism, SASL_MECHANISM_BYTES* init_bytes) |
Azure.IoT Build | 0:6ae2f7bca550 | 66 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 67 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 68 | |
Azure.IoT Build | 0:6ae2f7bca550 | 69 | /* 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 | 70 | if (sasl_mechanism == NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 71 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 72 | result = __LINE__; |
Azure.IoT Build | 0:6ae2f7bca550 | 73 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 74 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 75 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 76 | SASL_MECHANISM_INSTANCE* sasl_mechanism_instance = (SASL_MECHANISM_INSTANCE*)sasl_mechanism; |
Azure.IoT Build | 0:6ae2f7bca550 | 77 | |
Azure.IoT Build | 0:6ae2f7bca550 | 78 | /* 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 | 79 | 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 | 80 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 81 | /* 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.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 82 | result = __LINE__; |
Azure.IoT Build | 0:6ae2f7bca550 | 83 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 84 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 85 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 86 | /* Codes_SRS_SASL_MECHANISM_01_011: [On success, saslmechanism_get_init_bytes shall return 0.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 87 | result = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 88 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 89 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 90 | |
Azure.IoT Build | 0:6ae2f7bca550 | 91 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 92 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 93 | |
Azure.IoT Build | 0:6ae2f7bca550 | 94 | const char* saslmechanism_get_mechanism_name(SASL_MECHANISM_HANDLE sasl_mechanism) |
Azure.IoT Build | 0:6ae2f7bca550 | 95 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 96 | const char* result; |
Azure.IoT Build | 0:6ae2f7bca550 | 97 | |
Azure.IoT Build | 0:6ae2f7bca550 | 98 | /* 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 | 99 | if (sasl_mechanism == NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 100 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 101 | result = NULL; |
Azure.IoT Build | 0:6ae2f7bca550 | 102 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 103 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 104 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 105 | SASL_MECHANISM_INSTANCE* sasl_mechanism_instance = (SASL_MECHANISM_INSTANCE*)sasl_mechanism; |
Azure.IoT Build | 0:6ae2f7bca550 | 106 | |
Azure.IoT Build | 0:6ae2f7bca550 | 107 | /* 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 | 108 | /* 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 | 109 | /* 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 | 110 | 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 | 111 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 112 | |
Azure.IoT Build | 0:6ae2f7bca550 | 113 | return result; |
Azure.IoT Build | 0:6ae2f7bca550 | 114 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 115 | |
Azure.IoT Build | 0:6ae2f7bca550 | 116 | int saslmechanism_challenge(SASL_MECHANISM_HANDLE sasl_mechanism, const SASL_MECHANISM_BYTES* challenge_bytes, SASL_MECHANISM_BYTES* response_bytes) |
Azure.IoT Build | 0:6ae2f7bca550 | 117 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 118 | int result; |
Azure.IoT Build | 0:6ae2f7bca550 | 119 | |
Azure.IoT Build | 0:6ae2f7bca550 | 120 | /* 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 | 121 | if (sasl_mechanism == NULL) |
Azure.IoT Build | 0:6ae2f7bca550 | 122 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 123 | result = __LINE__; |
Azure.IoT Build | 0:6ae2f7bca550 | 124 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 125 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 126 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 127 | /* 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 | 128 | 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 | 129 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 130 | /* Codes_SRS_SASL_MECHANISM_01_021: [If the underlying concrete_sasl_mechanism_challenge fails, saslmechanism_challenge shall fail and return a non-zero value.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 131 | result = __LINE__; |
Azure.IoT Build | 0:6ae2f7bca550 | 132 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 133 | else |
Azure.IoT Build | 0:6ae2f7bca550 | 134 | { |
Azure.IoT Build | 0:6ae2f7bca550 | 135 | /* Codes_SRS_SASL_MECHANISM_01_019: [On success, saslmechanism_challenge shall return 0.] */ |
Azure.IoT Build | 0:6ae2f7bca550 | 136 | result = 0; |
Azure.IoT Build | 0:6ae2f7bca550 | 137 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 138 | } |
Azure.IoT Build | 0:6ae2f7bca550 | 139 | |
Azure.IoT Build | 0:6ae2f7bca550 | 140 | return result; |
AzureIoTClient | 6:641a9672db08 | 141 | } |