-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathmc-fle2-find-equality-payload-v2.c
More file actions
127 lines (107 loc) · 5.79 KB
/
mc-fle2-find-equality-payload-v2.c
File metadata and controls
127 lines (107 loc) · 5.79 KB
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
/*
* Copyright 2022-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 <bson/bson.h>
#include "mc-fle2-encryption-placeholder-private.h"
#include "mc-fle2-find-equality-payload-private-v2.h"
#include "mc-parse-utils-private.h"
#include "mongocrypt-buffer-private.h"
#include "mongocrypt.h"
void mc_FLE2FindEqualityPayloadV2_init(mc_FLE2FindEqualityPayloadV2_t *payload) {
memset(payload, 0, sizeof(mc_FLE2FindEqualityPayloadV2_t));
}
void mc_FLE2FindEqualityPayloadV2_cleanup(mc_FLE2FindEqualityPayloadV2_t *payload) {
BSON_ASSERT_PARAM(payload);
_mongocrypt_buffer_cleanup(&payload->edcDerivedToken);
_mongocrypt_buffer_cleanup(&payload->escDerivedToken);
_mongocrypt_buffer_cleanup(&payload->serverDerivedFromDataToken);
}
#define IF_FIELD(Name) \
if (0 == strcmp(field, #Name)) { \
if (has_##Name) { \
CLIENT_ERR("Duplicate field '" #Name "' in payload bson"); \
goto fail; \
} \
has_##Name = true;
#define END_IF_FIELD \
continue; \
}
#define PARSE_BINARY(Name, Dest) \
IF_FIELD(Name) { \
if (!parse_bindata(BSON_SUBTYPE_BINARY, &iter, &out->Dest, status)) { \
goto fail; \
} \
} \
END_IF_FIELD
#define CHECK_HAS(Name) \
if (!has_##Name) { \
CLIENT_ERR("Missing field '" #Name "' in payload"); \
goto fail; \
} else \
((void)0)
bool mc_FLE2FindEqualityPayloadV2_parse(mc_FLE2FindEqualityPayloadV2_t *out,
const bson_t *in,
mongocrypt_status_t *status) {
bson_iter_t iter;
bool has_d = false, has_s = false, has_l = false, has_cm = false;
BSON_ASSERT_PARAM(out);
BSON_ASSERT_PARAM(in);
mc_FLE2FindEqualityPayloadV2_init(out);
if (!bson_validate(in, BSON_VALIDATE_NONE, NULL) || !bson_iter_init(&iter, in)) {
CLIENT_ERR("invalid BSON");
return false;
}
while (bson_iter_next(&iter)) {
const char *field = bson_iter_key(&iter);
BSON_ASSERT(field);
PARSE_BINARY(d, edcDerivedToken)
PARSE_BINARY(s, escDerivedToken)
PARSE_BINARY(l, serverDerivedFromDataToken)
IF_FIELD(cm) {
if (!BSON_ITER_HOLDS_INT64(&iter)) {
CLIENT_ERR("Field 'cm' expected to hold an int64");
goto fail;
}
out->maxContentionFactor = bson_iter_int64(&iter);
if (!mc_validate_contention(out->maxContentionFactor, status)) {
goto fail;
}
}
END_IF_FIELD
}
CHECK_HAS(d);
CHECK_HAS(s);
CHECK_HAS(l);
CHECK_HAS(cm);
return true;
fail:
return false;
}
#define APPEND_BINDATA(name, value) \
if (!_mongocrypt_buffer_append(&(value), out, name, -1)) { \
return false; \
} else \
((void)0)
bool mc_FLE2FindEqualityPayloadV2_serialize(const mc_FLE2FindEqualityPayloadV2_t *payload, bson_t *out) {
BSON_ASSERT_PARAM(payload);
APPEND_BINDATA("d", payload->edcDerivedToken);
APPEND_BINDATA("s", payload->escDerivedToken);
APPEND_BINDATA("l", payload->serverDerivedFromDataToken);
if (!BSON_APPEND_INT64(out, "cm", payload->maxContentionFactor)) {
return false;
}
return true;
}
#undef APPEND_BINDATA