forked from mongodb/libmongocrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-mongocrypt-ctx-decrypt.c
More file actions
213 lines (179 loc) · 6.91 KB
/
test-mongocrypt-ctx-decrypt.c
File metadata and controls
213 lines (179 loc) · 6.91 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
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
/*
* 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-ctx-private.h"
#include "mongocrypt.h"
#include "test-mongocrypt.h"
static void
_test_explicit_decrypt_init (_mongocrypt_tester_t *tester)
{
mongocrypt_t *crypt;
mongocrypt_ctx_t *ctx;
mongocrypt_binary_t *msg;
crypt = _mongocrypt_tester_mongocrypt ();
msg = TEST_BSON ("{ 'v': { '$binary': { 'subType': '06', 'base64': "
"'AWFhYWFhYWFhYWFhYWFhYWECRTOW9yZzNDn5dGwuqsrJQNLtgMEKaujhs"
"9aRWRp+7Yo3JK8N8jC8P0Xjll6C1CwLsE/"
"iP5wjOMhVv1KMMyOCSCrHorXRsb2IKPtzl2lKTqQ=' } } }");
/* NULL document. */
ctx = mongocrypt_ctx_new (crypt);
ASSERT_FAILS (
mongocrypt_ctx_explicit_decrypt_init (ctx, NULL), ctx, "invalid msg");
BSON_ASSERT (mongocrypt_ctx_state (ctx) == MONGOCRYPT_CTX_ERROR);
mongocrypt_ctx_destroy (ctx);
/* Success. */
ctx = mongocrypt_ctx_new (crypt);
ASSERT_OK (mongocrypt_ctx_explicit_decrypt_init (ctx, msg), ctx);
BSON_ASSERT (mongocrypt_ctx_state (ctx) == MONGOCRYPT_CTX_NEED_MONGO_KEYS);
mongocrypt_ctx_destroy (ctx);
mongocrypt_destroy (crypt);
}
/* Test individual ctx states. */
static void
_test_decrypt_init (_mongocrypt_tester_t *tester)
{
mongocrypt_t *crypt;
mongocrypt_ctx_t *ctx;
mongocrypt_binary_t *encrypted;
crypt = _mongocrypt_tester_mongocrypt ();
encrypted = _mongocrypt_tester_encrypted_doc (tester);
/* Success. */
ctx = mongocrypt_ctx_new (crypt);
ASSERT_OK (mongocrypt_ctx_decrypt_init (ctx, encrypted), ctx);
BSON_ASSERT (mongocrypt_ctx_state (ctx) == MONGOCRYPT_CTX_NEED_MONGO_KEYS);
mongocrypt_ctx_destroy (ctx);
/* NULL document. */
ctx = mongocrypt_ctx_new (crypt);
ASSERT_FAILS (mongocrypt_ctx_decrypt_init (ctx, NULL), ctx, "invalid doc");
BSON_ASSERT (mongocrypt_ctx_state (ctx) == MONGOCRYPT_CTX_ERROR);
mongocrypt_ctx_destroy (ctx);
mongocrypt_binary_destroy (encrypted);
mongocrypt_destroy (crypt);
}
static void
_test_decrypt_need_keys (_mongocrypt_tester_t *tester)
{
mongocrypt_t *crypt;
mongocrypt_ctx_t *ctx;
mongocrypt_binary_t *encrypted;
encrypted = _mongocrypt_tester_encrypted_doc (tester);
/* Success. */
crypt = _mongocrypt_tester_mongocrypt ();
ctx = mongocrypt_ctx_new (crypt);
ASSERT_OK (mongocrypt_ctx_decrypt_init (ctx, encrypted), ctx);
ASSERT_OK (mongocrypt_ctx_mongo_feed (
ctx, TEST_FILE ("./test/example/key-document.json")),
ctx);
ASSERT_OK (mongocrypt_ctx_mongo_done (ctx), ctx);
BSON_ASSERT (mongocrypt_ctx_state (ctx) == MONGOCRYPT_CTX_NEED_KMS);
mongocrypt_ctx_destroy (ctx);
mongocrypt_destroy (crypt); /* recreate crypt because of caching. */
/* TODO: CDRIVER-3044 test that decryption warns when keys are not
* found/inactive. */
mongocrypt_binary_destroy (encrypted);
}
static void
_test_decrypt_ready (_mongocrypt_tester_t *tester)
{
mongocrypt_t *crypt;
mongocrypt_ctx_t *ctx;
mongocrypt_binary_t *encrypted, *decrypted;
bson_t as_bson;
bson_iter_t iter;
encrypted = _mongocrypt_tester_encrypted_doc (tester);
decrypted = mongocrypt_binary_new ();
crypt = _mongocrypt_tester_mongocrypt ();
/* Success. */
ctx = mongocrypt_ctx_new (crypt);
ASSERT_OK (mongocrypt_ctx_decrypt_init (ctx, encrypted), ctx);
_mongocrypt_tester_run_ctx_to (tester, ctx, MONGOCRYPT_CTX_READY);
ASSERT_OK (mongocrypt_ctx_finalize (ctx, decrypted), ctx);
BSON_ASSERT (_mongocrypt_binary_to_bson (decrypted, &as_bson));
bson_iter_init (&iter, &as_bson);
bson_iter_find_descendant (&iter, "filter.ssn", &iter);
BSON_ASSERT (BSON_ITER_HOLDS_UTF8 (&iter));
BSON_ASSERT (0 == strcmp (bson_iter_utf8 (&iter, NULL),
_mongocrypt_tester_plaintext (tester)));
mongocrypt_binary_destroy (decrypted);
mongocrypt_ctx_destroy (ctx);
mongocrypt_destroy (crypt);
mongocrypt_binary_destroy (encrypted);
}
/* Test with empty AWS credentials. */
void
_test_decrypt_empty_aws (_mongocrypt_tester_t *tester)
{
mongocrypt_t *crypt;
mongocrypt_ctx_t *ctx;
crypt = mongocrypt_new ();
ASSERT_OK (mongocrypt_setopt_kms_provider_aws (crypt, "", -1, "", -1),
crypt);
ASSERT_OK (mongocrypt_init (crypt), crypt);
ctx = mongocrypt_ctx_new (crypt);
ASSERT_OK (mongocrypt_ctx_decrypt_init (
ctx, TEST_FILE ("./test/data/encrypted-cmd.json")),
ctx);
_mongocrypt_tester_run_ctx_to (tester, ctx, MONGOCRYPT_CTX_NEED_MONGO_KEYS);
ASSERT_FAILS (mongocrypt_ctx_mongo_feed (
ctx, TEST_FILE ("./test/example/key-document.json")),
ctx,
"failed to create KMS message");
mongocrypt_ctx_destroy (ctx);
mongocrypt_destroy (crypt);
}
static void
_test_decrypt_empty_binary (_mongocrypt_tester_t *tester)
{
mongocrypt_t *crypt;
mongocrypt_ctx_t *ctx;
mongocrypt_binary_t *bin;
_mongocrypt_buffer_t encrypted;
bin = mongocrypt_binary_new ();
crypt = _mongocrypt_tester_mongocrypt ();
ctx = mongocrypt_ctx_new (crypt);
/* Encrypt an empty binary value. */
mongocrypt_ctx_setopt_key_alt_name (
ctx, TEST_BSON ("{'keyAltName': 'keyDocumentName'}"));
mongocrypt_ctx_setopt_algorithm (
ctx, "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic", -1);
mongocrypt_ctx_explicit_encrypt_init (
ctx,
TEST_BSON ("{'v': { '$binary': { 'base64': '', 'subType': '00' } } }"));
_mongocrypt_tester_run_ctx_to (tester, ctx, MONGOCRYPT_CTX_READY);
mongocrypt_ctx_finalize (ctx, bin);
/* Copy the encrypted ciphertext since it is tied to the lifetime of ctx. */
_mongocrypt_buffer_copy_from_binary (&encrypted, bin);
mongocrypt_ctx_destroy (ctx);
/* Decrypt it back. */
ctx = mongocrypt_ctx_new (crypt);
mongocrypt_ctx_explicit_decrypt_init (
ctx, _mongocrypt_buffer_as_binary (&encrypted));
_mongocrypt_tester_run_ctx_to (tester, ctx, MONGOCRYPT_CTX_READY);
mongocrypt_ctx_finalize (ctx, bin);
mongocrypt_binary_destroy (bin);
mongocrypt_ctx_destroy (ctx);
_mongocrypt_buffer_cleanup (&encrypted);
mongocrypt_destroy (crypt);
}
void
_mongocrypt_tester_install_ctx_decrypt (_mongocrypt_tester_t *tester)
{
INSTALL_TEST (_test_explicit_decrypt_init);
INSTALL_TEST (_test_decrypt_init);
INSTALL_TEST (_test_decrypt_need_keys);
INSTALL_TEST (_test_decrypt_ready);
INSTALL_TEST (_test_decrypt_empty_aws);
INSTALL_TEST (_test_decrypt_empty_binary);
}