libmongocrypt-helper 1.7.4.0.1001 → 1.8.0.0.1001

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