Azure IoT common library
Fork of azure_c_shared_utility by
sastoken.c@19:2e0811512ceb, 2017-01-28 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Sat Jan 28 09:35:22 2017 -0800
- Revision:
- 19:2e0811512ceb
- Parent:
- 15:956c6d205aa7
- Child:
- 23:ba126c6eb699
1.1.6
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Azure.IoT Build | 0:fa2de1b79154 | 1 | // Copyright (c) Microsoft. All rights reserved. |
Azure.IoT Build | 0:fa2de1b79154 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
Azure.IoT Build | 0:fa2de1b79154 | 3 | |
AzureIoTClient | 19:2e0811512ceb | 4 | #include <stdlib.h> |
Azure.IoT Build | 0:fa2de1b79154 | 5 | #include "azure_c_shared_utility/gballoc.h" |
Azure.IoT Build | 0:fa2de1b79154 | 6 | #include "azure_c_shared_utility/sastoken.h" |
Azure.IoT Build | 0:fa2de1b79154 | 7 | #include "azure_c_shared_utility/urlencode.h" |
Azure.IoT Build | 0:fa2de1b79154 | 8 | #include "azure_c_shared_utility/hmacsha256.h" |
Azure.IoT Build | 0:fa2de1b79154 | 9 | #include "azure_c_shared_utility/base64.h" |
Azure.IoT Build | 0:fa2de1b79154 | 10 | #include "azure_c_shared_utility/agenttime.h" |
Azure.IoT Build | 0:fa2de1b79154 | 11 | #include "azure_c_shared_utility/strings.h" |
Azure.IoT Build | 0:fa2de1b79154 | 12 | #include "azure_c_shared_utility/buffer_.h" |
Azure.IoT Build | 6:c55b013dfc2a | 13 | #include "azure_c_shared_utility/xlogging.h" |
AzureIoTClient | 15:956c6d205aa7 | 14 | #include "azure_c_shared_utility/crt_abstractions.h" |
AzureIoTClient | 15:956c6d205aa7 | 15 | |
AzureIoTClient | 15:956c6d205aa7 | 16 | static double getExpiryValue(const char* expiryASCII) |
AzureIoTClient | 15:956c6d205aa7 | 17 | { |
AzureIoTClient | 15:956c6d205aa7 | 18 | double value = 0; |
AzureIoTClient | 15:956c6d205aa7 | 19 | size_t i = 0; |
AzureIoTClient | 15:956c6d205aa7 | 20 | for (i = 0; expiryASCII[i] != '\0'; i++) |
AzureIoTClient | 15:956c6d205aa7 | 21 | { |
AzureIoTClient | 15:956c6d205aa7 | 22 | if (expiryASCII[i] >= '0' && expiryASCII[i] <= '9') |
AzureIoTClient | 15:956c6d205aa7 | 23 | { |
AzureIoTClient | 15:956c6d205aa7 | 24 | value = value * 10 + (expiryASCII[i] - '0'); |
AzureIoTClient | 15:956c6d205aa7 | 25 | } |
AzureIoTClient | 15:956c6d205aa7 | 26 | else |
AzureIoTClient | 15:956c6d205aa7 | 27 | { |
AzureIoTClient | 15:956c6d205aa7 | 28 | value = 0; |
AzureIoTClient | 15:956c6d205aa7 | 29 | break; |
AzureIoTClient | 15:956c6d205aa7 | 30 | } |
AzureIoTClient | 15:956c6d205aa7 | 31 | } |
AzureIoTClient | 15:956c6d205aa7 | 32 | return value; |
AzureIoTClient | 15:956c6d205aa7 | 33 | } |
AzureIoTClient | 15:956c6d205aa7 | 34 | |
AzureIoTClient | 15:956c6d205aa7 | 35 | bool SASToken_Validate(STRING_HANDLE sasToken) |
AzureIoTClient | 15:956c6d205aa7 | 36 | { |
AzureIoTClient | 15:956c6d205aa7 | 37 | bool result; |
AzureIoTClient | 15:956c6d205aa7 | 38 | /*Codes_SRS_SASTOKEN_25_025: [**SASToken_Validate shall get the SASToken value by invoking STRING_c_str on the handle.**]***/ |
AzureIoTClient | 15:956c6d205aa7 | 39 | const char* sasTokenArray = STRING_c_str(sasToken); |
AzureIoTClient | 15:956c6d205aa7 | 40 | |
AzureIoTClient | 15:956c6d205aa7 | 41 | /***Codes_SRS_SASTOKEN_25_024: [**If handle is NULL then SASToken_Validate shall return false.**] ***/ |
AzureIoTClient | 15:956c6d205aa7 | 42 | /*Codes_SRS_SASTOKEN_25_026: [**If STRING_c_str on handle return NULL then SASToken_Validate shall return false.**]***/ |
AzureIoTClient | 15:956c6d205aa7 | 43 | if (sasToken == NULL || sasTokenArray == NULL) |
AzureIoTClient | 15:956c6d205aa7 | 44 | { |
AzureIoTClient | 15:956c6d205aa7 | 45 | result = false; |
AzureIoTClient | 15:956c6d205aa7 | 46 | } |
AzureIoTClient | 15:956c6d205aa7 | 47 | else |
AzureIoTClient | 15:956c6d205aa7 | 48 | { |
AzureIoTClient | 15:956c6d205aa7 | 49 | int seStart = -1, seStop = -1; |
AzureIoTClient | 15:956c6d205aa7 | 50 | int srStart = -1, srStop = -1; |
AzureIoTClient | 15:956c6d205aa7 | 51 | int sigStart = -1, sigStop = -1; |
AzureIoTClient | 15:956c6d205aa7 | 52 | int tokenLength = (int) STRING_length(sasToken); |
AzureIoTClient | 15:956c6d205aa7 | 53 | int i ; |
AzureIoTClient | 15:956c6d205aa7 | 54 | for (i = 0; i < tokenLength; i++) |
AzureIoTClient | 15:956c6d205aa7 | 55 | { |
AzureIoTClient | 15:956c6d205aa7 | 56 | if (sasTokenArray[i] == 's' && sasTokenArray[i + 1] == 'e' && sasTokenArray[i + 2] == '=') // Look for se= |
AzureIoTClient | 15:956c6d205aa7 | 57 | { |
AzureIoTClient | 15:956c6d205aa7 | 58 | seStart = i + 3; |
AzureIoTClient | 15:956c6d205aa7 | 59 | if (srStart > 0 && srStop < 0) |
AzureIoTClient | 15:956c6d205aa7 | 60 | { |
AzureIoTClient | 15:956c6d205aa7 | 61 | if (sasTokenArray[i - 1] != '&' && sasTokenArray[i - 1] == ' ') // look for either & or space |
AzureIoTClient | 15:956c6d205aa7 | 62 | srStop = i - 1; |
AzureIoTClient | 15:956c6d205aa7 | 63 | else if (sasTokenArray[i - 1] == '&') |
AzureIoTClient | 15:956c6d205aa7 | 64 | srStop = i - 2; |
AzureIoTClient | 15:956c6d205aa7 | 65 | else |
AzureIoTClient | 15:956c6d205aa7 | 66 | seStart = -1; // as the format is not either "&se=" or " se=" |
AzureIoTClient | 15:956c6d205aa7 | 67 | } |
AzureIoTClient | 15:956c6d205aa7 | 68 | else if (sigStart > 0 && sigStop < 0) |
AzureIoTClient | 15:956c6d205aa7 | 69 | { |
AzureIoTClient | 15:956c6d205aa7 | 70 | if (sasTokenArray[i - 1] != '&' && sasTokenArray[i - 1] == ' ') |
AzureIoTClient | 15:956c6d205aa7 | 71 | sigStop = i - 1; |
AzureIoTClient | 15:956c6d205aa7 | 72 | else if (sasTokenArray[i - 1] == '&') |
AzureIoTClient | 15:956c6d205aa7 | 73 | sigStop = i - 2; |
AzureIoTClient | 15:956c6d205aa7 | 74 | else |
AzureIoTClient | 15:956c6d205aa7 | 75 | seStart = -1; |
AzureIoTClient | 15:956c6d205aa7 | 76 | } |
AzureIoTClient | 15:956c6d205aa7 | 77 | continue; |
AzureIoTClient | 15:956c6d205aa7 | 78 | } |
AzureIoTClient | 15:956c6d205aa7 | 79 | if (sasTokenArray[i] == 's' && sasTokenArray[i + 1] == 'r' && sasTokenArray[i + 2] == '=') // Look for sr= |
AzureIoTClient | 15:956c6d205aa7 | 80 | { |
AzureIoTClient | 15:956c6d205aa7 | 81 | srStart = i + 3; |
AzureIoTClient | 15:956c6d205aa7 | 82 | if (seStart > 0 && seStop < 0) |
AzureIoTClient | 15:956c6d205aa7 | 83 | { |
AzureIoTClient | 15:956c6d205aa7 | 84 | if (sasTokenArray[i - 1] != '&' && sasTokenArray[i - 1] == ' ') |
AzureIoTClient | 15:956c6d205aa7 | 85 | seStop = i - 1; |
AzureIoTClient | 15:956c6d205aa7 | 86 | else if (sasTokenArray[i - 1] == '&') |
AzureIoTClient | 15:956c6d205aa7 | 87 | seStop = i - 2; |
AzureIoTClient | 15:956c6d205aa7 | 88 | else |
AzureIoTClient | 15:956c6d205aa7 | 89 | srStart = -1; |
AzureIoTClient | 15:956c6d205aa7 | 90 | } |
AzureIoTClient | 15:956c6d205aa7 | 91 | else if (sigStart > 0 && sigStop < 0) |
AzureIoTClient | 15:956c6d205aa7 | 92 | { |
AzureIoTClient | 15:956c6d205aa7 | 93 | if (sasTokenArray[i - 1] != '&' && sasTokenArray[i - 1] == ' ') |
AzureIoTClient | 15:956c6d205aa7 | 94 | sigStop = i - 1; |
AzureIoTClient | 15:956c6d205aa7 | 95 | else if (sasTokenArray[i - 1] == '&') |
AzureIoTClient | 15:956c6d205aa7 | 96 | sigStop = i - 2; |
AzureIoTClient | 15:956c6d205aa7 | 97 | else |
AzureIoTClient | 15:956c6d205aa7 | 98 | srStart = -1; |
AzureIoTClient | 15:956c6d205aa7 | 99 | } |
AzureIoTClient | 15:956c6d205aa7 | 100 | continue; |
AzureIoTClient | 15:956c6d205aa7 | 101 | } |
AzureIoTClient | 15:956c6d205aa7 | 102 | if (sasTokenArray[i] == 's' && sasTokenArray[i + 1] == 'i' && sasTokenArray[i + 2] == 'g' && sasTokenArray[i + 3] == '=') // Look for sig= |
AzureIoTClient | 15:956c6d205aa7 | 103 | { |
AzureIoTClient | 15:956c6d205aa7 | 104 | sigStart = i + 4; |
AzureIoTClient | 15:956c6d205aa7 | 105 | if (srStart > 0 && srStop < 0) |
AzureIoTClient | 15:956c6d205aa7 | 106 | { |
AzureIoTClient | 15:956c6d205aa7 | 107 | if (sasTokenArray[i - 1] != '&' && sasTokenArray[i - 1] == ' ') |
AzureIoTClient | 15:956c6d205aa7 | 108 | srStop = i - 1; |
AzureIoTClient | 15:956c6d205aa7 | 109 | else if (sasTokenArray[i - 1] == '&') |
AzureIoTClient | 15:956c6d205aa7 | 110 | srStop = i - 2; |
AzureIoTClient | 15:956c6d205aa7 | 111 | else |
AzureIoTClient | 15:956c6d205aa7 | 112 | sigStart = -1; |
AzureIoTClient | 15:956c6d205aa7 | 113 | } |
AzureIoTClient | 15:956c6d205aa7 | 114 | else if (seStart > 0 && seStop < 0) |
AzureIoTClient | 15:956c6d205aa7 | 115 | { |
AzureIoTClient | 15:956c6d205aa7 | 116 | if (sasTokenArray[i - 1] != '&' && sasTokenArray[i - 1] == ' ') |
AzureIoTClient | 15:956c6d205aa7 | 117 | seStop = i - 1; |
AzureIoTClient | 15:956c6d205aa7 | 118 | else if (sasTokenArray[i - 1] == '&') |
AzureIoTClient | 15:956c6d205aa7 | 119 | seStop = i - 2; |
AzureIoTClient | 15:956c6d205aa7 | 120 | else |
AzureIoTClient | 15:956c6d205aa7 | 121 | sigStart = -1; |
AzureIoTClient | 15:956c6d205aa7 | 122 | } |
AzureIoTClient | 15:956c6d205aa7 | 123 | continue; |
AzureIoTClient | 15:956c6d205aa7 | 124 | } |
AzureIoTClient | 15:956c6d205aa7 | 125 | } |
AzureIoTClient | 15:956c6d205aa7 | 126 | /*Codes_SRS_SASTOKEN_25_027: [**If SASTOKEN does not obey the SASToken format then SASToken_Validate shall return false.**]***/ |
AzureIoTClient | 15:956c6d205aa7 | 127 | /*Codes_SRS_SASTOKEN_25_028: [**SASToken_validate shall check for the presence of sr, se and sig from the token and return false if not found**]***/ |
AzureIoTClient | 15:956c6d205aa7 | 128 | if (seStart < 0 || srStart < 0 || sigStart < 0) |
AzureIoTClient | 15:956c6d205aa7 | 129 | { |
AzureIoTClient | 15:956c6d205aa7 | 130 | result = false; |
AzureIoTClient | 15:956c6d205aa7 | 131 | } |
AzureIoTClient | 15:956c6d205aa7 | 132 | else |
AzureIoTClient | 15:956c6d205aa7 | 133 | { |
AzureIoTClient | 15:956c6d205aa7 | 134 | if (seStop < 0) |
AzureIoTClient | 15:956c6d205aa7 | 135 | { |
AzureIoTClient | 15:956c6d205aa7 | 136 | seStop = tokenLength; |
AzureIoTClient | 15:956c6d205aa7 | 137 | } |
AzureIoTClient | 15:956c6d205aa7 | 138 | else if (srStop < 0) |
AzureIoTClient | 15:956c6d205aa7 | 139 | { |
AzureIoTClient | 15:956c6d205aa7 | 140 | srStop = tokenLength; |
AzureIoTClient | 15:956c6d205aa7 | 141 | } |
AzureIoTClient | 15:956c6d205aa7 | 142 | else if (sigStop < 0) |
AzureIoTClient | 15:956c6d205aa7 | 143 | { |
AzureIoTClient | 15:956c6d205aa7 | 144 | sigStop = tokenLength; |
AzureIoTClient | 15:956c6d205aa7 | 145 | } |
AzureIoTClient | 15:956c6d205aa7 | 146 | |
AzureIoTClient | 15:956c6d205aa7 | 147 | if ((seStop <= seStart) || |
AzureIoTClient | 15:956c6d205aa7 | 148 | (srStop <= srStart) || |
AzureIoTClient | 15:956c6d205aa7 | 149 | (sigStop <= sigStart)) |
AzureIoTClient | 15:956c6d205aa7 | 150 | { |
AzureIoTClient | 15:956c6d205aa7 | 151 | result = false; |
AzureIoTClient | 15:956c6d205aa7 | 152 | } |
AzureIoTClient | 15:956c6d205aa7 | 153 | else |
AzureIoTClient | 15:956c6d205aa7 | 154 | { |
AzureIoTClient | 15:956c6d205aa7 | 155 | char* expiryASCII = malloc(seStop - seStart + 1); |
AzureIoTClient | 15:956c6d205aa7 | 156 | /*Codes_SRS_SASTOKEN_25_031: [**If malloc fails during validation then SASToken_Validate shall return false.**]***/ |
AzureIoTClient | 15:956c6d205aa7 | 157 | if (expiryASCII == NULL) |
AzureIoTClient | 15:956c6d205aa7 | 158 | { |
AzureIoTClient | 15:956c6d205aa7 | 159 | result = false; |
AzureIoTClient | 15:956c6d205aa7 | 160 | } |
AzureIoTClient | 15:956c6d205aa7 | 161 | else |
AzureIoTClient | 15:956c6d205aa7 | 162 | { |
AzureIoTClient | 15:956c6d205aa7 | 163 | double expiry; |
AzureIoTClient | 15:956c6d205aa7 | 164 | for (i = seStart; i < seStop; i++) |
AzureIoTClient | 15:956c6d205aa7 | 165 | { |
AzureIoTClient | 15:956c6d205aa7 | 166 | expiryASCII[i - seStart] = sasTokenArray[i]; |
AzureIoTClient | 15:956c6d205aa7 | 167 | } |
AzureIoTClient | 15:956c6d205aa7 | 168 | expiryASCII[seStop - seStart] = '\0'; |
AzureIoTClient | 15:956c6d205aa7 | 169 | |
AzureIoTClient | 15:956c6d205aa7 | 170 | expiry = getExpiryValue(expiryASCII); |
AzureIoTClient | 15:956c6d205aa7 | 171 | /*Codes_SRS_SASTOKEN_25_029: [**SASToken_validate shall check for expiry time from token and if token has expired then would return false **]***/ |
AzureIoTClient | 15:956c6d205aa7 | 172 | if (expiry <= 0) |
AzureIoTClient | 15:956c6d205aa7 | 173 | { |
AzureIoTClient | 15:956c6d205aa7 | 174 | result = false; |
AzureIoTClient | 15:956c6d205aa7 | 175 | } |
AzureIoTClient | 15:956c6d205aa7 | 176 | else |
AzureIoTClient | 15:956c6d205aa7 | 177 | { |
AzureIoTClient | 15:956c6d205aa7 | 178 | double secSinceEpoch = get_difftime(get_time(NULL), (time_t)0); |
AzureIoTClient | 15:956c6d205aa7 | 179 | if (expiry < secSinceEpoch) |
AzureIoTClient | 15:956c6d205aa7 | 180 | { |
AzureIoTClient | 15:956c6d205aa7 | 181 | /*Codes_SRS_SASTOKEN_25_029: [**SASToken_validate shall check for expiry time from token and if token has expired then would return false **]***/ |
AzureIoTClient | 15:956c6d205aa7 | 182 | result = false; |
AzureIoTClient | 15:956c6d205aa7 | 183 | } |
AzureIoTClient | 15:956c6d205aa7 | 184 | else |
AzureIoTClient | 15:956c6d205aa7 | 185 | { |
AzureIoTClient | 15:956c6d205aa7 | 186 | /*Codes_SRS_SASTOKEN_25_030: [**SASToken_validate shall return true only if the format is obeyed and the token has not yet expired **]***/ |
AzureIoTClient | 15:956c6d205aa7 | 187 | result = true; |
AzureIoTClient | 15:956c6d205aa7 | 188 | } |
AzureIoTClient | 15:956c6d205aa7 | 189 | } |
AzureIoTClient | 15:956c6d205aa7 | 190 | free(expiryASCII); |
AzureIoTClient | 15:956c6d205aa7 | 191 | } |
AzureIoTClient | 15:956c6d205aa7 | 192 | } |
AzureIoTClient | 15:956c6d205aa7 | 193 | } |
AzureIoTClient | 15:956c6d205aa7 | 194 | } |
AzureIoTClient | 15:956c6d205aa7 | 195 | |
AzureIoTClient | 15:956c6d205aa7 | 196 | return result; |
AzureIoTClient | 15:956c6d205aa7 | 197 | } |
Azure.IoT Build | 0:fa2de1b79154 | 198 | |
Azure.IoT Build | 0:fa2de1b79154 | 199 | STRING_HANDLE SASToken_Create(STRING_HANDLE key, STRING_HANDLE scope, STRING_HANDLE keyName, size_t expiry) |
Azure.IoT Build | 0:fa2de1b79154 | 200 | { |
Azure.IoT Build | 0:fa2de1b79154 | 201 | STRING_HANDLE result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 202 | char tokenExpirationTime[32] = { 0 }; |
Azure.IoT Build | 0:fa2de1b79154 | 203 | |
Azure.IoT Build | 0:fa2de1b79154 | 204 | /*Codes_SRS_SASTOKEN_06_001: [If key is NULL then SASToken_Create shall return NULL.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 205 | /*Codes_SRS_SASTOKEN_06_003: [If scope is NULL then SASToken_Create shall return NULL.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 206 | /*Codes_SRS_SASTOKEN_06_007: [If keyName is NULL then SASToken_Create shall return NULL.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 207 | if ((key == NULL) || |
Azure.IoT Build | 0:fa2de1b79154 | 208 | (scope == NULL) || |
Azure.IoT Build | 0:fa2de1b79154 | 209 | (keyName == NULL)) |
Azure.IoT Build | 0:fa2de1b79154 | 210 | { |
AzureIoTClient | 1:9190c0f4d23a | 211 | LogError("Invalid Parameter to SASToken_Create. handle key: %p, handle scope: %p, handle keyName: %p", key, scope, keyName); |
Azure.IoT Build | 0:fa2de1b79154 | 212 | } |
Azure.IoT Build | 0:fa2de1b79154 | 213 | else |
Azure.IoT Build | 0:fa2de1b79154 | 214 | { |
Azure.IoT Build | 0:fa2de1b79154 | 215 | BUFFER_HANDLE decodedKey; |
Azure.IoT Build | 0:fa2de1b79154 | 216 | /*Codes_SRS_SASTOKEN_06_029: [The key parameter is decoded from base64.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 217 | if ((decodedKey = Base64_Decoder(STRING_c_str(key))) == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 218 | { |
Azure.IoT Build | 0:fa2de1b79154 | 219 | /*Codes_SRS_SASTOKEN_06_030: [If there is an error in the decoding then SASToken_Create shall return NULL.]*/ |
AzureIoTClient | 1:9190c0f4d23a | 220 | LogError("Unable to decode the key for generating the SAS."); |
Azure.IoT Build | 0:fa2de1b79154 | 221 | } |
Azure.IoT Build | 0:fa2de1b79154 | 222 | else |
Azure.IoT Build | 0:fa2de1b79154 | 223 | { |
Azure.IoT Build | 0:fa2de1b79154 | 224 | /*Codes_SRS_SASTOKEN_06_026: [If the conversion to string form fails for any reason then SASToken_Create shall return NULL.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 225 | if (size_tToString(tokenExpirationTime, sizeof(tokenExpirationTime), expiry) != 0) |
Azure.IoT Build | 0:fa2de1b79154 | 226 | { |
AzureIoTClient | 1:9190c0f4d23a | 227 | LogError("For some reason converting seconds to a string failed. No SAS can be generated."); |
Azure.IoT Build | 0:fa2de1b79154 | 228 | } |
Azure.IoT Build | 0:fa2de1b79154 | 229 | else |
Azure.IoT Build | 0:fa2de1b79154 | 230 | { |
Azure.IoT Build | 0:fa2de1b79154 | 231 | STRING_HANDLE toBeHashed = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 232 | BUFFER_HANDLE hash = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 233 | if (((hash = BUFFER_new()) == NULL) || |
Azure.IoT Build | 0:fa2de1b79154 | 234 | ((toBeHashed = STRING_new()) == NULL) || |
Azure.IoT Build | 0:fa2de1b79154 | 235 | ((result = STRING_new()) == NULL)) |
Azure.IoT Build | 0:fa2de1b79154 | 236 | { |
AzureIoTClient | 1:9190c0f4d23a | 237 | LogError("Unable to allocate memory to prepare SAS token."); |
Azure.IoT Build | 0:fa2de1b79154 | 238 | } |
Azure.IoT Build | 0:fa2de1b79154 | 239 | else |
Azure.IoT Build | 0:fa2de1b79154 | 240 | { |
Azure.IoT Build | 0:fa2de1b79154 | 241 | /*Codes_SRS_SASTOKEN_06_009: [The scope is the basis for creating a STRING_HANDLE.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 242 | /*Codes_SRS_SASTOKEN_06_010: [A "\n" is appended to that string.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 243 | /*Codes_SRS_SASTOKEN_06_011: [tokenExpirationTime is appended to that string.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 244 | if ((STRING_concat_with_STRING(toBeHashed, scope) != 0) || |
Azure.IoT Build | 0:fa2de1b79154 | 245 | (STRING_concat(toBeHashed, "\n") != 0) || |
Azure.IoT Build | 0:fa2de1b79154 | 246 | (STRING_concat(toBeHashed, tokenExpirationTime) != 0)) |
Azure.IoT Build | 0:fa2de1b79154 | 247 | { |
AzureIoTClient | 1:9190c0f4d23a | 248 | LogError("Unable to build the input to the HMAC to prepare SAS token."); |
Azure.IoT Build | 0:fa2de1b79154 | 249 | STRING_delete(result); |
Azure.IoT Build | 0:fa2de1b79154 | 250 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 251 | } |
Azure.IoT Build | 0:fa2de1b79154 | 252 | else |
Azure.IoT Build | 0:fa2de1b79154 | 253 | { |
Azure.IoT Build | 0:fa2de1b79154 | 254 | STRING_HANDLE base64Signature = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 255 | STRING_HANDLE urlEncodedSignature = NULL; |
AzureIoTClient | 11:77df6d7e65ae | 256 | size_t inLen = STRING_length(toBeHashed); |
AzureIoTClient | 11:77df6d7e65ae | 257 | const unsigned char* inBuf = (const unsigned char*)STRING_c_str(toBeHashed); |
AzureIoTClient | 11:77df6d7e65ae | 258 | size_t outLen = BUFFER_length(decodedKey); |
AzureIoTClient | 11:77df6d7e65ae | 259 | unsigned char* outBuf = BUFFER_u_char(decodedKey); |
Azure.IoT Build | 0:fa2de1b79154 | 260 | /*Codes_SRS_SASTOKEN_06_013: [If an error is returned from the HMAC256 function then NULL is returned from SASToken_Create.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 261 | /*Codes_SRS_SASTOKEN_06_012: [An HMAC256 hash is calculated using the decodedKey, over toBeHashed.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 262 | /*Codes_SRS_SASTOKEN_06_014: [If there are any errors from the following operations then NULL shall be returned.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 263 | /*Codes_SRS_SASTOKEN_06_015: [The hash is base 64 encoded.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 264 | /*Codes_SRS_SASTOKEN_06_028: [base64Signature shall be url encoded.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 265 | /*Codes_SRS_SASTOKEN_06_016: [The string "SharedAccessSignature sr=" is the first part of the result of SASToken_Create.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 266 | /*Codes_SRS_SASTOKEN_06_017: [The scope parameter is appended to result.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 267 | /*Codes_SRS_SASTOKEN_06_018: [The string "&sig=" is appended to result.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 268 | /*Codes_SRS_SASTOKEN_06_019: [The string urlEncodedSignature shall be appended to result.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 269 | /*Codes_SRS_SASTOKEN_06_020: [The string "&se=" shall be appended to result.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 270 | /*Codes_SRS_SASTOKEN_06_021: [tokenExpirationTime is appended to result.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 271 | /*Codes_SRS_SASTOKEN_06_022: [The string "&skn=" is appended to result.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 272 | /*Codes_SRS_SASTOKEN_06_023: [The argument keyName is appended to result.]*/ |
AzureIoTClient | 11:77df6d7e65ae | 273 | if ((HMACSHA256_ComputeHash(outBuf, outLen, inBuf, inLen, hash) != HMACSHA256_OK) || |
Azure.IoT Build | 0:fa2de1b79154 | 274 | ((base64Signature = Base64_Encode(hash)) == NULL) || |
Azure.IoT Build | 0:fa2de1b79154 | 275 | ((urlEncodedSignature = URL_Encode(base64Signature)) == NULL) || |
Azure.IoT Build | 0:fa2de1b79154 | 276 | (STRING_copy(result, "SharedAccessSignature sr=") != 0) || |
Azure.IoT Build | 0:fa2de1b79154 | 277 | (STRING_concat_with_STRING(result, scope) != 0) || |
Azure.IoT Build | 0:fa2de1b79154 | 278 | (STRING_concat(result, "&sig=") != 0) || |
Azure.IoT Build | 0:fa2de1b79154 | 279 | (STRING_concat_with_STRING(result, urlEncodedSignature) != 0) || |
Azure.IoT Build | 0:fa2de1b79154 | 280 | (STRING_concat(result, "&se=") != 0) || |
Azure.IoT Build | 0:fa2de1b79154 | 281 | (STRING_concat(result, tokenExpirationTime) != 0) || |
Azure.IoT Build | 0:fa2de1b79154 | 282 | (STRING_concat(result, "&skn=") != 0) || |
Azure.IoT Build | 0:fa2de1b79154 | 283 | (STRING_concat_with_STRING(result, keyName) != 0)) |
Azure.IoT Build | 0:fa2de1b79154 | 284 | { |
AzureIoTClient | 1:9190c0f4d23a | 285 | LogError("Unable to build the SAS token."); |
Azure.IoT Build | 0:fa2de1b79154 | 286 | STRING_delete(result); |
Azure.IoT Build | 0:fa2de1b79154 | 287 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 288 | } |
Azure.IoT Build | 0:fa2de1b79154 | 289 | else |
Azure.IoT Build | 0:fa2de1b79154 | 290 | { |
Azure.IoT Build | 0:fa2de1b79154 | 291 | /* everything OK */ |
Azure.IoT Build | 0:fa2de1b79154 | 292 | } |
Azure.IoT Build | 0:fa2de1b79154 | 293 | STRING_delete(base64Signature); |
Azure.IoT Build | 0:fa2de1b79154 | 294 | STRING_delete(urlEncodedSignature); |
Azure.IoT Build | 0:fa2de1b79154 | 295 | } |
Azure.IoT Build | 0:fa2de1b79154 | 296 | } |
Azure.IoT Build | 0:fa2de1b79154 | 297 | STRING_delete(toBeHashed); |
Azure.IoT Build | 0:fa2de1b79154 | 298 | BUFFER_delete(hash); |
Azure.IoT Build | 0:fa2de1b79154 | 299 | } |
Azure.IoT Build | 0:fa2de1b79154 | 300 | BUFFER_delete(decodedKey); |
Azure.IoT Build | 0:fa2de1b79154 | 301 | } |
Azure.IoT Build | 0:fa2de1b79154 | 302 | } |
Azure.IoT Build | 0:fa2de1b79154 | 303 | |
Azure.IoT Build | 0:fa2de1b79154 | 304 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 305 | } |
Azure.IoT Build | 0:fa2de1b79154 | 306 |