aboutsummaryrefslogtreecommitdiff
path: root/third_party/libuweave/src/macaroon.c
blob: 50b2d00365d850f6c334abc9b7e6c1d6e340c4c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Copyright 2015 The Weave Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/macaroon.h"

#include <string.h>

#include "src/crypto_utils.h"
#include "src/macaroon_caveat.h"
#include "src/macaroon_caveat_internal.h"
#include "src/macaroon_encoding.h"

static bool create_mac_tag_(const uint8_t* key,
                            size_t key_len,
                            const UwMacaroonContext* context,
                            const UwMacaroonCaveat* const caveats[],
                            size_t num_caveats,
                            uint8_t mac_tag[UW_MACAROON_MAC_LEN]) {
  if (key == NULL || key_len == 0 || context == NULL || caveats == NULL ||
      num_caveats == 0 || mac_tag == NULL) {
    return false;
  }

  // Store the intermediate MAC tags in an internal buffer before we finish the
  // whole computation.
  // If we use the output buffer mac_tag directly and certain errors happen in
  // the middle of this computation, mac_tag will probably contain a valid
  // macaroon tag with large scope than expected.
  uint8_t mac_tag_buff[UW_MACAROON_MAC_LEN];

  // Compute the first tag by using the key
  if (!uw_macaroon_caveat_sign_(key, key_len, context, caveats[0], mac_tag_buff,
                                UW_MACAROON_MAC_LEN)) {
    return false;
  }

  // Compute the rest of the tags by using the tag as the key
  for (size_t i = 1; i < num_caveats; i++) {
    if (!uw_macaroon_caveat_sign_(mac_tag_buff, UW_MACAROON_MAC_LEN, context,
                                  caveats[i], mac_tag_buff,
                                  UW_MACAROON_MAC_LEN)) {
      return false;
    }
  }

  memcpy(mac_tag, mac_tag_buff, UW_MACAROON_MAC_LEN);
  return true;
}

static bool verify_mac_tag_(const uint8_t* root_key,
                            size_t root_key_len,
                            const UwMacaroonContext* context,
                            const UwMacaroonCaveat* const caveats[],
                            size_t num_caveats,
                            const uint8_t mac_tag[UW_MACAROON_MAC_LEN]) {
  if (root_key == NULL || root_key_len == 0 || context == NULL ||
      caveats == NULL || num_caveats == 0 || mac_tag == 0) {
    return false;
  }

  uint8_t computed_mac_tag[UW_MACAROON_MAC_LEN] = {0};
  if (!create_mac_tag_(root_key, root_key_len, context, caveats, num_caveats,
                       computed_mac_tag)) {
    return false;
  }

  return uw_crypto_utils_equal_(mac_tag, computed_mac_tag, UW_MACAROON_MAC_LEN);
}

bool uw_macaroon_create_from_root_key_(UwMacaroon* new_macaroon,
                                       const uint8_t* root_key,
                                       size_t root_key_len,
                                       const UwMacaroonContext* context,
                                       const UwMacaroonCaveat* const caveats[],
                                       size_t num_caveats) {
  if (new_macaroon == NULL || root_key == NULL || context == NULL ||
      root_key_len == 0 || caveats == NULL || num_caveats == 0) {
    return false;
  }

  if (!create_mac_tag_(root_key, root_key_len, context, caveats, num_caveats,
                       new_macaroon->mac_tag)) {
    return false;
  }

  new_macaroon->num_caveats = num_caveats;
  new_macaroon->caveats = caveats;

  return true;
}

bool uw_macaroon_extend_(const UwMacaroon* old_macaroon,
                         UwMacaroon* new_macaroon,
                         const UwMacaroonContext* context,
                         const UwMacaroonCaveat* additional_caveat,
                         uint8_t* buffer,
                         size_t buffer_size) {
  if (old_macaroon == NULL || new_macaroon == NULL || context == NULL ||
      additional_caveat == NULL || buffer == NULL || buffer_size == 0) {
    return false;
  }

  new_macaroon->num_caveats = old_macaroon->num_caveats + 1;

  // Extend the caveat pointer list
  if ((new_macaroon->num_caveats) * sizeof(UwMacaroonCaveat*) > buffer_size) {
    // Not enough memory to store the extended caveat pointer list
    return false;
  }
  const UwMacaroonCaveat** extended_list = (const UwMacaroonCaveat**)buffer;
  if (new_macaroon->caveats != old_macaroon->caveats) {
    memcpy(extended_list, old_macaroon->caveats,
           old_macaroon->num_caveats * sizeof(old_macaroon->caveats[0]));
  }
  extended_list[old_macaroon->num_caveats] = additional_caveat;
  new_macaroon->caveats = (const UwMacaroonCaveat* const*)extended_list;

  // Compute the new MAC tag
  return create_mac_tag_(old_macaroon->mac_tag, UW_MACAROON_MAC_LEN, context,
                         new_macaroon->caveats + old_macaroon->num_caveats, 1,
                         new_macaroon->mac_tag);
}

static void init_validation_result(UwMacaroonValidationResult* result) {
  // Start from the largest scope
  result->granted_scope = kUwMacaroonCaveatScopeTypeOwner;
  result->expiration_time = UINT32_MAX;
  result->weave_app_restricted = false;
  result->lan_session_id = NULL;
  result->lan_session_id_len = 0;
  result->num_delegatees = 0;
}

/** Reset the result object to the lowest scope when encountering errors */
static void reset_validation_result(UwMacaroonValidationResult* result) {
  // Start from the largest scope or highest privilege
  result->granted_scope =
      (UwMacaroonCaveatScopeType)UW_MACAROON_CAVEAT_SCOPE_LOWEST_POSSIBLE;
  result->expiration_time = 0;
  result->weave_app_restricted = true;
  result->lan_session_id = NULL;
  result->lan_session_id_len = 0;

  result->num_delegatees = 0;
  for (size_t i = 0; i < MAX_NUM_DELEGATEES; i++) {
    result->delegatees[i].id = NULL;
    result->delegatees[i].id_len = 0;
    result->delegatees[i].is_app = true;
  }
}

bool uw_macaroon_validate_(const UwMacaroon* macaroon,
                           const uint8_t* root_key,
                           size_t root_key_len,
                           const UwMacaroonContext* context,
                           UwMacaroonValidationResult* result) {
  if (result == NULL) {
    return false;
  }
  init_validation_result(result);

  if (root_key == NULL || root_key_len == 0 || macaroon == NULL ||
      context == NULL || result == NULL ||
      !verify_mac_tag_(root_key, root_key_len, context, macaroon->caveats,
                       macaroon->num_caveats, macaroon->mac_tag)) {
    return false;
  }

  UwMacaroonValidationState state;
  if (!uw_macaroon_caveat_init_validation_state_(&state)) {
    return false;
  }
  for (size_t i = 0; i < macaroon->num_caveats; i++) {
    if (!uw_macaroon_caveat_validate_(macaroon->caveats[i], context, &state,
                                      result)) {
      reset_validation_result(result);  // Reset the result object
      return false;
    }
  }

  return true;
}

// Encode a Macaroon to a byte string
bool uw_macaroon_serialize_(const UwMacaroon* macaroon,
                            uint8_t* out,
                            size_t out_len,
                            size_t* resulting_str_len) {
  if (macaroon == NULL || out == NULL ||
      out_len < UW_MACAROON_ENCODING_MAX_UINT_CBOR_LEN ||
      resulting_str_len == NULL) {
    return false;
  }

  // Need to encode the whole Macaroon again into a byte string.

  // First encode the part without the overall byte string header to the buffer
  // to get the total length.
  size_t item_len = 0;
  // Start with an offset
  size_t offset = UW_MACAROON_ENCODING_MAX_UINT_CBOR_LEN;
  if (!uw_macaroon_encoding_encode_array_len_((uint32_t)(macaroon->num_caveats),
                                              out + offset, out_len - offset,
                                              &item_len)) {
    return false;
  }
  offset += item_len;

  for (size_t i = 0; i < macaroon->num_caveats; i++) {
    if (!uw_macaroon_encoding_encode_byte_str_(
            macaroon->caveats[i]->bytes, macaroon->caveats[i]->num_bytes,
            out + offset, out_len - offset, &item_len)) {
      return false;
    }
    offset += item_len;
  }

  if (!uw_macaroon_encoding_encode_byte_str_(macaroon->mac_tag,
                                             UW_MACAROON_MAC_LEN, out + offset,
                                             out_len - offset, &item_len)) {
    return false;
  }
  offset += item_len;

  // Encode the length of the body at the beginning of the buffer
  size_t bstr_len = offset - UW_MACAROON_ENCODING_MAX_UINT_CBOR_LEN;
  if (!uw_macaroon_encoding_encode_byte_str_len_(
          bstr_len, out, UW_MACAROON_ENCODING_MAX_UINT_CBOR_LEN, &item_len)) {
    return false;
  }

  // Move the body part to be adjacent to the byte string header part
  memmove(out + item_len, out + UW_MACAROON_ENCODING_MAX_UINT_CBOR_LEN,
          bstr_len);

  *resulting_str_len = item_len + bstr_len;
  return true;
}

// Decode a byte string to a Macaroon
bool uw_macaroon_deserialize_(const uint8_t* in,
                              size_t in_len,
                              uint8_t* buffer,
                              size_t buffer_size,
                              UwMacaroon* macaroon) {
  if (in == NULL || in_len == 0 || buffer == NULL || buffer_size == 0 ||
      macaroon == NULL) {
    return false;
  }

  size_t offset = 0;
  size_t item_len = 0;

  const uint8_t* bstr = NULL;
  size_t bstr_len = 0;
  if (!uw_macaroon_encoding_decode_byte_str_(in + offset, in_len - offset,
                                             &bstr, &bstr_len)) {
    return false;
  }
  item_len = bstr - in;  // The length of the first byte string header
  offset += item_len;

  if (item_len + bstr_len != in_len) {
    // The string length doesn't match
    return false;
  }

  uint32_t array_len = 0;
  if (!uw_macaroon_encoding_decode_array_len_(in + offset, in_len - offset,
                                              &array_len)) {
    return false;
  }
  macaroon->num_caveats = (size_t)array_len;
  if (buffer_size <
      (array_len * (sizeof(UwMacaroonCaveat) + sizeof(UwMacaroonCaveat*)))) {
    // Need two levels of abstraction, one for structs and one for pointers
    return false;
  }

  if (!uw_macaroon_encoding_get_item_len_(in + offset, in_len - offset,
                                          &item_len)) {
    return false;
  }
  offset += item_len;

  const UwMacaroonCaveat** caveat_pointers = (const UwMacaroonCaveat**)buffer;
  buffer += array_len * sizeof(UwMacaroonCaveat*);
  UwMacaroonCaveat* caveat_structs = (UwMacaroonCaveat*)buffer;
  for (size_t i = 0; i < array_len; i++) {
    caveat_pointers[i] = &(caveat_structs[i]);

    if (!uw_macaroon_encoding_decode_byte_str_(
            in + offset, in_len - offset, &(caveat_structs[i].bytes),
            &(caveat_structs[i].num_bytes))) {
      return false;
    }

    if (!uw_macaroon_encoding_get_item_len_(in + offset, in_len - offset,
                                            &item_len)) {
      return false;
    }
    offset += item_len;
  }
  macaroon->caveats = caveat_pointers;

  const uint8_t* tag;
  size_t tag_len;
  if (!uw_macaroon_encoding_decode_byte_str_(in + offset, in_len - offset, &tag,
                                             &tag_len) ||
      tag_len != UW_MACAROON_MAC_LEN) {
    return false;
  }
  memcpy(macaroon->mac_tag, tag, UW_MACAROON_MAC_LEN);

  return true;
}