Vitaly Buka | 45dc9df | 2015-12-07 21:30:19 -0800 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "src/macaroon.h" |
| 6 | |
| 7 | #include <string.h> |
| 8 | |
| 9 | #include "src/crypto_utils.h" |
Vitaly Buka | 6a8bd5d | 2015-12-08 21:06:59 -0800 | [diff] [blame] | 10 | #include "src/macaroon_caveat.h" |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 11 | #include "src/macaroon_caveat_internal.h" |
Vitaly Buka | 6a8bd5d | 2015-12-08 21:06:59 -0800 | [diff] [blame] | 12 | #include "src/macaroon_encoding.h" |
Vitaly Buka | 45dc9df | 2015-12-07 21:30:19 -0800 | [diff] [blame] | 13 | |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 14 | static bool create_mac_tag_(const uint8_t* key, |
| 15 | size_t key_len, |
| 16 | const UwMacaroonContext* context, |
| 17 | const UwMacaroonCaveat* const caveats[], |
| 18 | size_t num_caveats, |
Vitaly Buka | 45dc9df | 2015-12-07 21:30:19 -0800 | [diff] [blame] | 19 | uint8_t mac_tag[UW_MACAROON_MAC_LEN]) { |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 20 | if (key == NULL || key_len == 0 || context == NULL || caveats == NULL || |
| 21 | num_caveats == 0 || mac_tag == NULL) { |
Vitaly Buka | 45dc9df | 2015-12-07 21:30:19 -0800 | [diff] [blame] | 22 | return false; |
| 23 | } |
| 24 | |
| 25 | // Store the intermediate MAC tags in an internal buffer before we finish the |
| 26 | // whole computation. |
| 27 | // If we use the output buffer mac_tag directly and certain errors happen in |
| 28 | // the middle of this computation, mac_tag will probably contain a valid |
| 29 | // macaroon tag with large scope than expected. |
| 30 | uint8_t mac_tag_buff[UW_MACAROON_MAC_LEN]; |
| 31 | |
| 32 | // Compute the first tag by using the key |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 33 | if (!uw_macaroon_caveat_sign_(key, key_len, context, caveats[0], mac_tag_buff, |
Vitaly Buka | 45dc9df | 2015-12-07 21:30:19 -0800 | [diff] [blame] | 34 | UW_MACAROON_MAC_LEN)) { |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | // Compute the rest of the tags by using the tag as the key |
| 39 | for (size_t i = 1; i < num_caveats; i++) { |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 40 | if (!uw_macaroon_caveat_sign_(mac_tag_buff, UW_MACAROON_MAC_LEN, context, |
| 41 | caveats[i], mac_tag_buff, |
Vitaly Buka | 45dc9df | 2015-12-07 21:30:19 -0800 | [diff] [blame] | 42 | UW_MACAROON_MAC_LEN)) { |
| 43 | return false; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | memcpy(mac_tag, mac_tag_buff, UW_MACAROON_MAC_LEN); |
| 48 | return true; |
| 49 | } |
| 50 | |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 51 | static bool verify_mac_tag_(const uint8_t* root_key, |
| 52 | size_t root_key_len, |
| 53 | const UwMacaroonContext* context, |
| 54 | const UwMacaroonCaveat* const caveats[], |
| 55 | size_t num_caveats, |
| 56 | const uint8_t mac_tag[UW_MACAROON_MAC_LEN]) { |
| 57 | if (root_key == NULL || root_key_len == 0 || context == NULL || |
| 58 | caveats == NULL || num_caveats == 0 || mac_tag == 0) { |
Vitaly Buka | 45dc9df | 2015-12-07 21:30:19 -0800 | [diff] [blame] | 59 | return false; |
| 60 | } |
| 61 | |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 62 | uint8_t computed_mac_tag[UW_MACAROON_MAC_LEN] = {0}; |
| 63 | if (!create_mac_tag_(root_key, root_key_len, context, caveats, num_caveats, |
| 64 | computed_mac_tag)) { |
| 65 | return false; |
| 66 | } |
Vitaly Buka | 45dc9df | 2015-12-07 21:30:19 -0800 | [diff] [blame] | 67 | |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 68 | return uw_crypto_utils_equal_(mac_tag, computed_mac_tag, UW_MACAROON_MAC_LEN); |
Vitaly Buka | 45dc9df | 2015-12-07 21:30:19 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 71 | bool uw_macaroon_create_from_root_key_(UwMacaroon* new_macaroon, |
| 72 | const uint8_t* root_key, |
| 73 | size_t root_key_len, |
| 74 | const UwMacaroonContext* context, |
| 75 | const UwMacaroonCaveat* const caveats[], |
| 76 | size_t num_caveats) { |
| 77 | if (new_macaroon == NULL || root_key == NULL || context == NULL || |
| 78 | root_key_len == 0 || caveats == NULL || num_caveats == 0) { |
Vitaly Buka | 45dc9df | 2015-12-07 21:30:19 -0800 | [diff] [blame] | 79 | return false; |
| 80 | } |
| 81 | |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 82 | if (!create_mac_tag_(root_key, root_key_len, context, caveats, num_caveats, |
Vitaly Buka | 45dc9df | 2015-12-07 21:30:19 -0800 | [diff] [blame] | 83 | new_macaroon->mac_tag)) { |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | new_macaroon->num_caveats = num_caveats; |
| 88 | new_macaroon->caveats = caveats; |
| 89 | |
| 90 | return true; |
| 91 | } |
| 92 | |
Vitaly Buka | 45dc9df | 2015-12-07 21:30:19 -0800 | [diff] [blame] | 93 | bool uw_macaroon_extend_(const UwMacaroon* old_macaroon, |
| 94 | UwMacaroon* new_macaroon, |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 95 | const UwMacaroonContext* context, |
Vitaly Buka | 45dc9df | 2015-12-07 21:30:19 -0800 | [diff] [blame] | 96 | const UwMacaroonCaveat* additional_caveat, |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 97 | uint8_t* buffer, |
| 98 | size_t buffer_size) { |
| 99 | if (old_macaroon == NULL || new_macaroon == NULL || context == NULL || |
Vitaly Buka | 45dc9df | 2015-12-07 21:30:19 -0800 | [diff] [blame] | 100 | additional_caveat == NULL || buffer == NULL || buffer_size == 0) { |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | new_macaroon->num_caveats = old_macaroon->num_caveats + 1; |
| 105 | |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 106 | // Extend the caveat pointer list |
| 107 | if ((new_macaroon->num_caveats) * sizeof(UwMacaroonCaveat*) > buffer_size) { |
| 108 | // Not enough memory to store the extended caveat pointer list |
Vitaly Buka | 45dc9df | 2015-12-07 21:30:19 -0800 | [diff] [blame] | 109 | return false; |
| 110 | } |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 111 | const UwMacaroonCaveat** extended_list = (const UwMacaroonCaveat**)buffer; |
| 112 | if (new_macaroon->caveats != old_macaroon->caveats) { |
Vitaly Buka | 45dc9df | 2015-12-07 21:30:19 -0800 | [diff] [blame] | 113 | memcpy(extended_list, old_macaroon->caveats, |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 114 | old_macaroon->num_caveats * sizeof(old_macaroon->caveats[0])); |
Vitaly Buka | 45dc9df | 2015-12-07 21:30:19 -0800 | [diff] [blame] | 115 | } |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 116 | extended_list[old_macaroon->num_caveats] = additional_caveat; |
| 117 | new_macaroon->caveats = (const UwMacaroonCaveat* const*)extended_list; |
Vitaly Buka | 45dc9df | 2015-12-07 21:30:19 -0800 | [diff] [blame] | 118 | |
| 119 | // Compute the new MAC tag |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 120 | return create_mac_tag_(old_macaroon->mac_tag, UW_MACAROON_MAC_LEN, context, |
| 121 | new_macaroon->caveats + old_macaroon->num_caveats, 1, |
| 122 | new_macaroon->mac_tag); |
| 123 | } |
| 124 | |
| 125 | static void init_validation_result(UwMacaroonValidationResult* result) { |
| 126 | // Start from the largest scope |
| 127 | result->granted_scope = kUwMacaroonCaveatScopeTypeOwner; |
| 128 | result->expiration_time = UINT32_MAX; |
| 129 | result->weave_app_restricted = false; |
| 130 | result->lan_session_id = NULL; |
| 131 | result->lan_session_id_len = 0; |
| 132 | result->num_delegatees = 0; |
| 133 | } |
| 134 | |
| 135 | /** Reset the result object to the lowest scope when encountering errors */ |
| 136 | static void reset_validation_result(UwMacaroonValidationResult* result) { |
| 137 | // Start from the largest scope or highest privilege |
| 138 | result->granted_scope = |
| 139 | (UwMacaroonCaveatScopeType)UW_MACAROON_CAVEAT_SCOPE_LOWEST_POSSIBLE; |
| 140 | result->expiration_time = 0; |
| 141 | result->weave_app_restricted = true; |
| 142 | result->lan_session_id = NULL; |
| 143 | result->lan_session_id_len = 0; |
| 144 | |
| 145 | result->num_delegatees = 0; |
| 146 | for (size_t i = 0; i < MAX_NUM_DELEGATEES; i++) { |
| 147 | result->delegatees[i].id = NULL; |
| 148 | result->delegatees[i].id_len = 0; |
Vitaly Buka | d74a732 | 2016-01-27 18:39:36 -0800 | [diff] [blame] | 149 | result->delegatees[i].type = kUwMacaroonDelegateeTypeNone; |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
| 153 | bool uw_macaroon_validate_(const UwMacaroon* macaroon, |
| 154 | const uint8_t* root_key, |
| 155 | size_t root_key_len, |
| 156 | const UwMacaroonContext* context, |
| 157 | UwMacaroonValidationResult* result) { |
| 158 | if (result == NULL) { |
| 159 | return false; |
| 160 | } |
| 161 | init_validation_result(result); |
| 162 | |
| 163 | if (root_key == NULL || root_key_len == 0 || macaroon == NULL || |
| 164 | context == NULL || result == NULL || |
| 165 | !verify_mac_tag_(root_key, root_key_len, context, macaroon->caveats, |
| 166 | macaroon->num_caveats, macaroon->mac_tag)) { |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | UwMacaroonValidationState state; |
| 171 | if (!uw_macaroon_caveat_init_validation_state_(&state)) { |
| 172 | return false; |
| 173 | } |
| 174 | for (size_t i = 0; i < macaroon->num_caveats; i++) { |
| 175 | if (!uw_macaroon_caveat_validate_(macaroon->caveats[i], context, &state, |
| 176 | result)) { |
| 177 | reset_validation_result(result); // Reset the result object |
| 178 | return false; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | return true; |
Vitaly Buka | 45dc9df | 2015-12-07 21:30:19 -0800 | [diff] [blame] | 183 | } |
Vitaly Buka | 6a8bd5d | 2015-12-08 21:06:59 -0800 | [diff] [blame] | 184 | |
| 185 | // Encode a Macaroon to a byte string |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 186 | bool uw_macaroon_serialize_(const UwMacaroon* macaroon, |
| 187 | uint8_t* out, |
| 188 | size_t out_len, |
| 189 | size_t* resulting_str_len) { |
| 190 | if (macaroon == NULL || out == NULL || |
| 191 | out_len < UW_MACAROON_ENCODING_MAX_UINT_CBOR_LEN || |
Vitaly Buka | 6a8bd5d | 2015-12-08 21:06:59 -0800 | [diff] [blame] | 192 | resulting_str_len == NULL) { |
| 193 | return false; |
| 194 | } |
| 195 | |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 196 | // Need to encode the whole Macaroon again into a byte string. |
Vitaly Buka | 6a8bd5d | 2015-12-08 21:06:59 -0800 | [diff] [blame] | 197 | |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 198 | // First encode the part without the overall byte string header to the buffer |
| 199 | // to get the total length. |
| 200 | size_t item_len = 0; |
| 201 | // Start with an offset |
| 202 | size_t offset = UW_MACAROON_ENCODING_MAX_UINT_CBOR_LEN; |
| 203 | if (!uw_macaroon_encoding_encode_array_len_((uint32_t)(macaroon->num_caveats), |
| 204 | out + offset, out_len - offset, |
| 205 | &item_len)) { |
Vitaly Buka | 6a8bd5d | 2015-12-08 21:06:59 -0800 | [diff] [blame] | 206 | return false; |
| 207 | } |
| 208 | offset += item_len; |
| 209 | |
| 210 | for (size_t i = 0; i < macaroon->num_caveats; i++) { |
| 211 | if (!uw_macaroon_encoding_encode_byte_str_( |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 212 | macaroon->caveats[i]->bytes, macaroon->caveats[i]->num_bytes, |
Vitaly Buka | 6a8bd5d | 2015-12-08 21:06:59 -0800 | [diff] [blame] | 213 | out + offset, out_len - offset, &item_len)) { |
| 214 | return false; |
| 215 | } |
| 216 | offset += item_len; |
| 217 | } |
| 218 | |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 219 | if (!uw_macaroon_encoding_encode_byte_str_(macaroon->mac_tag, |
| 220 | UW_MACAROON_MAC_LEN, out + offset, |
| 221 | out_len - offset, &item_len)) { |
| 222 | return false; |
| 223 | } |
| 224 | offset += item_len; |
| 225 | |
| 226 | // Encode the length of the body at the beginning of the buffer |
| 227 | size_t bstr_len = offset - UW_MACAROON_ENCODING_MAX_UINT_CBOR_LEN; |
| 228 | if (!uw_macaroon_encoding_encode_byte_str_len_( |
| 229 | bstr_len, out, UW_MACAROON_ENCODING_MAX_UINT_CBOR_LEN, &item_len)) { |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | // Move the body part to be adjacent to the byte string header part |
| 234 | memmove(out + item_len, out + UW_MACAROON_ENCODING_MAX_UINT_CBOR_LEN, |
| 235 | bstr_len); |
| 236 | |
| 237 | *resulting_str_len = item_len + bstr_len; |
Vitaly Buka | 6a8bd5d | 2015-12-08 21:06:59 -0800 | [diff] [blame] | 238 | return true; |
| 239 | } |
| 240 | |
| 241 | // Decode a byte string to a Macaroon |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 242 | bool uw_macaroon_deserialize_(const uint8_t* in, |
| 243 | size_t in_len, |
| 244 | uint8_t* buffer, |
| 245 | size_t buffer_size, |
| 246 | UwMacaroon* macaroon) { |
| 247 | if (in == NULL || in_len == 0 || buffer == NULL || buffer_size == 0 || |
| 248 | macaroon == NULL) { |
Vitaly Buka | 6a8bd5d | 2015-12-08 21:06:59 -0800 | [diff] [blame] | 249 | return false; |
| 250 | } |
| 251 | |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 252 | size_t offset = 0; |
| 253 | size_t item_len = 0; |
| 254 | |
| 255 | const uint8_t* bstr = NULL; |
| 256 | size_t bstr_len = 0; |
| 257 | if (!uw_macaroon_encoding_decode_byte_str_(in + offset, in_len - offset, |
| 258 | &bstr, &bstr_len)) { |
Vitaly Buka | 6a8bd5d | 2015-12-08 21:06:59 -0800 | [diff] [blame] | 259 | return false; |
| 260 | } |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 261 | item_len = bstr - in; // The length of the first byte string header |
| 262 | offset += item_len; |
Vitaly Buka | 6a8bd5d | 2015-12-08 21:06:59 -0800 | [diff] [blame] | 263 | |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 264 | if (item_len + bstr_len != in_len) { |
| 265 | // The string length doesn't match |
Vitaly Buka | 6a8bd5d | 2015-12-08 21:06:59 -0800 | [diff] [blame] | 266 | return false; |
| 267 | } |
Vitaly Buka | 6a8bd5d | 2015-12-08 21:06:59 -0800 | [diff] [blame] | 268 | |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 269 | uint32_t array_len = 0; |
Vitaly Buka | 6a8bd5d | 2015-12-08 21:06:59 -0800 | [diff] [blame] | 270 | if (!uw_macaroon_encoding_decode_array_len_(in + offset, in_len - offset, |
| 271 | &array_len)) { |
| 272 | return false; |
| 273 | } |
| 274 | macaroon->num_caveats = (size_t)array_len; |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 275 | if (buffer_size < |
| 276 | (array_len * (sizeof(UwMacaroonCaveat) + sizeof(UwMacaroonCaveat*)))) { |
| 277 | // Need two levels of abstraction, one for structs and one for pointers |
Vitaly Buka | 6a8bd5d | 2015-12-08 21:06:59 -0800 | [diff] [blame] | 278 | return false; |
| 279 | } |
| 280 | |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 281 | if (!uw_macaroon_encoding_get_item_len_(in + offset, in_len - offset, |
| 282 | &item_len)) { |
| 283 | return false; |
Vitaly Buka | 6a8bd5d | 2015-12-08 21:06:59 -0800 | [diff] [blame] | 284 | } |
Vitaly Buka | 7d29a5a | 2016-01-27 14:21:37 -0800 | [diff] [blame] | 285 | offset += item_len; |
| 286 | |
| 287 | const UwMacaroonCaveat** caveat_pointers = (const UwMacaroonCaveat**)buffer; |
| 288 | buffer += array_len * sizeof(UwMacaroonCaveat*); |
| 289 | UwMacaroonCaveat* caveat_structs = (UwMacaroonCaveat*)buffer; |
| 290 | for (size_t i = 0; i < array_len; i++) { |
| 291 | caveat_pointers[i] = &(caveat_structs[i]); |
| 292 | |
| 293 | if (!uw_macaroon_encoding_decode_byte_str_( |
| 294 | in + offset, in_len - offset, &(caveat_structs[i].bytes), |
| 295 | &(caveat_structs[i].num_bytes))) { |
| 296 | return false; |
| 297 | } |
| 298 | |
| 299 | if (!uw_macaroon_encoding_get_item_len_(in + offset, in_len - offset, |
| 300 | &item_len)) { |
| 301 | return false; |
| 302 | } |
| 303 | offset += item_len; |
| 304 | } |
| 305 | macaroon->caveats = caveat_pointers; |
| 306 | |
| 307 | const uint8_t* tag; |
| 308 | size_t tag_len; |
| 309 | if (!uw_macaroon_encoding_decode_byte_str_(in + offset, in_len - offset, &tag, |
| 310 | &tag_len) || |
| 311 | tag_len != UW_MACAROON_MAC_LEN) { |
| 312 | return false; |
| 313 | } |
| 314 | memcpy(macaroon->mac_tag, tag, UW_MACAROON_MAC_LEN); |
Vitaly Buka | 6a8bd5d | 2015-12-08 21:06:59 -0800 | [diff] [blame] | 315 | |
| 316 | return true; |
| 317 | } |