Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/mc-fle2-insert-update-payload-private-v2.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
_mongocrypt_buffer_t encryptedTokens; \
} mc_Text##Type##TokenSet_t; \
void mc_Text##Type##TokenSet_init(mc_Text##Type##TokenSet_t *); \
void mc_Text##Type##TokenSet_cleanup(mc_Text##Type##TokenSet_t *)
void mc_Text##Type##TokenSet_cleanup(mc_Text##Type##TokenSet_t *); \
void mc_Text##Type##TokenSet_copy(const mc_Text##Type##TokenSet_t *src, mc_Text##Type##TokenSet_t *dest)

DEF_TEXT_SEARCH_TOKEN_SET(Exact);
DEF_TEXT_SEARCH_TOKEN_SET(Substring);
Expand Down
8 changes: 8 additions & 0 deletions src/mc-fle2-insert-update-payload-v2.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
_mongocrypt_buffer_cleanup(&ts->escDerivedToken); \
_mongocrypt_buffer_cleanup(&ts->serverDerivedFromDataToken); \
_mongocrypt_buffer_cleanup(&ts->encryptedTokens); \
} \
void mc_Text##Type##TokenSet_copy(const mc_Text##Type##TokenSet_t *src, mc_Text##Type##TokenSet_t *dst) { \
BSON_ASSERT_PARAM(src); \
BSON_ASSERT_PARAM(dst); \
_mongocrypt_buffer_copy_to(&src->edcDerivedToken, &dst->edcDerivedToken); \
_mongocrypt_buffer_copy_to(&src->escDerivedToken, &dst->escDerivedToken); \
_mongocrypt_buffer_copy_to(&src->serverDerivedFromDataToken, &dst->serverDerivedFromDataToken); \
_mongocrypt_buffer_copy_to(&src->encryptedTokens, &dst->encryptedTokens); \
}

DEF_TEXT_SEARCH_TOKEN_SET_INIT_CLEANUP(Exact)
Expand Down
8 changes: 4 additions & 4 deletions src/mongocrypt-buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,23 +349,23 @@ static void _mongocrypt_buffer_copy_as_bson_value(_mongocrypt_buffer_t *plaintex
bson_destroy(&wrapper);
}

bool append_iter(bson_t *bson, const void *iter, int len) {
static bool _append_iter(bson_t *bson, const void *iter, int len) {
return bson_append_iter(bson, "", 0, (const bson_iter_t *)iter);
}

bool append_utf8(bson_t *bson, const void *str, int len) {
static bool _append_utf8(bson_t *bson, const void *str, int len) {
return bson_append_utf8(bson, "", 0, (const char *)str, len);
}

void _mongocrypt_buffer_copy_from_string_as_bson_value(_mongocrypt_buffer_t *plaintext, const char *str, int len) {
BSON_ASSERT_PARAM(str);
BSON_ASSERT(len >= 0);
_mongocrypt_buffer_copy_as_bson_value(plaintext, append_utf8, str, len);
_mongocrypt_buffer_copy_as_bson_value(plaintext, _append_utf8, str, len);
}

void _mongocrypt_buffer_from_iter(_mongocrypt_buffer_t *plaintext, bson_iter_t *iter) {
BSON_ASSERT_PARAM(iter);
_mongocrypt_buffer_copy_as_bson_value(plaintext, append_iter, iter, 0);
_mongocrypt_buffer_copy_as_bson_value(plaintext, _append_iter, iter, 0);
}

bool _mongocrypt_buffer_from_uuid_iter(_mongocrypt_buffer_t *buf, bson_iter_t *iter) {
Expand Down
147 changes: 84 additions & 63 deletions src/mongocrypt-marking.c
Original file line number Diff line number Diff line change
Expand Up @@ -1192,29 +1192,36 @@ static bool _fle2_generate_TextSearchTokenSets(_mongocrypt_key_broker_t *kb,
mc_substring_set_iter_init(&set_itr, encodeSets->substring_set);

while (mc_substring_set_iter_next(&set_itr, &substring, &bytelen, &appendCount)) {
for (; appendCount > 0; appendCount--) {
mc_TextSubstringTokenSet_t tset = {{0}};
mc_TextSubstringTokenSet_init(&tset);

_mongocrypt_buffer_t asBsonValue;
_mongocrypt_buffer_init(&asBsonValue);
BSON_ASSERT(bytelen < INT_MAX);
_mongocrypt_buffer_copy_from_string_as_bson_value(&asBsonValue, substring, (int)bytelen);

if (!_fle2_generate_TextSubstringTokenSet(kb,
&tset,
&asBsonValue,
contentionFactor,
common.collectionsLevel1Token,
common.serverTokenDerivationLevel1Token,
status)) {
_mongocrypt_buffer_cleanup(&asBsonValue);
mc_TextSubstringTokenSet_cleanup(&tset);
goto fail;
}
_mc_array_append_val(&tsts->substringArray, tset); // moves ownership of tset
BSON_ASSERT(appendCount > 0);
BSON_ASSERT(bytelen < INT_MAX);

mc_TextSubstringTokenSet_t tset = {{0}};
mc_TextSubstringTokenSet_init(&tset);

_mongocrypt_buffer_t asBsonValue;
_mongocrypt_buffer_init(&asBsonValue);
_mongocrypt_buffer_copy_from_string_as_bson_value(&asBsonValue, substring, (int)bytelen);

if (!_fle2_generate_TextSubstringTokenSet(kb,
&tset,
&asBsonValue,
contentionFactor,
common.collectionsLevel1Token,
common.serverTokenDerivationLevel1Token,
status)) {
_mongocrypt_buffer_cleanup(&asBsonValue);
mc_TextSubstringTokenSet_cleanup(&tset);
goto fail;
}
_mongocrypt_buffer_cleanup(&asBsonValue);

for (; appendCount > 1; appendCount--) {
mc_TextSubstringTokenSet_t tset_copy;
mc_TextSubstringTokenSet_init(&tset_copy);
mc_TextSubstringTokenSet_copy(&tset, &tset_copy);
_mc_array_append_val(&tsts->substringArray, tset_copy); // array now owns tset_copy
}
_mc_array_append_val(&tsts->substringArray, tset); // array now owns tset
}
}

Expand All @@ -1224,29 +1231,36 @@ static bool _fle2_generate_TextSearchTokenSets(_mongocrypt_key_broker_t *kb,
mc_affix_set_iter_init(&set_itr, encodeSets->suffix_set);

while (mc_affix_set_iter_next(&set_itr, &substring, &bytelen, &appendCount)) {
for (; appendCount > 0; appendCount--) {
mc_TextSuffixTokenSet_t tset = {{0}};
mc_TextSuffixTokenSet_init(&tset);

_mongocrypt_buffer_t asBsonValue;
_mongocrypt_buffer_init(&asBsonValue);
BSON_ASSERT(bytelen < INT_MAX);
_mongocrypt_buffer_copy_from_string_as_bson_value(&asBsonValue, substring, (int)bytelen);

if (!_fle2_generate_TextSuffixTokenSet(kb,
&tset,
&asBsonValue,
contentionFactor,
common.collectionsLevel1Token,
common.serverTokenDerivationLevel1Token,
status)) {
_mongocrypt_buffer_cleanup(&asBsonValue);
mc_TextSuffixTokenSet_cleanup(&tset);
goto fail;
}
_mc_array_append_val(&tsts->suffixArray, tset); // moves ownership of tset
BSON_ASSERT(appendCount > 0);
BSON_ASSERT(bytelen < INT_MAX);

mc_TextSuffixTokenSet_t tset = {{0}};
mc_TextSuffixTokenSet_init(&tset);

_mongocrypt_buffer_t asBsonValue;
_mongocrypt_buffer_init(&asBsonValue);
_mongocrypt_buffer_copy_from_string_as_bson_value(&asBsonValue, substring, (int)bytelen);

if (!_fle2_generate_TextSuffixTokenSet(kb,
&tset,
&asBsonValue,
contentionFactor,
common.collectionsLevel1Token,
common.serverTokenDerivationLevel1Token,
status)) {
_mongocrypt_buffer_cleanup(&asBsonValue);
mc_TextSuffixTokenSet_cleanup(&tset);
goto fail;
}
_mongocrypt_buffer_cleanup(&asBsonValue);

for (; appendCount > 1; appendCount--) {
mc_TextSuffixTokenSet_t tset_copy;
mc_TextSuffixTokenSet_init(&tset_copy);
mc_TextSuffixTokenSet_copy(&tset, &tset_copy);
_mc_array_append_val(&tsts->suffixArray, tset_copy); // array now owns tset_copy
}
_mc_array_append_val(&tsts->suffixArray, tset); // array now owns tset
}
}

Expand All @@ -1256,29 +1270,36 @@ static bool _fle2_generate_TextSearchTokenSets(_mongocrypt_key_broker_t *kb,
mc_affix_set_iter_init(&set_itr, encodeSets->prefix_set);

while (mc_affix_set_iter_next(&set_itr, &substring, &bytelen, &appendCount)) {
for (; appendCount > 0; appendCount--) {
mc_TextPrefixTokenSet_t tset = {{0}};
mc_TextPrefixTokenSet_init(&tset);

_mongocrypt_buffer_t asBsonValue;
_mongocrypt_buffer_init(&asBsonValue);
BSON_ASSERT(bytelen < INT_MAX);
_mongocrypt_buffer_copy_from_string_as_bson_value(&asBsonValue, substring, (int)bytelen);

if (!_fle2_generate_TextPrefixTokenSet(kb,
&tset,
&asBsonValue,
contentionFactor,
common.collectionsLevel1Token,
common.serverTokenDerivationLevel1Token,
status)) {
_mongocrypt_buffer_cleanup(&asBsonValue);
mc_TextPrefixTokenSet_cleanup(&tset);
goto fail;
}
_mc_array_append_val(&tsts->prefixArray, tset); // moves ownership of tset
BSON_ASSERT(appendCount > 0);
BSON_ASSERT(bytelen < INT_MAX);

mc_TextPrefixTokenSet_t tset = {{0}};
mc_TextPrefixTokenSet_init(&tset);

_mongocrypt_buffer_t asBsonValue;
_mongocrypt_buffer_init(&asBsonValue);
_mongocrypt_buffer_copy_from_string_as_bson_value(&asBsonValue, substring, (int)bytelen);

if (!_fle2_generate_TextPrefixTokenSet(kb,
&tset,
&asBsonValue,
contentionFactor,
common.collectionsLevel1Token,
common.serverTokenDerivationLevel1Token,
status)) {
_mongocrypt_buffer_cleanup(&asBsonValue);
mc_TextPrefixTokenSet_cleanup(&tset);
goto fail;
}
_mongocrypt_buffer_cleanup(&asBsonValue);

for (; appendCount > 1; appendCount--) {
mc_TextPrefixTokenSet_t tset_copy;
mc_TextPrefixTokenSet_init(&tset_copy);
mc_TextPrefixTokenSet_copy(&tset, &tset_copy);
_mc_array_append_val(&tsts->prefixArray, tset_copy); // array now owns tset_copy
}
_mc_array_append_val(&tsts->prefixArray, tset); // moves ownership of tset
}
}
payload->textSearchTokenSets.set = true;
Expand Down
6 changes: 3 additions & 3 deletions test/test-mongocrypt-marking.c
Original file line number Diff line number Diff line change
Expand Up @@ -1345,15 +1345,15 @@ static void validate_text_search_ciphertext(_mongocrypt_tester_t *tester,
size_t tscount = 0;
ASSERT(bson_iter_init_find(&b_iter, &b_bson, "s"));
tscount = validate_text_search_token_set_array_common(&b_iter, crypt);
ASSERT(expected_tag_counts.substrings == tscount);
ASSERT_CMPSIZE_T(expected_tag_counts.substrings, ==, tscount);

ASSERT(bson_iter_init_find(&b_iter, &b_bson, "u"));
tscount = validate_text_search_token_set_array_common(&b_iter, crypt);
ASSERT(expected_tag_counts.suffixes == tscount);
ASSERT_CMPSIZE_T(expected_tag_counts.suffixes, ==, tscount);

ASSERT(bson_iter_init_find(&b_iter, &b_bson, "p"));
tscount = validate_text_search_token_set_array_common(&b_iter, crypt);
ASSERT(expected_tag_counts.prefixes == tscount);
ASSERT_CMPSIZE_T(expected_tag_counts.prefixes, ==, tscount);
}

mc_ServerDataEncryptionLevel1Token_destroy(sdel1Token);
Expand Down