libmongocrypt-helper 1.7.4.0.1001 → 1.8.0.0.1001

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