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
@@ -16,1046 +16,832 @@
16
16
 
17
17
  #include "test-mongocrypt.h"
18
18
 
19
+ #define TEST_REWRAP_MASTER_KEY_ID_OLD \
20
+ "arn:aws:kms:us-east-1:579766882180:key/" \
21
+ "89fcc2c4-08b0-4bd9-9f25-e30687b580d0"
19
22
 
20
- #define TEST_REWRAP_MASTER_KEY_ID_OLD \
21
- "arn:aws:kms:us-east-1:579766882180:key/" \
22
- "89fcc2c4-08b0-4bd9-9f25-e30687b580d0"
23
-
24
- #define TEST_REWRAP_MASTER_KEY_ID_NEW \
25
- "arn:aws:kms:us-east-1:579766882180:key/" \
26
- "061334ae-07a8-4ceb-a813-8135540e837d"
27
-
23
+ #define TEST_REWRAP_MASTER_KEY_ID_NEW \
24
+ "arn:aws:kms:us-east-1:579766882180:key/" \
25
+ "061334ae-07a8-4ceb-a813-8135540e837d"
28
26
 
29
27
  typedef struct {
30
- _mongocrypt_buffer_t id;
31
- const char *kek_id;
32
- _mongocrypt_buffer_t key_material;
33
- int64_t creation_date;
34
- int64_t update_date;
35
- _mongocrypt_key_alt_name_t *key_alt_names;
28
+ _mongocrypt_buffer_t id;
29
+ const char *kek_id;
30
+ _mongocrypt_buffer_t key_material;
31
+ int64_t creation_date;
32
+ int64_t update_date;
33
+ _mongocrypt_key_alt_name_t *key_alt_names;
36
34
  } _test_datakey_fields_t;
37
35
 
38
- static _test_datakey_fields_t *
39
- _test_datakey_fields_new (void)
40
- {
41
- return bson_malloc0 (sizeof (_test_datakey_fields_t));
36
+ static _test_datakey_fields_t *_test_datakey_fields_new(void) {
37
+ return bson_malloc0(sizeof(_test_datakey_fields_t));
42
38
  }
43
39
 
44
- static void
45
- _test_datakey_fields_destroy (_test_datakey_fields_t *fields)
46
- {
47
- if (!fields) {
48
- return;
49
- }
40
+ static void _test_datakey_fields_destroy(_test_datakey_fields_t *fields) {
41
+ if (!fields) {
42
+ return;
43
+ }
50
44
 
51
- _mongocrypt_key_alt_name_destroy_all (fields->key_alt_names);
45
+ _mongocrypt_key_alt_name_destroy_all(fields->key_alt_names);
52
46
 
53
- bson_free (fields);
47
+ bson_free(fields);
54
48
  }
55
49
 
56
- static _mongocrypt_buffer_t
57
- _find_key_id (mongocrypt_binary_t *key)
58
- {
59
- bson_t bson;
60
- bson_iter_t iter;
61
- _mongocrypt_buffer_t buf;
50
+ static _mongocrypt_buffer_t _find_key_id(mongocrypt_binary_t *key) {
51
+ bson_t bson;
52
+ bson_iter_t iter;
53
+ _mongocrypt_buffer_t buf;
62
54
 
63
- BSON_ASSERT_PARAM (key);
55
+ BSON_ASSERT_PARAM(key);
64
56
 
65
- ASSERT (_mongocrypt_binary_to_bson (key, &bson));
66
- ASSERT (bson_iter_init (&iter, &bson));
67
- ASSERT (bson_iter_find_descendant (&iter, "_id", &iter));
68
- ASSERT (BSON_ITER_HOLDS_BINARY (&iter));
69
- ASSERT (_mongocrypt_buffer_from_binary_iter (&buf, &iter));
70
- ASSERT (buf.subtype == BSON_SUBTYPE_UUID);
71
- ASSERT (buf.len > 0u);
57
+ ASSERT(_mongocrypt_binary_to_bson(key, &bson));
58
+ ASSERT(bson_iter_init(&iter, &bson));
59
+ ASSERT(bson_iter_find_descendant(&iter, "_id", &iter));
60
+ ASSERT(BSON_ITER_HOLDS_BINARY(&iter));
61
+ ASSERT(_mongocrypt_buffer_from_binary_iter(&buf, &iter));
62
+ ASSERT(buf.subtype == BSON_SUBTYPE_UUID);
63
+ ASSERT(buf.len > 0u);
72
64
 
73
- return buf;
65
+ return buf;
74
66
  }
75
67
 
76
- static _mongocrypt_buffer_t
77
- _find_key_id_from_iter (bson_iter_t *iter)
78
- {
79
- _mongocrypt_buffer_t buf;
68
+ static _mongocrypt_buffer_t _find_key_id_from_iter(bson_iter_t *iter) {
69
+ _mongocrypt_buffer_t buf;
80
70
 
81
- ASSERT (BSON_ITER_HOLDS_BINARY (iter));
82
- ASSERT (_mongocrypt_buffer_from_binary_iter (&buf, iter));
83
- ASSERT (buf.subtype == BSON_SUBTYPE_UUID);
84
- ASSERT (buf.len > 0u);
71
+ ASSERT(BSON_ITER_HOLDS_BINARY(iter));
72
+ ASSERT(_mongocrypt_buffer_from_binary_iter(&buf, iter));
73
+ ASSERT(buf.subtype == BSON_SUBTYPE_UUID);
74
+ ASSERT(buf.len > 0u);
85
75
 
86
- return buf;
76
+ return buf;
87
77
  }
88
78
 
89
- static const char *
90
- _find_masterkey_id (mongocrypt_binary_t *key)
91
- {
92
- bson_t bson;
93
- bson_iter_t iter;
94
- const char *res;
79
+ static const char *_find_masterkey_id(mongocrypt_binary_t *key) {
80
+ bson_t bson;
81
+ bson_iter_t iter;
82
+ const char *res;
95
83
 
96
- BSON_ASSERT_PARAM (key);
84
+ BSON_ASSERT_PARAM(key);
97
85
 
98
- ASSERT (_mongocrypt_binary_to_bson (key, &bson));
99
- ASSERT (bson_iter_init (&iter, &bson));
100
- ASSERT (bson_iter_find_descendant (&iter, "masterKey.key", &iter));
101
- ASSERT ((res = bson_iter_utf8 (&iter, NULL)));
86
+ ASSERT(_mongocrypt_binary_to_bson(key, &bson));
87
+ ASSERT(bson_iter_init(&iter, &bson));
88
+ ASSERT(bson_iter_find_descendant(&iter, "masterKey.key", &iter));
89
+ ASSERT((res = bson_iter_utf8(&iter, NULL)));
102
90
 
103
- return res;
91
+ return res;
104
92
  }
105
93
 
106
- static _mongocrypt_buffer_t
107
- _find_key_material (mongocrypt_binary_t *key)
108
- {
109
- bson_t bson;
110
- bson_iter_t iter;
111
- _mongocrypt_buffer_t buf;
94
+ static _mongocrypt_buffer_t _find_key_material(mongocrypt_binary_t *key) {
95
+ bson_t bson;
96
+ bson_iter_t iter;
97
+ _mongocrypt_buffer_t buf;
112
98
 
113
- BSON_ASSERT_PARAM (key);
99
+ BSON_ASSERT_PARAM(key);
114
100
 
115
- ASSERT (_mongocrypt_binary_to_bson (key, &bson));
116
- ASSERT (bson_iter_init (&iter, &bson));
117
- ASSERT (bson_iter_find_descendant (&iter, "keyMaterial", &iter));
118
- ASSERT (BSON_ITER_HOLDS_BINARY (&iter));
119
- ASSERT (_mongocrypt_buffer_from_binary_iter (&buf, &iter));
120
- ASSERT (buf.subtype == BSON_SUBTYPE_BINARY);
121
- ASSERT (buf.len > 0u);
101
+ ASSERT(_mongocrypt_binary_to_bson(key, &bson));
102
+ ASSERT(bson_iter_init(&iter, &bson));
103
+ ASSERT(bson_iter_find_descendant(&iter, "keyMaterial", &iter));
104
+ ASSERT(BSON_ITER_HOLDS_BINARY(&iter));
105
+ ASSERT(_mongocrypt_buffer_from_binary_iter(&buf, &iter));
106
+ ASSERT(buf.subtype == BSON_SUBTYPE_BINARY);
107
+ ASSERT(buf.len > 0u);
122
108
 
123
- return buf;
109
+ return buf;
124
110
  }
125
111
 
126
- static _mongocrypt_buffer_t
127
- _key_material_from_iter (bson_iter_t *iter)
128
- {
129
- _mongocrypt_buffer_t buf;
112
+ static _mongocrypt_buffer_t _key_material_from_iter(bson_iter_t *iter) {
113
+ _mongocrypt_buffer_t buf;
130
114
 
131
- ASSERT (BSON_ITER_HOLDS_BINARY (iter));
132
- ASSERT (_mongocrypt_buffer_from_binary_iter (&buf, iter));
133
- ASSERT (buf.subtype == BSON_SUBTYPE_BINARY);
134
- ASSERT (buf.len > 0u);
115
+ ASSERT(BSON_ITER_HOLDS_BINARY(iter));
116
+ ASSERT(_mongocrypt_buffer_from_binary_iter(&buf, iter));
117
+ ASSERT(buf.subtype == BSON_SUBTYPE_BINARY);
118
+ ASSERT(buf.len > 0u);
135
119
 
136
- return buf;
120
+ return buf;
137
121
  }
138
122
 
139
- static bool
140
- _buffer_cmp_equal (const _mongocrypt_buffer_t *lhs,
141
- const _mongocrypt_buffer_t *rhs)
142
- {
143
- BSON_ASSERT_PARAM (lhs);
144
- BSON_ASSERT_PARAM (rhs);
123
+ static bool _buffer_cmp_equal(const _mongocrypt_buffer_t *lhs, const _mongocrypt_buffer_t *rhs) {
124
+ BSON_ASSERT_PARAM(lhs);
125
+ BSON_ASSERT_PARAM(rhs);
145
126
 
146
- return lhs->len == rhs->len && memcmp (lhs->data, rhs->data, lhs->len) == 0;
127
+ return lhs->len == rhs->len && memcmp(lhs->data, rhs->data, lhs->len) == 0;
147
128
  }
148
129
 
149
- static int64_t
150
- _find_date_field (mongocrypt_binary_t *key, const char *dotkey)
151
- {
152
- bson_t bson;
153
- bson_iter_t iter;
154
- int64_t res;
130
+ static int64_t _find_date_field(mongocrypt_binary_t *key, const char *dotkey) {
131
+ bson_t bson;
132
+ bson_iter_t iter;
133
+ int64_t res;
155
134
 
156
- BSON_ASSERT_PARAM (key);
135
+ BSON_ASSERT_PARAM(key);
157
136
 
158
- ASSERT (_mongocrypt_binary_to_bson (key, &bson));
159
- ASSERT (bson_iter_init (&iter, &bson));
160
- ASSERT (bson_iter_find_descendant (&iter, dotkey, &iter));
161
- ASSERT (BSON_ITER_HOLDS_DATE_TIME (&iter));
162
- ASSERT ((res = bson_iter_date_time (&iter)) != 0)
137
+ ASSERT(_mongocrypt_binary_to_bson(key, &bson));
138
+ ASSERT(bson_iter_init(&iter, &bson));
139
+ ASSERT(bson_iter_find_descendant(&iter, dotkey, &iter));
140
+ ASSERT(BSON_ITER_HOLDS_DATE_TIME(&iter));
141
+ ASSERT((res = bson_iter_date_time(&iter)) != 0)
163
142
 
164
- return res;
143
+ return res;
165
144
  }
166
145
 
167
- static int64_t
168
- _find_creation_date (mongocrypt_binary_t *key)
169
- {
170
- return _find_date_field (key, "creationDate");
146
+ static int64_t _find_creation_date(mongocrypt_binary_t *key) {
147
+ return _find_date_field(key, "creationDate");
171
148
  }
172
149
 
173
- static int64_t
174
- _find_update_date (mongocrypt_binary_t *key)
175
- {
176
- return _find_date_field (key, "updateDate");
150
+ static int64_t _find_update_date(mongocrypt_binary_t *key) {
151
+ return _find_date_field(key, "updateDate");
177
152
  }
178
153
 
179
- static _mongocrypt_key_alt_name_t *
180
- _find_key_alt_names (mongocrypt_binary_t *key)
181
- {
182
- bson_t bson;
183
- bson_iter_t iter;
184
- _mongocrypt_key_alt_name_t *res;
154
+ static _mongocrypt_key_alt_name_t *_find_key_alt_names(mongocrypt_binary_t *key) {
155
+ bson_t bson;
156
+ bson_iter_t iter;
157
+ _mongocrypt_key_alt_name_t *res;
185
158
 
186
- BSON_ASSERT_PARAM (key);
159
+ BSON_ASSERT_PARAM(key);
187
160
 
188
- ASSERT (_mongocrypt_binary_to_bson (key, &bson));
189
- ASSERT (bson_iter_init (&iter, &bson));
190
- ASSERT (bson_iter_find_descendant (&iter, "keyAltNames", &iter));
191
- ASSERT (_mongocrypt_key_alt_name_from_iter (&iter, &res, NULL));
192
- ASSERT (res);
161
+ ASSERT(_mongocrypt_binary_to_bson(key, &bson));
162
+ ASSERT(bson_iter_init(&iter, &bson));
163
+ ASSERT(bson_iter_find_descendant(&iter, "keyAltNames", &iter));
164
+ ASSERT(_mongocrypt_key_alt_name_from_iter(&iter, &res, NULL));
165
+ ASSERT(res);
193
166
 
194
- return res;
167
+ return res;
195
168
  }
196
169
 
197
- static _test_datakey_fields_t *
198
- _find_datakey_fields (mongocrypt_binary_t *key)
199
- {
200
- _test_datakey_fields_t *res = _test_datakey_fields_new ();
170
+ static _test_datakey_fields_t *_find_datakey_fields(mongocrypt_binary_t *key) {
171
+ _test_datakey_fields_t *res = _test_datakey_fields_new();
201
172
 
202
- BSON_ASSERT_PARAM (key);
173
+ BSON_ASSERT_PARAM(key);
203
174
 
204
- res->id = _find_key_id (key);
205
- res->kek_id = _find_masterkey_id (key);
206
- res->creation_date = _find_creation_date (key);
207
- res->update_date = _find_update_date (key);
208
- res->key_material = _find_key_material (key);
209
- res->key_alt_names = _find_key_alt_names (key);
175
+ res->id = _find_key_id(key);
176
+ res->kek_id = _find_masterkey_id(key);
177
+ res->creation_date = _find_creation_date(key);
178
+ res->update_date = _find_update_date(key);
179
+ res->key_material = _find_key_material(key);
180
+ res->key_alt_names = _find_key_alt_names(key);
210
181
 
211
- return res;
182
+ return res;
212
183
  }
213
184
 
214
- static void
215
- _assert_aws_kms_request (mongocrypt_kms_ctx_t *kms)
216
- {
217
- BSON_ASSERT_PARAM (kms);
185
+ static void _assert_aws_kms_request(mongocrypt_kms_ctx_t *kms) {
186
+ BSON_ASSERT_PARAM(kms);
218
187
 
219
- ASSERT_STREQUAL (mongocrypt_kms_ctx_get_kms_provider (kms, NULL), "aws");
188
+ ASSERT_STREQUAL(mongocrypt_kms_ctx_get_kms_provider(kms, NULL), "aws");
220
189
 
221
- {
222
- mongocrypt_binary_t bin;
223
- ASSERT (mongocrypt_kms_ctx_message (kms, &bin));
224
- ASSERT (bin.len > 0);
225
- }
190
+ {
191
+ mongocrypt_binary_t bin;
192
+ ASSERT(mongocrypt_kms_ctx_message(kms, &bin));
193
+ ASSERT(bin.len > 0);
194
+ }
226
195
 
227
- {
228
- const char *endpoint;
229
- ASSERT (mongocrypt_kms_ctx_endpoint (kms, &endpoint));
230
- ASSERT (endpoint);
231
- }
196
+ {
197
+ const char *endpoint;
198
+ ASSERT(mongocrypt_kms_ctx_endpoint(kms, &endpoint));
199
+ ASSERT(endpoint);
200
+ }
232
201
 
233
- ASSERT (mongocrypt_kms_ctx_bytes_needed (kms) > 0);
202
+ ASSERT(mongocrypt_kms_ctx_bytes_needed(kms) > 0);
234
203
  }
235
204
 
236
- static void
237
- _assert_aws_kms_endpoint (mongocrypt_kms_ctx_t *kms, const char *expected)
238
- {
239
- const char *endpoint = NULL;
240
- ASSERT ((mongocrypt_kms_ctx_endpoint (kms, &endpoint)));
241
- ASSERT_STREQUAL (expected, endpoint);
205
+ static void _assert_aws_kms_endpoint(mongocrypt_kms_ctx_t *kms, const char *expected) {
206
+ const char *endpoint = NULL;
207
+ ASSERT((mongocrypt_kms_ctx_endpoint(kms, &endpoint)));
208
+ ASSERT_STREQUAL(expected, endpoint);
242
209
  }
243
210
 
211
+ static void _test_rewrap_many_datakey_init(_mongocrypt_tester_t *tester) {
212
+ mongocrypt_t *const crypt = _mongocrypt_tester_mongocrypt(TESTER_MONGOCRYPT_DEFAULT);
213
+ mongocrypt_ctx_t *ctx = NULL;
214
+
215
+ /* No context, nothing to init. */
216
+ ASSERT(!mongocrypt_ctx_rewrap_many_datakey_init(NULL, NULL));
217
+
218
+ /* Filter argument required. */
219
+ ctx = mongocrypt_ctx_new(crypt);
220
+ ASSERT_FAILS(mongocrypt_ctx_rewrap_many_datakey_init(ctx, NULL), ctx, "filter must not be null");
221
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_ERROR);
222
+ mongocrypt_ctx_destroy(ctx);
223
+
224
+ /* Irrelevant options should trigger initialization error. */
225
+ ctx = mongocrypt_ctx_new(crypt);
226
+ ASSERT_OK(mongocrypt_ctx_setopt_key_alt_name(ctx, TEST_BSON("{'keyAltName': 'test'}")), ctx);
227
+ ASSERT_FAILS(mongocrypt_ctx_rewrap_many_datakey_init(ctx, TEST_BSON("{}")), ctx, "key id and alt name prohibited");
228
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_ERROR);
229
+ mongocrypt_ctx_destroy(ctx);
230
+
231
+ /* rewrapManyDataKeyOpts.newProvider and rewrapManyDataKeyOpts.newMasterKey
232
+ * should be provided via mongocrypt_ctx_setopt_key_encryption_key. */
233
+ ctx = mongocrypt_ctx_new(crypt);
234
+ ASSERT_OK(mongocrypt_ctx_setopt_key_encryption_key(ctx,
235
+ TEST_BSON("{'provider': 'aws',"
236
+ " 'region': 'us-east-1',"
237
+ " 'key': '" TEST_REWRAP_MASTER_KEY_ID_NEW "'}")),
238
+ ctx);
239
+ ASSERT_OK(mongocrypt_ctx_rewrap_many_datakey_init(ctx, TEST_BSON("{}")), ctx);
240
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_MONGO_KEYS);
241
+ mongocrypt_ctx_destroy(ctx);
242
+
243
+ /* Not providing rewrapManyDataKeyOpts is OK. */
244
+ ctx = mongocrypt_ctx_new(crypt);
245
+ ASSERT_OK(mongocrypt_ctx_rewrap_many_datakey_init(ctx, TEST_BSON("{}")), ctx);
246
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_MONGO_KEYS);
247
+ mongocrypt_ctx_destroy(ctx);
248
+
249
+ mongocrypt_destroy(crypt);
250
+ }
244
251
 
245
- static void
246
- _test_rewrap_many_datakey_init (_mongocrypt_tester_t *tester)
247
- {
248
- mongocrypt_t *const crypt =
249
- _mongocrypt_tester_mongocrypt (TESTER_MONGOCRYPT_DEFAULT);
250
- mongocrypt_ctx_t *ctx = NULL;
251
-
252
- /* No context, nothing to init. */
253
- ASSERT (!mongocrypt_ctx_rewrap_many_datakey_init (NULL, NULL));
254
-
255
- /* Filter argument required. */
256
- ctx = mongocrypt_ctx_new (crypt);
257
- ASSERT_FAILS (mongocrypt_ctx_rewrap_many_datakey_init (ctx, NULL),
258
- ctx,
259
- "filter must not be null");
260
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_ERROR);
261
- mongocrypt_ctx_destroy (ctx);
262
-
263
- /* Irrelevant options should trigger initialization error. */
264
- ctx = mongocrypt_ctx_new (crypt);
265
- ASSERT_OK (mongocrypt_ctx_setopt_key_alt_name (
266
- ctx, TEST_BSON ("{'keyAltName': 'test'}")),
267
- ctx);
268
- ASSERT_FAILS (
269
- mongocrypt_ctx_rewrap_many_datakey_init (ctx, TEST_BSON ("{}")),
270
- ctx,
271
- "key id and alt name prohibited");
272
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_ERROR);
273
- mongocrypt_ctx_destroy (ctx);
274
-
275
- /* rewrapManyDataKeyOpts.newProvider and rewrapManyDataKeyOpts.newMasterKey
276
- * should be provided via mongocrypt_ctx_setopt_key_encryption_key. */
277
- ctx = mongocrypt_ctx_new (crypt);
278
- ASSERT_OK (mongocrypt_ctx_setopt_key_encryption_key (
252
+ static void _test_rewrap_many_datakey_need_mongo_keys(_mongocrypt_tester_t *tester) {
253
+ mongocrypt_binary_t *const filter = TEST_BSON("{'keyAltName': {'$in': ['keyDocumentA', 'keyDocumentB']}}");
254
+
255
+ mongocrypt_t *const crypt = _mongocrypt_tester_mongocrypt(TESTER_MONGOCRYPT_DEFAULT);
256
+ mongocrypt_ctx_t *ctx = NULL;
257
+
258
+ /* Filter should be the same as what was provided in call to init. */
259
+ ctx = mongocrypt_ctx_new(crypt);
260
+ ASSERT_OK(mongocrypt_ctx_rewrap_many_datakey_init(ctx, filter), ctx);
261
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_MONGO_KEYS);
262
+ {
263
+ mongocrypt_binary_t *const op = mongocrypt_binary_new();
264
+ ASSERT_OK(mongocrypt_ctx_mongo_op(ctx, op), ctx);
265
+ ASSERT_MONGOCRYPT_BINARY_EQUAL_BSON(filter, op);
266
+ mongocrypt_binary_destroy(op);
267
+ }
268
+ mongocrypt_ctx_destroy(ctx);
269
+
270
+ /* No key documents is OK, no work to be done. */
271
+ ctx = mongocrypt_ctx_new(crypt);
272
+ ASSERT_OK(mongocrypt_ctx_rewrap_many_datakey_init(ctx, filter), ctx);
273
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_MONGO_KEYS);
274
+ ASSERT_OK(mongocrypt_ctx_mongo_done(ctx), ctx);
275
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_DONE);
276
+ mongocrypt_ctx_destroy(ctx);
277
+
278
+ /* Any number of key documents can be given. */
279
+ ctx = mongocrypt_ctx_new(crypt);
280
+ ASSERT_OK(mongocrypt_ctx_rewrap_many_datakey_init(ctx, filter), ctx);
281
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_MONGO_KEYS);
282
+ ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/rmd/key-document-a.json")), ctx);
283
+ ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/rmd/key-document-b.json")), ctx);
284
+ ASSERT_OK(mongocrypt_ctx_mongo_done(ctx), ctx);
285
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_KMS);
286
+ mongocrypt_ctx_destroy(ctx);
287
+
288
+ /* Key documents must not have duplicate key ID or alt names. */
289
+ ctx = mongocrypt_ctx_new(crypt);
290
+ ASSERT_OK(mongocrypt_ctx_rewrap_many_datakey_init(ctx, filter), ctx);
291
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_MONGO_KEYS);
292
+ ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/key-document-with-alt-name.json")), ctx);
293
+ ASSERT_FAILS(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/key-document-with-alt-name-duplicate-id.json")),
279
294
  ctx,
280
- TEST_BSON ("{'provider': 'aws',"
281
- " 'region': 'us-east-1',"
282
- " 'key': '" TEST_REWRAP_MASTER_KEY_ID_NEW "'}")),
283
- ctx);
284
- ASSERT_OK (mongocrypt_ctx_rewrap_many_datakey_init (ctx, TEST_BSON ("{}")),
285
- ctx);
286
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx),
287
- MONGOCRYPT_CTX_NEED_MONGO_KEYS);
288
- mongocrypt_ctx_destroy (ctx);
289
-
290
- /* Not providing rewrapManyDataKeyOpts is OK. */
291
- ctx = mongocrypt_ctx_new (crypt);
292
- ASSERT_OK (mongocrypt_ctx_rewrap_many_datakey_init (ctx, TEST_BSON ("{}")),
293
- ctx);
294
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx),
295
- MONGOCRYPT_CTX_NEED_MONGO_KEYS);
296
- mongocrypt_ctx_destroy (ctx);
295
+ "keys returned have duplicate keyAltNames or _id");
296
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_ERROR);
297
+ mongocrypt_ctx_destroy(ctx);
297
298
 
298
- mongocrypt_destroy (crypt);
299
+ mongocrypt_destroy(crypt);
299
300
  }
300
301
 
301
-
302
- static void
303
- _test_rewrap_many_datakey_need_mongo_keys (_mongocrypt_tester_t *tester)
304
- {
305
- mongocrypt_binary_t *const filter =
306
- TEST_BSON ("{'keyAltName': {'$in': ['keyDocumentA', 'keyDocumentB']}}");
307
-
308
- mongocrypt_t *const crypt =
309
- _mongocrypt_tester_mongocrypt (TESTER_MONGOCRYPT_DEFAULT);
310
- mongocrypt_ctx_t *ctx = NULL;
311
-
312
- /* Filter should be the same as what was provided in call to init. */
313
- ctx = mongocrypt_ctx_new (crypt);
314
- ASSERT_OK (mongocrypt_ctx_rewrap_many_datakey_init (ctx, filter), ctx);
315
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx),
316
- MONGOCRYPT_CTX_NEED_MONGO_KEYS);
317
- {
318
- mongocrypt_binary_t *const op = mongocrypt_binary_new ();
319
- ASSERT_OK (mongocrypt_ctx_mongo_op (ctx, op), ctx);
320
- ASSERT_MONGOCRYPT_BINARY_EQUAL_BSON (filter, op);
321
- mongocrypt_binary_destroy (op);
322
- }
323
- mongocrypt_ctx_destroy (ctx);
324
-
325
- /* No key documents is OK, no work to be done. */
326
- ctx = mongocrypt_ctx_new (crypt);
327
- ASSERT_OK (mongocrypt_ctx_rewrap_many_datakey_init (ctx, filter), ctx);
328
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx),
329
- MONGOCRYPT_CTX_NEED_MONGO_KEYS);
330
- ASSERT_OK (mongocrypt_ctx_mongo_done (ctx), ctx);
331
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_DONE);
332
- mongocrypt_ctx_destroy (ctx);
333
-
334
- /* Any number of key documents can be given. */
335
- ctx = mongocrypt_ctx_new (crypt);
336
- ASSERT_OK (mongocrypt_ctx_rewrap_many_datakey_init (ctx, filter), ctx);
337
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx),
338
- MONGOCRYPT_CTX_NEED_MONGO_KEYS);
339
- ASSERT_OK (mongocrypt_ctx_mongo_feed (
340
- ctx, TEST_FILE ("./test/data/rmd/key-document-a.json")),
341
- ctx);
342
- ASSERT_OK (mongocrypt_ctx_mongo_feed (
343
- ctx, TEST_FILE ("./test/data/rmd/key-document-b.json")),
344
- ctx);
345
- ASSERT_OK (mongocrypt_ctx_mongo_done (ctx), ctx);
346
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_NEED_KMS);
347
- mongocrypt_ctx_destroy (ctx);
348
-
349
- /* Key documents must not have duplicate key ID or alt names. */
350
- ctx = mongocrypt_ctx_new (crypt);
351
- ASSERT_OK (mongocrypt_ctx_rewrap_many_datakey_init (ctx, filter), ctx);
352
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx),
353
- MONGOCRYPT_CTX_NEED_MONGO_KEYS);
354
- ASSERT_OK (
355
- mongocrypt_ctx_mongo_feed (
356
- ctx, TEST_FILE ("./test/data/key-document-with-alt-name.json")),
357
- ctx);
358
- ASSERT_FAILS (
359
- mongocrypt_ctx_mongo_feed (
360
- ctx,
361
- TEST_FILE (
362
- "./test/data/key-document-with-alt-name-duplicate-id.json")),
363
- ctx,
364
- "keys returned have duplicate keyAltNames or _id");
365
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_ERROR);
366
- mongocrypt_ctx_destroy (ctx);
367
-
368
- mongocrypt_destroy (crypt);
302
+ static void _test_rewrap_many_datakey_need_kms_decrypt(_mongocrypt_tester_t *tester) {
303
+ mongocrypt_binary_t *const filter = TEST_BSON("{'keyAltName': {'$in': ['keyDocumentA', 'keyDocumentB']}}");
304
+
305
+ mongocrypt_t *crypt = _mongocrypt_tester_mongocrypt(TESTER_MONGOCRYPT_DEFAULT);
306
+ mongocrypt_ctx_t *ctx = NULL;
307
+ mongocrypt_kms_ctx_t *kms = NULL;
308
+
309
+ /* AWS */
310
+ ctx = mongocrypt_ctx_new(crypt);
311
+ ASSERT_OK(mongocrypt_ctx_rewrap_many_datakey_init(ctx, filter), ctx);
312
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_MONGO_KEYS);
313
+ ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/key-document-full.json")), ctx);
314
+ ASSERT_OK(mongocrypt_ctx_mongo_done(ctx), ctx);
315
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_KMS);
316
+ ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx)));
317
+ ASSERT_STREQUAL("aws", mongocrypt_kms_ctx_get_kms_provider(kms, NULL));
318
+ ASSERT_OK(!mongocrypt_ctx_next_kms_ctx(ctx), ctx);
319
+ mongocrypt_ctx_destroy(ctx);
320
+
321
+ /* Clear key cache. */
322
+ mongocrypt_destroy(crypt);
323
+ crypt = _mongocrypt_tester_mongocrypt(TESTER_MONGOCRYPT_DEFAULT);
324
+
325
+ /* Azure */
326
+ ctx = mongocrypt_ctx_new(crypt);
327
+ ASSERT_OK(mongocrypt_ctx_rewrap_many_datakey_init(ctx, filter), ctx);
328
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_MONGO_KEYS);
329
+ ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/key-document-azure.json")), ctx);
330
+ ASSERT_OK(mongocrypt_ctx_mongo_done(ctx), ctx);
331
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_KMS);
332
+ ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx)));
333
+ ASSERT_STREQUAL("azure", mongocrypt_kms_ctx_get_kms_provider(kms, NULL));
334
+ ASSERT_OK(!mongocrypt_ctx_next_kms_ctx(ctx), ctx);
335
+ mongocrypt_ctx_destroy(ctx);
336
+
337
+ /* Clear key cache. */
338
+ mongocrypt_destroy(crypt);
339
+ crypt = _mongocrypt_tester_mongocrypt(TESTER_MONGOCRYPT_DEFAULT);
340
+
341
+ /* GCP */
342
+ ctx = mongocrypt_ctx_new(crypt);
343
+ ASSERT_OK(mongocrypt_ctx_rewrap_many_datakey_init(ctx, filter), ctx);
344
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_MONGO_KEYS);
345
+ ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/key-document-gcp.json")), ctx);
346
+ ASSERT_OK(mongocrypt_ctx_mongo_done(ctx), ctx);
347
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_KMS);
348
+ ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx)));
349
+ ASSERT_STREQUAL("gcp", mongocrypt_kms_ctx_get_kms_provider(kms, NULL));
350
+ ASSERT_OK(!mongocrypt_ctx_next_kms_ctx(ctx), ctx);
351
+ mongocrypt_ctx_destroy(ctx);
352
+
353
+ /* Clear key cache. */
354
+ mongocrypt_destroy(crypt);
355
+ crypt = _mongocrypt_tester_mongocrypt(TESTER_MONGOCRYPT_DEFAULT);
356
+
357
+ /* KMIP */
358
+ ctx = mongocrypt_ctx_new(crypt);
359
+ ASSERT_OK(mongocrypt_ctx_rewrap_many_datakey_init(ctx, filter), ctx);
360
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_MONGO_KEYS);
361
+ ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/key-document-kmip.json")), ctx);
362
+ ASSERT_OK(mongocrypt_ctx_mongo_done(ctx), ctx);
363
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_KMS);
364
+ ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx)));
365
+ ASSERT_STREQUAL("kmip", mongocrypt_kms_ctx_get_kms_provider(kms, NULL));
366
+ ASSERT_OK(!mongocrypt_ctx_next_kms_ctx(ctx), ctx);
367
+ mongocrypt_ctx_destroy(ctx);
368
+
369
+ /* Clear key cache. */
370
+ mongocrypt_destroy(crypt);
371
+ crypt = _mongocrypt_tester_mongocrypt(TESTER_MONGOCRYPT_DEFAULT);
372
+
373
+ /* Local: no KMS required. */
374
+ ctx = mongocrypt_ctx_new(crypt);
375
+ ASSERT_OK(mongocrypt_ctx_rewrap_many_datakey_init(ctx, filter), ctx);
376
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_MONGO_KEYS);
377
+ ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/rmd/key-document-local.json")), ctx);
378
+ ASSERT_OK(mongocrypt_ctx_mongo_done(ctx), ctx);
379
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_READY);
380
+ mongocrypt_ctx_destroy(ctx);
381
+
382
+ /* Clear key cache. */
383
+ mongocrypt_destroy(crypt);
384
+ crypt = _mongocrypt_tester_mongocrypt(TESTER_MONGOCRYPT_DEFAULT);
385
+
386
+ /* Number of KMS requests should match number of keys that require it. */
387
+ ctx = mongocrypt_ctx_new(crypt);
388
+ ASSERT_OK(mongocrypt_ctx_rewrap_many_datakey_init(ctx, filter), ctx);
389
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_MONGO_KEYS);
390
+ ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/rmd/key-document-a.json")), ctx);
391
+ ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/rmd/key-document-b.json")), ctx);
392
+ ASSERT_OK(mongocrypt_ctx_mongo_done(ctx), ctx);
393
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_KMS);
394
+ ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx)));
395
+ ASSERT_STREQUAL("aws", mongocrypt_kms_ctx_get_kms_provider(kms, NULL));
396
+ ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx)));
397
+ ASSERT_STREQUAL("aws", mongocrypt_kms_ctx_get_kms_provider(kms, NULL));
398
+ ASSERT_OK(!mongocrypt_ctx_next_kms_ctx(ctx), ctx);
399
+ mongocrypt_ctx_destroy(ctx);
400
+
401
+ /* Ensure keys that don't require KMS do not request it. */
402
+ ctx = mongocrypt_ctx_new(crypt);
403
+ ASSERT_OK(mongocrypt_ctx_rewrap_many_datakey_init(ctx, filter), ctx);
404
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_MONGO_KEYS);
405
+ ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/rmd/key-document-a.json")), ctx);
406
+ ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/rmd/key-document-b.json")), ctx);
407
+ ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/rmd/key-document-local.json")), ctx);
408
+ ASSERT_OK(mongocrypt_ctx_mongo_done(ctx), ctx);
409
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_KMS);
410
+ ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx)));
411
+ ASSERT_STREQUAL("aws", mongocrypt_kms_ctx_get_kms_provider(kms, NULL));
412
+ ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx)));
413
+ ASSERT_STREQUAL("aws", mongocrypt_kms_ctx_get_kms_provider(kms, NULL));
414
+ ASSERT_OK(!mongocrypt_ctx_next_kms_ctx(ctx), ctx);
415
+ mongocrypt_ctx_destroy(ctx);
416
+
417
+ /* Clear key cache. */
418
+ mongocrypt_destroy(crypt);
419
+ crypt = _mongocrypt_tester_mongocrypt(TESTER_MONGOCRYPT_DEFAULT);
420
+
421
+ /* Ensure number of KMS requests matches number of keys that require it. */
422
+ ctx = mongocrypt_ctx_new(crypt);
423
+ ASSERT_OK(mongocrypt_ctx_rewrap_many_datakey_init(ctx, filter), ctx);
424
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_MONGO_KEYS);
425
+ ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/rmd/key-document-a.json")), ctx);
426
+ ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/rmd/key-document-b.json")), ctx);
427
+ ASSERT_OK(mongocrypt_ctx_mongo_done(ctx), ctx);
428
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_KMS);
429
+ ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx)));
430
+ /* Implementation detail: decryption KMS requests are issued in reverse order
431
+ * of provided key documents. */
432
+ _assert_aws_kms_endpoint(kms, "example.com:443");
433
+ ASSERT_OK(mongocrypt_kms_ctx_feed(kms, TEST_FILE("./test/data/rmd/kms-decrypt-reply-b.txt")), kms);
434
+ ASSERT(mongocrypt_kms_ctx_bytes_needed(kms) == 0);
435
+ ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx)));
436
+ _assert_aws_kms_endpoint(kms, "kms.us-east-1.amazonaws.com:443");
437
+ ASSERT_OK(mongocrypt_kms_ctx_feed(kms, TEST_FILE("./test/data/rmd/kms-decrypt-reply-a.txt")), kms);
438
+ ASSERT(mongocrypt_kms_ctx_bytes_needed(kms) == 0);
439
+ ASSERT_OK(!mongocrypt_ctx_next_kms_ctx(ctx), ctx);
440
+ ASSERT_OK(mongocrypt_ctx_kms_done(ctx), ctx);
441
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_KMS);
442
+ mongocrypt_ctx_destroy(ctx);
443
+
444
+ /* Clear key cache. */
445
+ mongocrypt_destroy(crypt);
446
+ crypt = _mongocrypt_tester_mongocrypt(TESTER_MONGOCRYPT_DEFAULT);
447
+
448
+ /* Ensure all KMS requests have a corresponding KMS response. */
449
+ ctx = mongocrypt_ctx_new(crypt);
450
+ ASSERT_OK(mongocrypt_ctx_rewrap_many_datakey_init(ctx, filter), ctx);
451
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_MONGO_KEYS);
452
+ ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/rmd/key-document-a.json")), ctx);
453
+ ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/rmd/key-document-b.json")), ctx);
454
+ ASSERT_OK(mongocrypt_ctx_mongo_done(ctx), ctx);
455
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_KMS);
456
+ ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx)));
457
+ ASSERT_OK(mongocrypt_kms_ctx_feed(kms, TEST_FILE("./test/data/rmd/kms-decrypt-reply-b.txt")), kms);
458
+ ASSERT(mongocrypt_kms_ctx_bytes_needed(kms) == 0);
459
+ ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx)));
460
+ ASSERT(mongocrypt_kms_ctx_bytes_needed(kms) > 0); /* "Oops." */
461
+ ASSERT_OK(!mongocrypt_ctx_next_kms_ctx(ctx), ctx);
462
+ ASSERT_FAILS(mongocrypt_ctx_kms_done(ctx), ctx, "KMS response unfinished");
463
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_ERROR);
464
+ mongocrypt_ctx_destroy(ctx);
465
+
466
+ /* Clear key cache. */
467
+ mongocrypt_destroy(crypt);
468
+ crypt = _mongocrypt_tester_mongocrypt(TESTER_MONGOCRYPT_DEFAULT);
469
+
470
+ /* Skip KMS for keys with cached decrypted key material. */
471
+ ctx = mongocrypt_ctx_new(crypt);
472
+ ASSERT_OK(mongocrypt_ctx_rewrap_many_datakey_init(ctx, filter), ctx);
473
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_MONGO_KEYS);
474
+ ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/rmd/key-document-b.json")), ctx);
475
+ ASSERT_OK(mongocrypt_ctx_mongo_done(ctx), ctx);
476
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_KMS);
477
+ ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx)));
478
+ /* Cache decrypted key material for datakey B. */
479
+ ASSERT_OK(mongocrypt_kms_ctx_feed(kms, TEST_FILE("./test/data/rmd/kms-decrypt-reply-b.txt")), kms);
480
+ ASSERT(mongocrypt_kms_ctx_bytes_needed(kms) == 0);
481
+ ASSERT_OK(mongocrypt_ctx_kms_done(ctx), ctx);
482
+ mongocrypt_ctx_destroy(ctx);
483
+ ctx = mongocrypt_ctx_new(crypt);
484
+ ASSERT_OK(mongocrypt_ctx_rewrap_many_datakey_init(ctx, filter), ctx);
485
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_MONGO_KEYS);
486
+ ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/rmd/key-document-a.json")), ctx);
487
+ ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/rmd/key-document-b.json")), ctx);
488
+ ASSERT_OK(mongocrypt_ctx_mongo_done(ctx), ctx);
489
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_KMS);
490
+ /* Only datakey A should make a KMS request. */
491
+ ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx)));
492
+ _assert_aws_kms_endpoint(kms, "kms.us-east-1.amazonaws.com:443");
493
+ ASSERT_OK(!mongocrypt_ctx_next_kms_ctx(ctx), ctx);
494
+ mongocrypt_ctx_destroy(ctx);
495
+
496
+ mongocrypt_destroy(crypt);
369
497
  }
370
498
 
371
-
372
- static void
373
- _test_rewrap_many_datakey_need_kms_decrypt (_mongocrypt_tester_t *tester)
374
- {
375
- mongocrypt_binary_t *const filter =
376
- TEST_BSON ("{'keyAltName': {'$in': ['keyDocumentA', 'keyDocumentB']}}");
377
-
378
- mongocrypt_t *crypt =
379
- _mongocrypt_tester_mongocrypt (TESTER_MONGOCRYPT_DEFAULT);
380
- mongocrypt_ctx_t *ctx = NULL;
381
- mongocrypt_kms_ctx_t *kms = NULL;
382
-
383
- /* AWS */
384
- ctx = mongocrypt_ctx_new (crypt);
385
- ASSERT_OK (mongocrypt_ctx_rewrap_many_datakey_init (ctx, filter), ctx);
386
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx),
387
- MONGOCRYPT_CTX_NEED_MONGO_KEYS);
388
- ASSERT_OK (mongocrypt_ctx_mongo_feed (
389
- ctx, TEST_FILE ("./test/data/key-document-full.json")),
390
- ctx);
391
- ASSERT_OK (mongocrypt_ctx_mongo_done (ctx), ctx);
392
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_NEED_KMS);
393
- ASSERT ((kms = mongocrypt_ctx_next_kms_ctx (ctx)));
394
- ASSERT_STREQUAL ("aws", mongocrypt_kms_ctx_get_kms_provider (kms, NULL));
395
- ASSERT_OK (!mongocrypt_ctx_next_kms_ctx (ctx), ctx);
396
- mongocrypt_ctx_destroy (ctx);
397
-
398
- /* Clear key cache. */
399
- mongocrypt_destroy (crypt);
400
- crypt = _mongocrypt_tester_mongocrypt (TESTER_MONGOCRYPT_DEFAULT);
401
-
402
- /* Azure */
403
- ctx = mongocrypt_ctx_new (crypt);
404
- ASSERT_OK (mongocrypt_ctx_rewrap_many_datakey_init (ctx, filter), ctx);
405
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx),
406
- MONGOCRYPT_CTX_NEED_MONGO_KEYS);
407
- ASSERT_OK (mongocrypt_ctx_mongo_feed (
408
- ctx, TEST_FILE ("./test/data/key-document-azure.json")),
409
- ctx);
410
- ASSERT_OK (mongocrypt_ctx_mongo_done (ctx), ctx);
411
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_NEED_KMS);
412
- ASSERT ((kms = mongocrypt_ctx_next_kms_ctx (ctx)));
413
- ASSERT_STREQUAL ("azure", mongocrypt_kms_ctx_get_kms_provider (kms, NULL));
414
- ASSERT_OK (!mongocrypt_ctx_next_kms_ctx (ctx), ctx);
415
- mongocrypt_ctx_destroy (ctx);
416
-
417
- /* Clear key cache. */
418
- mongocrypt_destroy (crypt);
419
- crypt = _mongocrypt_tester_mongocrypt (TESTER_MONGOCRYPT_DEFAULT);
420
-
421
- /* GCP */
422
- ctx = mongocrypt_ctx_new (crypt);
423
- ASSERT_OK (mongocrypt_ctx_rewrap_many_datakey_init (ctx, filter), ctx);
424
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx),
425
- MONGOCRYPT_CTX_NEED_MONGO_KEYS);
426
- ASSERT_OK (mongocrypt_ctx_mongo_feed (
427
- ctx, TEST_FILE ("./test/data/key-document-gcp.json")),
428
- ctx);
429
- ASSERT_OK (mongocrypt_ctx_mongo_done (ctx), ctx);
430
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_NEED_KMS);
431
- ASSERT ((kms = mongocrypt_ctx_next_kms_ctx (ctx)));
432
- ASSERT_STREQUAL ("gcp", mongocrypt_kms_ctx_get_kms_provider (kms, NULL));
433
- ASSERT_OK (!mongocrypt_ctx_next_kms_ctx (ctx), ctx);
434
- mongocrypt_ctx_destroy (ctx);
435
-
436
- /* Clear key cache. */
437
- mongocrypt_destroy (crypt);
438
- crypt = _mongocrypt_tester_mongocrypt (TESTER_MONGOCRYPT_DEFAULT);
439
-
440
- /* KMIP */
441
- ctx = mongocrypt_ctx_new (crypt);
442
- ASSERT_OK (mongocrypt_ctx_rewrap_many_datakey_init (ctx, filter), ctx);
443
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx),
444
- MONGOCRYPT_CTX_NEED_MONGO_KEYS);
445
- ASSERT_OK (mongocrypt_ctx_mongo_feed (
446
- ctx, TEST_FILE ("./test/data/key-document-kmip.json")),
447
- ctx);
448
- ASSERT_OK (mongocrypt_ctx_mongo_done (ctx), ctx);
449
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_NEED_KMS);
450
- ASSERT ((kms = mongocrypt_ctx_next_kms_ctx (ctx)));
451
- ASSERT_STREQUAL ("kmip", mongocrypt_kms_ctx_get_kms_provider (kms, NULL));
452
- ASSERT_OK (!mongocrypt_ctx_next_kms_ctx (ctx), ctx);
453
- mongocrypt_ctx_destroy (ctx);
454
-
455
- /* Clear key cache. */
456
- mongocrypt_destroy (crypt);
457
- crypt = _mongocrypt_tester_mongocrypt (TESTER_MONGOCRYPT_DEFAULT);
458
-
459
- /* Local: no KMS required. */
460
- ctx = mongocrypt_ctx_new (crypt);
461
- ASSERT_OK (mongocrypt_ctx_rewrap_many_datakey_init (ctx, filter), ctx);
462
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx),
463
- MONGOCRYPT_CTX_NEED_MONGO_KEYS);
464
- ASSERT_OK (mongocrypt_ctx_mongo_feed (
465
- ctx, TEST_FILE ("./test/data/rmd/key-document-local.json")),
466
- ctx);
467
- ASSERT_OK (mongocrypt_ctx_mongo_done (ctx), ctx);
468
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_READY);
469
- mongocrypt_ctx_destroy (ctx);
470
-
471
- /* Clear key cache. */
472
- mongocrypt_destroy (crypt);
473
- crypt = _mongocrypt_tester_mongocrypt (TESTER_MONGOCRYPT_DEFAULT);
474
-
475
- /* Number of KMS requests should match number of keys that require it. */
476
- ctx = mongocrypt_ctx_new (crypt);
477
- ASSERT_OK (mongocrypt_ctx_rewrap_many_datakey_init (ctx, filter), ctx);
478
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx),
479
- MONGOCRYPT_CTX_NEED_MONGO_KEYS);
480
- ASSERT_OK (mongocrypt_ctx_mongo_feed (
481
- ctx, TEST_FILE ("./test/data/rmd/key-document-a.json")),
482
- ctx);
483
- ASSERT_OK (mongocrypt_ctx_mongo_feed (
484
- ctx, TEST_FILE ("./test/data/rmd/key-document-b.json")),
485
- ctx);
486
- ASSERT_OK (mongocrypt_ctx_mongo_done (ctx), ctx);
487
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_NEED_KMS);
488
- ASSERT ((kms = mongocrypt_ctx_next_kms_ctx (ctx)));
489
- ASSERT_STREQUAL ("aws", mongocrypt_kms_ctx_get_kms_provider (kms, NULL));
490
- ASSERT ((kms = mongocrypt_ctx_next_kms_ctx (ctx)));
491
- ASSERT_STREQUAL ("aws", mongocrypt_kms_ctx_get_kms_provider (kms, NULL));
492
- ASSERT_OK (!mongocrypt_ctx_next_kms_ctx (ctx), ctx);
493
- mongocrypt_ctx_destroy (ctx);
494
-
495
- /* Ensure keys that don't require KMS do not request it. */
496
- ctx = mongocrypt_ctx_new (crypt);
497
- ASSERT_OK (mongocrypt_ctx_rewrap_many_datakey_init (ctx, filter), ctx);
498
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx),
499
- MONGOCRYPT_CTX_NEED_MONGO_KEYS);
500
- ASSERT_OK (mongocrypt_ctx_mongo_feed (
501
- ctx, TEST_FILE ("./test/data/rmd/key-document-a.json")),
502
- ctx);
503
- ASSERT_OK (mongocrypt_ctx_mongo_feed (
504
- ctx, TEST_FILE ("./test/data/rmd/key-document-b.json")),
505
- ctx);
506
- ASSERT_OK (mongocrypt_ctx_mongo_feed (
507
- ctx, TEST_FILE ("./test/data/rmd/key-document-local.json")),
508
- ctx);
509
- ASSERT_OK (mongocrypt_ctx_mongo_done (ctx), ctx);
510
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_NEED_KMS);
511
- ASSERT ((kms = mongocrypt_ctx_next_kms_ctx (ctx)));
512
- ASSERT_STREQUAL ("aws", mongocrypt_kms_ctx_get_kms_provider (kms, NULL));
513
- ASSERT ((kms = mongocrypt_ctx_next_kms_ctx (ctx)));
514
- ASSERT_STREQUAL ("aws", mongocrypt_kms_ctx_get_kms_provider (kms, NULL));
515
- ASSERT_OK (!mongocrypt_ctx_next_kms_ctx (ctx), ctx);
516
- mongocrypt_ctx_destroy (ctx);
517
-
518
- /* Clear key cache. */
519
- mongocrypt_destroy (crypt);
520
- crypt = _mongocrypt_tester_mongocrypt (TESTER_MONGOCRYPT_DEFAULT);
521
-
522
- /* Ensure number of KMS requests matches number of keys that require it. */
523
- ctx = mongocrypt_ctx_new (crypt);
524
- ASSERT_OK (mongocrypt_ctx_rewrap_many_datakey_init (ctx, filter), ctx);
525
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx),
526
- MONGOCRYPT_CTX_NEED_MONGO_KEYS);
527
- ASSERT_OK (mongocrypt_ctx_mongo_feed (
528
- ctx, TEST_FILE ("./test/data/rmd/key-document-a.json")),
529
- ctx);
530
- ASSERT_OK (mongocrypt_ctx_mongo_feed (
531
- ctx, TEST_FILE ("./test/data/rmd/key-document-b.json")),
532
- ctx);
533
- ASSERT_OK (mongocrypt_ctx_mongo_done (ctx), ctx);
534
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_NEED_KMS);
535
- ASSERT ((kms = mongocrypt_ctx_next_kms_ctx (ctx)));
536
- /* Implementation detail: decryption KMS requests are issued in reverse order
537
- * of provided key documents. */
538
- _assert_aws_kms_endpoint (kms, "example.com:443");
539
- ASSERT_OK (mongocrypt_kms_ctx_feed (
540
- kms, TEST_FILE ("./test/data/rmd/kms-decrypt-reply-b.txt")),
541
- kms);
542
- ASSERT (mongocrypt_kms_ctx_bytes_needed (kms) == 0);
543
- ASSERT ((kms = mongocrypt_ctx_next_kms_ctx (ctx)));
544
- _assert_aws_kms_endpoint (kms, "kms.us-east-1.amazonaws.com:443");
545
- ASSERT_OK (mongocrypt_kms_ctx_feed (
546
- kms, TEST_FILE ("./test/data/rmd/kms-decrypt-reply-a.txt")),
547
- kms);
548
- ASSERT (mongocrypt_kms_ctx_bytes_needed (kms) == 0);
549
- ASSERT_OK (!mongocrypt_ctx_next_kms_ctx (ctx), ctx);
550
- ASSERT_OK (mongocrypt_ctx_kms_done (ctx), ctx);
551
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_NEED_KMS);
552
- mongocrypt_ctx_destroy (ctx);
553
-
554
- /* Clear key cache. */
555
- mongocrypt_destroy (crypt);
556
- crypt = _mongocrypt_tester_mongocrypt (TESTER_MONGOCRYPT_DEFAULT);
557
-
558
- /* Ensure all KMS requests have a corresponding KMS response. */
559
- ctx = mongocrypt_ctx_new (crypt);
560
- ASSERT_OK (mongocrypt_ctx_rewrap_many_datakey_init (ctx, filter), ctx);
561
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx),
562
- MONGOCRYPT_CTX_NEED_MONGO_KEYS);
563
- ASSERT_OK (mongocrypt_ctx_mongo_feed (
564
- ctx, TEST_FILE ("./test/data/rmd/key-document-a.json")),
565
- ctx);
566
- ASSERT_OK (mongocrypt_ctx_mongo_feed (
567
- ctx, TEST_FILE ("./test/data/rmd/key-document-b.json")),
568
- ctx);
569
- ASSERT_OK (mongocrypt_ctx_mongo_done (ctx), ctx);
570
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_NEED_KMS);
571
- ASSERT ((kms = mongocrypt_ctx_next_kms_ctx (ctx)));
572
- ASSERT_OK (mongocrypt_kms_ctx_feed (
573
- kms, TEST_FILE ("./test/data/rmd/kms-decrypt-reply-b.txt")),
574
- kms);
575
- ASSERT (mongocrypt_kms_ctx_bytes_needed (kms) == 0);
576
- ASSERT ((kms = mongocrypt_ctx_next_kms_ctx (ctx)));
577
- ASSERT (mongocrypt_kms_ctx_bytes_needed (kms) > 0); /* "Oops." */
578
- ASSERT_OK (!mongocrypt_ctx_next_kms_ctx (ctx), ctx);
579
- ASSERT_FAILS (mongocrypt_ctx_kms_done (ctx), ctx, "KMS response unfinished");
580
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_ERROR);
581
- mongocrypt_ctx_destroy (ctx);
582
-
583
- /* Clear key cache. */
584
- mongocrypt_destroy (crypt);
585
- crypt = _mongocrypt_tester_mongocrypt (TESTER_MONGOCRYPT_DEFAULT);
586
-
587
- /* Skip KMS for keys with cached decrypted key material. */
588
- ctx = mongocrypt_ctx_new (crypt);
589
- ASSERT_OK (mongocrypt_ctx_rewrap_many_datakey_init (ctx, filter), ctx);
590
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx),
591
- MONGOCRYPT_CTX_NEED_MONGO_KEYS);
592
- ASSERT_OK (mongocrypt_ctx_mongo_feed (
593
- ctx, TEST_FILE ("./test/data/rmd/key-document-b.json")),
594
- ctx);
595
- ASSERT_OK (mongocrypt_ctx_mongo_done (ctx), ctx);
596
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_NEED_KMS);
597
- ASSERT ((kms = mongocrypt_ctx_next_kms_ctx (ctx)));
598
- /* Cache decrypted key material for datakey B. */
599
- ASSERT_OK (mongocrypt_kms_ctx_feed (
600
- kms, TEST_FILE ("./test/data/rmd/kms-decrypt-reply-b.txt")),
601
- kms);
602
- ASSERT (mongocrypt_kms_ctx_bytes_needed (kms) == 0);
603
- ASSERT_OK (mongocrypt_ctx_kms_done (ctx), ctx);
604
- mongocrypt_ctx_destroy (ctx);
605
- ctx = mongocrypt_ctx_new (crypt);
606
- ASSERT_OK (mongocrypt_ctx_rewrap_many_datakey_init (ctx, filter), ctx);
607
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx),
608
- MONGOCRYPT_CTX_NEED_MONGO_KEYS);
609
- ASSERT_OK (mongocrypt_ctx_mongo_feed (
610
- ctx, TEST_FILE ("./test/data/rmd/key-document-a.json")),
611
- ctx);
612
- ASSERT_OK (mongocrypt_ctx_mongo_feed (
613
- ctx, TEST_FILE ("./test/data/rmd/key-document-b.json")),
614
- ctx);
615
- ASSERT_OK (mongocrypt_ctx_mongo_done (ctx), ctx);
616
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_NEED_KMS);
617
- /* Only datakey A should make a KMS request. */
618
- ASSERT ((kms = mongocrypt_ctx_next_kms_ctx (ctx)));
619
- _assert_aws_kms_endpoint (kms, "kms.us-east-1.amazonaws.com:443");
620
- ASSERT_OK (!mongocrypt_ctx_next_kms_ctx (ctx), ctx);
621
- mongocrypt_ctx_destroy (ctx);
622
-
623
- mongocrypt_destroy (crypt);
624
- }
625
-
626
-
627
- static void
628
- _test_rewrap_many_datakey_need_kms_encrypt (_mongocrypt_tester_t *tester)
629
- {
630
- mongocrypt_binary_t *const filter =
631
- TEST_BSON ("{'keyAltName': {'$in': ['keyDocumentA', 'keyDocumentB']}}");
632
-
633
- mongocrypt_t *const crypt =
634
- _mongocrypt_tester_mongocrypt (TESTER_MONGOCRYPT_DEFAULT);
635
- mongocrypt_ctx_t *ctx = NULL;
636
- mongocrypt_kms_ctx_t *kms = NULL;
637
-
638
- /* If no new provider is given, encryption should reuse current KMS provider
639
- * for each key. */
640
- ctx = mongocrypt_ctx_new (crypt);
641
- ASSERT_OK (mongocrypt_ctx_rewrap_many_datakey_init (ctx, filter), ctx);
642
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx),
643
- MONGOCRYPT_CTX_NEED_MONGO_KEYS);
644
- ASSERT_OK (mongocrypt_ctx_mongo_feed (
645
- ctx, TEST_FILE ("./test/data/rmd/key-document-a.json")),
646
- ctx);
647
- ASSERT_OK (mongocrypt_ctx_mongo_feed (
648
- ctx, TEST_FILE ("./test/data/rmd/key-document-b.json")),
649
- ctx);
650
- ASSERT_OK (mongocrypt_ctx_mongo_done (ctx), ctx);
651
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_NEED_KMS);
652
- /* These decrypt replies should cache key material used by later blocks. */
653
- ASSERT ((kms = mongocrypt_ctx_next_kms_ctx (ctx)));
654
- ASSERT_OK (mongocrypt_kms_ctx_feed (
655
- kms, TEST_FILE ("./test/data/rmd/kms-decrypt-reply-b.txt")),
656
- kms);
657
- ASSERT (mongocrypt_kms_ctx_bytes_needed (kms) == 0);
658
- ASSERT ((kms = mongocrypt_ctx_next_kms_ctx (ctx)));
659
- ASSERT_OK (mongocrypt_kms_ctx_feed (
660
- kms, TEST_FILE ("./test/data/rmd/kms-decrypt-reply-a.txt")),
661
- kms);
662
- ASSERT (mongocrypt_kms_ctx_bytes_needed (kms) == 0);
663
- ASSERT_OK (!mongocrypt_ctx_next_kms_ctx (ctx), ctx);
664
- ASSERT_OK (mongocrypt_ctx_kms_done (ctx), ctx);
665
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_NEED_KMS);
666
- /* Implementation detail: encryption KMS requests are issued in same order as
667
- * provided key documents. */
668
- ASSERT ((kms = mongocrypt_ctx_next_kms_ctx (ctx)));
669
- _assert_aws_kms_endpoint (kms, "kms.us-east-1.amazonaws.com:443");
670
- ASSERT_OK (mongocrypt_kms_ctx_feed (
671
- kms, TEST_FILE ("./test/data/rmd/kms-encrypt-reply-a.txt")),
672
- kms);
673
- ASSERT (mongocrypt_kms_ctx_bytes_needed (kms) == 0);
674
- ASSERT ((kms = mongocrypt_ctx_next_kms_ctx (ctx)));
675
- _assert_aws_kms_endpoint (kms, "example.com:443");
676
- ASSERT_OK (mongocrypt_kms_ctx_feed (
677
- kms, TEST_FILE ("./test/data/rmd/kms-encrypt-reply-b.txt")),
678
- kms);
679
- ASSERT (mongocrypt_kms_ctx_bytes_needed (kms) == 0);
680
- ASSERT_OK (!mongocrypt_ctx_next_kms_ctx (ctx), ctx);
681
- ASSERT_OK (mongocrypt_ctx_kms_done (ctx), ctx);
682
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_READY);
683
- mongocrypt_ctx_destroy (ctx);
684
-
685
- /* If new provider is given, encryption should use new KMS provider for all
686
- * keys. */
687
- ctx = mongocrypt_ctx_new (crypt);
688
- ASSERT_OK (mongocrypt_ctx_setopt_key_encryption_key (
689
- ctx,
690
- TEST_BSON ("{'provider': 'aws',"
691
- " 'region': 'us-east-2',"
692
- " 'key': '" TEST_REWRAP_MASTER_KEY_ID_NEW "'}")),
693
- ctx);
694
- ASSERT_OK (mongocrypt_ctx_rewrap_many_datakey_init (ctx, filter), ctx);
695
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx),
696
- MONGOCRYPT_CTX_NEED_MONGO_KEYS);
697
- ASSERT_OK (mongocrypt_ctx_mongo_feed (
698
- ctx, TEST_FILE ("./test/data/rmd/key-document-a.json")),
699
- ctx);
700
- ASSERT_OK (mongocrypt_ctx_mongo_feed (
701
- ctx, TEST_FILE ("./test/data/rmd/key-document-b.json")),
702
- ctx);
703
- ASSERT_OK (mongocrypt_ctx_mongo_done (ctx), ctx);
704
- /* Skip decryption, key material should have been cached. */
705
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_NEED_KMS);
706
- ASSERT ((kms = mongocrypt_ctx_next_kms_ctx (ctx)));
707
- _assert_aws_kms_endpoint (kms, "kms.us-east-2.amazonaws.com:443");
708
- ASSERT ((kms = mongocrypt_ctx_next_kms_ctx (ctx)));
709
- _assert_aws_kms_endpoint (kms, "kms.us-east-2.amazonaws.com:443");
710
- ASSERT_OK (!mongocrypt_ctx_next_kms_ctx (ctx), ctx);
711
- mongocrypt_ctx_destroy (ctx);
712
-
713
- /* If no encryption KMS required, should skip straight to READY state. */
714
- ctx = mongocrypt_ctx_new (crypt);
715
- ASSERT_OK (mongocrypt_ctx_setopt_key_encryption_key (
716
- ctx, TEST_BSON ("{'provider': 'local'}")),
717
- ctx);
718
- ASSERT_OK (mongocrypt_ctx_rewrap_many_datakey_init (ctx, filter), ctx);
719
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx),
720
- MONGOCRYPT_CTX_NEED_MONGO_KEYS);
721
- ASSERT_OK (mongocrypt_ctx_mongo_feed (
722
- ctx, TEST_FILE ("./test/data/rmd/key-document-a.json")),
723
- ctx);
724
- ASSERT_OK (mongocrypt_ctx_mongo_feed (
725
- ctx, TEST_FILE ("./test/data/rmd/key-document-b.json")),
726
- ctx);
727
- ASSERT_OK (mongocrypt_ctx_mongo_done (ctx), ctx);
728
- /* Skip decryption, key material should have been cached. */
729
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_READY);
730
- mongocrypt_ctx_destroy (ctx);
731
-
732
- /* Ensure all KMS requests have a corresponding KMS response. */
733
- ctx = mongocrypt_ctx_new (crypt);
734
- ASSERT_OK (mongocrypt_ctx_setopt_key_encryption_key (
735
- ctx,
736
- TEST_BSON ("{'provider': 'aws',"
737
- " 'region': 'us-east-1',"
738
- " 'key': '" TEST_REWRAP_MASTER_KEY_ID_NEW "'}")),
739
- ctx);
740
- ASSERT_OK (mongocrypt_ctx_rewrap_many_datakey_init (ctx, filter), ctx);
741
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx),
742
- MONGOCRYPT_CTX_NEED_MONGO_KEYS);
743
- ASSERT_OK (mongocrypt_ctx_mongo_feed (
744
- ctx, TEST_FILE ("./test/data/rmd/key-document-a.json")),
745
- ctx);
746
- ASSERT_OK (mongocrypt_ctx_mongo_feed (
747
- ctx, TEST_FILE ("./test/data/rmd/key-document-b.json")),
748
- ctx);
749
- ASSERT_OK (mongocrypt_ctx_mongo_done (ctx), ctx);
750
- /* Skip decryption, key material should have been cached. */
751
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_NEED_KMS);
752
- ASSERT ((kms = mongocrypt_ctx_next_kms_ctx (ctx)));
753
- ASSERT_OK (mongocrypt_kms_ctx_feed (
754
- kms, TEST_FILE ("./test/data/rmd/kms-encrypt-reply-a.txt")),
755
- kms);
756
- ASSERT ((kms = mongocrypt_ctx_next_kms_ctx (ctx)));
757
- ASSERT (mongocrypt_kms_ctx_bytes_needed (kms) > 0); /* "Oops." */
758
- ASSERT_OK (!mongocrypt_ctx_next_kms_ctx (ctx), ctx);
759
- ASSERT_FAILS (mongocrypt_ctx_kms_done (ctx), ctx, "KMS response unfinished");
760
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_ERROR);
761
- mongocrypt_ctx_destroy (ctx);
762
-
763
- mongocrypt_destroy (crypt);
499
+ static void _test_rewrap_many_datakey_need_kms_encrypt(_mongocrypt_tester_t *tester) {
500
+ mongocrypt_binary_t *const filter = TEST_BSON("{'keyAltName': {'$in': ['keyDocumentA', 'keyDocumentB']}}");
501
+
502
+ mongocrypt_t *const crypt = _mongocrypt_tester_mongocrypt(TESTER_MONGOCRYPT_DEFAULT);
503
+ mongocrypt_ctx_t *ctx = NULL;
504
+ mongocrypt_kms_ctx_t *kms = NULL;
505
+
506
+ /* If no new provider is given, encryption should reuse current KMS provider
507
+ * for each key. */
508
+ ctx = mongocrypt_ctx_new(crypt);
509
+ ASSERT_OK(mongocrypt_ctx_rewrap_many_datakey_init(ctx, filter), ctx);
510
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_MONGO_KEYS);
511
+ ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/rmd/key-document-a.json")), ctx);
512
+ ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/rmd/key-document-b.json")), ctx);
513
+ ASSERT_OK(mongocrypt_ctx_mongo_done(ctx), ctx);
514
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_KMS);
515
+ /* These decrypt replies should cache key material used by later blocks. */
516
+ ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx)));
517
+ ASSERT_OK(mongocrypt_kms_ctx_feed(kms, TEST_FILE("./test/data/rmd/kms-decrypt-reply-b.txt")), kms);
518
+ ASSERT(mongocrypt_kms_ctx_bytes_needed(kms) == 0);
519
+ ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx)));
520
+ ASSERT_OK(mongocrypt_kms_ctx_feed(kms, TEST_FILE("./test/data/rmd/kms-decrypt-reply-a.txt")), kms);
521
+ ASSERT(mongocrypt_kms_ctx_bytes_needed(kms) == 0);
522
+ ASSERT_OK(!mongocrypt_ctx_next_kms_ctx(ctx), ctx);
523
+ ASSERT_OK(mongocrypt_ctx_kms_done(ctx), ctx);
524
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_KMS);
525
+ /* Implementation detail: encryption KMS requests are issued in same order as
526
+ * provided key documents. */
527
+ ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx)));
528
+ _assert_aws_kms_endpoint(kms, "kms.us-east-1.amazonaws.com:443");
529
+ ASSERT_OK(mongocrypt_kms_ctx_feed(kms, TEST_FILE("./test/data/rmd/kms-encrypt-reply-a.txt")), kms);
530
+ ASSERT(mongocrypt_kms_ctx_bytes_needed(kms) == 0);
531
+ ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx)));
532
+ _assert_aws_kms_endpoint(kms, "example.com:443");
533
+ ASSERT_OK(mongocrypt_kms_ctx_feed(kms, TEST_FILE("./test/data/rmd/kms-encrypt-reply-b.txt")), kms);
534
+ ASSERT(mongocrypt_kms_ctx_bytes_needed(kms) == 0);
535
+ ASSERT_OK(!mongocrypt_ctx_next_kms_ctx(ctx), ctx);
536
+ ASSERT_OK(mongocrypt_ctx_kms_done(ctx), ctx);
537
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_READY);
538
+ mongocrypt_ctx_destroy(ctx);
539
+
540
+ /* If new provider is given, encryption should use new KMS provider for all
541
+ * keys. */
542
+ ctx = mongocrypt_ctx_new(crypt);
543
+ ASSERT_OK(mongocrypt_ctx_setopt_key_encryption_key(ctx,
544
+ TEST_BSON("{'provider': 'aws',"
545
+ " 'region': 'us-east-2',"
546
+ " 'key': '" TEST_REWRAP_MASTER_KEY_ID_NEW "'}")),
547
+ ctx);
548
+ ASSERT_OK(mongocrypt_ctx_rewrap_many_datakey_init(ctx, filter), ctx);
549
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_MONGO_KEYS);
550
+ ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/rmd/key-document-a.json")), ctx);
551
+ ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/rmd/key-document-b.json")), ctx);
552
+ ASSERT_OK(mongocrypt_ctx_mongo_done(ctx), ctx);
553
+ /* Skip decryption, key material should have been cached. */
554
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_KMS);
555
+ ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx)));
556
+ _assert_aws_kms_endpoint(kms, "kms.us-east-2.amazonaws.com:443");
557
+ ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx)));
558
+ _assert_aws_kms_endpoint(kms, "kms.us-east-2.amazonaws.com:443");
559
+ ASSERT_OK(!mongocrypt_ctx_next_kms_ctx(ctx), ctx);
560
+ mongocrypt_ctx_destroy(ctx);
561
+
562
+ /* If no encryption KMS required, should skip straight to READY state. */
563
+ ctx = mongocrypt_ctx_new(crypt);
564
+ ASSERT_OK(mongocrypt_ctx_setopt_key_encryption_key(ctx, TEST_BSON("{'provider': 'local'}")), ctx);
565
+ ASSERT_OK(mongocrypt_ctx_rewrap_many_datakey_init(ctx, filter), ctx);
566
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_MONGO_KEYS);
567
+ ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/rmd/key-document-a.json")), ctx);
568
+ ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/rmd/key-document-b.json")), ctx);
569
+ ASSERT_OK(mongocrypt_ctx_mongo_done(ctx), ctx);
570
+ /* Skip decryption, key material should have been cached. */
571
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_READY);
572
+ mongocrypt_ctx_destroy(ctx);
573
+
574
+ /* Ensure all KMS requests have a corresponding KMS response. */
575
+ ctx = mongocrypt_ctx_new(crypt);
576
+ ASSERT_OK(mongocrypt_ctx_setopt_key_encryption_key(ctx,
577
+ TEST_BSON("{'provider': 'aws',"
578
+ " 'region': 'us-east-1',"
579
+ " 'key': '" TEST_REWRAP_MASTER_KEY_ID_NEW "'}")),
580
+ ctx);
581
+ ASSERT_OK(mongocrypt_ctx_rewrap_many_datakey_init(ctx, filter), ctx);
582
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_MONGO_KEYS);
583
+ ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/rmd/key-document-a.json")), ctx);
584
+ ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/rmd/key-document-b.json")), ctx);
585
+ ASSERT_OK(mongocrypt_ctx_mongo_done(ctx), ctx);
586
+ /* Skip decryption, key material should have been cached. */
587
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_KMS);
588
+ ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx)));
589
+ ASSERT_OK(mongocrypt_kms_ctx_feed(kms, TEST_FILE("./test/data/rmd/kms-encrypt-reply-a.txt")), kms);
590
+ ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx)));
591
+ ASSERT(mongocrypt_kms_ctx_bytes_needed(kms) > 0); /* "Oops." */
592
+ ASSERT_OK(!mongocrypt_ctx_next_kms_ctx(ctx), ctx);
593
+ ASSERT_FAILS(mongocrypt_ctx_kms_done(ctx), ctx, "KMS response unfinished");
594
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_ERROR);
595
+ mongocrypt_ctx_destroy(ctx);
596
+
597
+ mongocrypt_destroy(crypt);
764
598
  }
765
599
 
766
-
767
- static void
768
- _test_rewrap_many_datakey_finalize (_mongocrypt_tester_t *tester)
769
- {
770
- mongocrypt_t *const crypt =
771
- _mongocrypt_tester_mongocrypt (TESTER_MONGOCRYPT_DEFAULT);
772
- mongocrypt_ctx_t *const ctx = mongocrypt_ctx_new (crypt);
773
-
774
- mongocrypt_binary_t *const filter =
775
- TEST_BSON ("{'keyAltName': {'$in': ['keyDocumentA', 'keyDocumentB']}}");
776
-
777
- mongocrypt_binary_t *const key_doc_a =
778
- TEST_FILE ("./test/data/rmd/key-document-a.json");
779
- mongocrypt_binary_t *const key_doc_b =
780
- TEST_FILE ("./test/data/rmd/key-document-b.json");
781
-
782
- /* Save current key fields for comparison with rewrapped keys. */
783
- _test_datakey_fields_t *const fields_a = _find_datakey_fields (key_doc_a);
784
- _test_datakey_fields_t *const fields_b = _find_datakey_fields (key_doc_b);
785
-
786
- mongocrypt_kms_ctx_t *kms = NULL;
787
-
788
- ASSERT_OK (ctx, crypt);
789
- ASSERT_OK (mongocrypt_ctx_setopt_key_encryption_key (
790
- ctx,
791
- TEST_BSON ("{'provider': 'aws',"
792
- " 'region': 'us-east-1',"
793
- " 'key': '" TEST_REWRAP_MASTER_KEY_ID_NEW "'}")),
794
- ctx);
795
- ASSERT_OK (mongocrypt_ctx_rewrap_many_datakey_init (ctx, filter), ctx);
796
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx),
797
- MONGOCRYPT_CTX_NEED_MONGO_KEYS);
798
- ASSERT_OK (mongocrypt_ctx_mongo_feed (
799
- ctx, TEST_FILE ("./test/data/rmd/key-document-a.json")),
800
- ctx);
801
- ASSERT_OK (mongocrypt_ctx_mongo_feed (
802
- ctx, TEST_FILE ("./test/data/rmd/key-document-b.json")),
803
- ctx);
804
- ASSERT_OK (mongocrypt_ctx_mongo_done (ctx), ctx);
805
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_NEED_KMS);
806
- ASSERT ((kms = mongocrypt_ctx_next_kms_ctx (ctx)));
807
- _assert_aws_kms_request (kms);
808
- ASSERT_OK (mongocrypt_kms_ctx_feed (
809
- kms, TEST_FILE ("./test/data/rmd/kms-decrypt-reply-b.txt")),
810
- kms);
811
- ASSERT (mongocrypt_kms_ctx_bytes_needed (kms) == 0);
812
-
813
- ASSERT ((kms = mongocrypt_ctx_next_kms_ctx (ctx)));
814
- _assert_aws_kms_request (kms);
815
- ASSERT_OK (mongocrypt_kms_ctx_feed (
816
- kms, TEST_FILE ("./test/data/rmd/kms-decrypt-reply-a.txt")),
817
- kms);
818
- ASSERT (mongocrypt_kms_ctx_bytes_needed (kms) == 0);
819
- ASSERT_OK (!mongocrypt_ctx_next_kms_ctx (ctx), ctx);
820
- ASSERT_OK (mongocrypt_ctx_kms_done (ctx), ctx);
821
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_NEED_KMS);
822
- ASSERT ((kms = mongocrypt_ctx_next_kms_ctx (ctx)));
823
- _assert_aws_kms_request (kms);
824
- ASSERT_OK (mongocrypt_kms_ctx_feed (
825
- kms, TEST_FILE ("./test/data/rmd/kms-encrypt-reply-a.txt")),
826
- kms);
827
- ASSERT (mongocrypt_kms_ctx_bytes_needed (kms) == 0);
828
-
829
- ASSERT ((kms = mongocrypt_ctx_next_kms_ctx (ctx)));
830
- _assert_aws_kms_request (kms);
831
- ASSERT_OK (mongocrypt_kms_ctx_feed (
832
- kms, TEST_FILE ("./test/data/rmd/kms-encrypt-reply-b.txt")),
833
- kms);
834
- ASSERT (mongocrypt_kms_ctx_bytes_needed (kms) == 0);
835
- ASSERT_OK (!mongocrypt_ctx_next_kms_ctx (ctx), ctx);
836
- ASSERT_OK (mongocrypt_ctx_kms_done (ctx), ctx);
837
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_READY);
838
-
839
- {
840
- mongocrypt_binary_t res;
841
- bson_t bson;
842
- bson_iter_t iter;
843
- bson_iter_t a_iter;
844
- bson_iter_t b_iter;
845
-
846
- ASSERT_OK (mongocrypt_ctx_finalize (ctx, &res), ctx);
847
- ASSERT (_mongocrypt_binary_to_bson (&res, &bson));
848
-
849
- /* There should be exactly 2 documents. */
850
- ASSERT (bson_iter_init (&iter, &bson));
851
- ASSERT (bson_iter_find_descendant (&iter, "v.1", &iter));
852
- ASSERT (!bson_iter_find_descendant (&iter, "v.2", &iter));
853
-
854
- /* Both keys should have the same ID as prior to rewrap, but may be
855
- * returned in a different order from order they were fed. */
856
- {
857
- _mongocrypt_buffer_t id;
858
-
859
- ASSERT (bson_iter_init (&a_iter, &bson));
860
- ASSERT (bson_iter_init (&b_iter, &bson));
861
-
862
- /* Find first keyDocument. */
863
- ASSERT (bson_iter_init (&iter, &bson));
864
- ASSERT (bson_iter_find_descendant (&iter, "v.0._id", &iter));
865
- id = _find_key_id_from_iter (&iter);
866
- if (_buffer_cmp_equal (&fields_a->id, &id)) {
867
- ASSERT (bson_iter_init (&iter, &bson));
868
- ASSERT (bson_iter_find_descendant (&iter, "v.0", &iter));
869
- ASSERT (bson_iter_recurse (&iter, &a_iter));
870
- } else if (_buffer_cmp_equal (&fields_b->id, &id)) {
871
- ASSERT (bson_iter_init (&iter, &bson));
872
- ASSERT (bson_iter_find_descendant (&iter, "v.0", &iter));
873
- ASSERT (bson_iter_recurse (&iter, &b_iter));
874
- }
875
-
876
- /* Find second keyDocument. */
877
- ASSERT (bson_iter_init (&iter, &bson));
878
- ASSERT (bson_iter_find_descendant (&iter, "v.1._id", &iter));
879
- id = _find_key_id_from_iter (&iter);
880
- if (_buffer_cmp_equal (&fields_a->id, &id)) {
881
- ASSERT (bson_iter_init (&iter, &bson));
882
- ASSERT (bson_iter_find_descendant (&iter, "v.1", &iter));
883
- ASSERT (bson_iter_recurse (&iter, &a_iter));
884
- } else if (_buffer_cmp_equal (&fields_b->id, &id)) {
885
- ASSERT (bson_iter_init (&iter, &bson));
886
- ASSERT (bson_iter_find_descendant (&iter, "v.1", &iter));
887
- ASSERT (bson_iter_recurse (&iter, &b_iter));
888
- }
889
-
890
- ASSERT (bson_iter_init (&iter, &bson));
891
- ASSERT (iter.raw != a_iter.raw || iter.off != a_iter.off);
892
- ASSERT (iter.raw != b_iter.raw || iter.off != b_iter.off);
893
- }
894
-
895
- /* Both keys should be rewrapped with new masterKey. */
896
- iter = a_iter;
897
- ASSERT (bson_iter_find_descendant (&iter, "masterKey.key", &iter));
898
- ASSERT_STREQUAL (TEST_REWRAP_MASTER_KEY_ID_NEW,
899
- bson_iter_utf8 (&iter, NULL));
900
- iter = b_iter;
901
- ASSERT (bson_iter_find_descendant (&iter, "masterKey.key", &iter));
902
- ASSERT_STREQUAL (TEST_REWRAP_MASTER_KEY_ID_NEW,
903
- bson_iter_utf8 (&iter, NULL));
904
-
905
- /* Both keys should have new key material. */
906
- {
907
- _mongocrypt_buffer_t key_material_a;
908
- _mongocrypt_buffer_t key_material_b;
909
-
910
- iter = a_iter;
911
- ASSERT (bson_iter_find_descendant (&iter, "keyMaterial", &iter));
912
- key_material_a = _key_material_from_iter (&iter);
913
- ASSERT (!_buffer_cmp_equal (&fields_a->key_material, &key_material_a));
914
-
915
- iter = b_iter;
916
- ASSERT (bson_iter_find_descendant (&iter, "keyMaterial", &iter));
917
- key_material_b = _key_material_from_iter (&iter);
918
- ASSERT (!_buffer_cmp_equal (&fields_b->key_material, &key_material_b));
919
-
920
- /* Key materials should differ. */
921
- ASSERT (!_buffer_cmp_equal (&key_material_a, &key_material_b));
922
- }
923
-
924
- bson_destroy (&bson);
925
- }
926
-
927
- /* No more work to be done for RewrapManyDatakey. */
928
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_DONE);
929
-
930
- _test_datakey_fields_destroy (fields_b);
931
- _test_datakey_fields_destroy (fields_a);
932
- mongocrypt_ctx_destroy (ctx);
933
- mongocrypt_destroy (crypt);
600
+ static void _test_rewrap_many_datakey_finalize(_mongocrypt_tester_t *tester) {
601
+ mongocrypt_t *const crypt = _mongocrypt_tester_mongocrypt(TESTER_MONGOCRYPT_DEFAULT);
602
+ mongocrypt_ctx_t *const ctx = mongocrypt_ctx_new(crypt);
603
+
604
+ mongocrypt_binary_t *const filter = TEST_BSON("{'keyAltName': {'$in': ['keyDocumentA', 'keyDocumentB']}}");
605
+
606
+ mongocrypt_binary_t *const key_doc_a = TEST_FILE("./test/data/rmd/key-document-a.json");
607
+ mongocrypt_binary_t *const key_doc_b = TEST_FILE("./test/data/rmd/key-document-b.json");
608
+
609
+ /* Save current key fields for comparison with rewrapped keys. */
610
+ _test_datakey_fields_t *const fields_a = _find_datakey_fields(key_doc_a);
611
+ _test_datakey_fields_t *const fields_b = _find_datakey_fields(key_doc_b);
612
+
613
+ mongocrypt_kms_ctx_t *kms = NULL;
614
+
615
+ ASSERT_OK(ctx, crypt);
616
+ ASSERT_OK(mongocrypt_ctx_setopt_key_encryption_key(ctx,
617
+ TEST_BSON("{'provider': 'aws',"
618
+ " 'region': 'us-east-1',"
619
+ " 'key': '" TEST_REWRAP_MASTER_KEY_ID_NEW "'}")),
620
+ ctx);
621
+ ASSERT_OK(mongocrypt_ctx_rewrap_many_datakey_init(ctx, filter), ctx);
622
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_MONGO_KEYS);
623
+ ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/rmd/key-document-a.json")), ctx);
624
+ ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/rmd/key-document-b.json")), ctx);
625
+ ASSERT_OK(mongocrypt_ctx_mongo_done(ctx), ctx);
626
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_KMS);
627
+ ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx)));
628
+ _assert_aws_kms_request(kms);
629
+ ASSERT_OK(mongocrypt_kms_ctx_feed(kms, TEST_FILE("./test/data/rmd/kms-decrypt-reply-b.txt")), kms);
630
+ ASSERT(mongocrypt_kms_ctx_bytes_needed(kms) == 0);
631
+
632
+ ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx)));
633
+ _assert_aws_kms_request(kms);
634
+ ASSERT_OK(mongocrypt_kms_ctx_feed(kms, TEST_FILE("./test/data/rmd/kms-decrypt-reply-a.txt")), kms);
635
+ ASSERT(mongocrypt_kms_ctx_bytes_needed(kms) == 0);
636
+ ASSERT_OK(!mongocrypt_ctx_next_kms_ctx(ctx), ctx);
637
+ ASSERT_OK(mongocrypt_ctx_kms_done(ctx), ctx);
638
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_KMS);
639
+ ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx)));
640
+ _assert_aws_kms_request(kms);
641
+ ASSERT_OK(mongocrypt_kms_ctx_feed(kms, TEST_FILE("./test/data/rmd/kms-encrypt-reply-a.txt")), kms);
642
+ ASSERT(mongocrypt_kms_ctx_bytes_needed(kms) == 0);
643
+
644
+ ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx)));
645
+ _assert_aws_kms_request(kms);
646
+ ASSERT_OK(mongocrypt_kms_ctx_feed(kms, TEST_FILE("./test/data/rmd/kms-encrypt-reply-b.txt")), kms);
647
+ ASSERT(mongocrypt_kms_ctx_bytes_needed(kms) == 0);
648
+ ASSERT_OK(!mongocrypt_ctx_next_kms_ctx(ctx), ctx);
649
+ ASSERT_OK(mongocrypt_ctx_kms_done(ctx), ctx);
650
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_READY);
651
+
652
+ {
653
+ mongocrypt_binary_t res;
654
+ bson_t bson;
655
+ bson_iter_t iter;
656
+ bson_iter_t a_iter;
657
+ bson_iter_t b_iter;
658
+
659
+ ASSERT_OK(mongocrypt_ctx_finalize(ctx, &res), ctx);
660
+ ASSERT(_mongocrypt_binary_to_bson(&res, &bson));
661
+
662
+ /* There should be exactly 2 documents. */
663
+ ASSERT(bson_iter_init(&iter, &bson));
664
+ ASSERT(bson_iter_find_descendant(&iter, "v.1", &iter));
665
+ ASSERT(!bson_iter_find_descendant(&iter, "v.2", &iter));
666
+
667
+ /* Both keys should have the same ID as prior to rewrap, but may be
668
+ * returned in a different order from order they were fed. */
669
+ {
670
+ _mongocrypt_buffer_t id;
671
+
672
+ ASSERT(bson_iter_init(&a_iter, &bson));
673
+ ASSERT(bson_iter_init(&b_iter, &bson));
674
+
675
+ /* Find first keyDocument. */
676
+ ASSERT(bson_iter_init(&iter, &bson));
677
+ ASSERT(bson_iter_find_descendant(&iter, "v.0._id", &iter));
678
+ id = _find_key_id_from_iter(&iter);
679
+ if (_buffer_cmp_equal(&fields_a->id, &id)) {
680
+ ASSERT(bson_iter_init(&iter, &bson));
681
+ ASSERT(bson_iter_find_descendant(&iter, "v.0", &iter));
682
+ ASSERT(bson_iter_recurse(&iter, &a_iter));
683
+ } else if (_buffer_cmp_equal(&fields_b->id, &id)) {
684
+ ASSERT(bson_iter_init(&iter, &bson));
685
+ ASSERT(bson_iter_find_descendant(&iter, "v.0", &iter));
686
+ ASSERT(bson_iter_recurse(&iter, &b_iter));
687
+ }
688
+
689
+ /* Find second keyDocument. */
690
+ ASSERT(bson_iter_init(&iter, &bson));
691
+ ASSERT(bson_iter_find_descendant(&iter, "v.1._id", &iter));
692
+ id = _find_key_id_from_iter(&iter);
693
+ if (_buffer_cmp_equal(&fields_a->id, &id)) {
694
+ ASSERT(bson_iter_init(&iter, &bson));
695
+ ASSERT(bson_iter_find_descendant(&iter, "v.1", &iter));
696
+ ASSERT(bson_iter_recurse(&iter, &a_iter));
697
+ } else if (_buffer_cmp_equal(&fields_b->id, &id)) {
698
+ ASSERT(bson_iter_init(&iter, &bson));
699
+ ASSERT(bson_iter_find_descendant(&iter, "v.1", &iter));
700
+ ASSERT(bson_iter_recurse(&iter, &b_iter));
701
+ }
702
+
703
+ ASSERT(bson_iter_init(&iter, &bson));
704
+ ASSERT(iter.raw != a_iter.raw || iter.off != a_iter.off);
705
+ ASSERT(iter.raw != b_iter.raw || iter.off != b_iter.off);
706
+ }
707
+
708
+ /* Both keys should be rewrapped with new masterKey. */
709
+ iter = a_iter;
710
+ ASSERT(bson_iter_find_descendant(&iter, "masterKey.key", &iter));
711
+ ASSERT_STREQUAL(TEST_REWRAP_MASTER_KEY_ID_NEW, bson_iter_utf8(&iter, NULL));
712
+ iter = b_iter;
713
+ ASSERT(bson_iter_find_descendant(&iter, "masterKey.key", &iter));
714
+ ASSERT_STREQUAL(TEST_REWRAP_MASTER_KEY_ID_NEW, bson_iter_utf8(&iter, NULL));
715
+
716
+ /* Both keys should have new key material. */
717
+ {
718
+ _mongocrypt_buffer_t key_material_a;
719
+ _mongocrypt_buffer_t key_material_b;
720
+
721
+ iter = a_iter;
722
+ ASSERT(bson_iter_find_descendant(&iter, "keyMaterial", &iter));
723
+ key_material_a = _key_material_from_iter(&iter);
724
+ ASSERT(!_buffer_cmp_equal(&fields_a->key_material, &key_material_a));
725
+
726
+ iter = b_iter;
727
+ ASSERT(bson_iter_find_descendant(&iter, "keyMaterial", &iter));
728
+ key_material_b = _key_material_from_iter(&iter);
729
+ ASSERT(!_buffer_cmp_equal(&fields_b->key_material, &key_material_b));
730
+
731
+ /* Key materials should differ. */
732
+ ASSERT(!_buffer_cmp_equal(&key_material_a, &key_material_b));
733
+ }
734
+
735
+ bson_destroy(&bson);
736
+ }
737
+
738
+ /* No more work to be done for RewrapManyDatakey. */
739
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_DONE);
740
+
741
+ _test_datakey_fields_destroy(fields_b);
742
+ _test_datakey_fields_destroy(fields_a);
743
+ mongocrypt_ctx_destroy(ctx);
744
+ mongocrypt_destroy(crypt);
934
745
  }
935
746
 
936
-
937
- static void
938
- _test_rewrap_many_datakey_kms_credentials (_mongocrypt_tester_t *tester)
939
- {
940
- mongocrypt_t *crypt = NULL;
941
- mongocrypt_ctx_t *ctx = NULL;
942
-
943
- /* Ensure rewrapManyDataKey correctly handles need KMS credentials option. */
944
- {
945
- crypt = mongocrypt_new ();
946
- mongocrypt_setopt_use_need_kms_credentials_state (crypt);
947
- ASSERT_OK (
948
- mongocrypt_setopt_kms_providers (crypt, TEST_BSON ("{'aws': {}}")),
949
- crypt);
950
- ASSERT_OK (mongocrypt_init (crypt), crypt);
951
- ctx = mongocrypt_ctx_new (crypt);
952
-
953
- ASSERT_OK (ctx, crypt);
954
-
955
- ASSERT_OK (
956
- mongocrypt_ctx_rewrap_many_datakey_init (ctx, TEST_BSON ("{}")), ctx);
957
-
958
- /* NEED_KMS_CREDENTIALS comes before NEED_MONGO_KEYS. */
959
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx),
960
- MONGOCRYPT_CTX_NEED_KMS_CREDENTIALS);
961
- ASSERT_OK (mongocrypt_ctx_provide_kms_providers (
962
- ctx,
963
- TEST_BSON ("{'aws': {"
964
- " 'accessKeyId': 'example',"
965
- " 'secretAccessKey': 'example'}}")),
966
- ctx);
967
-
968
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx),
969
- MONGOCRYPT_CTX_NEED_MONGO_KEYS);
970
- ASSERT_OK (mongocrypt_ctx_mongo_feed (
971
- ctx, TEST_FILE ("./test/data/rmd/key-document-a.json")),
972
- ctx);
973
- ASSERT_OK (mongocrypt_ctx_mongo_done (ctx), ctx);
974
-
975
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_NEED_KMS);
976
- {
977
- mongocrypt_kms_ctx_t *kms = NULL;
978
-
979
- ASSERT ((kms = mongocrypt_ctx_next_kms_ctx (ctx)));
980
- _assert_aws_kms_request (kms);
981
- ASSERT_OK (
982
- mongocrypt_kms_ctx_feed (
983
- kms, TEST_FILE ("./test/data/rmd/kms-decrypt-reply-a.txt")),
984
- kms);
985
- ASSERT (mongocrypt_kms_ctx_bytes_needed (kms) == 0);
986
-
987
- ASSERT_OK (!mongocrypt_ctx_next_kms_ctx (ctx), ctx);
988
- ASSERT_OK (mongocrypt_ctx_kms_done (ctx), ctx);
989
- }
990
-
991
- /* KMS credentials provided before decryption should be reused here. */
992
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_NEED_KMS);
993
- {
994
- mongocrypt_kms_ctx_t *kms = NULL;
995
-
996
- ASSERT ((kms = mongocrypt_ctx_next_kms_ctx (ctx)));
997
- _assert_aws_kms_request (kms);
998
- ASSERT_OK (
999
- mongocrypt_kms_ctx_feed (
1000
- kms, TEST_FILE ("./test/data/rmd/kms-encrypt-reply-a.txt")),
1001
- kms);
1002
- ASSERT (mongocrypt_kms_ctx_bytes_needed (kms) == 0);
1003
-
1004
- ASSERT_OK (!mongocrypt_ctx_next_kms_ctx (ctx), ctx);
1005
- ASSERT_OK (mongocrypt_ctx_kms_done (ctx), ctx);
1006
- }
1007
-
1008
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_READY);
1009
-
1010
- {
1011
- mongocrypt_binary_t res;
1012
- bson_t bson;
1013
- bson_iter_t iter;
1014
-
1015
- ASSERT_OK (mongocrypt_ctx_finalize (ctx, &res), ctx);
1016
- ASSERT (_mongocrypt_binary_to_bson (&res, &bson));
1017
- ASSERT (bson_iter_init (&iter, &bson));
1018
- ASSERT (bson_iter_find_descendant (&iter, "v.0.masterKey.key", &iter));
1019
- ASSERT_STREQUAL (TEST_REWRAP_MASTER_KEY_ID_OLD,
1020
- bson_iter_utf8 (&iter, NULL));
1021
- }
1022
-
1023
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx), MONGOCRYPT_CTX_DONE);
1024
-
1025
- mongocrypt_ctx_destroy (ctx);
1026
- mongocrypt_destroy (crypt);
1027
- }
1028
-
1029
- /* Should not enter NEED_KMS_CREDENTIALS state if credentials already
1030
- * provided. */
1031
- crypt = mongocrypt_new ();
1032
- mongocrypt_setopt_use_need_kms_credentials_state (crypt);
1033
- ASSERT_OK (mongocrypt_setopt_kms_providers (
1034
- crypt,
1035
- TEST_BSON ("{'aws': {"
1036
- " 'accessKeyId': 'example',"
1037
- " 'secretAccessKey': 'example'}}")),
747
+ static void _test_rewrap_many_datakey_kms_credentials(_mongocrypt_tester_t *tester) {
748
+ mongocrypt_t *crypt = NULL;
749
+ mongocrypt_ctx_t *ctx = NULL;
750
+
751
+ /* Ensure rewrapManyDataKey correctly handles need KMS credentials option. */
752
+ {
753
+ crypt = mongocrypt_new();
754
+ mongocrypt_setopt_use_need_kms_credentials_state(crypt);
755
+ ASSERT_OK(mongocrypt_setopt_kms_providers(crypt, TEST_BSON("{'aws': {}}")), crypt);
756
+ ASSERT_OK(mongocrypt_init(crypt), crypt);
757
+ ctx = mongocrypt_ctx_new(crypt);
758
+
759
+ ASSERT_OK(ctx, crypt);
760
+
761
+ ASSERT_OK(mongocrypt_ctx_rewrap_many_datakey_init(ctx, TEST_BSON("{}")), ctx);
762
+
763
+ /* NEED_KMS_CREDENTIALS comes before NEED_MONGO_KEYS. */
764
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_KMS_CREDENTIALS);
765
+ ASSERT_OK(mongocrypt_ctx_provide_kms_providers(ctx,
766
+ TEST_BSON("{'aws': {"
767
+ " 'accessKeyId': 'example',"
768
+ " 'secretAccessKey': 'example'}}")),
769
+ ctx);
770
+
771
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_MONGO_KEYS);
772
+ ASSERT_OK(mongocrypt_ctx_mongo_feed(ctx, TEST_FILE("./test/data/rmd/key-document-a.json")), ctx);
773
+ ASSERT_OK(mongocrypt_ctx_mongo_done(ctx), ctx);
774
+
775
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_KMS);
776
+ {
777
+ mongocrypt_kms_ctx_t *kms = NULL;
778
+
779
+ ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx)));
780
+ _assert_aws_kms_request(kms);
781
+ ASSERT_OK(mongocrypt_kms_ctx_feed(kms, TEST_FILE("./test/data/rmd/kms-decrypt-reply-a.txt")), kms);
782
+ ASSERT(mongocrypt_kms_ctx_bytes_needed(kms) == 0);
783
+
784
+ ASSERT_OK(!mongocrypt_ctx_next_kms_ctx(ctx), ctx);
785
+ ASSERT_OK(mongocrypt_ctx_kms_done(ctx), ctx);
786
+ }
787
+
788
+ /* KMS credentials provided before decryption should be reused here. */
789
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_KMS);
790
+ {
791
+ mongocrypt_kms_ctx_t *kms = NULL;
792
+
793
+ ASSERT((kms = mongocrypt_ctx_next_kms_ctx(ctx)));
794
+ _assert_aws_kms_request(kms);
795
+ ASSERT_OK(mongocrypt_kms_ctx_feed(kms, TEST_FILE("./test/data/rmd/kms-encrypt-reply-a.txt")), kms);
796
+ ASSERT(mongocrypt_kms_ctx_bytes_needed(kms) == 0);
797
+
798
+ ASSERT_OK(!mongocrypt_ctx_next_kms_ctx(ctx), ctx);
799
+ ASSERT_OK(mongocrypt_ctx_kms_done(ctx), ctx);
800
+ }
801
+
802
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_READY);
803
+
804
+ {
805
+ mongocrypt_binary_t res;
806
+ bson_t bson;
807
+ bson_iter_t iter;
808
+
809
+ ASSERT_OK(mongocrypt_ctx_finalize(ctx, &res), ctx);
810
+ ASSERT(_mongocrypt_binary_to_bson(&res, &bson));
811
+ ASSERT(bson_iter_init(&iter, &bson));
812
+ ASSERT(bson_iter_find_descendant(&iter, "v.0.masterKey.key", &iter));
813
+ ASSERT_STREQUAL(TEST_REWRAP_MASTER_KEY_ID_OLD, bson_iter_utf8(&iter, NULL));
814
+ }
815
+
816
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_DONE);
817
+
818
+ mongocrypt_ctx_destroy(ctx);
819
+ mongocrypt_destroy(crypt);
820
+ }
821
+
822
+ /* Should not enter NEED_KMS_CREDENTIALS state if credentials already
823
+ * provided. */
824
+ crypt = mongocrypt_new();
825
+ mongocrypt_setopt_use_need_kms_credentials_state(crypt);
826
+ ASSERT_OK(mongocrypt_setopt_kms_providers(crypt,
827
+ TEST_BSON("{'aws': {"
828
+ " 'accessKeyId': 'example',"
829
+ " 'secretAccessKey': 'example'}}")),
1038
830
  crypt);
1039
- ASSERT_OK (mongocrypt_init (crypt), crypt);
1040
- ctx = mongocrypt_ctx_new (crypt);
1041
- ASSERT_OK (ctx, crypt);
1042
- ASSERT_OK (mongocrypt_ctx_rewrap_many_datakey_init (ctx, TEST_BSON ("{}")),
1043
- ctx);
1044
- ASSERT_STATE_EQUAL (mongocrypt_ctx_state (ctx),
1045
- MONGOCRYPT_CTX_NEED_MONGO_KEYS);
1046
- mongocrypt_ctx_destroy (ctx);
1047
- mongocrypt_destroy (crypt);
831
+ ASSERT_OK(mongocrypt_init(crypt), crypt);
832
+ ctx = mongocrypt_ctx_new(crypt);
833
+ ASSERT_OK(ctx, crypt);
834
+ ASSERT_OK(mongocrypt_ctx_rewrap_many_datakey_init(ctx, TEST_BSON("{}")), ctx);
835
+ ASSERT_STATE_EQUAL(mongocrypt_ctx_state(ctx), MONGOCRYPT_CTX_NEED_MONGO_KEYS);
836
+ mongocrypt_ctx_destroy(ctx);
837
+ mongocrypt_destroy(crypt);
1048
838
  }
1049
839
 
1050
-
1051
- void
1052
- _mongocrypt_tester_install_ctx_rewrap_many_datakey (
1053
- _mongocrypt_tester_t *tester)
1054
- {
1055
- INSTALL_TEST (_test_rewrap_many_datakey_init);
1056
- INSTALL_TEST (_test_rewrap_many_datakey_need_mongo_keys);
1057
- INSTALL_TEST (_test_rewrap_many_datakey_need_kms_decrypt);
1058
- INSTALL_TEST (_test_rewrap_many_datakey_need_kms_encrypt);
1059
- INSTALL_TEST (_test_rewrap_many_datakey_finalize);
1060
- INSTALL_TEST (_test_rewrap_many_datakey_kms_credentials);
840
+ void _mongocrypt_tester_install_ctx_rewrap_many_datakey(_mongocrypt_tester_t *tester) {
841
+ INSTALL_TEST(_test_rewrap_many_datakey_init);
842
+ INSTALL_TEST(_test_rewrap_many_datakey_need_mongo_keys);
843
+ INSTALL_TEST(_test_rewrap_many_datakey_need_kms_decrypt);
844
+ INSTALL_TEST(_test_rewrap_many_datakey_need_kms_encrypt);
845
+ INSTALL_TEST(_test_rewrap_many_datakey_finalize);
846
+ INSTALL_TEST(_test_rewrap_many_datakey_kms_credentials);
1061
847
  }