Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
uamqp/src/sasl_server_mechanism.c@0:f7f1f0d76dd6, 2018-08-23 (annotated)
- Committer:
- XinZhangMS
- Date:
- Thu Aug 23 06:52:14 2018 +0000
- Revision:
- 0:f7f1f0d76dd6
azure-c-sdk for mbed os supporting NUCLEO_F767ZI
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
XinZhangMS | 0:f7f1f0d76dd6 | 1 | // Copyright (c) Microsoft. All rights reserved. |
XinZhangMS | 0:f7f1f0d76dd6 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
XinZhangMS | 0:f7f1f0d76dd6 | 3 | |
XinZhangMS | 0:f7f1f0d76dd6 | 4 | #include <stdlib.h> |
XinZhangMS | 0:f7f1f0d76dd6 | 5 | #include "azure_c_shared_utility/optimize_size.h" |
XinZhangMS | 0:f7f1f0d76dd6 | 6 | #include "azure_c_shared_utility/gballoc.h" |
XinZhangMS | 0:f7f1f0d76dd6 | 7 | #include "azure_c_shared_utility/xlogging.h" |
XinZhangMS | 0:f7f1f0d76dd6 | 8 | #include "azure_uamqp_c/sasl_server_mechanism.h" |
XinZhangMS | 0:f7f1f0d76dd6 | 9 | |
XinZhangMS | 0:f7f1f0d76dd6 | 10 | typedef struct SASL_SERVER_MECHANISM_INSTANCE_TAG |
XinZhangMS | 0:f7f1f0d76dd6 | 11 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 12 | const SASL_SERVER_MECHANISM_INTERFACE_DESCRIPTION* sasl_server_mechanism_interface_description; |
XinZhangMS | 0:f7f1f0d76dd6 | 13 | CONCRETE_SASL_SERVER_MECHANISM_HANDLE concrete_sasl_server_mechanism; |
XinZhangMS | 0:f7f1f0d76dd6 | 14 | } SASL_SERVER_MECHANISM_INSTANCE; |
XinZhangMS | 0:f7f1f0d76dd6 | 15 | |
XinZhangMS | 0:f7f1f0d76dd6 | 16 | SASL_SERVER_MECHANISM_HANDLE sasl_server_mechanism_create(const SASL_SERVER_MECHANISM_INTERFACE_DESCRIPTION* sasl_server_mechanism_interface_description, void* sasl_server_mechanism_create_parameters) |
XinZhangMS | 0:f7f1f0d76dd6 | 17 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 18 | SASL_SERVER_MECHANISM_HANDLE result; |
XinZhangMS | 0:f7f1f0d76dd6 | 19 | |
XinZhangMS | 0:f7f1f0d76dd6 | 20 | if (sasl_server_mechanism_interface_description == NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 21 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 22 | /* Codes_SRS_SASL_SERVER_MECHANISM_01_004: [ If the argument `sasl_server_mechanism_interface_description` is NULL, `sasl_server_mechanism_create` shall return NULL.]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 23 | LogError("NULL sasl_server_mechanism_interface_description"); |
XinZhangMS | 0:f7f1f0d76dd6 | 24 | result = NULL; |
XinZhangMS | 0:f7f1f0d76dd6 | 25 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 26 | else if ((sasl_server_mechanism_interface_description->create == NULL) || |
XinZhangMS | 0:f7f1f0d76dd6 | 27 | (sasl_server_mechanism_interface_description->destroy == NULL) || |
XinZhangMS | 0:f7f1f0d76dd6 | 28 | (sasl_server_mechanism_interface_description->handle_initial_response == NULL) || |
XinZhangMS | 0:f7f1f0d76dd6 | 29 | (sasl_server_mechanism_interface_description->handle_response == NULL) || |
XinZhangMS | 0:f7f1f0d76dd6 | 30 | (sasl_server_mechanism_interface_description->get_mechanism_name == NULL)) |
XinZhangMS | 0:f7f1f0d76dd6 | 31 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 32 | /* Codes_SRS_SASL_SERVER_MECHANISM_01_005: [ If any `sasl_server_mechanism_interface_description` member is NULL, `sasl_server_mechanism_create` shall fail and return NULL.]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 33 | LogError("Bad interface, create = %p, destroy = %p, handle_initial_response = %p, handle_response = %p, get_mechanism_name = %p", |
XinZhangMS | 0:f7f1f0d76dd6 | 34 | sasl_server_mechanism_interface_description->create, |
XinZhangMS | 0:f7f1f0d76dd6 | 35 | sasl_server_mechanism_interface_description->destroy, |
XinZhangMS | 0:f7f1f0d76dd6 | 36 | sasl_server_mechanism_interface_description->handle_initial_response, |
XinZhangMS | 0:f7f1f0d76dd6 | 37 | sasl_server_mechanism_interface_description->handle_response, |
XinZhangMS | 0:f7f1f0d76dd6 | 38 | sasl_server_mechanism_interface_description->get_mechanism_name); |
XinZhangMS | 0:f7f1f0d76dd6 | 39 | result = NULL; |
XinZhangMS | 0:f7f1f0d76dd6 | 40 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 41 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 42 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 43 | /* Codes_SRS_SASL_SERVER_MECHANISM_01_001: [`sasl_server_mechanism_create` shall return on success a non-NULL handle to a new SASL server mechanism interface.]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 44 | result = (SASL_SERVER_MECHANISM_HANDLE)malloc(sizeof(SASL_SERVER_MECHANISM_INSTANCE)); |
XinZhangMS | 0:f7f1f0d76dd6 | 45 | if (result == NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 46 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 47 | /* Codes_SRS_SASL_SERVER_MECHANISM_01_006: [ If allocating the memory needed for the SASL server mechanism interface fails then `sasl_server_mechanism_create` shall fail and return NULL. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 48 | LogError("Could not allocate memory for SASL mechanism"); |
XinZhangMS | 0:f7f1f0d76dd6 | 49 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 50 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 51 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 52 | result->sasl_server_mechanism_interface_description = sasl_server_mechanism_interface_description; |
XinZhangMS | 0:f7f1f0d76dd6 | 53 | |
XinZhangMS | 0:f7f1f0d76dd6 | 54 | /* Codes_SRS_SASL_SERVER_MECHANISM_01_002: [ In order to instantiate the concrete SASL server mechanism implementation the function `create` from the `sasl_server_mechanism_interface_description` shall be called, passing the `sasl_server_mechanism_create_parameters` to it.]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 55 | result->concrete_sasl_server_mechanism = result->sasl_server_mechanism_interface_description->create(sasl_server_mechanism_create_parameters); |
XinZhangMS | 0:f7f1f0d76dd6 | 56 | if (result->concrete_sasl_server_mechanism == NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 57 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 58 | /* Codes_SRS_SASL_SERVER_MECHANISM_01_003: [ If the underlying `create` call fails, `sasl_server_mechanism_create` shall return NULL. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 59 | LogError("concrete sasl server mechanism create failed"); |
XinZhangMS | 0:f7f1f0d76dd6 | 60 | free(result); |
XinZhangMS | 0:f7f1f0d76dd6 | 61 | result = NULL; |
XinZhangMS | 0:f7f1f0d76dd6 | 62 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 63 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 64 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 65 | |
XinZhangMS | 0:f7f1f0d76dd6 | 66 | return result; |
XinZhangMS | 0:f7f1f0d76dd6 | 67 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 68 | |
XinZhangMS | 0:f7f1f0d76dd6 | 69 | void sasl_server_mechanism_destroy(SASL_SERVER_MECHANISM_HANDLE sasl_server_mechanism) |
XinZhangMS | 0:f7f1f0d76dd6 | 70 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 71 | if (sasl_server_mechanism == NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 72 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 73 | /* Codes_SRS_SASL_SERVER_MECHANISM_01_009: [ If the argument `sasl_server_mechanism` is NULL, `sasl_server_mechanism_destroy` shall do nothing. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 74 | LogError("NULL sasl_server_mechanism"); |
XinZhangMS | 0:f7f1f0d76dd6 | 75 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 76 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 77 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 78 | /* Codes_SRS_SASL_SERVER_MECHANISM_01_008: [ `sasl_server_mechanism_destroy` shall also call the `destroy` function that is member of the `sasl_mechanism_interface_description` argument passed to `sasl_server_mechanism_create`, while passing as argument to `destroy` the result of the underlying concrete SASL mechanism handle. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 79 | sasl_server_mechanism->sasl_server_mechanism_interface_description->destroy(sasl_server_mechanism->concrete_sasl_server_mechanism); |
XinZhangMS | 0:f7f1f0d76dd6 | 80 | /* Codes_SRS_SASL_SERVER_MECHANISM_01_007: [ `sasl_server_mechanism_destroy` shall free all resources associated with the SASL mechanism handle. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 81 | free(sasl_server_mechanism); |
XinZhangMS | 0:f7f1f0d76dd6 | 82 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 83 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 84 | |
XinZhangMS | 0:f7f1f0d76dd6 | 85 | int sasl_server_mechanism_handle_initial_response(SASL_SERVER_MECHANISM_HANDLE sasl_server_mechanism, const SASL_SERVER_MECHANISM_BYTES* initial_response_bytes, const char* hostname, bool* send_challenge, SASL_SERVER_MECHANISM_BYTES* challenge_bytes) |
XinZhangMS | 0:f7f1f0d76dd6 | 86 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 87 | int result; |
XinZhangMS | 0:f7f1f0d76dd6 | 88 | |
XinZhangMS | 0:f7f1f0d76dd6 | 89 | if (sasl_server_mechanism == NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 90 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 91 | /* Codes_SRS_SASL_SERVER_MECHANISM_01_012: [ If the argument `sasl_server_mechanism` is NULL, `sasl_server_mechanism_handle_initial_response` shall fail and return a non-zero value. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 92 | LogError("NULL sasl_server_mechanism"); |
XinZhangMS | 0:f7f1f0d76dd6 | 93 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 94 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 95 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 96 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 97 | /* Codes_SRS_SASL_SERVER_MECHANISM_01_010: [ `sasl_server_mechanism_handle_initial_response` shall call the specific `handle_initial_response` function specified in `sasl_server_mechanism_create`, passing the `initial_response_bytes`, `hostname`, `send_challenge` and `challenge_bytes` arguments to it. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 98 | if (sasl_server_mechanism->sasl_server_mechanism_interface_description->handle_initial_response(sasl_server_mechanism->concrete_sasl_server_mechanism, initial_response_bytes, hostname, send_challenge, challenge_bytes) != 0) |
XinZhangMS | 0:f7f1f0d76dd6 | 99 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 100 | /* Codes_SRS_SASL_SERVER_MECHANISM_01_013: [ If the underlying `handle_initial_response` fails, `sasl_server_mechanism_handle_initial_response` shall fail and return a non-zero value. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 101 | LogError("handle_initial_response_failed"); |
XinZhangMS | 0:f7f1f0d76dd6 | 102 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 103 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 104 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 105 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 106 | /* Codes_SRS_SASL_SERVER_MECHANISM_01_011: [ On success, `sasl_server_mechanism_handle_initial_response` shall return 0. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 107 | result = 0; |
XinZhangMS | 0:f7f1f0d76dd6 | 108 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 109 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 110 | |
XinZhangMS | 0:f7f1f0d76dd6 | 111 | return result; |
XinZhangMS | 0:f7f1f0d76dd6 | 112 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 113 | |
XinZhangMS | 0:f7f1f0d76dd6 | 114 | int sasl_server_mechanism_handle_response(SASL_SERVER_MECHANISM_HANDLE sasl_server_mechanism, const SASL_SERVER_MECHANISM_BYTES* response_bytes, bool* send_next_challenge, SASL_SERVER_MECHANISM_BYTES* next_challenge_bytes) |
XinZhangMS | 0:f7f1f0d76dd6 | 115 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 116 | int result; |
XinZhangMS | 0:f7f1f0d76dd6 | 117 | |
XinZhangMS | 0:f7f1f0d76dd6 | 118 | if (sasl_server_mechanism == NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 119 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 120 | /* Codes_SRS_SASL_SERVER_MECHANISM_01_017: [ If the argument `sasl_server_mechanism` is NULL, `sasl_server_mechanism_handle_response` shall fail and return a non-zero value. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 121 | LogError("NULL sasl_server_mechanism"); |
XinZhangMS | 0:f7f1f0d76dd6 | 122 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 123 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 124 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 125 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 126 | /* Codes_SRS_SASL_SERVER_MECHANISM_01_014: [ `sasl_server_mechanism_handle_response` shall call the specific `handle_response` function specified in `sasl_server_mechanism_create`, passing the `response_bytes`, `send_next_challenge` and `next_challenge_bytes` arguments to it. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 127 | if (sasl_server_mechanism->sasl_server_mechanism_interface_description->handle_response(sasl_server_mechanism->concrete_sasl_server_mechanism, response_bytes, send_next_challenge, next_challenge_bytes) != 0) |
XinZhangMS | 0:f7f1f0d76dd6 | 128 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 129 | /* Codes_SRS_SASL_SERVER_MECHANISM_01_018: [ If the underlying `handle_response` fails, `sasl_server_mechanism_handle_response` shall fail and return a non-zero value. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 130 | LogError("handle_response_failed"); |
XinZhangMS | 0:f7f1f0d76dd6 | 131 | result = __FAILURE__; |
XinZhangMS | 0:f7f1f0d76dd6 | 132 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 133 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 134 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 135 | /* Codes_SRS_SASL_SERVER_MECHANISM_01_016: [ On success, `sasl_server_mechanism_handle_response` shall return 0. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 136 | result = 0; |
XinZhangMS | 0:f7f1f0d76dd6 | 137 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 138 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 139 | |
XinZhangMS | 0:f7f1f0d76dd6 | 140 | return result; |
XinZhangMS | 0:f7f1f0d76dd6 | 141 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 142 | |
XinZhangMS | 0:f7f1f0d76dd6 | 143 | const char* sasl_server_mechanism_get_mechanism_name(SASL_SERVER_MECHANISM_HANDLE sasl_server_mechanism) |
XinZhangMS | 0:f7f1f0d76dd6 | 144 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 145 | const char* result; |
XinZhangMS | 0:f7f1f0d76dd6 | 146 | |
XinZhangMS | 0:f7f1f0d76dd6 | 147 | if (sasl_server_mechanism == NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 148 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 149 | /* Codes_SRS_SASL_SERVER_MECHANISM_01_021: [ If the argument `sasl_server_mechanism` is NULL, `sasl_server_mechanism_get_mechanism_name` shall fail and return a non-zero value. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 150 | LogError("NULL sasl_server_mechanism"); |
XinZhangMS | 0:f7f1f0d76dd6 | 151 | result = NULL; |
XinZhangMS | 0:f7f1f0d76dd6 | 152 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 153 | else |
XinZhangMS | 0:f7f1f0d76dd6 | 154 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 155 | /* Codes_SRS_SASL_SERVER_MECHANISM_01_019: [ `sasl_server_mechanism_get_mechanism_name` shall call the specific `get_mechanism_name` function specified in `sasl_server_mechanism_create`. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 156 | /* Codes_SRS_SASL_SERVER_MECHANISM_01_020: [ On success, `sasl_server_mechanism_get_mechanism_name` shall return a pointer to a string with the mechanism name. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 157 | result = sasl_server_mechanism->sasl_server_mechanism_interface_description->get_mechanism_name(); |
XinZhangMS | 0:f7f1f0d76dd6 | 158 | if (result == NULL) |
XinZhangMS | 0:f7f1f0d76dd6 | 159 | { |
XinZhangMS | 0:f7f1f0d76dd6 | 160 | /* Codes_SRS_SASL_SERVER_MECHANISM_01_022: [ If the underlying `get_mechanism_name` fails, `sasl_server_mechanism_get_mechanism_name` shall return NULL. ]*/ |
XinZhangMS | 0:f7f1f0d76dd6 | 161 | LogError("concrete_sasl_mechanism_get_mechanism_name failed"); |
XinZhangMS | 0:f7f1f0d76dd6 | 162 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 163 | } |
XinZhangMS | 0:f7f1f0d76dd6 | 164 | |
XinZhangMS | 0:f7f1f0d76dd6 | 165 | return result; |
XinZhangMS | 0:f7f1f0d76dd6 | 166 | } |