libmongocrypt-helper 1.7.4.0.1002 → 1.8.0.0.1001

Sign up to get free protection for your applications and to get access to all the features.
Files changed (396) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/ext/libmongocrypt/libmongocrypt/CHANGELOG.md +9 -7
  4. data/ext/libmongocrypt/libmongocrypt/CMakeLists.txt +17 -23
  5. data/ext/libmongocrypt/libmongocrypt/Earthfile +374 -0
  6. data/ext/libmongocrypt/libmongocrypt/README.md +7 -7
  7. data/ext/libmongocrypt/libmongocrypt/VERSION_CURRENT +1 -1
  8. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt/CryptClient.cs +25 -6
  9. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt/Library.cs +25 -3
  10. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt.Example/MongoDB.Libmongocrypt.Example.csproj +1 -1
  11. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt.Test/BasicTests.cs +1 -1
  12. data/ext/libmongocrypt/libmongocrypt/bindings/cs/Scripts/build.cake +3 -2
  13. data/ext/libmongocrypt/libmongocrypt/bindings/cs/cs.sln +79 -0
  14. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/build.gradle.kts +2 -2
  15. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/test/java/com/mongodb/crypt/capi/MongoCryptTest.java +6 -4
  16. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/test/resources/fle2-find-range-explicit-v2/int32/encrypted-payload.json +26 -0
  17. data/ext/libmongocrypt/libmongocrypt/bindings/node/CHANGELOG.md +24 -0
  18. data/ext/libmongocrypt/libmongocrypt/bindings/node/README.md +134 -5
  19. data/ext/libmongocrypt/libmongocrypt/bindings/node/etc/build-static.sh +3 -6
  20. data/ext/libmongocrypt/libmongocrypt/bindings/node/index.d.ts +203 -77
  21. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/autoEncrypter.js +1 -1
  22. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/clientEncryption.js +165 -43
  23. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/common.js +12 -14
  24. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/errors.js +75 -0
  25. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/index.js +34 -3
  26. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/providers/aws.js +26 -0
  27. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/providers/azure.js +178 -0
  28. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/providers/gcp.js +24 -0
  29. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/providers/index.js +54 -0
  30. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/providers/utils.js +39 -0
  31. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/stateMachine.js +9 -4
  32. data/ext/libmongocrypt/libmongocrypt/bindings/node/package-lock.json +4440 -5189
  33. data/ext/libmongocrypt/libmongocrypt/bindings/node/package.json +27 -8
  34. data/ext/libmongocrypt/libmongocrypt/bindings/node/src/mongocrypt.cc +65 -38
  35. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/autoEncrypter.test.js +4 -7
  36. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/clientEncryption.test.js +434 -42
  37. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/common.test.js +94 -0
  38. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/cryptoCallbacks.test.js +1 -45
  39. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/index.test.js +45 -0
  40. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/mongocryptdManager.test.js +1 -1
  41. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/providers/credentialsProvider.test.js +551 -0
  42. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/release.test.js +10 -3
  43. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/requirements.helper.js +23 -1
  44. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/stateMachine.test.js +1 -1
  45. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/tools/chai-addons.js +8 -0
  46. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/types/index.test-d.ts +63 -0
  47. data/ext/libmongocrypt/libmongocrypt/bindings/python/CHANGELOG.rst +26 -0
  48. data/ext/libmongocrypt/libmongocrypt/bindings/python/README.rst +2 -2
  49. data/ext/libmongocrypt/libmongocrypt/bindings/python/build-manylinux-wheel.sh +1 -1
  50. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/binding.py +128 -238
  51. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/compat.py +0 -27
  52. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/explicit_encrypter.py +1 -1
  53. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/mongocrypt.py +5 -21
  54. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/version.py +2 -2
  55. data/ext/libmongocrypt/libmongocrypt/bindings/python/release.sh +5 -5
  56. data/ext/libmongocrypt/libmongocrypt/bindings/python/setup.py +14 -9
  57. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/__init__.py +1 -10
  58. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/fle2-find-range-explicit-v2/int32/encrypted-payload.json +26 -0
  59. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/test_mongocrypt.py +5 -5
  60. data/ext/libmongocrypt/libmongocrypt/bindings/python/test-requirements.txt +4 -12
  61. data/ext/libmongocrypt/libmongocrypt/cmake/ImportBSON.cmake +8 -6
  62. data/ext/libmongocrypt/libmongocrypt/cmake/IntelDFP.cmake +2 -1
  63. data/ext/libmongocrypt/libmongocrypt/cmake/Platform.cmake +50 -0
  64. data/ext/libmongocrypt/libmongocrypt/cmake/mongocrypt-config.cmake +21 -0
  65. data/ext/libmongocrypt/libmongocrypt/debian/gbp.conf +2 -2
  66. data/ext/libmongocrypt/libmongocrypt/etc/c6-vault.repo +39 -0
  67. data/ext/libmongocrypt/libmongocrypt/etc/fle2_aead_generate_tests.py +15 -24
  68. data/ext/libmongocrypt/libmongocrypt/etc/fle2_crypto.py +66 -54
  69. data/ext/libmongocrypt/libmongocrypt/etc/fle2_generate_tests.py +14 -23
  70. data/ext/libmongocrypt/libmongocrypt/etc/fle2v2_aead_generate_tests.py +32 -0
  71. data/ext/libmongocrypt/libmongocrypt/etc/format-all.sh +12 -0
  72. data/ext/libmongocrypt/libmongocrypt/etc/format.sh +16 -0
  73. data/ext/libmongocrypt/libmongocrypt/etc/install-package.sh +48 -0
  74. data/ext/libmongocrypt/libmongocrypt/etc/mongo-inteldfp-libmongocrypt-pr-625.patch +13 -0
  75. data/ext/libmongocrypt/libmongocrypt/src/crypto/cng.c +381 -436
  76. data/ext/libmongocrypt/libmongocrypt/src/crypto/commoncrypto.c +162 -227
  77. data/ext/libmongocrypt/libmongocrypt/src/crypto/libcrypto.c +180 -248
  78. data/ext/libmongocrypt/libmongocrypt/src/crypto/none.c +33 -55
  79. data/ext/libmongocrypt/libmongocrypt/src/csfle-markup.cpp +175 -205
  80. data/ext/libmongocrypt/libmongocrypt/src/mc-array-private.h +10 -21
  81. data/ext/libmongocrypt/libmongocrypt/src/mc-array.c +44 -56
  82. data/ext/libmongocrypt/libmongocrypt/src/mc-check-conversions-private.h +4 -8
  83. data/ext/libmongocrypt/libmongocrypt/src/mc-dec128.h +351 -463
  84. data/ext/libmongocrypt/libmongocrypt/src/mc-dec128.test.cpp +59 -66
  85. data/ext/libmongocrypt/libmongocrypt/src/mc-efc-private.h +7 -11
  86. data/ext/libmongocrypt/libmongocrypt/src/mc-efc.c +80 -92
  87. data/ext/libmongocrypt/libmongocrypt/src/mc-fle-blob-subtype-private.h +18 -10
  88. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-encryption-placeholder-private.h +59 -70
  89. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-encryption-placeholder.c +384 -439
  90. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-equality-payload-private-v2.h +41 -0
  91. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-equality-payload-private.h +11 -18
  92. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-equality-payload-v2.c +135 -0
  93. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-equality-payload.c +109 -126
  94. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-range-payload-private-v2.h +88 -0
  95. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-range-payload-private.h +24 -27
  96. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-range-payload-v2.c +137 -0
  97. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-range-payload.c +106 -125
  98. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-insert-update-payload-private-v2.h +117 -0
  99. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-insert-update-payload-private.h +28 -36
  100. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-insert-update-payload-v2.c +294 -0
  101. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-insert-update-payload.c +237 -278
  102. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-iev-private-v2.h +133 -0
  103. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-iev-private.h +67 -56
  104. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-iev-v2.c +437 -0
  105. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-iev.c +476 -325
  106. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-uev-common-private.h +69 -0
  107. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-uev-common.c +182 -0
  108. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-uev-private.h +23 -31
  109. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-uev-v2-private.h +85 -0
  110. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-uev-v2.c +142 -0
  111. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-uev.c +104 -231
  112. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-range-operator-private.h +8 -7
  113. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-rfds-private.h +55 -56
  114. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-rfds.c +517 -580
  115. data/ext/libmongocrypt/libmongocrypt/src/mc-optional-private.h +36 -66
  116. data/ext/libmongocrypt/libmongocrypt/src/mc-range-edge-generation-private.h +57 -76
  117. data/ext/libmongocrypt/libmongocrypt/src/mc-range-edge-generation.c +158 -189
  118. data/ext/libmongocrypt/libmongocrypt/src/mc-range-encoding-private.h +36 -42
  119. data/ext/libmongocrypt/libmongocrypt/src/mc-range-encoding.c +515 -558
  120. data/ext/libmongocrypt/libmongocrypt/src/mc-range-mincover-generator.template.h +155 -191
  121. data/ext/libmongocrypt/libmongocrypt/src/mc-range-mincover-private.h +43 -53
  122. data/ext/libmongocrypt/libmongocrypt/src/mc-range-mincover.c +170 -240
  123. data/ext/libmongocrypt/libmongocrypt/src/mc-rangeopts-private.h +29 -34
  124. data/ext/libmongocrypt/libmongocrypt/src/mc-rangeopts.c +289 -338
  125. data/ext/libmongocrypt/libmongocrypt/src/mc-reader-private.h +36 -71
  126. data/ext/libmongocrypt/libmongocrypt/src/mc-reader.c +111 -164
  127. data/ext/libmongocrypt/libmongocrypt/src/mc-tokens-private.h +55 -49
  128. data/ext/libmongocrypt/libmongocrypt/src/mc-tokens.c +109 -125
  129. data/ext/libmongocrypt/libmongocrypt/src/mc-writer-private.h +66 -0
  130. data/ext/libmongocrypt/libmongocrypt/src/mc-writer.c +141 -0
  131. data/ext/libmongocrypt/libmongocrypt/src/mlib/check.hpp +37 -55
  132. data/ext/libmongocrypt/libmongocrypt/src/mlib/endian.h +11 -11
  133. data/ext/libmongocrypt/libmongocrypt/src/mlib/error.h +27 -32
  134. data/ext/libmongocrypt/libmongocrypt/src/mlib/int128.h +444 -499
  135. data/ext/libmongocrypt/libmongocrypt/src/mlib/int128.test.cpp +251 -334
  136. data/ext/libmongocrypt/libmongocrypt/src/mlib/macros.h +4 -5
  137. data/ext/libmongocrypt/libmongocrypt/src/mlib/path.h +196 -231
  138. data/ext/libmongocrypt/libmongocrypt/src/mlib/path.test.c +56 -79
  139. data/ext/libmongocrypt/libmongocrypt/src/mlib/str.h +411 -530
  140. data/ext/libmongocrypt/libmongocrypt/src/mlib/str.test.c +116 -131
  141. data/ext/libmongocrypt/libmongocrypt/src/mlib/thread.h +17 -26
  142. data/ext/libmongocrypt/libmongocrypt/src/mlib/user-check.h +2 -2
  143. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-binary-private.h +3 -6
  144. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-binary.c +31 -48
  145. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-buffer-private.h +55 -131
  146. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-buffer.c +444 -565
  147. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-collinfo-private.h +1 -2
  148. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-collinfo.c +28 -45
  149. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-key-private.h +11 -17
  150. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-key.c +103 -132
  151. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-oauth-private.h +8 -14
  152. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-oauth.c +78 -90
  153. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-private.h +26 -45
  154. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache.c +220 -273
  155. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ciphertext-private.h +13 -25
  156. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ciphertext.c +147 -166
  157. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-compat.h +2 -2
  158. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-crypto-private.h +110 -204
  159. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-crypto.c +1137 -1565
  160. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ctx-datakey.c +425 -472
  161. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ctx-decrypt.c +817 -694
  162. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ctx-encrypt.c +2394 -2697
  163. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ctx-private.h +166 -186
  164. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ctx-rewrap-many-datakey.c +308 -351
  165. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ctx.c +921 -1138
  166. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-dll-private.h +16 -24
  167. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-endian-private.h +44 -58
  168. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-endpoint-private.h +21 -25
  169. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-endpoint.c +167 -181
  170. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-kek-private.h +37 -43
  171. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-kek.c +215 -253
  172. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-key-broker-private.h +73 -108
  173. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-key-broker.c +889 -1034
  174. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-key-private.h +27 -44
  175. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-key.c +349 -402
  176. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-kms-ctx-private.h +92 -120
  177. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-kms-ctx.c +1397 -1612
  178. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-log-private.h +35 -67
  179. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-log.c +49 -83
  180. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-marking-private.h +30 -36
  181. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-marking.c +1821 -1470
  182. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-mutex-private.h +7 -12
  183. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-opts-private.h +80 -108
  184. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-opts.c +354 -424
  185. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-private.h +101 -117
  186. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-status-private.h +3 -8
  187. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-status.c +92 -119
  188. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-traverse-util-private.h +19 -29
  189. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-traverse-util.c +136 -176
  190. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-util-private.h +11 -21
  191. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-util.c +96 -135
  192. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt.c +1092 -1320
  193. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt.h +130 -295
  194. data/ext/libmongocrypt/libmongocrypt/src/os_posix/os_dll.c +66 -78
  195. data/ext/libmongocrypt/libmongocrypt/src/os_posix/os_mutex.c +20 -28
  196. data/ext/libmongocrypt/libmongocrypt/src/os_win/os_dll.c +60 -73
  197. data/ext/libmongocrypt/libmongocrypt/src/os_win/os_mutex.c +8 -16
  198. data/ext/libmongocrypt/libmongocrypt/test/crypt_shared-stub.cpp +57 -87
  199. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-delete/empty/encrypted-payload-v2.json +60 -0
  200. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-delete/success/encrypted-payload-v2.json +67 -0
  201. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explain/with-csfle/encrypted-payload.json +2 -2
  202. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explicit/find-indexed-contentionFactor1-v2.json +8 -0
  203. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explicit/find-indexed-v2.json +8 -0
  204. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explicit/insert-indexed-contentionFactor1-v2.json +8 -0
  205. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explicit/insert-indexed-same-user-and-index-key-v2.json +8 -0
  206. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explicit/insert-indexed-v2.json +8 -0
  207. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-equality-v2/cmd.json +6 -0
  208. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-equality-v2/encrypted-field-map.json +22 -0
  209. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-equality-v2/encrypted-payload.json +40 -0
  210. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-equality-v2/mongocryptd-reply.json +19 -0
  211. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/date-v2/cmd.json +10 -0
  212. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/date-v2/encrypted-field-map.json +27 -0
  213. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/date-v2/encrypted-payload.json +41 -0
  214. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/date-v2/mongocryptd-reply.json +49 -0
  215. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-precision-v2/cmd.json +6 -0
  216. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-precision-v2/encrypted-field-map.json +30 -0
  217. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-precision-v2/encrypted-payload.json +50 -0
  218. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-precision-v2/mongocryptd-reply.json +58 -0
  219. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-v2/cmd.json +6 -0
  220. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-v2/encrypted-field-map.json +27 -0
  221. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-v2/encrypted-payload.json +41 -0
  222. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-v2/mongocryptd-reply.json +49 -0
  223. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-precision-v2/cmd.json +8 -0
  224. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-precision-v2/encrypted-field-map.json +30 -0
  225. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-precision-v2/encrypted-payload.json +44 -0
  226. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-precision-v2/mongocryptd-reply.json +52 -0
  227. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-v2/cmd.json +8 -0
  228. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-v2/encrypted-field-map.json +27 -0
  229. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-v2/encrypted-payload.json +41 -0
  230. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-v2/mongocryptd-reply.json +49 -0
  231. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int32-v2/cmd.json +8 -0
  232. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int32-v2/encrypted-field-map.json +27 -0
  233. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int32-v2/encrypted-payload.json +41 -0
  234. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int32-v2/mongocryptd-reply.json +49 -0
  235. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int64-v2/cmd.json +8 -0
  236. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int64-v2/encrypted-field-map.json +27 -0
  237. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int64-v2/encrypted-payload.json +41 -0
  238. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int64-v2/mongocryptd-reply.json +49 -0
  239. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/double/encrypted-payload-v2.json +26 -0
  240. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/double-precision/encrypted-payload-v2.json +26 -0
  241. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/int32/encrypted-payload-v2.json +26 -0
  242. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/int32-nominmax/encrypted-payload-v2.json +26 -0
  243. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/int32-openinterval/encrypted-payload-v2.json +16 -0
  244. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert/cmd.json +1 -1
  245. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/date/RNG_DATA.h +65 -65
  246. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/date-v2/cmd.json +13 -0
  247. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/date-v2/encrypted-field-map.json +27 -0
  248. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/date-v2/encrypted-payload.json +44 -0
  249. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/date-v2/mongocryptd-reply.json +52 -0
  250. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128/RNG_DATA.h +132 -132
  251. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-precision/RNG_DATA.h +71 -71
  252. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-precision-v2/cmd.json +9 -0
  253. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-precision-v2/encrypted-field-map.json +30 -0
  254. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-precision-v2/encrypted-payload.json +53 -0
  255. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-precision-v2/mongocryptd-reply.json +61 -0
  256. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-v2/cmd.json +9 -0
  257. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-v2/encrypted-field-map.json +27 -0
  258. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-v2/encrypted-payload.json +44 -0
  259. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-v2/mongocryptd-reply.json +52 -0
  260. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double/RNG_DATA.h +68 -68
  261. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-precision/RNG_DATA.h +19 -19
  262. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-precision-v2/cmd.json +11 -0
  263. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-precision-v2/encrypted-field-map.json +30 -0
  264. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-precision-v2/encrypted-payload.json +47 -0
  265. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-precision-v2/mongocryptd-reply.json +55 -0
  266. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-v2/cmd.json +11 -0
  267. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-v2/encrypted-field-map.json +27 -0
  268. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-v2/encrypted-payload.json +44 -0
  269. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-v2/mongocryptd-reply.json +52 -0
  270. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int32/RNG_DATA.h +25 -25
  271. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int32-v2/cmd.json +11 -0
  272. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int32-v2/encrypted-field-map.json +27 -0
  273. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int32-v2/encrypted-payload.json +44 -0
  274. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int32-v2/mongocryptd-reply.json +52 -0
  275. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int64/RNG_DATA.h +65 -65
  276. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int64-v2/cmd.json +11 -0
  277. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int64-v2/encrypted-field-map.json +27 -0
  278. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int64-v2/encrypted-payload.json +44 -0
  279. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int64-v2/mongocryptd-reply.json +52 -0
  280. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/double/RNG_DATA.h +68 -68
  281. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/double/encrypted-payload-v2.json +8 -0
  282. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/double-precision/RNG_DATA.h +19 -19
  283. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/double-precision/encrypted-payload-v2.json +8 -0
  284. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32/RNG_DATA.h +25 -25
  285. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32/encrypted-payload-v2.json +8 -0
  286. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32-nominmax/encrypted-payload-v2.json +8 -0
  287. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/sparsity-2/RNG_DATA.h +15 -15
  288. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/sparsity-2/encrypted-payload-v2.json +8 -0
  289. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-unindexed-v2/cmd.json +9 -0
  290. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-unindexed-v2/encrypted-field-map.json +18 -0
  291. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-unindexed-v2/encrypted-payload.json +14 -0
  292. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-unindexed-v2/mongocryptd-reply.json +41 -0
  293. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-v2/cmd.json +9 -0
  294. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-v2/encrypted-field-map.json +22 -0
  295. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-v2/encrypted-payload.json +39 -0
  296. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-v2/mongocryptd-reply.json +49 -0
  297. data/ext/libmongocrypt/libmongocrypt/test/data/iev-v2/FLECrudTest-insertOneRangeV2.json +10 -0
  298. data/ext/libmongocrypt/libmongocrypt/test/data/iev-v2/FLECrudTest-insertOneV2.json +10 -0
  299. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/README.md +17 -0
  300. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/aes-ctr.json +29 -0
  301. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2-fixed.json +10 -0
  302. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2-generated.json +38 -0
  303. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2aead-decrypt.json +35 -0
  304. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2aead-fixed.json +29 -0
  305. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2aead-generated.json +122 -0
  306. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2v2-aead-fixed.json +29 -0
  307. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2v2-aead-generated.json +122 -0
  308. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/mcgrew.json +12 -0
  309. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/nist.json +20 -0
  310. data/ext/libmongocrypt/libmongocrypt/test/data/tokens/README.md +27 -0
  311. data/ext/libmongocrypt/libmongocrypt/test/data/tokens/mc.json +21 -0
  312. data/ext/libmongocrypt/libmongocrypt/test/data/tokens/server.json +21 -0
  313. data/ext/libmongocrypt/libmongocrypt/test/example-no-bson.c +4 -4
  314. data/ext/libmongocrypt/libmongocrypt/test/example-state-machine.c +278 -323
  315. data/ext/libmongocrypt/libmongocrypt/test/fuzz_kms.c +8 -7
  316. data/ext/libmongocrypt/libmongocrypt/test/test-dll.cpp +6 -7
  317. data/ext/libmongocrypt/libmongocrypt/test/test-gcp-auth.c +221 -283
  318. data/ext/libmongocrypt/libmongocrypt/test/test-mc-efc.c +58 -73
  319. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-find-equality-payload-v2.c +78 -0
  320. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-find-range-payload-v2.c +89 -0
  321. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-payload-iev-v2.c +248 -0
  322. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-payload-iev.c +467 -414
  323. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-payload-iup-v2.c +172 -0
  324. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-payload-iup.c +141 -159
  325. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-payload-uev-v2.c +338 -0
  326. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-payload-uev.c +238 -176
  327. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-rfds.c +373 -474
  328. data/ext/libmongocrypt/libmongocrypt/test/test-mc-range-edge-generation.c +346 -388
  329. data/ext/libmongocrypt/libmongocrypt/test/test-mc-range-encoding.c +708 -825
  330. data/ext/libmongocrypt/libmongocrypt/test/test-mc-range-mincover.c +417 -481
  331. data/ext/libmongocrypt/libmongocrypt/test/test-mc-rangeopts.c +110 -144
  332. data/ext/libmongocrypt/libmongocrypt/test/test-mc-reader.c +124 -207
  333. data/ext/libmongocrypt/libmongocrypt/test/test-mc-tokens.c +223 -213
  334. data/ext/libmongocrypt/libmongocrypt/test/test-mc-writer.c +176 -0
  335. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-assert-match-bson.c +634 -807
  336. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-assert-match-bson.h +1 -2
  337. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-assert.h +168 -189
  338. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-buffer.c +187 -211
  339. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-cache-oauth.c +34 -36
  340. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-cache.c +210 -233
  341. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-ciphertext.c +185 -220
  342. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-compact.c +331 -445
  343. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-crypto-hooks.c +670 -814
  344. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-crypto-std-hooks.c +102 -135
  345. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-crypto-std-hooks.h +54 -71
  346. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-crypto.c +394 -846
  347. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-csfle-lib.c +159 -183
  348. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-ctx-decrypt.c +867 -1045
  349. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-ctx-encrypt.c +4349 -4713
  350. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-ctx-rewrap-many-datakey.c +750 -964
  351. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-ctx-setopt.c +991 -1178
  352. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-datakey.c +342 -419
  353. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-dll.c +23 -30
  354. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-endpoint.c +98 -111
  355. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-kek.c +49 -52
  356. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-key-broker.c +770 -920
  357. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-key-cache.c +354 -407
  358. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-key.c +197 -245
  359. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-kms-ctx.c +286 -370
  360. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-kms-responses.c +147 -166
  361. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-local-kms.c +50 -61
  362. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-log.c +85 -100
  363. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-marking.c +656 -692
  364. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-status.c +46 -58
  365. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-traverse-util.c +377 -451
  366. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-util.c +48 -67
  367. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-util.h +4 -10
  368. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt.c +827 -918
  369. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt.h +111 -172
  370. data/ext/libmongocrypt/libmongocrypt/test/util/csfle.c +508 -559
  371. data/ext/libmongocrypt/libmongocrypt/test/util/util.c +735 -881
  372. data/ext/libmongocrypt/libmongocrypt/test/util/util.h +33 -55
  373. data/lib/libmongocrypt_helper/version.rb +2 -2
  374. data.tar.gz.sig +0 -0
  375. metadata +154 -26
  376. metadata.gz.sig +0 -0
  377. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/test/resources/fle2-find-range-explicit/int32/encrypted-payload.json +0 -26
  378. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/test/resources/fle2-find-range-explicit/int32/key-document.json +0 -0
  379. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/credentialsProvider.js +0 -33
  380. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/credentialsProvider.test.js +0 -163
  381. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/fle2-find-range-explicit/int32/encrypted-payload.json +0 -26
  382. data/ext/libmongocrypt/libmongocrypt/test/data/aes-ctr.cstructs +0 -359
  383. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-aead.cstructs +0 -109
  384. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/int32-nominmax/encrypted-payload.json +0 -26
  385. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/int32-nominmax/rangeopts.json +0 -5
  386. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/int32-nominmax/value-to-encrypt.json +0 -20
  387. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32-nominmax/RNG_DATA.h +0 -70
  388. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32-nominmax/encrypted-payload.json +0 -8
  389. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32-nominmax/rangeopts.json +0 -5
  390. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32-nominmax/value-to-encrypt.json +0 -5
  391. data/ext/libmongocrypt/libmongocrypt/test/data/fle2.cstructs +0 -33
  392. /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
  393. /data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/test/resources/{fle2-find-range-explicit → fle2-find-range-explicit-v2}/int32/rangeopts.json +0 -0
  394. /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
  395. /data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/{fle2-find-range-explicit → fle2-find-range-explicit-v2}/int32/rangeopts.json +0 -0
  396. /data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/{fle2-find-range-explicit → fle2-find-range-explicit-v2}/int32/value-to-encrypt.json +0 -0
@@ -14,14 +14,13 @@
14
14
  * limitations under the License.
15
15
  */
16
16
 
17
- #include "mlib/thread.h"
18
- #include "mlib/path.h"
19
17
  #include "mlib/error.h"
18
+ #include "mlib/path.h"
19
+ #include "mlib/thread.h"
20
20
 
21
- #include <kms_message/kms_message.h>
22
21
  #include <bson/bson.h>
22
+ #include <kms_message/kms_message.h>
23
23
 
24
- #include "mongocrypt-private.h"
25
24
  #include "mongocrypt-binary-private.h"
26
25
  #include "mongocrypt-cache-collinfo-private.h"
27
26
  #include "mongocrypt-cache-key-private.h"
@@ -30,465 +29,398 @@
30
29
  #include "mongocrypt-log-private.h"
31
30
  #include "mongocrypt-mutex-private.h"
32
31
  #include "mongocrypt-opts-private.h"
32
+ #include "mongocrypt-private.h"
33
33
  #include "mongocrypt-status-private.h"
34
34
  #include "mongocrypt-util-private.h"
35
35
 
36
36
  /* Assert size for interop with wrapper purposes */
37
- BSON_STATIC_ASSERT (sizeof (mongocrypt_log_level_t) == 4);
37
+ BSON_STATIC_ASSERT(sizeof(mongocrypt_log_level_t) == 4);
38
38
 
39
+ const char *mongocrypt_version(uint32_t *len) {
40
+ if (len) {
41
+ *len = (uint32_t)strlen(MONGOCRYPT_VERSION);
42
+ }
43
+ return MONGOCRYPT_VERSION;
44
+ }
39
45
 
40
- const char *
41
- mongocrypt_version (uint32_t *len)
42
- {
43
- if (len) {
44
- *len = (uint32_t) strlen (MONGOCRYPT_VERSION);
45
- }
46
- return MONGOCRYPT_VERSION;
46
+ void _mongocrypt_set_error(mongocrypt_status_t *status,
47
+ mongocrypt_status_type_t type,
48
+ uint32_t code,
49
+ const char *format,
50
+ ...) {
51
+ va_list args;
52
+ char *prepared_message;
53
+
54
+ if (status) {
55
+ va_start(args, format);
56
+ prepared_message = bson_strdupv_printf(format, args);
57
+ if (!prepared_message) {
58
+ mongocrypt_status_set(status, type, code, "Out of memory", -1);
59
+ } else {
60
+ mongocrypt_status_set(status, type, code, prepared_message, -1);
61
+ bson_free(prepared_message);
62
+ }
63
+ va_end(args);
64
+ }
47
65
  }
48
66
 
67
+ const char *tmp_json(const bson_t *bson) {
68
+ static char storage[1024];
69
+ char *json;
70
+
71
+ BSON_ASSERT_PARAM(bson);
72
+
73
+ memset(storage, 0, 1024);
74
+ json = bson_as_canonical_extended_json(bson, NULL);
75
+ bson_snprintf(storage, sizeof(storage), "%s", json);
76
+ bson_free(json);
77
+ return (const char *)storage;
78
+ }
79
+
80
+ const char *tmp_buf(const _mongocrypt_buffer_t *buf) {
81
+ static char storage[1024];
82
+ size_t i, n;
83
+
84
+ BSON_ASSERT_PARAM(buf);
49
85
 
50
- void
51
- _mongocrypt_set_error (mongocrypt_status_t *status,
52
- mongocrypt_status_type_t type,
53
- uint32_t code,
54
- const char *format,
55
- ...)
56
- {
57
- va_list args;
58
- char *prepared_message;
59
-
60
- if (status) {
61
- va_start (args, format);
62
- prepared_message = bson_strdupv_printf (format, args);
63
- if (!prepared_message) {
64
- mongocrypt_status_set (status, type, code, "Out of memory", -1);
65
- } else {
66
- mongocrypt_status_set (status, type, code, prepared_message, -1);
67
- bson_free (prepared_message);
68
- }
69
- va_end (args);
70
- }
86
+ memset(storage, 0, 1024);
87
+ /* capped at two characters per byte, minus 1 for trailing \0 */
88
+ n = sizeof(storage) / 2 - 1;
89
+ if (buf->len < n) {
90
+ n = buf->len;
91
+ }
92
+
93
+ for (i = 0; i < n; i++) {
94
+ bson_snprintf(storage + (i * 2), 3, "%02x", buf->data[i]);
95
+ }
96
+
97
+ return (const char *)storage;
71
98
  }
72
99
 
100
+ static void _mongocrypt_do_init(void) {
101
+ (void)kms_message_init();
102
+ _native_crypto_init();
103
+ }
73
104
 
74
- const char *
75
- tmp_json (const bson_t *bson)
76
- {
77
- static char storage[1024];
78
- char *json;
105
+ mongocrypt_t *mongocrypt_new(void) {
106
+ mongocrypt_t *crypt;
107
+
108
+ crypt = bson_malloc0(sizeof(mongocrypt_t));
109
+ BSON_ASSERT(crypt);
110
+ crypt->crypto = bson_malloc0(sizeof(*crypt->crypto));
111
+ BSON_ASSERT(crypt->crypto);
112
+
113
+ _mongocrypt_mutex_init(&crypt->mutex);
114
+ _mongocrypt_cache_collinfo_init(&crypt->cache_collinfo);
115
+ _mongocrypt_cache_key_init(&crypt->cache_key);
116
+ crypt->status = mongocrypt_status_new();
117
+ _mongocrypt_opts_init(&crypt->opts);
118
+ _mongocrypt_log_init(&crypt->log);
119
+ // Default to using FLEv2 (aka QEv2)
120
+ crypt->opts.use_fle2_v2 = true;
121
+ crypt->ctx_counter = 1;
122
+ crypt->cache_oauth_azure = _mongocrypt_cache_oauth_new();
123
+ crypt->cache_oauth_gcp = _mongocrypt_cache_oauth_new();
124
+ crypt->csfle = (_mongo_crypt_v1_vtable){.okay = false};
125
+
126
+ static mlib_once_flag init_flag = MLIB_ONCE_INITIALIZER;
127
+
128
+ if (!mlib_call_once(&init_flag, _mongocrypt_do_init) || !_native_crypto_initialized) {
129
+ mongocrypt_status_t *status = crypt->status;
130
+
131
+ CLIENT_ERR("failed to initialize");
132
+ /* Return crypt with failure status so caller can obtain error when
133
+ * calling mongocrypt_init */
134
+ }
135
+
136
+ return crypt;
137
+ }
79
138
 
80
- BSON_ASSERT_PARAM (bson);
139
+ #define ASSERT_MONGOCRYPT_PARAM_UNINIT(crypt) \
140
+ { \
141
+ const mongocrypt_t *_crypt = (crypt); \
142
+ BSON_ASSERT_PARAM(_crypt); \
143
+ if (_crypt->initialized) { \
144
+ mongocrypt_status_t *status = _crypt->status; \
145
+ CLIENT_ERR("options cannot be set after initialization"); \
146
+ return false; \
147
+ } \
148
+ }
149
+
150
+ bool mongocrypt_setopt_fle2v2(mongocrypt_t *crypt, bool enable) {
151
+ ASSERT_MONGOCRYPT_PARAM_UNINIT(crypt);
152
+
153
+ crypt->opts.use_fle2_v2 = enable;
154
+ return true;
155
+ }
81
156
 
82
- memset (storage, 0, 1024);
83
- json = bson_as_canonical_extended_json (bson, NULL);
84
- bson_snprintf (storage, sizeof (storage), "%s", json);
85
- bson_free (json);
86
- return (const char *) storage;
157
+ bool mongocrypt_setopt_log_handler(mongocrypt_t *crypt, mongocrypt_log_fn_t log_fn, void *log_ctx) {
158
+ ASSERT_MONGOCRYPT_PARAM_UNINIT(crypt);
159
+ crypt->opts.log_fn = log_fn;
160
+ crypt->opts.log_ctx = log_ctx;
161
+ return true;
87
162
  }
88
163
 
164
+ bool mongocrypt_setopt_kms_provider_aws(mongocrypt_t *crypt,
165
+ const char *aws_access_key_id,
166
+ int32_t aws_access_key_id_len,
167
+ const char *aws_secret_access_key,
168
+ int32_t aws_secret_access_key_len) {
169
+ ASSERT_MONGOCRYPT_PARAM_UNINIT(crypt);
170
+
171
+ mongocrypt_status_t *status = crypt->status;
172
+ _mongocrypt_opts_kms_providers_t *const kms_providers = &crypt->opts.kms_providers;
173
+
174
+ if (0 != (kms_providers->configured_providers & MONGOCRYPT_KMS_PROVIDER_AWS)) {
175
+ CLIENT_ERR("aws kms provider already set");
176
+ return false;
177
+ }
178
+
179
+ if (!_mongocrypt_validate_and_copy_string(aws_access_key_id,
180
+ aws_access_key_id_len,
181
+ &kms_providers->aws.access_key_id)) {
182
+ CLIENT_ERR("invalid aws access key id");
183
+ return false;
184
+ }
185
+
186
+ if (!_mongocrypt_validate_and_copy_string(aws_secret_access_key,
187
+ aws_secret_access_key_len,
188
+ &kms_providers->aws.secret_access_key)) {
189
+ CLIENT_ERR("invalid aws secret access key");
190
+ return false;
191
+ }
192
+
193
+ if (crypt->log.trace_enabled) {
194
+ _mongocrypt_log(&crypt->log,
195
+ MONGOCRYPT_LOG_LEVEL_TRACE,
196
+ "%s (%s=\"%s\", %s=%d, %s=\"%s\", %s=%d)",
197
+ BSON_FUNC,
198
+ "aws_access_key_id",
199
+ kms_providers->aws.access_key_id,
200
+ "aws_access_key_id_len",
201
+ aws_access_key_id_len,
202
+ "aws_secret_access_key",
203
+ kms_providers->aws.secret_access_key,
204
+ "aws_secret_access_key_len",
205
+ aws_secret_access_key_len);
206
+ }
207
+ kms_providers->configured_providers |= MONGOCRYPT_KMS_PROVIDER_AWS;
208
+ return true;
209
+ }
89
210
 
90
- const char *
91
- tmp_buf (const _mongocrypt_buffer_t *buf)
92
- {
93
- static char storage[1024];
94
- size_t i, n;
211
+ char *_mongocrypt_new_string_from_bytes(const void *in, int len) {
212
+ const int max_bytes = 100;
213
+ const int chars_per_byte = 2;
214
+ int out_size = max_bytes * chars_per_byte;
215
+ const unsigned char *src = in;
216
+ char *out;
217
+ char *ret;
95
218
 
96
- BSON_ASSERT_PARAM (buf);
219
+ out_size += len > max_bytes ? (int)sizeof("...") : 1 /* for null */;
220
+ out = bson_malloc0((size_t)out_size);
221
+ BSON_ASSERT(out);
97
222
 
98
- memset (storage, 0, 1024);
99
- /* capped at two characters per byte, minus 1 for trailing \0 */
100
- n = sizeof (storage) / 2 - 1;
101
- if (buf->len < n) {
102
- n = buf->len;
103
- }
223
+ ret = out;
104
224
 
105
- for (i = 0; i < n; i++) {
106
- bson_snprintf (storage + (i * 2), 3, "%02x", buf->data[i]);
107
- }
225
+ for (int i = 0; i < len && i < max_bytes; i++, out += chars_per_byte) {
226
+ sprintf(out, "%02X", src[i]);
227
+ }
108
228
 
109
- return (const char *) storage;
229
+ sprintf(out, (len > max_bytes) ? "..." : "");
230
+ return ret;
110
231
  }
111
232
 
112
- static void
113
- _mongocrypt_do_init (void)
114
- {
115
- (void) kms_message_init ();
116
- _native_crypto_init ();
233
+ char *_mongocrypt_new_json_string_from_binary(mongocrypt_binary_t *binary) {
234
+ bson_t bson;
235
+ uint32_t len;
236
+
237
+ BSON_ASSERT_PARAM(binary);
238
+
239
+ if (!_mongocrypt_binary_to_bson(binary, &bson) || !bson_validate(&bson, BSON_VALIDATE_NONE, NULL)) {
240
+ char *hex;
241
+ char *full_str;
242
+
243
+ BSON_ASSERT(binary->len <= (uint32_t)INT_MAX);
244
+ hex = _mongocrypt_new_string_from_bytes(binary->data, (int)binary->len);
245
+ full_str = bson_strdup_printf("(malformed) %s", hex);
246
+ bson_free(hex);
247
+ return full_str;
248
+ }
249
+ return bson_as_canonical_extended_json(&bson, (size_t *)&len);
117
250
  }
118
251
 
252
+ bool mongocrypt_setopt_schema_map(mongocrypt_t *crypt, mongocrypt_binary_t *schema_map) {
253
+ ASSERT_MONGOCRYPT_PARAM_UNINIT(crypt);
119
254
 
120
- mongocrypt_t *
121
- mongocrypt_new (void)
122
- {
123
- mongocrypt_t *crypt;
255
+ bson_t tmp;
256
+ bson_error_t bson_err;
257
+ mongocrypt_status_t *status = crypt->status;
124
258
 
125
- crypt = bson_malloc0 (sizeof (mongocrypt_t));
126
- BSON_ASSERT (crypt);
127
- crypt->crypto = bson_malloc0 (sizeof (*crypt->crypto));
128
- BSON_ASSERT (crypt->crypto);
259
+ if (!schema_map || !mongocrypt_binary_data(schema_map)) {
260
+ CLIENT_ERR("passed null schema map");
261
+ return false;
262
+ }
129
263
 
130
- _mongocrypt_mutex_init (&crypt->mutex);
131
- _mongocrypt_cache_collinfo_init (&crypt->cache_collinfo);
132
- _mongocrypt_cache_key_init (&crypt->cache_key);
133
- crypt->status = mongocrypt_status_new ();
134
- _mongocrypt_opts_init (&crypt->opts);
135
- _mongocrypt_log_init (&crypt->log);
136
- crypt->ctx_counter = 1;
137
- crypt->cache_oauth_azure = _mongocrypt_cache_oauth_new ();
138
- crypt->cache_oauth_gcp = _mongocrypt_cache_oauth_new ();
139
- crypt->csfle = (_mongo_crypt_v1_vtable){.okay = false};
264
+ if (!_mongocrypt_buffer_empty(&crypt->opts.schema_map)) {
265
+ CLIENT_ERR("already set schema map");
266
+ return false;
267
+ }
140
268
 
141
- static mlib_once_flag init_flag = MLIB_ONCE_INITIALIZER;
269
+ _mongocrypt_buffer_copy_from_binary(&crypt->opts.schema_map, schema_map);
142
270
 
143
- if (!mlib_call_once (&init_flag, _mongocrypt_do_init) ||
144
- !_native_crypto_initialized) {
145
- mongocrypt_status_t *status = crypt->status;
271
+ /* validate bson */
272
+ if (!_mongocrypt_buffer_to_bson(&crypt->opts.schema_map, &tmp)) {
273
+ CLIENT_ERR("invalid bson");
274
+ return false;
275
+ }
146
276
 
147
- CLIENT_ERR ("failed to initialize");
148
- /* Return crypt with failure status so caller can obtain error when
149
- * calling mongocrypt_init */
150
- }
277
+ if (!bson_validate_with_error(&tmp, BSON_VALIDATE_NONE, &bson_err)) {
278
+ CLIENT_ERR("%s", bson_err.message);
279
+ return false;
280
+ }
151
281
 
152
- return crypt;
282
+ return true;
153
283
  }
154
284
 
285
+ bool mongocrypt_setopt_encrypted_field_config_map(mongocrypt_t *crypt, mongocrypt_binary_t *efc_map) {
286
+ ASSERT_MONGOCRYPT_PARAM_UNINIT(crypt);
155
287
 
156
- bool
157
- mongocrypt_setopt_log_handler (mongocrypt_t *crypt,
158
- mongocrypt_log_fn_t log_fn,
159
- void *log_ctx)
160
- {
161
- if (!crypt) {
162
- return false;
163
- }
164
-
165
- if (crypt->initialized) {
166
- mongocrypt_status_t *status = crypt->status;
167
- CLIENT_ERR ("options cannot be set after initialization");
168
- return false;
169
- }
170
- crypt->opts.log_fn = log_fn;
171
- crypt->opts.log_ctx = log_ctx;
172
- return true;
173
- }
288
+ mongocrypt_status_t *status = crypt->status;
289
+ bson_t as_bson;
290
+ bson_error_t bson_err;
174
291
 
175
- bool
176
- mongocrypt_setopt_kms_provider_aws (mongocrypt_t *crypt,
177
- const char *aws_access_key_id,
178
- int32_t aws_access_key_id_len,
179
- const char *aws_secret_access_key,
180
- int32_t aws_secret_access_key_len)
181
- {
182
- BSON_ASSERT_PARAM (crypt);
183
-
184
- mongocrypt_status_t *status = crypt->status;
185
- _mongocrypt_opts_kms_providers_t *const kms_providers =
186
- &crypt->opts.kms_providers;
187
-
188
- if (crypt->initialized) {
189
- CLIENT_ERR ("options cannot be set after initialization");
190
- return false;
191
- }
192
-
193
- if (0 !=
194
- (kms_providers->configured_providers & MONGOCRYPT_KMS_PROVIDER_AWS)) {
195
- CLIENT_ERR ("aws kms provider already set");
196
- return false;
197
- }
198
-
199
- if (!_mongocrypt_validate_and_copy_string (
200
- aws_access_key_id,
201
- aws_access_key_id_len,
202
- &kms_providers->aws.access_key_id)) {
203
- CLIENT_ERR ("invalid aws access key id");
204
- return false;
205
- }
206
-
207
- if (!_mongocrypt_validate_and_copy_string (
208
- aws_secret_access_key,
209
- aws_secret_access_key_len,
210
- &kms_providers->aws.secret_access_key)) {
211
- CLIENT_ERR ("invalid aws secret access key");
212
- return false;
213
- }
214
-
215
- if (crypt->log.trace_enabled) {
216
- _mongocrypt_log (&crypt->log,
217
- MONGOCRYPT_LOG_LEVEL_TRACE,
218
- "%s (%s=\"%s\", %s=%d, %s=\"%s\", %s=%d)",
219
- BSON_FUNC,
220
- "aws_access_key_id",
221
- kms_providers->aws.access_key_id,
222
- "aws_access_key_id_len",
223
- aws_access_key_id_len,
224
- "aws_secret_access_key",
225
- kms_providers->aws.secret_access_key,
226
- "aws_secret_access_key_len",
227
- aws_secret_access_key_len);
228
- }
229
- kms_providers->configured_providers |= MONGOCRYPT_KMS_PROVIDER_AWS;
230
- return true;
231
- }
292
+ if (!efc_map || !mongocrypt_binary_data(efc_map)) {
293
+ CLIENT_ERR("passed null encrypted_field_config_map");
294
+ return false;
295
+ }
232
296
 
233
- char *
234
- _mongocrypt_new_string_from_bytes (const void *in, int len)
235
- {
236
- const int max_bytes = 100;
237
- const int chars_per_byte = 2;
238
- int out_size = max_bytes * chars_per_byte;
239
- const unsigned char *src = in;
240
- char *out;
241
- char *ret;
297
+ if (!_mongocrypt_buffer_empty(&crypt->opts.encrypted_field_config_map)) {
298
+ CLIENT_ERR("already set encrypted_field_config_map");
299
+ return false;
300
+ }
242
301
 
243
- out_size += len > max_bytes ? (int) sizeof ("...") : 1 /* for null */;
244
- out = bson_malloc0 ((size_t) out_size);
245
- BSON_ASSERT (out);
302
+ _mongocrypt_buffer_copy_from_binary(&crypt->opts.encrypted_field_config_map, efc_map);
246
303
 
247
- ret = out;
304
+ /* validate bson */
305
+ if (!_mongocrypt_buffer_to_bson(&crypt->opts.encrypted_field_config_map, &as_bson)) {
306
+ CLIENT_ERR("invalid bson");
307
+ return false;
308
+ }
248
309
 
249
- for (int i = 0; i < len && i < max_bytes; i++, out += chars_per_byte) {
250
- sprintf (out, "%02X", src[i]);
251
- }
310
+ if (!bson_validate_with_error(&as_bson, BSON_VALIDATE_NONE, &bson_err)) {
311
+ CLIENT_ERR("%s", bson_err.message);
312
+ return false;
313
+ }
252
314
 
253
- sprintf (out, (len > max_bytes) ? "..." : "");
254
- return ret;
315
+ return true;
255
316
  }
256
317
 
257
- char *
258
- _mongocrypt_new_json_string_from_binary (mongocrypt_binary_t *binary)
259
- {
260
- bson_t bson;
261
- uint32_t len;
262
-
263
- BSON_ASSERT_PARAM (binary);
264
-
265
- if (!_mongocrypt_binary_to_bson (binary, &bson) ||
266
- !bson_validate (&bson, BSON_VALIDATE_NONE, NULL)) {
267
- char *hex;
268
- char *full_str;
269
-
270
- BSON_ASSERT (binary->len <= (uint32_t) INT_MAX);
271
- hex = _mongocrypt_new_string_from_bytes (binary->data, (int) binary->len);
272
- full_str = bson_strdup_printf ("(malformed) %s", hex);
273
- bson_free (hex);
274
- return full_str;
275
- }
276
- return bson_as_canonical_extended_json (&bson, (size_t *) &len);
277
- }
318
+ bool mongocrypt_setopt_kms_provider_local(mongocrypt_t *crypt, mongocrypt_binary_t *key) {
319
+ ASSERT_MONGOCRYPT_PARAM_UNINIT(crypt);
278
320
 
279
- bool
280
- mongocrypt_setopt_schema_map (mongocrypt_t *crypt,
281
- mongocrypt_binary_t *schema_map)
282
- {
283
- BSON_ASSERT_PARAM (crypt);
284
-
285
- bson_t tmp;
286
- bson_error_t bson_err;
287
- mongocrypt_status_t *status = crypt->status;
288
-
289
- if (crypt->initialized) {
290
- CLIENT_ERR ("options cannot be set after initialization");
291
- return false;
292
- }
293
-
294
- if (!schema_map || !mongocrypt_binary_data (schema_map)) {
295
- CLIENT_ERR ("passed null schema map");
296
- return false;
297
- }
298
-
299
- if (!_mongocrypt_buffer_empty (&crypt->opts.schema_map)) {
300
- CLIENT_ERR ("already set schema map");
301
- return false;
302
- }
303
-
304
- _mongocrypt_buffer_copy_from_binary (&crypt->opts.schema_map, schema_map);
305
-
306
- /* validate bson */
307
- if (!_mongocrypt_buffer_to_bson (&crypt->opts.schema_map, &tmp)) {
308
- CLIENT_ERR ("invalid bson");
309
- return false;
310
- }
311
-
312
- if (!bson_validate_with_error (&tmp, BSON_VALIDATE_NONE, &bson_err)) {
313
- CLIENT_ERR ("%s", bson_err.message);
314
- return false;
315
- }
316
-
317
- return true;
318
- }
321
+ mongocrypt_status_t *status = crypt->status;
322
+ _mongocrypt_opts_kms_providers_t *const kms_providers = &crypt->opts.kms_providers;
319
323
 
320
- bool
321
- mongocrypt_setopt_encrypted_field_config_map (mongocrypt_t *crypt,
322
- mongocrypt_binary_t *efc_map)
323
- {
324
- BSON_ASSERT_PARAM (crypt);
325
-
326
- mongocrypt_status_t *status = crypt->status;
327
- bson_t as_bson;
328
- bson_error_t bson_err;
329
-
330
- if (crypt->initialized) {
331
- CLIENT_ERR ("options cannot be set after initialization");
332
- return false;
333
- }
334
-
335
- if (!efc_map || !mongocrypt_binary_data (efc_map)) {
336
- CLIENT_ERR ("passed null encrypted_field_config_map");
337
- return false;
338
- }
339
-
340
- if (!_mongocrypt_buffer_empty (&crypt->opts.encrypted_field_config_map)) {
341
- CLIENT_ERR ("already set encrypted_field_config_map");
342
- return false;
343
- }
344
-
345
- _mongocrypt_buffer_copy_from_binary (&crypt->opts.encrypted_field_config_map,
346
- efc_map);
347
-
348
- /* validate bson */
349
- if (!_mongocrypt_buffer_to_bson (&crypt->opts.encrypted_field_config_map,
350
- &as_bson)) {
351
- CLIENT_ERR ("invalid bson");
352
- return false;
353
- }
354
-
355
- if (!bson_validate_with_error (&as_bson, BSON_VALIDATE_NONE, &bson_err)) {
356
- CLIENT_ERR ("%s", bson_err.message);
357
- return false;
358
- }
359
-
360
- return true;
361
- }
324
+ if (0 != (kms_providers->configured_providers & MONGOCRYPT_KMS_PROVIDER_LOCAL)) {
325
+ CLIENT_ERR("local kms provider already set");
326
+ return false;
327
+ }
362
328
 
363
- bool
364
- mongocrypt_setopt_kms_provider_local (mongocrypt_t *crypt,
365
- mongocrypt_binary_t *key)
366
- {
367
- BSON_ASSERT_PARAM (crypt);
368
-
369
- mongocrypt_status_t *status = crypt->status;
370
- _mongocrypt_opts_kms_providers_t *const kms_providers =
371
- &crypt->opts.kms_providers;
372
-
373
- if (crypt->initialized) {
374
- CLIENT_ERR ("options cannot be set after initialization");
375
- return false;
376
- }
377
-
378
- if (0 !=
379
- (kms_providers->configured_providers & MONGOCRYPT_KMS_PROVIDER_LOCAL)) {
380
- CLIENT_ERR ("local kms provider already set");
381
- return false;
382
- }
383
-
384
- if (!key) {
385
- CLIENT_ERR ("passed null key");
386
- return false;
387
- }
388
-
389
- if (mongocrypt_binary_len (key) != MONGOCRYPT_KEY_LEN) {
390
- CLIENT_ERR ("local key must be %d bytes", MONGOCRYPT_KEY_LEN);
391
- return false;
392
- }
393
-
394
- if (crypt->log.trace_enabled) {
395
- char *key_val;
396
- BSON_ASSERT (key->len <= (uint32_t) INT_MAX);
397
- key_val = _mongocrypt_new_string_from_bytes (key->data, (int) key->len);
398
-
399
- _mongocrypt_log (&crypt->log,
400
- MONGOCRYPT_LOG_LEVEL_TRACE,
401
- "%s (%s=\"%s\")",
402
- BSON_FUNC,
403
- "key",
404
- key_val);
405
- bson_free (key_val);
406
- }
407
-
408
- _mongocrypt_buffer_copy_from_binary (&kms_providers->local.key, key);
409
- kms_providers->configured_providers |= MONGOCRYPT_KMS_PROVIDER_LOCAL;
410
- return true;
329
+ if (!key) {
330
+ CLIENT_ERR("passed null key");
331
+ return false;
332
+ }
333
+
334
+ if (mongocrypt_binary_len(key) != MONGOCRYPT_KEY_LEN) {
335
+ CLIENT_ERR("local key must be %d bytes", MONGOCRYPT_KEY_LEN);
336
+ return false;
337
+ }
338
+
339
+ if (crypt->log.trace_enabled) {
340
+ char *key_val;
341
+ BSON_ASSERT(key->len <= (uint32_t)INT_MAX);
342
+ key_val = _mongocrypt_new_string_from_bytes(key->data, (int)key->len);
343
+
344
+ _mongocrypt_log(&crypt->log, MONGOCRYPT_LOG_LEVEL_TRACE, "%s (%s=\"%s\")", BSON_FUNC, "key", key_val);
345
+ bson_free(key_val);
346
+ }
347
+
348
+ _mongocrypt_buffer_copy_from_binary(&kms_providers->local.key, key);
349
+ kms_providers->configured_providers |= MONGOCRYPT_KMS_PROVIDER_LOCAL;
350
+ return true;
411
351
  }
412
352
 
413
353
  typedef struct {
414
- /// Whether the load is successful
415
- bool okay;
416
- /// The DLL handle to the opened library.
417
- mcr_dll lib;
418
- /// A vtable for the functions in the DLL
419
- _mongo_crypt_v1_vtable vtable;
354
+ /// Whether the load is successful
355
+ bool okay;
356
+ /// The DLL handle to the opened library.
357
+ mcr_dll lib;
358
+ /// A vtable for the functions in the DLL
359
+ _mongo_crypt_v1_vtable vtable;
420
360
  } _loaded_csfle;
421
361
 
422
362
  /**
423
363
  * @brief Attempt to open the CSFLE dynamic library and initialize a vtable for
424
364
  * it.
365
+ *
366
+ * @param status is an optional status to set an error message if `mcr_dll_open` fails.
425
367
  */
426
- static _loaded_csfle
427
- _try_load_csfle (const char *filepath, _mongocrypt_log_t *log)
428
- {
429
- // Try to open the dynamic lib
430
- mcr_dll lib = mcr_dll_open (filepath);
431
- // Check for errors, which are represented by strings
432
- if (lib.error_string.data) {
433
- // Error opening candidate
434
- _mongocrypt_log (
435
- log,
436
- MONGOCRYPT_LOG_LEVEL_WARNING,
437
- "Error while opening candidate for CSFLE dynamic library [%s]: %s",
438
- filepath,
439
- lib.error_string.data);
440
- // Free resources, which will include the error string
441
- mcr_dll_close (lib);
442
- // Bad:
443
- return (_loaded_csfle){.okay = false};
444
- }
445
-
446
- // Successfully opened DLL
447
- _mongocrypt_log (log,
448
- MONGOCRYPT_LOG_LEVEL_TRACE,
449
- "Loading CSFLE dynamic library [%s]",
450
- filepath);
451
-
452
- // Construct the library vtable
453
- _mongo_crypt_v1_vtable vtable = {.okay = true};
454
- #define X_FUNC(Name, RetType, ...) \
455
- { \
456
- /* Symbol names are qualified by the lib name and version: */ \
457
- const char *symname = "mongo_crypt_v1_" #Name; \
458
- vtable.Name = mcr_dll_sym (lib, symname); \
459
- if (vtable.Name == NULL) { \
460
- /* The requested symbol is not present */ \
461
- _mongocrypt_log ( \
462
- log, \
463
- MONGOCRYPT_LOG_LEVEL_ERROR, \
464
- "Missing required symbol '%s' from CSFLE dynamic library [%s]", \
465
- symname, \
466
- filepath); \
467
- /* Mark the vtable as broken, but keep trying to load more symbols to \
468
- * produce error messages for all missing symbols */ \
469
- vtable.okay = false; \
470
- } \
471
- }
472
- MONGOC_CSFLE_FUNCTIONS_X
368
+ static _loaded_csfle _try_load_csfle(const char *filepath, _mongocrypt_log_t *log, mongocrypt_status_t *status) {
369
+ // Try to open the dynamic lib
370
+ mcr_dll lib = mcr_dll_open(filepath);
371
+ // Check for errors, which are represented by strings
372
+ if (lib.error_string.data) {
373
+ // Error opening candidate
374
+ _mongocrypt_log(log,
375
+ MONGOCRYPT_LOG_LEVEL_WARNING,
376
+ "Error while opening candidate for CSFLE dynamic library [%s]: %s",
377
+ filepath,
378
+ lib.error_string.data);
379
+ CLIENT_ERR("Error while opening candidate for CSFLE dynamic library [%s]: %s", filepath, lib.error_string.data);
380
+ // Free resources, which will include the error string
381
+ mcr_dll_close(lib);
382
+ // Bad:
383
+ return (_loaded_csfle){.okay = false};
384
+ }
385
+
386
+ // Successfully opened DLL
387
+ _mongocrypt_log(log, MONGOCRYPT_LOG_LEVEL_TRACE, "Loading CSFLE dynamic library [%s]", filepath);
388
+
389
+ // Construct the library vtable
390
+ _mongo_crypt_v1_vtable vtable = {.okay = true};
391
+ #define X_FUNC(Name, RetType, ...) \
392
+ { \
393
+ /* Symbol names are qualified by the lib name and version: */ \
394
+ const char *symname = "mongo_crypt_v1_" #Name; \
395
+ vtable.Name = mcr_dll_sym(lib, symname); \
396
+ if (vtable.Name == NULL) { \
397
+ /* The requested symbol is not present */ \
398
+ _mongocrypt_log(log, \
399
+ MONGOCRYPT_LOG_LEVEL_ERROR, \
400
+ "Missing required symbol '%s' from CSFLE dynamic library [%s]", \
401
+ symname, \
402
+ filepath); \
403
+ /* Mark the vtable as broken, but keep trying to load more symbols to \
404
+ * produce error messages for all missing symbols */ \
405
+ vtable.okay = false; \
406
+ } \
407
+ }
408
+ MONGOC_CSFLE_FUNCTIONS_X
473
409
  #undef X_FUNC
474
410
 
475
- if (!vtable.okay) {
476
- mcr_dll_close (lib);
477
- _mongocrypt_log (
478
- log,
479
- MONGOCRYPT_LOG_LEVEL_ERROR,
480
- "One or more required symbols are missing from CSFLE dynamic library "
481
- "[%s], so this dynamic library will not be used.",
482
- filepath);
483
- return (_loaded_csfle){.okay = false};
484
- }
485
-
486
- // Success!
487
- _mongocrypt_log (log,
488
- MONGOCRYPT_LOG_LEVEL_INFO,
489
- "Opened CSFLE dynamic library [%s]",
490
- filepath);
491
- return (_loaded_csfle){.okay = true, .lib = lib, .vtable = vtable};
411
+ if (!vtable.okay) {
412
+ mcr_dll_close(lib);
413
+ _mongocrypt_log(log,
414
+ MONGOCRYPT_LOG_LEVEL_ERROR,
415
+ "One or more required symbols are missing from CSFLE dynamic library "
416
+ "[%s], so this dynamic library will not be used.",
417
+ filepath);
418
+ return (_loaded_csfle){.okay = false};
419
+ }
420
+
421
+ // Success!
422
+ _mongocrypt_log(log, MONGOCRYPT_LOG_LEVEL_INFO, "Opened CSFLE dynamic library [%s]", filepath);
423
+ return (_loaded_csfle){.okay = true, .lib = lib, .vtable = vtable};
492
424
  }
493
425
 
494
426
  /**
@@ -498,120 +430,108 @@ _try_load_csfle (const char *filepath, _mongocrypt_log_t *log)
498
430
  * @return true If no error occurred and the path is valid
499
431
  * @return false If there was an error and `filepath` cannot be processed
500
432
  */
501
- static bool
502
- _try_replace_dollar_origin (mstr *filepath, _mongocrypt_log_t *log)
503
- {
504
- const mstr_view dollar_origin = mstrv_lit ("$ORIGIN");
505
-
506
- BSON_ASSERT_PARAM (filepath);
507
-
508
- if (!mstr_starts_with (filepath->view, dollar_origin)) {
509
- // Nothing to replace
510
- return true;
511
- }
512
- // Check that the next char is a path separator or end-of-string:
513
- char peek = filepath->data[dollar_origin.len];
514
- if (peek != 0 && !mpath_is_sep (peek, MPATH_NATIVE)) {
515
- // Not a single path element
516
- return true;
517
- }
518
- // Replace $ORIGIN with the directory of the current module
519
- const current_module_result self_exe_r = current_module_path ();
520
- if (self_exe_r.error) {
521
- // Failed to get the current module to load replace $ORIGIN
522
- mstr error = merror_system_error_string (self_exe_r.error);
523
- _mongocrypt_log (log,
524
- MONGOCRYPT_LOG_LEVEL_WARNING,
525
- "Error while loading the executable module path for "
526
- "substitution of $ORIGIN in CSFLE search path [%s]: %s",
527
- filepath->data,
528
- error.data);
529
- mstr_free (error);
530
- return false;
531
- }
532
- const mstr_view self_dir = mpath_parent (self_exe_r.path.view, MPATH_NATIVE);
533
- mstr_inplace_splice (filepath, 0, dollar_origin.len, self_dir);
534
- mstr_free (self_exe_r.path);
535
- return true;
433
+ static bool _try_replace_dollar_origin(mstr *filepath, _mongocrypt_log_t *log) {
434
+ const mstr_view dollar_origin = mstrv_lit("$ORIGIN");
435
+
436
+ BSON_ASSERT_PARAM(filepath);
437
+
438
+ if (!mstr_starts_with(filepath->view, dollar_origin)) {
439
+ // Nothing to replace
440
+ return true;
441
+ }
442
+ // Check that the next char is a path separator or end-of-string:
443
+ char peek = filepath->data[dollar_origin.len];
444
+ if (peek != 0 && !mpath_is_sep(peek, MPATH_NATIVE)) {
445
+ // Not a single path element
446
+ return true;
447
+ }
448
+ // Replace $ORIGIN with the directory of the current module
449
+ const current_module_result self_exe_r = current_module_path();
450
+ if (self_exe_r.error) {
451
+ // Failed to get the current module to load replace $ORIGIN
452
+ mstr error = merror_system_error_string(self_exe_r.error);
453
+ _mongocrypt_log(log,
454
+ MONGOCRYPT_LOG_LEVEL_WARNING,
455
+ "Error while loading the executable module path for "
456
+ "substitution of $ORIGIN in CSFLE search path [%s]: %s",
457
+ filepath->data,
458
+ error.data);
459
+ mstr_free(error);
460
+ return false;
461
+ }
462
+ const mstr_view self_dir = mpath_parent(self_exe_r.path.view, MPATH_NATIVE);
463
+ mstr_inplace_splice(filepath, 0, dollar_origin.len, self_dir);
464
+ mstr_free(self_exe_r.path);
465
+ return true;
536
466
  }
537
467
 
538
- static _loaded_csfle
539
- _try_find_csfle (mongocrypt_t *crypt)
540
- {
541
- _loaded_csfle candidate_csfle = {0};
542
- mstr csfle_cand_filepath = MSTR_NULL;
543
-
544
- BSON_ASSERT_PARAM (crypt);
545
-
546
- if (crypt->opts.crypt_shared_lib_override_path.data) {
547
- // If an override path was specified, skip the library searching behavior
548
- csfle_cand_filepath =
549
- mstr_copy (crypt->opts.crypt_shared_lib_override_path.view);
550
- if (_try_replace_dollar_origin (&csfle_cand_filepath, &crypt->log)) {
551
- // Succesfully substituted $ORIGIN
552
- // Do not allow a plain filename to go through, as that will cause the
553
- // DLL load to search the system.
554
- mstr_assign (&csfle_cand_filepath,
555
- mpath_absolute (csfle_cand_filepath.view, MPATH_NATIVE));
556
- candidate_csfle =
557
- _try_load_csfle (csfle_cand_filepath.data, &crypt->log);
558
- }
559
- } else {
560
- // No override path was specified, so try to find it on the provided
561
- // search paths.
562
- for (int i = 0; i < crypt->opts.n_crypt_shared_lib_search_paths; ++i) {
563
- mstr_view cand_dir = crypt->opts.crypt_shared_lib_search_paths[i].view;
564
- mstr_view csfle_filename = mstrv_lit ("mongo_crypt_v1" MCR_DLL_SUFFIX);
565
- if (mstr_eq (cand_dir, mstrv_lit ("$SYSTEM"))) {
566
- // Caller wants us to search for the library on the system's default
567
- // library paths. Pass only the library's filename to cause dll_open
568
- // to search on the library paths.
569
- mstr_assign (&csfle_cand_filepath, mstr_copy (csfle_filename));
570
- } else {
571
- // Compose the candidate filepath:
572
- mstr_assign (&csfle_cand_filepath,
573
- mpath_join (cand_dir, csfle_filename, MPATH_NATIVE));
574
- if (!_try_replace_dollar_origin (&csfle_cand_filepath,
575
- &crypt->log)) {
576
- // Error while substituting $ORIGIN
577
- continue;
468
+ static _loaded_csfle _try_find_csfle(mongocrypt_t *crypt) {
469
+ _loaded_csfle candidate_csfle = {0};
470
+ mstr csfle_cand_filepath = MSTR_NULL;
471
+
472
+ BSON_ASSERT_PARAM(crypt);
473
+
474
+ if (crypt->opts.crypt_shared_lib_override_path.data) {
475
+ // If an override path was specified, skip the library searching behavior
476
+ csfle_cand_filepath = mstr_copy(crypt->opts.crypt_shared_lib_override_path.view);
477
+ if (_try_replace_dollar_origin(&csfle_cand_filepath, &crypt->log)) {
478
+ // Succesfully substituted $ORIGIN
479
+ // Do not allow a plain filename to go through, as that will cause the
480
+ // DLL load to search the system.
481
+ mstr_assign(&csfle_cand_filepath, mpath_absolute(csfle_cand_filepath.view, MPATH_NATIVE));
482
+ candidate_csfle = _try_load_csfle(csfle_cand_filepath.data, &crypt->log, crypt->status);
483
+ }
484
+ } else {
485
+ // No override path was specified, so try to find it on the provided
486
+ // search paths.
487
+ for (int i = 0; i < crypt->opts.n_crypt_shared_lib_search_paths; ++i) {
488
+ mstr_view cand_dir = crypt->opts.crypt_shared_lib_search_paths[i].view;
489
+ mstr_view csfle_filename = mstrv_lit("mongo_crypt_v1" MCR_DLL_SUFFIX);
490
+ if (mstr_eq(cand_dir, mstrv_lit("$SYSTEM"))) {
491
+ // Caller wants us to search for the library on the system's default
492
+ // library paths. Pass only the library's filename to cause dll_open
493
+ // to search on the library paths.
494
+ mstr_assign(&csfle_cand_filepath, mstr_copy(csfle_filename));
495
+ } else {
496
+ // Compose the candidate filepath:
497
+ mstr_assign(&csfle_cand_filepath, mpath_join(cand_dir, csfle_filename, MPATH_NATIVE));
498
+ if (!_try_replace_dollar_origin(&csfle_cand_filepath, &crypt->log)) {
499
+ // Error while substituting $ORIGIN
500
+ continue;
501
+ }
578
502
  }
579
- }
580
- // Try to load the file:
581
- candidate_csfle =
582
- _try_load_csfle (csfle_cand_filepath.data, &crypt->log);
583
- if (candidate_csfle.okay) {
584
- // Stop searching:
585
- break;
586
- }
587
- }
588
- }
589
-
590
- mstr_free (csfle_cand_filepath);
591
- return candidate_csfle;
592
- }
503
+ // Try to load the file:
504
+ candidate_csfle = _try_load_csfle(csfle_cand_filepath.data, &crypt->log, NULL /* status */);
505
+ if (candidate_csfle.okay) {
506
+ // Stop searching:
507
+ break;
508
+ }
509
+ }
510
+ }
593
511
 
512
+ mstr_free(csfle_cand_filepath);
513
+ return candidate_csfle;
514
+ }
594
515
 
595
516
  /// Global state for the application's csfle library
596
517
  typedef struct csfle_global_lib_state {
597
- /// Synchronization around the reference count:
598
- mongocrypt_mutex_t mtx;
599
- int refcount;
600
- /// The open library handle:
601
- mcr_dll dll;
602
- /// vtable for the APIs:
603
- _mongo_crypt_v1_vtable vtable;
604
- /// The global library state managed by the csfle library:
605
- mongo_crypt_v1_lib *csfle_lib;
518
+ /// Synchronization around the reference count:
519
+ mongocrypt_mutex_t mtx;
520
+ int refcount;
521
+ /// The open library handle:
522
+ mcr_dll dll;
523
+ /// vtable for the APIs:
524
+ _mongo_crypt_v1_vtable vtable;
525
+ /// The global library state managed by the csfle library:
526
+ mongo_crypt_v1_lib *csfle_lib;
606
527
  } csfle_global_lib_state;
607
528
 
608
529
  csfle_global_lib_state g_csfle_state;
609
530
 
610
- static void
611
- init_csfle_state (void)
612
- {
613
- _mongocrypt_mutex_init (&g_csfle_state.mtx);
531
+ static void init_csfle_state(void) {
532
+ _mongocrypt_mutex_init(&g_csfle_state.mtx);
614
533
  }
534
+
615
535
  mlib_once_flag g_csfle_init_flag = MLIB_ONCE_INITIALIZER;
616
536
 
617
537
  /**
@@ -627,52 +547,48 @@ mlib_once_flag g_csfle_init_flag = MLIB_ONCE_INITIALIZER;
627
547
  * @note This function assumes that the global csfle state is valid and will not
628
548
  * be destroyed by any other thread. (One must hold the reference count >= 1)
629
549
  */
630
- static bool
631
- _validate_csfle_singleton (mongocrypt_t *crypt, _loaded_csfle found)
632
- {
633
- mongocrypt_status_t *status;
634
-
635
- BSON_ASSERT_PARAM (crypt);
636
-
637
- status = crypt->status;
638
-
639
- // Path to the existing loaded csfle:
640
- mcr_dll_path_result existing_path_ = mcr_dll_path (g_csfle_state.dll);
641
- assert (existing_path_.path.data &&
642
- "Failed to get path to already-loaded csfle library");
643
- mstr_view existing_path = existing_path_.path.view;
644
- bool okay = true;
645
- if (!found.okay) {
646
- // There is one loaded, but we failed to find that same library. Error:
647
- CLIENT_ERR ("An existing CSFLE library is loaded by the application at "
648
- "[%s], but the current call to mongocrypt_init() failed to "
649
- "find that same library.",
650
- existing_path.data);
651
- okay = false;
652
- } else {
653
- // Get the path to what we found:
654
- mcr_dll_path_result found_path = mcr_dll_path (found.lib);
655
- assert (found_path.path.data &&
656
- "Failed to get the dynamic library filepath of the library that "
657
- "was loaded for csfle");
658
- if (!mstr_eq (found_path.path.view, existing_path)) {
659
- // Our find-result should only ever find the existing same library.
660
- // Error:
661
- CLIENT_ERR (
662
- "An existing CSFLE library is loaded by the application at [%s], "
663
- "but the current call to mongocrypt_init() attempted to load a "
664
- "second CSFLE library from [%s]. This is not allowed.",
665
- existing_path.data,
666
- found_path.path.data);
667
- okay = false;
668
- }
669
- mstr_free (found_path.path);
670
- mstr_free (found_path.error_string);
671
- }
672
-
673
- mstr_free (existing_path_.path);
674
- mstr_free (existing_path_.error_string);
675
- return okay;
550
+ static bool _validate_csfle_singleton(mongocrypt_t *crypt, _loaded_csfle found) {
551
+ mongocrypt_status_t *status;
552
+
553
+ BSON_ASSERT_PARAM(crypt);
554
+
555
+ status = crypt->status;
556
+
557
+ // Path to the existing loaded csfle:
558
+ mcr_dll_path_result existing_path_ = mcr_dll_path(g_csfle_state.dll);
559
+ assert(existing_path_.path.data && "Failed to get path to already-loaded csfle library");
560
+ mstr_view existing_path = existing_path_.path.view;
561
+ bool okay = true;
562
+ if (!found.okay) {
563
+ // There is one loaded, but we failed to find that same library. Error:
564
+ CLIENT_ERR("An existing CSFLE library is loaded by the application at "
565
+ "[%s], but the current call to mongocrypt_init() failed to "
566
+ "find that same library.",
567
+ existing_path.data);
568
+ okay = false;
569
+ } else {
570
+ // Get the path to what we found:
571
+ mcr_dll_path_result found_path = mcr_dll_path(found.lib);
572
+ assert(found_path.path.data
573
+ && "Failed to get the dynamic library filepath of the library that "
574
+ "was loaded for csfle");
575
+ if (!mstr_eq(found_path.path.view, existing_path)) {
576
+ // Our find-result should only ever find the existing same library.
577
+ // Error:
578
+ CLIENT_ERR("An existing CSFLE library is loaded by the application at [%s], "
579
+ "but the current call to mongocrypt_init() attempted to load a "
580
+ "second CSFLE library from [%s]. This is not allowed.",
581
+ existing_path.data,
582
+ found_path.path.data);
583
+ okay = false;
584
+ }
585
+ mstr_free(found_path.path);
586
+ mstr_free(found_path.error_string);
587
+ }
588
+
589
+ mstr_free(existing_path_.path);
590
+ mstr_free(existing_path_.error_string);
591
+ return okay;
676
592
  }
677
593
 
678
594
  /**
@@ -681,44 +597,35 @@ _validate_csfle_singleton (mongocrypt_t *crypt, _loaded_csfle found)
681
597
  * This should be called as part of mongocrypt_t destruction following a
682
598
  * successful loading of csfle.
683
599
  */
684
- static void
685
- _csfle_drop_global_ref (void)
686
- {
687
- mlib_call_once (&g_csfle_init_flag, init_csfle_state);
688
-
689
- bool dropped_last_ref = false;
690
- csfle_global_lib_state old_state = {.refcount = 0};
691
- MONGOCRYPT_WITH_MUTEX (g_csfle_state.mtx)
692
- {
693
- assert (g_csfle_state.refcount > 0);
694
- int new_rc = --g_csfle_state.refcount;
695
- if (new_rc == 0) {
696
- old_state = g_csfle_state;
697
- dropped_last_ref = true;
698
- }
699
- }
700
-
701
- if (dropped_last_ref) {
702
- mongo_crypt_v1_status *status = old_state.vtable.status_create ();
703
- const int destroy_rc =
704
- old_state.vtable.lib_destroy (old_state.csfle_lib, status);
705
- if (destroy_rc != MONGO_CRYPT_V1_SUCCESS && status) {
706
- fprintf (stderr,
707
- "csfle lib_destroy() failed: %s [Error %d, code %d]\n",
708
- old_state.vtable.status_get_explanation (status),
709
- old_state.vtable.status_get_error (status),
710
- old_state.vtable.status_get_code (status));
711
- }
712
- old_state.vtable.status_destroy (status);
713
-
600
+ static void _csfle_drop_global_ref(void) {
601
+ mlib_call_once(&g_csfle_init_flag, init_csfle_state);
602
+
603
+ MONGOCRYPT_WITH_MUTEX(g_csfle_state.mtx) {
604
+ assert(g_csfle_state.refcount > 0);
605
+ int new_rc = --g_csfle_state.refcount;
606
+ if (new_rc == 0) {
607
+ mongo_crypt_v1_status *status = g_csfle_state.vtable.status_create();
608
+ const int destroy_rc = g_csfle_state.vtable.lib_destroy(g_csfle_state.csfle_lib, status);
609
+ if (destroy_rc != MONGO_CRYPT_V1_SUCCESS && status) {
610
+ fprintf(stderr,
611
+ "csfle lib_destroy() failed: %s [Error %d, code %d]\n",
612
+ g_csfle_state.vtable.status_get_explanation(status),
613
+ g_csfle_state.vtable.status_get_error(status),
614
+ g_csfle_state.vtable.status_get_code(status));
615
+ }
616
+ g_csfle_state.vtable.status_destroy(status);
714
617
  #ifndef __linux__
715
- mcr_dll_close (old_state.dll);
618
+ mcr_dll_close(g_csfle_state.dll);
619
+ #else
620
+ /// NOTE: On Linux, skip closing the CSFLE library itself, since a bug in
621
+ /// the way ld-linux and GCC interact causes static destructors to not run
622
+ /// during dlclose(). Still, free the error string:
623
+ ///
624
+ /// Please see: https://jira.mongodb.org/browse/SERVER-63710
625
+ mstr_free(g_csfle_state.dll.error_string);
716
626
  #endif
717
- /// NOTE: On Linux, skip closing the CSFLE library itself, since a bug in
718
- /// the way ld-linux and GCC interact causes static destructors to not run
719
- /// during dlclose(). Still, free the error string:
720
- mstr_free (old_state.dll.error_string);
721
- }
627
+ }
628
+ }
722
629
  }
723
630
 
724
631
  /**
@@ -755,132 +662,122 @@ _csfle_drop_global_ref (void)
755
662
  * the library referenced by `found`. The caller should release `found->lib`
756
663
  * regardless.
757
664
  */
758
- static bool
759
- _csfle_replace_or_take_validate_singleton (mongocrypt_t *crypt,
760
- _loaded_csfle *found)
761
- {
762
- mlib_call_once (&g_csfle_init_flag, init_csfle_state);
763
-
764
- // If we have a loaded library, create a csfle_status object to use with
765
- // lib_create
766
- mongo_crypt_v1_status *csfle_status = NULL;
767
-
768
- BSON_ASSERT_PARAM (crypt);
769
- BSON_ASSERT_PARAM (found);
770
-
771
- if (found->okay) {
772
- // Create the status. Note that this may fail, so do not assume
773
- // csfle_status is non-null.
774
- csfle_status = found->vtable.status_create ();
775
- }
776
-
777
- /**
778
- * Atomically:
779
- *
780
- * 1. If there is an existing global library, increment its reference count.
781
- * 2. Otherwise, if we have successfully loaded a new csfle, replace the
782
- * global library and set its reference count to 1.
783
- * 3. Otherwise, do nothing.
784
- */
785
- enum {
786
- TOOK_REFERENCE,
787
- DID_NOTHING,
788
- REPLACED_GLOBAL,
789
- LIB_CREATE_FAILED,
790
- } action;
791
- MONGOCRYPT_WITH_MUTEX (g_csfle_state.mtx)
792
- {
793
- if (g_csfle_state.refcount) {
794
- // Increment the refcount to prevent the global csfle library from
795
- // disappearing
796
- ++g_csfle_state.refcount;
797
- action = TOOK_REFERENCE;
798
- } else if (found->okay) {
799
- // We have found csfle, and no one else is holding one. Our result will
800
- // now become the global result.
801
- // Create the single csfle_lib object for the application:
802
- mongo_crypt_v1_lib *csfle_lib =
803
- found->vtable.lib_create (csfle_status);
804
- if (csfle_lib == NULL) {
805
- // Creation failed:
806
- action = LIB_CREATE_FAILED;
807
- } else {
808
- // Creation succeeded: Store the result:
809
- g_csfle_state.dll = found->lib;
810
- g_csfle_state.vtable = found->vtable;
811
- g_csfle_state.csfle_lib = csfle_lib;
812
- g_csfle_state.refcount = 1;
813
- action = REPLACED_GLOBAL;
814
- }
815
- } else {
816
- // We failed to load the library, and no one else has one either.
817
- // Nothing to do.
818
- action = DID_NOTHING;
819
- }
820
- }
821
-
822
- // Get the possible failure status information.
823
- mstr message = MSTR_NULL;
824
- int err = 0;
825
- int code = 0;
826
- if (csfle_status) {
827
- assert (found->okay);
828
- message =
829
- mstr_copy_cstr (found->vtable.status_get_explanation (csfle_status));
830
- err = found->vtable.status_get_error (csfle_status);
831
- code = found->vtable.status_get_code (csfle_status);
832
- found->vtable.status_destroy (csfle_status);
833
- }
834
-
835
- bool have_csfle = true;
836
- switch (action) {
837
- case TOOK_REFERENCE: {
838
- const bool is_valid = _validate_csfle_singleton (crypt, *found);
839
- if (!is_valid) {
840
- // We've failed validation, so we're not going to continue to
841
- // reference the global instance it. Drop it now:
842
- _csfle_drop_global_ref ();
843
- }
844
- have_csfle = is_valid;
845
- break;
846
- }
847
- case REPLACED_GLOBAL:
848
- // Reset the library in the caller so they can't unload the DLL. The DLL
849
- // is now managed in the global variable.
850
- found->lib = MCR_DLL_NULL;
851
- _mongocrypt_log (&crypt->log,
852
- MONGOCRYPT_LOG_LEVEL_TRACE,
853
- "Loading new csfle library for the application.");
854
- have_csfle = true;
855
- break;
856
- case LIB_CREATE_FAILED:
857
- if (!message.data) {
858
- // We failed to obtain a message about the failure
859
- _mongocrypt_set_error (crypt->status,
860
- MONGOCRYPT_STATUS_ERROR_CRYPT_SHARED,
861
- MONGOCRYPT_GENERIC_ERROR_CODE,
862
- "csfle lib_create() failed");
863
- } else {
864
- // Record the message, error, and code from csfle about the failure
865
- _mongocrypt_set_error (
866
- crypt->status,
867
- MONGOCRYPT_STATUS_ERROR_CRYPT_SHARED,
868
- MONGOCRYPT_GENERIC_ERROR_CODE,
869
- "csfle lib_create() failed: %s [Error %d, code %d]",
870
- message.data,
871
- err,
872
- code);
873
- }
874
- have_csfle = false;
875
- break;
876
- case DID_NOTHING:
877
- default:
878
- have_csfle = false;
879
- break;
880
- }
881
-
882
- mstr_free (message);
883
- return have_csfle;
665
+ static bool _csfle_replace_or_take_validate_singleton(mongocrypt_t *crypt, _loaded_csfle *found) {
666
+ mlib_call_once(&g_csfle_init_flag, init_csfle_state);
667
+
668
+ // If we have a loaded library, create a csfle_status object to use with
669
+ // lib_create
670
+ mongo_crypt_v1_status *csfle_status = NULL;
671
+
672
+ BSON_ASSERT_PARAM(crypt);
673
+ BSON_ASSERT_PARAM(found);
674
+
675
+ if (found->okay) {
676
+ // Create the status. Note that this may fail, so do not assume
677
+ // csfle_status is non-null.
678
+ csfle_status = found->vtable.status_create();
679
+ }
680
+
681
+ /**
682
+ * Atomically:
683
+ *
684
+ * 1. If there is an existing global library, increment its reference count.
685
+ * 2. Otherwise, if we have successfully loaded a new csfle, replace the
686
+ * global library and set its reference count to 1.
687
+ * 3. Otherwise, do nothing.
688
+ */
689
+ enum {
690
+ TOOK_REFERENCE,
691
+ DID_NOTHING,
692
+ REPLACED_GLOBAL,
693
+ LIB_CREATE_FAILED,
694
+ } action;
695
+
696
+ MONGOCRYPT_WITH_MUTEX(g_csfle_state.mtx) {
697
+ if (g_csfle_state.refcount) {
698
+ // Increment the refcount to prevent the global csfle library from
699
+ // disappearing
700
+ ++g_csfle_state.refcount;
701
+ action = TOOK_REFERENCE;
702
+ } else if (found->okay) {
703
+ // We have found csfle, and no one else is holding one. Our result will
704
+ // now become the global result.
705
+ // Create the single csfle_lib object for the application:
706
+ mongo_crypt_v1_lib *csfle_lib = found->vtable.lib_create(csfle_status);
707
+ if (csfle_lib == NULL) {
708
+ // Creation failed:
709
+ action = LIB_CREATE_FAILED;
710
+ } else {
711
+ // Creation succeeded: Store the result:
712
+ g_csfle_state.dll = found->lib;
713
+ g_csfle_state.vtable = found->vtable;
714
+ g_csfle_state.csfle_lib = csfle_lib;
715
+ g_csfle_state.refcount = 1;
716
+ action = REPLACED_GLOBAL;
717
+ }
718
+ } else {
719
+ // We failed to load the library, and no one else has one either.
720
+ // Nothing to do.
721
+ action = DID_NOTHING;
722
+ }
723
+ }
724
+
725
+ // Get the possible failure status information.
726
+ mstr message = MSTR_NULL;
727
+ int err = 0;
728
+ int code = 0;
729
+ if (csfle_status) {
730
+ assert(found->okay);
731
+ message = mstr_copy_cstr(found->vtable.status_get_explanation(csfle_status));
732
+ err = found->vtable.status_get_error(csfle_status);
733
+ code = found->vtable.status_get_code(csfle_status);
734
+ found->vtable.status_destroy(csfle_status);
735
+ }
736
+
737
+ bool have_csfle = true;
738
+ switch (action) {
739
+ case TOOK_REFERENCE: {
740
+ const bool is_valid = _validate_csfle_singleton(crypt, *found);
741
+ if (!is_valid) {
742
+ // We've failed validation, so we're not going to continue to
743
+ // reference the global instance it. Drop it now:
744
+ _csfle_drop_global_ref();
745
+ }
746
+ have_csfle = is_valid;
747
+ break;
748
+ }
749
+ case REPLACED_GLOBAL:
750
+ // Reset the library in the caller so they can't unload the DLL. The DLL
751
+ // is now managed in the global variable.
752
+ found->lib = MCR_DLL_NULL;
753
+ _mongocrypt_log(&crypt->log, MONGOCRYPT_LOG_LEVEL_TRACE, "Loading new csfle library for the application.");
754
+ have_csfle = true;
755
+ break;
756
+ case LIB_CREATE_FAILED:
757
+ if (!message.data) {
758
+ // We failed to obtain a message about the failure
759
+ _mongocrypt_set_error(crypt->status,
760
+ MONGOCRYPT_STATUS_ERROR_CRYPT_SHARED,
761
+ MONGOCRYPT_GENERIC_ERROR_CODE,
762
+ "csfle lib_create() failed");
763
+ } else {
764
+ // Record the message, error, and code from csfle about the failure
765
+ _mongocrypt_set_error(crypt->status,
766
+ MONGOCRYPT_STATUS_ERROR_CRYPT_SHARED,
767
+ MONGOCRYPT_GENERIC_ERROR_CODE,
768
+ "csfle lib_create() failed: %s [Error %d, code %d]",
769
+ message.data,
770
+ err,
771
+ code);
772
+ }
773
+ have_csfle = false;
774
+ break;
775
+ case DID_NOTHING:
776
+ default: have_csfle = false; break;
777
+ }
778
+
779
+ mstr_free(message);
780
+ return have_csfle;
884
781
  }
885
782
 
886
783
  /**
@@ -890,16 +787,13 @@ _csfle_replace_or_take_validate_singleton (mongocrypt_t *crypt,
890
787
  * @note "Requesting csfle" means that it has set at least one search path OR
891
788
  * has set the override path
892
789
  */
893
- static bool
894
- _wants_csfle (mongocrypt_t *c)
895
- {
896
- BSON_ASSERT_PARAM (c);
897
-
898
- if (c->opts.bypass_query_analysis) {
899
- return false;
900
- }
901
- return c->opts.n_crypt_shared_lib_search_paths != 0 ||
902
- c->opts.crypt_shared_lib_override_path.data != NULL;
790
+ static bool _wants_csfle(mongocrypt_t *c) {
791
+ BSON_ASSERT_PARAM(c);
792
+
793
+ if (c->opts.bypass_query_analysis) {
794
+ return false;
795
+ }
796
+ return c->opts.n_crypt_shared_lib_search_paths != 0 || c->opts.crypt_shared_lib_override_path.data != NULL;
903
797
  }
904
798
 
905
799
  /**
@@ -913,693 +807,571 @@ _wants_csfle (mongocrypt_t *c)
913
807
  * requested paths. `false` is only for hard-errors, which includes failure to
914
808
  * load from the override path.
915
809
  */
916
- static bool
917
- _try_enable_csfle (mongocrypt_t *crypt)
918
- {
919
- mongocrypt_status_t *status;
920
- _loaded_csfle found;
921
-
922
- BSON_ASSERT_PARAM (crypt);
923
-
924
- found = _try_find_csfle (crypt);
925
-
926
- status = crypt->status;
927
-
928
- // If a crypt_shared override path was specified, but we did not succeed in
929
- // loading crypt_shared, that is a hard-error.
930
- if (crypt->opts.crypt_shared_lib_override_path.data && !found.okay) {
931
- CLIENT_ERR (
932
- "A crypt_shared override path was specified [%s], but we failed to "
933
- "open a dynamic library at that location",
934
- crypt->opts.crypt_shared_lib_override_path.data);
935
- return false;
936
- }
937
-
938
- // Attempt to validate the try-find result against the global state:
939
- const bool got_csfle =
940
- _csfle_replace_or_take_validate_singleton (crypt, &found);
941
- // Close the lib we found (may have been stolen in validate_singleton())
942
- mcr_dll_close (found.lib);
943
-
944
- if (got_csfle) {
945
- crypt->csfle = g_csfle_state.vtable;
946
- crypt->csfle_lib = g_csfle_state.csfle_lib;
947
- }
948
- // In cast of failure, validate_singleton() will set a non-ok status.
949
- return mongocrypt_status_type (status) == MONGOCRYPT_STATUS_OK;
810
+ static bool _try_enable_csfle(mongocrypt_t *crypt) {
811
+ mongocrypt_status_t *status;
812
+ _loaded_csfle found;
813
+
814
+ BSON_ASSERT_PARAM(crypt);
815
+
816
+ found = _try_find_csfle(crypt);
817
+
818
+ status = crypt->status;
819
+
820
+ // If a crypt_shared override path was specified, but we did not succeed in
821
+ // loading crypt_shared, that is a hard-error.
822
+ if (crypt->opts.crypt_shared_lib_override_path.data && !found.okay) {
823
+ // Wrap error with additional information.
824
+ CLIENT_ERR("A crypt_shared override path was specified [%s], but we failed to open a dynamic "
825
+ "library at that location. Load error: [%s]",
826
+ crypt->opts.crypt_shared_lib_override_path.data,
827
+ mongocrypt_status_message(crypt->status, NULL /* len */));
828
+ return false;
829
+ }
830
+
831
+ // Attempt to validate the try-find result against the global state:
832
+ const bool got_csfle = _csfle_replace_or_take_validate_singleton(crypt, &found);
833
+ // Close the lib we found (may have been stolen in validate_singleton())
834
+ mcr_dll_close(found.lib);
835
+
836
+ if (got_csfle) {
837
+ crypt->csfle = g_csfle_state.vtable;
838
+ crypt->csfle_lib = g_csfle_state.csfle_lib;
839
+ }
840
+ // In cast of failure, validate_singleton() will set a non-ok status.
841
+ return mongocrypt_status_type(status) == MONGOCRYPT_STATUS_OK;
950
842
  }
951
843
 
952
- bool
953
- mongocrypt_init (mongocrypt_t *crypt)
954
- {
955
- BSON_ASSERT_PARAM (crypt);
844
+ bool mongocrypt_init(mongocrypt_t *crypt) {
845
+ BSON_ASSERT_PARAM(crypt);
956
846
 
957
- mongocrypt_status_t *status = crypt->status;
958
- if (crypt->initialized) {
959
- CLIENT_ERR ("already initialized");
960
- return false;
961
- }
847
+ mongocrypt_status_t *status = crypt->status;
848
+ if (crypt->initialized) {
849
+ CLIENT_ERR("already initialized");
850
+ return false;
851
+ }
962
852
 
963
- crypt->initialized = true;
853
+ crypt->initialized = true;
964
854
 
965
- if (!mongocrypt_status_ok (crypt->status)) {
966
- return false;
967
- }
855
+ if (!mongocrypt_status_ok(crypt->status)) {
856
+ return false;
857
+ }
968
858
 
969
- if (!_mongocrypt_opts_validate (&crypt->opts, status)) {
970
- return false;
971
- }
859
+ if (!_mongocrypt_opts_validate(&crypt->opts, status)) {
860
+ return false;
861
+ }
972
862
 
973
- if (crypt->opts.log_fn) {
974
- _mongocrypt_log_set_fn (
975
- &crypt->log, crypt->opts.log_fn, crypt->opts.log_ctx);
976
- }
863
+ if (crypt->opts.log_fn) {
864
+ _mongocrypt_log_set_fn(&crypt->log, crypt->opts.log_fn, crypt->opts.log_ctx);
865
+ }
977
866
 
978
- if (!crypt->crypto) {
867
+ if (!crypt->crypto) {
979
868
  #ifndef MONGOCRYPT_ENABLE_CRYPTO
980
- CLIENT_ERR ("libmongocrypt built with native crypto disabled. crypto "
981
- "hooks required");
982
- return false;
869
+ CLIENT_ERR("libmongocrypt built with native crypto disabled. crypto "
870
+ "hooks required");
871
+ return false;
983
872
  #else
984
- /* set default hooks. */
985
- crypt->crypto = bson_malloc0 (sizeof (*crypt->crypto));
986
- BSON_ASSERT (crypt->crypto);
873
+ /* set default hooks. */
874
+ crypt->crypto = bson_malloc0(sizeof(*crypt->crypto));
875
+ BSON_ASSERT(crypt->crypto);
987
876
  #endif
988
- }
877
+ }
989
878
 
990
- if (!_wants_csfle (crypt)) {
991
- // User does not want csfle. Just succeed.
992
- return true;
993
- }
879
+ if (!_wants_csfle(crypt)) {
880
+ // User does not want csfle. Just succeed.
881
+ return true;
882
+ }
994
883
 
995
- return _try_enable_csfle (crypt);
884
+ return _try_enable_csfle(crypt);
996
885
  }
997
886
 
998
-
999
- bool
1000
- mongocrypt_status (mongocrypt_t *crypt, mongocrypt_status_t *out)
1001
- {
1002
- BSON_ASSERT_PARAM (crypt);
1003
-
1004
- if (!out) {
1005
- mongocrypt_status_t *status = crypt->status;
1006
- CLIENT_ERR ("argument 'out' is required");
1007
- return false;
1008
- }
1009
-
1010
- if (!mongocrypt_status_ok (crypt->status)) {
1011
- _mongocrypt_status_copy_to (crypt->status, out);
1012
- return false;
1013
- }
1014
- _mongocrypt_status_reset (out);
1015
- return true;
887
+ bool mongocrypt_status(mongocrypt_t *crypt, mongocrypt_status_t *out) {
888
+ BSON_ASSERT_PARAM(crypt);
889
+
890
+ if (!out) {
891
+ mongocrypt_status_t *status = crypt->status;
892
+ CLIENT_ERR("argument 'out' is required");
893
+ return false;
894
+ }
895
+
896
+ if (!mongocrypt_status_ok(crypt->status)) {
897
+ _mongocrypt_status_copy_to(crypt->status, out);
898
+ return false;
899
+ }
900
+ _mongocrypt_status_reset(out);
901
+ return true;
1016
902
  }
1017
903
 
1018
-
1019
- void
1020
- mongocrypt_destroy (mongocrypt_t *crypt)
1021
- {
1022
- if (!crypt) {
1023
- return;
1024
- }
1025
- _mongocrypt_opts_cleanup (&crypt->opts);
1026
- _mongocrypt_cache_cleanup (&crypt->cache_collinfo);
1027
- _mongocrypt_cache_cleanup (&crypt->cache_key);
1028
- _mongocrypt_mutex_cleanup (&crypt->mutex);
1029
- _mongocrypt_log_cleanup (&crypt->log);
1030
- mongocrypt_status_destroy (crypt->status);
1031
- bson_free (crypt->crypto);
1032
- _mongocrypt_cache_oauth_destroy (crypt->cache_oauth_azure);
1033
- _mongocrypt_cache_oauth_destroy (crypt->cache_oauth_gcp);
1034
-
1035
- if (crypt->csfle.okay) {
1036
- _csfle_drop_global_ref ();
1037
- crypt->csfle.okay = false;
1038
- }
1039
-
1040
-
1041
- bson_free (crypt);
904
+ void mongocrypt_destroy(mongocrypt_t *crypt) {
905
+ if (!crypt) {
906
+ return;
907
+ }
908
+ _mongocrypt_opts_cleanup(&crypt->opts);
909
+ _mongocrypt_cache_cleanup(&crypt->cache_collinfo);
910
+ _mongocrypt_cache_cleanup(&crypt->cache_key);
911
+ _mongocrypt_mutex_cleanup(&crypt->mutex);
912
+ _mongocrypt_log_cleanup(&crypt->log);
913
+ mongocrypt_status_destroy(crypt->status);
914
+ bson_free(crypt->crypto);
915
+ _mongocrypt_cache_oauth_destroy(crypt->cache_oauth_azure);
916
+ _mongocrypt_cache_oauth_destroy(crypt->cache_oauth_gcp);
917
+
918
+ if (crypt->csfle.okay) {
919
+ _csfle_drop_global_ref();
920
+ crypt->csfle.okay = false;
921
+ }
922
+
923
+ bson_free(crypt);
1042
924
  }
1043
925
 
1044
-
1045
- const char *
1046
- mongocrypt_crypt_shared_lib_version_string (const mongocrypt_t *crypt,
1047
- uint32_t *len)
1048
- {
1049
- BSON_ASSERT_PARAM (crypt);
1050
-
1051
- if (!crypt->csfle.okay) {
1052
- if (len) {
1053
- *len = 0;
1054
- }
1055
- return NULL;
1056
- }
1057
- const char *version = crypt->csfle.get_version_str ();
1058
- if (len) {
1059
- *len = (uint32_t) (strlen (version));
1060
- }
1061
- return version;
926
+ const char *mongocrypt_crypt_shared_lib_version_string(const mongocrypt_t *crypt, uint32_t *len) {
927
+ BSON_ASSERT_PARAM(crypt);
928
+
929
+ if (!crypt->csfle.okay) {
930
+ if (len) {
931
+ *len = 0;
932
+ }
933
+ return NULL;
934
+ }
935
+ const char *version = crypt->csfle.get_version_str();
936
+ if (len) {
937
+ *len = (uint32_t)(strlen(version));
938
+ }
939
+ return version;
1062
940
  }
1063
941
 
1064
- uint64_t
1065
- mongocrypt_crypt_shared_lib_version (const mongocrypt_t *crypt)
1066
- {
1067
- BSON_ASSERT_PARAM (crypt);
942
+ uint64_t mongocrypt_crypt_shared_lib_version(const mongocrypt_t *crypt) {
943
+ BSON_ASSERT_PARAM(crypt);
1068
944
 
1069
- if (!crypt->csfle.okay) {
1070
- return 0;
1071
- }
1072
- return crypt->csfle.get_version ();
945
+ if (!crypt->csfle.okay) {
946
+ return 0;
947
+ }
948
+ return crypt->csfle.get_version();
1073
949
  }
1074
950
 
951
+ bool _mongocrypt_validate_and_copy_string(const char *in, int32_t in_len, char **out) {
952
+ BSON_ASSERT_PARAM(out);
1075
953
 
1076
- bool
1077
- _mongocrypt_validate_and_copy_string (const char *in,
1078
- int32_t in_len,
1079
- char **out)
1080
- {
1081
- BSON_ASSERT_PARAM (out);
954
+ if (!in || in_len < -1) {
955
+ return false;
956
+ }
1082
957
 
1083
- if (!in || in_len < -1) {
1084
- return false;
1085
- }
958
+ const size_t len = in_len < 0 ? strlen(in) : (size_t)in_len;
1086
959
 
1087
- const size_t len = in_len < 0 ? strlen (in) : (size_t) in_len;
960
+ if (!bson_utf8_validate(in, len, false)) {
961
+ return false;
962
+ }
963
+ *out = bson_strndup(in, len);
964
+ return true;
965
+ }
1088
966
 
1089
- if (!bson_utf8_validate (in, len, false)) {
1090
- return false;
1091
- }
1092
- *out = bson_strndup (in, len);
1093
- return true;
967
+ bool mongocrypt_setopt_crypto_hooks(mongocrypt_t *crypt,
968
+ mongocrypt_crypto_fn aes_256_cbc_encrypt,
969
+ mongocrypt_crypto_fn aes_256_cbc_decrypt,
970
+ mongocrypt_random_fn random,
971
+ mongocrypt_hmac_fn hmac_sha_512,
972
+ mongocrypt_hmac_fn hmac_sha_256,
973
+ mongocrypt_hash_fn sha_256,
974
+ void *ctx) {
975
+ ASSERT_MONGOCRYPT_PARAM_UNINIT(crypt);
976
+
977
+ mongocrypt_status_t *status = crypt->status;
978
+
979
+ if (!crypt->crypto) {
980
+ crypt->crypto = bson_malloc0(sizeof(*crypt->crypto));
981
+ BSON_ASSERT(crypt->crypto);
982
+ }
983
+
984
+ crypt->crypto->hooks_enabled = true;
985
+ crypt->crypto->ctx = ctx;
986
+
987
+ if (!aes_256_cbc_encrypt) {
988
+ CLIENT_ERR("aes_256_cbc_encrypt not set");
989
+ return false;
990
+ }
991
+ crypt->crypto->aes_256_cbc_encrypt = aes_256_cbc_encrypt;
992
+
993
+ if (!aes_256_cbc_decrypt) {
994
+ CLIENT_ERR("aes_256_cbc_decrypt not set");
995
+ return false;
996
+ }
997
+ crypt->crypto->aes_256_cbc_decrypt = aes_256_cbc_decrypt;
998
+
999
+ if (!random) {
1000
+ CLIENT_ERR("random not set");
1001
+ return false;
1002
+ }
1003
+ crypt->crypto->random = random;
1004
+
1005
+ if (!hmac_sha_512) {
1006
+ CLIENT_ERR("hmac_sha_512 not set");
1007
+ return false;
1008
+ }
1009
+ crypt->crypto->hmac_sha_512 = hmac_sha_512;
1010
+
1011
+ if (!hmac_sha_256) {
1012
+ CLIENT_ERR("hmac_sha_256 not set");
1013
+ return false;
1014
+ }
1015
+ crypt->crypto->hmac_sha_256 = hmac_sha_256;
1016
+
1017
+ if (!sha_256) {
1018
+ CLIENT_ERR("sha_256 not set");
1019
+ return false;
1020
+ }
1021
+ crypt->crypto->sha_256 = sha_256;
1022
+
1023
+ return true;
1094
1024
  }
1095
1025
 
1026
+ bool mongocrypt_setopt_crypto_hook_sign_rsaes_pkcs1_v1_5(mongocrypt_t *crypt,
1027
+ mongocrypt_hmac_fn sign_rsaes_pkcs1_v1_5,
1028
+ void *sign_ctx) {
1029
+ ASSERT_MONGOCRYPT_PARAM_UNINIT(crypt);
1096
1030
 
1097
- bool
1098
- mongocrypt_setopt_crypto_hooks (mongocrypt_t *crypt,
1099
- mongocrypt_crypto_fn aes_256_cbc_encrypt,
1100
- mongocrypt_crypto_fn aes_256_cbc_decrypt,
1101
- mongocrypt_random_fn random,
1102
- mongocrypt_hmac_fn hmac_sha_512,
1103
- mongocrypt_hmac_fn hmac_sha_256,
1104
- mongocrypt_hash_fn sha_256,
1105
- void *ctx)
1106
- {
1107
- BSON_ASSERT_PARAM (crypt);
1108
-
1109
- mongocrypt_status_t *status = crypt->status;
1110
-
1111
- if (crypt->initialized) {
1112
- CLIENT_ERR ("options cannot be set after initialization");
1113
- return false;
1114
- }
1115
-
1116
- if (!crypt->crypto) {
1117
- crypt->crypto = bson_malloc0 (sizeof (*crypt->crypto));
1118
- BSON_ASSERT (crypt->crypto);
1119
- }
1120
-
1121
- crypt->crypto->hooks_enabled = true;
1122
- crypt->crypto->ctx = ctx;
1123
-
1124
- if (!aes_256_cbc_encrypt) {
1125
- CLIENT_ERR ("aes_256_cbc_encrypt not set");
1126
- return false;
1127
- }
1128
- crypt->crypto->aes_256_cbc_encrypt = aes_256_cbc_encrypt;
1129
-
1130
- if (!aes_256_cbc_decrypt) {
1131
- CLIENT_ERR ("aes_256_cbc_decrypt not set");
1132
- return false;
1133
- }
1134
- crypt->crypto->aes_256_cbc_decrypt = aes_256_cbc_decrypt;
1135
-
1136
- if (!random) {
1137
- CLIENT_ERR ("random not set");
1138
- return false;
1139
- }
1140
- crypt->crypto->random = random;
1141
-
1142
- if (!hmac_sha_512) {
1143
- CLIENT_ERR ("hmac_sha_512 not set");
1144
- return false;
1145
- }
1146
- crypt->crypto->hmac_sha_512 = hmac_sha_512;
1147
-
1148
- if (!hmac_sha_256) {
1149
- CLIENT_ERR ("hmac_sha_256 not set");
1150
- return false;
1151
- }
1152
- crypt->crypto->hmac_sha_256 = hmac_sha_256;
1153
-
1154
- if (!sha_256) {
1155
- CLIENT_ERR ("sha_256 not set");
1156
- return false;
1157
- }
1158
- crypt->crypto->sha_256 = sha_256;
1159
-
1160
- return true;
1161
- }
1031
+ if (crypt->opts.sign_rsaes_pkcs1_v1_5) {
1032
+ mongocrypt_status_t *status = crypt->status;
1033
+ CLIENT_ERR("signature hook already set");
1034
+ return false;
1035
+ }
1162
1036
 
1163
- bool
1164
- mongocrypt_setopt_crypto_hook_sign_rsaes_pkcs1_v1_5 (
1165
- mongocrypt_t *crypt,
1166
- mongocrypt_hmac_fn sign_rsaes_pkcs1_v1_5,
1167
- void *sign_ctx)
1168
- {
1169
- BSON_ASSERT_PARAM (crypt);
1170
-
1171
- mongocrypt_status_t *status = crypt->status;
1172
-
1173
- if (crypt->initialized) {
1174
- CLIENT_ERR ("options cannot be set after initialization");
1175
- return false;
1176
- }
1177
-
1178
- if (crypt->opts.sign_rsaes_pkcs1_v1_5) {
1179
- CLIENT_ERR ("signature hook already set");
1180
- return false;
1181
- }
1182
-
1183
- crypt->opts.sign_rsaes_pkcs1_v1_5 = sign_rsaes_pkcs1_v1_5;
1184
- crypt->opts.sign_ctx = sign_ctx;
1185
- return true;
1037
+ crypt->opts.sign_rsaes_pkcs1_v1_5 = sign_rsaes_pkcs1_v1_5;
1038
+ crypt->opts.sign_ctx = sign_ctx;
1039
+ return true;
1186
1040
  }
1187
1041
 
1188
- bool
1189
- mongocrypt_setopt_aes_256_ctr (mongocrypt_t *crypt,
1190
- mongocrypt_crypto_fn aes_256_ctr_encrypt,
1191
- mongocrypt_crypto_fn aes_256_ctr_decrypt,
1192
- void *ctx)
1193
- {
1194
- BSON_ASSERT_PARAM (crypt);
1042
+ bool mongocrypt_setopt_aes_256_ctr(mongocrypt_t *crypt,
1043
+ mongocrypt_crypto_fn aes_256_ctr_encrypt,
1044
+ mongocrypt_crypto_fn aes_256_ctr_decrypt,
1045
+ void *ctx) {
1046
+ ASSERT_MONGOCRYPT_PARAM_UNINIT(crypt);
1195
1047
 
1196
- mongocrypt_status_t *status = crypt->status;
1048
+ mongocrypt_status_t *status = crypt->status;
1197
1049
 
1198
- if (!crypt->crypto) {
1199
- crypt->crypto = bson_malloc0 (sizeof (*crypt->crypto));
1200
- BSON_ASSERT (crypt->crypto);
1201
- }
1050
+ if (!crypt->crypto) {
1051
+ crypt->crypto = bson_malloc0(sizeof(*crypt->crypto));
1052
+ BSON_ASSERT(crypt->crypto);
1053
+ }
1202
1054
 
1203
- if (crypt->initialized) {
1204
- CLIENT_ERR ("options cannot be set after initialization");
1205
- return false;
1206
- }
1055
+ if (!aes_256_ctr_encrypt) {
1056
+ CLIENT_ERR("aes_256_ctr_encrypt not set");
1057
+ return false;
1058
+ }
1207
1059
 
1208
- if (!aes_256_ctr_encrypt) {
1209
- CLIENT_ERR ("aes_256_ctr_encrypt not set");
1210
- return false;
1211
- }
1060
+ if (!aes_256_ctr_decrypt) {
1061
+ CLIENT_ERR("aes_256_ctr_decrypt not set");
1062
+ return false;
1063
+ }
1212
1064
 
1213
- if (!aes_256_ctr_decrypt) {
1214
- CLIENT_ERR ("aes_256_ctr_decrypt not set");
1215
- return false;
1216
- }
1065
+ crypt->crypto->aes_256_ctr_encrypt = aes_256_ctr_encrypt;
1066
+ crypt->crypto->aes_256_ctr_decrypt = aes_256_ctr_decrypt;
1217
1067
 
1218
- crypt->crypto->aes_256_ctr_encrypt = aes_256_ctr_encrypt;
1219
- crypt->crypto->aes_256_ctr_decrypt = aes_256_ctr_decrypt;
1220
-
1221
- return true;
1068
+ return true;
1222
1069
  }
1223
1070
 
1224
- bool
1225
- mongocrypt_setopt_aes_256_ecb (mongocrypt_t *crypt,
1226
- mongocrypt_crypto_fn aes_256_ecb_encrypt,
1227
- void *ctx)
1228
- {
1229
- BSON_ASSERT_PARAM (crypt);
1230
-
1231
- mongocrypt_status_t *status = crypt->status;
1232
-
1233
- if (!crypt->crypto) {
1234
- crypt->crypto = bson_malloc0 (sizeof (*crypt->crypto));
1235
- BSON_ASSERT (crypt->crypto);
1236
- }
1071
+ bool mongocrypt_setopt_aes_256_ecb(mongocrypt_t *crypt, mongocrypt_crypto_fn aes_256_ecb_encrypt, void *ctx) {
1072
+ ASSERT_MONGOCRYPT_PARAM_UNINIT(crypt);
1237
1073
 
1238
- if (crypt->initialized) {
1239
- CLIENT_ERR ("options cannot be set after initialization");
1240
- return false;
1241
- }
1074
+ if (!crypt->crypto) {
1075
+ crypt->crypto = bson_malloc0(sizeof(*crypt->crypto));
1076
+ BSON_ASSERT(crypt->crypto);
1077
+ }
1242
1078
 
1243
- if (!aes_256_ecb_encrypt) {
1244
- CLIENT_ERR ("aes_256_ecb_encrypt not set");
1245
- return false;
1246
- }
1079
+ if (!aes_256_ecb_encrypt) {
1080
+ mongocrypt_status_t *status = crypt->status;
1081
+ CLIENT_ERR("aes_256_ecb_encrypt not set");
1082
+ return false;
1083
+ }
1247
1084
 
1248
- crypt->crypto->aes_256_ecb_encrypt = aes_256_ecb_encrypt;
1085
+ crypt->crypto->aes_256_ecb_encrypt = aes_256_ecb_encrypt;
1249
1086
 
1250
- return true;
1087
+ return true;
1251
1088
  }
1252
1089
 
1253
- bool
1254
- mongocrypt_setopt_kms_providers (mongocrypt_t *crypt,
1255
- mongocrypt_binary_t *kms_providers_definition)
1256
- {
1257
- BSON_ASSERT_PARAM (crypt);
1258
- BSON_ASSERT_PARAM (kms_providers_definition);
1090
+ bool mongocrypt_setopt_kms_providers(mongocrypt_t *crypt, mongocrypt_binary_t *kms_providers_definition) {
1091
+ ASSERT_MONGOCRYPT_PARAM_UNINIT(crypt);
1092
+ BSON_ASSERT_PARAM(kms_providers_definition);
1259
1093
 
1260
- mongocrypt_status_t *const status = crypt->status;
1261
-
1262
- if (crypt->initialized) {
1263
- CLIENT_ERR ("options cannot be set after initialization");
1264
- return false;
1265
- }
1266
-
1267
- return _mongocrypt_parse_kms_providers (kms_providers_definition,
1094
+ return _mongocrypt_parse_kms_providers(kms_providers_definition,
1268
1095
  &crypt->opts.kms_providers,
1269
1096
  crypt->status,
1270
1097
  &crypt->log);
1271
1098
  }
1272
1099
 
1273
- bool
1274
- _mongocrypt_parse_kms_providers (
1275
- mongocrypt_binary_t *kms_providers_definition,
1276
- _mongocrypt_opts_kms_providers_t *kms_providers,
1277
- mongocrypt_status_t *status,
1278
- _mongocrypt_log_t *log)
1279
- {
1280
- bson_t as_bson;
1281
- bson_iter_t iter;
1282
-
1283
- BSON_ASSERT_PARAM (kms_providers_definition);
1284
- BSON_ASSERT_PARAM (kms_providers);
1285
- if (!_mongocrypt_binary_to_bson (kms_providers_definition, &as_bson) ||
1286
- !bson_iter_init (&iter, &as_bson)) {
1287
- CLIENT_ERR ("invalid BSON");
1288
- return false;
1289
- }
1290
-
1291
- while (bson_iter_next (&iter)) {
1292
- const char *field_name;
1293
- bson_t field_bson;
1294
-
1295
- field_name = bson_iter_key (&iter);
1296
- if (!mc_iter_document_as_bson (&iter, &field_bson, status)) {
1297
- return false;
1298
- }
1299
-
1300
- if (0 == strcmp (field_name, "azure") && bson_empty (&field_bson)) {
1301
- kms_providers->need_credentials |= MONGOCRYPT_KMS_PROVIDER_AZURE;
1302
- } else if (0 == strcmp (field_name, "azure")) {
1303
- if (0 != (kms_providers->configured_providers &
1304
- MONGOCRYPT_KMS_PROVIDER_AZURE)) {
1305
- CLIENT_ERR ("azure KMS provider already set");
1100
+ bool _mongocrypt_parse_kms_providers(mongocrypt_binary_t *kms_providers_definition,
1101
+ _mongocrypt_opts_kms_providers_t *kms_providers,
1102
+ mongocrypt_status_t *status,
1103
+ _mongocrypt_log_t *log) {
1104
+ bson_t as_bson;
1105
+ bson_iter_t iter;
1106
+
1107
+ BSON_ASSERT_PARAM(kms_providers_definition);
1108
+ BSON_ASSERT_PARAM(kms_providers);
1109
+ if (!_mongocrypt_binary_to_bson(kms_providers_definition, &as_bson) || !bson_iter_init(&iter, &as_bson)) {
1110
+ CLIENT_ERR("invalid BSON");
1111
+ return false;
1112
+ }
1113
+
1114
+ while (bson_iter_next(&iter)) {
1115
+ const char *field_name;
1116
+ bson_t field_bson;
1117
+
1118
+ field_name = bson_iter_key(&iter);
1119
+ if (!mc_iter_document_as_bson(&iter, &field_bson, status)) {
1306
1120
  return false;
1307
- }
1121
+ }
1122
+
1123
+ if (0 == strcmp(field_name, "azure") && bson_empty(&field_bson)) {
1124
+ kms_providers->need_credentials |= MONGOCRYPT_KMS_PROVIDER_AZURE;
1125
+ } else if (0 == strcmp(field_name, "azure")) {
1126
+ if (0 != (kms_providers->configured_providers & MONGOCRYPT_KMS_PROVIDER_AZURE)) {
1127
+ CLIENT_ERR("azure KMS provider already set");
1128
+ return false;
1129
+ }
1308
1130
 
1309
- if (!_mongocrypt_parse_optional_utf8 (
1310
- &as_bson,
1311
- "azure.accessToken",
1312
- &kms_providers->azure.access_token,
1313
- status)) {
1314
- return false;
1315
- }
1131
+ if (!_mongocrypt_parse_optional_utf8(&as_bson,
1132
+ "azure.accessToken",
1133
+ &kms_providers->azure.access_token,
1134
+ status)) {
1135
+ return false;
1136
+ }
1316
1137
 
1317
- if (kms_providers->azure.access_token) {
1318
- // Caller provides an accessToken directly
1319
- if (!_mongocrypt_check_allowed_fields (
1320
- &as_bson, "azure", status, "accessToken")) {
1321
- return false;
1138
+ if (kms_providers->azure.access_token) {
1139
+ // Caller provides an accessToken directly
1140
+ if (!_mongocrypt_check_allowed_fields(&as_bson, "azure", status, "accessToken")) {
1141
+ return false;
1142
+ }
1143
+ kms_providers->configured_providers |= MONGOCRYPT_KMS_PROVIDER_AZURE;
1144
+ continue;
1322
1145
  }
1323
- kms_providers->configured_providers |=
1324
- MONGOCRYPT_KMS_PROVIDER_AZURE;
1325
- continue;
1326
- }
1327
-
1328
- // No accessToken given, so we'll need to look one up on our own later
1329
- // using the Azure API
1330
-
1331
- if (!_mongocrypt_parse_required_utf8 (&as_bson,
1332
- "azure.tenantId",
1333
- &kms_providers->azure.tenant_id,
1334
- status)) {
1335
- return false;
1336
- }
1337
1146
 
1338
- if (!_mongocrypt_parse_required_utf8 (&as_bson,
1339
- "azure.clientId",
1340
- &kms_providers->azure.client_id,
1341
- status)) {
1342
- return false;
1343
- }
1147
+ // No accessToken given, so we'll need to look one up on our own later
1148
+ // using the Azure API
1344
1149
 
1345
- if (!_mongocrypt_parse_required_utf8 (
1346
- &as_bson,
1347
- "azure.clientSecret",
1348
- &kms_providers->azure.client_secret,
1349
- status)) {
1350
- return false;
1351
- }
1352
-
1353
- if (!_mongocrypt_parse_optional_endpoint (
1354
- &as_bson,
1355
- "azure.identityPlatformEndpoint",
1356
- &kms_providers->azure.identity_platform_endpoint,
1357
- NULL /* opts */,
1358
- status)) {
1359
- return false;
1360
- }
1361
-
1362
- if (!_mongocrypt_check_allowed_fields (&as_bson,
1363
- "azure",
1364
- status,
1365
- "tenantId",
1366
- "clientId",
1367
- "clientSecret",
1368
- "identityPlatformEndpoint")) {
1369
- return false;
1370
- }
1371
- kms_providers->configured_providers |= MONGOCRYPT_KMS_PROVIDER_AZURE;
1372
- } else if (0 == strcmp (field_name, "gcp") && bson_empty (&field_bson)) {
1373
- kms_providers->need_credentials |= MONGOCRYPT_KMS_PROVIDER_GCP;
1374
- } else if (0 == strcmp (field_name, "gcp")) {
1375
- if (0 != (kms_providers->configured_providers &
1376
- MONGOCRYPT_KMS_PROVIDER_GCP)) {
1377
- CLIENT_ERR ("gcp KMS provider already set");
1378
- return false;
1379
- }
1150
+ if (!_mongocrypt_parse_required_utf8(&as_bson, "azure.tenantId", &kms_providers->azure.tenant_id, status)) {
1151
+ return false;
1152
+ }
1380
1153
 
1381
- if (!_mongocrypt_parse_optional_utf8 (&as_bson,
1382
- "gcp.accessToken",
1383
- &kms_providers->gcp.access_token,
1384
- status)) {
1385
- return false;
1386
- }
1154
+ if (!_mongocrypt_parse_required_utf8(&as_bson, "azure.clientId", &kms_providers->azure.client_id, status)) {
1155
+ return false;
1156
+ }
1157
+
1158
+ if (!_mongocrypt_parse_required_utf8(&as_bson,
1159
+ "azure.clientSecret",
1160
+ &kms_providers->azure.client_secret,
1161
+ status)) {
1162
+ return false;
1163
+ }
1164
+
1165
+ if (!_mongocrypt_parse_optional_endpoint(&as_bson,
1166
+ "azure.identityPlatformEndpoint",
1167
+ &kms_providers->azure.identity_platform_endpoint,
1168
+ NULL /* opts */,
1169
+ status)) {
1170
+ return false;
1171
+ }
1172
+
1173
+ if (!_mongocrypt_check_allowed_fields(&as_bson,
1174
+ "azure",
1175
+ status,
1176
+ "tenantId",
1177
+ "clientId",
1178
+ "clientSecret",
1179
+ "identityPlatformEndpoint")) {
1180
+ return false;
1181
+ }
1182
+ kms_providers->configured_providers |= MONGOCRYPT_KMS_PROVIDER_AZURE;
1183
+ } else if (0 == strcmp(field_name, "gcp") && bson_empty(&field_bson)) {
1184
+ kms_providers->need_credentials |= MONGOCRYPT_KMS_PROVIDER_GCP;
1185
+ } else if (0 == strcmp(field_name, "gcp")) {
1186
+ if (0 != (kms_providers->configured_providers & MONGOCRYPT_KMS_PROVIDER_GCP)) {
1187
+ CLIENT_ERR("gcp KMS provider already set");
1188
+ return false;
1189
+ }
1190
+
1191
+ if (!_mongocrypt_parse_optional_utf8(&as_bson,
1192
+ "gcp.accessToken",
1193
+ &kms_providers->gcp.access_token,
1194
+ status)) {
1195
+ return false;
1196
+ }
1197
+
1198
+ if (NULL != kms_providers->gcp.access_token) {
1199
+ /* "gcp" document has form:
1200
+ * {
1201
+ * "accessToken": <required UTF-8>
1202
+ * }
1203
+ */
1204
+ if (!_mongocrypt_check_allowed_fields(&as_bson, "gcp", status, "accessToken")) {
1205
+ return false;
1206
+ }
1207
+ kms_providers->configured_providers |= MONGOCRYPT_KMS_PROVIDER_GCP;
1208
+ continue;
1209
+ }
1387
1210
 
1388
- if (NULL != kms_providers->gcp.access_token) {
1389
1211
  /* "gcp" document has form:
1390
1212
  * {
1391
- * "accessToken": <required UTF-8>
1213
+ * "email": <required UTF-8>
1214
+ * "privateKey": <required UTF-8 or Binary>
1392
1215
  * }
1393
1216
  */
1394
- if (!_mongocrypt_check_allowed_fields (
1395
- &as_bson, "gcp", status, "accessToken")) {
1396
- return false;
1217
+ if (!_mongocrypt_parse_required_utf8(&as_bson, "gcp.email", &kms_providers->gcp.email, status)) {
1218
+ return false;
1397
1219
  }
1398
- kms_providers->configured_providers |= MONGOCRYPT_KMS_PROVIDER_GCP;
1399
- continue;
1400
- }
1401
-
1402
- /* "gcp" document has form:
1403
- * {
1404
- * "email": <required UTF-8>
1405
- * "privateKey": <required UTF-8 or Binary>
1406
- * }
1407
- */
1408
- if (!_mongocrypt_parse_required_utf8 (
1409
- &as_bson, "gcp.email", &kms_providers->gcp.email, status)) {
1410
- return false;
1411
- }
1412
1220
 
1413
- if (!_mongocrypt_parse_required_binary (
1414
- &as_bson,
1415
- "gcp.privateKey",
1416
- &kms_providers->gcp.private_key,
1417
- status)) {
1418
- return false;
1419
- }
1420
-
1421
- if (!_mongocrypt_parse_optional_endpoint (&as_bson,
1422
- "gcp.endpoint",
1423
- &kms_providers->gcp.endpoint,
1424
- NULL /* opts */,
1221
+ if (!_mongocrypt_parse_required_binary(&as_bson,
1222
+ "gcp.privateKey",
1223
+ &kms_providers->gcp.private_key,
1425
1224
  status)) {
1426
- return false;
1427
- }
1225
+ return false;
1226
+ }
1428
1227
 
1429
- if (!_mongocrypt_check_allowed_fields (
1430
- &as_bson, "gcp", status, "email", "privateKey", "endpoint")) {
1431
- return false;
1432
- }
1433
- kms_providers->configured_providers |= MONGOCRYPT_KMS_PROVIDER_GCP;
1434
- } else if (0 == strcmp (field_name, "local") &&
1435
- bson_empty (&field_bson)) {
1436
- kms_providers->need_credentials |= MONGOCRYPT_KMS_PROVIDER_LOCAL;
1437
- } else if (0 == strcmp (field_name, "local")) {
1438
- if (!_mongocrypt_parse_required_binary (
1439
- &as_bson, "local.key", &kms_providers->local.key, status)) {
1440
- return false;
1441
- }
1228
+ if (!_mongocrypt_parse_optional_endpoint(&as_bson,
1229
+ "gcp.endpoint",
1230
+ &kms_providers->gcp.endpoint,
1231
+ NULL /* opts */,
1232
+ status)) {
1233
+ return false;
1234
+ }
1442
1235
 
1443
- if (kms_providers->local.key.len != MONGOCRYPT_KEY_LEN) {
1444
- CLIENT_ERR ("local key must be %d bytes", MONGOCRYPT_KEY_LEN);
1445
- return false;
1446
- }
1236
+ if (!_mongocrypt_check_allowed_fields(&as_bson, "gcp", status, "email", "privateKey", "endpoint")) {
1237
+ return false;
1238
+ }
1239
+ kms_providers->configured_providers |= MONGOCRYPT_KMS_PROVIDER_GCP;
1240
+ } else if (0 == strcmp(field_name, "local") && bson_empty(&field_bson)) {
1241
+ kms_providers->need_credentials |= MONGOCRYPT_KMS_PROVIDER_LOCAL;
1242
+ } else if (0 == strcmp(field_name, "local")) {
1243
+ if (!_mongocrypt_parse_required_binary(&as_bson, "local.key", &kms_providers->local.key, status)) {
1244
+ return false;
1245
+ }
1447
1246
 
1448
- if (!_mongocrypt_check_allowed_fields (
1449
- &as_bson, "local", status, "key")) {
1450
- return false;
1451
- }
1452
- kms_providers->configured_providers |= MONGOCRYPT_KMS_PROVIDER_LOCAL;
1453
- } else if (0 == strcmp (field_name, "aws") && bson_empty (&field_bson)) {
1454
- kms_providers->need_credentials |= MONGOCRYPT_KMS_PROVIDER_AWS;
1455
- } else if (0 == strcmp (field_name, "aws")) {
1456
- if (!_mongocrypt_parse_required_utf8 (
1457
- &as_bson,
1458
- "aws.accessKeyId",
1459
- &kms_providers->aws.access_key_id,
1460
- status)) {
1461
- return false;
1462
- }
1463
- if (!_mongocrypt_parse_required_utf8 (
1464
- &as_bson,
1465
- "aws.secretAccessKey",
1466
- &kms_providers->aws.secret_access_key,
1467
- status)) {
1468
- return false;
1469
- }
1247
+ if (kms_providers->local.key.len != MONGOCRYPT_KEY_LEN) {
1248
+ CLIENT_ERR("local key must be %d bytes", MONGOCRYPT_KEY_LEN);
1249
+ return false;
1250
+ }
1470
1251
 
1471
- if (!_mongocrypt_parse_optional_utf8 (
1472
- &as_bson,
1473
- "aws.sessionToken",
1474
- &kms_providers->aws.session_token,
1475
- status)) {
1476
- return false;
1477
- }
1478
-
1479
- if (!_mongocrypt_check_allowed_fields (&as_bson,
1480
- "aws",
1481
- status,
1482
- "accessKeyId",
1483
- "secretAccessKey",
1484
- "sessionToken")) {
1485
- return false;
1486
- }
1487
- kms_providers->configured_providers |= MONGOCRYPT_KMS_PROVIDER_AWS;
1488
- } else if (0 == strcmp (field_name, "kmip") && bson_empty (&field_bson)) {
1489
- kms_providers->need_credentials |= MONGOCRYPT_KMS_PROVIDER_KMIP;
1490
- } else if (0 == strcmp (field_name, "kmip")) {
1491
- _mongocrypt_endpoint_parse_opts_t opts = {0};
1492
-
1493
- opts.allow_empty_subdomain = true;
1494
- if (!_mongocrypt_parse_required_endpoint (
1495
- &as_bson,
1496
- "kmip.endpoint",
1497
- &kms_providers->kmip.endpoint,
1498
- &opts,
1499
- status)) {
1500
- return false;
1501
- }
1252
+ if (!_mongocrypt_check_allowed_fields(&as_bson, "local", status, "key")) {
1253
+ return false;
1254
+ }
1255
+ kms_providers->configured_providers |= MONGOCRYPT_KMS_PROVIDER_LOCAL;
1256
+ } else if (0 == strcmp(field_name, "aws") && bson_empty(&field_bson)) {
1257
+ kms_providers->need_credentials |= MONGOCRYPT_KMS_PROVIDER_AWS;
1258
+ } else if (0 == strcmp(field_name, "aws")) {
1259
+ if (!_mongocrypt_parse_required_utf8(&as_bson,
1260
+ "aws.accessKeyId",
1261
+ &kms_providers->aws.access_key_id,
1262
+ status)) {
1263
+ return false;
1264
+ }
1265
+ if (!_mongocrypt_parse_required_utf8(&as_bson,
1266
+ "aws.secretAccessKey",
1267
+ &kms_providers->aws.secret_access_key,
1268
+ status)) {
1269
+ return false;
1270
+ }
1502
1271
 
1503
- if (!_mongocrypt_check_allowed_fields (
1504
- &as_bson, "kmip", status, "endpoint")) {
1272
+ if (!_mongocrypt_parse_optional_utf8(&as_bson,
1273
+ "aws.sessionToken",
1274
+ &kms_providers->aws.session_token,
1275
+ status)) {
1276
+ return false;
1277
+ }
1278
+
1279
+ if (!_mongocrypt_check_allowed_fields(&as_bson,
1280
+ "aws",
1281
+ status,
1282
+ "accessKeyId",
1283
+ "secretAccessKey",
1284
+ "sessionToken")) {
1285
+ return false;
1286
+ }
1287
+ kms_providers->configured_providers |= MONGOCRYPT_KMS_PROVIDER_AWS;
1288
+ } else if (0 == strcmp(field_name, "kmip") && bson_empty(&field_bson)) {
1289
+ kms_providers->need_credentials |= MONGOCRYPT_KMS_PROVIDER_KMIP;
1290
+ } else if (0 == strcmp(field_name, "kmip")) {
1291
+ _mongocrypt_endpoint_parse_opts_t opts = {0};
1292
+
1293
+ opts.allow_empty_subdomain = true;
1294
+ if (!_mongocrypt_parse_required_endpoint(&as_bson,
1295
+ "kmip.endpoint",
1296
+ &kms_providers->kmip.endpoint,
1297
+ &opts,
1298
+ status)) {
1299
+ return false;
1300
+ }
1301
+
1302
+ if (!_mongocrypt_check_allowed_fields(&as_bson, "kmip", status, "endpoint")) {
1303
+ return false;
1304
+ }
1305
+ kms_providers->configured_providers |= MONGOCRYPT_KMS_PROVIDER_KMIP;
1306
+ } else {
1307
+ CLIENT_ERR("unsupported KMS provider: %s", field_name);
1505
1308
  return false;
1506
- }
1507
- kms_providers->configured_providers |= MONGOCRYPT_KMS_PROVIDER_KMIP;
1508
- } else {
1509
- CLIENT_ERR ("unsupported KMS provider: %s", field_name);
1510
- return false;
1511
- }
1512
- }
1513
-
1514
- if (log && log->trace_enabled) {
1515
- char *as_str = bson_as_json (&as_bson, NULL);
1516
- _mongocrypt_log (log,
1517
- MONGOCRYPT_LOG_LEVEL_TRACE,
1518
- "%s (%s=\"%s\")",
1519
- BSON_FUNC,
1520
- "kms_providers",
1521
- as_str);
1522
- bson_free (as_str);
1523
- }
1524
-
1525
- return true;
1526
- }
1309
+ }
1310
+ }
1527
1311
 
1312
+ if (log && log->trace_enabled) {
1313
+ char *as_str = bson_as_json(&as_bson, NULL);
1314
+ _mongocrypt_log(log, MONGOCRYPT_LOG_LEVEL_TRACE, "%s (%s=\"%s\")", BSON_FUNC, "kms_providers", as_str);
1315
+ bson_free(as_str);
1316
+ }
1528
1317
 
1529
- void
1530
- mongocrypt_setopt_append_crypt_shared_lib_search_path (mongocrypt_t *crypt,
1531
- const char *path)
1532
- {
1533
- BSON_ASSERT_PARAM (crypt);
1534
- BSON_ASSERT_PARAM (path);
1535
-
1536
- // Dup the path string for us to manage
1537
- mstr pathdup = mstr_copy_cstr (path);
1538
- // Increase array len
1539
- BSON_ASSERT (crypt->opts.n_crypt_shared_lib_search_paths < INT_MAX);
1540
- const int new_len = crypt->opts.n_crypt_shared_lib_search_paths + 1;
1541
- BSON_ASSERT (new_len > 0 && sizeof (mstr) <= SIZE_MAX / (size_t) new_len);
1542
- mstr *const new_array =
1543
- bson_realloc (crypt->opts.crypt_shared_lib_search_paths,
1544
- sizeof (mstr) * (size_t) new_len);
1545
-
1546
- // Store the path
1547
- new_array[new_len - 1] = pathdup;
1548
- // Write back opts
1549
- crypt->opts.crypt_shared_lib_search_paths = new_array;
1550
- crypt->opts.n_crypt_shared_lib_search_paths = new_len;
1318
+ return true;
1551
1319
  }
1552
1320
 
1321
+ void mongocrypt_setopt_append_crypt_shared_lib_search_path(mongocrypt_t *crypt, const char *path) {
1322
+ BSON_ASSERT_PARAM(crypt);
1323
+ BSON_ASSERT_PARAM(path);
1324
+
1325
+ // Dup the path string for us to manage
1326
+ mstr pathdup = mstr_copy_cstr(path);
1327
+ // Increase array len
1328
+ BSON_ASSERT(crypt->opts.n_crypt_shared_lib_search_paths < INT_MAX);
1329
+ const int new_len = crypt->opts.n_crypt_shared_lib_search_paths + 1;
1330
+ BSON_ASSERT(new_len > 0 && sizeof(mstr) <= SIZE_MAX / (size_t)new_len);
1331
+ mstr *const new_array = bson_realloc(crypt->opts.crypt_shared_lib_search_paths, sizeof(mstr) * (size_t)new_len);
1332
+
1333
+ // Store the path
1334
+ new_array[new_len - 1] = pathdup;
1335
+ // Write back opts
1336
+ crypt->opts.crypt_shared_lib_search_paths = new_array;
1337
+ crypt->opts.n_crypt_shared_lib_search_paths = new_len;
1338
+ }
1553
1339
 
1554
- void
1555
- mongocrypt_setopt_use_need_kms_credentials_state (mongocrypt_t *crypt)
1556
- {
1557
- BSON_ASSERT_PARAM (crypt);
1340
+ void mongocrypt_setopt_use_need_kms_credentials_state(mongocrypt_t *crypt) {
1341
+ BSON_ASSERT_PARAM(crypt);
1558
1342
 
1559
- crypt->opts.use_need_kms_credentials_state = true;
1343
+ crypt->opts.use_need_kms_credentials_state = true;
1560
1344
  }
1561
1345
 
1346
+ void mongocrypt_setopt_set_crypt_shared_lib_path_override(mongocrypt_t *crypt, const char *path) {
1347
+ BSON_ASSERT_PARAM(crypt);
1348
+ BSON_ASSERT_PARAM(path);
1562
1349
 
1563
- void
1564
- mongocrypt_setopt_set_crypt_shared_lib_path_override (mongocrypt_t *crypt,
1565
- const char *path)
1566
- {
1567
- BSON_ASSERT_PARAM (crypt);
1568
- BSON_ASSERT_PARAM (path);
1569
-
1570
- mstr_assign (&crypt->opts.crypt_shared_lib_override_path,
1571
- mstr_copy_cstr (path));
1350
+ mstr_assign(&crypt->opts.crypt_shared_lib_override_path, mstr_copy_cstr(path));
1572
1351
  }
1573
1352
 
1574
- bool
1575
- _mongocrypt_needs_credentials (mongocrypt_t *crypt)
1576
- {
1577
- BSON_ASSERT_PARAM (crypt);
1353
+ bool _mongocrypt_needs_credentials(mongocrypt_t *crypt) {
1354
+ BSON_ASSERT_PARAM(crypt);
1578
1355
 
1579
- if (!crypt->opts.use_need_kms_credentials_state) {
1580
- return false;
1581
- }
1356
+ if (!crypt->opts.use_need_kms_credentials_state) {
1357
+ return false;
1358
+ }
1582
1359
 
1583
- return crypt->opts.kms_providers.need_credentials != 0;
1360
+ return crypt->opts.kms_providers.need_credentials != 0;
1584
1361
  }
1585
1362
 
1586
- bool
1587
- _mongocrypt_needs_credentials_for_provider (mongocrypt_t *crypt,
1588
- _mongocrypt_kms_provider_t provider)
1589
- {
1590
- BSON_ASSERT_PARAM (crypt);
1363
+ bool _mongocrypt_needs_credentials_for_provider(mongocrypt_t *crypt, _mongocrypt_kms_provider_t provider) {
1364
+ BSON_ASSERT_PARAM(crypt);
1591
1365
 
1592
- if (!crypt->opts.use_need_kms_credentials_state) {
1593
- return false;
1594
- }
1366
+ if (!crypt->opts.use_need_kms_credentials_state) {
1367
+ return false;
1368
+ }
1595
1369
 
1596
- return (crypt->opts.kms_providers.need_credentials & (int) provider) != 0;
1370
+ return (crypt->opts.kms_providers.need_credentials & (int)provider) != 0;
1597
1371
  }
1598
1372
 
1599
- void
1600
- mongocrypt_setopt_bypass_query_analysis (mongocrypt_t *crypt)
1601
- {
1602
- BSON_ASSERT_PARAM (crypt);
1373
+ void mongocrypt_setopt_bypass_query_analysis(mongocrypt_t *crypt) {
1374
+ BSON_ASSERT_PARAM(crypt);
1603
1375
 
1604
- crypt->opts.bypass_query_analysis = true;
1376
+ crypt->opts.bypass_query_analysis = true;
1605
1377
  }