blob: aa775c28a10ee199ee9f9d42a2a226d9c72b1453 [file] [log] [blame]
Vitaly Buka45dc9df2015-12-07 21:30:19 -08001// 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 Buka6a8bd5d2015-12-08 21:06:59 -080010#include "src/macaroon_caveat.h"
Vitaly Buka7d29a5a2016-01-27 14:21:37 -080011#include "src/macaroon_caveat_internal.h"
Vitaly Buka6a8bd5d2015-12-08 21:06:59 -080012#include "src/macaroon_encoding.h"
Vitaly Buka45dc9df2015-12-07 21:30:19 -080013
Vitaly Buka7d29a5a2016-01-27 14:21:37 -080014static 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 Buka45dc9df2015-12-07 21:30:19 -080019 uint8_t mac_tag[UW_MACAROON_MAC_LEN]) {
Vitaly Buka7d29a5a2016-01-27 14:21:37 -080020 if (key == NULL || key_len == 0 || context == NULL || caveats == NULL ||
21 num_caveats == 0 || mac_tag == NULL) {
Vitaly Buka45dc9df2015-12-07 21:30:19 -080022 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 Buka7d29a5a2016-01-27 14:21:37 -080033 if (!uw_macaroon_caveat_sign_(key, key_len, context, caveats[0], mac_tag_buff,
Vitaly Buka45dc9df2015-12-07 21:30:19 -080034 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 Buka7d29a5a2016-01-27 14:21:37 -080040 if (!uw_macaroon_caveat_sign_(mac_tag_buff, UW_MACAROON_MAC_LEN, context,
41 caveats[i], mac_tag_buff,
Vitaly Buka45dc9df2015-12-07 21:30:19 -080042 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 Buka7d29a5a2016-01-27 14:21:37 -080051static 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 Buka45dc9df2015-12-07 21:30:19 -080059 return false;
60 }
61
Vitaly Buka7d29a5a2016-01-27 14:21:37 -080062 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 Buka45dc9df2015-12-07 21:30:19 -080067
Vitaly Buka7d29a5a2016-01-27 14:21:37 -080068 return uw_crypto_utils_equal_(mac_tag, computed_mac_tag, UW_MACAROON_MAC_LEN);
Vitaly Buka45dc9df2015-12-07 21:30:19 -080069}
70
Vitaly Buka7d29a5a2016-01-27 14:21:37 -080071bool 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 Buka45dc9df2015-12-07 21:30:19 -080079 return false;
80 }
81
Vitaly Buka7d29a5a2016-01-27 14:21:37 -080082 if (!create_mac_tag_(root_key, root_key_len, context, caveats, num_caveats,
Vitaly Buka45dc9df2015-12-07 21:30:19 -080083 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 Buka45dc9df2015-12-07 21:30:19 -080093bool uw_macaroon_extend_(const UwMacaroon* old_macaroon,
94 UwMacaroon* new_macaroon,
Vitaly Buka7d29a5a2016-01-27 14:21:37 -080095 const UwMacaroonContext* context,
Vitaly Buka45dc9df2015-12-07 21:30:19 -080096 const UwMacaroonCaveat* additional_caveat,
Vitaly Buka7d29a5a2016-01-27 14:21:37 -080097 uint8_t* buffer,
98 size_t buffer_size) {
99 if (old_macaroon == NULL || new_macaroon == NULL || context == NULL ||
Vitaly Buka45dc9df2015-12-07 21:30:19 -0800100 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 Buka7d29a5a2016-01-27 14:21:37 -0800106 // 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 Buka45dc9df2015-12-07 21:30:19 -0800109 return false;
110 }
Vitaly Buka7d29a5a2016-01-27 14:21:37 -0800111 const UwMacaroonCaveat** extended_list = (const UwMacaroonCaveat**)buffer;
112 if (new_macaroon->caveats != old_macaroon->caveats) {
Vitaly Buka45dc9df2015-12-07 21:30:19 -0800113 memcpy(extended_list, old_macaroon->caveats,
Vitaly Buka7d29a5a2016-01-27 14:21:37 -0800114 old_macaroon->num_caveats * sizeof(old_macaroon->caveats[0]));
Vitaly Buka45dc9df2015-12-07 21:30:19 -0800115 }
Vitaly Buka7d29a5a2016-01-27 14:21:37 -0800116 extended_list[old_macaroon->num_caveats] = additional_caveat;
117 new_macaroon->caveats = (const UwMacaroonCaveat* const*)extended_list;
Vitaly Buka45dc9df2015-12-07 21:30:19 -0800118
119 // Compute the new MAC tag
Vitaly Buka7d29a5a2016-01-27 14:21:37 -0800120 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
125static 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 */
136static 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 Bukad74a7322016-01-27 18:39:36 -0800149 result->delegatees[i].type = kUwMacaroonDelegateeTypeNone;
Vitaly Buka7d29a5a2016-01-27 14:21:37 -0800150 }
151}
152
153bool 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 Buka45dc9df2015-12-07 21:30:19 -0800183}
Vitaly Buka6a8bd5d2015-12-08 21:06:59 -0800184
185// Encode a Macaroon to a byte string
Vitaly Buka7d29a5a2016-01-27 14:21:37 -0800186bool 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 Buka6a8bd5d2015-12-08 21:06:59 -0800192 resulting_str_len == NULL) {
193 return false;
194 }
195
Vitaly Buka7d29a5a2016-01-27 14:21:37 -0800196 // Need to encode the whole Macaroon again into a byte string.
Vitaly Buka6a8bd5d2015-12-08 21:06:59 -0800197
Vitaly Buka7d29a5a2016-01-27 14:21:37 -0800198 // 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 Buka6a8bd5d2015-12-08 21:06:59 -0800206 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 Buka7d29a5a2016-01-27 14:21:37 -0800212 macaroon->caveats[i]->bytes, macaroon->caveats[i]->num_bytes,
Vitaly Buka6a8bd5d2015-12-08 21:06:59 -0800213 out + offset, out_len - offset, &item_len)) {
214 return false;
215 }
216 offset += item_len;
217 }
218
Vitaly Buka7d29a5a2016-01-27 14:21:37 -0800219 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 Buka6a8bd5d2015-12-08 21:06:59 -0800238 return true;
239}
240
241// Decode a byte string to a Macaroon
Vitaly Buka7d29a5a2016-01-27 14:21:37 -0800242bool 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 Buka6a8bd5d2015-12-08 21:06:59 -0800249 return false;
250 }
251
Vitaly Buka7d29a5a2016-01-27 14:21:37 -0800252 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 Buka6a8bd5d2015-12-08 21:06:59 -0800259 return false;
260 }
Vitaly Buka7d29a5a2016-01-27 14:21:37 -0800261 item_len = bstr - in; // The length of the first byte string header
262 offset += item_len;
Vitaly Buka6a8bd5d2015-12-08 21:06:59 -0800263
Vitaly Buka7d29a5a2016-01-27 14:21:37 -0800264 if (item_len + bstr_len != in_len) {
265 // The string length doesn't match
Vitaly Buka6a8bd5d2015-12-08 21:06:59 -0800266 return false;
267 }
Vitaly Buka6a8bd5d2015-12-08 21:06:59 -0800268
Vitaly Buka7d29a5a2016-01-27 14:21:37 -0800269 uint32_t array_len = 0;
Vitaly Buka6a8bd5d2015-12-08 21:06:59 -0800270 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 Buka7d29a5a2016-01-27 14:21:37 -0800275 if (buffer_size <
276 (array_len * (sizeof(UwMacaroonCaveat) + sizeof(UwMacaroonCaveat*)))) {
277 // Need two levels of abstraction, one for structs and one for pointers
Vitaly Buka6a8bd5d2015-12-08 21:06:59 -0800278 return false;
279 }
280
Vitaly Buka7d29a5a2016-01-27 14:21:37 -0800281 if (!uw_macaroon_encoding_get_item_len_(in + offset, in_len - offset,
282 &item_len)) {
283 return false;
Vitaly Buka6a8bd5d2015-12-08 21:06:59 -0800284 }
Vitaly Buka7d29a5a2016-01-27 14:21:37 -0800285 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 Buka6a8bd5d2015-12-08 21:06:59 -0800315
316 return true;
317}