File: mongocrypt-key.c

package info (click to toggle)
libmongocrypt 1.17.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,572 kB
  • sloc: ansic: 70,067; python: 4,547; cpp: 615; sh: 460; makefile: 44; awk: 8
file content (424 lines) | stat: -rw-r--r-- 12,535 bytes parent folder | download | duplicates (4)
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
 * Copyright 2019-present MongoDB, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "mongocrypt-key-private.h"
#include "mongocrypt-private.h"
#include "mongocrypt-util-private.h" // mc_iter_document_as_bson

/* Check if two single entries are equal (i.e. ignore the 'next' pointer). */
static bool _one_key_alt_name_equal(_mongocrypt_key_alt_name_t *ptr_a, _mongocrypt_key_alt_name_t *ptr_b) {
    BSON_ASSERT_PARAM(ptr_a);
    BSON_ASSERT_PARAM(ptr_b);
    BSON_ASSERT(ptr_a->value.value_type == BSON_TYPE_UTF8);
    BSON_ASSERT(ptr_b->value.value_type == BSON_TYPE_UTF8);
    return 0 == strcmp(_mongocrypt_key_alt_name_get_string(ptr_a), _mongocrypt_key_alt_name_get_string(ptr_b));
}

static bool _find(_mongocrypt_key_alt_name_t *list, _mongocrypt_key_alt_name_t *entry) {
    BSON_ASSERT_PARAM(entry);

    for (; NULL != list; list = list->next) {
        if (_one_key_alt_name_equal(list, entry)) {
            return true;
        }
    }
    return false;
}

static uint32_t _list_len(_mongocrypt_key_alt_name_t *list) {
    uint32_t count = 0;

    while (NULL != list && count < UINT32_MAX) {
        count++;
        list = list->next;
    }
    return count;
}

static bool _check_unique(_mongocrypt_key_alt_name_t *list) {
    for (; NULL != list; list = list->next) {
        /* Check if we can find the current entry in the remaining. */
        if (_find(list->next, list)) {
            return false;
        }
    }
    return true;
}

static bool _parse_masterkey(bson_iter_t *iter, _mongocrypt_key_doc_t *out, mongocrypt_status_t *status) {
    bson_t kek_doc;

    BSON_ASSERT_PARAM(iter);
    BSON_ASSERT_PARAM(out);

    if (!BSON_ITER_HOLDS_DOCUMENT(iter)) {
        CLIENT_ERR("invalid 'masterKey', expected document");
        return false;
    }

    if (!mc_iter_document_as_bson(iter, &kek_doc, status)) {
        return false;
    }

    if (!_mongocrypt_kek_parse_owned(&kek_doc, &out->kek, status)) {
        return false;
    }
    return true;
}

bool _mongocrypt_key_alt_name_from_iter(const bson_iter_t *iter_in,
                                        _mongocrypt_key_alt_name_t **out,
                                        mongocrypt_status_t *status) {
    _mongocrypt_key_alt_name_t *key_alt_names = NULL, *tmp;
    bson_iter_t iter;

    BSON_ASSERT_PARAM(iter_in);
    BSON_ASSERT_PARAM(out);

    memcpy(&iter, iter_in, sizeof(iter));
    *out = NULL;

    /* A key parsed with no keyAltNames will have a zero'ed out bson value. Not
     * an error. */
    if (!BSON_ITER_HOLDS_ARRAY(&iter)) {
        CLIENT_ERR("malformed keyAltNames, expected array");
        return false;
    }

    if (!bson_iter_recurse(&iter, &iter)) {
        CLIENT_ERR("malformed keyAltNames, could not recurse into array");
        return false;
    }

    while (bson_iter_next(&iter)) {
        if (!BSON_ITER_HOLDS_UTF8(&iter)) {
            _mongocrypt_key_alt_name_destroy_all(key_alt_names);
            CLIENT_ERR("unexpected non-UTF8 keyAltName");
            return false;
        }

        tmp = _mongocrypt_key_alt_name_new(bson_iter_value(&iter));
        tmp->next = key_alt_names;
        key_alt_names = tmp;
    }

    if (!_check_unique(key_alt_names)) {
        _mongocrypt_key_alt_name_destroy_all(key_alt_names);
        CLIENT_ERR("unexpected duplicate keyAltNames");
        return false;
    }

    *out = key_alt_names;
    return true;
}

/* Takes ownership of all fields. */
bool _mongocrypt_key_parse_owned(const bson_t *bson, _mongocrypt_key_doc_t *out, mongocrypt_status_t *status) {
    bson_iter_t iter = {0};
    bool has_id = false, has_key_material = false, has_status = false, has_creation_date = false,
         has_update_date = false, has_master_key = false;

    BSON_ASSERT_PARAM(bson);
    BSON_ASSERT_PARAM(out);

    if (!bson_validate(bson, BSON_VALIDATE_NONE, NULL) || !bson_iter_init(&iter, bson)) {
        CLIENT_ERR("invalid BSON");
        return false;
    }

    bson_destroy(&out->bson);
    bson_copy_to(bson, &out->bson);

    while (bson_iter_next(&iter)) {
        const char *field;

        field = bson_iter_key(&iter);
        if (!field) {
            CLIENT_ERR("invalid BSON, could not retrieve field name");
            return false;
        }
        if (0 == strcmp("_id", field)) {
            has_id = true;
            if (!_mongocrypt_buffer_copy_from_uuid_iter(&out->id, &iter)) {
                CLIENT_ERR("invalid key, '_id' is not a UUID");
                return false;
            }
            continue;
        }

        /* keyAltNames (optional) */
        if (0 == strcmp("keyAltNames", field)) {
            if (!_mongocrypt_key_alt_name_from_iter(&iter, &out->key_alt_names, status)) {
                return false;
            }
            continue;
        }

        if (0 == strcmp("keyMaterial", field)) {
            has_key_material = true;
            if (!_mongocrypt_buffer_copy_from_binary_iter(&out->key_material, &iter)) {
                CLIENT_ERR("invalid 'keyMaterial', expected binary");
                return false;
            }
            if (out->key_material.subtype != BSON_SUBTYPE_BINARY) {
                CLIENT_ERR("invalid 'keyMaterial', expected subtype 0");
                return false;
            }
            continue;
        }

        if (0 == strcmp("masterKey", field)) {
            has_master_key = true;
            if (!_parse_masterkey(&iter, out, status)) {
                return false;
            }
            continue;
        }

        if (0 == strcmp("version", field)) {
            if (!BSON_ITER_HOLDS_INT(&iter)) {
                CLIENT_ERR("invalid 'version', expect int");
                return false;
            }
            if (bson_iter_as_int64(&iter) != 0) {
                CLIENT_ERR("unsupported key document version, only supports version=0");
                return false;
            }
            continue;
        }

        if (0 == strcmp("status", field)) {
            /* Don't need status. Check that it's present and ignore it. */
            has_status = true;
            continue;
        }

        if (0 == strcmp("creationDate", field)) {
            has_creation_date = true;

            if (!BSON_ITER_HOLDS_DATE_TIME(&iter)) {
                CLIENT_ERR("invalid 'creationDate', expect datetime");
                return false;
            }

            out->creation_date = bson_iter_date_time(&iter);
            continue;
        }

        if (0 == strcmp("updateDate", field)) {
            has_update_date = true;

            if (!BSON_ITER_HOLDS_DATE_TIME(&iter)) {
                CLIENT_ERR("invalid 'updateDate', expect datetime");
                return false;
            }

            out->update_date = bson_iter_date_time(&iter);
            continue;
        }

        CLIENT_ERR("unrecognized field '%s'", field);
        return false;
    }

    /* Check that required fields were set. */
    if (!has_id) {
        CLIENT_ERR("invalid key, no '_id'");
        return false;
    }

    if (!has_master_key) {
        CLIENT_ERR("invalid key, no 'masterKey'");
        return false;
    }

    if (!has_key_material) {
        CLIENT_ERR("invalid key, no 'keyMaterial'");
        return false;
    }

    if (!has_status) {
        CLIENT_ERR("invalid key, no 'status'");
        return false;
    }

    if (!has_creation_date) {
        CLIENT_ERR("invalid key, no 'creationDate'");
        return false;
    }

    if (!has_update_date) {
        CLIENT_ERR("invalid key, no 'updateDate'");
        return false;
    }

    return true;
}

_mongocrypt_key_doc_t *_mongocrypt_key_new(void) {
    _mongocrypt_key_doc_t *key_doc;

    key_doc = BSON_ALIGNED_ALLOC(_mongocrypt_key_doc_t);
    // Use two sets of braces to avoid erroneous missing-braces warning in GCC. Refer:
    // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119
    *key_doc = (_mongocrypt_key_doc_t){{0}};
    bson_init(&key_doc->bson);

    return key_doc;
}

void _mongocrypt_key_destroy(_mongocrypt_key_doc_t *key) {
    if (!key) {
        return;
    }

    _mongocrypt_buffer_cleanup(&key->id);
    _mongocrypt_key_alt_name_destroy_all(key->key_alt_names);
    _mongocrypt_buffer_cleanup(&key->key_material);
    _mongocrypt_kek_cleanup(&key->kek);

    bson_destroy(&key->bson);
    bson_free(key);
}

void _mongocrypt_key_doc_copy_to(_mongocrypt_key_doc_t *src, _mongocrypt_key_doc_t *dst) {
    BSON_ASSERT_PARAM(src);
    BSON_ASSERT_PARAM(dst);

    _mongocrypt_buffer_copy_to(&src->id, &dst->id);
    _mongocrypt_buffer_copy_to(&src->key_material, &dst->key_material);
    dst->key_alt_names = _mongocrypt_key_alt_name_copy_all(src->key_alt_names);
    bson_destroy(&dst->bson);
    bson_copy_to(&src->bson, &dst->bson);
    _mongocrypt_kek_copy_to(&src->kek, &dst->kek);
    dst->creation_date = src->creation_date;
    dst->update_date = src->update_date;
}

_mongocrypt_key_alt_name_t *_mongocrypt_key_alt_name_copy_all(_mongocrypt_key_alt_name_t *ptr) {
    _mongocrypt_key_alt_name_t *ptr_copy = NULL, *head = NULL;

    while (ptr) {
        _mongocrypt_key_alt_name_t *copied;
        copied = bson_malloc0(sizeof(*copied));
        BSON_ASSERT(copied);

        bson_value_copy(&ptr->value, &copied->value);

        if (!ptr_copy) {
            ptr_copy = copied;
            head = ptr_copy;
        } else {
            ptr_copy->next = copied;
            ptr_copy = ptr_copy->next;
        }
        ptr = ptr->next;
    }
    return head;
}

void _mongocrypt_key_alt_name_destroy_all(_mongocrypt_key_alt_name_t *ptr) {
    _mongocrypt_key_alt_name_t *next;
    while (ptr) {
        next = ptr->next;
        bson_value_destroy(&ptr->value);
        bson_free(ptr);
        ptr = next;
    }
}

bool _mongocrypt_key_alt_name_intersects(_mongocrypt_key_alt_name_t *ptr_a, _mongocrypt_key_alt_name_t *ptr_b) {
    _mongocrypt_key_alt_name_t *orig_ptr_b = ptr_b;

    if (!ptr_a || !ptr_b) {
        return false;
    }

    for (; ptr_a; ptr_a = ptr_a->next) {
        for (ptr_b = orig_ptr_b; ptr_b; ptr_b = ptr_b->next) {
            if (_one_key_alt_name_equal(ptr_a, ptr_b)) {
                return true;
            }
        }
    }
    return false;
}

_mongocrypt_key_alt_name_t *_mongocrypt_key_alt_name_create(const char *name, ...) {
    va_list args;
    const char *arg_ptr;
    _mongocrypt_key_alt_name_t *head, *prev;

    head = NULL;
    prev = NULL;
    va_start(args, name);
    arg_ptr = name;
    while (arg_ptr) {
        _mongocrypt_key_alt_name_t *curr;

        curr = bson_malloc0(sizeof(*curr));
        BSON_ASSERT(curr);

        curr->value.value_type = BSON_TYPE_UTF8;
        curr->value.value.v_utf8.str = bson_strdup(arg_ptr);
        curr->value.value.v_utf8.len = (uint32_t)strlen(arg_ptr);
        if (!prev) {
            head = curr;
        } else {
            prev->next = curr;
        }

        arg_ptr = va_arg(args, const char *);
        prev = curr;
    }
    va_end(args);

    return head;
}

_mongocrypt_key_alt_name_t *_mongocrypt_key_alt_name_new(const bson_value_t *value) {
    BSON_ASSERT_PARAM(value);

    _mongocrypt_key_alt_name_t *name = BSON_ALIGNED_ALLOC(_mongocrypt_key_alt_name_t);
    *name = (_mongocrypt_key_alt_name_t){0};
    BSON_ASSERT(name);

    bson_value_copy(value, &name->value);
    return name;
}

bool _mongocrypt_key_alt_name_unique_list_equal(_mongocrypt_key_alt_name_t *list_a,
                                                _mongocrypt_key_alt_name_t *list_b) {
    _mongocrypt_key_alt_name_t *ptr;

    BSON_ASSERT(_check_unique(list_a));
    BSON_ASSERT(_check_unique(list_b));
    if (_list_len(list_a) != _list_len(list_b)) {
        return false;
    }
    for (ptr = list_a; NULL != ptr; ptr = ptr->next) {
        if (!_find(list_b, ptr)) {
            return false;
        }
    }
    return true;
}

const char *_mongocrypt_key_alt_name_get_string(_mongocrypt_key_alt_name_t *key_alt_name) {
    BSON_ASSERT_PARAM(key_alt_name);

    return key_alt_name->value.value.v_utf8.str;
}