libmongocrypt-helper 1.7.4.0.1002 → 1.11.0.0.1001

Sign up to get free protection for your applications and to get access to all the features.
Files changed (600) hide show
  1. checksums.yaml +4 -4
  2. data/ext/libmongocrypt/libmongocrypt/CHANGELOG.md +40 -5
  3. data/ext/libmongocrypt/libmongocrypt/CMakeLists.txt +29 -24
  4. data/ext/libmongocrypt/libmongocrypt/CODEOWNERS +1 -4
  5. data/ext/libmongocrypt/libmongocrypt/Earthfile +522 -0
  6. data/ext/libmongocrypt/libmongocrypt/README.md +36 -40
  7. data/ext/libmongocrypt/libmongocrypt/bindings/cs/CMakeLists.txt +1 -0
  8. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Driver.snk +0 -0
  9. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt/AssemblyInfo.cs +2 -2
  10. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt/Binary.cs +16 -2
  11. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt/CryptClient.cs +25 -6
  12. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt/CryptClientFactory.cs +8 -4
  13. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt/Library.cs +35 -3
  14. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt/LibraryLoader.cs +81 -44
  15. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt/MongoDB.Libmongocrypt.csproj +2 -1
  16. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt.Example/MongoDB.Libmongocrypt.Example.csproj +1 -1
  17. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt.Test/BasicTests.cs +1 -1
  18. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt.Test/MongoDB.Libmongocrypt.Test.csproj +2 -0
  19. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt.Test32/MongoDB.Libmongocrypt.Test32.csproj +2 -0
  20. data/ext/libmongocrypt/libmongocrypt/bindings/cs/README.md +3 -0
  21. data/ext/libmongocrypt/libmongocrypt/bindings/cs/Scripts/build.cake +22 -26
  22. data/ext/libmongocrypt/libmongocrypt/bindings/cs/Scripts/build.config +3 -0
  23. data/ext/libmongocrypt/libmongocrypt/bindings/cs/Scripts/build.sh +0 -0
  24. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/benchmarks/build.gradle.kts +28 -0
  25. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/benchmarks/src/main/java/com/mongodb/crypt/benchmark/BenchmarkRunner.java +217 -0
  26. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/benchmarks/src/main/resources/keyDocument.json +24 -0
  27. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/build.gradle.kts +22 -7
  28. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/gradle/wrapper/gradle-wrapper.jar +0 -0
  29. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/gradle/wrapper/gradle-wrapper.properties +1 -1
  30. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/gradlew +154 -108
  31. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/gradlew.bat +7 -18
  32. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/settings.gradle.kts +1 -0
  33. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/main/java/com/mongodb/crypt/capi/CAPI.java +41 -6
  34. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/main/java/com/mongodb/crypt/capi/CAPIHelper.java +5 -5
  35. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/main/java/com/mongodb/crypt/capi/CipherCallback.java +27 -1
  36. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/main/java/com/mongodb/crypt/capi/MongoCryptImpl.java +34 -19
  37. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/main/java/com/mongodb/crypt/capi/MongoExplicitEncryptOptions.java +6 -4
  38. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/main/resources/META-INF/native-image/jni-config.json +180 -0
  39. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/main/resources/META-INF/native-image/reflect-config.json +134 -0
  40. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/test/java/com/mongodb/crypt/capi/MongoCryptTest.java +50 -6
  41. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/test/resources/fle2-find-range-explicit-v2/int32/encrypted-payload.json +26 -0
  42. data/ext/libmongocrypt/libmongocrypt/bindings/{python/test/data/fle2-find-range-explicit → java/mongocrypt/src/test/resources/fle2-find-range-explicit-v2}/int32/rangeopts.json +3 -0
  43. data/ext/libmongocrypt/libmongocrypt/bindings/node/README.md +4 -771
  44. data/ext/libmongocrypt/libmongocrypt/bindings/python/CHANGELOG.rst +86 -0
  45. data/ext/libmongocrypt/libmongocrypt/bindings/python/README.rst +41 -20
  46. data/ext/libmongocrypt/libmongocrypt/bindings/python/RELEASE.rst +6 -24
  47. data/ext/libmongocrypt/libmongocrypt/bindings/python/build-manylinux-wheel.sh +4 -13
  48. data/ext/libmongocrypt/libmongocrypt/bindings/python/hatch_build.py +36 -0
  49. data/ext/libmongocrypt/libmongocrypt/bindings/python/libmongocrypt-version.txt +1 -0
  50. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/__init__.py +2 -2
  51. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/asynchronous/auto_encrypter.py +61 -0
  52. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/asynchronous/credentials.py +156 -0
  53. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/asynchronous/explicit_encrypter.py +156 -0
  54. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/asynchronous/state_machine.py +149 -0
  55. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/auto_encrypter.py +2 -46
  56. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/binary.py +14 -17
  57. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/binding.py +232 -296
  58. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/compat.py +6 -31
  59. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/credentials.py +2 -121
  60. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/crypto.py +31 -20
  61. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/errors.py +2 -2
  62. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/explicit_encrypter.py +2 -233
  63. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/mongocrypt.py +168 -254
  64. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/options.py +265 -0
  65. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/state_machine.py +2 -141
  66. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/synchronous/auto_encrypter.py +61 -0
  67. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/synchronous/credentials.py +156 -0
  68. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/synchronous/explicit_encrypter.py +156 -0
  69. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/synchronous/state_machine.py +149 -0
  70. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/version.py +2 -2
  71. data/ext/libmongocrypt/libmongocrypt/bindings/python/pyproject.toml +118 -0
  72. data/ext/libmongocrypt/libmongocrypt/bindings/python/release.sh +97 -61
  73. data/ext/libmongocrypt/libmongocrypt/bindings/python/requirements-test.txt +7 -0
  74. data/ext/libmongocrypt/libmongocrypt/bindings/python/requirements.txt +4 -0
  75. data/ext/libmongocrypt/libmongocrypt/bindings/python/sbom.json +76 -0
  76. data/ext/libmongocrypt/libmongocrypt/bindings/python/strip_header.py +6 -7
  77. data/ext/libmongocrypt/libmongocrypt/bindings/python/synchro.py +64 -0
  78. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/__init__.py +2 -10
  79. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/collection-info.json +1 -1
  80. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/command.json +1 -1
  81. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/compact/success/encrypted-payload.json +21 -21
  82. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/encrypted-command-reply.json +1 -1
  83. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/encrypted-field-config-map.json +1 -1
  84. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/fle2-find-range-explicit-v2/int32/encrypted-payload.json +26 -0
  85. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/fle2-find-range-explicit-v2/int32/rangeopts.json +14 -0
  86. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/key-document-azure.json +1 -1
  87. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/key-document-gcp.json +1 -1
  88. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/key-document.json +1 -1
  89. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/key-filter.json +1 -1
  90. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/list-collections-filter.json +1 -1
  91. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/mongocryptd-command.json +1 -1
  92. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/mongocryptd-reply.json +1 -1
  93. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/schema-map.json +1 -1
  94. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/performance/keyDocument.json +24 -0
  95. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/performance/perf_test.py +165 -0
  96. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/test_binding.py +8 -12
  97. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/test_crypto.py +9 -11
  98. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/test_mongocrypt.py +991 -343
  99. data/ext/libmongocrypt/libmongocrypt/bindings/python/update-sbom.sh +14 -0
  100. data/ext/libmongocrypt/libmongocrypt/cmake/FetchMongoC.cmake +19 -1
  101. data/ext/libmongocrypt/libmongocrypt/cmake/ImportBSON.cmake +31 -6
  102. data/ext/libmongocrypt/libmongocrypt/cmake/IntelDFP.cmake +20 -227
  103. data/ext/libmongocrypt/libmongocrypt/cmake/Patch.cmake +54 -0
  104. data/ext/libmongocrypt/libmongocrypt/cmake/Platform.cmake +50 -0
  105. data/ext/libmongocrypt/libmongocrypt/cmake/mongocrypt-config.cmake +21 -0
  106. data/ext/libmongocrypt/libmongocrypt/doc/img/cli-icon.png +0 -0
  107. data/ext/libmongocrypt/libmongocrypt/doc/img/reference-targets.png +0 -0
  108. data/ext/libmongocrypt/libmongocrypt/doc/releasing.md +153 -0
  109. data/ext/libmongocrypt/libmongocrypt/etc/c6-vault.repo +39 -0
  110. data/ext/libmongocrypt/libmongocrypt/etc/calc_release_version.py +61 -28
  111. data/ext/libmongocrypt/libmongocrypt/etc/calc_release_version_selftest.sh +73 -0
  112. data/ext/libmongocrypt/libmongocrypt/etc/cyclonedx.sbom.json +108 -0
  113. data/ext/libmongocrypt/libmongocrypt/etc/fle2_aead_generate_tests.py +15 -24
  114. data/ext/libmongocrypt/libmongocrypt/etc/fle2_crypto.py +66 -54
  115. data/ext/libmongocrypt/libmongocrypt/etc/fle2_generate_tests.py +14 -23
  116. data/ext/libmongocrypt/libmongocrypt/etc/fle2v2_aead_generate_tests.py +32 -0
  117. data/ext/libmongocrypt/libmongocrypt/etc/format-all.sh +12 -0
  118. data/ext/libmongocrypt/libmongocrypt/etc/format.sh +16 -0
  119. data/ext/libmongocrypt/libmongocrypt/etc/install-package.sh +48 -0
  120. data/ext/libmongocrypt/libmongocrypt/etc/libbson-remove-GCC-diagnostic-pragma.patch +27 -0
  121. data/ext/libmongocrypt/libmongocrypt/etc/mongo-inteldfp-alpine-arm-fix.patch +17 -0
  122. data/ext/libmongocrypt/libmongocrypt/etc/mongo-inteldfp-libmongocrypt-pr-625.patch +13 -0
  123. data/ext/libmongocrypt/libmongocrypt/etc/packager.py +120 -91
  124. data/ext/libmongocrypt/libmongocrypt/etc/purls.txt +14 -0
  125. data/ext/libmongocrypt/libmongocrypt/etc/repo_config.yaml +56 -0
  126. data/ext/libmongocrypt/libmongocrypt/etc/silk-create-asset-group.sh +70 -0
  127. data/ext/libmongocrypt/libmongocrypt/etc/ssdlc_compliance_report.md +37 -0
  128. data/ext/libmongocrypt/libmongocrypt/etc/third_party_vulnerabilities.md +42 -0
  129. data/ext/libmongocrypt/libmongocrypt/integrating.md +18 -1
  130. data/ext/libmongocrypt/libmongocrypt/kms-message/CMakeLists.txt +11 -3
  131. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_gcp_request.c +1 -1
  132. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_kmip_reader_writer.c +17 -0
  133. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_kmip_reader_writer_private.h +6 -0
  134. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_kmip_request.c +211 -1
  135. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_kmip_response.c +163 -0
  136. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_kmip_tag_type_private.h +2 -1
  137. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_message/kms_kmip_request.h +17 -0
  138. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_message/kms_kmip_response.h +6 -0
  139. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_port.c +3 -2
  140. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_request.c +4 -2
  141. data/ext/libmongocrypt/libmongocrypt/kms-message/src/kms_request_str.c +2 -2
  142. data/ext/libmongocrypt/libmongocrypt/kms-message/test/test_kmip_reader_writer.c +23 -2
  143. data/ext/libmongocrypt/libmongocrypt/src/crypto/cng.c +381 -436
  144. data/ext/libmongocrypt/libmongocrypt/src/crypto/commoncrypto.c +162 -227
  145. data/ext/libmongocrypt/libmongocrypt/src/crypto/libcrypto.c +183 -248
  146. data/ext/libmongocrypt/libmongocrypt/src/crypto/none.c +33 -55
  147. data/ext/libmongocrypt/libmongocrypt/src/csfle-markup.cpp +175 -205
  148. data/ext/libmongocrypt/libmongocrypt/src/mc-array-private.h +10 -21
  149. data/ext/libmongocrypt/libmongocrypt/src/mc-array.c +44 -56
  150. data/ext/libmongocrypt/libmongocrypt/src/mc-check-conversions-private.h +4 -8
  151. data/ext/libmongocrypt/libmongocrypt/src/mc-dec128.h +351 -463
  152. data/ext/libmongocrypt/libmongocrypt/src/mc-dec128.test.cpp +59 -66
  153. data/ext/libmongocrypt/libmongocrypt/src/mc-efc-private.h +21 -11
  154. data/ext/libmongocrypt/libmongocrypt/src/mc-efc.c +175 -99
  155. data/ext/libmongocrypt/libmongocrypt/src/mc-fle-blob-subtype-private.h +18 -10
  156. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-encryption-placeholder-private.h +69 -70
  157. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-encryption-placeholder.c +445 -439
  158. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-equality-payload-private-v2.h +41 -0
  159. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-equality-payload-private.h +11 -18
  160. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-equality-payload-v2.c +135 -0
  161. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-equality-payload.c +109 -126
  162. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-range-payload-private-v2.h +103 -0
  163. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-range-payload-private.h +28 -31
  164. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-range-payload-v2.c +173 -0
  165. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-range-payload.c +106 -125
  166. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-insert-update-payload-private-v2.h +130 -0
  167. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-insert-update-payload-private.h +36 -44
  168. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-insert-update-payload-v2.c +382 -0
  169. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-insert-update-payload.c +237 -278
  170. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-iev-private-v2.h +133 -0
  171. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-iev-private.h +67 -56
  172. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-iev-v2.c +437 -0
  173. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-iev.c +454 -325
  174. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-uev-common-private.h +69 -0
  175. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-uev-common.c +182 -0
  176. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-uev-private.h +23 -31
  177. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-uev-v2-private.h +85 -0
  178. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-uev-v2.c +142 -0
  179. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-uev.c +104 -231
  180. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-range-operator-private.h +8 -7
  181. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-rfds-private.h +56 -56
  182. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-rfds.c +520 -580
  183. data/ext/libmongocrypt/libmongocrypt/src/mc-optional-private.h +47 -66
  184. data/ext/libmongocrypt/libmongocrypt/src/mc-range-edge-generation-private.h +67 -76
  185. data/ext/libmongocrypt/libmongocrypt/src/mc-range-edge-generation.c +201 -190
  186. data/ext/libmongocrypt/libmongocrypt/src/mc-range-encoding-private.h +55 -42
  187. data/ext/libmongocrypt/libmongocrypt/src/mc-range-encoding.c +809 -558
  188. data/ext/libmongocrypt/libmongocrypt/src/mc-range-mincover-generator.template.h +177 -201
  189. data/ext/libmongocrypt/libmongocrypt/src/mc-range-mincover-private.h +54 -53
  190. data/ext/libmongocrypt/libmongocrypt/src/mc-range-mincover.c +188 -240
  191. data/ext/libmongocrypt/libmongocrypt/src/mc-rangeopts-private.h +43 -35
  192. data/ext/libmongocrypt/libmongocrypt/src/mc-rangeopts.c +484 -337
  193. data/ext/libmongocrypt/libmongocrypt/src/mc-reader-private.h +36 -71
  194. data/ext/libmongocrypt/libmongocrypt/src/mc-reader.c +111 -164
  195. data/ext/libmongocrypt/libmongocrypt/src/mc-tokens-private.h +87 -65
  196. data/ext/libmongocrypt/libmongocrypt/src/mc-tokens.c +153 -125
  197. data/ext/libmongocrypt/libmongocrypt/src/mc-writer-private.h +66 -0
  198. data/ext/libmongocrypt/libmongocrypt/src/mc-writer.c +141 -0
  199. data/ext/libmongocrypt/libmongocrypt/src/mlib/check.hpp +37 -55
  200. data/ext/libmongocrypt/libmongocrypt/src/mlib/endian.h +11 -11
  201. data/ext/libmongocrypt/libmongocrypt/src/mlib/error.h +27 -32
  202. data/ext/libmongocrypt/libmongocrypt/src/mlib/int128.h +461 -499
  203. data/ext/libmongocrypt/libmongocrypt/src/mlib/int128.test.cpp +256 -334
  204. data/ext/libmongocrypt/libmongocrypt/src/mlib/macros.h +4 -5
  205. data/ext/libmongocrypt/libmongocrypt/src/mlib/path.h +196 -231
  206. data/ext/libmongocrypt/libmongocrypt/src/mlib/path.test.c +56 -79
  207. data/ext/libmongocrypt/libmongocrypt/src/mlib/str.h +411 -530
  208. data/ext/libmongocrypt/libmongocrypt/src/mlib/str.test.c +116 -131
  209. data/ext/libmongocrypt/libmongocrypt/src/mlib/thread.h +17 -26
  210. data/ext/libmongocrypt/libmongocrypt/src/mlib/user-check.h +2 -2
  211. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-binary-private.h +1 -9
  212. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-binary.c +31 -48
  213. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-buffer-private.h +55 -131
  214. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-buffer.c +442 -565
  215. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-collinfo-private.h +1 -2
  216. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-collinfo.c +28 -45
  217. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-key-private.h +11 -17
  218. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-key.c +104 -132
  219. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-oauth-private.h +16 -24
  220. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-oauth.c +110 -93
  221. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-private.h +26 -45
  222. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache.c +220 -273
  223. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ciphertext-private.h +13 -25
  224. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ciphertext.c +147 -166
  225. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-compat.h +2 -2
  226. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-crypto-private.h +110 -204
  227. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-crypto.c +1145 -1567
  228. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ctx-datakey.c +510 -476
  229. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ctx-decrypt.c +817 -694
  230. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ctx-encrypt.c +2775 -2697
  231. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ctx-private.h +189 -184
  232. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ctx-rewrap-many-datakey.c +308 -351
  233. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ctx.c +990 -1139
  234. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-dll-private.h +23 -24
  235. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-endian-private.h +44 -58
  236. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-endpoint-private.h +21 -25
  237. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-endpoint.c +167 -181
  238. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-kek-private.h +42 -44
  239. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-kek.c +292 -272
  240. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-key-broker-private.h +70 -110
  241. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-key-broker.c +1010 -1033
  242. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-key-private.h +27 -44
  243. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-key.c +349 -402
  244. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-kms-ctx-private.h +127 -120
  245. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-kms-ctx.c +1692 -1611
  246. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-log-private.h +35 -67
  247. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-log.c +49 -83
  248. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-marking-private.h +31 -36
  249. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-marking.c +1923 -1479
  250. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-mutex-private.h +7 -12
  251. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-opts-private.h +124 -107
  252. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-opts.c +925 -419
  253. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-private.h +99 -122
  254. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-status-private.h +3 -8
  255. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-status.c +92 -119
  256. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-traverse-util-private.h +19 -29
  257. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-traverse-util.c +136 -176
  258. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-util-private.h +11 -21
  259. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-util.c +98 -136
  260. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt.c +924 -1339
  261. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt.h +199 -313
  262. data/ext/libmongocrypt/libmongocrypt/src/os_posix/os_dll.c +83 -79
  263. data/ext/libmongocrypt/libmongocrypt/src/os_posix/os_mutex.c +20 -28
  264. data/ext/libmongocrypt/libmongocrypt/src/os_win/os_dll.c +64 -73
  265. data/ext/libmongocrypt/libmongocrypt/src/os_win/os_mutex.c +8 -16
  266. data/ext/libmongocrypt/libmongocrypt/test/crypt_shared-stub.cpp +57 -87
  267. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/bypassQueryAnalysis/payload.json +53 -0
  268. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/jsonSchema/cmd-to-mongocryptd.json +23 -0
  269. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/jsonSchema/cmd.json +20 -0
  270. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/simple/cmd-to-mongocryptd.json +50 -0
  271. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/simple/cmd.json +20 -0
  272. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/simple/collinfo.json +44 -0
  273. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/simple/encrypted-field-map.json +24 -0
  274. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/simple/encrypted-payload-pattern.json +53 -0
  275. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/simple/mongocryptd-reply.json +62 -0
  276. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/unencrypted/cmd-to-mongocryptd.json +30 -0
  277. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/unencrypted/cmd.json +17 -0
  278. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/unencrypted/mongocryptd-reply.json +39 -0
  279. data/ext/libmongocrypt/libmongocrypt/test/data/bulkWrite/unencrypted/payload.json +21 -0
  280. data/ext/libmongocrypt/libmongocrypt/test/data/cleanup/missing-key-id/collinfo.json +20 -0
  281. data/ext/libmongocrypt/libmongocrypt/test/data/cleanup/no-fields/collinfo.json +9 -0
  282. data/ext/libmongocrypt/libmongocrypt/test/data/cleanup/no-fields/encrypted-payload.json +4 -0
  283. data/ext/libmongocrypt/libmongocrypt/test/data/cleanup/success/cmd.json +1 -0
  284. data/ext/libmongocrypt/libmongocrypt/test/data/cleanup/success/collinfo.json +63 -0
  285. data/ext/libmongocrypt/libmongocrypt/test/data/cleanup/success/encrypted-field-config-map.json +61 -0
  286. data/ext/libmongocrypt/libmongocrypt/test/data/cleanup/success/encrypted-payload-range-v2.json +37 -0
  287. data/ext/libmongocrypt/libmongocrypt/test/data/cleanup/success/encrypted-payload.json +29 -0
  288. data/ext/libmongocrypt/libmongocrypt/test/data/compact/anchor-pad/cmd.json +1 -0
  289. data/ext/libmongocrypt/libmongocrypt/test/data/compact/anchor-pad/collinfo.json +64 -0
  290. data/ext/libmongocrypt/libmongocrypt/test/data/compact/anchor-pad/encrypted-payload-range-v2.json +105 -0
  291. data/ext/libmongocrypt/libmongocrypt/test/data/compact/anchor-pad/encrypted-payload.json +30 -0
  292. data/ext/libmongocrypt/libmongocrypt/test/data/compact/no-range/cmd.json +1 -0
  293. data/ext/libmongocrypt/libmongocrypt/test/data/compact/no-range/collinfo.json +49 -0
  294. data/ext/libmongocrypt/libmongocrypt/test/data/compact/no-range/encrypted-field-config-map.json +47 -0
  295. data/ext/libmongocrypt/libmongocrypt/test/data/compact/no-range/encrypted-payload.json +23 -0
  296. data/ext/libmongocrypt/libmongocrypt/test/data/compact/success/collinfo.json +15 -0
  297. data/ext/libmongocrypt/libmongocrypt/test/data/compact/success/encrypted-field-config-map.json +10 -0
  298. data/ext/libmongocrypt/libmongocrypt/test/data/compact/success/encrypted-payload-range-v2.json +104 -0
  299. data/ext/libmongocrypt/libmongocrypt/test/data/compact/success/encrypted-payload.json +6 -0
  300. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-delete/empty/encrypted-payload-v2.json +60 -0
  301. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-delete/success/encrypted-payload-v2.json +67 -0
  302. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explain/with-csfle/collinfo.json +4 -4
  303. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explain/with-csfle/encrypted-payload.json +5 -5
  304. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explicit/find-indexed-contentionFactor1-v2.json +8 -0
  305. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explicit/find-indexed-v2.json +8 -0
  306. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explicit/insert-indexed-contentionFactor1-v2.json +8 -0
  307. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explicit/insert-indexed-same-user-and-index-key-v2.json +8 -0
  308. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explicit/insert-indexed-v2.json +8 -0
  309. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-equality-v2/cmd.json +6 -0
  310. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-equality-v2/encrypted-field-map.json +22 -0
  311. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-equality-v2/encrypted-payload.json +40 -0
  312. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-equality-v2/mongocryptd-reply.json +19 -0
  313. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/date-v2/cmd.json +10 -0
  314. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/date-v2/encrypted-field-map.json +27 -0
  315. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/date-v2/encrypted-payload.json +41 -0
  316. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/date-v2/mongocryptd-reply.json +49 -0
  317. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-precision-v2/cmd.json +6 -0
  318. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-precision-v2/encrypted-field-map.json +30 -0
  319. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-precision-v2/encrypted-payload.json +50 -0
  320. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-precision-v2/mongocryptd-reply.json +58 -0
  321. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-v2/cmd.json +6 -0
  322. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-v2/encrypted-field-map.json +27 -0
  323. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-v2/encrypted-payload.json +41 -0
  324. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-v2/mongocryptd-reply.json +49 -0
  325. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-precision-v2/cmd.json +8 -0
  326. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-precision-v2/encrypted-field-map.json +30 -0
  327. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-precision-v2/encrypted-payload.json +44 -0
  328. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-precision-v2/mongocryptd-reply.json +52 -0
  329. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-v2/cmd.json +8 -0
  330. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-v2/encrypted-field-map.json +27 -0
  331. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-v2/encrypted-payload.json +41 -0
  332. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-v2/mongocryptd-reply.json +49 -0
  333. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int32-v2/cmd.json +8 -0
  334. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int32-v2/encrypted-field-map.json +27 -0
  335. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int32-v2/encrypted-payload.json +41 -0
  336. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int32-v2/mongocryptd-reply.json +49 -0
  337. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int64-v2/cmd.json +8 -0
  338. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int64-v2/encrypted-field-map.json +27 -0
  339. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int64-v2/encrypted-payload.json +41 -0
  340. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int64-v2/mongocryptd-reply.json +49 -0
  341. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/double/encrypted-payload-v2.json +26 -0
  342. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/double-precision/encrypted-payload-v2.json +26 -0
  343. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/int32/encrypted-payload-v2.json +26 -0
  344. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/int32-nominmax/encrypted-payload-v2.json +26 -0
  345. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/int32-openinterval/encrypted-payload-v2.json +16 -0
  346. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert/cmd.json +1 -1
  347. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/date/RNG_DATA.h +65 -65
  348. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/date-v2/cmd.json +13 -0
  349. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/date-v2/encrypted-field-map.json +27 -0
  350. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/date-v2/encrypted-payload.json +44 -0
  351. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/date-v2/mongocryptd-reply.json +52 -0
  352. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128/RNG_DATA.h +132 -132
  353. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-precision/RNG_DATA.h +71 -71
  354. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-precision-v2/cmd.json +9 -0
  355. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-precision-v2/encrypted-field-map.json +30 -0
  356. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-precision-v2/encrypted-payload.json +53 -0
  357. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-precision-v2/mongocryptd-reply.json +61 -0
  358. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-v2/cmd.json +9 -0
  359. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-v2/encrypted-field-map.json +27 -0
  360. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-v2/encrypted-payload.json +44 -0
  361. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-v2/mongocryptd-reply.json +52 -0
  362. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double/RNG_DATA.h +68 -68
  363. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-precision/RNG_DATA.h +19 -19
  364. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-precision-v2/cmd.json +11 -0
  365. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-precision-v2/encrypted-field-map.json +30 -0
  366. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-precision-v2/encrypted-payload.json +47 -0
  367. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-precision-v2/mongocryptd-reply.json +55 -0
  368. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-v2/cmd.json +11 -0
  369. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-v2/encrypted-field-map.json +27 -0
  370. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-v2/encrypted-payload.json +44 -0
  371. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-v2/mongocryptd-reply.json +52 -0
  372. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int32/RNG_DATA.h +25 -25
  373. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int32-v2/cmd.json +11 -0
  374. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int32-v2/encrypted-field-map.json +27 -0
  375. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int32-v2/encrypted-payload.json +44 -0
  376. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int32-v2/mongocryptd-reply.json +52 -0
  377. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int64/RNG_DATA.h +65 -65
  378. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int64-v2/cmd.json +11 -0
  379. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int64-v2/encrypted-field-map.json +27 -0
  380. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int64-v2/encrypted-payload.json +44 -0
  381. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int64-v2/mongocryptd-reply.json +52 -0
  382. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/double/RNG_DATA.h +68 -68
  383. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/double/encrypted-payload-v2.json +8 -0
  384. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/double-precision/RNG_DATA.h +19 -19
  385. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/double-precision/encrypted-payload-v2.json +8 -0
  386. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32/RNG_DATA.h +25 -25
  387. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32/encrypted-payload-v2.json +8 -0
  388. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32-nominmax/encrypted-payload-v2.json +8 -0
  389. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/sparsity-2/RNG_DATA.h +15 -15
  390. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/sparsity-2/encrypted-payload-v2.json +8 -0
  391. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-unindexed-v2/cmd.json +9 -0
  392. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-unindexed-v2/encrypted-field-map.json +18 -0
  393. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-unindexed-v2/encrypted-payload.json +14 -0
  394. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-unindexed-v2/mongocryptd-reply.json +41 -0
  395. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-v2/cmd.json +9 -0
  396. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-v2/encrypted-field-map.json +22 -0
  397. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-v2/encrypted-payload.json +39 -0
  398. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-v2/mongocryptd-reply.json +49 -0
  399. data/ext/libmongocrypt/libmongocrypt/test/data/iev-v2/FLECrudTest-insertOneRangeV2.json +10 -0
  400. data/ext/libmongocrypt/libmongocrypt/test/data/iev-v2/FLECrudTest-insertOneV2.json +10 -0
  401. data/ext/libmongocrypt/libmongocrypt/test/data/kms-azure/decrypt-response.txt +16 -0
  402. data/ext/libmongocrypt/libmongocrypt/test/data/kms-azure/encrypt-response.txt +16 -0
  403. data/ext/libmongocrypt/libmongocrypt/test/data/kms-azure/oauth-response.txt +19 -0
  404. data/ext/libmongocrypt/libmongocrypt/test/data/no-trimFactor/find/cmd.json +9 -0
  405. data/ext/libmongocrypt/libmongocrypt/test/data/no-trimFactor/find/encrypted-field-map.json +19 -0
  406. data/ext/libmongocrypt/libmongocrypt/test/data/no-trimFactor/find/encrypted-payload.json +62 -0
  407. data/ext/libmongocrypt/libmongocrypt/test/data/no-trimFactor/find/mongocryptd-reply.json +69 -0
  408. data/ext/libmongocrypt/libmongocrypt/test/data/no-trimFactor/insert/cmd.json +11 -0
  409. data/ext/libmongocrypt/libmongocrypt/test/data/no-trimFactor/insert/encrypted-field-map.json +19 -0
  410. data/ext/libmongocrypt/libmongocrypt/test/data/no-trimFactor/insert/encrypted-payload.json +40 -0
  411. data/ext/libmongocrypt/libmongocrypt/test/data/no-trimFactor/insert/mongocryptd-reply.json +47 -0
  412. data/ext/libmongocrypt/libmongocrypt/test/data/range-edge-generation/edges_decimal128.cstruct +1 -1
  413. data/ext/libmongocrypt/libmongocrypt/test/data/range-edge-generation/edges_double.cstruct +8637 -7958
  414. data/ext/libmongocrypt/libmongocrypt/test/data/range-edge-generation/edges_int32.cstruct +5522 -1382
  415. data/ext/libmongocrypt/libmongocrypt/test/data/range-edge-generation/edges_int64.cstruct +5042 -1262
  416. data/ext/libmongocrypt/libmongocrypt/test/data/range-min-cover/mincover_decimal128.cstruct +1 -1
  417. data/ext/libmongocrypt/libmongocrypt/test/data/range-min-cover/mincover_decimal128_precision.cstruct +1 -1
  418. data/ext/libmongocrypt/libmongocrypt/test/data/range-min-cover/mincover_double.cstruct +1 -1
  419. data/ext/libmongocrypt/libmongocrypt/test/data/range-min-cover/mincover_double_precision.cstruct +2 -2
  420. data/ext/libmongocrypt/libmongocrypt/test/data/range-min-cover/mincover_int32.cstruct +1 -1
  421. data/ext/libmongocrypt/libmongocrypt/test/data/range-min-cover/mincover_int64.cstruct +1 -1
  422. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/auto-find-int32/cmd.json +8 -0
  423. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/auto-find-int32/encrypted-field-map.json +19 -0
  424. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/auto-find-int32/encrypted-payload.json +53 -0
  425. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/auto-find-int32/mongocryptd-reply.json +58 -0
  426. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/auto-insert-int32/cmd.json +11 -0
  427. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/auto-insert-int32/encrypted-field-map.json +19 -0
  428. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/auto-insert-int32/encrypted-payload.json +40 -0
  429. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/auto-insert-int32/mongocryptd-reply.json +45 -0
  430. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/explicit-find-int32/expected.json +26 -0
  431. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/explicit-find-int32/to-encrypt.json +20 -0
  432. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/explicit-find-int32-defaults/expected.json +26 -0
  433. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/explicit-find-int32-defaults/to-encrypt.json +20 -0
  434. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/explicit-insert-double/expected.json +8 -0
  435. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/explicit-insert-int32/expected.json +8 -0
  436. data/ext/libmongocrypt/libmongocrypt/test/data/range-sends-cryptoParams/explicit-insert-int32-defaults/expected.json +8 -0
  437. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/README.md +17 -0
  438. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/aes-ctr.json +29 -0
  439. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2-fixed.json +10 -0
  440. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2-generated.json +38 -0
  441. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2aead-decrypt.json +35 -0
  442. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2aead-fixed.json +29 -0
  443. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2aead-generated.json +122 -0
  444. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2v2-aead-fixed.json +29 -0
  445. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2v2-aead-generated.json +122 -0
  446. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/mcgrew.json +12 -0
  447. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/nist.json +20 -0
  448. data/ext/libmongocrypt/libmongocrypt/test/data/tokens/README.md +30 -0
  449. data/ext/libmongocrypt/libmongocrypt/test/data/tokens/mc.json +25 -0
  450. data/ext/libmongocrypt/libmongocrypt/test/data/tokens/server.json +25 -0
  451. data/ext/libmongocrypt/libmongocrypt/test/example-no-bson.c +4 -4
  452. data/ext/libmongocrypt/libmongocrypt/test/example-state-machine.c +279 -323
  453. data/ext/libmongocrypt/libmongocrypt/test/fuzz_kms.c +8 -7
  454. data/ext/libmongocrypt/libmongocrypt/test/test-dll.cpp +6 -7
  455. data/ext/libmongocrypt/libmongocrypt/test/test-gcp-auth.c +221 -283
  456. data/ext/libmongocrypt/libmongocrypt/test/test-mc-efc.c +60 -73
  457. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-find-equality-payload-v2.c +78 -0
  458. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-find-range-payload-v2.c +129 -0
  459. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-payload-iev-v2.c +248 -0
  460. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-payload-iev.c +467 -414
  461. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-payload-iup-v2.c +248 -0
  462. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-payload-iup.c +141 -159
  463. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-payload-uev-v2.c +338 -0
  464. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-payload-uev.c +238 -176
  465. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-rfds.c +373 -474
  466. data/ext/libmongocrypt/libmongocrypt/test/test-mc-range-edge-generation.c +421 -388
  467. data/ext/libmongocrypt/libmongocrypt/test/test-mc-range-encoding.c +974 -825
  468. data/ext/libmongocrypt/libmongocrypt/test/test-mc-range-mincover.c +499 -481
  469. data/ext/libmongocrypt/libmongocrypt/test/test-mc-rangeopts.c +312 -148
  470. data/ext/libmongocrypt/libmongocrypt/test/test-mc-reader.c +124 -207
  471. data/ext/libmongocrypt/libmongocrypt/test/test-mc-tokens.c +249 -213
  472. data/ext/libmongocrypt/libmongocrypt/test/test-mc-writer.c +176 -0
  473. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-assert-match-bson.c +632 -808
  474. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-assert-match-bson.h +22 -2
  475. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-assert.h +168 -189
  476. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-buffer.c +187 -211
  477. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-cache-oauth.c +118 -37
  478. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-cache.c +210 -233
  479. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-ciphertext.c +185 -220
  480. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-cleanup.c +374 -0
  481. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-compact.c +410 -445
  482. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-crypto-hooks.c +798 -812
  483. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-crypto-std-hooks.c +142 -135
  484. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-crypto-std-hooks.h +70 -71
  485. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-crypto.c +394 -846
  486. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-csfle-lib.c +159 -183
  487. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-ctx-decrypt.c +870 -1045
  488. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-ctx-encrypt.c +5183 -4717
  489. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-ctx-rewrap-many-datakey.c +750 -964
  490. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-ctx-setopt.c +1093 -1178
  491. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-datakey.c +348 -420
  492. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-dll.c +23 -30
  493. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-endpoint.c +98 -111
  494. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-kek.c +49 -52
  495. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-key-broker.c +770 -920
  496. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-key-cache.c +354 -407
  497. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-key.c +197 -245
  498. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-kms-ctx.c +705 -368
  499. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-kms-responses.c +147 -166
  500. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-local-kms.c +50 -61
  501. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-log.c +85 -100
  502. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-marking.c +1073 -690
  503. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-opts.c +42 -0
  504. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-status.c +46 -58
  505. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-traverse-util.c +381 -451
  506. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-util.c +55 -67
  507. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-util.h +7 -10
  508. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt.c +879 -918
  509. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt.h +119 -169
  510. data/ext/libmongocrypt/libmongocrypt/test/test-named-kms-providers.c +2381 -0
  511. data/ext/libmongocrypt/libmongocrypt/test/util/HELP.autogen +3 -1
  512. data/ext/libmongocrypt/libmongocrypt/test/util/README.md +1 -0
  513. data/ext/libmongocrypt/libmongocrypt/test/util/csfle.c +512 -559
  514. data/ext/libmongocrypt/libmongocrypt/test/util/make_includes.py +1 -1
  515. data/ext/libmongocrypt/libmongocrypt/test/util/util.c +771 -882
  516. data/ext/libmongocrypt/libmongocrypt/test/util/util.h +33 -55
  517. data/lib/libmongocrypt_helper/version.rb +2 -2
  518. metadata +245 -111
  519. checksums.yaml.gz.sig +0 -0
  520. data/ext/libmongocrypt/libmongocrypt/VERSION_CURRENT +0 -1
  521. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/test/resources/fle2-find-range-explicit/int32/encrypted-payload.json +0 -26
  522. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/test/resources/fle2-find-range-explicit/int32/key-document.json +0 -0
  523. data/ext/libmongocrypt/libmongocrypt/bindings/node/CHANGELOG.md +0 -81
  524. data/ext/libmongocrypt/libmongocrypt/bindings/node/LICENSE +0 -201
  525. data/ext/libmongocrypt/libmongocrypt/bindings/node/binding.gyp +0 -79
  526. data/ext/libmongocrypt/libmongocrypt/bindings/node/etc/README.hbs +0 -44
  527. data/ext/libmongocrypt/libmongocrypt/bindings/node/etc/build-static.sh +0 -39
  528. data/ext/libmongocrypt/libmongocrypt/bindings/node/index.d.ts +0 -515
  529. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/autoEncrypter.js +0 -420
  530. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/buffer_pool.js +0 -123
  531. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/clientEncryption.js +0 -699
  532. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/common.js +0 -100
  533. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/credentialsProvider.js +0 -33
  534. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/cryptoCallbacks.js +0 -87
  535. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/index.js +0 -42
  536. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/mongocryptdManager.js +0 -66
  537. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/stateMachine.js +0 -487
  538. data/ext/libmongocrypt/libmongocrypt/bindings/node/package-lock.json +0 -16051
  539. data/ext/libmongocrypt/libmongocrypt/bindings/node/package.json +0 -81
  540. data/ext/libmongocrypt/libmongocrypt/bindings/node/src/mongocrypt.cc +0 -929
  541. data/ext/libmongocrypt/libmongocrypt/bindings/node/src/mongocrypt.h +0 -114
  542. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/autoEncrypter.test.js +0 -953
  543. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/buffer_pool.test.js +0 -91
  544. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/clientEncryption.test.js +0 -701
  545. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/credentialsProvider.test.js +0 -163
  546. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/cryptoCallbacks.test.js +0 -284
  547. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/data/README.md +0 -5
  548. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/data/cmd.json +0 -6
  549. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/data/collection-info.json +0 -37
  550. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/data/encrypted-document-nested.json +0 -8
  551. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/data/encrypted-document.json +0 -11
  552. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/data/encryptedFields.json +0 -30
  553. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/data/key-document.json +0 -32
  554. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/data/key1-document.json +0 -30
  555. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/data/mongocryptd-reply.json +0 -18
  556. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/mongocryptdManager.test.js +0 -48
  557. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/release.test.js +0 -59
  558. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/requirements.helper.js +0 -29
  559. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/stateMachine.test.js +0 -331
  560. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/tools/mongodb_reporter.js +0 -325
  561. data/ext/libmongocrypt/libmongocrypt/bindings/python/setup.py +0 -84
  562. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/fle2-find-range-explicit/int32/encrypted-payload.json +0 -26
  563. data/ext/libmongocrypt/libmongocrypt/bindings/python/test-requirements.txt +0 -12
  564. data/ext/libmongocrypt/libmongocrypt/debian/build_snapshot.sh +0 -79
  565. data/ext/libmongocrypt/libmongocrypt/debian/changelog +0 -105
  566. data/ext/libmongocrypt/libmongocrypt/debian/compat +0 -1
  567. data/ext/libmongocrypt/libmongocrypt/debian/control +0 -41
  568. data/ext/libmongocrypt/libmongocrypt/debian/copyright +0 -129
  569. data/ext/libmongocrypt/libmongocrypt/debian/gbp.conf +0 -23
  570. data/ext/libmongocrypt/libmongocrypt/debian/libmongocrypt-dev.dirs +0 -2
  571. data/ext/libmongocrypt/libmongocrypt/debian/libmongocrypt-dev.install +0 -5
  572. data/ext/libmongocrypt/libmongocrypt/debian/libmongocrypt0.dirs +0 -1
  573. data/ext/libmongocrypt/libmongocrypt/debian/libmongocrypt0.install +0 -1
  574. data/ext/libmongocrypt/libmongocrypt/debian/rules +0 -46
  575. data/ext/libmongocrypt/libmongocrypt/debian/source/format +0 -1
  576. data/ext/libmongocrypt/libmongocrypt/debian/source/lintian-overrides +0 -3
  577. data/ext/libmongocrypt/libmongocrypt/debian/source/options +0 -1
  578. data/ext/libmongocrypt/libmongocrypt/debian/watch +0 -3
  579. data/ext/libmongocrypt/libmongocrypt/test/data/aes-ctr.cstructs +0 -359
  580. data/ext/libmongocrypt/libmongocrypt/test/data/compact/success/mongocryptd-reply.json +0 -72
  581. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-aead.cstructs +0 -109
  582. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/int32-nominmax/encrypted-payload.json +0 -26
  583. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/int32-nominmax/rangeopts.json +0 -5
  584. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32-nominmax/RNG_DATA.h +0 -70
  585. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32-nominmax/encrypted-payload.json +0 -8
  586. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32-nominmax/rangeopts.json +0 -5
  587. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32-nominmax/value-to-encrypt.json +0 -5
  588. data/ext/libmongocrypt/libmongocrypt/test/data/fle2.cstructs +0 -33
  589. data.tar.gz.sig +0 -0
  590. metadata.gz.sig +0 -0
  591. /data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/test/resources/{fle2-find-range-explicit → fle2-find-range-explicit-v2}/int32/key-filter.json +0 -0
  592. /data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/test/resources/{fle2-find-range-explicit → fle2-find-range-explicit-v2}/int32/value-to-encrypt.json +0 -0
  593. /data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/{fle2-find-range-explicit → fle2-find-range-explicit-v2}/int32/value-to-encrypt.json +0 -0
  594. /data/ext/libmongocrypt/libmongocrypt/bindings/{java/mongocrypt/src/test/resources/fle2-find-range-explicit → python/test/data/fle2-find-rangePreview-explicit}/int32/rangeopts.json +0 -0
  595. /data/ext/libmongocrypt/libmongocrypt/{test/data/fle2-find-range-explicit/int32-nominmax → bindings/python/test/data/fle2-find-rangePreview-explicit/int32}/value-to-encrypt.json +0 -0
  596. /data/ext/libmongocrypt/libmongocrypt/{bindings/node/test/data/kms-decrypt-reply.txt → test/data/kms-aws/decrypt-response.txt} +0 -0
  597. /data/ext/libmongocrypt/libmongocrypt/{bindings/node/test/data/kms-encrypt-reply.txt → test/data/kms-aws/encrypt-response.txt} +0 -0
  598. /data/ext/libmongocrypt/libmongocrypt/test/data/{gcp-auth → kms-gcp}/decrypt-response.txt +0 -0
  599. /data/ext/libmongocrypt/libmongocrypt/test/data/{gcp-auth → kms-gcp}/encrypt-response.txt +0 -0
  600. /data/ext/libmongocrypt/libmongocrypt/test/data/{gcp-auth → kms-gcp}/oauth-response.txt +0 -0
@@ -14,31 +14,32 @@
14
14
  * limitations under the License.
15
15
  */
16
16
 
17
-
18
- #include "mongocrypt-private.h"
17
+ #include "kms_message/kms_kmip_request.h"
19
18
  #include "mongocrypt-binary-private.h"
20
19
  #include "mongocrypt-buffer-private.h"
20
+ #include "mongocrypt-crypto-private.h"
21
21
  #include "mongocrypt-ctx-private.h"
22
+ #include "mongocrypt-endpoint-private.h"
22
23
  #include "mongocrypt-kms-ctx-private.h"
24
+ #include "mongocrypt-log-private.h"
23
25
  #include "mongocrypt-opts-private.h"
26
+ #include "mongocrypt-private.h"
24
27
  #include "mongocrypt-status-private.h"
25
28
  #include "mongocrypt-util-private.h"
26
- #include <kms_message/kms_b64.h>
29
+ #include "mongocrypt.h"
27
30
  #include <kms_message/kms_azure_request.h>
31
+ #include <kms_message/kms_b64.h>
28
32
  #include <kms_message/kms_gcp_request.h>
29
- #include "mongocrypt.h"
30
33
 
31
34
  /* Sadly, Windows does not define SSIZE_MAX. It is defined in bson-compat.h,
32
35
  * but only since 1.22.x, so copy this from bson-compat.h for now. */
33
36
  #ifndef SSIZE_MAX
34
- #define SSIZE_MAX \
35
- (ssize_t) ( \
36
- (((size_t) 0x01u) << (sizeof (ssize_t) * (size_t) CHAR_BIT - 1u)) - 1u)
37
+ #define SSIZE_MAX (ssize_t)((((size_t)0x01u) << (sizeof(ssize_t) * (size_t)CHAR_BIT - 1u)) - 1u)
37
38
  #endif
38
39
 
39
40
  typedef struct {
40
- mongocrypt_status_t *status;
41
- void *ctx;
41
+ mongocrypt_status_t *status;
42
+ void *ctx;
42
43
  } ctx_with_status_t;
43
44
 
44
45
  /* Before we've read the Content-Length header in an HTTP response,
@@ -50,1544 +51,1537 @@ typedef struct {
50
51
  #define DEFAULT_HTTPS_PORT "443"
51
52
  #define DEFAULT_KMIP_PORT "5696"
52
53
 
53
- static bool
54
- _sha256 (void *ctx, const char *input, size_t len, unsigned char *hash_out)
55
- {
56
- BSON_ASSERT_PARAM (ctx);
57
- BSON_ASSERT_PARAM (input);
58
- BSON_ASSERT_PARAM (hash_out);
59
-
60
- bool ret;
61
- ctx_with_status_t *ctx_with_status = (ctx_with_status_t *) ctx;
62
- _mongocrypt_crypto_t *crypto;
63
- mongocrypt_binary_t *plaintext, *out;
64
-
65
- crypto = (_mongocrypt_crypto_t *) ctx_with_status->ctx;
66
- BSON_ASSERT (crypto);
67
- BSON_ASSERT (len <= UINT32_MAX);
68
- plaintext =
69
- mongocrypt_binary_new_from_data ((uint8_t *) input, (uint32_t) len);
70
- out = mongocrypt_binary_new ();
71
-
72
- out->data = hash_out;
73
- out->len = SHA256_LEN;
74
-
75
- ret = crypto->sha_256 (crypto->ctx, plaintext, out, ctx_with_status->status);
76
-
77
- mongocrypt_binary_destroy (plaintext);
78
- mongocrypt_binary_destroy (out);
79
- return ret;
54
+ static bool _sha256(void *ctx, const char *input, size_t len, unsigned char *hash_out) {
55
+ BSON_ASSERT_PARAM(ctx);
56
+ BSON_ASSERT_PARAM(input);
57
+ BSON_ASSERT_PARAM(hash_out);
58
+
59
+ bool ret;
60
+ ctx_with_status_t *ctx_with_status = (ctx_with_status_t *)ctx;
61
+ _mongocrypt_crypto_t *crypto;
62
+ mongocrypt_binary_t *plaintext, *out;
63
+
64
+ crypto = (_mongocrypt_crypto_t *)ctx_with_status->ctx;
65
+ BSON_ASSERT(crypto);
66
+ BSON_ASSERT(len <= UINT32_MAX);
67
+ plaintext = mongocrypt_binary_new_from_data((uint8_t *)input, (uint32_t)len);
68
+ out = mongocrypt_binary_new();
69
+
70
+ out->data = hash_out;
71
+ out->len = SHA256_LEN;
72
+
73
+ ret = crypto->sha_256(crypto->ctx, plaintext, out, ctx_with_status->status);
74
+
75
+ mongocrypt_binary_destroy(plaintext);
76
+ mongocrypt_binary_destroy(out);
77
+ return ret;
80
78
  }
81
79
 
82
80
  static bool
83
- _sha256_hmac (void *ctx,
84
- const char *key_input,
85
- size_t key_len,
86
- const char *input,
87
- size_t len,
88
- unsigned char *hash_out)
89
- {
90
- BSON_ASSERT_PARAM (ctx);
91
- BSON_ASSERT_PARAM (key_input);
92
- BSON_ASSERT_PARAM (input);
93
- BSON_ASSERT_PARAM (hash_out);
94
-
95
- ctx_with_status_t *ctx_with_status = (ctx_with_status_t *) ctx;
96
- _mongocrypt_crypto_t *crypto;
97
- mongocrypt_binary_t *key, *plaintext, *out;
98
- bool ret;
99
-
100
- crypto = (_mongocrypt_crypto_t *) ctx_with_status->ctx;
101
- BSON_ASSERT (crypto);
102
-
103
- BSON_ASSERT (key_len <= UINT32_MAX);
104
- key = mongocrypt_binary_new_from_data ((uint8_t *) key_input,
105
- (uint32_t) key_len);
106
- BSON_ASSERT (len <= UINT32_MAX);
107
- plaintext =
108
- mongocrypt_binary_new_from_data ((uint8_t *) input, (uint32_t) len);
109
- out = mongocrypt_binary_new ();
110
-
111
- out->data = hash_out;
112
- out->len = SHA256_LEN;
113
-
114
- ret = crypto->hmac_sha_256 (
115
- crypto->ctx, key, plaintext, out, ctx_with_status->status);
116
-
117
- mongocrypt_binary_destroy (key);
118
- mongocrypt_binary_destroy (plaintext);
119
- mongocrypt_binary_destroy (out);
120
- return ret;
81
+ _sha256_hmac(void *ctx, const char *key_input, size_t key_len, const char *input, size_t len, unsigned char *hash_out) {
82
+ BSON_ASSERT_PARAM(ctx);
83
+ BSON_ASSERT_PARAM(key_input);
84
+ BSON_ASSERT_PARAM(input);
85
+ BSON_ASSERT_PARAM(hash_out);
86
+
87
+ ctx_with_status_t *ctx_with_status = (ctx_with_status_t *)ctx;
88
+ _mongocrypt_crypto_t *crypto;
89
+ mongocrypt_binary_t *key, *plaintext, *out;
90
+ bool ret;
91
+
92
+ crypto = (_mongocrypt_crypto_t *)ctx_with_status->ctx;
93
+ BSON_ASSERT(crypto);
94
+
95
+ BSON_ASSERT(key_len <= UINT32_MAX);
96
+ key = mongocrypt_binary_new_from_data((uint8_t *)key_input, (uint32_t)key_len);
97
+ BSON_ASSERT(len <= UINT32_MAX);
98
+ plaintext = mongocrypt_binary_new_from_data((uint8_t *)input, (uint32_t)len);
99
+ out = mongocrypt_binary_new();
100
+
101
+ out->data = hash_out;
102
+ out->len = SHA256_LEN;
103
+
104
+ ret = crypto->hmac_sha_256(crypto->ctx, key, plaintext, out, ctx_with_status->status);
105
+
106
+ mongocrypt_binary_destroy(key);
107
+ mongocrypt_binary_destroy(plaintext);
108
+ mongocrypt_binary_destroy(out);
109
+ return ret;
121
110
  }
122
111
 
123
112
  static void
124
- _set_kms_crypto_hooks (_mongocrypt_crypto_t *crypto,
125
- ctx_with_status_t *ctx_with_status,
126
- kms_request_opt_t *opts)
127
- {
128
- BSON_ASSERT_PARAM (crypto);
129
- BSON_ASSERT_PARAM (ctx_with_status);
130
- BSON_ASSERT_PARAM (opts);
131
-
132
- if (crypto->hooks_enabled) {
133
- kms_request_opt_set_crypto_hooks (
134
- opts, _sha256, _sha256_hmac, ctx_with_status);
135
- }
113
+ _set_kms_crypto_hooks(_mongocrypt_crypto_t *crypto, ctx_with_status_t *ctx_with_status, kms_request_opt_t *opts) {
114
+ BSON_ASSERT_PARAM(crypto);
115
+ BSON_ASSERT_PARAM(ctx_with_status);
116
+ BSON_ASSERT_PARAM(opts);
117
+
118
+ if (crypto->hooks_enabled) {
119
+ kms_request_opt_set_crypto_hooks(opts, _sha256, _sha256_hmac, ctx_with_status);
120
+ }
136
121
  }
137
122
 
138
- static bool
139
- is_kms (_kms_request_type_t kms_type)
140
- {
141
- return kms_type == MONGOCRYPT_KMS_KMIP_REGISTER ||
142
- kms_type == MONGOCRYPT_KMS_KMIP_ACTIVATE ||
143
- kms_type == MONGOCRYPT_KMS_KMIP_GET;
123
+ static bool is_kms(_kms_request_type_t kms_type) {
124
+ return kms_type == MONGOCRYPT_KMS_KMIP_REGISTER || kms_type == MONGOCRYPT_KMS_KMIP_ACTIVATE
125
+ || kms_type == MONGOCRYPT_KMS_KMIP_GET || kms_type == MONGOCRYPT_KMS_KMIP_ENCRYPT
126
+ || kms_type == MONGOCRYPT_KMS_KMIP_DECRYPT || kms_type == MONGOCRYPT_KMS_KMIP_CREATE;
144
127
  }
145
128
 
146
129
  static void
147
- _init_common (mongocrypt_kms_ctx_t *kms,
148
- _mongocrypt_log_t *log,
149
- _kms_request_type_t kms_type)
150
- {
151
- BSON_ASSERT_PARAM (kms);
152
-
153
- if (is_kms (kms_type)) {
154
- kms->parser = kms_kmip_response_parser_new (NULL /* reserved */);
155
- } else {
156
- kms->parser = kms_response_parser_new ();
157
- }
158
- kms->log = log;
159
- kms->status = mongocrypt_status_new ();
160
- kms->req_type = kms_type;
161
- _mongocrypt_buffer_init (&kms->result);
130
+ _init_common(mongocrypt_kms_ctx_t *kms, _mongocrypt_log_t *log, _kms_request_type_t kms_type, const char *kmsid) {
131
+ BSON_ASSERT_PARAM(kms);
132
+ BSON_ASSERT_PARAM(kmsid);
133
+
134
+ kms->kmsid = bson_strdup(kmsid);
135
+
136
+ if (is_kms(kms_type)) {
137
+ kms->parser = kms_kmip_response_parser_new(NULL /* reserved */);
138
+ } else {
139
+ kms->parser = kms_response_parser_new();
140
+ }
141
+ kms->log = log;
142
+ kms->status = mongocrypt_status_new();
143
+ kms->req_type = kms_type;
144
+ _mongocrypt_buffer_init(&kms->result);
162
145
  }
163
146
 
164
- bool
165
- _mongocrypt_kms_ctx_init_aws_decrypt (
166
- mongocrypt_kms_ctx_t *kms,
167
- _mongocrypt_opts_kms_providers_t *kms_providers,
168
- _mongocrypt_key_doc_t *key,
169
- _mongocrypt_log_t *log,
170
- _mongocrypt_crypto_t *crypto)
171
- {
172
- BSON_ASSERT_PARAM (kms);
173
- BSON_ASSERT_PARAM (key);
174
- BSON_ASSERT_PARAM (kms_providers);
175
- BSON_ASSERT_PARAM (crypto);
176
-
177
- kms_request_opt_t *opt;
178
- mongocrypt_status_t *status;
179
- ctx_with_status_t ctx_with_status;
180
- bool ret = false;
181
-
182
- _init_common (kms, log, MONGOCRYPT_KMS_AWS_DECRYPT);
183
- status = kms->status;
184
- ctx_with_status.ctx = crypto;
185
- ctx_with_status.status = mongocrypt_status_new ();
186
-
187
- if (!key->kek.kms_provider) {
188
- CLIENT_ERR ("no kms provider specified on key");
189
- goto done;
190
- }
191
-
192
- if (MONGOCRYPT_KMS_PROVIDER_AWS != key->kek.kms_provider) {
193
- CLIENT_ERR ("expected aws kms provider");
194
- goto done;
195
- }
196
-
197
- if (!key->kek.provider.aws.region) {
198
- CLIENT_ERR ("no key region provided");
199
- goto done;
200
- }
201
-
202
- if (0 ==
203
- (kms_providers->configured_providers & MONGOCRYPT_KMS_PROVIDER_AWS)) {
204
- CLIENT_ERR ("aws kms not configured");
205
- goto done;
206
- }
207
-
208
- if (!kms_providers->aws.access_key_id) {
209
- CLIENT_ERR ("aws access key id not provided");
210
- goto done;
211
- }
212
-
213
- if (!kms_providers->aws.secret_access_key) {
214
- CLIENT_ERR ("aws secret access key not provided");
215
- goto done;
216
- }
217
-
218
- /* create the KMS request. */
219
- opt = kms_request_opt_new ();
220
- BSON_ASSERT (opt);
221
-
222
- _set_kms_crypto_hooks (crypto, &ctx_with_status, opt);
223
- kms_request_opt_set_connection_close (opt, true);
224
-
225
- kms->req = kms_decrypt_request_new (
226
- key->key_material.data, key->key_material.len, opt);
227
-
228
- kms_request_opt_destroy (opt);
229
- if (!kms_request_set_service (kms->req, "kms")) {
230
- CLIENT_ERR ("failed to set service: %s",
231
- kms_request_get_error (kms->req));
232
- _mongocrypt_status_append (status, ctx_with_status.status);
233
- goto done;
234
- }
235
-
236
- if (kms_providers->aws.session_token) {
237
- if (!kms_request_add_header_field (kms->req,
238
- "X-Amz-Security-Token",
239
- kms_providers->aws.session_token)) {
240
- CLIENT_ERR ("failed to set session token: %s",
241
- kms_request_get_error (kms->req));
242
- _mongocrypt_status_append (status, ctx_with_status.status);
243
- goto done;
244
- }
245
- }
246
-
247
- if (kms_request_get_error (kms->req)) {
248
- CLIENT_ERR ("error constructing KMS message: %s",
249
- kms_request_get_error (kms->req));
250
- _mongocrypt_status_append (status, ctx_with_status.status);
251
- goto done;
252
- }
253
-
254
- /* If an endpoint was set, override the default Host header. */
255
- if (key->kek.provider.aws.endpoint) {
256
- if (!kms_request_add_header_field (
257
- kms->req, "Host", key->kek.provider.aws.endpoint->host_and_port)) {
258
- CLIENT_ERR ("error constructing KMS message: %s",
259
- kms_request_get_error (kms->req));
260
- _mongocrypt_status_append (status, ctx_with_status.status);
261
- goto done;
262
- }
263
- }
264
-
265
- if (!kms_request_set_region (kms->req, key->kek.provider.aws.region)) {
266
- CLIENT_ERR ("failed to set region: %s", kms_request_get_error (kms->req));
267
- _mongocrypt_status_append (status, ctx_with_status.status);
268
- goto done;
269
- }
270
-
271
- if (!kms_request_set_access_key_id (kms->req,
272
- kms_providers->aws.access_key_id)) {
273
- CLIENT_ERR ("failed to set aws access key id: %s",
274
- kms_request_get_error (kms->req));
275
- _mongocrypt_status_append (status, ctx_with_status.status);
276
- goto done;
277
- }
278
- if (!kms_request_set_secret_key (kms->req,
279
- kms_providers->aws.secret_access_key)) {
280
- CLIENT_ERR ("failed to set aws secret access key: %s",
281
- kms_request_get_error (kms->req));
282
- _mongocrypt_status_append (status, ctx_with_status.status);
283
- goto done;
284
- }
285
-
286
- _mongocrypt_buffer_init (&kms->msg);
287
- kms->msg.data = (uint8_t *) kms_request_get_signed (kms->req);
288
- if (!kms->msg.data) {
289
- CLIENT_ERR ("failed to create KMS message: %s",
290
- kms_request_get_error (kms->req));
291
- _mongocrypt_status_append (status, ctx_with_status.status);
292
- goto done;
293
- }
294
- kms->msg.len = (uint32_t) strlen ((char *) kms->msg.data);
295
- kms->msg.owned = true;
296
-
297
- if (key->kek.provider.aws.endpoint) {
298
- kms->endpoint =
299
- bson_strdup (key->kek.provider.aws.endpoint->host_and_port);
300
- } else {
301
- /* construct the endpoint from AWS region. */
302
- kms->endpoint = bson_strdup_printf ("kms.%s.amazonaws.com",
303
- key->kek.provider.aws.region);
304
- }
305
- _mongocrypt_apply_default_port (&kms->endpoint, DEFAULT_HTTPS_PORT);
306
-
307
- ret = true;
147
+ bool _mongocrypt_kms_ctx_init_aws_decrypt(mongocrypt_kms_ctx_t *kms,
148
+ _mongocrypt_opts_kms_providers_t *kms_providers,
149
+ _mongocrypt_key_doc_t *key,
150
+ _mongocrypt_crypto_t *crypto,
151
+ const char *kmsid,
152
+ _mongocrypt_log_t *log) {
153
+ BSON_ASSERT_PARAM(kms);
154
+ BSON_ASSERT_PARAM(key);
155
+ BSON_ASSERT_PARAM(kms_providers);
156
+ BSON_ASSERT_PARAM(crypto);
157
+
158
+ kms_request_opt_t *opt;
159
+ mongocrypt_status_t *status;
160
+ ctx_with_status_t ctx_with_status;
161
+ bool ret = false;
162
+
163
+ _init_common(kms, log, MONGOCRYPT_KMS_AWS_DECRYPT, kmsid);
164
+ status = kms->status;
165
+ ctx_with_status.ctx = crypto;
166
+ ctx_with_status.status = mongocrypt_status_new();
167
+
168
+ if (!key->kek.kms_provider) {
169
+ CLIENT_ERR("no kms provider specified on key");
170
+ goto done;
171
+ }
172
+
173
+ if (MONGOCRYPT_KMS_PROVIDER_AWS != key->kek.kms_provider) {
174
+ CLIENT_ERR("expected aws kms provider");
175
+ goto done;
176
+ }
177
+
178
+ if (!key->kek.provider.aws.region) {
179
+ CLIENT_ERR("no key region provided");
180
+ goto done;
181
+ }
182
+
183
+ mc_kms_creds_t kc;
184
+ if (!_mongocrypt_opts_kms_providers_lookup(kms_providers, key->kek.kmsid, &kc)) {
185
+ CLIENT_ERR("KMS provider `%s` is not configured", key->kek.kmsid);
186
+ goto done;
187
+ }
188
+ BSON_ASSERT(kc.type == MONGOCRYPT_KMS_PROVIDER_AWS);
189
+
190
+ if (!kc.value.aws.access_key_id) {
191
+ CLIENT_ERR("aws access key id not provided");
192
+ goto done;
193
+ }
194
+
195
+ if (!kc.value.aws.secret_access_key) {
196
+ CLIENT_ERR("aws secret access key not provided");
197
+ goto done;
198
+ }
199
+
200
+ /* create the KMS request. */
201
+ opt = kms_request_opt_new();
202
+ BSON_ASSERT(opt);
203
+
204
+ _set_kms_crypto_hooks(crypto, &ctx_with_status, opt);
205
+ kms_request_opt_set_connection_close(opt, true);
206
+
207
+ kms->req = kms_decrypt_request_new(key->key_material.data, key->key_material.len, opt);
208
+
209
+ kms_request_opt_destroy(opt);
210
+ if (!kms_request_set_service(kms->req, "kms")) {
211
+ CLIENT_ERR("failed to set service: %s", kms_request_get_error(kms->req));
212
+ _mongocrypt_status_append(status, ctx_with_status.status);
213
+ goto done;
214
+ }
215
+
216
+ if (kc.value.aws.session_token) {
217
+ if (!kms_request_add_header_field(kms->req, "X-Amz-Security-Token", kc.value.aws.session_token)) {
218
+ CLIENT_ERR("failed to set session token: %s", kms_request_get_error(kms->req));
219
+ _mongocrypt_status_append(status, ctx_with_status.status);
220
+ goto done;
221
+ }
222
+ }
223
+
224
+ if (kms_request_get_error(kms->req)) {
225
+ CLIENT_ERR("error constructing KMS message: %s", kms_request_get_error(kms->req));
226
+ _mongocrypt_status_append(status, ctx_with_status.status);
227
+ goto done;
228
+ }
229
+
230
+ /* If an endpoint was set, override the default Host header. */
231
+ if (key->kek.provider.aws.endpoint) {
232
+ if (!kms_request_add_header_field(kms->req, "Host", key->kek.provider.aws.endpoint->host_and_port)) {
233
+ CLIENT_ERR("error constructing KMS message: %s", kms_request_get_error(kms->req));
234
+ _mongocrypt_status_append(status, ctx_with_status.status);
235
+ goto done;
236
+ }
237
+ }
238
+
239
+ if (!kms_request_set_region(kms->req, key->kek.provider.aws.region)) {
240
+ CLIENT_ERR("failed to set region: %s", kms_request_get_error(kms->req));
241
+ _mongocrypt_status_append(status, ctx_with_status.status);
242
+ goto done;
243
+ }
244
+
245
+ if (!kms_request_set_access_key_id(kms->req, kc.value.aws.access_key_id)) {
246
+ CLIENT_ERR("failed to set aws access key id: %s", kms_request_get_error(kms->req));
247
+ _mongocrypt_status_append(status, ctx_with_status.status);
248
+ goto done;
249
+ }
250
+ if (!kms_request_set_secret_key(kms->req, kc.value.aws.secret_access_key)) {
251
+ CLIENT_ERR("failed to set aws secret access key: %s", kms_request_get_error(kms->req));
252
+ _mongocrypt_status_append(status, ctx_with_status.status);
253
+ goto done;
254
+ }
255
+
256
+ _mongocrypt_buffer_init(&kms->msg);
257
+ kms->msg.data = (uint8_t *)kms_request_get_signed(kms->req);
258
+ if (!kms->msg.data) {
259
+ CLIENT_ERR("failed to create KMS message: %s", kms_request_get_error(kms->req));
260
+ _mongocrypt_status_append(status, ctx_with_status.status);
261
+ goto done;
262
+ }
263
+ kms->msg.len = (uint32_t)strlen((char *)kms->msg.data);
264
+ kms->msg.owned = true;
265
+
266
+ if (key->kek.provider.aws.endpoint) {
267
+ kms->endpoint = bson_strdup(key->kek.provider.aws.endpoint->host_and_port);
268
+ } else {
269
+ /* construct the endpoint from AWS region. */
270
+ kms->endpoint = bson_strdup_printf("kms.%s.amazonaws.com", key->kek.provider.aws.region);
271
+ }
272
+ _mongocrypt_apply_default_port(&kms->endpoint, DEFAULT_HTTPS_PORT);
273
+
274
+ ret = true;
308
275
  done:
309
- mongocrypt_status_destroy (ctx_with_status.status);
276
+ mongocrypt_status_destroy(ctx_with_status.status);
310
277
 
311
- return ret;
278
+ return ret;
312
279
  }
313
280
 
314
-
315
- bool
316
- _mongocrypt_kms_ctx_init_aws_encrypt (
317
- mongocrypt_kms_ctx_t *kms,
318
- _mongocrypt_opts_kms_providers_t *kms_providers,
319
- _mongocrypt_ctx_opts_t *ctx_opts,
320
- _mongocrypt_buffer_t *plaintext_key_material,
321
- _mongocrypt_log_t *log,
322
- _mongocrypt_crypto_t *crypto)
323
- {
324
- BSON_ASSERT_PARAM (kms);
325
- BSON_ASSERT_PARAM (ctx_opts);
326
- BSON_ASSERT_PARAM (kms_providers);
327
- BSON_ASSERT_PARAM (crypto);
328
- BSON_ASSERT_PARAM (plaintext_key_material);
329
-
330
- kms_request_opt_t *opt;
331
- mongocrypt_status_t *status;
332
- ctx_with_status_t ctx_with_status;
333
- bool ret = false;
334
-
335
- _init_common (kms, log, MONGOCRYPT_KMS_AWS_ENCRYPT);
336
- status = kms->status;
337
- ctx_with_status.ctx = crypto;
338
- ctx_with_status.status = mongocrypt_status_new ();
339
-
340
- if (MONGOCRYPT_KMS_PROVIDER_AWS != ctx_opts->kek.kms_provider) {
341
- CLIENT_ERR ("expected aws kms provider");
342
- goto done;
343
- }
344
-
345
- if (!ctx_opts->kek.provider.aws.region) {
346
- CLIENT_ERR ("no key region provided");
347
- goto done;
348
- }
349
-
350
- if (!ctx_opts->kek.provider.aws.cmk) {
351
- CLIENT_ERR ("no aws cmk provided");
352
- goto done;
353
- }
354
-
355
- if (0 ==
356
- (kms_providers->configured_providers & MONGOCRYPT_KMS_PROVIDER_AWS)) {
357
- CLIENT_ERR ("aws kms not configured");
358
- goto done;
359
- }
360
-
361
- if (!kms_providers->aws.access_key_id) {
362
- CLIENT_ERR ("aws access key id not provided");
363
- goto done;
364
- }
365
-
366
- if (!kms_providers->aws.secret_access_key) {
367
- CLIENT_ERR ("aws secret access key not provided");
368
- goto done;
369
- }
370
-
371
- /* create the KMS request. */
372
- opt = kms_request_opt_new ();
373
- BSON_ASSERT (opt);
374
-
375
- _set_kms_crypto_hooks (crypto, &ctx_with_status, opt);
376
- kms_request_opt_set_connection_close (opt, true);
377
-
378
- kms->req = kms_encrypt_request_new (plaintext_key_material->data,
281
+ bool _mongocrypt_kms_ctx_init_aws_encrypt(mongocrypt_kms_ctx_t *kms,
282
+ _mongocrypt_opts_kms_providers_t *kms_providers,
283
+ _mongocrypt_ctx_opts_t *ctx_opts,
284
+ _mongocrypt_buffer_t *plaintext_key_material,
285
+ _mongocrypt_crypto_t *crypto,
286
+ const char *kmsid,
287
+ _mongocrypt_log_t *log) {
288
+ BSON_ASSERT_PARAM(kms);
289
+ BSON_ASSERT_PARAM(ctx_opts);
290
+ BSON_ASSERT_PARAM(kms_providers);
291
+ BSON_ASSERT_PARAM(crypto);
292
+ BSON_ASSERT_PARAM(plaintext_key_material);
293
+
294
+ kms_request_opt_t *opt;
295
+ mongocrypt_status_t *status;
296
+ ctx_with_status_t ctx_with_status;
297
+ bool ret = false;
298
+
299
+ _init_common(kms, log, MONGOCRYPT_KMS_AWS_ENCRYPT, kmsid);
300
+ status = kms->status;
301
+ ctx_with_status.ctx = crypto;
302
+ ctx_with_status.status = mongocrypt_status_new();
303
+
304
+ if (MONGOCRYPT_KMS_PROVIDER_AWS != ctx_opts->kek.kms_provider) {
305
+ CLIENT_ERR("expected aws kms provider");
306
+ goto done;
307
+ }
308
+
309
+ if (!ctx_opts->kek.provider.aws.region) {
310
+ CLIENT_ERR("no key region provided");
311
+ goto done;
312
+ }
313
+
314
+ if (!ctx_opts->kek.provider.aws.cmk) {
315
+ CLIENT_ERR("no aws cmk provided");
316
+ goto done;
317
+ }
318
+
319
+ mc_kms_creds_t kc;
320
+ if (!_mongocrypt_opts_kms_providers_lookup(kms_providers, ctx_opts->kek.kmsid, &kc)) {
321
+ CLIENT_ERR("KMS provider `%s` is not configured", ctx_opts->kek.kmsid);
322
+ goto done;
323
+ }
324
+ BSON_ASSERT(kc.type == MONGOCRYPT_KMS_PROVIDER_AWS);
325
+
326
+ if (!kc.value.aws.access_key_id) {
327
+ CLIENT_ERR("aws access key id not provided");
328
+ goto done;
329
+ }
330
+
331
+ if (!kc.value.aws.secret_access_key) {
332
+ CLIENT_ERR("aws secret access key not provided");
333
+ goto done;
334
+ }
335
+
336
+ /* create the KMS request. */
337
+ opt = kms_request_opt_new();
338
+ BSON_ASSERT(opt);
339
+
340
+ _set_kms_crypto_hooks(crypto, &ctx_with_status, opt);
341
+ kms_request_opt_set_connection_close(opt, true);
342
+
343
+ kms->req = kms_encrypt_request_new(plaintext_key_material->data,
379
344
  plaintext_key_material->len,
380
345
  ctx_opts->kek.provider.aws.cmk,
381
346
  opt);
382
347
 
383
- kms_request_opt_destroy (opt);
384
- if (!kms_request_set_service (kms->req, "kms")) {
385
- CLIENT_ERR ("failed to set service: %s",
386
- kms_request_get_error (kms->req));
387
- _mongocrypt_status_append (status, ctx_with_status.status);
388
- goto done;
389
- }
390
-
391
- if (kms_providers->aws.session_token) {
392
- if (!kms_request_add_header_field (kms->req,
393
- "X-Amz-Security-Token",
394
- kms_providers->aws.session_token)) {
395
- CLIENT_ERR ("failed to set session token: %s",
396
- kms_request_get_error (kms->req));
397
- _mongocrypt_status_append (status, ctx_with_status.status);
398
- goto done;
399
- }
400
- }
401
-
402
- if (kms_request_get_error (kms->req)) {
403
- CLIENT_ERR ("error constructing KMS message: %s",
404
- kms_request_get_error (kms->req));
405
- _mongocrypt_status_append (status, ctx_with_status.status);
406
- goto done;
407
- }
408
-
409
- /* If an endpoint was set, override the default Host header. */
410
- if (ctx_opts->kek.provider.aws.endpoint) {
411
- if (!kms_request_add_header_field (
412
- kms->req, "Host", ctx_opts->kek.provider.aws.endpoint->host)) {
413
- CLIENT_ERR ("error constructing KMS message: %s",
414
- kms_request_get_error (kms->req));
415
- _mongocrypt_status_append (status, ctx_with_status.status);
416
- goto done;
417
- }
418
- }
419
-
420
- if (!kms_request_set_region (kms->req, ctx_opts->kek.provider.aws.region)) {
421
- CLIENT_ERR ("failed to set region: %s", kms_request_get_error (kms->req));
422
- _mongocrypt_status_append (status, ctx_with_status.status);
423
- goto done;
424
- }
425
-
426
- if (!kms_request_set_access_key_id (kms->req,
427
- kms_providers->aws.access_key_id)) {
428
- CLIENT_ERR ("failed to set aws access key id: %s",
429
- kms_request_get_error (kms->req));
430
- _mongocrypt_status_append (status, ctx_with_status.status);
431
- goto done;
432
- }
433
- if (!kms_request_set_secret_key (kms->req,
434
- kms_providers->aws.secret_access_key)) {
435
- CLIENT_ERR ("failed to set aws secret access key: %s",
436
- kms_request_get_error (kms->req));
437
- _mongocrypt_status_append (status, ctx_with_status.status);
438
- goto done;
439
- }
440
-
441
- _mongocrypt_buffer_init (&kms->msg);
442
- kms->msg.data = (uint8_t *) kms_request_get_signed (kms->req);
443
- if (!kms->msg.data) {
444
- CLIENT_ERR ("failed to create KMS message: %s",
445
- kms_request_get_error (kms->req));
446
- _mongocrypt_status_append (status, ctx_with_status.status);
447
- goto done;
448
- }
449
- kms->msg.len = (uint32_t) strlen ((char *) kms->msg.data);
450
- kms->msg.owned = true;
451
-
452
- /* construct the endpoint */
453
- if (ctx_opts->kek.provider.aws.endpoint) {
454
- kms->endpoint =
455
- bson_strdup (ctx_opts->kek.provider.aws.endpoint->host_and_port);
456
- } else {
457
- kms->endpoint = bson_strdup_printf ("kms.%s.amazonaws.com",
458
- ctx_opts->kek.provider.aws.region);
459
- }
460
- _mongocrypt_apply_default_port (&kms->endpoint, DEFAULT_HTTPS_PORT);
461
-
462
- ret = true;
348
+ kms_request_opt_destroy(opt);
349
+ if (!kms_request_set_service(kms->req, "kms")) {
350
+ CLIENT_ERR("failed to set service: %s", kms_request_get_error(kms->req));
351
+ _mongocrypt_status_append(status, ctx_with_status.status);
352
+ goto done;
353
+ }
354
+
355
+ if (kc.value.aws.session_token) {
356
+ if (!kms_request_add_header_field(kms->req, "X-Amz-Security-Token", kc.value.aws.session_token)) {
357
+ CLIENT_ERR("failed to set session token: %s", kms_request_get_error(kms->req));
358
+ _mongocrypt_status_append(status, ctx_with_status.status);
359
+ goto done;
360
+ }
361
+ }
362
+
363
+ if (kms_request_get_error(kms->req)) {
364
+ CLIENT_ERR("error constructing KMS message: %s", kms_request_get_error(kms->req));
365
+ _mongocrypt_status_append(status, ctx_with_status.status);
366
+ goto done;
367
+ }
368
+
369
+ /* If an endpoint was set, override the default Host header. */
370
+ if (ctx_opts->kek.provider.aws.endpoint) {
371
+ if (!kms_request_add_header_field(kms->req, "Host", ctx_opts->kek.provider.aws.endpoint->host)) {
372
+ CLIENT_ERR("error constructing KMS message: %s", kms_request_get_error(kms->req));
373
+ _mongocrypt_status_append(status, ctx_with_status.status);
374
+ goto done;
375
+ }
376
+ }
377
+
378
+ if (!kms_request_set_region(kms->req, ctx_opts->kek.provider.aws.region)) {
379
+ CLIENT_ERR("failed to set region: %s", kms_request_get_error(kms->req));
380
+ _mongocrypt_status_append(status, ctx_with_status.status);
381
+ goto done;
382
+ }
383
+
384
+ if (!kms_request_set_access_key_id(kms->req, kc.value.aws.access_key_id)) {
385
+ CLIENT_ERR("failed to set aws access key id: %s", kms_request_get_error(kms->req));
386
+ _mongocrypt_status_append(status, ctx_with_status.status);
387
+ goto done;
388
+ }
389
+ if (!kms_request_set_secret_key(kms->req, kc.value.aws.secret_access_key)) {
390
+ CLIENT_ERR("failed to set aws secret access key: %s", kms_request_get_error(kms->req));
391
+ _mongocrypt_status_append(status, ctx_with_status.status);
392
+ goto done;
393
+ }
394
+
395
+ _mongocrypt_buffer_init(&kms->msg);
396
+ kms->msg.data = (uint8_t *)kms_request_get_signed(kms->req);
397
+ if (!kms->msg.data) {
398
+ CLIENT_ERR("failed to create KMS message: %s", kms_request_get_error(kms->req));
399
+ _mongocrypt_status_append(status, ctx_with_status.status);
400
+ goto done;
401
+ }
402
+ kms->msg.len = (uint32_t)strlen((char *)kms->msg.data);
403
+ kms->msg.owned = true;
404
+
405
+ /* construct the endpoint */
406
+ if (ctx_opts->kek.provider.aws.endpoint) {
407
+ kms->endpoint = bson_strdup(ctx_opts->kek.provider.aws.endpoint->host_and_port);
408
+ } else {
409
+ kms->endpoint = bson_strdup_printf("kms.%s.amazonaws.com", ctx_opts->kek.provider.aws.region);
410
+ }
411
+ _mongocrypt_apply_default_port(&kms->endpoint, DEFAULT_HTTPS_PORT);
412
+
413
+ ret = true;
463
414
  done:
464
- mongocrypt_status_destroy (ctx_with_status.status);
465
- return ret;
415
+ mongocrypt_status_destroy(ctx_with_status.status);
416
+ return ret;
466
417
  }
467
418
 
468
-
469
- uint32_t
470
- mongocrypt_kms_ctx_bytes_needed (mongocrypt_kms_ctx_t *kms)
471
- {
472
- int want_bytes;
473
-
474
- if (!kms) {
475
- return 0;
476
- }
477
- /* TODO: an oddity of kms-message. After retrieving the result, it
478
- * resets the parser. */
479
- if (!mongocrypt_status_ok (kms->status) ||
480
- !_mongocrypt_buffer_empty (&kms->result)) {
481
- return 0;
482
- }
483
- want_bytes = kms_response_parser_wants_bytes (kms->parser,
484
- DEFAULT_MAX_KMS_BYTE_REQUEST);
485
- BSON_ASSERT (want_bytes >= 0);
486
- return (uint32_t) want_bytes;
419
+ uint32_t mongocrypt_kms_ctx_bytes_needed(mongocrypt_kms_ctx_t *kms) {
420
+ int want_bytes;
421
+
422
+ if (!kms) {
423
+ return 0;
424
+ }
425
+ /* TODO: an oddity of kms-message. After retrieving the result, it
426
+ * resets the parser. */
427
+ if (!mongocrypt_status_ok(kms->status) || !_mongocrypt_buffer_empty(&kms->result)) {
428
+ return 0;
429
+ }
430
+ want_bytes = kms_response_parser_wants_bytes(kms->parser, DEFAULT_MAX_KMS_BYTE_REQUEST);
431
+ BSON_ASSERT(want_bytes >= 0);
432
+ return (uint32_t)want_bytes;
487
433
  }
488
434
 
489
435
  static void
490
- _handle_non200_http_status (int http_status,
491
- const char *body,
492
- size_t body_len,
493
- mongocrypt_status_t *status)
494
- {
495
- BSON_ASSERT_PARAM (body);
496
-
497
- /* 1xx, 2xx, and 3xx HTTP status codes are not errors, but we only
498
- * support handling 200 response. */
499
- if (http_status < 400) {
500
- CLIENT_ERR ("Unsupported HTTP code in KMS response. HTTP status=%d. "
501
- "Response body=\n%s",
502
- http_status,
503
- body);
504
- return;
505
- }
506
-
507
- /* Either empty body or body containing JSON with error message. */
508
- if (body_len == 0) {
509
- CLIENT_ERR ("Error in KMS response. HTTP status=%d. Empty body.",
510
- http_status);
511
- return;
512
- }
513
-
514
- CLIENT_ERR ("Error in KMS response. HTTP status=%d. Response body=\n%s",
515
- http_status,
516
- body);
436
+ _handle_non200_http_status(int http_status, const char *body, size_t body_len, mongocrypt_status_t *status) {
437
+ BSON_ASSERT_PARAM(body);
438
+
439
+ /* 1xx, 2xx, and 3xx HTTP status codes are not errors, but we only
440
+ * support handling 200 response. */
441
+ if (http_status < 400) {
442
+ CLIENT_ERR("Unsupported HTTP code in KMS response. HTTP status=%d. "
443
+ "Response body=\n%s",
444
+ http_status,
445
+ body);
446
+ return;
447
+ }
448
+
449
+ /* Either empty body or body containing JSON with error message. */
450
+ if (body_len == 0) {
451
+ CLIENT_ERR("Error in KMS response. HTTP status=%d. Empty body.", http_status);
452
+ return;
453
+ }
454
+
455
+ CLIENT_ERR("Error in KMS response. HTTP status=%d. Response body=\n%s", http_status, body);
517
456
  }
518
457
 
519
458
  /* An AWS KMS context has received full response. Parse out the result or error.
520
459
  */
521
- static bool
522
- _ctx_done_aws (mongocrypt_kms_ctx_t *kms, const char *json_field)
523
- {
524
- BSON_ASSERT_PARAM (kms);
525
- BSON_ASSERT_PARAM (json_field);
526
-
527
- kms_response_t *response = NULL;
528
- const char *body;
529
- bson_t body_bson = BSON_INITIALIZER;
530
- bool ret;
531
- bson_error_t bson_error;
532
- bson_iter_t iter;
533
- uint32_t b64_strlen;
534
- char *b64_str;
535
- int http_status;
536
- size_t body_len;
537
- int result_len;
538
- mongocrypt_status_t *status;
539
-
540
- status = kms->status;
541
- ret = false;
542
- /* Parse out the {en|de}crypted result. */
543
- http_status = kms_response_parser_status (kms->parser);
544
- response = kms_response_parser_get_response (kms->parser);
545
- body = kms_response_get_body (response, &body_len);
546
-
547
- if (http_status != 200) {
548
- _handle_non200_http_status (http_status, body, body_len, status);
549
- goto fail;
550
- }
551
-
552
- /* If HTTP response succeeded (status 200) then body should contain JSON.
553
- */
554
- bson_destroy (&body_bson);
555
- if (body_len > (size_t) SSIZE_MAX) {
556
- CLIENT_ERR ("Error parsing JSON in KMS response '%s'. "
557
- "Response body exceeds maximum supported length",
558
- bson_error.message);
559
- bson_init (&body_bson);
560
- goto fail;
561
- }
562
- if (!bson_init_from_json (
563
- &body_bson, body, (ssize_t) body_len, &bson_error)) {
564
- CLIENT_ERR ("Error parsing JSON in KMS response '%s'. "
565
- "HTTP status=%d. Response body=\n%s",
566
- bson_error.message,
567
- http_status,
568
- body);
569
- bson_init (&body_bson);
570
- goto fail;
571
- }
572
-
573
- if (!bson_iter_init_find (&iter, &body_bson, json_field) ||
574
- !BSON_ITER_HOLDS_UTF8 (&iter)) {
575
- CLIENT_ERR (
576
- "KMS JSON response does not include field '%s'. HTTP status=%d. "
577
- "Response body=\n%s",
578
- json_field,
579
- http_status,
580
- body);
581
- goto fail;
582
- }
583
-
584
- b64_str = (char *) bson_iter_utf8 (&iter, &b64_strlen);
585
- BSON_ASSERT (b64_str);
586
- uint8_t *result_data = bson_malloc ((size_t) b64_strlen + 1u);
587
- BSON_ASSERT (result_data);
588
-
589
- result_len = kms_message_b64_pton (b64_str, result_data, b64_strlen);
590
- if (result_len < 0) {
591
- CLIENT_ERR (
592
- "Failed to base64 decode response. HTTP status=%d. Response body=\n%s",
593
- http_status,
594
- body);
595
- bson_free (result_data);
596
- goto fail;
597
- }
598
- kms->result.data = result_data;
599
- kms->result.len = (uint32_t) result_len;
600
- kms->result.owned = true;
601
- ret = true;
460
+ static bool _ctx_done_aws(mongocrypt_kms_ctx_t *kms, const char *json_field) {
461
+ BSON_ASSERT_PARAM(kms);
462
+ BSON_ASSERT_PARAM(json_field);
463
+
464
+ kms_response_t *response = NULL;
465
+ const char *body;
466
+ bson_t body_bson = BSON_INITIALIZER;
467
+ bool ret;
468
+ bson_error_t bson_error;
469
+ bson_iter_t iter;
470
+ uint32_t b64_strlen;
471
+ char *b64_str;
472
+ int http_status;
473
+ size_t body_len;
474
+ int result_len;
475
+ mongocrypt_status_t *status;
476
+
477
+ status = kms->status;
478
+ ret = false;
479
+ /* Parse out the {en|de}crypted result. */
480
+ http_status = kms_response_parser_status(kms->parser);
481
+ response = kms_response_parser_get_response(kms->parser);
482
+ if (!response) {
483
+ CLIENT_ERR("Failed to get response from parser: %s", kms_response_parser_error(kms->parser));
484
+ goto fail;
485
+ }
486
+ body = kms_response_get_body(response, &body_len);
487
+
488
+ if (http_status != 200) {
489
+ _handle_non200_http_status(http_status, body, body_len, status);
490
+ goto fail;
491
+ }
492
+
493
+ /* If HTTP response succeeded (status 200) then body should contain JSON.
494
+ */
495
+ bson_destroy(&body_bson);
496
+ if (body_len > (size_t)SSIZE_MAX) {
497
+ CLIENT_ERR("Error parsing JSON in KMS response '%s'. "
498
+ "Response body exceeds maximum supported length",
499
+ bson_error.message);
500
+ bson_init(&body_bson);
501
+ goto fail;
502
+ }
503
+ if (!bson_init_from_json(&body_bson, body, (ssize_t)body_len, &bson_error)) {
504
+ CLIENT_ERR("Error parsing JSON in KMS response '%s'. "
505
+ "HTTP status=%d. Response body=\n%s",
506
+ bson_error.message,
507
+ http_status,
508
+ body);
509
+ bson_init(&body_bson);
510
+ goto fail;
511
+ }
512
+
513
+ if (!bson_iter_init_find(&iter, &body_bson, json_field) || !BSON_ITER_HOLDS_UTF8(&iter)) {
514
+ CLIENT_ERR("KMS JSON response does not include field '%s'. HTTP status=%d. "
515
+ "Response body=\n%s",
516
+ json_field,
517
+ http_status,
518
+ body);
519
+ goto fail;
520
+ }
521
+
522
+ b64_str = (char *)bson_iter_utf8(&iter, &b64_strlen);
523
+ BSON_ASSERT(b64_str);
524
+ uint8_t *result_data = bson_malloc((size_t)b64_strlen + 1u);
525
+ BSON_ASSERT(result_data);
526
+
527
+ result_len = kms_message_b64_pton(b64_str, result_data, b64_strlen);
528
+ if (result_len < 0) {
529
+ CLIENT_ERR("Failed to base64 decode response. HTTP status=%d. Response body=\n%s", http_status, body);
530
+ bson_free(result_data);
531
+ goto fail;
532
+ }
533
+ kms->result.data = result_data;
534
+ kms->result.len = (uint32_t)result_len;
535
+ kms->result.owned = true;
536
+ ret = true;
602
537
  fail:
603
- bson_destroy (&body_bson);
604
- kms_response_destroy (response);
605
- return ret;
538
+ bson_destroy(&body_bson);
539
+ kms_response_destroy(response);
540
+ return ret;
606
541
  }
607
542
 
608
543
  /* A Azure/GCP oauth KMS context has received full response. Parse out the
609
544
  * bearer token or error. */
610
- static bool
611
- _ctx_done_oauth (mongocrypt_kms_ctx_t *kms)
612
- {
613
- BSON_ASSERT_PARAM (kms);
614
-
615
- kms_response_t *response = NULL;
616
- const char *body;
617
- bson_t *bson_body = NULL;
618
- bool ret;
619
- bson_error_t bson_error;
620
- bson_iter_t iter;
621
- int http_status;
622
- size_t body_len;
623
- mongocrypt_status_t *status;
624
-
625
- status = kms->status;
626
- ret = false;
627
- /* Parse out the oauth token result (or error). */
628
- http_status = kms_response_parser_status (kms->parser);
629
- response = kms_response_parser_get_response (kms->parser);
630
- body = kms_response_get_body (response, &body_len);
631
-
632
- if (body_len == 0) {
633
- CLIENT_ERR ("Empty KMS response. HTTP status=%d", http_status);
634
- goto fail;
635
- }
636
-
637
- if (body_len > (size_t) SSIZE_MAX) {
638
- CLIENT_ERR ("Error parsing JSON in KMS response '%s'. "
639
- "Response body exceeds maximum supported length",
640
- bson_error.message);
641
- goto fail;
642
- }
643
- bson_body = bson_new_from_json (
644
- (const uint8_t *) body, (ssize_t) body_len, &bson_error);
645
- if (!bson_body) {
646
- CLIENT_ERR ("Error parsing JSON in KMS response '%s'. "
647
- "HTTP status=%d. Response body=\n%s",
648
- bson_error.message,
649
- http_status,
650
- body);
651
- goto fail;
652
- }
653
-
654
- if (http_status != 200) {
655
- _handle_non200_http_status (http_status, body, body_len, status);
656
- goto fail;
657
- }
658
-
659
- if (!bson_iter_init_find (&iter, bson_body, "access_token") ||
660
- !BSON_ITER_HOLDS_UTF8 (&iter)) {
661
- CLIENT_ERR ("Invalid KMS response. KMS JSON response does not include "
662
- "field 'access_token'. "
663
- "HTTP status=%d. Response body=\n%s",
664
- http_status,
665
- body);
666
- goto fail;
667
- }
668
-
669
- /* Store the full response, to include the expiration time. */
670
- _mongocrypt_buffer_steal_from_bson (&kms->result, bson_body);
671
- bson_body = NULL;
672
-
673
- ret = true;
545
+ static bool _ctx_done_oauth(mongocrypt_kms_ctx_t *kms) {
546
+ BSON_ASSERT_PARAM(kms);
547
+
548
+ kms_response_t *response = NULL;
549
+ const char *body;
550
+ bson_t *bson_body = NULL;
551
+ bool ret;
552
+ bson_error_t bson_error;
553
+ bson_iter_t iter;
554
+ int http_status;
555
+ size_t body_len;
556
+ mongocrypt_status_t *status;
557
+
558
+ status = kms->status;
559
+ ret = false;
560
+ /* Parse out the oauth token result (or error). */
561
+ http_status = kms_response_parser_status(kms->parser);
562
+ response = kms_response_parser_get_response(kms->parser);
563
+ if (!response) {
564
+ CLIENT_ERR("Failed to get response from parser: %s", kms_response_parser_error(kms->parser));
565
+ goto fail;
566
+ }
567
+ body = kms_response_get_body(response, &body_len);
568
+
569
+ if (body_len == 0) {
570
+ CLIENT_ERR("Empty KMS response. HTTP status=%d", http_status);
571
+ goto fail;
572
+ }
573
+
574
+ if (body_len > (size_t)SSIZE_MAX) {
575
+ CLIENT_ERR("Error parsing JSON in KMS response '%s'. "
576
+ "Response body exceeds maximum supported length",
577
+ bson_error.message);
578
+ goto fail;
579
+ }
580
+ bson_body = bson_new_from_json((const uint8_t *)body, (ssize_t)body_len, &bson_error);
581
+ if (!bson_body) {
582
+ CLIENT_ERR("Error parsing JSON in KMS response '%s'. "
583
+ "HTTP status=%d. Response body=\n%s",
584
+ bson_error.message,
585
+ http_status,
586
+ body);
587
+ goto fail;
588
+ }
589
+
590
+ if (http_status != 200) {
591
+ _handle_non200_http_status(http_status, body, body_len, status);
592
+ goto fail;
593
+ }
594
+
595
+ if (!bson_iter_init_find(&iter, bson_body, "access_token") || !BSON_ITER_HOLDS_UTF8(&iter)) {
596
+ CLIENT_ERR("Invalid KMS response. KMS JSON response does not include "
597
+ "field 'access_token'. "
598
+ "HTTP status=%d. Response body=\n%s",
599
+ http_status,
600
+ body);
601
+ goto fail;
602
+ }
603
+
604
+ /* Store the full response, to include the expiration time. */
605
+ _mongocrypt_buffer_steal_from_bson(&kms->result, bson_body);
606
+ bson_body = NULL;
607
+
608
+ ret = true;
674
609
  fail:
675
- bson_destroy (bson_body);
676
- kms_response_destroy (response);
677
- return ret;
610
+ bson_destroy(bson_body);
611
+ kms_response_destroy(response);
612
+ return ret;
678
613
  }
679
614
 
680
615
  /* An Azure oauth KMS context has received full response. Parse out the bearer
681
616
  * token or error. */
682
- static bool
683
- _ctx_done_azure_wrapkey_unwrapkey (mongocrypt_kms_ctx_t *kms)
684
- {
685
- BSON_ASSERT_PARAM (kms);
686
-
687
- kms_response_t *response = NULL;
688
- const char *body;
689
- bson_t *bson_body = NULL;
690
- bool ret;
691
- bson_error_t bson_error;
692
- bson_iter_t iter;
693
- int http_status;
694
- size_t body_len;
695
- mongocrypt_status_t *status;
696
- const char *b64url_data = NULL;
697
- uint32_t b64url_len;
698
- char *b64_data = NULL;
699
- uint32_t b64_len;
700
- int result_len;
701
-
702
- status = kms->status;
703
- ret = false;
704
- /* Parse out the oauth token result (or error). */
705
- http_status = kms_response_parser_status (kms->parser);
706
- response = kms_response_parser_get_response (kms->parser);
707
- body = kms_response_get_body (response, &body_len);
708
-
709
- if (body_len == 0) {
710
- CLIENT_ERR ("Empty KMS response. HTTP status=%d", http_status);
711
- goto fail;
712
- }
713
-
714
- if (body_len > (size_t) SSIZE_MAX) {
715
- CLIENT_ERR ("Error parsing JSON in KMS response '%s'. "
716
- "Response body exceeds maximum supported length",
717
- bson_error.message);
718
- goto fail;
719
- }
720
- bson_body = bson_new_from_json (
721
- (const uint8_t *) body, (ssize_t) body_len, &bson_error);
722
- if (!bson_body) {
723
- CLIENT_ERR ("Error parsing JSON in KMS response '%s'. "
724
- "HTTP status=%d. Response body=\n%s",
725
- bson_error.message,
726
- http_status,
727
- body);
728
- goto fail;
729
- }
730
-
731
- if (http_status != 200) {
732
- _handle_non200_http_status (http_status, body, body_len, status);
733
- goto fail;
734
- }
735
-
736
- if (!bson_iter_init_find (&iter, bson_body, "value") ||
737
- !BSON_ITER_HOLDS_UTF8 (&iter)) {
738
- CLIENT_ERR (
739
- "KMS JSON response does not include field 'value'. HTTP status=%d. "
740
- "Response body=\n%s",
741
- http_status,
742
- body);
743
- goto fail;
744
- }
745
-
746
- b64url_data = bson_iter_utf8 (&iter, &b64url_len);
747
- BSON_ASSERT (b64url_len <= UINT32_MAX - 4u);
748
- /* add four for padding. */
749
- b64_len = b64url_len + 4;
750
- b64_data = bson_malloc0 (b64_len);
751
- if (kms_message_b64url_to_b64 (b64url_data, b64url_len, b64_data, b64_len) ==
752
- -1) {
753
- CLIENT_ERR ("Error converting base64url to base64");
754
- goto fail;
755
- }
756
-
757
- uint8_t *result_data = bson_malloc (b64_len);
758
- BSON_ASSERT (result_data);
759
- result_len = kms_message_b64_pton (b64_data, result_data, b64_len);
760
- if (result_len < 0) {
761
- CLIENT_ERR (
762
- "Failed to base64 decode response. HTTP status=%d. Response body=\n%s",
763
- http_status,
764
- body);
765
- bson_free (result_data);
766
- goto fail;
767
- }
768
-
769
- kms->result.data = result_data;
770
- kms->result.len = (uint32_t) result_len;
771
- kms->result.owned = true;
772
-
773
- ret = true;
617
+ static bool _ctx_done_azure_wrapkey_unwrapkey(mongocrypt_kms_ctx_t *kms) {
618
+ BSON_ASSERT_PARAM(kms);
619
+
620
+ kms_response_t *response = NULL;
621
+ const char *body;
622
+ bson_t *bson_body = NULL;
623
+ bool ret;
624
+ bson_error_t bson_error;
625
+ bson_iter_t iter;
626
+ int http_status;
627
+ size_t body_len;
628
+ mongocrypt_status_t *status;
629
+ const char *b64url_data = NULL;
630
+ uint32_t b64url_len;
631
+ char *b64_data = NULL;
632
+ uint32_t b64_len;
633
+ int result_len;
634
+
635
+ status = kms->status;
636
+ ret = false;
637
+ /* Parse out the oauth token result (or error). */
638
+ http_status = kms_response_parser_status(kms->parser);
639
+ response = kms_response_parser_get_response(kms->parser);
640
+ if (!response) {
641
+ CLIENT_ERR("Failed to get response from parser: %s", kms_response_parser_error(kms->parser));
642
+ goto fail;
643
+ }
644
+ body = kms_response_get_body(response, &body_len);
645
+
646
+ if (body_len == 0) {
647
+ CLIENT_ERR("Empty KMS response. HTTP status=%d", http_status);
648
+ goto fail;
649
+ }
650
+
651
+ if (body_len > (size_t)SSIZE_MAX) {
652
+ CLIENT_ERR("Error parsing JSON in KMS response '%s'. "
653
+ "Response body exceeds maximum supported length",
654
+ bson_error.message);
655
+ goto fail;
656
+ }
657
+ bson_body = bson_new_from_json((const uint8_t *)body, (ssize_t)body_len, &bson_error);
658
+ if (!bson_body) {
659
+ CLIENT_ERR("Error parsing JSON in KMS response '%s'. "
660
+ "HTTP status=%d. Response body=\n%s",
661
+ bson_error.message,
662
+ http_status,
663
+ body);
664
+ goto fail;
665
+ }
666
+
667
+ if (http_status != 200) {
668
+ _handle_non200_http_status(http_status, body, body_len, status);
669
+ goto fail;
670
+ }
671
+
672
+ if (!bson_iter_init_find(&iter, bson_body, "value") || !BSON_ITER_HOLDS_UTF8(&iter)) {
673
+ CLIENT_ERR("KMS JSON response does not include field 'value'. HTTP status=%d. "
674
+ "Response body=\n%s",
675
+ http_status,
676
+ body);
677
+ goto fail;
678
+ }
679
+
680
+ b64url_data = bson_iter_utf8(&iter, &b64url_len);
681
+ BSON_ASSERT(b64url_len <= UINT32_MAX - 4u);
682
+ /* add four for padding. */
683
+ b64_len = b64url_len + 4;
684
+ b64_data = bson_malloc0(b64_len);
685
+ if (kms_message_b64url_to_b64(b64url_data, b64url_len, b64_data, b64_len) == -1) {
686
+ CLIENT_ERR("Error converting base64url to base64");
687
+ goto fail;
688
+ }
689
+
690
+ uint8_t *result_data = bson_malloc(b64_len);
691
+ BSON_ASSERT(result_data);
692
+ result_len = kms_message_b64_pton(b64_data, result_data, b64_len);
693
+ if (result_len < 0) {
694
+ CLIENT_ERR("Failed to base64 decode response. HTTP status=%d. Response body=\n%s", http_status, body);
695
+ bson_free(result_data);
696
+ goto fail;
697
+ }
698
+
699
+ kms->result.data = result_data;
700
+ kms->result.len = (uint32_t)result_len;
701
+ kms->result.owned = true;
702
+
703
+ ret = true;
774
704
  fail:
775
- bson_destroy (bson_body);
776
- kms_response_destroy (response);
777
- bson_free (b64_data);
778
- return ret;
705
+ bson_destroy(bson_body);
706
+ kms_response_destroy(response);
707
+ bson_free(b64_data);
708
+ return ret;
779
709
  }
780
710
 
781
711
  /* A GCP KMS context has received full response. Parse out the result or error.
782
712
  */
783
- static bool
784
- _ctx_done_gcp (mongocrypt_kms_ctx_t *kms, const char *json_field)
785
- {
786
- BSON_ASSERT_PARAM (kms);
787
- BSON_ASSERT_PARAM (json_field);
788
-
789
- kms_response_t *response = NULL;
790
- const char *body;
791
- bson_t body_bson = BSON_INITIALIZER;
792
- bool ret;
793
- bson_error_t bson_error;
794
- bson_iter_t iter;
795
- size_t outlen;
796
- char *b64_str;
797
- int http_status;
798
- size_t body_len;
799
- mongocrypt_status_t *status;
800
-
801
- status = kms->status;
802
- ret = false;
803
- /* Parse out the {en|de}crypted result. */
804
- http_status = kms_response_parser_status (kms->parser);
805
- response = kms_response_parser_get_response (kms->parser);
806
- body = kms_response_get_body (response, &body_len);
807
-
808
- if (http_status != 200) {
809
- _handle_non200_http_status (http_status, body, body_len, status);
810
- goto fail;
811
- }
812
-
813
- /* If HTTP response succeeded (status 200) then body should contain JSON.
814
- */
815
- bson_destroy (&body_bson);
816
- if (body_len > (size_t) SSIZE_MAX) {
817
- CLIENT_ERR ("Error parsing JSON in KMS response '%s'. "
818
- "Response body exceeds maximum supported length",
819
- bson_error.message);
820
- bson_init (&body_bson);
821
- goto fail;
822
- }
823
- if (!bson_init_from_json (
824
- &body_bson, body, (ssize_t) body_len, &bson_error)) {
825
- CLIENT_ERR ("Error parsing JSON in KMS response '%s'. "
826
- "HTTP status=%d. Response body=\n%s",
827
- bson_error.message,
828
- http_status,
829
- body);
830
- bson_init (&body_bson);
831
- goto fail;
832
- }
833
-
834
- if (!bson_iter_init_find (&iter, &body_bson, json_field) ||
835
- !BSON_ITER_HOLDS_UTF8 (&iter)) {
836
- CLIENT_ERR (
837
- "KMS JSON response does not include field '%s'. HTTP status=%d. "
838
- "Response body=\n%s",
839
- json_field,
840
- http_status,
841
- body);
842
- goto fail;
843
- }
844
-
845
-
846
- b64_str = (char *) bson_iter_utf8 (&iter, NULL);
847
- BSON_ASSERT (b64_str);
848
- kms->result.data = kms_message_b64_to_raw (b64_str, &outlen);
849
- BSON_ASSERT (outlen <= UINT32_MAX);
850
- kms->result.len = (uint32_t) outlen;
851
- kms->result.owned = true;
852
- ret = true;
713
+ static bool _ctx_done_gcp(mongocrypt_kms_ctx_t *kms, const char *json_field) {
714
+ BSON_ASSERT_PARAM(kms);
715
+ BSON_ASSERT_PARAM(json_field);
716
+
717
+ kms_response_t *response = NULL;
718
+ const char *body;
719
+ bson_t body_bson = BSON_INITIALIZER;
720
+ bool ret;
721
+ bson_error_t bson_error;
722
+ bson_iter_t iter;
723
+ size_t outlen;
724
+ char *b64_str;
725
+ int http_status;
726
+ size_t body_len;
727
+ mongocrypt_status_t *status;
728
+
729
+ status = kms->status;
730
+ ret = false;
731
+ /* Parse out the {en|de}crypted result. */
732
+ http_status = kms_response_parser_status(kms->parser);
733
+ response = kms_response_parser_get_response(kms->parser);
734
+ if (!response) {
735
+ CLIENT_ERR("Failed to get response from parser: %s", kms_response_parser_error(kms->parser));
736
+ goto fail;
737
+ }
738
+ body = kms_response_get_body(response, &body_len);
739
+
740
+ if (http_status != 200) {
741
+ _handle_non200_http_status(http_status, body, body_len, status);
742
+ goto fail;
743
+ }
744
+
745
+ /* If HTTP response succeeded (status 200) then body should contain JSON.
746
+ */
747
+ bson_destroy(&body_bson);
748
+ if (body_len > (size_t)SSIZE_MAX) {
749
+ CLIENT_ERR("Error parsing JSON in KMS response '%s'. "
750
+ "Response body exceeds maximum supported length",
751
+ bson_error.message);
752
+ bson_init(&body_bson);
753
+ goto fail;
754
+ }
755
+ if (!bson_init_from_json(&body_bson, body, (ssize_t)body_len, &bson_error)) {
756
+ CLIENT_ERR("Error parsing JSON in KMS response '%s'. "
757
+ "HTTP status=%d. Response body=\n%s",
758
+ bson_error.message,
759
+ http_status,
760
+ body);
761
+ bson_init(&body_bson);
762
+ goto fail;
763
+ }
764
+
765
+ if (!bson_iter_init_find(&iter, &body_bson, json_field) || !BSON_ITER_HOLDS_UTF8(&iter)) {
766
+ CLIENT_ERR("KMS JSON response does not include field '%s'. HTTP status=%d. "
767
+ "Response body=\n%s",
768
+ json_field,
769
+ http_status,
770
+ body);
771
+ goto fail;
772
+ }
773
+
774
+ b64_str = (char *)bson_iter_utf8(&iter, NULL);
775
+ BSON_ASSERT(b64_str);
776
+ kms->result.data = kms_message_b64_to_raw(b64_str, &outlen);
777
+ BSON_ASSERT(outlen <= UINT32_MAX);
778
+ kms->result.len = (uint32_t)outlen;
779
+ kms->result.owned = true;
780
+ ret = true;
853
781
  fail:
854
- bson_destroy (&body_bson);
855
- kms_response_destroy (response);
856
- return ret;
782
+ bson_destroy(&body_bson);
783
+ kms_response_destroy(response);
784
+ return ret;
857
785
  }
858
786
 
859
- static bool
860
- _ctx_done_kmip_register (mongocrypt_kms_ctx_t *kms_ctx)
861
- {
862
- BSON_ASSERT_PARAM (kms_ctx);
863
-
864
- kms_response_t *res = NULL;
865
-
866
- mongocrypt_status_t *status = kms_ctx->status;
867
- bool ret = false;
868
- char *uid;
869
-
870
- res = kms_response_parser_get_response (kms_ctx->parser);
871
- if (!res) {
872
- CLIENT_ERR ("Error getting KMIP response: %s",
873
- kms_response_parser_error (kms_ctx->parser));
874
- goto done;
875
- }
876
-
877
- uid = kms_kmip_response_get_unique_identifier (res);
878
- if (!uid) {
879
- CLIENT_ERR (
880
- "Error getting UniqueIdentifer from KMIP Register response: %s",
881
- kms_response_get_error (res));
882
- goto done;
883
- }
884
-
885
- if (!_mongocrypt_buffer_steal_from_string (&kms_ctx->result, uid)) {
886
- CLIENT_ERR ("Error storing KMS UniqueIdentifer result");
887
- bson_free (uid);
888
- goto done;
889
- }
890
- ret = true;
787
+ static bool _ctx_done_kmip_register(mongocrypt_kms_ctx_t *kms_ctx) {
788
+ BSON_ASSERT_PARAM(kms_ctx);
789
+
790
+ kms_response_t *res = NULL;
791
+
792
+ mongocrypt_status_t *status = kms_ctx->status;
793
+ bool ret = false;
794
+ char *uid;
795
+
796
+ res = kms_response_parser_get_response(kms_ctx->parser);
797
+ if (!res) {
798
+ CLIENT_ERR("Error getting KMIP response: %s", kms_response_parser_error(kms_ctx->parser));
799
+ goto done;
800
+ }
801
+
802
+ uid = kms_kmip_response_get_unique_identifier(res);
803
+ if (!uid) {
804
+ CLIENT_ERR("Error getting UniqueIdentifer from KMIP Register response: %s", kms_response_get_error(res));
805
+ goto done;
806
+ }
807
+
808
+ if (!_mongocrypt_buffer_steal_from_string(&kms_ctx->result, uid)) {
809
+ CLIENT_ERR("Error storing KMS UniqueIdentifer result");
810
+ bson_free(uid);
811
+ goto done;
812
+ }
813
+ ret = true;
891
814
 
892
815
  done:
893
- kms_response_destroy (res);
894
- return ret;
816
+ kms_response_destroy(res);
817
+ return ret;
895
818
  }
896
819
 
897
- static bool
898
- _ctx_done_kmip_activate (mongocrypt_kms_ctx_t *kms_ctx)
899
- {
900
- BSON_ASSERT_PARAM (kms_ctx);
901
- return _ctx_done_kmip_register (kms_ctx);
820
+ static bool _ctx_done_kmip_activate(mongocrypt_kms_ctx_t *kms_ctx) {
821
+ BSON_ASSERT_PARAM(kms_ctx);
822
+ return _ctx_done_kmip_register(kms_ctx);
902
823
  }
903
824
 
904
- static bool
905
- _ctx_done_kmip_get (mongocrypt_kms_ctx_t *kms_ctx)
906
- {
907
- BSON_ASSERT_PARAM (kms_ctx);
908
-
909
- kms_response_t *res = NULL;
910
-
911
- mongocrypt_status_t *status = kms_ctx->status;
912
- bool ret = false;
913
- uint8_t *secretdata;
914
- size_t secretdata_len;
915
-
916
- res = kms_response_parser_get_response (kms_ctx->parser);
917
- if (!res) {
918
- CLIENT_ERR ("Error getting KMIP response: %s",
919
- kms_response_parser_error (kms_ctx->parser));
920
- goto done;
921
- }
922
-
923
- secretdata = kms_kmip_response_get_secretdata (res, &secretdata_len);
924
- if (!secretdata) {
925
- CLIENT_ERR ("Error getting SecretData from KMIP Get response: %s",
926
- kms_response_get_error (res));
927
- goto done;
928
- }
929
-
930
- if (!_mongocrypt_buffer_steal_from_data_and_size (
931
- &kms_ctx->result, secretdata, secretdata_len)) {
932
- CLIENT_ERR ("Error storing KMS SecretData result");
933
- bson_free (secretdata);
934
- goto done;
935
- }
936
-
937
- ret = true;
825
+ static bool _ctx_done_kmip_get(mongocrypt_kms_ctx_t *kms_ctx) {
826
+ BSON_ASSERT_PARAM(kms_ctx);
827
+
828
+ kms_response_t *res = NULL;
829
+
830
+ mongocrypt_status_t *status = kms_ctx->status;
831
+ bool ret = false;
832
+ uint8_t *secretdata;
833
+ size_t secretdata_len;
834
+
835
+ res = kms_response_parser_get_response(kms_ctx->parser);
836
+ if (!res) {
837
+ CLIENT_ERR("Error getting KMIP response: %s", kms_response_parser_error(kms_ctx->parser));
838
+ goto done;
839
+ }
840
+
841
+ secretdata = kms_kmip_response_get_secretdata(res, &secretdata_len);
842
+ if (!secretdata) {
843
+ CLIENT_ERR("Error getting SecretData from KMIP Get response: %s", kms_response_get_error(res));
844
+ goto done;
845
+ }
846
+
847
+ if (!_mongocrypt_buffer_steal_from_data_and_size(&kms_ctx->result, secretdata, secretdata_len)) {
848
+ CLIENT_ERR("Error storing KMS SecretData result");
849
+ bson_free(secretdata);
850
+ goto done;
851
+ }
852
+
853
+ ret = true;
938
854
 
939
855
  done:
940
- kms_response_destroy (res);
941
- return ret;
856
+ kms_response_destroy(res);
857
+ return ret;
942
858
  }
943
859
 
944
- bool
945
- mongocrypt_kms_ctx_feed (mongocrypt_kms_ctx_t *kms, mongocrypt_binary_t *bytes)
946
- {
947
- if (!kms) {
948
- return false;
949
- }
950
-
951
- mongocrypt_status_t *status = kms->status;
952
- if (!mongocrypt_status_ok (status)) {
953
- return false;
954
- }
955
-
956
- if (!bytes) {
957
- CLIENT_ERR ("argument 'bytes' is required");
958
- return false;
959
- }
960
-
961
- if (0 == bytes->len) {
962
- CLIENT_ERR ("argument 'bytes' cannot be empty");
963
- return false;
964
- }
965
-
966
- if (bytes->len > mongocrypt_kms_ctx_bytes_needed (kms)) {
967
- CLIENT_ERR ("KMS response fed too much data");
968
- return false;
969
- }
970
-
971
- if (kms->log && kms->log->trace_enabled) {
972
- _mongocrypt_log (kms->log,
973
- MONGOCRYPT_LOG_LEVEL_TRACE,
974
- "%s (%s=\"%.*s\")",
975
- BSON_FUNC,
976
- "bytes",
977
- mongocrypt_binary_len (bytes),
978
- mongocrypt_binary_data (bytes));
979
- }
980
-
981
- if (!kms_response_parser_feed (kms->parser, bytes->data, bytes->len)) {
982
- if (is_kms (kms->req_type)) {
983
- /* The KMIP response parser does not suport kms_response_parser_status.
984
- * Only report the error string. */
985
- CLIENT_ERR ("KMS response parser error with error: '%s'",
986
- kms_response_parser_error (kms->parser));
987
- } else {
988
- CLIENT_ERR ("KMS response parser error with status %d, error: '%s'",
989
- kms_response_parser_status (kms->parser),
990
- kms_response_parser_error (kms->parser));
991
- }
992
-
993
- return false;
994
- }
995
-
996
- if (0 == mongocrypt_kms_ctx_bytes_needed (kms)) {
997
- switch (kms->req_type) {
998
- default:
999
- CLIENT_ERR ("Unknown request type");
1000
- return false;
1001
- case MONGOCRYPT_KMS_AWS_ENCRYPT:
1002
- return _ctx_done_aws (kms, "CiphertextBlob");
1003
- case MONGOCRYPT_KMS_AWS_DECRYPT:
1004
- return _ctx_done_aws (kms, "Plaintext");
1005
- case MONGOCRYPT_KMS_AZURE_OAUTH:
1006
- return _ctx_done_oauth (kms);
1007
- case MONGOCRYPT_KMS_AZURE_WRAPKEY:
1008
- return _ctx_done_azure_wrapkey_unwrapkey (kms);
1009
- case MONGOCRYPT_KMS_AZURE_UNWRAPKEY:
1010
- return _ctx_done_azure_wrapkey_unwrapkey (kms);
1011
- case MONGOCRYPT_KMS_GCP_OAUTH:
1012
- return _ctx_done_oauth (kms);
1013
- case MONGOCRYPT_KMS_GCP_ENCRYPT:
1014
- return _ctx_done_gcp (kms, "ciphertext");
1015
- case MONGOCRYPT_KMS_GCP_DECRYPT:
1016
- return _ctx_done_gcp (kms, "plaintext");
1017
- case MONGOCRYPT_KMS_KMIP_REGISTER:
1018
- return _ctx_done_kmip_register (kms);
1019
- case MONGOCRYPT_KMS_KMIP_ACTIVATE:
1020
- return _ctx_done_kmip_activate (kms);
1021
- case MONGOCRYPT_KMS_KMIP_GET:
1022
- return _ctx_done_kmip_get (kms);
1023
- }
1024
- }
1025
- return true;
1026
- }
860
+ static bool _ctx_done_kmip_create(mongocrypt_kms_ctx_t *kms_ctx) {
861
+ BSON_ASSERT_PARAM(kms_ctx);
1027
862
 
863
+ kms_response_t *res = NULL;
1028
864
 
1029
- bool
1030
- _mongocrypt_kms_ctx_result (mongocrypt_kms_ctx_t *kms,
1031
- _mongocrypt_buffer_t *out)
1032
- {
1033
- BSON_ASSERT_PARAM (kms);
1034
- BSON_ASSERT_PARAM (out);
865
+ mongocrypt_status_t *status = kms_ctx->status;
866
+ bool ret = false;
867
+ char *uid;
1035
868
 
1036
- mongocrypt_status_t *status = kms->status;
869
+ res = kms_response_parser_get_response(kms_ctx->parser);
870
+ if (!res) {
871
+ CLIENT_ERR("Error getting KMIP response: %s", kms_response_parser_error(kms_ctx->parser));
872
+ goto done;
873
+ }
1037
874
 
1038
- /* If we have no status, we were never initialized */
1039
- if (!status) {
1040
- return false;
1041
- }
875
+ uid = kms_kmip_response_get_unique_identifier(res);
876
+ if (!uid) {
877
+ CLIENT_ERR("Error getting UniqueIdentifer from KMIP Create response: %s", kms_response_get_error(res));
878
+ goto done;
879
+ }
1042
880
 
1043
- if (!mongocrypt_status_ok (status)) {
1044
- return false;
1045
- }
881
+ if (!_mongocrypt_buffer_steal_from_string(&kms_ctx->result, uid)) {
882
+ CLIENT_ERR("Error storing KMS UniqueIdentifer result");
883
+ bson_free(uid);
884
+ goto done;
885
+ }
886
+ ret = true;
1046
887
 
1047
- if (mongocrypt_kms_ctx_bytes_needed (kms) > 0) {
1048
- CLIENT_ERR ("KMS response unfinished");
1049
- return false;
1050
- }
888
+ done:
889
+ kms_response_destroy(res);
890
+ return ret;
891
+ }
892
+
893
+ static bool _ctx_done_kmip_encrypt(mongocrypt_kms_ctx_t *kms_ctx) {
894
+ BSON_ASSERT_PARAM(kms_ctx);
895
+
896
+ kms_response_t *res = NULL;
897
+
898
+ mongocrypt_status_t *status = kms_ctx->status;
899
+ bool ret = false;
900
+ uint8_t *ciphertext;
901
+ size_t ciphertext_len;
902
+ uint8_t *iv;
903
+ size_t iv_len;
904
+ _mongocrypt_buffer_t data_buf, iv_buf;
905
+ _mongocrypt_buffer_init(&data_buf);
906
+ _mongocrypt_buffer_init(&iv_buf);
907
+
908
+ res = kms_response_parser_get_response(kms_ctx->parser);
909
+ if (!res) {
910
+ CLIENT_ERR("Error getting KMIP response: %s", kms_response_parser_error(kms_ctx->parser));
911
+ goto done;
912
+ }
913
+
914
+ ciphertext = kms_kmip_response_get_data(res, &ciphertext_len);
915
+ if (!ciphertext) {
916
+ CLIENT_ERR("Error getting data from KMIP Encrypt response: %s", kms_response_get_error(res));
917
+ goto done;
918
+ }
919
+
920
+ iv = kms_kmip_response_get_iv(res, &iv_len);
921
+ if (!iv) {
922
+ CLIENT_ERR("Error getting IV from KMIP Encrypt response: %s", kms_response_get_error(res));
923
+ bson_free(ciphertext);
924
+ goto done;
925
+ }
926
+
927
+ if (iv_len != MONGOCRYPT_IV_LEN) {
928
+ CLIENT_ERR("KMIP IV response has unexpected length: %zu", iv_len);
929
+ bson_free(ciphertext);
930
+ bson_free(iv);
931
+ goto done;
932
+ }
933
+
934
+ if (!_mongocrypt_buffer_steal_from_data_and_size(&data_buf, ciphertext, ciphertext_len)) {
935
+ CLIENT_ERR("Error storing KMS Encrypt result");
936
+ bson_free(ciphertext);
937
+ bson_free(iv);
938
+ goto done;
939
+ }
940
+
941
+ if (!_mongocrypt_buffer_steal_from_data_and_size(&iv_buf, iv, iv_len)) {
942
+ CLIENT_ERR("Error storing KMS Encrypt IV");
943
+ bson_free(ciphertext);
944
+ bson_free(iv);
945
+ goto done;
946
+ }
947
+
948
+ const _mongocrypt_buffer_t results_buf[2] = {iv_buf, data_buf};
949
+ if (!_mongocrypt_buffer_concat(&kms_ctx->result, results_buf, 2)) {
950
+ CLIENT_ERR("Error concatenating IV and ciphertext");
951
+ goto done;
952
+ }
953
+
954
+ ret = true;
1051
955
 
1052
- _mongocrypt_buffer_init (out);
1053
- out->data = kms->result.data;
1054
- out->len = kms->result.len;
1055
- return true;
956
+ done:
957
+ kms_response_destroy(res);
958
+ _mongocrypt_buffer_cleanup(&iv_buf);
959
+ _mongocrypt_buffer_cleanup(&data_buf);
960
+ return ret;
1056
961
  }
1057
962
 
963
+ static bool _ctx_done_kmip_decrypt(mongocrypt_kms_ctx_t *kms_ctx) {
964
+ BSON_ASSERT_PARAM(kms_ctx);
965
+
966
+ kms_response_t *res = NULL;
967
+
968
+ mongocrypt_status_t *status = kms_ctx->status;
969
+ bool ret = false;
970
+ uint8_t *ciphertext;
971
+ size_t ciphertext_len;
972
+
973
+ res = kms_response_parser_get_response(kms_ctx->parser);
974
+ if (!res) {
975
+ CLIENT_ERR("Error getting KMIP response: %s", kms_response_parser_error(kms_ctx->parser));
976
+ goto done;
977
+ }
978
+
979
+ ciphertext = kms_kmip_response_get_data(res, &ciphertext_len);
980
+ if (!ciphertext) {
981
+ CLIENT_ERR("Error getting data from KMIP Decrypt response: %s", kms_response_get_error(res));
982
+ goto done;
983
+ }
984
+
985
+ if (!_mongocrypt_buffer_steal_from_data_and_size(&kms_ctx->result, ciphertext, ciphertext_len)) {
986
+ CLIENT_ERR("Error storing KMS Decrypt result");
987
+ bson_free(ciphertext);
988
+ goto done;
989
+ }
1058
990
 
1059
- bool
1060
- mongocrypt_kms_ctx_status (mongocrypt_kms_ctx_t *kms,
1061
- mongocrypt_status_t *status_out)
1062
- {
1063
- if (!kms) {
1064
- return false;
1065
- }
1066
-
1067
- if (!status_out) {
1068
- mongocrypt_status_t *status = kms->status;
1069
- CLIENT_ERR ("argument 'status' is required");
1070
- return false;
1071
- }
1072
- _mongocrypt_status_copy_to (kms->status, status_out);
1073
- return mongocrypt_status_ok (status_out);
991
+ ret = true;
992
+
993
+ done:
994
+ kms_response_destroy(res);
995
+ return ret;
1074
996
  }
1075
997
 
998
+ bool mongocrypt_kms_ctx_feed(mongocrypt_kms_ctx_t *kms, mongocrypt_binary_t *bytes) {
999
+ if (!kms) {
1000
+ return false;
1001
+ }
1002
+
1003
+ mongocrypt_status_t *status = kms->status;
1004
+ if (!mongocrypt_status_ok(status)) {
1005
+ return false;
1006
+ }
1007
+
1008
+ if (!bytes) {
1009
+ CLIENT_ERR("argument 'bytes' is required");
1010
+ return false;
1011
+ }
1012
+
1013
+ if (0 == bytes->len) {
1014
+ CLIENT_ERR("argument 'bytes' cannot be empty");
1015
+ return false;
1016
+ }
1017
+
1018
+ if (bytes->len > mongocrypt_kms_ctx_bytes_needed(kms)) {
1019
+ CLIENT_ERR("KMS response fed too much data");
1020
+ return false;
1021
+ }
1022
+
1023
+ if (kms->log && kms->log->trace_enabled) {
1024
+ _mongocrypt_log(kms->log,
1025
+ MONGOCRYPT_LOG_LEVEL_TRACE,
1026
+ "%s (%s=\"%.*s\")",
1027
+ BSON_FUNC,
1028
+ "bytes",
1029
+ mongocrypt_binary_len(bytes),
1030
+ mongocrypt_binary_data(bytes));
1031
+ }
1032
+
1033
+ if (!kms_response_parser_feed(kms->parser, bytes->data, bytes->len)) {
1034
+ if (is_kms(kms->req_type)) {
1035
+ /* The KMIP response parser does not suport kms_response_parser_status.
1036
+ * Only report the error string. */
1037
+ CLIENT_ERR("KMS response parser error with error: '%s'", kms_response_parser_error(kms->parser));
1038
+ } else {
1039
+ CLIENT_ERR("KMS response parser error with status %d, error: '%s'",
1040
+ kms_response_parser_status(kms->parser),
1041
+ kms_response_parser_error(kms->parser));
1042
+ }
1043
+
1044
+ return false;
1045
+ }
1046
+
1047
+ if (0 == mongocrypt_kms_ctx_bytes_needed(kms)) {
1048
+ switch (kms->req_type) {
1049
+ default: CLIENT_ERR("Unknown request type"); return false;
1050
+ case MONGOCRYPT_KMS_AWS_ENCRYPT: return _ctx_done_aws(kms, "CiphertextBlob");
1051
+ case MONGOCRYPT_KMS_AWS_DECRYPT: return _ctx_done_aws(kms, "Plaintext");
1052
+ case MONGOCRYPT_KMS_AZURE_OAUTH: return _ctx_done_oauth(kms);
1053
+ case MONGOCRYPT_KMS_AZURE_WRAPKEY: return _ctx_done_azure_wrapkey_unwrapkey(kms);
1054
+ case MONGOCRYPT_KMS_AZURE_UNWRAPKEY: return _ctx_done_azure_wrapkey_unwrapkey(kms);
1055
+ case MONGOCRYPT_KMS_GCP_OAUTH: return _ctx_done_oauth(kms);
1056
+ case MONGOCRYPT_KMS_GCP_ENCRYPT: return _ctx_done_gcp(kms, "ciphertext");
1057
+ case MONGOCRYPT_KMS_GCP_DECRYPT: return _ctx_done_gcp(kms, "plaintext");
1058
+ case MONGOCRYPT_KMS_KMIP_REGISTER: return _ctx_done_kmip_register(kms);
1059
+ case MONGOCRYPT_KMS_KMIP_ACTIVATE: return _ctx_done_kmip_activate(kms);
1060
+ case MONGOCRYPT_KMS_KMIP_GET: return _ctx_done_kmip_get(kms);
1061
+ case MONGOCRYPT_KMS_KMIP_ENCRYPT: return _ctx_done_kmip_encrypt(kms);
1062
+ case MONGOCRYPT_KMS_KMIP_DECRYPT: return _ctx_done_kmip_decrypt(kms);
1063
+ case MONGOCRYPT_KMS_KMIP_CREATE: return _ctx_done_kmip_create(kms);
1064
+ }
1065
+ }
1066
+ return true;
1067
+ }
1068
+
1069
+ bool _mongocrypt_kms_ctx_result(mongocrypt_kms_ctx_t *kms, _mongocrypt_buffer_t *out) {
1070
+ BSON_ASSERT_PARAM(kms);
1071
+ BSON_ASSERT_PARAM(out);
1072
+
1073
+ mongocrypt_status_t *status = kms->status;
1076
1074
 
1077
- void
1078
- _mongocrypt_kms_ctx_cleanup (mongocrypt_kms_ctx_t *kms)
1079
- {
1080
- if (!kms) {
1081
- return;
1082
- }
1083
- if (kms->req) {
1084
- kms_request_destroy (kms->req);
1085
- }
1086
- if (kms->parser) {
1087
- kms_response_parser_destroy (kms->parser);
1088
- }
1089
- mongocrypt_status_destroy (kms->status);
1090
- _mongocrypt_buffer_cleanup (&kms->msg);
1091
- _mongocrypt_buffer_cleanup (&kms->result);
1092
- bson_free (kms->endpoint);
1075
+ /* If we have no status, we were never initialized */
1076
+ if (!status) {
1077
+ return false;
1078
+ }
1079
+
1080
+ if (!mongocrypt_status_ok(status)) {
1081
+ return false;
1082
+ }
1083
+
1084
+ if (mongocrypt_kms_ctx_bytes_needed(kms) > 0) {
1085
+ CLIENT_ERR("KMS response unfinished");
1086
+ return false;
1087
+ }
1088
+
1089
+ _mongocrypt_buffer_init(out);
1090
+ out->data = kms->result.data;
1091
+ out->len = kms->result.len;
1092
+ return true;
1093
1093
  }
1094
1094
 
1095
+ bool mongocrypt_kms_ctx_status(mongocrypt_kms_ctx_t *kms, mongocrypt_status_t *status_out) {
1096
+ if (!kms) {
1097
+ return false;
1098
+ }
1099
+
1100
+ if (!status_out) {
1101
+ mongocrypt_status_t *status = kms->status;
1102
+ CLIENT_ERR("argument 'status' is required");
1103
+ return false;
1104
+ }
1105
+ _mongocrypt_status_copy_to(kms->status, status_out);
1106
+ return mongocrypt_status_ok(status_out);
1107
+ }
1095
1108
 
1096
- bool
1097
- mongocrypt_kms_ctx_message (mongocrypt_kms_ctx_t *kms, mongocrypt_binary_t *msg)
1098
- {
1099
- if (!kms) {
1100
- return false;
1101
- }
1102
-
1103
- if (!msg) {
1104
- mongocrypt_status_t *status = kms->status;
1105
- CLIENT_ERR ("argument 'msg' is required");
1106
- return false;
1107
- }
1108
- msg->data = kms->msg.data;
1109
- msg->len = kms->msg.len;
1110
- return true;
1109
+ void _mongocrypt_kms_ctx_cleanup(mongocrypt_kms_ctx_t *kms) {
1110
+ if (!kms) {
1111
+ return;
1112
+ }
1113
+ if (kms->req) {
1114
+ kms_request_destroy(kms->req);
1115
+ }
1116
+ if (kms->parser) {
1117
+ kms_response_parser_destroy(kms->parser);
1118
+ }
1119
+ mongocrypt_status_destroy(kms->status);
1120
+ _mongocrypt_buffer_cleanup(&kms->msg);
1121
+ _mongocrypt_buffer_cleanup(&kms->result);
1122
+ bson_free(kms->endpoint);
1123
+ bson_free(kms->kmsid);
1111
1124
  }
1112
1125
 
1126
+ bool mongocrypt_kms_ctx_message(mongocrypt_kms_ctx_t *kms, mongocrypt_binary_t *msg) {
1127
+ if (!kms) {
1128
+ return false;
1129
+ }
1130
+
1131
+ if (!msg) {
1132
+ mongocrypt_status_t *status = kms->status;
1133
+ CLIENT_ERR("argument 'msg' is required");
1134
+ return false;
1135
+ }
1136
+ msg->data = kms->msg.data;
1137
+ msg->len = kms->msg.len;
1138
+ return true;
1139
+ }
1113
1140
 
1114
- bool
1115
- mongocrypt_kms_ctx_endpoint (mongocrypt_kms_ctx_t *kms, const char **endpoint)
1116
- {
1117
- if (!kms) {
1118
- return false;
1119
- }
1120
- if (!endpoint) {
1121
- mongocrypt_status_t *status = kms->status;
1122
- CLIENT_ERR ("argument 'endpoint' is required");
1123
- return false;
1124
- }
1125
- *endpoint = kms->endpoint;
1126
- return true;
1141
+ bool mongocrypt_kms_ctx_endpoint(mongocrypt_kms_ctx_t *kms, const char **endpoint) {
1142
+ if (!kms) {
1143
+ return false;
1144
+ }
1145
+ if (!endpoint) {
1146
+ mongocrypt_status_t *status = kms->status;
1147
+ CLIENT_ERR("argument 'endpoint' is required");
1148
+ return false;
1149
+ }
1150
+ *endpoint = kms->endpoint;
1151
+ return true;
1127
1152
  }
1128
1153
 
1129
- bool
1130
- _mongocrypt_kms_ctx_init_azure_auth (
1131
- mongocrypt_kms_ctx_t *kms,
1132
- _mongocrypt_log_t *log,
1133
- _mongocrypt_opts_kms_providers_t *kms_providers,
1134
- _mongocrypt_endpoint_t *key_vault_endpoint)
1135
- {
1136
- BSON_ASSERT_PARAM (kms);
1137
- BSON_ASSERT_PARAM (kms_providers);
1138
-
1139
- kms_request_opt_t *opt = NULL;
1140
- mongocrypt_status_t *status;
1141
- _mongocrypt_endpoint_t *identity_platform_endpoint;
1142
- char *scope = NULL;
1143
- const char *hostname;
1144
- char *request_string;
1145
- bool ret = false;
1146
-
1147
- _init_common (kms, log, MONGOCRYPT_KMS_AZURE_OAUTH);
1148
- status = kms->status;
1149
-
1150
- identity_platform_endpoint = kms_providers->azure.identity_platform_endpoint;
1151
-
1152
- if (identity_platform_endpoint) {
1153
- kms->endpoint = bson_strdup (identity_platform_endpoint->host_and_port);
1154
- hostname = identity_platform_endpoint->host;
1155
- } else {
1156
- kms->endpoint = bson_strdup ("login.microsoftonline.com");
1157
- hostname = "login.microsoftonline.com";
1158
- }
1159
- _mongocrypt_apply_default_port (&kms->endpoint, DEFAULT_HTTPS_PORT);
1160
-
1161
- if (key_vault_endpoint) {
1162
- /* Request a custom scope. It is URL encoded, like
1163
- * https%3A%2F%2Fvault.azure.net%2F.default */
1164
- scope = bson_strdup_printf (
1165
- "%s%s%s", "https%3A%2F%2F", key_vault_endpoint->domain, "%2F.default");
1166
- } else {
1167
- /* Default to commercial Azure endpoint. */
1168
- scope = bson_strdup ("https%3A%2F%2Fvault.azure.net%2F.default");
1169
- }
1170
-
1171
- opt = kms_request_opt_new ();
1172
- BSON_ASSERT (opt);
1173
- kms_request_opt_set_connection_close (opt, true);
1174
- kms_request_opt_set_provider (opt, KMS_REQUEST_PROVIDER_AZURE);
1175
- kms->req = kms_azure_request_oauth_new (hostname,
1154
+ bool _mongocrypt_kms_ctx_init_azure_auth(mongocrypt_kms_ctx_t *kms,
1155
+ const mc_kms_creds_t *kc,
1156
+ _mongocrypt_endpoint_t *key_vault_endpoint,
1157
+ const char *kmsid,
1158
+ _mongocrypt_log_t *log) {
1159
+ BSON_ASSERT_PARAM(kms);
1160
+ BSON_ASSERT_PARAM(kc);
1161
+
1162
+ kms_request_opt_t *opt = NULL;
1163
+ mongocrypt_status_t *status;
1164
+ const _mongocrypt_endpoint_t *identity_platform_endpoint;
1165
+ char *scope = NULL;
1166
+ const char *hostname;
1167
+ char *request_string;
1168
+ bool ret = false;
1169
+
1170
+ _init_common(kms, log, MONGOCRYPT_KMS_AZURE_OAUTH, kmsid);
1171
+ status = kms->status;
1172
+
1173
+ BSON_ASSERT(kc->type == MONGOCRYPT_KMS_PROVIDER_AZURE);
1174
+
1175
+ identity_platform_endpoint = kc->value.azure.identity_platform_endpoint;
1176
+
1177
+ if (identity_platform_endpoint) {
1178
+ kms->endpoint = bson_strdup(identity_platform_endpoint->host_and_port);
1179
+ hostname = identity_platform_endpoint->host;
1180
+ } else {
1181
+ kms->endpoint = bson_strdup("login.microsoftonline.com");
1182
+ hostname = "login.microsoftonline.com";
1183
+ }
1184
+ _mongocrypt_apply_default_port(&kms->endpoint, DEFAULT_HTTPS_PORT);
1185
+
1186
+ if (key_vault_endpoint) {
1187
+ /* Request a custom scope. It is URL encoded, like
1188
+ * https%3A%2F%2Fvault.azure.net%2F.default */
1189
+ scope = bson_strdup_printf("%s%s%s", "https%3A%2F%2F", key_vault_endpoint->domain, "%2F.default");
1190
+ } else {
1191
+ /* Default to commercial Azure endpoint. */
1192
+ scope = bson_strdup("https%3A%2F%2Fvault.azure.net%2F.default");
1193
+ }
1194
+
1195
+ opt = kms_request_opt_new();
1196
+ BSON_ASSERT(opt);
1197
+ kms_request_opt_set_connection_close(opt, true);
1198
+ kms_request_opt_set_provider(opt, KMS_REQUEST_PROVIDER_AZURE);
1199
+ kms->req = kms_azure_request_oauth_new(hostname,
1176
1200
  scope,
1177
- kms_providers->azure.tenant_id,
1178
- kms_providers->azure.client_id,
1179
- kms_providers->azure.client_secret,
1201
+ kc->value.azure.tenant_id,
1202
+ kc->value.azure.client_id,
1203
+ kc->value.azure.client_secret,
1180
1204
  opt);
1181
- if (kms_request_get_error (kms->req)) {
1182
- CLIENT_ERR ("error constructing KMS message: %s",
1183
- kms_request_get_error (kms->req));
1184
- goto fail;
1185
- }
1186
-
1187
- request_string = kms_request_to_string (kms->req);
1188
- if (!request_string) {
1189
- CLIENT_ERR ("error getting Azure OAuth KMS message: %s",
1190
- kms_request_get_error (kms->req));
1191
- goto fail;
1192
- }
1193
- _mongocrypt_buffer_init (&kms->msg);
1194
- kms->msg.data = (uint8_t *) request_string;
1195
- kms->msg.len = (uint32_t) strlen (request_string);
1196
- kms->msg.owned = true;
1197
-
1198
- ret = true;
1205
+ if (kms_request_get_error(kms->req)) {
1206
+ CLIENT_ERR("error constructing KMS message: %s", kms_request_get_error(kms->req));
1207
+ goto fail;
1208
+ }
1209
+
1210
+ request_string = kms_request_to_string(kms->req);
1211
+ if (!request_string) {
1212
+ CLIENT_ERR("error getting Azure OAuth KMS message: %s", kms_request_get_error(kms->req));
1213
+ goto fail;
1214
+ }
1215
+ _mongocrypt_buffer_init(&kms->msg);
1216
+ kms->msg.data = (uint8_t *)request_string;
1217
+ kms->msg.len = (uint32_t)strlen(request_string);
1218
+ kms->msg.owned = true;
1219
+
1220
+ ret = true;
1199
1221
  fail:
1200
- bson_free (scope);
1201
- kms_request_opt_destroy (opt);
1202
- return ret;
1222
+ bson_free(scope);
1223
+ kms_request_opt_destroy(opt);
1224
+ return ret;
1203
1225
  }
1204
1226
 
1205
- bool
1206
- _mongocrypt_kms_ctx_init_azure_wrapkey (
1207
- mongocrypt_kms_ctx_t *kms,
1208
- _mongocrypt_log_t *log,
1209
- _mongocrypt_opts_kms_providers_t *kms_providers,
1210
- struct __mongocrypt_ctx_opts_t *ctx_opts,
1211
- const char *access_token,
1212
- _mongocrypt_buffer_t *plaintext_key_material)
1213
- {
1214
- BSON_ASSERT_PARAM (kms);
1215
- BSON_ASSERT_PARAM (ctx_opts);
1216
- BSON_ASSERT_PARAM (plaintext_key_material);
1217
-
1218
- kms_request_opt_t *opt = NULL;
1219
- mongocrypt_status_t *status;
1220
- char *path_and_query = NULL;
1221
- char *payload = NULL;
1222
- const char *host;
1223
- char *request_string;
1224
- bool ret = false;
1225
-
1226
- _init_common (kms, log, MONGOCRYPT_KMS_AZURE_WRAPKEY);
1227
- status = kms->status;
1228
-
1229
- BSON_ASSERT (ctx_opts->kek.provider.azure.key_vault_endpoint);
1230
-
1231
- kms->endpoint = bson_strdup (
1232
- ctx_opts->kek.provider.azure.key_vault_endpoint->host_and_port);
1233
- _mongocrypt_apply_default_port (&kms->endpoint, DEFAULT_HTTPS_PORT);
1234
- host = ctx_opts->kek.provider.azure.key_vault_endpoint->host;
1235
-
1236
- opt = kms_request_opt_new ();
1237
- BSON_ASSERT (opt);
1238
- kms_request_opt_set_connection_close (opt, true);
1239
- kms_request_opt_set_provider (opt, KMS_REQUEST_PROVIDER_AZURE);
1240
- kms->req =
1241
- kms_azure_request_wrapkey_new (host,
1242
- access_token,
1243
- ctx_opts->kek.provider.azure.key_name,
1244
- ctx_opts->kek.provider.azure.key_version,
1245
- plaintext_key_material->data,
1246
- plaintext_key_material->len,
1247
- opt);
1248
-
1249
- if (kms_request_get_error (kms->req)) {
1250
- CLIENT_ERR ("error constructing KMS wrapkey message: %s",
1251
- kms_request_get_error (kms->req));
1252
- goto fail;
1253
- }
1254
-
1255
- request_string = kms_request_to_string (kms->req);
1256
- if (!request_string) {
1257
- CLIENT_ERR ("error getting Azure wrapkey KMS message: %s",
1258
- kms_request_get_error (kms->req));
1259
- goto fail;
1260
- }
1261
- _mongocrypt_buffer_init (&kms->msg);
1262
- kms->msg.data = (uint8_t *) request_string;
1263
- kms->msg.len = (uint32_t) strlen (request_string);
1264
- kms->msg.owned = true;
1265
-
1266
- ret = true;
1227
+ bool _mongocrypt_kms_ctx_init_azure_wrapkey(mongocrypt_kms_ctx_t *kms,
1228
+ _mongocrypt_opts_kms_providers_t *kms_providers,
1229
+ struct __mongocrypt_ctx_opts_t *ctx_opts,
1230
+ const char *access_token,
1231
+ _mongocrypt_buffer_t *plaintext_key_material,
1232
+ const char *kmsid,
1233
+ _mongocrypt_log_t *log) {
1234
+ BSON_ASSERT_PARAM(kms);
1235
+ BSON_ASSERT_PARAM(ctx_opts);
1236
+ BSON_ASSERT_PARAM(plaintext_key_material);
1237
+
1238
+ kms_request_opt_t *opt = NULL;
1239
+ mongocrypt_status_t *status;
1240
+ char *path_and_query = NULL;
1241
+ char *payload = NULL;
1242
+ const char *host;
1243
+ char *request_string;
1244
+ bool ret = false;
1245
+
1246
+ _init_common(kms, log, MONGOCRYPT_KMS_AZURE_WRAPKEY, kmsid);
1247
+ status = kms->status;
1248
+
1249
+ BSON_ASSERT(ctx_opts->kek.provider.azure.key_vault_endpoint);
1250
+
1251
+ kms->endpoint = bson_strdup(ctx_opts->kek.provider.azure.key_vault_endpoint->host_and_port);
1252
+ _mongocrypt_apply_default_port(&kms->endpoint, DEFAULT_HTTPS_PORT);
1253
+ host = ctx_opts->kek.provider.azure.key_vault_endpoint->host;
1254
+
1255
+ opt = kms_request_opt_new();
1256
+ BSON_ASSERT(opt);
1257
+ kms_request_opt_set_connection_close(opt, true);
1258
+ kms_request_opt_set_provider(opt, KMS_REQUEST_PROVIDER_AZURE);
1259
+ kms->req = kms_azure_request_wrapkey_new(host,
1260
+ access_token,
1261
+ ctx_opts->kek.provider.azure.key_name,
1262
+ ctx_opts->kek.provider.azure.key_version,
1263
+ plaintext_key_material->data,
1264
+ plaintext_key_material->len,
1265
+ opt);
1266
+
1267
+ if (kms_request_get_error(kms->req)) {
1268
+ CLIENT_ERR("error constructing KMS wrapkey message: %s", kms_request_get_error(kms->req));
1269
+ goto fail;
1270
+ }
1271
+
1272
+ request_string = kms_request_to_string(kms->req);
1273
+ if (!request_string) {
1274
+ CLIENT_ERR("error getting Azure wrapkey KMS message: %s", kms_request_get_error(kms->req));
1275
+ goto fail;
1276
+ }
1277
+ _mongocrypt_buffer_init(&kms->msg);
1278
+ kms->msg.data = (uint8_t *)request_string;
1279
+ kms->msg.len = (uint32_t)strlen(request_string);
1280
+ kms->msg.owned = true;
1281
+
1282
+ ret = true;
1267
1283
  fail:
1268
- kms_request_opt_destroy (opt);
1269
- bson_free (path_and_query);
1270
- bson_free (payload);
1271
- return ret;
1284
+ kms_request_opt_destroy(opt);
1285
+ bson_free(path_and_query);
1286
+ bson_free(payload);
1287
+ return ret;
1272
1288
  }
1273
1289
 
1274
- bool
1275
- _mongocrypt_kms_ctx_init_azure_unwrapkey (
1276
- mongocrypt_kms_ctx_t *kms,
1277
- _mongocrypt_opts_kms_providers_t *kms_providers,
1278
- const char *access_token,
1279
- _mongocrypt_key_doc_t *key,
1280
- _mongocrypt_log_t *log)
1281
- {
1282
- BSON_ASSERT_PARAM (kms);
1283
- BSON_ASSERT_PARAM (key);
1284
-
1285
- kms_request_opt_t *opt = NULL;
1286
- mongocrypt_status_t *status;
1287
- char *path_and_query = NULL;
1288
- char *payload = NULL;
1289
- const char *host;
1290
- char *request_string;
1291
- bool ret = false;
1292
-
1293
- _init_common (kms, log, MONGOCRYPT_KMS_AZURE_UNWRAPKEY);
1294
- status = kms->status;
1295
-
1296
- BSON_ASSERT (key->kek.provider.azure.key_vault_endpoint);
1297
-
1298
- kms->endpoint =
1299
- bson_strdup (key->kek.provider.azure.key_vault_endpoint->host_and_port);
1300
- _mongocrypt_apply_default_port (&kms->endpoint, DEFAULT_HTTPS_PORT);
1301
- host = key->kek.provider.azure.key_vault_endpoint->host;
1302
-
1303
- opt = kms_request_opt_new ();
1304
- BSON_ASSERT (opt);
1305
- kms_request_opt_set_connection_close (opt, true);
1306
- kms_request_opt_set_provider (opt, KMS_REQUEST_PROVIDER_AZURE);
1307
- kms->req =
1308
- kms_azure_request_unwrapkey_new (host,
1309
- access_token,
1310
- key->kek.provider.azure.key_name,
1311
- key->kek.provider.azure.key_version,
1312
- key->key_material.data,
1313
- key->key_material.len,
1314
- opt);
1315
-
1316
- if (kms_request_get_error (kms->req)) {
1317
- CLIENT_ERR ("error constructing KMS unwrapkey message: %s",
1318
- kms_request_get_error (kms->req));
1319
- goto fail;
1320
- }
1321
-
1322
- request_string = kms_request_to_string (kms->req);
1323
- if (!request_string) {
1324
- CLIENT_ERR ("error getting Azure unwrapkey KMS message: %s",
1325
- kms_request_get_error (kms->req));
1326
- goto fail;
1327
- }
1328
- _mongocrypt_buffer_init (&kms->msg);
1329
- kms->msg.data = (uint8_t *) request_string;
1330
- kms->msg.len = (uint32_t) strlen (request_string);
1331
- kms->msg.owned = true;
1332
-
1333
- ret = true;
1290
+ bool _mongocrypt_kms_ctx_init_azure_unwrapkey(mongocrypt_kms_ctx_t *kms,
1291
+ _mongocrypt_opts_kms_providers_t *kms_providers,
1292
+ const char *access_token,
1293
+ _mongocrypt_key_doc_t *key,
1294
+ const char *kmsid,
1295
+ _mongocrypt_log_t *log) {
1296
+ BSON_ASSERT_PARAM(kms);
1297
+ BSON_ASSERT_PARAM(key);
1298
+
1299
+ kms_request_opt_t *opt = NULL;
1300
+ mongocrypt_status_t *status;
1301
+ char *path_and_query = NULL;
1302
+ char *payload = NULL;
1303
+ const char *host;
1304
+ char *request_string;
1305
+ bool ret = false;
1306
+
1307
+ _init_common(kms, log, MONGOCRYPT_KMS_AZURE_UNWRAPKEY, kmsid);
1308
+ status = kms->status;
1309
+
1310
+ BSON_ASSERT(key->kek.provider.azure.key_vault_endpoint);
1311
+
1312
+ kms->endpoint = bson_strdup(key->kek.provider.azure.key_vault_endpoint->host_and_port);
1313
+ _mongocrypt_apply_default_port(&kms->endpoint, DEFAULT_HTTPS_PORT);
1314
+ host = key->kek.provider.azure.key_vault_endpoint->host;
1315
+
1316
+ opt = kms_request_opt_new();
1317
+ BSON_ASSERT(opt);
1318
+ kms_request_opt_set_connection_close(opt, true);
1319
+ kms_request_opt_set_provider(opt, KMS_REQUEST_PROVIDER_AZURE);
1320
+ kms->req = kms_azure_request_unwrapkey_new(host,
1321
+ access_token,
1322
+ key->kek.provider.azure.key_name,
1323
+ key->kek.provider.azure.key_version,
1324
+ key->key_material.data,
1325
+ key->key_material.len,
1326
+ opt);
1327
+
1328
+ if (kms_request_get_error(kms->req)) {
1329
+ CLIENT_ERR("error constructing KMS unwrapkey message: %s", kms_request_get_error(kms->req));
1330
+ goto fail;
1331
+ }
1332
+
1333
+ request_string = kms_request_to_string(kms->req);
1334
+ if (!request_string) {
1335
+ CLIENT_ERR("error getting Azure unwrapkey KMS message: %s", kms_request_get_error(kms->req));
1336
+ goto fail;
1337
+ }
1338
+ _mongocrypt_buffer_init(&kms->msg);
1339
+ kms->msg.data = (uint8_t *)request_string;
1340
+ kms->msg.len = (uint32_t)strlen(request_string);
1341
+ kms->msg.owned = true;
1342
+
1343
+ ret = true;
1334
1344
  fail:
1335
- kms_request_opt_destroy (opt);
1336
- bson_free (path_and_query);
1337
- bson_free (payload);
1338
- return ret;
1345
+ kms_request_opt_destroy(opt);
1346
+ bson_free(path_and_query);
1347
+ bson_free(payload);
1348
+ return ret;
1339
1349
  }
1340
1350
 
1341
1351
  #define RSAES_PKCS1_V1_5_SIGNATURE_LEN 256
1342
1352
 
1343
1353
  /* This is the form of the callback that KMS message calls. */
1344
- static bool
1345
- _sign_rsaes_pkcs1_v1_5_trampoline (void *ctx,
1346
- const char *private_key,
1347
- size_t private_key_len,
1348
- const char *input,
1349
- size_t input_len,
1350
- unsigned char *signature_out)
1351
- {
1352
- ctx_with_status_t *ctx_with_status;
1353
- _mongocrypt_opts_t *crypt_opts;
1354
- mongocrypt_binary_t private_key_bin;
1355
- mongocrypt_binary_t input_bin;
1356
- mongocrypt_binary_t output_bin;
1357
- bool ret;
1358
-
1359
- BSON_ASSERT_PARAM (ctx);
1360
- BSON_ASSERT_PARAM (input);
1361
- BSON_ASSERT_PARAM (private_key);
1362
- BSON_ASSERT_PARAM (signature_out);
1363
-
1364
- ctx_with_status = (ctx_with_status_t *) ctx;
1365
- crypt_opts = (_mongocrypt_opts_t *) ctx_with_status->ctx;
1366
- BSON_ASSERT (crypt_opts);
1367
- private_key_bin.data = (uint8_t *) private_key;
1368
- BSON_ASSERT (private_key_len <= UINT32_MAX);
1369
- private_key_bin.len = (uint32_t) private_key_len;
1370
- input_bin.data = (uint8_t *) input;
1371
- BSON_ASSERT (input_len <= UINT32_MAX);
1372
- input_bin.len = (uint32_t) input_len;
1373
- output_bin.data = (uint8_t *) signature_out;
1374
- output_bin.len = RSAES_PKCS1_V1_5_SIGNATURE_LEN;
1375
-
1376
- ret = crypt_opts->sign_rsaes_pkcs1_v1_5 (crypt_opts->sign_ctx,
1354
+ static bool _sign_rsaes_pkcs1_v1_5_trampoline(void *ctx,
1355
+ const char *private_key,
1356
+ size_t private_key_len,
1357
+ const char *input,
1358
+ size_t input_len,
1359
+ unsigned char *signature_out) {
1360
+ ctx_with_status_t *ctx_with_status;
1361
+ _mongocrypt_opts_t *crypt_opts;
1362
+ mongocrypt_binary_t private_key_bin;
1363
+ mongocrypt_binary_t input_bin;
1364
+ mongocrypt_binary_t output_bin;
1365
+ bool ret;
1366
+
1367
+ BSON_ASSERT_PARAM(ctx);
1368
+ BSON_ASSERT_PARAM(input);
1369
+ BSON_ASSERT_PARAM(private_key);
1370
+ BSON_ASSERT_PARAM(signature_out);
1371
+
1372
+ ctx_with_status = (ctx_with_status_t *)ctx;
1373
+ crypt_opts = (_mongocrypt_opts_t *)ctx_with_status->ctx;
1374
+ BSON_ASSERT(crypt_opts);
1375
+ private_key_bin.data = (uint8_t *)private_key;
1376
+ BSON_ASSERT(private_key_len <= UINT32_MAX);
1377
+ private_key_bin.len = (uint32_t)private_key_len;
1378
+ input_bin.data = (uint8_t *)input;
1379
+ BSON_ASSERT(input_len <= UINT32_MAX);
1380
+ input_bin.len = (uint32_t)input_len;
1381
+ output_bin.data = (uint8_t *)signature_out;
1382
+ output_bin.len = RSAES_PKCS1_V1_5_SIGNATURE_LEN;
1383
+
1384
+ ret = crypt_opts->sign_rsaes_pkcs1_v1_5(crypt_opts->sign_ctx,
1377
1385
  &private_key_bin,
1378
1386
  &input_bin,
1379
1387
  &output_bin,
1380
1388
  ctx_with_status->status);
1381
- return ret;
1389
+ return ret;
1382
1390
  }
1383
1391
 
1384
- bool
1385
- _mongocrypt_kms_ctx_init_gcp_auth (
1386
- mongocrypt_kms_ctx_t *kms,
1387
- _mongocrypt_log_t *log,
1388
- _mongocrypt_opts_t *crypt_opts,
1389
- _mongocrypt_opts_kms_providers_t *kms_providers,
1390
- _mongocrypt_endpoint_t *kms_endpoint)
1391
- {
1392
- BSON_ASSERT_PARAM (kms);
1393
- BSON_ASSERT_PARAM (kms_providers);
1394
- BSON_ASSERT_PARAM (crypt_opts);
1395
-
1396
- kms_request_opt_t *opt = NULL;
1397
- mongocrypt_status_t *status;
1398
- _mongocrypt_endpoint_t *auth_endpoint;
1399
- char *scope = NULL;
1400
- char *audience = NULL;
1401
- const char *hostname;
1402
- char *request_string;
1403
- bool ret = false;
1404
- ctx_with_status_t ctx_with_status;
1405
-
1406
- _init_common (kms, log, MONGOCRYPT_KMS_GCP_OAUTH);
1407
- status = kms->status;
1408
- ctx_with_status.ctx = crypt_opts;
1409
- ctx_with_status.status = mongocrypt_status_new ();
1410
-
1411
- auth_endpoint = kms_providers->gcp.endpoint;
1412
- if (auth_endpoint) {
1413
- kms->endpoint = bson_strdup (auth_endpoint->host_and_port);
1414
- hostname = auth_endpoint->host;
1415
- audience = bson_strdup_printf ("https://%s/token", auth_endpoint->host);
1416
- } else {
1417
- kms->endpoint = bson_strdup ("oauth2.googleapis.com");
1418
- hostname = "oauth2.googleapis.com";
1419
- audience = bson_strdup_printf ("https://oauth2.googleapis.com/token");
1420
- }
1421
- _mongocrypt_apply_default_port (&kms->endpoint, DEFAULT_HTTPS_PORT);
1422
-
1423
- if (kms_endpoint) {
1424
- /* Request a custom scope. */
1425
- scope = bson_strdup_printf ("https://www.%s/auth/cloudkms",
1426
- kms_endpoint->domain);
1427
- } else {
1428
- scope = bson_strdup ("https://www.googleapis.com/auth/cloudkms");
1429
- }
1430
-
1431
- opt = kms_request_opt_new ();
1432
- BSON_ASSERT (opt);
1433
- kms_request_opt_set_connection_close (opt, true);
1434
- kms_request_opt_set_provider (opt, KMS_REQUEST_PROVIDER_GCP);
1435
- if (crypt_opts->sign_rsaes_pkcs1_v1_5) {
1436
- kms_request_opt_set_crypto_hook_sign_rsaes_pkcs1_v1_5 (
1437
- opt, _sign_rsaes_pkcs1_v1_5_trampoline, &ctx_with_status);
1438
- }
1439
- kms->req = kms_gcp_request_oauth_new (
1440
- hostname,
1441
- kms_providers->gcp.email,
1442
- audience,
1443
- scope,
1444
- (const char *) kms_providers->gcp.private_key.data,
1445
- kms_providers->gcp.private_key.len,
1446
- opt);
1447
- if (kms_request_get_error (kms->req)) {
1448
- CLIENT_ERR ("error constructing KMS message: %s",
1449
- kms_request_get_error (kms->req));
1450
- _mongocrypt_status_append (status, ctx_with_status.status);
1451
- goto fail;
1452
- }
1453
-
1454
- request_string = kms_request_to_string (kms->req);
1455
- if (!request_string) {
1456
- CLIENT_ERR ("error getting GCP OAuth KMS message: %s",
1457
- kms_request_get_error (kms->req));
1458
- _mongocrypt_status_append (status, ctx_with_status.status);
1459
- goto fail;
1460
- }
1461
- _mongocrypt_buffer_init (&kms->msg);
1462
- kms->msg.data = (uint8_t *) request_string;
1463
- kms->msg.len = (uint32_t) strlen (request_string);
1464
- kms->msg.owned = true;
1465
-
1466
- ret = true;
1392
+ bool _mongocrypt_kms_ctx_init_gcp_auth(mongocrypt_kms_ctx_t *kms,
1393
+ _mongocrypt_opts_t *crypt_opts,
1394
+ const mc_kms_creds_t *kc,
1395
+ _mongocrypt_endpoint_t *kms_endpoint,
1396
+ const char *kmsid,
1397
+ _mongocrypt_log_t *log) {
1398
+ BSON_ASSERT_PARAM(kms);
1399
+ BSON_ASSERT_PARAM(kc);
1400
+ BSON_ASSERT_PARAM(crypt_opts);
1401
+
1402
+ kms_request_opt_t *opt = NULL;
1403
+ mongocrypt_status_t *status;
1404
+ const _mongocrypt_endpoint_t *auth_endpoint;
1405
+ char *scope = NULL;
1406
+ char *audience = NULL;
1407
+ const char *hostname;
1408
+ char *request_string;
1409
+ bool ret = false;
1410
+ ctx_with_status_t ctx_with_status;
1411
+
1412
+ _init_common(kms, log, MONGOCRYPT_KMS_GCP_OAUTH, kmsid);
1413
+ status = kms->status;
1414
+ ctx_with_status.ctx = crypt_opts;
1415
+ ctx_with_status.status = mongocrypt_status_new();
1416
+
1417
+ BSON_ASSERT(kc->type == MONGOCRYPT_KMS_PROVIDER_GCP);
1418
+
1419
+ auth_endpoint = kc->value.gcp.endpoint;
1420
+ if (auth_endpoint) {
1421
+ kms->endpoint = bson_strdup(auth_endpoint->host_and_port);
1422
+ hostname = auth_endpoint->host;
1423
+ audience = bson_strdup_printf("https://%s/token", auth_endpoint->host);
1424
+ } else {
1425
+ kms->endpoint = bson_strdup("oauth2.googleapis.com");
1426
+ hostname = "oauth2.googleapis.com";
1427
+ audience = bson_strdup_printf("https://oauth2.googleapis.com/token");
1428
+ }
1429
+ _mongocrypt_apply_default_port(&kms->endpoint, DEFAULT_HTTPS_PORT);
1430
+
1431
+ if (kms_endpoint) {
1432
+ /* Request a custom scope. */
1433
+ scope = bson_strdup_printf("https://www.%s/auth/cloudkms", kms_endpoint->domain);
1434
+ } else {
1435
+ scope = bson_strdup("https://www.googleapis.com/auth/cloudkms");
1436
+ }
1437
+
1438
+ opt = kms_request_opt_new();
1439
+ BSON_ASSERT(opt);
1440
+ kms_request_opt_set_connection_close(opt, true);
1441
+ kms_request_opt_set_provider(opt, KMS_REQUEST_PROVIDER_GCP);
1442
+ if (crypt_opts->sign_rsaes_pkcs1_v1_5) {
1443
+ kms_request_opt_set_crypto_hook_sign_rsaes_pkcs1_v1_5(opt, _sign_rsaes_pkcs1_v1_5_trampoline, &ctx_with_status);
1444
+ }
1445
+ kms->req = kms_gcp_request_oauth_new(hostname,
1446
+ kc->value.gcp.email,
1447
+ audience,
1448
+ scope,
1449
+ (const char *)kc->value.gcp.private_key.data,
1450
+ kc->value.gcp.private_key.len,
1451
+ opt);
1452
+ if (kms_request_get_error(kms->req)) {
1453
+ CLIENT_ERR("error constructing KMS message: %s", kms_request_get_error(kms->req));
1454
+ _mongocrypt_status_append(status, ctx_with_status.status);
1455
+ goto fail;
1456
+ }
1457
+
1458
+ request_string = kms_request_to_string(kms->req);
1459
+ if (!request_string) {
1460
+ CLIENT_ERR("error getting GCP OAuth KMS message: %s", kms_request_get_error(kms->req));
1461
+ _mongocrypt_status_append(status, ctx_with_status.status);
1462
+ goto fail;
1463
+ }
1464
+ _mongocrypt_buffer_init(&kms->msg);
1465
+ kms->msg.data = (uint8_t *)request_string;
1466
+ kms->msg.len = (uint32_t)strlen(request_string);
1467
+ kms->msg.owned = true;
1468
+
1469
+ ret = true;
1467
1470
  fail:
1468
- bson_free (scope);
1469
- bson_free (audience);
1470
- kms_request_opt_destroy (opt);
1471
- mongocrypt_status_destroy (ctx_with_status.status);
1472
- return ret;
1471
+ bson_free(scope);
1472
+ bson_free(audience);
1473
+ kms_request_opt_destroy(opt);
1474
+ mongocrypt_status_destroy(ctx_with_status.status);
1475
+ return ret;
1473
1476
  }
1474
1477
 
1475
- bool
1476
- _mongocrypt_kms_ctx_init_gcp_encrypt (
1477
- mongocrypt_kms_ctx_t *kms,
1478
- _mongocrypt_log_t *log,
1479
- _mongocrypt_opts_kms_providers_t *kms_providers,
1480
- struct __mongocrypt_ctx_opts_t *ctx_opts,
1481
- const char *access_token,
1482
- _mongocrypt_buffer_t *plaintext_key_material)
1483
- {
1484
- BSON_ASSERT_PARAM (kms);
1485
- BSON_ASSERT_PARAM (ctx_opts);
1486
- BSON_ASSERT_PARAM (kms_providers);
1487
- BSON_ASSERT_PARAM (access_token);
1488
- BSON_ASSERT_PARAM (plaintext_key_material);
1489
-
1490
- kms_request_opt_t *opt = NULL;
1491
- mongocrypt_status_t *status;
1492
- char *path_and_query = NULL;
1493
- char *payload = NULL;
1494
- const char *hostname;
1495
- char *request_string;
1496
- bool ret = false;
1497
-
1498
- _init_common (kms, log, MONGOCRYPT_KMS_GCP_ENCRYPT);
1499
- status = kms->status;
1500
-
1501
- if (ctx_opts->kek.provider.gcp.endpoint) {
1502
- kms->endpoint =
1503
- bson_strdup (ctx_opts->kek.provider.gcp.endpoint->host_and_port);
1504
- hostname = ctx_opts->kek.provider.gcp.endpoint->host;
1505
- } else {
1506
- kms->endpoint = bson_strdup ("cloudkms.googleapis.com");
1507
- hostname = "cloudkms.googleapis.com";
1508
- }
1509
- _mongocrypt_apply_default_port (&kms->endpoint, DEFAULT_HTTPS_PORT);
1510
-
1511
- opt = kms_request_opt_new ();
1512
- BSON_ASSERT (opt);
1513
- kms_request_opt_set_connection_close (opt, true);
1514
- kms_request_opt_set_provider (opt, KMS_REQUEST_PROVIDER_GCP);
1515
- kms->req =
1516
- kms_gcp_request_encrypt_new (hostname,
1517
- access_token,
1518
- ctx_opts->kek.provider.gcp.project_id,
1519
- ctx_opts->kek.provider.gcp.location,
1520
- ctx_opts->kek.provider.gcp.key_ring,
1521
- ctx_opts->kek.provider.gcp.key_name,
1522
- ctx_opts->kek.provider.gcp.key_version,
1523
- plaintext_key_material->data,
1524
- plaintext_key_material->len,
1525
- opt);
1526
-
1527
- if (kms_request_get_error (kms->req)) {
1528
- CLIENT_ERR ("error constructing GCP KMS encrypt message: %s",
1529
- kms_request_get_error (kms->req));
1530
- goto fail;
1531
- }
1532
-
1533
- request_string = kms_request_to_string (kms->req);
1534
- if (!request_string) {
1535
- CLIENT_ERR ("error getting GCP KMS encrypt KMS message: %s",
1536
- kms_request_get_error (kms->req));
1537
- goto fail;
1538
- }
1539
- _mongocrypt_buffer_init (&kms->msg);
1540
- kms->msg.data = (uint8_t *) request_string;
1541
- kms->msg.len = (uint32_t) strlen (request_string);
1542
- kms->msg.owned = true;
1543
-
1544
- ret = true;
1478
+ bool _mongocrypt_kms_ctx_init_gcp_encrypt(mongocrypt_kms_ctx_t *kms,
1479
+ _mongocrypt_opts_kms_providers_t *kms_providers,
1480
+ struct __mongocrypt_ctx_opts_t *ctx_opts,
1481
+ const char *access_token,
1482
+ _mongocrypt_buffer_t *plaintext_key_material,
1483
+ const char *kmsid,
1484
+ _mongocrypt_log_t *log) {
1485
+ BSON_ASSERT_PARAM(kms);
1486
+ BSON_ASSERT_PARAM(ctx_opts);
1487
+ BSON_ASSERT_PARAM(kms_providers);
1488
+ BSON_ASSERT_PARAM(access_token);
1489
+ BSON_ASSERT_PARAM(plaintext_key_material);
1490
+
1491
+ kms_request_opt_t *opt = NULL;
1492
+ mongocrypt_status_t *status;
1493
+ char *path_and_query = NULL;
1494
+ char *payload = NULL;
1495
+ const char *hostname;
1496
+ char *request_string;
1497
+ bool ret = false;
1498
+
1499
+ _init_common(kms, log, MONGOCRYPT_KMS_GCP_ENCRYPT, kmsid);
1500
+ status = kms->status;
1501
+
1502
+ if (ctx_opts->kek.provider.gcp.endpoint) {
1503
+ kms->endpoint = bson_strdup(ctx_opts->kek.provider.gcp.endpoint->host_and_port);
1504
+ hostname = ctx_opts->kek.provider.gcp.endpoint->host;
1505
+ } else {
1506
+ kms->endpoint = bson_strdup("cloudkms.googleapis.com");
1507
+ hostname = "cloudkms.googleapis.com";
1508
+ }
1509
+ _mongocrypt_apply_default_port(&kms->endpoint, DEFAULT_HTTPS_PORT);
1510
+
1511
+ opt = kms_request_opt_new();
1512
+ BSON_ASSERT(opt);
1513
+ kms_request_opt_set_connection_close(opt, true);
1514
+ kms_request_opt_set_provider(opt, KMS_REQUEST_PROVIDER_GCP);
1515
+ kms->req = kms_gcp_request_encrypt_new(hostname,
1516
+ access_token,
1517
+ ctx_opts->kek.provider.gcp.project_id,
1518
+ ctx_opts->kek.provider.gcp.location,
1519
+ ctx_opts->kek.provider.gcp.key_ring,
1520
+ ctx_opts->kek.provider.gcp.key_name,
1521
+ ctx_opts->kek.provider.gcp.key_version,
1522
+ plaintext_key_material->data,
1523
+ plaintext_key_material->len,
1524
+ opt);
1525
+
1526
+ if (kms_request_get_error(kms->req)) {
1527
+ CLIENT_ERR("error constructing GCP KMS encrypt message: %s", kms_request_get_error(kms->req));
1528
+ goto fail;
1529
+ }
1530
+
1531
+ request_string = kms_request_to_string(kms->req);
1532
+ if (!request_string) {
1533
+ CLIENT_ERR("error getting GCP KMS encrypt KMS message: %s", kms_request_get_error(kms->req));
1534
+ goto fail;
1535
+ }
1536
+ _mongocrypt_buffer_init(&kms->msg);
1537
+ kms->msg.data = (uint8_t *)request_string;
1538
+ kms->msg.len = (uint32_t)strlen(request_string);
1539
+ kms->msg.owned = true;
1540
+
1541
+ ret = true;
1545
1542
  fail:
1546
- kms_request_opt_destroy (opt);
1547
- bson_free (path_and_query);
1548
- bson_free (payload);
1549
- return ret;
1543
+ kms_request_opt_destroy(opt);
1544
+ bson_free(path_and_query);
1545
+ bson_free(payload);
1546
+ return ret;
1550
1547
  }
1551
1548
 
1552
- bool
1553
- _mongocrypt_kms_ctx_init_gcp_decrypt (
1554
- mongocrypt_kms_ctx_t *kms,
1555
- _mongocrypt_opts_kms_providers_t *kms_providers,
1556
- const char *access_token,
1557
- _mongocrypt_key_doc_t *key,
1558
- _mongocrypt_log_t *log)
1559
- {
1560
- BSON_ASSERT_PARAM (kms);
1561
- BSON_ASSERT_PARAM (kms_providers);
1562
- BSON_ASSERT_PARAM (access_token);
1563
- BSON_ASSERT_PARAM (key);
1564
-
1565
- kms_request_opt_t *opt = NULL;
1566
- mongocrypt_status_t *status;
1567
- char *path_and_query = NULL;
1568
- char *payload = NULL;
1569
- const char *hostname;
1570
- char *request_string;
1571
- bool ret = false;
1572
-
1573
- _init_common (kms, log, MONGOCRYPT_KMS_GCP_DECRYPT);
1574
- status = kms->status;
1575
-
1576
- if (key->kek.provider.gcp.endpoint) {
1577
- kms->endpoint =
1578
- bson_strdup (key->kek.provider.gcp.endpoint->host_and_port);
1579
- hostname = key->kek.provider.gcp.endpoint->host;
1580
- } else {
1581
- kms->endpoint = bson_strdup ("cloudkms.googleapis.com");
1582
- hostname = "cloudkms.googleapis.com";
1583
- }
1584
- _mongocrypt_apply_default_port (&kms->endpoint, DEFAULT_HTTPS_PORT);
1585
-
1586
- opt = kms_request_opt_new ();
1587
- BSON_ASSERT (opt);
1588
- kms_request_opt_set_connection_close (opt, true);
1589
- kms_request_opt_set_provider (opt, KMS_REQUEST_PROVIDER_GCP);
1590
- kms->req = kms_gcp_request_decrypt_new (hostname,
1549
+ bool _mongocrypt_kms_ctx_init_gcp_decrypt(mongocrypt_kms_ctx_t *kms,
1550
+ _mongocrypt_opts_kms_providers_t *kms_providers,
1551
+ const char *access_token,
1552
+ _mongocrypt_key_doc_t *key,
1553
+ const char *kmsid,
1554
+ _mongocrypt_log_t *log) {
1555
+ BSON_ASSERT_PARAM(kms);
1556
+ BSON_ASSERT_PARAM(kms_providers);
1557
+ BSON_ASSERT_PARAM(access_token);
1558
+ BSON_ASSERT_PARAM(key);
1559
+
1560
+ kms_request_opt_t *opt = NULL;
1561
+ mongocrypt_status_t *status;
1562
+ char *path_and_query = NULL;
1563
+ char *payload = NULL;
1564
+ const char *hostname;
1565
+ char *request_string;
1566
+ bool ret = false;
1567
+
1568
+ _init_common(kms, log, MONGOCRYPT_KMS_GCP_DECRYPT, kmsid);
1569
+ status = kms->status;
1570
+
1571
+ if (key->kek.provider.gcp.endpoint) {
1572
+ kms->endpoint = bson_strdup(key->kek.provider.gcp.endpoint->host_and_port);
1573
+ hostname = key->kek.provider.gcp.endpoint->host;
1574
+ } else {
1575
+ kms->endpoint = bson_strdup("cloudkms.googleapis.com");
1576
+ hostname = "cloudkms.googleapis.com";
1577
+ }
1578
+ _mongocrypt_apply_default_port(&kms->endpoint, DEFAULT_HTTPS_PORT);
1579
+
1580
+ opt = kms_request_opt_new();
1581
+ BSON_ASSERT(opt);
1582
+ kms_request_opt_set_connection_close(opt, true);
1583
+ kms_request_opt_set_provider(opt, KMS_REQUEST_PROVIDER_GCP);
1584
+ kms->req = kms_gcp_request_decrypt_new(hostname,
1591
1585
  access_token,
1592
1586
  key->kek.provider.gcp.project_id,
1593
1587
  key->kek.provider.gcp.location,
@@ -1597,189 +1591,276 @@ _mongocrypt_kms_ctx_init_gcp_decrypt (
1597
1591
  key->key_material.len,
1598
1592
  opt);
1599
1593
 
1600
- if (kms_request_get_error (kms->req)) {
1601
- CLIENT_ERR ("error constructing GCP KMS decrypt message: %s",
1602
- kms_request_get_error (kms->req));
1603
- goto fail;
1604
- }
1605
-
1606
- request_string = kms_request_to_string (kms->req);
1607
- if (!request_string) {
1608
- CLIENT_ERR ("error getting GCP KMS decrypt KMS message: %s",
1609
- kms_request_get_error (kms->req));
1610
- goto fail;
1611
- }
1612
- _mongocrypt_buffer_init (&kms->msg);
1613
- kms->msg.data = (uint8_t *) request_string;
1614
- kms->msg.len = (uint32_t) strlen (request_string);
1615
- kms->msg.owned = true;
1616
-
1617
- ret = true;
1594
+ if (kms_request_get_error(kms->req)) {
1595
+ CLIENT_ERR("error constructing GCP KMS decrypt message: %s", kms_request_get_error(kms->req));
1596
+ goto fail;
1597
+ }
1598
+
1599
+ request_string = kms_request_to_string(kms->req);
1600
+ if (!request_string) {
1601
+ CLIENT_ERR("error getting GCP KMS decrypt KMS message: %s", kms_request_get_error(kms->req));
1602
+ goto fail;
1603
+ }
1604
+ _mongocrypt_buffer_init(&kms->msg);
1605
+ kms->msg.data = (uint8_t *)request_string;
1606
+ kms->msg.len = (uint32_t)strlen(request_string);
1607
+ kms->msg.owned = true;
1608
+
1609
+ ret = true;
1618
1610
  fail:
1619
- kms_request_opt_destroy (opt);
1620
- bson_free (path_and_query);
1621
- bson_free (payload);
1622
- return ret;
1611
+ kms_request_opt_destroy(opt);
1612
+ bson_free(path_and_query);
1613
+ bson_free(payload);
1614
+ return ret;
1623
1615
  }
1624
1616
 
1625
- bool
1626
- _mongocrypt_kms_ctx_init_kmip_register (mongocrypt_kms_ctx_t *kms_ctx,
1627
- const _mongocrypt_endpoint_t *endpoint,
1628
- const uint8_t *secretdata,
1629
- uint32_t secretdata_len,
1630
- _mongocrypt_log_t *log)
1631
- {
1632
- BSON_ASSERT_PARAM (kms_ctx);
1633
- BSON_ASSERT_PARAM (endpoint);
1634
- BSON_ASSERT_PARAM (secretdata);
1635
-
1636
- mongocrypt_status_t *status;
1637
- bool ret = false;
1638
- const uint8_t *reqdata;
1639
- size_t reqlen;
1640
-
1641
- _init_common (kms_ctx, log, MONGOCRYPT_KMS_KMIP_REGISTER);
1642
- status = kms_ctx->status;
1643
-
1644
- kms_ctx->endpoint = bson_strdup (endpoint->host_and_port);
1645
- _mongocrypt_apply_default_port (&kms_ctx->endpoint, DEFAULT_KMIP_PORT);
1646
- kms_ctx->req = kms_kmip_request_register_secretdata_new (
1647
- NULL /* reserved */, secretdata, secretdata_len);
1648
-
1649
- if (kms_request_get_error (kms_ctx->req)) {
1650
- CLIENT_ERR ("Error creating KMIP register request: %s",
1651
- kms_request_get_error (kms_ctx->req));
1652
- goto done;
1653
- }
1654
-
1655
- reqdata = kms_request_to_bytes (kms_ctx->req, &reqlen);
1656
- if (!_mongocrypt_buffer_copy_from_data_and_size (
1657
- &kms_ctx->msg, reqdata, reqlen)) {
1658
- CLIENT_ERR ("Error storing KMS request payload");
1659
- goto done;
1660
- }
1661
-
1662
- ret = true;
1617
+ bool _mongocrypt_kms_ctx_init_kmip_register(mongocrypt_kms_ctx_t *kms_ctx,
1618
+ const _mongocrypt_endpoint_t *endpoint,
1619
+ const uint8_t *secretdata,
1620
+ uint32_t secretdata_len,
1621
+ const char *kmsid,
1622
+ _mongocrypt_log_t *log) {
1623
+ BSON_ASSERT_PARAM(kms_ctx);
1624
+ BSON_ASSERT_PARAM(endpoint);
1625
+ BSON_ASSERT_PARAM(secretdata);
1626
+
1627
+ mongocrypt_status_t *status;
1628
+ bool ret = false;
1629
+ const uint8_t *reqdata;
1630
+ size_t reqlen;
1631
+
1632
+ _init_common(kms_ctx, log, MONGOCRYPT_KMS_KMIP_REGISTER, kmsid);
1633
+ status = kms_ctx->status;
1634
+
1635
+ kms_ctx->endpoint = bson_strdup(endpoint->host_and_port);
1636
+ _mongocrypt_apply_default_port(&kms_ctx->endpoint, DEFAULT_KMIP_PORT);
1637
+ kms_ctx->req = kms_kmip_request_register_secretdata_new(NULL /* reserved */, secretdata, secretdata_len);
1638
+
1639
+ if (kms_request_get_error(kms_ctx->req)) {
1640
+ CLIENT_ERR("Error creating KMIP register request: %s", kms_request_get_error(kms_ctx->req));
1641
+ goto done;
1642
+ }
1643
+
1644
+ reqdata = kms_request_to_bytes(kms_ctx->req, &reqlen);
1645
+ if (!_mongocrypt_buffer_copy_from_data_and_size(&kms_ctx->msg, reqdata, reqlen)) {
1646
+ CLIENT_ERR("Error storing KMS request payload");
1647
+ goto done;
1648
+ }
1649
+
1650
+ ret = true;
1663
1651
  done:
1664
- return ret;
1652
+ return ret;
1665
1653
  }
1666
1654
 
1667
- bool
1668
- _mongocrypt_kms_ctx_init_kmip_activate (mongocrypt_kms_ctx_t *kms_ctx,
1669
- const _mongocrypt_endpoint_t *endpoint,
1670
- const char *unique_identifier,
1671
- _mongocrypt_log_t *log)
1672
- {
1673
- BSON_ASSERT_PARAM (kms_ctx);
1674
- BSON_ASSERT_PARAM (endpoint);
1675
- BSON_ASSERT_PARAM (unique_identifier);
1676
-
1677
- mongocrypt_status_t *status;
1678
- bool ret = false;
1679
- size_t reqlen;
1680
- const uint8_t *reqdata;
1681
-
1682
- _init_common (kms_ctx, log, MONGOCRYPT_KMS_KMIP_ACTIVATE);
1683
- status = kms_ctx->status;
1684
-
1685
- kms_ctx->endpoint = bson_strdup (endpoint->host_and_port);
1686
- _mongocrypt_apply_default_port (&kms_ctx->endpoint, DEFAULT_KMIP_PORT);
1687
- kms_ctx->req =
1688
- kms_kmip_request_activate_new (NULL /* reserved */, unique_identifier);
1689
-
1690
- if (kms_request_get_error (kms_ctx->req)) {
1691
- CLIENT_ERR ("Error creating KMIP activate request: %s",
1692
- kms_request_get_error (kms_ctx->req));
1693
- goto done;
1694
- }
1695
-
1696
- reqdata = kms_request_to_bytes (kms_ctx->req, &reqlen);
1697
- if (!_mongocrypt_buffer_copy_from_data_and_size (
1698
- &kms_ctx->msg, reqdata, reqlen)) {
1699
- CLIENT_ERR ("Error storing KMS request payload");
1700
- goto done;
1701
- }
1702
-
1703
- ret = true;
1655
+ bool _mongocrypt_kms_ctx_init_kmip_activate(mongocrypt_kms_ctx_t *kms_ctx,
1656
+ const _mongocrypt_endpoint_t *endpoint,
1657
+ const char *unique_identifier,
1658
+ const char *kmsid,
1659
+ _mongocrypt_log_t *log) {
1660
+ BSON_ASSERT_PARAM(kms_ctx);
1661
+ BSON_ASSERT_PARAM(endpoint);
1662
+ BSON_ASSERT_PARAM(unique_identifier);
1663
+
1664
+ mongocrypt_status_t *status;
1665
+ bool ret = false;
1666
+ size_t reqlen;
1667
+ const uint8_t *reqdata;
1668
+
1669
+ _init_common(kms_ctx, log, MONGOCRYPT_KMS_KMIP_ACTIVATE, kmsid);
1670
+ status = kms_ctx->status;
1671
+
1672
+ kms_ctx->endpoint = bson_strdup(endpoint->host_and_port);
1673
+ _mongocrypt_apply_default_port(&kms_ctx->endpoint, DEFAULT_KMIP_PORT);
1674
+ kms_ctx->req = kms_kmip_request_activate_new(NULL /* reserved */, unique_identifier);
1675
+
1676
+ if (kms_request_get_error(kms_ctx->req)) {
1677
+ CLIENT_ERR("Error creating KMIP activate request: %s", kms_request_get_error(kms_ctx->req));
1678
+ goto done;
1679
+ }
1680
+
1681
+ reqdata = kms_request_to_bytes(kms_ctx->req, &reqlen);
1682
+ if (!_mongocrypt_buffer_copy_from_data_and_size(&kms_ctx->msg, reqdata, reqlen)) {
1683
+ CLIENT_ERR("Error storing KMS request payload");
1684
+ goto done;
1685
+ }
1686
+
1687
+ ret = true;
1704
1688
  done:
1705
- return ret;
1689
+ return ret;
1706
1690
  }
1707
1691
 
1708
- bool
1709
- _mongocrypt_kms_ctx_init_kmip_get (mongocrypt_kms_ctx_t *kms_ctx,
1710
- const _mongocrypt_endpoint_t *endpoint,
1711
- const char *unique_identifier,
1712
- _mongocrypt_log_t *log)
1713
- {
1714
- BSON_ASSERT_PARAM (kms_ctx);
1715
- BSON_ASSERT_PARAM (endpoint);
1716
- BSON_ASSERT_PARAM (unique_identifier);
1717
-
1718
- mongocrypt_status_t *status;
1719
- bool ret = false;
1720
- size_t reqlen;
1721
- const uint8_t *reqdata;
1722
-
1723
- _init_common (kms_ctx, log, MONGOCRYPT_KMS_KMIP_GET);
1724
- status = kms_ctx->status;
1725
-
1726
- kms_ctx->endpoint = bson_strdup (endpoint->host_and_port);
1727
- _mongocrypt_apply_default_port (&kms_ctx->endpoint, DEFAULT_KMIP_PORT);
1728
- kms_ctx->req =
1729
- kms_kmip_request_get_new (NULL /* reserved */, unique_identifier);
1730
-
1731
- if (kms_request_get_error (kms_ctx->req)) {
1732
- CLIENT_ERR ("Error creating KMIP get request: %s",
1733
- kms_request_get_error (kms_ctx->req));
1734
- goto done;
1735
- }
1736
-
1737
- reqdata = kms_request_to_bytes (kms_ctx->req, &reqlen);
1738
- if (!_mongocrypt_buffer_copy_from_data_and_size (
1739
- &kms_ctx->msg, reqdata, reqlen)) {
1740
- CLIENT_ERR ("Error storing KMS request payload");
1741
- goto done;
1742
- }
1743
-
1744
- ret = true;
1692
+ bool _mongocrypt_kms_ctx_init_kmip_get(mongocrypt_kms_ctx_t *kms_ctx,
1693
+ const _mongocrypt_endpoint_t *endpoint,
1694
+ const char *unique_identifier,
1695
+ const char *kmsid,
1696
+ _mongocrypt_log_t *log) {
1697
+ BSON_ASSERT_PARAM(kms_ctx);
1698
+ BSON_ASSERT_PARAM(endpoint);
1699
+ BSON_ASSERT_PARAM(unique_identifier);
1700
+
1701
+ mongocrypt_status_t *status;
1702
+ bool ret = false;
1703
+ size_t reqlen;
1704
+ const uint8_t *reqdata;
1705
+
1706
+ _init_common(kms_ctx, log, MONGOCRYPT_KMS_KMIP_GET, kmsid);
1707
+ status = kms_ctx->status;
1708
+
1709
+ kms_ctx->endpoint = bson_strdup(endpoint->host_and_port);
1710
+ _mongocrypt_apply_default_port(&kms_ctx->endpoint, DEFAULT_KMIP_PORT);
1711
+ kms_ctx->req = kms_kmip_request_get_new(NULL /* reserved */, unique_identifier);
1712
+
1713
+ if (kms_request_get_error(kms_ctx->req)) {
1714
+ CLIENT_ERR("Error creating KMIP get request: %s", kms_request_get_error(kms_ctx->req));
1715
+ goto done;
1716
+ }
1717
+
1718
+ reqdata = kms_request_to_bytes(kms_ctx->req, &reqlen);
1719
+ if (!_mongocrypt_buffer_copy_from_data_and_size(&kms_ctx->msg, reqdata, reqlen)) {
1720
+ CLIENT_ERR("Error storing KMS request payload");
1721
+ goto done;
1722
+ }
1723
+
1724
+ ret = true;
1745
1725
  done:
1746
- return ret;
1726
+ return ret;
1747
1727
  }
1748
1728
 
1749
- static const char *
1750
- set_and_ret (const char *what, uint32_t *len)
1751
- {
1752
- BSON_ASSERT_PARAM (what);
1729
+ bool _mongocrypt_kms_ctx_init_kmip_create(mongocrypt_kms_ctx_t *kms_ctx,
1730
+ const _mongocrypt_endpoint_t *endpoint,
1731
+ const char *kmsid,
1732
+ _mongocrypt_log_t *log) {
1733
+ BSON_ASSERT_PARAM(kms_ctx);
1734
+ BSON_ASSERT_PARAM(endpoint);
1735
+ bool ret = false;
1736
+
1737
+ _init_common(kms_ctx, log, MONGOCRYPT_KMS_KMIP_CREATE, kmsid);
1738
+ mongocrypt_status_t *status = kms_ctx->status;
1739
+ kms_ctx->endpoint = bson_strdup(endpoint->host_and_port);
1740
+ _mongocrypt_apply_default_port(&kms_ctx->endpoint, DEFAULT_KMIP_PORT);
1741
+
1742
+ kms_ctx->req = kms_kmip_request_create_new(NULL /* reserved */);
1743
+
1744
+ if (kms_request_get_error(kms_ctx->req)) {
1745
+ CLIENT_ERR("Error creating KMIP create request: %s", kms_request_get_error(kms_ctx->req));
1746
+ goto done;
1747
+ }
1748
+
1749
+ size_t reqlen;
1750
+ const uint8_t *reqdata = kms_request_to_bytes(kms_ctx->req, &reqlen);
1751
+ if (!_mongocrypt_buffer_copy_from_data_and_size(&kms_ctx->msg, reqdata, reqlen)) {
1752
+ CLIENT_ERR("Error storing KMS request payload");
1753
+ goto done;
1754
+ }
1755
+
1756
+ ret = true;
1757
+ done:
1758
+ return ret;
1759
+ }
1753
1760
 
1754
- if (len) {
1755
- BSON_ASSERT (size_to_uint32 (strlen (what), len));
1756
- }
1757
- return what;
1761
+ bool _mongocrypt_kms_ctx_init_kmip_encrypt(mongocrypt_kms_ctx_t *kms_ctx,
1762
+ const _mongocrypt_endpoint_t *endpoint,
1763
+ const char *unique_identifier,
1764
+ const char *kmsid,
1765
+ _mongocrypt_buffer_t *plaintext,
1766
+ _mongocrypt_log_t *log) {
1767
+ BSON_ASSERT_PARAM(kms_ctx);
1768
+ BSON_ASSERT_PARAM(endpoint);
1769
+ BSON_ASSERT_PARAM(plaintext);
1770
+ bool ret = false;
1771
+
1772
+ _init_common(kms_ctx, log, MONGOCRYPT_KMS_KMIP_ENCRYPT, kmsid);
1773
+ mongocrypt_status_t *status = kms_ctx->status;
1774
+ kms_ctx->endpoint = bson_strdup(endpoint->host_and_port);
1775
+ _mongocrypt_apply_default_port(&kms_ctx->endpoint, DEFAULT_KMIP_PORT);
1776
+
1777
+ kms_ctx->req =
1778
+ kms_kmip_request_encrypt_new(NULL /* reserved */, unique_identifier, plaintext->data, plaintext->len);
1779
+
1780
+ if (kms_request_get_error(kms_ctx->req)) {
1781
+ CLIENT_ERR("Error creating KMIP encrypt request: %s", kms_request_get_error(kms_ctx->req));
1782
+ goto done;
1783
+ }
1784
+
1785
+ size_t reqlen;
1786
+ const uint8_t *reqdata = kms_request_to_bytes(kms_ctx->req, &reqlen);
1787
+ if (!_mongocrypt_buffer_copy_from_data_and_size(&kms_ctx->msg, reqdata, reqlen)) {
1788
+ CLIENT_ERR("Error storing KMS request payload");
1789
+ goto done;
1790
+ }
1791
+
1792
+ ret = true;
1793
+ done:
1794
+ return ret;
1795
+ }
1796
+
1797
+ bool _mongocrypt_kms_ctx_init_kmip_decrypt(mongocrypt_kms_ctx_t *kms_ctx,
1798
+ const _mongocrypt_endpoint_t *endpoint,
1799
+ const char *kmsid,
1800
+ _mongocrypt_key_doc_t *key,
1801
+ _mongocrypt_log_t *log) {
1802
+ BSON_ASSERT_PARAM(kms_ctx);
1803
+ BSON_ASSERT_PARAM(endpoint);
1804
+ BSON_ASSERT_PARAM(key);
1805
+ bool ret = false;
1806
+
1807
+ _init_common(kms_ctx, log, MONGOCRYPT_KMS_KMIP_DECRYPT, kmsid);
1808
+ mongocrypt_status_t *status = kms_ctx->status;
1809
+ kms_ctx->endpoint = bson_strdup(endpoint->host_and_port);
1810
+ _mongocrypt_apply_default_port(&kms_ctx->endpoint, DEFAULT_KMIP_PORT);
1811
+
1812
+ _mongocrypt_buffer_t iv;
1813
+ if (!_mongocrypt_buffer_from_subrange(&iv, &key->key_material, 0, MONGOCRYPT_IV_LEN)) {
1814
+ CLIENT_ERR("Error getting IV from key material");
1815
+ goto done;
1816
+ }
1817
+ _mongocrypt_buffer_t ciphertext;
1818
+ if (!_mongocrypt_buffer_from_subrange(&ciphertext,
1819
+ &key->key_material,
1820
+ MONGOCRYPT_IV_LEN,
1821
+ key->key_material.len - MONGOCRYPT_IV_LEN)) {
1822
+ CLIENT_ERR("Error getting ciphertext from key material");
1823
+ goto done;
1824
+ }
1825
+
1826
+ BSON_ASSERT(key->kek.kms_provider == MONGOCRYPT_KMS_PROVIDER_KMIP);
1827
+ BSON_ASSERT(key->kek.provider.kmip.delegated);
1828
+ kms_ctx->req = kms_kmip_request_decrypt_new(NULL /* reserved */,
1829
+ key->kek.provider.kmip.key_id,
1830
+ ciphertext.data,
1831
+ ciphertext.len,
1832
+ iv.data,
1833
+ iv.len);
1834
+
1835
+ if (kms_request_get_error(kms_ctx->req)) {
1836
+ CLIENT_ERR("Error creating KMIP decrypt request: %s", kms_request_get_error(kms_ctx->req));
1837
+ goto done;
1838
+ }
1839
+
1840
+ size_t reqlen;
1841
+ const uint8_t *reqdata = kms_request_to_bytes(kms_ctx->req, &reqlen);
1842
+ if (!_mongocrypt_buffer_copy_from_data_and_size(&kms_ctx->msg, reqdata, reqlen)) {
1843
+ CLIENT_ERR("Error storing KMS request payload");
1844
+ goto done;
1845
+ }
1846
+
1847
+ ret = true;
1848
+ done:
1849
+ return ret;
1758
1850
  }
1759
1851
 
1760
- const char *
1761
- mongocrypt_kms_ctx_get_kms_provider (mongocrypt_kms_ctx_t *kms, uint32_t *len)
1762
- {
1763
- BSON_ASSERT_PARAM (kms);
1764
- /* len is checked in set_and_ret () before it is used */
1765
-
1766
- switch (kms->req_type) {
1767
- default:
1768
- BSON_ASSERT (false && "unknown KMS request type");
1769
- case MONGOCRYPT_KMS_AWS_ENCRYPT:
1770
- case MONGOCRYPT_KMS_AWS_DECRYPT:
1771
- return set_and_ret ("aws", len);
1772
- case MONGOCRYPT_KMS_AZURE_OAUTH:
1773
- case MONGOCRYPT_KMS_AZURE_WRAPKEY:
1774
- case MONGOCRYPT_KMS_AZURE_UNWRAPKEY:
1775
- return set_and_ret ("azure", len);
1776
- case MONGOCRYPT_KMS_GCP_OAUTH:
1777
- case MONGOCRYPT_KMS_GCP_ENCRYPT:
1778
- case MONGOCRYPT_KMS_GCP_DECRYPT:
1779
- return set_and_ret ("gcp", len);
1780
- case MONGOCRYPT_KMS_KMIP_REGISTER:
1781
- case MONGOCRYPT_KMS_KMIP_ACTIVATE:
1782
- case MONGOCRYPT_KMS_KMIP_GET:
1783
- return set_and_ret ("kmip", len);
1784
- }
1852
+ static const char *set_and_ret(const char *what, uint32_t *len) {
1853
+ BSON_ASSERT_PARAM(what);
1854
+
1855
+ if (len) {
1856
+ BSON_ASSERT(size_to_uint32(strlen(what), len));
1857
+ }
1858
+ return what;
1859
+ }
1860
+
1861
+ const char *mongocrypt_kms_ctx_get_kms_provider(mongocrypt_kms_ctx_t *kms, uint32_t *len) {
1862
+ BSON_ASSERT_PARAM(kms);
1863
+ /* len is checked in set_and_ret () before it is used */
1864
+
1865
+ return set_and_ret(kms->kmsid, len);
1785
1866
  }