libmongocrypt-helper 1.7.4.0.1002 → 1.11.0.0.1001

Sign up to get free protection for your applications and to get access to all the features.
Files changed (600) hide show
  1. checksums.yaml +4 -4
  2. data/ext/libmongocrypt/libmongocrypt/CHANGELOG.md +40 -5
  3. data/ext/libmongocrypt/libmongocrypt/CMakeLists.txt +29 -24
  4. data/ext/libmongocrypt/libmongocrypt/CODEOWNERS +1 -4
  5. data/ext/libmongocrypt/libmongocrypt/Earthfile +522 -0
  6. data/ext/libmongocrypt/libmongocrypt/README.md +36 -40
  7. data/ext/libmongocrypt/libmongocrypt/bindings/cs/CMakeLists.txt +1 -0
  8. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Driver.snk +0 -0
  9. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt/AssemblyInfo.cs +2 -2
  10. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt/Binary.cs +16 -2
  11. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt/CryptClient.cs +25 -6
  12. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt/CryptClientFactory.cs +8 -4
  13. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt/Library.cs +35 -3
  14. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt/LibraryLoader.cs +81 -44
  15. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt/MongoDB.Libmongocrypt.csproj +2 -1
  16. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt.Example/MongoDB.Libmongocrypt.Example.csproj +1 -1
  17. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt.Test/BasicTests.cs +1 -1
  18. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt.Test/MongoDB.Libmongocrypt.Test.csproj +2 -0
  19. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt.Test32/MongoDB.Libmongocrypt.Test32.csproj +2 -0
  20. data/ext/libmongocrypt/libmongocrypt/bindings/cs/README.md +3 -0
  21. data/ext/libmongocrypt/libmongocrypt/bindings/cs/Scripts/build.cake +22 -26
  22. data/ext/libmongocrypt/libmongocrypt/bindings/cs/Scripts/build.config +3 -0
  23. data/ext/libmongocrypt/libmongocrypt/bindings/cs/Scripts/build.sh +0 -0
  24. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/benchmarks/build.gradle.kts +28 -0
  25. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/benchmarks/src/main/java/com/mongodb/crypt/benchmark/BenchmarkRunner.java +217 -0
  26. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/benchmarks/src/main/resources/keyDocument.json +24 -0
  27. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/build.gradle.kts +22 -7
  28. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/gradle/wrapper/gradle-wrapper.jar +0 -0
  29. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/gradle/wrapper/gradle-wrapper.properties +1 -1
  30. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/gradlew +154 -108
  31. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/gradlew.bat +7 -18
  32. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/settings.gradle.kts +1 -0
  33. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/main/java/com/mongodb/crypt/capi/CAPI.java +41 -6
  34. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/main/java/com/mongodb/crypt/capi/CAPIHelper.java +5 -5
  35. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/main/java/com/mongodb/crypt/capi/CipherCallback.java +27 -1
  36. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/main/java/com/mongodb/crypt/capi/MongoCryptImpl.java +34 -19
  37. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/main/java/com/mongodb/crypt/capi/MongoExplicitEncryptOptions.java +6 -4
  38. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/main/resources/META-INF/native-image/jni-config.json +180 -0
  39. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/main/resources/META-INF/native-image/reflect-config.json +134 -0
  40. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/test/java/com/mongodb/crypt/capi/MongoCryptTest.java +50 -6
  41. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/test/resources/fle2-find-range-explicit-v2/int32/encrypted-payload.json +26 -0
  42. data/ext/libmongocrypt/libmongocrypt/bindings/{python/test/data/fle2-find-range-explicit → java/mongocrypt/src/test/resources/fle2-find-range-explicit-v2}/int32/rangeopts.json +3 -0
  43. data/ext/libmongocrypt/libmongocrypt/bindings/node/README.md +4 -771
  44. data/ext/libmongocrypt/libmongocrypt/bindings/python/CHANGELOG.rst +86 -0
  45. data/ext/libmongocrypt/libmongocrypt/bindings/python/README.rst +41 -20
  46. data/ext/libmongocrypt/libmongocrypt/bindings/python/RELEASE.rst +6 -24
  47. data/ext/libmongocrypt/libmongocrypt/bindings/python/build-manylinux-wheel.sh +4 -13
  48. data/ext/libmongocrypt/libmongocrypt/bindings/python/hatch_build.py +36 -0
  49. data/ext/libmongocrypt/libmongocrypt/bindings/python/libmongocrypt-version.txt +1 -0
  50. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/__init__.py +2 -2
  51. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/asynchronous/auto_encrypter.py +61 -0
  52. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/asynchronous/credentials.py +156 -0
  53. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/asynchronous/explicit_encrypter.py +156 -0
  54. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/asynchronous/state_machine.py +149 -0
  55. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/auto_encrypter.py +2 -46
  56. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/binary.py +14 -17
  57. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/binding.py +232 -296
  58. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/compat.py +6 -31
  59. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/credentials.py +2 -121
  60. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/crypto.py +31 -20
  61. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/errors.py +2 -2
  62. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/explicit_encrypter.py +2 -233
  63. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/mongocrypt.py +168 -254
  64. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/options.py +265 -0
  65. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/state_machine.py +2 -141
  66. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/synchronous/auto_encrypter.py +61 -0
  67. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/synchronous/credentials.py +156 -0
  68. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/synchronous/explicit_encrypter.py +156 -0
  69. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/synchronous/state_machine.py +149 -0
  70. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/version.py +2 -2
  71. data/ext/libmongocrypt/libmongocrypt/bindings/python/pyproject.toml +118 -0
  72. data/ext/libmongocrypt/libmongocrypt/bindings/python/release.sh +97 -61
  73. data/ext/libmongocrypt/libmongocrypt/bindings/python/requirements-test.txt +7 -0
  74. data/ext/libmongocrypt/libmongocrypt/bindings/python/requirements.txt +4 -0
  75. data/ext/libmongocrypt/libmongocrypt/bindings/python/sbom.json +76 -0
  76. data/ext/libmongocrypt/libmongocrypt/bindings/python/strip_header.py +6 -7
  77. data/ext/libmongocrypt/libmongocrypt/bindings/python/synchro.py +64 -0
  78. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/__init__.py +2 -10
  79. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/collection-info.json +1 -1
  80. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/command.json +1 -1
  81. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/compact/success/encrypted-payload.json +21 -21
  82. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/encrypted-command-reply.json +1 -1
  83. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/encrypted-field-config-map.json +1 -1
  84. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/fle2-find-range-explicit-v2/int32/encrypted-payload.json +26 -0
  85. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/fle2-find-range-explicit-v2/int32/rangeopts.json +14 -0
  86. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/key-document-azure.json +1 -1
  87. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/key-document-gcp.json +1 -1
  88. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/key-document.json +1 -1
  89. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/key-filter.json +1 -1
  90. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/list-collections-filter.json +1 -1
  91. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/mongocryptd-command.json +1 -1
  92. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/mongocryptd-reply.json +1 -1
  93. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/schema-map.json +1 -1
  94. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/performance/keyDocument.json +24 -0
  95. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/performance/perf_test.py +165 -0
  96. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/test_binding.py +8 -12
  97. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/test_crypto.py +9 -11
  98. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/test_mongocrypt.py +991 -343
  99. data/ext/libmongocrypt/libmongocrypt/bindings/python/update-sbom.sh +14 -0
  100. data/ext/libmongocrypt/libmongocrypt/cmake/FetchMongoC.cmake +19 -1
  101. data/ext/libmongocrypt/libmongocrypt/cmake/ImportBSON.cmake +31 -6
  102. data/ext/libmongocrypt/libmongocrypt/cmake/IntelDFP.cmake +20 -227
  103. data/ext/libmongocrypt/libmongocrypt/cmake/Patch.cmake +54 -0
  104. data/ext/libmongocrypt/libmongocrypt/cmake/Platform.cmake +50 -0
  105. data/ext/libmongocrypt/libmongocrypt/cmake/mongocrypt-config.cmake +21 -0
  106. data/ext/libmongocrypt/libmongocrypt/doc/img/cli-icon.png +0 -0
  107. data/ext/libmongocrypt/libmongocrypt/doc/img/reference-targets.png +0 -0
  108. data/ext/libmongocrypt/libmongocrypt/doc/releasing.md +153 -0
  109. data/ext/libmongocrypt/libmongocrypt/etc/c6-vault.repo +39 -0
  110. data/ext/libmongocrypt/libmongocrypt/etc/calc_release_version.py +61 -28
  111. data/ext/libmongocrypt/libmongocrypt/etc/calc_release_version_selftest.sh +73 -0
  112. data/ext/libmongocrypt/libmongocrypt/etc/cyclonedx.sbom.json +108 -0
  113. data/ext/libmongocrypt/libmongocrypt/etc/fle2_aead_generate_tests.py +15 -24
  114. data/ext/libmongocrypt/libmongocrypt/etc/fle2_crypto.py +66 -54
  115. data/ext/libmongocrypt/libmongocrypt/etc/fle2_generate_tests.py +14 -23
  116. data/ext/libmongocrypt/libmongocrypt/etc/fle2v2_aead_generate_tests.py +32 -0
  117. data/ext/libmongocrypt/libmongocrypt/etc/format-all.sh +12 -0
  118. data/ext/libmongocrypt/libmongocrypt/etc/format.sh +16 -0
  119. data/ext/libmongocrypt/libmongocrypt/etc/install-package.sh +48 -0
  120. data/ext/libmongocrypt/libmongocrypt/etc/libbson-remove-GCC-diagnostic-pragma.patch +27 -0
  121. data/ext/libmongocrypt/libmongocrypt/etc/mongo-inteldfp-alpine-arm-fix.patch +17 -0
  122. data/ext/libmongocrypt/libmongocrypt/etc/mongo-inteldfp-libmongocrypt-pr-625.patch +13 -0
  123. data/ext/libmongocrypt/libmongocrypt/etc/packager.py +120 -91
  124. data/ext/libmongocrypt/libmongocrypt/etc/purls.txt +14 -0
  125. data/ext/libmongocrypt/libmongocrypt/etc/repo_config.yaml +56 -0
  126. data/ext/libmongocrypt/libmongocrypt/etc/silk-create-asset-group.sh +70 -0
  127. data/ext/libmongocrypt/libmongocrypt/etc/ssdlc_compliance_report.md +37 -0
  128. data/ext/libmongocrypt/libmongocrypt/etc/third_party_vulnerabilities.md +42 -0
  129. data/ext/libmongocrypt/libmongocrypt/integrating.md +18 -1
  130. data/ext/libmongocrypt/libmongocrypt/kms-message/CMakeLists.txt +11 -3
  131. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_gcp_request.c +1 -1
  132. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_kmip_reader_writer.c +17 -0
  133. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_kmip_reader_writer_private.h +6 -0
  134. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_kmip_request.c +211 -1
  135. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_kmip_response.c +163 -0
  136. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_kmip_tag_type_private.h +2 -1
  137. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_message/kms_kmip_request.h +17 -0
  138. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_message/kms_kmip_response.h +6 -0
  139. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_port.c +3 -2
  140. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_request.c +4 -2
  141. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_request_str.c +2 -2
  142. data/ext/libmongocrypt/libmongocrypt/kms-message/test/test_kmip_reader_writer.c +23 -2
  143. data/ext/libmongocrypt/libmongocrypt/src/crypto/cng.c +381 -436
  144. data/ext/libmongocrypt/libmongocrypt/src/crypto/commoncrypto.c +162 -227
  145. data/ext/libmongocrypt/libmongocrypt/src/crypto/libcrypto.c +183 -248
  146. data/ext/libmongocrypt/libmongocrypt/src/crypto/none.c +33 -55
  147. data/ext/libmongocrypt/libmongocrypt/src/csfle-markup.cpp +175 -205
  148. data/ext/libmongocrypt/libmongocrypt/src/mc-array-private.h +10 -21
  149. data/ext/libmongocrypt/libmongocrypt/src/mc-array.c +44 -56
  150. data/ext/libmongocrypt/libmongocrypt/src/mc-check-conversions-private.h +4 -8
  151. data/ext/libmongocrypt/libmongocrypt/src/mc-dec128.h +351 -463
  152. data/ext/libmongocrypt/libmongocrypt/src/mc-dec128.test.cpp +59 -66
  153. data/ext/libmongocrypt/libmongocrypt/src/mc-efc-private.h +21 -11
  154. data/ext/libmongocrypt/libmongocrypt/src/mc-efc.c +175 -99
  155. data/ext/libmongocrypt/libmongocrypt/src/mc-fle-blob-subtype-private.h +18 -10
  156. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-encryption-placeholder-private.h +69 -70
  157. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-encryption-placeholder.c +445 -439
  158. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-equality-payload-private-v2.h +41 -0
  159. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-equality-payload-private.h +11 -18
  160. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-equality-payload-v2.c +135 -0
  161. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-equality-payload.c +109 -126
  162. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-range-payload-private-v2.h +103 -0
  163. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-range-payload-private.h +28 -31
  164. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-range-payload-v2.c +173 -0
  165. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-range-payload.c +106 -125
  166. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-insert-update-payload-private-v2.h +130 -0
  167. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-insert-update-payload-private.h +36 -44
  168. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-insert-update-payload-v2.c +382 -0
  169. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-insert-update-payload.c +237 -278
  170. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-iev-private-v2.h +133 -0
  171. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-iev-private.h +67 -56
  172. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-iev-v2.c +437 -0
  173. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-iev.c +454 -325
  174. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-uev-common-private.h +69 -0
  175. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-uev-common.c +182 -0
  176. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-uev-private.h +23 -31
  177. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-uev-v2-private.h +85 -0
  178. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-uev-v2.c +142 -0
  179. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-uev.c +104 -231
  180. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-range-operator-private.h +8 -7
  181. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-rfds-private.h +56 -56
  182. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-rfds.c +520 -580
  183. data/ext/libmongocrypt/libmongocrypt/src/mc-optional-private.h +47 -66
  184. data/ext/libmongocrypt/libmongocrypt/src/mc-range-edge-generation-private.h +67 -76
  185. data/ext/libmongocrypt/libmongocrypt/src/mc-range-edge-generation.c +201 -190
  186. data/ext/libmongocrypt/libmongocrypt/src/mc-range-encoding-private.h +55 -42
  187. data/ext/libmongocrypt/libmongocrypt/src/mc-range-encoding.c +809 -558
  188. data/ext/libmongocrypt/libmongocrypt/src/mc-range-mincover-generator.template.h +177 -201
  189. data/ext/libmongocrypt/libmongocrypt/src/mc-range-mincover-private.h +54 -53
  190. data/ext/libmongocrypt/libmongocrypt/src/mc-range-mincover.c +188 -240
  191. data/ext/libmongocrypt/libmongocrypt/src/mc-rangeopts-private.h +43 -35
  192. data/ext/libmongocrypt/libmongocrypt/src/mc-rangeopts.c +484 -337
  193. data/ext/libmongocrypt/libmongocrypt/src/mc-reader-private.h +36 -71
  194. data/ext/libmongocrypt/libmongocrypt/src/mc-reader.c +111 -164
  195. data/ext/libmongocrypt/libmongocrypt/src/mc-tokens-private.h +87 -65
  196. data/ext/libmongocrypt/libmongocrypt/src/mc-tokens.c +153 -125
  197. data/ext/libmongocrypt/libmongocrypt/src/mc-writer-private.h +66 -0
  198. data/ext/libmongocrypt/libmongocrypt/src/mc-writer.c +141 -0
  199. data/ext/libmongocrypt/libmongocrypt/src/mlib/check.hpp +37 -55
  200. data/ext/libmongocrypt/libmongocrypt/src/mlib/endian.h +11 -11
  201. data/ext/libmongocrypt/libmongocrypt/src/mlib/error.h +27 -32
  202. data/ext/libmongocrypt/libmongocrypt/src/mlib/int128.h +461 -499
  203. data/ext/libmongocrypt/libmongocrypt/src/mlib/int128.test.cpp +256 -334
  204. data/ext/libmongocrypt/libmongocrypt/src/mlib/macros.h +4 -5
  205. data/ext/libmongocrypt/libmongocrypt/src/mlib/path.h +196 -231
  206. data/ext/libmongocrypt/libmongocrypt/src/mlib/path.test.c +56 -79
  207. data/ext/libmongocrypt/libmongocrypt/src/mlib/str.h +411 -530
  208. data/ext/libmongocrypt/libmongocrypt/src/mlib/str.test.c +116 -131
  209. data/ext/libmongocrypt/libmongocrypt/src/mlib/thread.h +17 -26
  210. data/ext/libmongocrypt/libmongocrypt/src/mlib/user-check.h +2 -2
  211. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-binary-private.h +1 -9
  212. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-binary.c +31 -48
  213. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-buffer-private.h +55 -131
  214. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-buffer.c +442 -565
  215. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-collinfo-private.h +1 -2
  216. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-collinfo.c +28 -45
  217. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-key-private.h +11 -17
  218. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-key.c +104 -132
  219. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-oauth-private.h +16 -24
  220. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-oauth.c +110 -93
  221. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-private.h +26 -45
  222. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache.c +220 -273
  223. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ciphertext-private.h +13 -25
  224. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ciphertext.c +147 -166
  225. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-compat.h +2 -2
  226. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-crypto-private.h +110 -204
  227. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-crypto.c +1145 -1567
  228. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ctx-datakey.c +510 -476
  229. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ctx-decrypt.c +817 -694
  230. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ctx-encrypt.c +2775 -2697
  231. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ctx-private.h +189 -184
  232. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ctx-rewrap-many-datakey.c +308 -351
  233. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ctx.c +990 -1139
  234. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-dll-private.h +23 -24
  235. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-endian-private.h +44 -58
  236. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-endpoint-private.h +21 -25
  237. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-endpoint.c +167 -181
  238. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-kek-private.h +42 -44
  239. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-kek.c +292 -272
  240. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-key-broker-private.h +70 -110
  241. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-key-broker.c +1010 -1033
  242. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-key-private.h +27 -44
  243. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-key.c +349 -402
  244. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-kms-ctx-private.h +127 -120
  245. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-kms-ctx.c +1692 -1611
  246. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-log-private.h +35 -67
  247. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-log.c +49 -83
  248. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-marking-private.h +31 -36
  249. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-marking.c +1923 -1479
  250. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-mutex-private.h +7 -12
  251. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-opts-private.h +124 -107
  252. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-opts.c +925 -419
  253. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-private.h +99 -122
  254. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-status-private.h +3 -8
  255. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-status.c +92 -119
  256. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-traverse-util-private.h +19 -29
  257. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-traverse-util.c +136 -176
  258. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-util-private.h +11 -21
  259. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-util.c +98 -136
  260. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt.c +924 -1339
  261. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt.h +199 -313
  262. data/ext/libmongocrypt/libmongocrypt/src/os_posix/os_dll.c +83 -79
  263. data/ext/libmongocrypt/libmongocrypt/src/os_posix/os_mutex.c +20 -28
  264. data/ext/libmongocrypt/libmongocrypt/src/os_win/os_dll.c +64 -73
  265. data/ext/libmongocrypt/libmongocrypt/src/os_win/os_mutex.c +8 -16
  266. data/ext/libmongocrypt/libmongocrypt/test/crypt_shared-stub.cpp +57 -87
  267. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/bypassQueryAnalysis/payload.json +53 -0
  268. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/jsonSchema/cmd-to-mongocryptd.json +23 -0
  269. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/jsonSchema/cmd.json +20 -0
  270. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/simple/cmd-to-mongocryptd.json +50 -0
  271. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/simple/cmd.json +20 -0
  272. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/simple/collinfo.json +44 -0
  273. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/simple/encrypted-field-map.json +24 -0
  274. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/simple/encrypted-payload-pattern.json +53 -0
  275. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/simple/mongocryptd-reply.json +62 -0
  276. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/unencrypted/cmd-to-mongocryptd.json +30 -0
  277. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/unencrypted/cmd.json +17 -0
  278. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/unencrypted/mongocryptd-reply.json +39 -0
  279. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/unencrypted/payload.json +21 -0
  280. data/ext/libmongocrypt/libmongocrypt/test/data/cleanup/missing-key-id/collinfo.json +20 -0
  281. data/ext/libmongocrypt/libmongocrypt/test/data/cleanup/no-fields/collinfo.json +9 -0
  282. data/ext/libmongocrypt/libmongocrypt/test/data/cleanup/no-fields/encrypted-payload.json +4 -0
  283. data/ext/libmongocrypt/libmongocrypt/test/data/cleanup/success/cmd.json +1 -0
  284. data/ext/libmongocrypt/libmongocrypt/test/data/cleanup/success/collinfo.json +63 -0
  285. data/ext/libmongocrypt/libmongocrypt/test/data/cleanup/success/encrypted-field-config-map.json +61 -0
  286. data/ext/libmongocrypt/libmongocrypt/test/data/cleanup/success/encrypted-payload-range-v2.json +37 -0
  287. data/ext/libmongocrypt/libmongocrypt/test/data/cleanup/success/encrypted-payload.json +29 -0
  288. data/ext/libmongocrypt/libmongocrypt/test/data/compact/anchor-pad/cmd.json +1 -0
  289. data/ext/libmongocrypt/libmongocrypt/test/data/compact/anchor-pad/collinfo.json +64 -0
  290. data/ext/libmongocrypt/libmongocrypt/test/data/compact/anchor-pad/encrypted-payload-range-v2.json +105 -0
  291. data/ext/libmongocrypt/libmongocrypt/test/data/compact/anchor-pad/encrypted-payload.json +30 -0
  292. data/ext/libmongocrypt/libmongocrypt/test/data/compact/no-range/cmd.json +1 -0
  293. data/ext/libmongocrypt/libmongocrypt/test/data/compact/no-range/collinfo.json +49 -0
  294. data/ext/libmongocrypt/libmongocrypt/test/data/compact/no-range/encrypted-field-config-map.json +47 -0
  295. data/ext/libmongocrypt/libmongocrypt/test/data/compact/no-range/encrypted-payload.json +23 -0
  296. data/ext/libmongocrypt/libmongocrypt/test/data/compact/success/collinfo.json +15 -0
  297. data/ext/libmongocrypt/libmongocrypt/test/data/compact/success/encrypted-field-config-map.json +10 -0
  298. data/ext/libmongocrypt/libmongocrypt/test/data/compact/success/encrypted-payload-range-v2.json +104 -0
  299. data/ext/libmongocrypt/libmongocrypt/test/data/compact/success/encrypted-payload.json +6 -0
  300. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-delete/empty/encrypted-payload-v2.json +60 -0
  301. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-delete/success/encrypted-payload-v2.json +67 -0
  302. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explain/with-csfle/collinfo.json +4 -4
  303. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explain/with-csfle/encrypted-payload.json +5 -5
  304. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explicit/find-indexed-contentionFactor1-v2.json +8 -0
  305. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explicit/find-indexed-v2.json +8 -0
  306. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explicit/insert-indexed-contentionFactor1-v2.json +8 -0
  307. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explicit/insert-indexed-same-user-and-index-key-v2.json +8 -0
  308. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explicit/insert-indexed-v2.json +8 -0
  309. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-equality-v2/cmd.json +6 -0
  310. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-equality-v2/encrypted-field-map.json +22 -0
  311. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-equality-v2/encrypted-payload.json +40 -0
  312. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-equality-v2/mongocryptd-reply.json +19 -0
  313. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/date-v2/cmd.json +10 -0
  314. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/date-v2/encrypted-field-map.json +27 -0
  315. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/date-v2/encrypted-payload.json +41 -0
  316. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/date-v2/mongocryptd-reply.json +49 -0
  317. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-precision-v2/cmd.json +6 -0
  318. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-precision-v2/encrypted-field-map.json +30 -0
  319. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-precision-v2/encrypted-payload.json +50 -0
  320. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-precision-v2/mongocryptd-reply.json +58 -0
  321. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-v2/cmd.json +6 -0
  322. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-v2/encrypted-field-map.json +27 -0
  323. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-v2/encrypted-payload.json +41 -0
  324. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-v2/mongocryptd-reply.json +49 -0
  325. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-precision-v2/cmd.json +8 -0
  326. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-precision-v2/encrypted-field-map.json +30 -0
  327. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-precision-v2/encrypted-payload.json +44 -0
  328. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-precision-v2/mongocryptd-reply.json +52 -0
  329. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-v2/cmd.json +8 -0
  330. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-v2/encrypted-field-map.json +27 -0
  331. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-v2/encrypted-payload.json +41 -0
  332. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-v2/mongocryptd-reply.json +49 -0
  333. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int32-v2/cmd.json +8 -0
  334. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int32-v2/encrypted-field-map.json +27 -0
  335. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int32-v2/encrypted-payload.json +41 -0
  336. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int32-v2/mongocryptd-reply.json +49 -0
  337. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int64-v2/cmd.json +8 -0
  338. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int64-v2/encrypted-field-map.json +27 -0
  339. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int64-v2/encrypted-payload.json +41 -0
  340. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int64-v2/mongocryptd-reply.json +49 -0
  341. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/double/encrypted-payload-v2.json +26 -0
  342. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/double-precision/encrypted-payload-v2.json +26 -0
  343. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/int32/encrypted-payload-v2.json +26 -0
  344. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/int32-nominmax/encrypted-payload-v2.json +26 -0
  345. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/int32-openinterval/encrypted-payload-v2.json +16 -0
  346. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert/cmd.json +1 -1
  347. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/date/RNG_DATA.h +65 -65
  348. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/date-v2/cmd.json +13 -0
  349. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/date-v2/encrypted-field-map.json +27 -0
  350. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/date-v2/encrypted-payload.json +44 -0
  351. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/date-v2/mongocryptd-reply.json +52 -0
  352. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128/RNG_DATA.h +132 -132
  353. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-precision/RNG_DATA.h +71 -71
  354. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-precision-v2/cmd.json +9 -0
  355. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-precision-v2/encrypted-field-map.json +30 -0
  356. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-precision-v2/encrypted-payload.json +53 -0
  357. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-precision-v2/mongocryptd-reply.json +61 -0
  358. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-v2/cmd.json +9 -0
  359. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-v2/encrypted-field-map.json +27 -0
  360. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-v2/encrypted-payload.json +44 -0
  361. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-v2/mongocryptd-reply.json +52 -0
  362. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double/RNG_DATA.h +68 -68
  363. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-precision/RNG_DATA.h +19 -19
  364. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-precision-v2/cmd.json +11 -0
  365. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-precision-v2/encrypted-field-map.json +30 -0
  366. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-precision-v2/encrypted-payload.json +47 -0
  367. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-precision-v2/mongocryptd-reply.json +55 -0
  368. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-v2/cmd.json +11 -0
  369. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-v2/encrypted-field-map.json +27 -0
  370. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-v2/encrypted-payload.json +44 -0
  371. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-v2/mongocryptd-reply.json +52 -0
  372. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int32/RNG_DATA.h +25 -25
  373. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int32-v2/cmd.json +11 -0
  374. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int32-v2/encrypted-field-map.json +27 -0
  375. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int32-v2/encrypted-payload.json +44 -0
  376. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int32-v2/mongocryptd-reply.json +52 -0
  377. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int64/RNG_DATA.h +65 -65
  378. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int64-v2/cmd.json +11 -0
  379. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int64-v2/encrypted-field-map.json +27 -0
  380. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int64-v2/encrypted-payload.json +44 -0
  381. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int64-v2/mongocryptd-reply.json +52 -0
  382. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/double/RNG_DATA.h +68 -68
  383. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/double/encrypted-payload-v2.json +8 -0
  384. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/double-precision/RNG_DATA.h +19 -19
  385. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/double-precision/encrypted-payload-v2.json +8 -0
  386. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32/RNG_DATA.h +25 -25
  387. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32/encrypted-payload-v2.json +8 -0
  388. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32-nominmax/encrypted-payload-v2.json +8 -0
  389. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/sparsity-2/RNG_DATA.h +15 -15
  390. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/sparsity-2/encrypted-payload-v2.json +8 -0
  391. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-unindexed-v2/cmd.json +9 -0
  392. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-unindexed-v2/encrypted-field-map.json +18 -0
  393. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-unindexed-v2/encrypted-payload.json +14 -0
  394. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-unindexed-v2/mongocryptd-reply.json +41 -0
  395. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-v2/cmd.json +9 -0
  396. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-v2/encrypted-field-map.json +22 -0
  397. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-v2/encrypted-payload.json +39 -0
  398. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-v2/mongocryptd-reply.json +49 -0
  399. data/ext/libmongocrypt/libmongocrypt/test/data/iev-v2/FLECrudTest-insertOneRangeV2.json +10 -0
  400. data/ext/libmongocrypt/libmongocrypt/test/data/iev-v2/FLECrudTest-insertOneV2.json +10 -0
  401. data/ext/libmongocrypt/libmongocrypt/test/data/kms-azure/decrypt-response.txt +16 -0
  402. data/ext/libmongocrypt/libmongocrypt/test/data/kms-azure/encrypt-response.txt +16 -0
  403. data/ext/libmongocrypt/libmongocrypt/test/data/kms-azure/oauth-response.txt +19 -0
  404. data/ext/libmongocrypt/libmongocrypt/test/data/no-trimFactor/find/cmd.json +9 -0
  405. data/ext/libmongocrypt/libmongocrypt/test/data/no-trimFactor/find/encrypted-field-map.json +19 -0
  406. data/ext/libmongocrypt/libmongocrypt/test/data/no-trimFactor/find/encrypted-payload.json +62 -0
  407. data/ext/libmongocrypt/libmongocrypt/test/data/no-trimFactor/find/mongocryptd-reply.json +69 -0
  408. data/ext/libmongocrypt/libmongocrypt/test/data/no-trimFactor/insert/cmd.json +11 -0
  409. data/ext/libmongocrypt/libmongocrypt/test/data/no-trimFactor/insert/encrypted-field-map.json +19 -0
  410. data/ext/libmongocrypt/libmongocrypt/test/data/no-trimFactor/insert/encrypted-payload.json +40 -0
  411. data/ext/libmongocrypt/libmongocrypt/test/data/no-trimFactor/insert/mongocryptd-reply.json +47 -0
  412. data/ext/libmongocrypt/libmongocrypt/test/data/range-edge-generation/edges_decimal128.cstruct +1 -1
  413. data/ext/libmongocrypt/libmongocrypt/test/data/range-edge-generation/edges_double.cstruct +8637 -7958
  414. data/ext/libmongocrypt/libmongocrypt/test/data/range-edge-generation/edges_int32.cstruct +5522 -1382
  415. data/ext/libmongocrypt/libmongocrypt/test/data/range-edge-generation/edges_int64.cstruct +5042 -1262
  416. data/ext/libmongocrypt/libmongocrypt/test/data/range-min-cover/mincover_decimal128.cstruct +1 -1
  417. data/ext/libmongocrypt/libmongocrypt/test/data/range-min-cover/mincover_decimal128_precision.cstruct +1 -1
  418. data/ext/libmongocrypt/libmongocrypt/test/data/range-min-cover/mincover_double.cstruct +1 -1
  419. data/ext/libmongocrypt/libmongocrypt/test/data/range-min-cover/mincover_double_precision.cstruct +2 -2
  420. data/ext/libmongocrypt/libmongocrypt/test/data/range-min-cover/mincover_int32.cstruct +1 -1
  421. data/ext/libmongocrypt/libmongocrypt/test/data/range-min-cover/mincover_int64.cstruct +1 -1
  422. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/auto-find-int32/cmd.json +8 -0
  423. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/auto-find-int32/encrypted-field-map.json +19 -0
  424. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/auto-find-int32/encrypted-payload.json +53 -0
  425. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/auto-find-int32/mongocryptd-reply.json +58 -0
  426. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/auto-insert-int32/cmd.json +11 -0
  427. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/auto-insert-int32/encrypted-field-map.json +19 -0
  428. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/auto-insert-int32/encrypted-payload.json +40 -0
  429. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/auto-insert-int32/mongocryptd-reply.json +45 -0
  430. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/explicit-find-int32/expected.json +26 -0
  431. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/explicit-find-int32/to-encrypt.json +20 -0
  432. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/explicit-find-int32-defaults/expected.json +26 -0
  433. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/explicit-find-int32-defaults/to-encrypt.json +20 -0
  434. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/explicit-insert-double/expected.json +8 -0
  435. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/explicit-insert-int32/expected.json +8 -0
  436. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/explicit-insert-int32-defaults/expected.json +8 -0
  437. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/README.md +17 -0
  438. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/aes-ctr.json +29 -0
  439. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2-fixed.json +10 -0
  440. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2-generated.json +38 -0
  441. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2aead-decrypt.json +35 -0
  442. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2aead-fixed.json +29 -0
  443. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2aead-generated.json +122 -0
  444. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2v2-aead-fixed.json +29 -0
  445. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2v2-aead-generated.json +122 -0
  446. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/mcgrew.json +12 -0
  447. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/nist.json +20 -0
  448. data/ext/libmongocrypt/libmongocrypt/test/data/tokens/README.md +30 -0
  449. data/ext/libmongocrypt/libmongocrypt/test/data/tokens/mc.json +25 -0
  450. data/ext/libmongocrypt/libmongocrypt/test/data/tokens/server.json +25 -0
  451. data/ext/libmongocrypt/libmongocrypt/test/example-no-bson.c +4 -4
  452. data/ext/libmongocrypt/libmongocrypt/test/example-state-machine.c +279 -323
  453. data/ext/libmongocrypt/libmongocrypt/test/fuzz_kms.c +8 -7
  454. data/ext/libmongocrypt/libmongocrypt/test/test-dll.cpp +6 -7
  455. data/ext/libmongocrypt/libmongocrypt/test/test-gcp-auth.c +221 -283
  456. data/ext/libmongocrypt/libmongocrypt/test/test-mc-efc.c +60 -73
  457. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-find-equality-payload-v2.c +78 -0
  458. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-find-range-payload-v2.c +129 -0
  459. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-payload-iev-v2.c +248 -0
  460. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-payload-iev.c +467 -414
  461. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-payload-iup-v2.c +248 -0
  462. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-payload-iup.c +141 -159
  463. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-payload-uev-v2.c +338 -0
  464. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-payload-uev.c +238 -176
  465. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-rfds.c +373 -474
  466. data/ext/libmongocrypt/libmongocrypt/test/test-mc-range-edge-generation.c +421 -388
  467. data/ext/libmongocrypt/libmongocrypt/test/test-mc-range-encoding.c +974 -825
  468. data/ext/libmongocrypt/libmongocrypt/test/test-mc-range-mincover.c +499 -481
  469. data/ext/libmongocrypt/libmongocrypt/test/test-mc-rangeopts.c +312 -148
  470. data/ext/libmongocrypt/libmongocrypt/test/test-mc-reader.c +124 -207
  471. data/ext/libmongocrypt/libmongocrypt/test/test-mc-tokens.c +249 -213
  472. data/ext/libmongocrypt/libmongocrypt/test/test-mc-writer.c +176 -0
  473. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-assert-match-bson.c +632 -808
  474. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-assert-match-bson.h +22 -2
  475. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-assert.h +168 -189
  476. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-buffer.c +187 -211
  477. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-cache-oauth.c +118 -37
  478. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-cache.c +210 -233
  479. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-ciphertext.c +185 -220
  480. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-cleanup.c +374 -0
  481. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-compact.c +410 -445
  482. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-crypto-hooks.c +798 -812
  483. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-crypto-std-hooks.c +142 -135
  484. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-crypto-std-hooks.h +70 -71
  485. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-crypto.c +394 -846
  486. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-csfle-lib.c +159 -183
  487. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-ctx-decrypt.c +870 -1045
  488. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-ctx-encrypt.c +5183 -4717
  489. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-ctx-rewrap-many-datakey.c +750 -964
  490. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-ctx-setopt.c +1093 -1178
  491. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-datakey.c +348 -420
  492. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-dll.c +23 -30
  493. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-endpoint.c +98 -111
  494. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-kek.c +49 -52
  495. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-key-broker.c +770 -920
  496. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-key-cache.c +354 -407
  497. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-key.c +197 -245
  498. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-kms-ctx.c +705 -368
  499. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-kms-responses.c +147 -166
  500. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-local-kms.c +50 -61
  501. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-log.c +85 -100
  502. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-marking.c +1073 -690
  503. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-opts.c +42 -0
  504. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-status.c +46 -58
  505. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-traverse-util.c +381 -451
  506. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-util.c +55 -67
  507. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-util.h +7 -10
  508. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt.c +879 -918
  509. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt.h +119 -169
  510. data/ext/libmongocrypt/libmongocrypt/test/test-named-kms-providers.c +2381 -0
  511. data/ext/libmongocrypt/libmongocrypt/test/util/HELP.autogen +3 -1
  512. data/ext/libmongocrypt/libmongocrypt/test/util/README.md +1 -0
  513. data/ext/libmongocrypt/libmongocrypt/test/util/csfle.c +512 -559
  514. data/ext/libmongocrypt/libmongocrypt/test/util/make_includes.py +1 -1
  515. data/ext/libmongocrypt/libmongocrypt/test/util/util.c +771 -882
  516. data/ext/libmongocrypt/libmongocrypt/test/util/util.h +33 -55
  517. data/lib/libmongocrypt_helper/version.rb +2 -2
  518. metadata +245 -111
  519. checksums.yaml.gz.sig +0 -0
  520. data/ext/libmongocrypt/libmongocrypt/VERSION_CURRENT +0 -1
  521. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/test/resources/fle2-find-range-explicit/int32/encrypted-payload.json +0 -26
  522. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/test/resources/fle2-find-range-explicit/int32/key-document.json +0 -0
  523. data/ext/libmongocrypt/libmongocrypt/bindings/node/CHANGELOG.md +0 -81
  524. data/ext/libmongocrypt/libmongocrypt/bindings/node/LICENSE +0 -201
  525. data/ext/libmongocrypt/libmongocrypt/bindings/node/binding.gyp +0 -79
  526. data/ext/libmongocrypt/libmongocrypt/bindings/node/etc/README.hbs +0 -44
  527. data/ext/libmongocrypt/libmongocrypt/bindings/node/etc/build-static.sh +0 -39
  528. data/ext/libmongocrypt/libmongocrypt/bindings/node/index.d.ts +0 -515
  529. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/autoEncrypter.js +0 -420
  530. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/buffer_pool.js +0 -123
  531. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/clientEncryption.js +0 -699
  532. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/common.js +0 -100
  533. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/credentialsProvider.js +0 -33
  534. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/cryptoCallbacks.js +0 -87
  535. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/index.js +0 -42
  536. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/mongocryptdManager.js +0 -66
  537. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/stateMachine.js +0 -487
  538. data/ext/libmongocrypt/libmongocrypt/bindings/node/package-lock.json +0 -16051
  539. data/ext/libmongocrypt/libmongocrypt/bindings/node/package.json +0 -81
  540. data/ext/libmongocrypt/libmongocrypt/bindings/node/src/mongocrypt.cc +0 -929
  541. data/ext/libmongocrypt/libmongocrypt/bindings/node/src/mongocrypt.h +0 -114
  542. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/autoEncrypter.test.js +0 -953
  543. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/buffer_pool.test.js +0 -91
  544. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/clientEncryption.test.js +0 -701
  545. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/credentialsProvider.test.js +0 -163
  546. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/cryptoCallbacks.test.js +0 -284
  547. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/data/README.md +0 -5
  548. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/data/cmd.json +0 -6
  549. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/data/collection-info.json +0 -37
  550. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/data/encrypted-document-nested.json +0 -8
  551. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/data/encrypted-document.json +0 -11
  552. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/data/encryptedFields.json +0 -30
  553. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/data/key-document.json +0 -32
  554. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/data/key1-document.json +0 -30
  555. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/data/mongocryptd-reply.json +0 -18
  556. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/mongocryptdManager.test.js +0 -48
  557. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/release.test.js +0 -59
  558. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/requirements.helper.js +0 -29
  559. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/stateMachine.test.js +0 -331
  560. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/tools/mongodb_reporter.js +0 -325
  561. data/ext/libmongocrypt/libmongocrypt/bindings/python/setup.py +0 -84
  562. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/fle2-find-range-explicit/int32/encrypted-payload.json +0 -26
  563. data/ext/libmongocrypt/libmongocrypt/bindings/python/test-requirements.txt +0 -12
  564. data/ext/libmongocrypt/libmongocrypt/debian/build_snapshot.sh +0 -79
  565. data/ext/libmongocrypt/libmongocrypt/debian/changelog +0 -105
  566. data/ext/libmongocrypt/libmongocrypt/debian/compat +0 -1
  567. data/ext/libmongocrypt/libmongocrypt/debian/control +0 -41
  568. data/ext/libmongocrypt/libmongocrypt/debian/copyright +0 -129
  569. data/ext/libmongocrypt/libmongocrypt/debian/gbp.conf +0 -23
  570. data/ext/libmongocrypt/libmongocrypt/debian/libmongocrypt-dev.dirs +0 -2
  571. data/ext/libmongocrypt/libmongocrypt/debian/libmongocrypt-dev.install +0 -5
  572. data/ext/libmongocrypt/libmongocrypt/debian/libmongocrypt0.dirs +0 -1
  573. data/ext/libmongocrypt/libmongocrypt/debian/libmongocrypt0.install +0 -1
  574. data/ext/libmongocrypt/libmongocrypt/debian/rules +0 -46
  575. data/ext/libmongocrypt/libmongocrypt/debian/source/format +0 -1
  576. data/ext/libmongocrypt/libmongocrypt/debian/source/lintian-overrides +0 -3
  577. data/ext/libmongocrypt/libmongocrypt/debian/source/options +0 -1
  578. data/ext/libmongocrypt/libmongocrypt/debian/watch +0 -3
  579. data/ext/libmongocrypt/libmongocrypt/test/data/aes-ctr.cstructs +0 -359
  580. data/ext/libmongocrypt/libmongocrypt/test/data/compact/success/mongocryptd-reply.json +0 -72
  581. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-aead.cstructs +0 -109
  582. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/int32-nominmax/encrypted-payload.json +0 -26
  583. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/int32-nominmax/rangeopts.json +0 -5
  584. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32-nominmax/RNG_DATA.h +0 -70
  585. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32-nominmax/encrypted-payload.json +0 -8
  586. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32-nominmax/rangeopts.json +0 -5
  587. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32-nominmax/value-to-encrypt.json +0 -5
  588. data/ext/libmongocrypt/libmongocrypt/test/data/fle2.cstructs +0 -33
  589. data.tar.gz.sig +0 -0
  590. metadata.gz.sig +0 -0
  591. /data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/test/resources/{fle2-find-range-explicit → fle2-find-range-explicit-v2}/int32/key-filter.json +0 -0
  592. /data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/test/resources/{fle2-find-range-explicit → fle2-find-range-explicit-v2}/int32/value-to-encrypt.json +0 -0
  593. /data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/{fle2-find-range-explicit → fle2-find-range-explicit-v2}/int32/value-to-encrypt.json +0 -0
  594. /data/ext/libmongocrypt/libmongocrypt/bindings/{java/mongocrypt/src/test/resources/fle2-find-range-explicit → python/test/data/fle2-find-rangePreview-explicit}/int32/rangeopts.json +0 -0
  595. /data/ext/libmongocrypt/libmongocrypt/{test/data/fle2-find-range-explicit/int32-nominmax → bindings/python/test/data/fle2-find-rangePreview-explicit/int32}/value-to-encrypt.json +0 -0
  596. /data/ext/libmongocrypt/libmongocrypt/{bindings/node/test/data/kms-decrypt-reply.txt → test/data/kms-aws/decrypt-response.txt} +0 -0
  597. /data/ext/libmongocrypt/libmongocrypt/{bindings/node/test/data/kms-encrypt-reply.txt → test/data/kms-aws/encrypt-response.txt} +0 -0
  598. /data/ext/libmongocrypt/libmongocrypt/test/data/{gcp-auth → kms-gcp}/decrypt-response.txt +0 -0
  599. /data/ext/libmongocrypt/libmongocrypt/test/data/{gcp-auth → kms-gcp}/encrypt-response.txt +0 -0
  600. /data/ext/libmongocrypt/libmongocrypt/test/data/{gcp-auth → kms-gcp}/oauth-response.txt +0 -0
@@ -19,1265 +19,1116 @@
19
19
  #include "mongocrypt-ctx-private.h"
20
20
  #include "mongocrypt-key-broker-private.h"
21
21
 
22
- bool
23
- _mongocrypt_ctx_fail_w_msg (mongocrypt_ctx_t *ctx, const char *msg)
24
- {
25
- BSON_ASSERT_PARAM (ctx);
26
- BSON_ASSERT_PARAM (msg);
27
-
28
- _mongocrypt_set_error (ctx->status,
29
- MONGOCRYPT_STATUS_ERROR_CLIENT,
30
- MONGOCRYPT_GENERIC_ERROR_CODE,
31
- "%s",
32
- msg);
33
- return _mongocrypt_ctx_fail (ctx);
22
+ bool _mongocrypt_ctx_fail_w_msg(mongocrypt_ctx_t *ctx, const char *msg) {
23
+ BSON_ASSERT_PARAM(ctx);
24
+ BSON_ASSERT_PARAM(msg);
25
+
26
+ _mongocrypt_set_error(ctx->status, MONGOCRYPT_STATUS_ERROR_CLIENT, MONGOCRYPT_GENERIC_ERROR_CODE, "%s", msg);
27
+ return _mongocrypt_ctx_fail(ctx);
34
28
  }
35
29
 
36
30
  /* A failure status has already been set. */
37
- bool
38
- _mongocrypt_ctx_fail (mongocrypt_ctx_t *ctx)
39
- {
40
- BSON_ASSERT_PARAM (ctx);
41
-
42
- if (mongocrypt_status_ok (ctx->status)) {
43
- return _mongocrypt_ctx_fail_w_msg (
44
- ctx, "unexpected, failing but no error status set");
45
- }
46
- ctx->state = MONGOCRYPT_CTX_ERROR;
47
- return false;
31
+ bool _mongocrypt_ctx_fail(mongocrypt_ctx_t *ctx) {
32
+ BSON_ASSERT_PARAM(ctx);
33
+
34
+ if (mongocrypt_status_ok(ctx->status)) {
35
+ return _mongocrypt_ctx_fail_w_msg(ctx, "unexpected, failing but no error status set");
36
+ }
37
+ ctx->state = MONGOCRYPT_CTX_ERROR;
38
+ return false;
48
39
  }
49
40
 
50
-
51
41
  static bool
52
- _set_binary_opt (mongocrypt_ctx_t *ctx,
53
- mongocrypt_binary_t *binary,
54
- _mongocrypt_buffer_t *buf,
55
- bson_subtype_t subtype)
56
- {
57
- BSON_ASSERT_PARAM (ctx);
42
+ _set_binary_opt(mongocrypt_ctx_t *ctx, mongocrypt_binary_t *binary, _mongocrypt_buffer_t *buf, bson_subtype_t subtype) {
43
+ BSON_ASSERT_PARAM(ctx);
58
44
 
59
- if (ctx->state == MONGOCRYPT_CTX_ERROR) {
60
- return false;
61
- }
45
+ if (ctx->state == MONGOCRYPT_CTX_ERROR) {
46
+ return false;
47
+ }
62
48
 
63
- if (!binary || !binary->data) {
64
- return _mongocrypt_ctx_fail_w_msg (ctx, "option must be non-NULL");
65
- }
49
+ if (!binary || !binary->data) {
50
+ return _mongocrypt_ctx_fail_w_msg(ctx, "option must be non-NULL");
51
+ }
66
52
 
67
- if (!_mongocrypt_buffer_empty (buf)) {
68
- return _mongocrypt_ctx_fail_w_msg (ctx, "option already set");
69
- }
53
+ if (!_mongocrypt_buffer_empty(buf)) {
54
+ return _mongocrypt_ctx_fail_w_msg(ctx, "option already set");
55
+ }
70
56
 
71
- if (ctx->initialized) {
72
- return _mongocrypt_ctx_fail_w_msg (ctx, "cannot set options after init");
73
- }
57
+ if (ctx->initialized) {
58
+ return _mongocrypt_ctx_fail_w_msg(ctx, "cannot set options after init");
59
+ }
74
60
 
75
- if (subtype == BSON_SUBTYPE_UUID && binary->len != 16) {
76
- return _mongocrypt_ctx_fail_w_msg (ctx, "expected 16 byte UUID");
77
- }
61
+ if (subtype == BSON_SUBTYPE_UUID && binary->len != 16) {
62
+ return _mongocrypt_ctx_fail_w_msg(ctx, "expected 16 byte UUID");
63
+ }
78
64
 
79
- _mongocrypt_buffer_copy_from_binary (buf, binary);
80
- buf->subtype = subtype;
65
+ _mongocrypt_buffer_copy_from_binary(buf, binary);
66
+ buf->subtype = subtype;
81
67
 
82
- return true;
68
+ return true;
83
69
  }
84
70
 
85
-
86
- bool
87
- mongocrypt_ctx_setopt_key_id (mongocrypt_ctx_t *ctx,
88
- mongocrypt_binary_t *key_id)
89
- {
90
- if (!ctx) {
91
- return false;
92
- }
93
-
94
- if (ctx->crypt->log.trace_enabled && key_id && key_id->data) {
95
- char *key_id_val;
96
- /* this should never happen, so assert rather than return false */
97
- BSON_ASSERT (key_id->len <= INT_MAX);
98
- key_id_val =
99
- _mongocrypt_new_string_from_bytes (key_id->data, (int) key_id->len);
100
- _mongocrypt_log (&ctx->crypt->log,
101
- MONGOCRYPT_LOG_LEVEL_TRACE,
102
- "%s (%s=\"%s\")",
103
- BSON_FUNC,
104
- "key_id",
105
- key_id_val);
106
- bson_free (key_id_val);
107
- }
108
-
109
- return _set_binary_opt (ctx, key_id, &ctx->opts.key_id, BSON_SUBTYPE_UUID);
71
+ bool mongocrypt_ctx_setopt_key_id(mongocrypt_ctx_t *ctx, mongocrypt_binary_t *key_id) {
72
+ if (!ctx) {
73
+ return false;
74
+ }
75
+
76
+ if (ctx->crypt->log.trace_enabled && key_id && key_id->data) {
77
+ char *key_id_val;
78
+ /* this should never happen, so assert rather than return false */
79
+ BSON_ASSERT(key_id->len <= INT_MAX);
80
+ key_id_val = _mongocrypt_new_string_from_bytes(key_id->data, (int)key_id->len);
81
+ _mongocrypt_log(&ctx->crypt->log,
82
+ MONGOCRYPT_LOG_LEVEL_TRACE,
83
+ "%s (%s=\"%s\")",
84
+ BSON_FUNC,
85
+ "key_id",
86
+ key_id_val);
87
+ bson_free(key_id_val);
88
+ }
89
+
90
+ return _set_binary_opt(ctx, key_id, &ctx->opts.key_id, BSON_SUBTYPE_UUID);
110
91
  }
111
92
 
93
+ bool mongocrypt_ctx_setopt_key_alt_name(mongocrypt_ctx_t *ctx, mongocrypt_binary_t *key_alt_name) {
94
+ bson_t as_bson;
95
+ bson_iter_t iter;
96
+ _mongocrypt_key_alt_name_t *new_key_alt_name;
97
+ const char *key;
112
98
 
113
- bool
114
- mongocrypt_ctx_setopt_key_alt_name (mongocrypt_ctx_t *ctx,
115
- mongocrypt_binary_t *key_alt_name)
116
- {
117
- bson_t as_bson;
118
- bson_iter_t iter;
119
- _mongocrypt_key_alt_name_t *new_key_alt_name;
120
- const char *key;
121
-
122
- if (!ctx) {
123
- return false;
124
- }
125
-
126
- if (ctx->initialized) {
127
- return _mongocrypt_ctx_fail_w_msg (ctx, "cannot set options after init");
128
- }
129
-
130
- if (ctx->state == MONGOCRYPT_CTX_ERROR) {
131
- return false;
132
- }
133
-
134
- if (!key_alt_name || !key_alt_name->data) {
135
- return _mongocrypt_ctx_fail_w_msg (ctx, "option must be non-NULL");
136
- }
137
-
138
- if (!_mongocrypt_binary_to_bson (key_alt_name, &as_bson)) {
139
- return _mongocrypt_ctx_fail_w_msg (ctx, "invalid keyAltName bson object");
140
- }
141
-
142
- if (!bson_iter_init (&iter, &as_bson) || !bson_iter_next (&iter)) {
143
- return _mongocrypt_ctx_fail_w_msg (ctx, "invalid bson");
144
- }
145
-
146
- key = bson_iter_key (&iter);
147
- BSON_ASSERT (key);
148
- if (0 != strcmp (key, "keyAltName")) {
149
- return _mongocrypt_ctx_fail_w_msg (
150
- ctx, "keyAltName must have field 'keyAltName'");
151
- }
152
-
153
- if (!BSON_ITER_HOLDS_UTF8 (&iter)) {
154
- return _mongocrypt_ctx_fail_w_msg (ctx, "keyAltName expected to be UTF8");
155
- }
156
-
157
- new_key_alt_name = _mongocrypt_key_alt_name_new (bson_iter_value (&iter));
158
-
159
- if (ctx->opts.key_alt_names &&
160
- _mongocrypt_key_alt_name_intersects (ctx->opts.key_alt_names,
161
- new_key_alt_name)) {
162
- _mongocrypt_key_alt_name_destroy_all (new_key_alt_name);
163
- return _mongocrypt_ctx_fail_w_msg (ctx, "duplicate keyAltNames found");
164
- }
165
- new_key_alt_name->next = ctx->opts.key_alt_names;
166
- ctx->opts.key_alt_names = new_key_alt_name;
167
-
168
- if (bson_iter_next (&iter)) {
169
- return _mongocrypt_ctx_fail_w_msg (
170
- ctx, "unrecognized field, only keyAltName expected");
171
- }
172
-
173
- return true;
174
- }
99
+ if (!ctx) {
100
+ return false;
101
+ }
175
102
 
103
+ if (ctx->initialized) {
104
+ return _mongocrypt_ctx_fail_w_msg(ctx, "cannot set options after init");
105
+ }
176
106
 
177
- bool
178
- mongocrypt_ctx_setopt_key_material (mongocrypt_ctx_t *ctx,
179
- mongocrypt_binary_t *key_material)
180
- {
181
- bson_t as_bson;
182
- bson_iter_t iter;
183
- const char *key;
184
- _mongocrypt_buffer_t buffer;
185
-
186
- if (!ctx) {
187
- return false;
188
- }
189
-
190
- if (ctx->initialized) {
191
- return _mongocrypt_ctx_fail_w_msg (ctx, "cannot set options after init");
192
- }
193
-
194
- if (ctx->opts.key_material.owned) {
195
- return _mongocrypt_ctx_fail_w_msg (ctx, "keyMaterial already set");
196
- }
197
-
198
- if (ctx->state == MONGOCRYPT_CTX_ERROR) {
199
- return false;
200
- }
201
-
202
- if (!key_material || !key_material->data) {
203
- return _mongocrypt_ctx_fail_w_msg (ctx, "option must be non-NULL");
204
- }
205
-
206
- if (!_mongocrypt_binary_to_bson (key_material, &as_bson)) {
207
- return _mongocrypt_ctx_fail_w_msg (ctx,
208
- "invalid keyMaterial bson object");
209
- }
210
-
211
- /* TODO: use _mongocrypt_parse_required_binary once MONGOCRYPT-380 is
212
- * resolved.*/
213
- if (!bson_iter_init (&iter, &as_bson) || !bson_iter_next (&iter)) {
214
- return _mongocrypt_ctx_fail_w_msg (ctx, "invalid bson");
215
- }
216
-
217
- key = bson_iter_key (&iter);
218
- BSON_ASSERT (key);
219
- if (0 != strcmp (key, "keyMaterial")) {
220
- return _mongocrypt_ctx_fail_w_msg (
221
- ctx, "keyMaterial must have field 'keyMaterial'");
222
- }
223
-
224
- if (!_mongocrypt_buffer_from_binary_iter (&buffer, &iter)) {
225
- return _mongocrypt_ctx_fail_w_msg (ctx,
226
- "keyMaterial must be binary data");
227
- }
228
-
229
- if (buffer.len != MONGOCRYPT_KEY_LEN) {
230
- _mongocrypt_set_error (
231
- ctx->status,
232
- MONGOCRYPT_STATUS_ERROR_CLIENT,
233
- MONGOCRYPT_GENERIC_ERROR_CODE,
234
- "keyMaterial should have length %d, but has length %" PRIu32,
235
- MONGOCRYPT_KEY_LEN,
236
- buffer.len);
237
- return _mongocrypt_ctx_fail (ctx);
238
- }
239
-
240
- _mongocrypt_buffer_steal (&ctx->opts.key_material, &buffer);
241
-
242
- if (bson_iter_next (&iter)) {
243
- return _mongocrypt_ctx_fail_w_msg (
244
- ctx, "unrecognized field, only keyMaterial expected");
245
- }
246
-
247
- return true;
248
- }
107
+ if (ctx->state == MONGOCRYPT_CTX_ERROR) {
108
+ return false;
109
+ }
249
110
 
111
+ if (!key_alt_name || !key_alt_name->data) {
112
+ return _mongocrypt_ctx_fail_w_msg(ctx, "option must be non-NULL");
113
+ }
250
114
 
251
- bool
252
- mongocrypt_ctx_setopt_algorithm (mongocrypt_ctx_t *ctx,
253
- const char *algorithm,
254
- int len)
255
- {
256
- if (!ctx) {
257
- return false;
258
- }
259
-
260
- if (ctx->initialized) {
261
- return _mongocrypt_ctx_fail_w_msg (ctx, "cannot set options after init");
262
- }
263
-
264
- if (ctx->state == MONGOCRYPT_CTX_ERROR) {
265
- return false;
266
- }
267
-
268
- if (ctx->opts.algorithm != MONGOCRYPT_ENCRYPTION_ALGORITHM_NONE ||
269
- ctx->opts.index_type.set) {
270
- return _mongocrypt_ctx_fail_w_msg (ctx, "already set algorithm");
271
- }
272
-
273
- if (len < -1) {
274
- return _mongocrypt_ctx_fail_w_msg (ctx, "invalid algorithm length");
275
- }
276
-
277
- if (!algorithm) {
278
- return _mongocrypt_ctx_fail_w_msg (ctx, "passed null algorithm");
279
- }
280
-
281
- const size_t calculated_len = len == -1 ? strlen (algorithm) : (size_t) len;
282
- if (ctx->crypt->log.trace_enabled) {
283
- _mongocrypt_log (&ctx->crypt->log,
284
- MONGOCRYPT_LOG_LEVEL_TRACE,
285
- "%s (%s=\"%.*s\")",
286
- BSON_FUNC,
287
- "algorithm",
288
- calculated_len <= (size_t) INT_MAX ? (int) calculated_len
289
- : INT_MAX,
290
- algorithm);
291
- }
292
-
293
- mstr_view algo_str = mstrv_view_data (algorithm, calculated_len);
294
- if (mstr_eq_ignore_case (
295
- algo_str, mstrv_lit (MONGOCRYPT_ALGORITHM_DETERMINISTIC_STR))) {
296
- ctx->opts.algorithm = MONGOCRYPT_ENCRYPTION_ALGORITHM_DETERMINISTIC;
297
- } else if (mstr_eq_ignore_case (
298
- algo_str, mstrv_lit (MONGOCRYPT_ALGORITHM_RANDOM_STR))) {
299
- ctx->opts.algorithm = MONGOCRYPT_ENCRYPTION_ALGORITHM_RANDOM;
300
- } else if (mstr_eq_ignore_case (
301
- algo_str, mstrv_lit (MONGOCRYPT_ALGORITHM_INDEXED_STR))) {
302
- ctx->opts.index_type.value = MONGOCRYPT_INDEX_TYPE_EQUALITY;
303
- ctx->opts.index_type.set = true;
304
- } else if (mstr_eq_ignore_case (
305
- algo_str, mstrv_lit (MONGOCRYPT_ALGORITHM_UNINDEXED_STR))) {
306
- ctx->opts.index_type.value = MONGOCRYPT_INDEX_TYPE_NONE;
307
- ctx->opts.index_type.set = true;
308
- } else if (mstr_eq_ignore_case (
309
- algo_str, mstrv_lit (MONGOCRYPT_ALGORITHM_RANGEPREVIEW_STR))) {
310
- ctx->opts.index_type.value = MONGOCRYPT_INDEX_TYPE_RANGEPREVIEW;
311
- ctx->opts.index_type.set = true;
312
- } else {
313
- char *error = bson_strdup_printf (
314
- "unsupported algorithm string \"%.*s\"",
315
- algo_str.len <= (size_t) INT_MAX ? (int) algo_str.len : INT_MAX,
316
- algo_str.data);
317
- _mongocrypt_ctx_fail_w_msg (ctx, error);
318
- bson_free (error);
319
- return false;
320
- }
321
-
322
- return true;
323
- }
115
+ if (!_mongocrypt_binary_to_bson(key_alt_name, &as_bson)) {
116
+ return _mongocrypt_ctx_fail_w_msg(ctx, "invalid keyAltName bson object");
117
+ }
324
118
 
119
+ if (!bson_iter_init(&iter, &as_bson) || !bson_iter_next(&iter)) {
120
+ return _mongocrypt_ctx_fail_w_msg(ctx, "invalid bson");
121
+ }
325
122
 
326
- mongocrypt_ctx_t *
327
- mongocrypt_ctx_new (mongocrypt_t *crypt)
328
- {
329
- mongocrypt_ctx_t *ctx;
330
- size_t ctx_size;
331
-
332
- if (!crypt) {
333
- return NULL;
334
- }
335
- if (!crypt->initialized) {
336
- mongocrypt_status_t *status;
337
-
338
- status = crypt->status;
339
- CLIENT_ERR ("cannot create context from uninitialized crypt");
340
- return NULL;
341
- }
342
- ctx_size = sizeof (_mongocrypt_ctx_encrypt_t);
343
- if (sizeof (_mongocrypt_ctx_decrypt_t) > ctx_size) {
344
- ctx_size = sizeof (_mongocrypt_ctx_decrypt_t);
345
- }
346
- if (sizeof (_mongocrypt_ctx_datakey_t) > ctx_size) {
347
- ctx_size = sizeof (_mongocrypt_ctx_datakey_t);
348
- }
349
- ctx = bson_malloc0 (ctx_size);
350
- BSON_ASSERT (ctx);
351
-
352
- ctx->crypt = crypt;
353
- ctx->status = mongocrypt_status_new ();
354
- ctx->opts.algorithm = MONGOCRYPT_ENCRYPTION_ALGORITHM_NONE;
355
- ctx->state = MONGOCRYPT_CTX_DONE;
356
- return ctx;
357
- }
123
+ key = bson_iter_key(&iter);
124
+ BSON_ASSERT(key);
125
+ if (0 != strcmp(key, "keyAltName")) {
126
+ return _mongocrypt_ctx_fail_w_msg(ctx, "keyAltName must have field 'keyAltName'");
127
+ }
358
128
 
359
- #define CHECK_AND_CALL(fn, ...) \
360
- do { \
361
- if (!ctx->vtable.fn) { \
362
- return _mongocrypt_ctx_fail_w_msg (ctx, "not applicable to context"); \
363
- } \
364
- return ctx->vtable.fn (__VA_ARGS__); \
365
- } while (0)
129
+ if (!BSON_ITER_HOLDS_UTF8(&iter)) {
130
+ return _mongocrypt_ctx_fail_w_msg(ctx, "keyAltName expected to be UTF8");
131
+ }
366
132
 
367
- /* Common to both encrypt and decrypt context. */
368
- static bool
369
- _mongo_op_keys (mongocrypt_ctx_t *ctx, mongocrypt_binary_t *out)
370
- {
371
- BSON_ASSERT_PARAM (ctx);
372
- BSON_ASSERT_PARAM (out);
373
-
374
- /* Construct the find filter to fetch keys. */
375
- if (!_mongocrypt_key_broker_filter (&ctx->kb, out)) {
376
- BSON_ASSERT (!_mongocrypt_key_broker_status (&ctx->kb, ctx->status));
377
- return _mongocrypt_ctx_fail (ctx);
378
- }
379
- return true;
380
- }
133
+ new_key_alt_name = _mongocrypt_key_alt_name_new(bson_iter_value(&iter));
381
134
 
135
+ if (ctx->opts.key_alt_names && _mongocrypt_key_alt_name_intersects(ctx->opts.key_alt_names, new_key_alt_name)) {
136
+ _mongocrypt_key_alt_name_destroy_all(new_key_alt_name);
137
+ return _mongocrypt_ctx_fail_w_msg(ctx, "duplicate keyAltNames found");
138
+ }
139
+ new_key_alt_name->next = ctx->opts.key_alt_names;
140
+ ctx->opts.key_alt_names = new_key_alt_name;
382
141
 
383
- static bool
384
- _mongo_feed_keys (mongocrypt_ctx_t *ctx, mongocrypt_binary_t *in)
385
- {
386
- _mongocrypt_buffer_t buf;
387
-
388
- BSON_ASSERT_PARAM (ctx);
389
- BSON_ASSERT_PARAM (in);
390
-
391
- _mongocrypt_buffer_from_binary (&buf, in);
392
- if (!_mongocrypt_key_broker_add_doc (
393
- &ctx->kb, _mongocrypt_ctx_kms_providers (ctx), &buf)) {
394
- BSON_ASSERT (!_mongocrypt_key_broker_status (&ctx->kb, ctx->status));
395
- return _mongocrypt_ctx_fail (ctx);
396
- }
397
- return true;
142
+ if (bson_iter_next(&iter)) {
143
+ return _mongocrypt_ctx_fail_w_msg(ctx, "unrecognized field, only keyAltName expected");
144
+ }
145
+
146
+ return true;
398
147
  }
399
148
 
149
+ bool mongocrypt_ctx_setopt_key_material(mongocrypt_ctx_t *ctx, mongocrypt_binary_t *key_material) {
150
+ bson_t as_bson;
151
+ bson_iter_t iter;
152
+ const char *key;
153
+ _mongocrypt_buffer_t buffer;
154
+
155
+ if (!ctx) {
156
+ return false;
157
+ }
158
+
159
+ if (ctx->initialized) {
160
+ return _mongocrypt_ctx_fail_w_msg(ctx, "cannot set options after init");
161
+ }
162
+
163
+ if (ctx->opts.key_material.owned) {
164
+ return _mongocrypt_ctx_fail_w_msg(ctx, "keyMaterial already set");
165
+ }
166
+
167
+ if (ctx->state == MONGOCRYPT_CTX_ERROR) {
168
+ return false;
169
+ }
170
+
171
+ if (!key_material || !key_material->data) {
172
+ return _mongocrypt_ctx_fail_w_msg(ctx, "option must be non-NULL");
173
+ }
174
+
175
+ if (!_mongocrypt_binary_to_bson(key_material, &as_bson)) {
176
+ return _mongocrypt_ctx_fail_w_msg(ctx, "invalid keyMaterial bson object");
177
+ }
178
+
179
+ /* TODO: use _mongocrypt_parse_required_binary once MONGOCRYPT-380 is
180
+ * resolved.*/
181
+ if (!bson_iter_init(&iter, &as_bson) || !bson_iter_next(&iter)) {
182
+ return _mongocrypt_ctx_fail_w_msg(ctx, "invalid bson");
183
+ }
184
+
185
+ key = bson_iter_key(&iter);
186
+ BSON_ASSERT(key);
187
+ if (0 != strcmp(key, "keyMaterial")) {
188
+ return _mongocrypt_ctx_fail_w_msg(ctx, "keyMaterial must have field 'keyMaterial'");
189
+ }
190
+
191
+ if (!_mongocrypt_buffer_from_binary_iter(&buffer, &iter)) {
192
+ return _mongocrypt_ctx_fail_w_msg(ctx, "keyMaterial must be binary data");
193
+ }
194
+
195
+ if (buffer.len != MONGOCRYPT_KEY_LEN) {
196
+ _mongocrypt_set_error(ctx->status,
197
+ MONGOCRYPT_STATUS_ERROR_CLIENT,
198
+ MONGOCRYPT_GENERIC_ERROR_CODE,
199
+ "keyMaterial should have length %d, but has length %" PRIu32,
200
+ MONGOCRYPT_KEY_LEN,
201
+ buffer.len);
202
+ return _mongocrypt_ctx_fail(ctx);
203
+ }
204
+
205
+ _mongocrypt_buffer_steal(&ctx->opts.key_material, &buffer);
206
+
207
+ if (bson_iter_next(&iter)) {
208
+ return _mongocrypt_ctx_fail_w_msg(ctx, "unrecognized field, only keyMaterial expected");
209
+ }
210
+
211
+ return true;
212
+ }
400
213
 
401
- static bool
402
- _mongo_done_keys (mongocrypt_ctx_t *ctx)
403
- {
404
- BSON_ASSERT_PARAM (ctx);
214
+ bool mongocrypt_ctx_setopt_algorithm(mongocrypt_ctx_t *ctx, const char *algorithm, int len) {
215
+ if (!ctx) {
216
+ return false;
217
+ }
218
+
219
+ if (ctx->initialized) {
220
+ return _mongocrypt_ctx_fail_w_msg(ctx, "cannot set options after init");
221
+ }
222
+
223
+ if (ctx->state == MONGOCRYPT_CTX_ERROR) {
224
+ return false;
225
+ }
226
+
227
+ if (ctx->opts.algorithm != MONGOCRYPT_ENCRYPTION_ALGORITHM_NONE || ctx->opts.index_type.set) {
228
+ return _mongocrypt_ctx_fail_w_msg(ctx, "already set algorithm");
229
+ }
230
+
231
+ if (len < -1) {
232
+ return _mongocrypt_ctx_fail_w_msg(ctx, "invalid algorithm length");
233
+ }
234
+
235
+ if (!algorithm) {
236
+ return _mongocrypt_ctx_fail_w_msg(ctx, "passed null algorithm");
237
+ }
238
+
239
+ const size_t calculated_len = len == -1 ? strlen(algorithm) : (size_t)len;
240
+ if (ctx->crypt->log.trace_enabled) {
241
+ _mongocrypt_log(&ctx->crypt->log,
242
+ MONGOCRYPT_LOG_LEVEL_TRACE,
243
+ "%s (%s=\"%.*s\")",
244
+ BSON_FUNC,
245
+ "algorithm",
246
+ calculated_len <= (size_t)INT_MAX ? (int)calculated_len : INT_MAX,
247
+ algorithm);
248
+ }
249
+
250
+ mstr_view algo_str = mstrv_view_data(algorithm, calculated_len);
251
+ if (mstr_eq_ignore_case(algo_str, mstrv_lit(MONGOCRYPT_ALGORITHM_DETERMINISTIC_STR))) {
252
+ ctx->opts.algorithm = MONGOCRYPT_ENCRYPTION_ALGORITHM_DETERMINISTIC;
253
+ } else if (mstr_eq_ignore_case(algo_str, mstrv_lit(MONGOCRYPT_ALGORITHM_RANDOM_STR))) {
254
+ ctx->opts.algorithm = MONGOCRYPT_ENCRYPTION_ALGORITHM_RANDOM;
255
+ } else if (mstr_eq_ignore_case(algo_str, mstrv_lit(MONGOCRYPT_ALGORITHM_INDEXED_STR))) {
256
+ ctx->opts.index_type.value = MONGOCRYPT_INDEX_TYPE_EQUALITY;
257
+ ctx->opts.index_type.set = true;
258
+ } else if (mstr_eq_ignore_case(algo_str, mstrv_lit(MONGOCRYPT_ALGORITHM_UNINDEXED_STR))) {
259
+ ctx->opts.index_type.value = MONGOCRYPT_INDEX_TYPE_NONE;
260
+ ctx->opts.index_type.set = true;
261
+ } else if (mstr_eq_ignore_case(algo_str, mstrv_lit(MONGOCRYPT_ALGORITHM_RANGE_STR))) {
262
+ ctx->opts.index_type.value = MONGOCRYPT_INDEX_TYPE_RANGE;
263
+ ctx->opts.index_type.set = true;
264
+ } else if (mstr_eq_ignore_case(algo_str, mstrv_lit(MONGOCRYPT_ALGORITHM_RANGEPREVIEW_DEPRECATED_STR))) {
265
+ if (ctx->crypt->opts.use_range_v2) {
266
+ _mongocrypt_ctx_fail_w_msg(ctx, "Algorithm 'rangePreview' is deprecated, please use 'range'");
267
+ return false;
268
+ }
269
+ ctx->opts.index_type.value = MONGOCRYPT_INDEX_TYPE_RANGEPREVIEW_DEPRECATED;
270
+ ctx->opts.index_type.set = true;
271
+ } else {
272
+ char *error = bson_strdup_printf("unsupported algorithm string \"%.*s\"",
273
+ algo_str.len <= (size_t)INT_MAX ? (int)algo_str.len : INT_MAX,
274
+ algo_str.data);
275
+ _mongocrypt_ctx_fail_w_msg(ctx, error);
276
+ bson_free(error);
277
+ return false;
278
+ }
279
+
280
+ return true;
281
+ }
405
282
 
406
- (void) _mongocrypt_key_broker_docs_done (&ctx->kb);
407
- return _mongocrypt_ctx_state_from_key_broker (ctx);
283
+ mongocrypt_ctx_t *mongocrypt_ctx_new(mongocrypt_t *crypt) {
284
+ mongocrypt_ctx_t *ctx;
285
+ size_t ctx_size;
286
+
287
+ if (!crypt) {
288
+ return NULL;
289
+ }
290
+ if (!crypt->initialized) {
291
+ mongocrypt_status_t *status;
292
+
293
+ status = crypt->status;
294
+ CLIENT_ERR("cannot create context from uninitialized crypt");
295
+ return NULL;
296
+ }
297
+ ctx_size = sizeof(_mongocrypt_ctx_encrypt_t);
298
+ if (sizeof(_mongocrypt_ctx_decrypt_t) > ctx_size) {
299
+ ctx_size = sizeof(_mongocrypt_ctx_decrypt_t);
300
+ }
301
+ if (sizeof(_mongocrypt_ctx_datakey_t) > ctx_size) {
302
+ ctx_size = sizeof(_mongocrypt_ctx_datakey_t);
303
+ }
304
+ ctx = bson_malloc0(ctx_size);
305
+ BSON_ASSERT(ctx);
306
+
307
+ ctx->crypt = crypt;
308
+ ctx->status = mongocrypt_status_new();
309
+ ctx->opts.algorithm = MONGOCRYPT_ENCRYPTION_ALGORITHM_NONE;
310
+ ctx->state = MONGOCRYPT_CTX_DONE;
311
+ return ctx;
408
312
  }
409
313
 
410
- static mongocrypt_kms_ctx_t *
411
- _next_kms_ctx (mongocrypt_ctx_t *ctx)
412
- {
413
- BSON_ASSERT_PARAM (ctx);
314
+ #define CHECK_AND_CALL(fn, ...) \
315
+ do { \
316
+ if (!ctx->vtable.fn) { \
317
+ return _mongocrypt_ctx_fail_w_msg(ctx, "not applicable to context"); \
318
+ } \
319
+ return ctx->vtable.fn(__VA_ARGS__); \
320
+ } while (0)
414
321
 
415
- return _mongocrypt_key_broker_next_kms (&ctx->kb);
322
+ /* Common to both encrypt and decrypt context. */
323
+ static bool _mongo_op_keys(mongocrypt_ctx_t *ctx, mongocrypt_binary_t *out) {
324
+ BSON_ASSERT_PARAM(ctx);
325
+ BSON_ASSERT_PARAM(out);
326
+
327
+ /* Construct the find filter to fetch keys. */
328
+ if (!_mongocrypt_key_broker_filter(&ctx->kb, out)) {
329
+ BSON_ASSERT(!_mongocrypt_key_broker_status(&ctx->kb, ctx->status));
330
+ return _mongocrypt_ctx_fail(ctx);
331
+ }
332
+ return true;
416
333
  }
417
334
 
335
+ static bool _mongo_feed_keys(mongocrypt_ctx_t *ctx, mongocrypt_binary_t *in) {
336
+ _mongocrypt_buffer_t buf;
418
337
 
419
- static bool
420
- _kms_done (mongocrypt_ctx_t *ctx)
421
- {
422
- _mongocrypt_opts_kms_providers_t *kms_providers;
338
+ BSON_ASSERT_PARAM(ctx);
339
+ BSON_ASSERT_PARAM(in);
423
340
 
424
- BSON_ASSERT_PARAM (ctx);
341
+ _mongocrypt_buffer_from_binary(&buf, in);
342
+ if (!_mongocrypt_key_broker_add_doc(&ctx->kb, _mongocrypt_ctx_kms_providers(ctx), &buf)) {
343
+ BSON_ASSERT(!_mongocrypt_key_broker_status(&ctx->kb, ctx->status));
344
+ return _mongocrypt_ctx_fail(ctx);
345
+ }
346
+ return true;
347
+ }
425
348
 
426
- kms_providers = _mongocrypt_ctx_kms_providers (ctx);
349
+ static bool _mongo_done_keys(mongocrypt_ctx_t *ctx) {
350
+ BSON_ASSERT_PARAM(ctx);
427
351
 
428
- if (!_mongocrypt_key_broker_kms_done (&ctx->kb, kms_providers)) {
429
- BSON_ASSERT (!_mongocrypt_key_broker_status (&ctx->kb, ctx->status));
430
- return _mongocrypt_ctx_fail (ctx);
431
- }
432
- return _mongocrypt_ctx_state_from_key_broker (ctx);
352
+ (void)_mongocrypt_key_broker_docs_done(&ctx->kb);
353
+ return _mongocrypt_ctx_state_from_key_broker(ctx);
433
354
  }
434
355
 
356
+ static mongocrypt_kms_ctx_t *_next_kms_ctx(mongocrypt_ctx_t *ctx) {
357
+ BSON_ASSERT_PARAM(ctx);
435
358
 
436
- bool
437
- mongocrypt_ctx_mongo_op (mongocrypt_ctx_t *ctx, mongocrypt_binary_t *out)
438
- {
439
- if (!ctx) {
440
- return false;
441
- }
442
- if (!ctx->initialized) {
443
- return _mongocrypt_ctx_fail_w_msg (ctx, "ctx NULL or uninitialized");
444
- }
445
-
446
- if (!out) {
447
- return _mongocrypt_ctx_fail_w_msg (ctx, "invalid NULL output");
448
- }
449
-
450
- switch (ctx->state) {
451
- case MONGOCRYPT_CTX_NEED_MONGO_COLLINFO:
452
- CHECK_AND_CALL (mongo_op_collinfo, ctx, out);
453
- case MONGOCRYPT_CTX_NEED_MONGO_MARKINGS:
454
- CHECK_AND_CALL (mongo_op_markings, ctx, out);
455
- case MONGOCRYPT_CTX_NEED_MONGO_KEYS:
456
- CHECK_AND_CALL (mongo_op_keys, ctx, out);
457
- case MONGOCRYPT_CTX_ERROR:
458
- return false;
459
- case MONGOCRYPT_CTX_DONE:
460
- case MONGOCRYPT_CTX_NEED_KMS_CREDENTIALS:
461
- case MONGOCRYPT_CTX_NEED_KMS:
462
- case MONGOCRYPT_CTX_READY:
463
- default:
464
- return _mongocrypt_ctx_fail_w_msg (ctx, "wrong state");
465
- }
359
+ return _mongocrypt_key_broker_next_kms(&ctx->kb);
466
360
  }
467
361
 
362
+ static bool _kms_done(mongocrypt_ctx_t *ctx) {
363
+ _mongocrypt_opts_kms_providers_t *kms_providers;
468
364
 
469
- bool
470
- mongocrypt_ctx_mongo_feed (mongocrypt_ctx_t *ctx, mongocrypt_binary_t *in)
471
- {
472
- if (!ctx) {
473
- return false;
474
- }
475
- if (!ctx->initialized) {
476
- return _mongocrypt_ctx_fail_w_msg (ctx, "ctx NULL or uninitialized");
477
- }
478
-
479
- if (!in) {
480
- return _mongocrypt_ctx_fail_w_msg (ctx, "invalid NULL input");
481
- }
482
-
483
- if (ctx->crypt->log.trace_enabled) {
484
- char *in_val;
485
-
486
- in_val = _mongocrypt_new_json_string_from_binary (in);
487
- _mongocrypt_log (&ctx->crypt->log,
488
- MONGOCRYPT_LOG_LEVEL_TRACE,
489
- "%s (%s=\"%s\")",
490
- BSON_FUNC,
491
- "in",
492
- in_val);
493
- bson_free (in_val);
494
- }
495
-
496
- switch (ctx->state) {
497
- case MONGOCRYPT_CTX_NEED_MONGO_COLLINFO:
498
- CHECK_AND_CALL (mongo_feed_collinfo, ctx, in);
499
- case MONGOCRYPT_CTX_NEED_MONGO_MARKINGS:
500
- CHECK_AND_CALL (mongo_feed_markings, ctx, in);
501
- case MONGOCRYPT_CTX_NEED_MONGO_KEYS:
502
- CHECK_AND_CALL (mongo_feed_keys, ctx, in);
503
- case MONGOCRYPT_CTX_ERROR:
504
- return false;
505
- case MONGOCRYPT_CTX_DONE:
506
- case MONGOCRYPT_CTX_NEED_KMS_CREDENTIALS:
507
- case MONGOCRYPT_CTX_NEED_KMS:
508
- case MONGOCRYPT_CTX_READY:
509
- default:
510
- return _mongocrypt_ctx_fail_w_msg (ctx, "wrong state");
511
- }
512
- }
365
+ BSON_ASSERT_PARAM(ctx);
513
366
 
367
+ kms_providers = _mongocrypt_ctx_kms_providers(ctx);
368
+
369
+ if (!_mongocrypt_key_broker_kms_done(&ctx->kb, kms_providers)) {
370
+ BSON_ASSERT(!_mongocrypt_key_broker_status(&ctx->kb, ctx->status));
371
+ return _mongocrypt_ctx_fail(ctx);
372
+ }
373
+ return _mongocrypt_ctx_state_from_key_broker(ctx);
374
+ }
514
375
 
515
- bool
516
- mongocrypt_ctx_mongo_done (mongocrypt_ctx_t *ctx)
517
- {
518
- if (!ctx) {
519
- return false;
520
- }
521
- if (!ctx->initialized) {
522
- return _mongocrypt_ctx_fail_w_msg (ctx, "ctx NULL or uninitialized");
523
- }
524
-
525
- switch (ctx->state) {
526
- case MONGOCRYPT_CTX_NEED_MONGO_COLLINFO:
527
- CHECK_AND_CALL (mongo_done_collinfo, ctx);
528
- case MONGOCRYPT_CTX_NEED_MONGO_MARKINGS:
529
- CHECK_AND_CALL (mongo_done_markings, ctx);
530
- case MONGOCRYPT_CTX_NEED_MONGO_KEYS:
531
- CHECK_AND_CALL (mongo_done_keys, ctx);
532
- case MONGOCRYPT_CTX_ERROR:
533
- return false;
534
- case MONGOCRYPT_CTX_DONE:
535
- case MONGOCRYPT_CTX_NEED_KMS_CREDENTIALS:
536
- case MONGOCRYPT_CTX_NEED_KMS:
537
- case MONGOCRYPT_CTX_READY:
538
- default:
539
- return _mongocrypt_ctx_fail_w_msg (ctx, "wrong state");
540
- }
376
+ bool mongocrypt_ctx_mongo_op(mongocrypt_ctx_t *ctx, mongocrypt_binary_t *out) {
377
+ if (!ctx) {
378
+ return false;
379
+ }
380
+ if (!ctx->initialized) {
381
+ return _mongocrypt_ctx_fail_w_msg(ctx, "ctx NULL or uninitialized");
382
+ }
383
+
384
+ if (!out) {
385
+ return _mongocrypt_ctx_fail_w_msg(ctx, "invalid NULL output");
386
+ }
387
+
388
+ switch (ctx->state) {
389
+ case MONGOCRYPT_CTX_NEED_MONGO_COLLINFO_WITH_DB:
390
+ case MONGOCRYPT_CTX_NEED_MONGO_COLLINFO: CHECK_AND_CALL(mongo_op_collinfo, ctx, out);
391
+ case MONGOCRYPT_CTX_NEED_MONGO_MARKINGS: CHECK_AND_CALL(mongo_op_markings, ctx, out);
392
+ case MONGOCRYPT_CTX_NEED_MONGO_KEYS: CHECK_AND_CALL(mongo_op_keys, ctx, out);
393
+ case MONGOCRYPT_CTX_ERROR: return false;
394
+ case MONGOCRYPT_CTX_DONE:
395
+ case MONGOCRYPT_CTX_NEED_KMS_CREDENTIALS:
396
+ case MONGOCRYPT_CTX_NEED_KMS:
397
+ case MONGOCRYPT_CTX_READY:
398
+ default: return _mongocrypt_ctx_fail_w_msg(ctx, "wrong state");
399
+ }
541
400
  }
542
401
 
402
+ const char *mongocrypt_ctx_mongo_db(mongocrypt_ctx_t *ctx) {
403
+ if (!ctx) {
404
+ return NULL;
405
+ }
406
+ if (!ctx->initialized) {
407
+ _mongocrypt_ctx_fail_w_msg(ctx, "ctx NULL or uninitialized");
408
+ return NULL;
409
+ }
410
+
411
+ switch (ctx->state) {
412
+ case MONGOCRYPT_CTX_NEED_MONGO_COLLINFO_WITH_DB: {
413
+ if (!ctx->vtable.mongo_db_collinfo) {
414
+ _mongocrypt_ctx_fail_w_msg(ctx, "not applicable to context");
415
+ return NULL;
416
+ }
417
+ return ctx->vtable.mongo_db_collinfo(ctx);
418
+ }
419
+ case MONGOCRYPT_CTX_ERROR: return false;
420
+ case MONGOCRYPT_CTX_NEED_MONGO_COLLINFO:
421
+ case MONGOCRYPT_CTX_NEED_MONGO_MARKINGS:
422
+ case MONGOCRYPT_CTX_NEED_MONGO_KEYS:
423
+ case MONGOCRYPT_CTX_DONE:
424
+ case MONGOCRYPT_CTX_NEED_KMS_CREDENTIALS:
425
+ case MONGOCRYPT_CTX_NEED_KMS:
426
+ case MONGOCRYPT_CTX_READY:
427
+ default: {
428
+ _mongocrypt_ctx_fail_w_msg(ctx, "wrong state");
429
+ return NULL;
430
+ }
431
+ }
432
+ }
543
433
 
544
- mongocrypt_ctx_state_t
545
- mongocrypt_ctx_state (mongocrypt_ctx_t *ctx)
546
- {
547
- if (!ctx) {
548
- return MONGOCRYPT_CTX_ERROR;
549
- }
550
- if (!ctx->initialized) {
551
- _mongocrypt_ctx_fail_w_msg (ctx, "ctx NULL or uninitialized");
552
- return MONGOCRYPT_CTX_ERROR;
553
- }
434
+ bool mongocrypt_ctx_mongo_feed(mongocrypt_ctx_t *ctx, mongocrypt_binary_t *in) {
435
+ if (!ctx) {
436
+ return false;
437
+ }
438
+ if (!ctx->initialized) {
439
+ return _mongocrypt_ctx_fail_w_msg(ctx, "ctx NULL or uninitialized");
440
+ }
441
+
442
+ if (!in) {
443
+ return _mongocrypt_ctx_fail_w_msg(ctx, "invalid NULL input");
444
+ }
445
+
446
+ if (ctx->crypt->log.trace_enabled) {
447
+ char *in_val;
448
+
449
+ in_val = _mongocrypt_new_json_string_from_binary(in);
450
+ _mongocrypt_log(&ctx->crypt->log, MONGOCRYPT_LOG_LEVEL_TRACE, "%s (%s=\"%s\")", BSON_FUNC, "in", in_val);
451
+ bson_free(in_val);
452
+ }
453
+
454
+ switch (ctx->state) {
455
+ case MONGOCRYPT_CTX_NEED_MONGO_COLLINFO_WITH_DB:
456
+ case MONGOCRYPT_CTX_NEED_MONGO_COLLINFO: CHECK_AND_CALL(mongo_feed_collinfo, ctx, in);
457
+ case MONGOCRYPT_CTX_NEED_MONGO_MARKINGS: CHECK_AND_CALL(mongo_feed_markings, ctx, in);
458
+ case MONGOCRYPT_CTX_NEED_MONGO_KEYS: CHECK_AND_CALL(mongo_feed_keys, ctx, in);
459
+ case MONGOCRYPT_CTX_ERROR: return false;
460
+ case MONGOCRYPT_CTX_DONE:
461
+ case MONGOCRYPT_CTX_NEED_KMS_CREDENTIALS:
462
+ case MONGOCRYPT_CTX_NEED_KMS:
463
+ case MONGOCRYPT_CTX_READY:
464
+ default: return _mongocrypt_ctx_fail_w_msg(ctx, "wrong state");
465
+ }
466
+ }
554
467
 
555
- return ctx->state;
468
+ bool mongocrypt_ctx_mongo_done(mongocrypt_ctx_t *ctx) {
469
+ if (!ctx) {
470
+ return false;
471
+ }
472
+ if (!ctx->initialized) {
473
+ return _mongocrypt_ctx_fail_w_msg(ctx, "ctx NULL or uninitialized");
474
+ }
475
+
476
+ switch (ctx->state) {
477
+ case MONGOCRYPT_CTX_NEED_MONGO_COLLINFO_WITH_DB:
478
+ case MONGOCRYPT_CTX_NEED_MONGO_COLLINFO: CHECK_AND_CALL(mongo_done_collinfo, ctx);
479
+ case MONGOCRYPT_CTX_NEED_MONGO_MARKINGS: CHECK_AND_CALL(mongo_done_markings, ctx);
480
+ case MONGOCRYPT_CTX_NEED_MONGO_KEYS: CHECK_AND_CALL(mongo_done_keys, ctx);
481
+ case MONGOCRYPT_CTX_ERROR: return false;
482
+ case MONGOCRYPT_CTX_DONE:
483
+ case MONGOCRYPT_CTX_NEED_KMS_CREDENTIALS:
484
+ case MONGOCRYPT_CTX_NEED_KMS:
485
+ case MONGOCRYPT_CTX_READY:
486
+ default: return _mongocrypt_ctx_fail_w_msg(ctx, "wrong state");
487
+ }
556
488
  }
557
489
 
490
+ mongocrypt_ctx_state_t mongocrypt_ctx_state(mongocrypt_ctx_t *ctx) {
491
+ if (!ctx) {
492
+ return MONGOCRYPT_CTX_ERROR;
493
+ }
494
+ if (!ctx->initialized) {
495
+ _mongocrypt_ctx_fail_w_msg(ctx, "ctx NULL or uninitialized");
496
+ return MONGOCRYPT_CTX_ERROR;
497
+ }
498
+
499
+ return ctx->state;
500
+ }
558
501
 
559
- mongocrypt_kms_ctx_t *
560
- mongocrypt_ctx_next_kms_ctx (mongocrypt_ctx_t *ctx)
561
- {
562
- if (!ctx) {
563
- return NULL;
564
- }
565
- if (!ctx->initialized) {
566
- _mongocrypt_ctx_fail_w_msg (ctx, "ctx NULL or uninitialized");
567
- return NULL;
568
- }
569
-
570
- if (!ctx->vtable.next_kms_ctx) {
571
- _mongocrypt_ctx_fail_w_msg (ctx, "not applicable to context");
572
- return NULL;
573
- }
574
-
575
- switch (ctx->state) {
576
- case MONGOCRYPT_CTX_NEED_KMS:
577
- return ctx->vtable.next_kms_ctx (ctx);
578
- case MONGOCRYPT_CTX_ERROR:
579
- return NULL;
580
- case MONGOCRYPT_CTX_DONE:
581
- case MONGOCRYPT_CTX_NEED_KMS_CREDENTIALS:
582
- case MONGOCRYPT_CTX_NEED_MONGO_COLLINFO:
583
- case MONGOCRYPT_CTX_NEED_MONGO_KEYS:
584
- case MONGOCRYPT_CTX_NEED_MONGO_MARKINGS:
585
- case MONGOCRYPT_CTX_READY:
586
- default:
587
- _mongocrypt_ctx_fail_w_msg (ctx, "wrong state");
588
- return NULL;
589
- }
502
+ mongocrypt_kms_ctx_t *mongocrypt_ctx_next_kms_ctx(mongocrypt_ctx_t *ctx) {
503
+ if (!ctx) {
504
+ return NULL;
505
+ }
506
+ if (!ctx->initialized) {
507
+ _mongocrypt_ctx_fail_w_msg(ctx, "ctx NULL or uninitialized");
508
+ return NULL;
509
+ }
510
+
511
+ if (!ctx->vtable.next_kms_ctx) {
512
+ _mongocrypt_ctx_fail_w_msg(ctx, "not applicable to context");
513
+ return NULL;
514
+ }
515
+
516
+ switch (ctx->state) {
517
+ case MONGOCRYPT_CTX_NEED_KMS: return ctx->vtable.next_kms_ctx(ctx);
518
+ case MONGOCRYPT_CTX_ERROR: return NULL;
519
+ case MONGOCRYPT_CTX_DONE:
520
+ case MONGOCRYPT_CTX_NEED_KMS_CREDENTIALS:
521
+ case MONGOCRYPT_CTX_NEED_MONGO_COLLINFO_WITH_DB:
522
+ case MONGOCRYPT_CTX_NEED_MONGO_COLLINFO:
523
+ case MONGOCRYPT_CTX_NEED_MONGO_KEYS:
524
+ case MONGOCRYPT_CTX_NEED_MONGO_MARKINGS:
525
+ case MONGOCRYPT_CTX_READY:
526
+ default: _mongocrypt_ctx_fail_w_msg(ctx, "wrong state"); return NULL;
527
+ }
590
528
  }
591
529
 
530
+ bool mongocrypt_ctx_provide_kms_providers(mongocrypt_ctx_t *ctx, mongocrypt_binary_t *kms_providers_definition) {
531
+ if (!ctx) {
532
+ return false;
533
+ }
592
534
 
593
- bool
594
- mongocrypt_ctx_provide_kms_providers (
595
- mongocrypt_ctx_t *ctx, mongocrypt_binary_t *kms_providers_definition)
596
- {
597
- if (!ctx) {
598
- return false;
599
- }
535
+ if (!ctx->initialized) {
536
+ _mongocrypt_ctx_fail_w_msg(ctx, "ctx NULL or uninitialized");
537
+ return false;
538
+ }
600
539
 
601
- if (!ctx->initialized) {
602
- _mongocrypt_ctx_fail_w_msg (ctx, "ctx NULL or uninitialized");
603
- return false;
604
- }
540
+ if (ctx->state != MONGOCRYPT_CTX_NEED_KMS_CREDENTIALS) {
541
+ _mongocrypt_ctx_fail_w_msg(ctx, "wrong state");
542
+ return false;
543
+ }
605
544
 
606
- if (ctx->state != MONGOCRYPT_CTX_NEED_KMS_CREDENTIALS) {
607
- _mongocrypt_ctx_fail_w_msg (ctx, "wrong state");
608
- return false;
609
- }
545
+ if (!kms_providers_definition) {
546
+ _mongocrypt_ctx_fail_w_msg(ctx, "KMS provider credential mapping not provided");
547
+ return false;
548
+ }
610
549
 
611
- if (!kms_providers_definition) {
612
- _mongocrypt_ctx_fail_w_msg (
613
- ctx, "KMS provider credential mapping not provided");
614
- return false;
615
- }
550
+ _mongocrypt_opts_kms_providers_init(&ctx->per_ctx_kms_providers);
616
551
 
617
- if (!_mongocrypt_parse_kms_providers (kms_providers_definition,
552
+ if (!_mongocrypt_parse_kms_providers(kms_providers_definition,
618
553
  &ctx->per_ctx_kms_providers,
619
554
  ctx->status,
620
555
  &ctx->crypt->log)) {
621
- return _mongocrypt_ctx_fail (ctx);
622
- }
623
-
624
- if (!_mongocrypt_opts_kms_providers_validate (
625
- &ctx->crypt->opts, &ctx->per_ctx_kms_providers, ctx->status)) {
626
- /* Remove the parsed KMS providers if they are invalid */
627
- _mongocrypt_opts_kms_providers_cleanup (&ctx->per_ctx_kms_providers);
628
- memset (
629
- &ctx->per_ctx_kms_providers, 0, sizeof (ctx->per_ctx_kms_providers));
630
- return _mongocrypt_ctx_fail (ctx);
631
- }
632
-
633
- memcpy (&ctx->kms_providers,
634
- &ctx->crypt->opts.kms_providers,
635
- sizeof (_mongocrypt_opts_kms_providers_t));
636
- _mongocrypt_opts_merge_kms_providers (&ctx->kms_providers,
637
- &ctx->per_ctx_kms_providers);
638
-
639
- ctx->state = ctx->kb.state == KB_ADDING_DOCS ? MONGOCRYPT_CTX_NEED_MONGO_KEYS
640
- : MONGOCRYPT_CTX_NEED_KMS;
641
- if (ctx->vtable.after_kms_credentials_provided) {
642
- return ctx->vtable.after_kms_credentials_provided (ctx);
643
- }
644
- return true;
556
+ return _mongocrypt_ctx_fail(ctx);
557
+ }
558
+
559
+ if (!_mongocrypt_opts_kms_providers_validate(&ctx->crypt->opts, &ctx->per_ctx_kms_providers, ctx->status)) {
560
+ /* Remove the parsed KMS providers if they are invalid */
561
+ _mongocrypt_opts_kms_providers_cleanup(&ctx->per_ctx_kms_providers);
562
+ memset(&ctx->per_ctx_kms_providers, 0, sizeof(ctx->per_ctx_kms_providers));
563
+ return _mongocrypt_ctx_fail(ctx);
564
+ }
565
+
566
+ memcpy(&ctx->kms_providers, &ctx->crypt->opts.kms_providers, sizeof(_mongocrypt_opts_kms_providers_t));
567
+ _mongocrypt_opts_merge_kms_providers(&ctx->kms_providers, &ctx->per_ctx_kms_providers);
568
+
569
+ ctx->state = ctx->kb.state == KB_ADDING_DOCS ? MONGOCRYPT_CTX_NEED_MONGO_KEYS : MONGOCRYPT_CTX_NEED_KMS;
570
+ if (ctx->vtable.after_kms_credentials_provided) {
571
+ return ctx->vtable.after_kms_credentials_provided(ctx);
572
+ }
573
+ return true;
645
574
  }
646
575
 
647
-
648
- bool
649
- mongocrypt_ctx_kms_done (mongocrypt_ctx_t *ctx)
650
- {
651
- if (!ctx) {
652
- return false;
653
- }
654
- if (!ctx->initialized) {
655
- return _mongocrypt_ctx_fail_w_msg (ctx, "ctx NULL or uninitialized");
656
- }
657
-
658
- if (!ctx->vtable.kms_done) {
659
- return _mongocrypt_ctx_fail_w_msg (ctx, "not applicable to context");
660
- }
661
-
662
- switch (ctx->state) {
663
- case MONGOCRYPT_CTX_NEED_KMS:
664
- return ctx->vtable.kms_done (ctx);
665
- case MONGOCRYPT_CTX_ERROR:
666
- return false;
667
- case MONGOCRYPT_CTX_DONE:
668
- case MONGOCRYPT_CTX_NEED_KMS_CREDENTIALS:
669
- case MONGOCRYPT_CTX_NEED_MONGO_COLLINFO:
670
- case MONGOCRYPT_CTX_NEED_MONGO_KEYS:
671
- case MONGOCRYPT_CTX_NEED_MONGO_MARKINGS:
672
- case MONGOCRYPT_CTX_READY:
673
- default:
674
- return _mongocrypt_ctx_fail_w_msg (ctx, "wrong state");
675
- }
576
+ bool mongocrypt_ctx_kms_done(mongocrypt_ctx_t *ctx) {
577
+ if (!ctx) {
578
+ return false;
579
+ }
580
+ if (!ctx->initialized) {
581
+ return _mongocrypt_ctx_fail_w_msg(ctx, "ctx NULL or uninitialized");
582
+ }
583
+
584
+ if (!ctx->vtable.kms_done) {
585
+ return _mongocrypt_ctx_fail_w_msg(ctx, "not applicable to context");
586
+ }
587
+
588
+ switch (ctx->state) {
589
+ case MONGOCRYPT_CTX_NEED_KMS: return ctx->vtable.kms_done(ctx);
590
+ case MONGOCRYPT_CTX_ERROR: return false;
591
+ case MONGOCRYPT_CTX_DONE:
592
+ case MONGOCRYPT_CTX_NEED_KMS_CREDENTIALS:
593
+ case MONGOCRYPT_CTX_NEED_MONGO_COLLINFO_WITH_DB:
594
+ case MONGOCRYPT_CTX_NEED_MONGO_COLLINFO:
595
+ case MONGOCRYPT_CTX_NEED_MONGO_KEYS:
596
+ case MONGOCRYPT_CTX_NEED_MONGO_MARKINGS:
597
+ case MONGOCRYPT_CTX_READY:
598
+ default: return _mongocrypt_ctx_fail_w_msg(ctx, "wrong state");
599
+ }
676
600
  }
677
601
 
602
+ bool mongocrypt_ctx_finalize(mongocrypt_ctx_t *ctx, mongocrypt_binary_t *out) {
603
+ if (!ctx) {
604
+ return false;
605
+ }
606
+ if (!ctx->initialized) {
607
+ return _mongocrypt_ctx_fail_w_msg(ctx, "ctx NULL or uninitialized");
608
+ }
609
+
610
+ if (!out) {
611
+ return _mongocrypt_ctx_fail_w_msg(ctx, "invalid NULL output");
612
+ }
613
+
614
+ if (!ctx->vtable.finalize) {
615
+ return _mongocrypt_ctx_fail_w_msg(ctx, "not applicable to context");
616
+ }
617
+
618
+ switch (ctx->state) {
619
+ case MONGOCRYPT_CTX_READY: return ctx->vtable.finalize(ctx, out);
620
+ case MONGOCRYPT_CTX_ERROR: return false;
621
+ case MONGOCRYPT_CTX_DONE:
622
+ case MONGOCRYPT_CTX_NEED_KMS_CREDENTIALS:
623
+ case MONGOCRYPT_CTX_NEED_KMS:
624
+ case MONGOCRYPT_CTX_NEED_MONGO_COLLINFO_WITH_DB:
625
+ case MONGOCRYPT_CTX_NEED_MONGO_COLLINFO:
626
+ case MONGOCRYPT_CTX_NEED_MONGO_KEYS:
627
+ case MONGOCRYPT_CTX_NEED_MONGO_MARKINGS:
628
+ default: return _mongocrypt_ctx_fail_w_msg(ctx, "wrong state");
629
+ }
630
+ }
678
631
 
679
- bool
680
- mongocrypt_ctx_finalize (mongocrypt_ctx_t *ctx, mongocrypt_binary_t *out)
681
- {
682
- if (!ctx) {
683
- return false;
684
- }
685
- if (!ctx->initialized) {
686
- return _mongocrypt_ctx_fail_w_msg (ctx, "ctx NULL or uninitialized");
687
- }
688
-
689
- if (!out) {
690
- return _mongocrypt_ctx_fail_w_msg (ctx, "invalid NULL output");
691
- }
692
-
693
- if (!ctx->vtable.finalize) {
694
- return _mongocrypt_ctx_fail_w_msg (ctx, "not applicable to context");
695
- }
696
-
697
- switch (ctx->state) {
698
- case MONGOCRYPT_CTX_READY:
699
- return ctx->vtable.finalize (ctx, out);
700
- case MONGOCRYPT_CTX_ERROR:
701
- return false;
702
- case MONGOCRYPT_CTX_DONE:
703
- case MONGOCRYPT_CTX_NEED_KMS_CREDENTIALS:
704
- case MONGOCRYPT_CTX_NEED_KMS:
705
- case MONGOCRYPT_CTX_NEED_MONGO_COLLINFO:
706
- case MONGOCRYPT_CTX_NEED_MONGO_KEYS:
707
- case MONGOCRYPT_CTX_NEED_MONGO_MARKINGS:
708
- default:
709
- return _mongocrypt_ctx_fail_w_msg (ctx, "wrong state");
710
- }
632
+ bool mongocrypt_ctx_status(mongocrypt_ctx_t *ctx, mongocrypt_status_t *out) {
633
+ if (!ctx) {
634
+ return false;
635
+ }
636
+
637
+ if (!out) {
638
+ return _mongocrypt_ctx_fail_w_msg(ctx, "invalid NULL output");
639
+ }
640
+
641
+ if (!mongocrypt_status_ok(ctx->status)) {
642
+ _mongocrypt_status_copy_to(ctx->status, out);
643
+ return false;
644
+ }
645
+ _mongocrypt_status_reset(out);
646
+ return true;
711
647
  }
712
648
 
713
- bool
714
- mongocrypt_ctx_status (mongocrypt_ctx_t *ctx, mongocrypt_status_t *out)
715
- {
716
- if (!ctx) {
717
- return false;
718
- }
719
-
720
- if (!out) {
721
- return _mongocrypt_ctx_fail_w_msg (ctx, "invalid NULL output");
722
- }
723
-
724
- if (!mongocrypt_status_ok (ctx->status)) {
725
- _mongocrypt_status_copy_to (ctx->status, out);
726
- return false;
727
- }
728
- _mongocrypt_status_reset (out);
729
- return true;
649
+ void mongocrypt_ctx_destroy(mongocrypt_ctx_t *ctx) {
650
+ if (!ctx) {
651
+ return;
652
+ }
653
+
654
+ if (ctx->vtable.cleanup) {
655
+ ctx->vtable.cleanup(ctx);
656
+ }
657
+
658
+ mc_RangeOpts_cleanup(&ctx->opts.rangeopts.value);
659
+ _mongocrypt_opts_kms_providers_cleanup(&ctx->per_ctx_kms_providers);
660
+ _mongocrypt_kek_cleanup(&ctx->opts.kek);
661
+ mongocrypt_status_destroy(ctx->status);
662
+ _mongocrypt_key_broker_cleanup(&ctx->kb);
663
+ _mongocrypt_buffer_cleanup(&ctx->opts.key_material);
664
+ _mongocrypt_key_alt_name_destroy_all(ctx->opts.key_alt_names);
665
+ _mongocrypt_buffer_cleanup(&ctx->opts.key_id);
666
+ _mongocrypt_buffer_cleanup(&ctx->opts.index_key_id);
667
+ bson_free(ctx);
668
+ return;
730
669
  }
731
670
 
671
+ bool mongocrypt_ctx_setopt_masterkey_aws(mongocrypt_ctx_t *ctx,
672
+ const char *region,
673
+ int32_t region_len,
674
+ const char *cmk,
675
+ int32_t cmk_len) {
676
+ mongocrypt_binary_t *bin;
677
+ bson_t as_bson;
678
+ bool ret;
679
+ char *temp = NULL;
680
+
681
+ if (!ctx) {
682
+ return false;
683
+ }
684
+ if (ctx->initialized) {
685
+ return _mongocrypt_ctx_fail_w_msg(ctx, "cannot set options after init");
686
+ }
687
+
688
+ if (ctx->state == MONGOCRYPT_CTX_ERROR) {
689
+ return false;
690
+ }
691
+
692
+ if (ctx->opts.kek.kms_provider != MONGOCRYPT_KMS_PROVIDER_AWS
693
+ && ctx->opts.kek.kms_provider != MONGOCRYPT_KMS_PROVIDER_NONE) {
694
+ return _mongocrypt_ctx_fail_w_msg(ctx, "master key already set");
695
+ }
696
+
697
+ if (ctx->opts.kek.kms_provider == MONGOCRYPT_KMS_PROVIDER_AWS && ctx->opts.kek.provider.aws.region) {
698
+ return _mongocrypt_ctx_fail_w_msg(ctx, "master key already set");
699
+ }
700
+
701
+ if (!_mongocrypt_validate_and_copy_string(region, region_len, &temp) || region_len == 0) {
702
+ bson_free(temp);
703
+ return _mongocrypt_ctx_fail_w_msg(ctx, "invalid region");
704
+ }
705
+ bson_free(temp);
706
+
707
+ temp = NULL;
708
+ if (!_mongocrypt_validate_and_copy_string(cmk, cmk_len, &temp) || cmk_len == 0) {
709
+ bson_free(temp);
710
+ return _mongocrypt_ctx_fail_w_msg(ctx, "invalid cmk");
711
+ }
712
+ bson_free(temp);
713
+
714
+ bson_init(&as_bson);
715
+ bson_append_utf8(&as_bson, MONGOCRYPT_STR_AND_LEN("provider"), MONGOCRYPT_STR_AND_LEN("aws"));
716
+ BSON_ASSERT(region_len <= INT_MAX);
717
+ bson_append_utf8(&as_bson, MONGOCRYPT_STR_AND_LEN("region"), region, region_len);
718
+ BSON_ASSERT(cmk_len <= INT_MAX);
719
+ bson_append_utf8(&as_bson, MONGOCRYPT_STR_AND_LEN("key"), cmk, cmk_len);
720
+ bin = mongocrypt_binary_new_from_data((uint8_t *)bson_get_data(&as_bson), as_bson.len);
721
+
722
+ ret = mongocrypt_ctx_setopt_key_encryption_key(ctx, bin);
723
+ mongocrypt_binary_destroy(bin);
724
+ bson_destroy(&as_bson);
725
+
726
+ if (ctx->crypt->log.trace_enabled) {
727
+ _mongocrypt_log(&ctx->crypt->log,
728
+ MONGOCRYPT_LOG_LEVEL_TRACE,
729
+ "%s (%s=\"%s\", %s=%d, %s=\"%s\", %s=%d)",
730
+ BSON_FUNC,
731
+ "region",
732
+ ctx->opts.kek.provider.aws.region,
733
+ "region_len",
734
+ region_len,
735
+ "cmk",
736
+ ctx->opts.kek.provider.aws.cmk,
737
+ "cmk_len",
738
+ cmk_len);
739
+ }
740
+
741
+ return ret;
742
+ }
732
743
 
733
- void
734
- mongocrypt_ctx_destroy (mongocrypt_ctx_t *ctx)
735
- {
736
- if (!ctx) {
737
- return;
738
- }
739
-
740
- if (ctx->vtable.cleanup) {
741
- ctx->vtable.cleanup (ctx);
742
- }
743
-
744
- mc_RangeOpts_cleanup (&ctx->opts.rangeopts.value);
745
- _mongocrypt_opts_kms_providers_cleanup (&ctx->per_ctx_kms_providers);
746
- _mongocrypt_kek_cleanup (&ctx->opts.kek);
747
- mongocrypt_status_destroy (ctx->status);
748
- _mongocrypt_key_broker_cleanup (&ctx->kb);
749
- _mongocrypt_buffer_cleanup (&ctx->opts.key_material);
750
- _mongocrypt_key_alt_name_destroy_all (ctx->opts.key_alt_names);
751
- _mongocrypt_buffer_cleanup (&ctx->opts.key_id);
752
- _mongocrypt_buffer_cleanup (&ctx->opts.index_key_id);
753
- bson_free (ctx);
754
- return;
744
+ bool mongocrypt_ctx_setopt_masterkey_local(mongocrypt_ctx_t *ctx) {
745
+ if (!ctx) {
746
+ return false;
747
+ }
748
+ if (ctx->initialized) {
749
+ return _mongocrypt_ctx_fail_w_msg(ctx, "cannot set options after init");
750
+ }
751
+
752
+ if (ctx->state == MONGOCRYPT_CTX_ERROR) {
753
+ return false;
754
+ }
755
+
756
+ if (ctx->opts.kek.kms_provider) {
757
+ return _mongocrypt_ctx_fail_w_msg(ctx, "master key already set");
758
+ }
759
+
760
+ ctx->opts.kek.kms_provider = MONGOCRYPT_KMS_PROVIDER_LOCAL;
761
+ ctx->opts.kek.kmsid = bson_strdup("local");
762
+ return true;
755
763
  }
756
764
 
765
+ bool _mongocrypt_ctx_init(mongocrypt_ctx_t *ctx, _mongocrypt_ctx_opts_spec_t *opts_spec) {
766
+ bool has_id = false, has_alt_name = false, has_multiple_alt_names = false;
767
+
768
+ BSON_ASSERT_PARAM(ctx);
769
+ BSON_ASSERT_PARAM(opts_spec);
770
+
771
+ // This condition is guarded in setopt_algorithm:
772
+ BSON_ASSERT(!(ctx->opts.index_type.set && ctx->opts.algorithm != MONGOCRYPT_ENCRYPTION_ALGORITHM_NONE)
773
+ && "Both an encryption algorithm and an index_type were set.");
774
+
775
+ if (ctx->initialized) {
776
+ return _mongocrypt_ctx_fail_w_msg(ctx, "cannot double initialize");
777
+ }
778
+ ctx->initialized = true;
779
+
780
+ if (ctx->state == MONGOCRYPT_CTX_ERROR) {
781
+ return false;
782
+ }
783
+ /* Set some default functions */
784
+ ctx->vtable.mongo_op_keys = _mongo_op_keys;
785
+ ctx->vtable.mongo_feed_keys = _mongo_feed_keys;
786
+ ctx->vtable.mongo_done_keys = _mongo_done_keys;
787
+ ctx->vtable.next_kms_ctx = _next_kms_ctx;
788
+ ctx->vtable.kms_done = _kms_done;
789
+
790
+ /* Check that required options are included and prohibited options are
791
+ * not.
792
+ */
793
+
794
+ if (opts_spec->kek == OPT_REQUIRED) {
795
+ if (!ctx->opts.kek.kms_provider) {
796
+ return _mongocrypt_ctx_fail_w_msg(ctx, "master key required");
797
+ }
798
+ mc_kms_creds_t unused;
799
+ bool is_configured =
800
+ _mongocrypt_opts_kms_providers_lookup(_mongocrypt_ctx_kms_providers(ctx), ctx->opts.kek.kmsid, &unused);
801
+ if (!ctx->crypt->opts.use_need_kms_credentials_state && !is_configured) {
802
+ mongocrypt_status_t *status = ctx->status;
803
+ CLIENT_ERR("requested kms provider not configured: `%s`", ctx->opts.kek.kmsid);
804
+ return _mongocrypt_ctx_fail(ctx);
805
+ }
806
+ }
807
+
808
+ if (opts_spec->kek == OPT_PROHIBITED && ctx->opts.kek.kms_provider) {
809
+ return _mongocrypt_ctx_fail_w_msg(ctx, "master key prohibited");
810
+ }
811
+
812
+ /* Check that the kms provider required by the datakey is configured. */
813
+ if (ctx->opts.kek.kms_provider) {
814
+ mc_kms_creds_t unused;
815
+ bool is_configured =
816
+ _mongocrypt_opts_kms_providers_lookup(_mongocrypt_ctx_kms_providers(ctx), ctx->opts.kek.kmsid, &unused);
817
+ bool needs = _mongocrypt_needs_credentials_for_provider(ctx->crypt,
818
+ ctx->opts.kek.kms_provider,
819
+ ctx->opts.kek.kmsid_name);
820
+ if (!is_configured && !needs) {
821
+ mongocrypt_status_t *status = ctx->status;
822
+ CLIENT_ERR("requested kms provider required by datakey is not configured: `%s`", ctx->opts.kek.kmsid);
823
+ return _mongocrypt_ctx_fail(ctx);
824
+ }
825
+ }
826
+
827
+ /* Special case. key_descriptor applies to explicit encryption. It must be
828
+ * either a key id or *one* key alt name, but not both.
829
+ * key_alt_names applies to creating a data key. It may be one or multiple
830
+ * key alt names.
831
+ */
832
+ has_id = !_mongocrypt_buffer_empty(&ctx->opts.key_id);
833
+ has_alt_name = !!(ctx->opts.key_alt_names);
834
+ has_multiple_alt_names = has_alt_name && !!(ctx->opts.key_alt_names->next);
835
+
836
+ if (opts_spec->key_descriptor == OPT_REQUIRED) {
837
+ if (!has_id && !has_alt_name) {
838
+ return _mongocrypt_ctx_fail_w_msg(ctx, "either key id or key alt name required");
839
+ }
840
+
841
+ if (has_id && has_alt_name) {
842
+ return _mongocrypt_ctx_fail_w_msg(ctx, "cannot have both key id and key alt name");
843
+ }
844
+
845
+ if (has_multiple_alt_names) {
846
+ return _mongocrypt_ctx_fail_w_msg(ctx, "must not specify multiple key alt names");
847
+ }
848
+ }
849
+
850
+ if (opts_spec->key_descriptor == OPT_PROHIBITED) {
851
+ /* still okay if key_alt_names are allowed and only alt names were
852
+ * specified. */
853
+ if ((opts_spec->key_alt_names == OPT_PROHIBITED && has_alt_name) || has_id) {
854
+ return _mongocrypt_ctx_fail_w_msg(ctx, "key id and alt name prohibited");
855
+ }
856
+ }
857
+
858
+ if (opts_spec->key_material == OPT_PROHIBITED && ctx->opts.key_material.owned) {
859
+ return _mongocrypt_ctx_fail_w_msg(ctx, "key material prohibited");
860
+ }
861
+
862
+ if (opts_spec->algorithm == OPT_REQUIRED && ctx->opts.algorithm == MONGOCRYPT_ENCRYPTION_ALGORITHM_NONE) {
863
+ return _mongocrypt_ctx_fail_w_msg(ctx, "algorithm required");
864
+ }
865
+
866
+ if (opts_spec->algorithm == OPT_PROHIBITED && ctx->opts.algorithm != MONGOCRYPT_ENCRYPTION_ALGORITHM_NONE) {
867
+ return _mongocrypt_ctx_fail_w_msg(ctx, "algorithm prohibited");
868
+ }
869
+
870
+ if (opts_spec->rangeopts == OPT_PROHIBITED && ctx->opts.rangeopts.set) {
871
+ return _mongocrypt_ctx_fail_w_msg(ctx, "range opts are prohibited on this context");
872
+ }
873
+
874
+ _mongocrypt_key_broker_init(&ctx->kb, ctx->crypt);
875
+ return true;
876
+ }
757
877
 
758
- bool
759
- mongocrypt_ctx_setopt_masterkey_aws (mongocrypt_ctx_t *ctx,
760
- const char *region,
761
- int32_t region_len,
762
- const char *cmk,
763
- int32_t cmk_len)
764
- {
765
- mongocrypt_binary_t *bin;
766
- bson_t as_bson;
767
- bool ret;
768
- char *temp = NULL;
769
-
770
- if (!ctx) {
771
- return false;
772
- }
773
- if (ctx->initialized) {
774
- return _mongocrypt_ctx_fail_w_msg (ctx, "cannot set options after init");
775
- }
776
-
777
- if (ctx->state == MONGOCRYPT_CTX_ERROR) {
778
- return false;
779
- }
780
-
781
- if (ctx->opts.kek.kms_provider != MONGOCRYPT_KMS_PROVIDER_AWS &&
782
- ctx->opts.kek.kms_provider != MONGOCRYPT_KMS_PROVIDER_NONE) {
783
- return _mongocrypt_ctx_fail_w_msg (ctx, "master key already set");
784
- }
785
-
786
- if (ctx->opts.kek.kms_provider == MONGOCRYPT_KMS_PROVIDER_AWS &&
787
- ctx->opts.kek.provider.aws.region) {
788
- return _mongocrypt_ctx_fail_w_msg (ctx, "master key already set");
789
- }
790
-
791
- if (!_mongocrypt_validate_and_copy_string (region, region_len, &temp) ||
792
- region_len == 0) {
793
- bson_free (temp);
794
- return _mongocrypt_ctx_fail_w_msg (ctx, "invalid region");
795
- }
796
- bson_free (temp);
797
-
798
- temp = NULL;
799
- if (!_mongocrypt_validate_and_copy_string (cmk, cmk_len, &temp) ||
800
- cmk_len == 0) {
801
- bson_free (temp);
802
- return _mongocrypt_ctx_fail_w_msg (ctx, "invalid cmk");
803
- }
804
- bson_free (temp);
805
-
806
- bson_init (&as_bson);
807
- bson_append_utf8 (&as_bson,
808
- MONGOCRYPT_STR_AND_LEN ("provider"),
809
- MONGOCRYPT_STR_AND_LEN ("aws"));
810
- BSON_ASSERT (region_len <= INT_MAX);
811
- bson_append_utf8 (
812
- &as_bson, MONGOCRYPT_STR_AND_LEN ("region"), region, region_len);
813
- BSON_ASSERT (cmk_len <= INT_MAX);
814
- bson_append_utf8 (&as_bson, MONGOCRYPT_STR_AND_LEN ("key"), cmk, cmk_len);
815
- bin = mongocrypt_binary_new_from_data ((uint8_t *) bson_get_data (&as_bson),
816
- as_bson.len);
817
-
818
- ret = mongocrypt_ctx_setopt_key_encryption_key (ctx, bin);
819
- mongocrypt_binary_destroy (bin);
820
- bson_destroy (&as_bson);
821
-
822
- if (ctx->crypt->log.trace_enabled) {
823
- _mongocrypt_log (&ctx->crypt->log,
824
- MONGOCRYPT_LOG_LEVEL_TRACE,
825
- "%s (%s=\"%s\", %s=%d, %s=\"%s\", %s=%d)",
826
- BSON_FUNC,
827
- "region",
828
- ctx->opts.kek.provider.aws.region,
829
- "region_len",
830
- region_len,
831
- "cmk",
832
- ctx->opts.kek.provider.aws.cmk,
833
- "cmk_len",
834
- cmk_len);
835
- }
836
-
837
- return ret;
878
+ bool _mongocrypt_ctx_state_from_key_broker(mongocrypt_ctx_t *ctx) {
879
+ _mongocrypt_key_broker_t *kb;
880
+ mongocrypt_status_t *status;
881
+ mongocrypt_ctx_state_t new_state = MONGOCRYPT_CTX_ERROR;
882
+ bool ret = false;
883
+
884
+ BSON_ASSERT_PARAM(ctx);
885
+
886
+ status = ctx->status;
887
+ kb = &ctx->kb;
888
+
889
+ if (ctx->state == MONGOCRYPT_CTX_ERROR) {
890
+ return false;
891
+ }
892
+
893
+ switch (kb->state) {
894
+ case KB_ERROR:
895
+ _mongocrypt_status_copy_to(kb->status, status);
896
+ new_state = MONGOCRYPT_CTX_ERROR;
897
+ ret = false;
898
+ break;
899
+ case KB_ADDING_DOCS:
900
+ /* Encrypted keys need KMS, which need to be provided before
901
+ * adding docs. */
902
+ if (_mongocrypt_needs_credentials(ctx->crypt)) {
903
+ new_state = MONGOCRYPT_CTX_NEED_KMS_CREDENTIALS;
904
+ } else {
905
+ /* Require key documents from driver. */
906
+ new_state = MONGOCRYPT_CTX_NEED_MONGO_KEYS;
907
+ }
908
+ ret = true;
909
+ break;
910
+ case KB_ADDING_DOCS_ANY:
911
+ /* Assume KMS credentials have been provided. */
912
+ new_state = MONGOCRYPT_CTX_NEED_MONGO_KEYS;
913
+ ret = true;
914
+ break;
915
+ case KB_AUTHENTICATING:
916
+ case KB_DECRYPTING_KEY_MATERIAL:
917
+ new_state = MONGOCRYPT_CTX_NEED_KMS;
918
+ ret = true;
919
+ break;
920
+ case KB_DONE:
921
+ new_state = MONGOCRYPT_CTX_READY;
922
+ if (kb->key_requests == NULL) {
923
+ /* No key requests were ever added. */
924
+ ctx->nothing_to_do = true; /* nothing to encrypt/decrypt */
925
+ }
926
+ ret = true;
927
+ break;
928
+ /* As currently implemented, we do not expect to ever be in KB_REQUESTING
929
+ * or KB_REQUESTING_ANY state when calling this function. */
930
+ case KB_REQUESTING:
931
+ default:
932
+ CLIENT_ERR("key broker in unexpected state");
933
+ new_state = MONGOCRYPT_CTX_ERROR;
934
+ ret = false;
935
+ break;
936
+ }
937
+
938
+ if (new_state != ctx->state) {
939
+ ctx->state = new_state;
940
+ }
941
+
942
+ return ret;
838
943
  }
839
944
 
945
+ bool mongocrypt_ctx_setopt_masterkey_aws_endpoint(mongocrypt_ctx_t *ctx, const char *endpoint, int32_t endpoint_len) {
946
+ if (!ctx) {
947
+ return false;
948
+ }
840
949
 
841
- bool
842
- mongocrypt_ctx_setopt_masterkey_local (mongocrypt_ctx_t *ctx)
843
- {
844
- if (!ctx) {
845
- return false;
846
- }
847
- if (ctx->initialized) {
848
- return _mongocrypt_ctx_fail_w_msg (ctx, "cannot set options after init");
849
- }
950
+ if (ctx->initialized) {
951
+ return _mongocrypt_ctx_fail_w_msg(ctx, "cannot set options after init");
952
+ }
850
953
 
851
- if (ctx->state == MONGOCRYPT_CTX_ERROR) {
852
- return false;
853
- }
954
+ if (ctx->state == MONGOCRYPT_CTX_ERROR) {
955
+ return false;
956
+ }
854
957
 
855
- if (ctx->opts.kek.kms_provider) {
856
- return _mongocrypt_ctx_fail_w_msg (ctx, "master key already set");
857
- }
958
+ if (ctx->opts.kek.kms_provider != MONGOCRYPT_KMS_PROVIDER_AWS
959
+ && ctx->opts.kek.kms_provider != MONGOCRYPT_KMS_PROVIDER_NONE) {
960
+ return _mongocrypt_ctx_fail_w_msg(ctx, "endpoint prohibited");
961
+ }
858
962
 
859
- ctx->opts.kek.kms_provider = MONGOCRYPT_KMS_PROVIDER_LOCAL;
860
- return true;
861
- }
963
+ ctx->opts.kek.kms_provider = MONGOCRYPT_KMS_PROVIDER_AWS;
862
964
 
965
+ if (ctx->opts.kek.provider.aws.endpoint) {
966
+ return _mongocrypt_ctx_fail_w_msg(ctx, "already set masterkey endpoint");
967
+ }
863
968
 
864
- bool
865
- _mongocrypt_ctx_init (mongocrypt_ctx_t *ctx,
866
- _mongocrypt_ctx_opts_spec_t *opts_spec)
867
- {
868
- bool has_id = false, has_alt_name = false, has_multiple_alt_names = false;
869
-
870
- BSON_ASSERT_PARAM (ctx);
871
- BSON_ASSERT_PARAM (opts_spec);
872
-
873
- // This condition is guarded in setopt_algorithm:
874
- BSON_ASSERT (
875
- !(ctx->opts.index_type.set &&
876
- ctx->opts.algorithm != MONGOCRYPT_ENCRYPTION_ALGORITHM_NONE) &&
877
- "Both an encryption algorithm and an index_type were set.");
878
-
879
- if (ctx->initialized) {
880
- return _mongocrypt_ctx_fail_w_msg (ctx, "cannot double initialize");
881
- }
882
- ctx->initialized = true;
883
-
884
- if (ctx->state == MONGOCRYPT_CTX_ERROR) {
885
- return false;
886
- }
887
- /* Set some default functions */
888
- ctx->vtable.mongo_op_keys = _mongo_op_keys;
889
- ctx->vtable.mongo_feed_keys = _mongo_feed_keys;
890
- ctx->vtable.mongo_done_keys = _mongo_done_keys;
891
- ctx->vtable.next_kms_ctx = _next_kms_ctx;
892
- ctx->vtable.kms_done = _kms_done;
893
-
894
- /* Check that required options are included and prohibited options are
895
- * not.
896
- */
897
-
898
- if (opts_spec->kek == OPT_REQUIRED) {
899
- if (!ctx->opts.kek.kms_provider) {
900
- return _mongocrypt_ctx_fail_w_msg (ctx, "master key required");
901
- }
902
- if (!ctx->crypt->opts.use_need_kms_credentials_state &&
903
- !((int) ctx->opts.kek.kms_provider &
904
- _mongocrypt_ctx_kms_providers (ctx)->configured_providers)) {
905
- return _mongocrypt_ctx_fail_w_msg (
906
- ctx, "requested kms provider not configured");
907
- }
908
- }
909
-
910
- if (opts_spec->kek == OPT_PROHIBITED && ctx->opts.kek.kms_provider) {
911
- return _mongocrypt_ctx_fail_w_msg (ctx, "master key prohibited");
912
- }
913
-
914
- /* Check that the kms provider required by the datakey is configured. */
915
- if (ctx->opts.kek.kms_provider) {
916
- if (!((ctx->crypt->opts.kms_providers.need_credentials |
917
- ctx->crypt->opts.kms_providers.configured_providers) &
918
- (int) ctx->opts.kek.kms_provider)) {
919
- return _mongocrypt_ctx_fail_w_msg (
920
- ctx, "kms provider required by datakey is not configured");
921
- }
922
- }
923
-
924
- /* Special case. key_descriptor applies to explicit encryption. It must be
925
- * either a key id or *one* key alt name, but not both.
926
- * key_alt_names applies to creating a data key. It may be one or multiple
927
- * key alt names.
928
- */
929
- has_id = !_mongocrypt_buffer_empty (&ctx->opts.key_id);
930
- has_alt_name = !!(ctx->opts.key_alt_names);
931
- has_multiple_alt_names = has_alt_name && !!(ctx->opts.key_alt_names->next);
932
-
933
- if (opts_spec->key_descriptor == OPT_REQUIRED) {
934
- if (!has_id && !has_alt_name) {
935
- return _mongocrypt_ctx_fail_w_msg (
936
- ctx, "either key id or key alt name required");
937
- }
938
-
939
- if (has_id && has_alt_name) {
940
- return _mongocrypt_ctx_fail_w_msg (
941
- ctx, "cannot have both key id and key alt name");
942
- }
943
-
944
- if (has_multiple_alt_names) {
945
- return _mongocrypt_ctx_fail_w_msg (
946
- ctx, "must not specify multiple key alt names");
947
- }
948
- }
949
-
950
- if (opts_spec->key_descriptor == OPT_PROHIBITED) {
951
- /* still okay if key_alt_names are allowed and only alt names were
952
- * specified. */
953
- if ((opts_spec->key_alt_names == OPT_PROHIBITED && has_alt_name) ||
954
- has_id) {
955
- return _mongocrypt_ctx_fail_w_msg (ctx,
956
- "key id and alt name prohibited");
957
- }
958
- }
959
-
960
- if (opts_spec->key_material == OPT_PROHIBITED &&
961
- ctx->opts.key_material.owned) {
962
- return _mongocrypt_ctx_fail_w_msg (ctx, "key material prohibited");
963
- }
964
-
965
- if (opts_spec->algorithm == OPT_REQUIRED &&
966
- ctx->opts.algorithm == MONGOCRYPT_ENCRYPTION_ALGORITHM_NONE) {
967
- return _mongocrypt_ctx_fail_w_msg (ctx, "algorithm required");
968
- }
969
-
970
- if (opts_spec->algorithm == OPT_PROHIBITED &&
971
- ctx->opts.algorithm != MONGOCRYPT_ENCRYPTION_ALGORITHM_NONE) {
972
- return _mongocrypt_ctx_fail_w_msg (ctx, "algorithm prohibited");
973
- }
974
-
975
- if (opts_spec->rangeopts == OPT_PROHIBITED && ctx->opts.rangeopts.set) {
976
- return _mongocrypt_ctx_fail_w_msg (
977
- ctx, "range opts are prohibited on this context");
978
- }
979
-
980
- _mongocrypt_key_broker_init (&ctx->kb, ctx->crypt);
981
- return true;
982
- }
969
+ ctx->opts.kek.provider.aws.endpoint =
970
+ _mongocrypt_endpoint_new(endpoint, endpoint_len, NULL /* opts */, ctx->status);
971
+ if (!ctx->opts.kek.provider.aws.endpoint) {
972
+ return _mongocrypt_ctx_fail(ctx);
973
+ }
983
974
 
984
- bool
985
- _mongocrypt_ctx_state_from_key_broker (mongocrypt_ctx_t *ctx)
986
- {
987
- _mongocrypt_key_broker_t *kb;
988
- mongocrypt_status_t *status;
989
- mongocrypt_ctx_state_t new_state = MONGOCRYPT_CTX_ERROR;
990
- bool ret = false;
991
-
992
- BSON_ASSERT_PARAM (ctx);
993
-
994
- status = ctx->status;
995
- kb = &ctx->kb;
996
-
997
- if (ctx->state == MONGOCRYPT_CTX_ERROR) {
998
- return false;
999
- }
1000
-
1001
-
1002
- switch (kb->state) {
1003
- case KB_ERROR:
1004
- _mongocrypt_status_copy_to (kb->status, status);
1005
- new_state = MONGOCRYPT_CTX_ERROR;
1006
- ret = false;
1007
- break;
1008
- case KB_ADDING_DOCS:
1009
- /* Encrypted keys need KMS, which need to be provided before
1010
- * adding docs. */
1011
- if (_mongocrypt_needs_credentials (ctx->crypt)) {
1012
- new_state = MONGOCRYPT_CTX_NEED_KMS_CREDENTIALS;
1013
- } else {
1014
- /* Require key documents from driver. */
1015
- new_state = MONGOCRYPT_CTX_NEED_MONGO_KEYS;
1016
- }
1017
- ret = true;
1018
- break;
1019
- case KB_ADDING_DOCS_ANY:
1020
- /* Assume KMS credentials have been provided. */
1021
- new_state = MONGOCRYPT_CTX_NEED_MONGO_KEYS;
1022
- ret = true;
1023
- break;
1024
- case KB_AUTHENTICATING:
1025
- case KB_DECRYPTING_KEY_MATERIAL:
1026
- new_state = MONGOCRYPT_CTX_NEED_KMS;
1027
- ret = true;
1028
- break;
1029
- case KB_DONE:
1030
- new_state = MONGOCRYPT_CTX_READY;
1031
- if (kb->key_requests == NULL) {
1032
- /* No key requests were ever added. */
1033
- ctx->nothing_to_do = true; /* nothing to encrypt/decrypt */
1034
- }
1035
- ret = true;
1036
- break;
1037
- /* As currently implemented, we do not expect to ever be in KB_REQUESTING
1038
- * or KB_REQUESTING_ANY state when calling this function. */
1039
- case KB_REQUESTING:
1040
- default:
1041
- CLIENT_ERR ("key broker in unexpected state");
1042
- new_state = MONGOCRYPT_CTX_ERROR;
1043
- ret = false;
1044
- break;
1045
- }
1046
-
1047
- if (new_state != ctx->state) {
1048
- ctx->state = new_state;
1049
- }
1050
-
1051
- return ret;
975
+ return true;
1052
976
  }
1053
977
 
978
+ bool mongocrypt_ctx_setopt_key_encryption_key(mongocrypt_ctx_t *ctx, mongocrypt_binary_t *bin) {
979
+ bson_t as_bson;
1054
980
 
1055
- bool
1056
- mongocrypt_ctx_setopt_masterkey_aws_endpoint (mongocrypt_ctx_t *ctx,
1057
- const char *endpoint,
1058
- int32_t endpoint_len)
1059
- {
1060
- if (!ctx) {
1061
- return false;
1062
- }
981
+ if (!ctx) {
982
+ return false;
983
+ }
1063
984
 
1064
- if (ctx->initialized) {
1065
- return _mongocrypt_ctx_fail_w_msg (ctx, "cannot set options after init");
1066
- }
985
+ if (ctx->initialized) {
986
+ return _mongocrypt_ctx_fail_w_msg(ctx, "cannot set options after init");
987
+ }
1067
988
 
1068
- if (ctx->state == MONGOCRYPT_CTX_ERROR) {
1069
- return false;
1070
- }
989
+ if (ctx->state == MONGOCRYPT_CTX_ERROR) {
990
+ return false;
991
+ }
1071
992
 
1072
- if (ctx->opts.kek.kms_provider != MONGOCRYPT_KMS_PROVIDER_AWS &&
1073
- ctx->opts.kek.kms_provider != MONGOCRYPT_KMS_PROVIDER_NONE) {
1074
- return _mongocrypt_ctx_fail_w_msg (ctx, "endpoint prohibited");
1075
- }
993
+ if (ctx->opts.kek.kms_provider) {
994
+ return _mongocrypt_ctx_fail_w_msg(ctx, "key encryption key already set");
995
+ }
1076
996
 
1077
- ctx->opts.kek.kms_provider = MONGOCRYPT_KMS_PROVIDER_AWS;
997
+ if (!bin) {
998
+ return _mongocrypt_ctx_fail_w_msg(ctx, "invalid NULL key encryption key document");
999
+ }
1078
1000
 
1079
- if (ctx->opts.kek.provider.aws.endpoint) {
1080
- return _mongocrypt_ctx_fail_w_msg (ctx, "already set masterkey endpoint");
1081
- }
1001
+ if (!_mongocrypt_binary_to_bson(bin, &as_bson)) {
1002
+ return _mongocrypt_ctx_fail_w_msg(ctx, "invalid BSON");
1003
+ }
1082
1004
 
1083
- ctx->opts.kek.provider.aws.endpoint = _mongocrypt_endpoint_new (
1084
- endpoint, endpoint_len, NULL /* opts */, ctx->status);
1085
- if (!ctx->opts.kek.provider.aws.endpoint) {
1086
- return _mongocrypt_ctx_fail (ctx);
1087
- }
1005
+ if (!_mongocrypt_kek_parse_owned(&as_bson, &ctx->opts.kek, ctx->status)) {
1006
+ return _mongocrypt_ctx_fail(ctx);
1007
+ }
1088
1008
 
1089
- return true;
1090
- }
1009
+ if (ctx->crypt->log.trace_enabled) {
1010
+ char *bin_str = bson_as_canonical_extended_json(&as_bson, NULL);
1011
+ _mongocrypt_log(&ctx->crypt->log, MONGOCRYPT_LOG_LEVEL_TRACE, "%s (%s=\"%s\")", BSON_FUNC, "bin", bin_str);
1012
+ bson_free(bin_str);
1013
+ }
1091
1014
 
1092
- bool
1093
- mongocrypt_ctx_setopt_key_encryption_key (mongocrypt_ctx_t *ctx,
1094
- mongocrypt_binary_t *bin)
1095
- {
1096
- bson_t as_bson;
1097
-
1098
- if (!ctx) {
1099
- return false;
1100
- }
1101
-
1102
- if (ctx->initialized) {
1103
- return _mongocrypt_ctx_fail_w_msg (ctx, "cannot set options after init");
1104
- }
1105
-
1106
- if (ctx->state == MONGOCRYPT_CTX_ERROR) {
1107
- return false;
1108
- }
1109
-
1110
- if (ctx->opts.kek.kms_provider) {
1111
- return _mongocrypt_ctx_fail_w_msg (ctx, "key encryption key already set");
1112
- }
1113
-
1114
- if (!bin) {
1115
- return _mongocrypt_ctx_fail_w_msg (
1116
- ctx, "invalid NULL key encryption key document");
1117
- }
1118
-
1119
- if (!_mongocrypt_binary_to_bson (bin, &as_bson)) {
1120
- return _mongocrypt_ctx_fail_w_msg (ctx, "invalid BSON");
1121
- }
1122
-
1123
- if (!_mongocrypt_kek_parse_owned (&as_bson, &ctx->opts.kek, ctx->status)) {
1124
- return _mongocrypt_ctx_fail (ctx);
1125
- }
1126
-
1127
- if (ctx->crypt->log.trace_enabled) {
1128
- char *bin_str = bson_as_canonical_extended_json (&as_bson, NULL);
1129
- _mongocrypt_log (&ctx->crypt->log,
1130
- MONGOCRYPT_LOG_LEVEL_TRACE,
1131
- "%s (%s=\"%s\")",
1132
- BSON_FUNC,
1133
- "bin",
1134
- bin_str);
1135
- bson_free (bin_str);
1136
- }
1137
-
1138
- return true;
1015
+ return true;
1139
1016
  }
1140
1017
 
1141
- _mongocrypt_opts_kms_providers_t *
1142
- _mongocrypt_ctx_kms_providers (mongocrypt_ctx_t *ctx)
1143
- {
1144
- BSON_ASSERT_PARAM (ctx);
1018
+ _mongocrypt_opts_kms_providers_t *_mongocrypt_ctx_kms_providers(mongocrypt_ctx_t *ctx) {
1019
+ BSON_ASSERT_PARAM(ctx);
1145
1020
 
1146
- return ctx->kms_providers.configured_providers
1147
- ? &ctx->kms_providers
1148
- : &ctx->crypt->opts.kms_providers;
1021
+ return ctx->kms_providers.configured_providers ? &ctx->kms_providers : &ctx->crypt->opts.kms_providers;
1149
1022
  }
1150
1023
 
1151
- bool
1152
- mongocrypt_ctx_setopt_contention_factor (mongocrypt_ctx_t *ctx,
1153
- int64_t contention_factor)
1154
- {
1155
- if (!ctx) {
1156
- return false;
1157
- }
1158
- ctx->opts.contention_factor.value = contention_factor;
1159
- ctx->opts.contention_factor.set = true;
1160
- return true;
1024
+ bool mongocrypt_ctx_setopt_contention_factor(mongocrypt_ctx_t *ctx, int64_t contention_factor) {
1025
+ if (!ctx) {
1026
+ return false;
1027
+ }
1028
+ ctx->opts.contention_factor.value = contention_factor;
1029
+ ctx->opts.contention_factor.set = true;
1030
+ return true;
1161
1031
  }
1162
1032
 
1163
- bool
1164
- mongocrypt_ctx_setopt_index_key_id (mongocrypt_ctx_t *ctx,
1165
- mongocrypt_binary_t *key_id)
1166
- {
1167
- if (!ctx) {
1168
- return false;
1169
- }
1033
+ bool mongocrypt_ctx_setopt_index_key_id(mongocrypt_ctx_t *ctx, mongocrypt_binary_t *key_id) {
1034
+ if (!ctx) {
1035
+ return false;
1036
+ }
1170
1037
 
1171
- return _set_binary_opt (
1172
- ctx, key_id, &ctx->opts.index_key_id, BSON_SUBTYPE_UUID);
1038
+ return _set_binary_opt(ctx, key_id, &ctx->opts.index_key_id, BSON_SUBTYPE_UUID);
1173
1039
  }
1174
1040
 
1175
- bool
1176
- mongocrypt_ctx_setopt_query_type (mongocrypt_ctx_t *ctx,
1177
- const char *query_type,
1178
- int len)
1179
- {
1180
- if (!ctx) {
1181
- return false;
1182
- }
1183
-
1184
- if (ctx->initialized) {
1185
- return _mongocrypt_ctx_fail_w_msg (ctx, "Cannot set options after init");
1186
- }
1187
- if (ctx->state == MONGOCRYPT_CTX_ERROR) {
1188
- return false;
1189
- }
1190
- if (len < -1) {
1191
- return _mongocrypt_ctx_fail_w_msg (ctx,
1192
- "Invalid query_type string length");
1193
- }
1194
- if (!query_type) {
1195
- return _mongocrypt_ctx_fail_w_msg (ctx, "Invalid null query_type string");
1196
- }
1197
-
1198
- const size_t calc_len = len == -1 ? strlen (query_type) : (size_t) len;
1199
- mstr_view qt_str = mstrv_view_data (query_type, calc_len);
1200
- if (mstr_eq_ignore_case (qt_str,
1201
- mstrv_lit (MONGOCRYPT_QUERY_TYPE_EQUALITY_STR))) {
1202
- ctx->opts.query_type.value = MONGOCRYPT_QUERY_TYPE_EQUALITY;
1203
- ctx->opts.query_type.set = true;
1204
- } else if (mstr_eq_ignore_case (
1205
- qt_str, mstrv_lit (MONGOCRYPT_QUERY_TYPE_RANGEPREVIEW_STR))) {
1206
- ctx->opts.query_type.value = MONGOCRYPT_QUERY_TYPE_RANGEPREVIEW;
1207
- ctx->opts.query_type.set = true;
1208
- } else {
1209
- /* don't check if qt_str.len fits in int; we want the diagnostic output */
1210
- char *error = bson_strdup_printf (
1211
- "Unsupported query_type \"%.*s\"",
1212
- qt_str.len <= (size_t) INT_MAX ? (int) qt_str.len : INT_MAX,
1213
- qt_str.data);
1214
- _mongocrypt_ctx_fail_w_msg (ctx, error);
1215
- bson_free (error);
1216
- return false;
1217
- }
1218
- return true;
1041
+ bool mongocrypt_ctx_setopt_query_type(mongocrypt_ctx_t *ctx, const char *query_type, int len) {
1042
+ if (!ctx) {
1043
+ return false;
1044
+ }
1045
+
1046
+ if (ctx->initialized) {
1047
+ return _mongocrypt_ctx_fail_w_msg(ctx, "Cannot set options after init");
1048
+ }
1049
+ if (ctx->state == MONGOCRYPT_CTX_ERROR) {
1050
+ return false;
1051
+ }
1052
+ if (len < -1) {
1053
+ return _mongocrypt_ctx_fail_w_msg(ctx, "Invalid query_type string length");
1054
+ }
1055
+ if (!query_type) {
1056
+ return _mongocrypt_ctx_fail_w_msg(ctx, "Invalid null query_type string");
1057
+ }
1058
+
1059
+ const size_t calc_len = len == -1 ? strlen(query_type) : (size_t)len;
1060
+ mstr_view qt_str = mstrv_view_data(query_type, calc_len);
1061
+ if (mstr_eq_ignore_case(qt_str, mstrv_lit(MONGOCRYPT_QUERY_TYPE_EQUALITY_STR))) {
1062
+ ctx->opts.query_type.value = MONGOCRYPT_QUERY_TYPE_EQUALITY;
1063
+ ctx->opts.query_type.set = true;
1064
+ } else if (mstr_eq_ignore_case(qt_str, mstrv_lit(MONGOCRYPT_QUERY_TYPE_RANGE_STR))) {
1065
+ ctx->opts.query_type.value = MONGOCRYPT_QUERY_TYPE_RANGE;
1066
+ ctx->opts.query_type.set = true;
1067
+ } else if (mstr_eq_ignore_case(qt_str, mstrv_lit(MONGOCRYPT_QUERY_TYPE_RANGEPREVIEW_DEPRECATED_STR))) {
1068
+ if (ctx->crypt->opts.use_range_v2) {
1069
+ _mongocrypt_ctx_fail_w_msg(ctx, "Query type 'rangePreview' is deprecated, please use 'range'");
1070
+ return false;
1071
+ }
1072
+ ctx->opts.query_type.value = MONGOCRYPT_QUERY_TYPE_RANGEPREVIEW_DEPRECATED;
1073
+ ctx->opts.query_type.set = true;
1074
+ } else {
1075
+ /* don't check if qt_str.len fits in int; we want the diagnostic output */
1076
+ char *error = bson_strdup_printf("Unsupported query_type \"%.*s\"",
1077
+ qt_str.len <= (size_t)INT_MAX ? (int)qt_str.len : INT_MAX,
1078
+ qt_str.data);
1079
+ _mongocrypt_ctx_fail_w_msg(ctx, error);
1080
+ bson_free(error);
1081
+ return false;
1082
+ }
1083
+ return true;
1219
1084
  }
1220
1085
 
1221
- const char *
1222
- _mongocrypt_index_type_to_string (mongocrypt_index_type_t val)
1223
- {
1224
- switch (val) {
1225
- case MONGOCRYPT_INDEX_TYPE_NONE:
1226
- return "None";
1227
- case MONGOCRYPT_INDEX_TYPE_EQUALITY:
1228
- return "Equality";
1229
- case MONGOCRYPT_INDEX_TYPE_RANGEPREVIEW:
1230
- return "RangePreview";
1231
- default:
1232
- return "Unknown";
1233
- }
1086
+ const char *_mongocrypt_index_type_to_string(mongocrypt_index_type_t val) {
1087
+ switch (val) {
1088
+ case MONGOCRYPT_INDEX_TYPE_NONE: return "None";
1089
+ case MONGOCRYPT_INDEX_TYPE_EQUALITY: return "Equality";
1090
+ case MONGOCRYPT_INDEX_TYPE_RANGE: return "Range";
1091
+ case MONGOCRYPT_INDEX_TYPE_RANGEPREVIEW_DEPRECATED: return "RangePreview";
1092
+ default: return "Unknown";
1093
+ }
1234
1094
  }
1235
1095
 
1236
- const char *
1237
- _mongocrypt_query_type_to_string (mongocrypt_query_type_t val)
1238
- {
1239
- switch (val) {
1240
- case MONGOCRYPT_QUERY_TYPE_EQUALITY:
1241
- return "Equality";
1242
- case MONGOCRYPT_QUERY_TYPE_RANGEPREVIEW:
1243
- return "RangePreview";
1244
- default:
1245
- return "Unknown";
1246
- }
1096
+ const char *_mongocrypt_query_type_to_string(mongocrypt_query_type_t val) {
1097
+ switch (val) {
1098
+ case MONGOCRYPT_QUERY_TYPE_EQUALITY: return "Equality";
1099
+ case MONGOCRYPT_QUERY_TYPE_RANGEPREVIEW_DEPRECATED: return "RangePreview";
1100
+ case MONGOCRYPT_QUERY_TYPE_RANGE: return "Range";
1101
+ default: return "Unknown";
1102
+ }
1247
1103
  }
1248
1104
 
1249
- bool
1250
- mongocrypt_ctx_setopt_algorithm_range (mongocrypt_ctx_t *ctx,
1251
- mongocrypt_binary_t *opts)
1252
- {
1253
- bson_t as_bson;
1254
-
1255
- if (!ctx) {
1256
- return false;
1257
- }
1105
+ bool mongocrypt_ctx_setopt_algorithm_range(mongocrypt_ctx_t *ctx, mongocrypt_binary_t *opts) {
1106
+ bson_t as_bson;
1258
1107
 
1259
- if (ctx->initialized) {
1260
- return _mongocrypt_ctx_fail_w_msg (ctx, "cannot set options after init");
1261
- }
1108
+ if (!ctx) {
1109
+ return false;
1110
+ }
1262
1111
 
1263
- if (ctx->state == MONGOCRYPT_CTX_ERROR) {
1264
- return false;
1265
- }
1112
+ if (ctx->initialized) {
1113
+ return _mongocrypt_ctx_fail_w_msg(ctx, "cannot set options after init");
1114
+ }
1266
1115
 
1267
- if (ctx->opts.rangeopts.set) {
1268
- return _mongocrypt_ctx_fail_w_msg (ctx, "RangeOpts already set");
1269
- }
1116
+ if (ctx->state == MONGOCRYPT_CTX_ERROR) {
1117
+ return false;
1118
+ }
1270
1119
 
1120
+ if (ctx->opts.rangeopts.set) {
1121
+ return _mongocrypt_ctx_fail_w_msg(ctx, "RangeOpts already set");
1122
+ }
1271
1123
 
1272
- if (!_mongocrypt_binary_to_bson (opts, &as_bson)) {
1273
- return _mongocrypt_ctx_fail_w_msg (ctx, "invalid BSON");
1274
- }
1124
+ if (!_mongocrypt_binary_to_bson(opts, &as_bson)) {
1125
+ return _mongocrypt_ctx_fail_w_msg(ctx, "invalid BSON");
1126
+ }
1275
1127
 
1276
- if (!mc_RangeOpts_parse (
1277
- &ctx->opts.rangeopts.value, &as_bson, ctx->status)) {
1278
- return _mongocrypt_ctx_fail (ctx);
1279
- }
1128
+ if (!mc_RangeOpts_parse(&ctx->opts.rangeopts.value, &as_bson, ctx->crypt->opts.use_range_v2, ctx->status)) {
1129
+ return _mongocrypt_ctx_fail(ctx);
1130
+ }
1280
1131
 
1281
- ctx->opts.rangeopts.set = true;
1282
- return true;
1132
+ ctx->opts.rangeopts.set = true;
1133
+ return true;
1283
1134
  }