encrypt.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2020 MongoDB Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include <mongocxx/config/prelude.hpp>
  16. #include <bsoncxx/stdx/optional.hpp>
  17. #include <bsoncxx/types.hpp>
  18. #include <bsoncxx/types/bson_value/view_or_value.hpp>
  19. #include <mongocxx/stdx.hpp>
  20. namespace mongocxx {
  21. MONGOCXX_INLINE_NAMESPACE_BEGIN
  22. class client_encryption;
  23. namespace options {
  24. ///
  25. /// Class representing options for explicit client-side encryption.
  26. ///
  27. class MONGOCXX_API encrypt {
  28. public:
  29. ///
  30. /// Sets the key to use for this encryption operation. A key id can be used instead
  31. /// of a key alt name.
  32. ///
  33. /// If a non-owning bson_value::view is passed in as the key_id, the object that owns
  34. /// key_id's memory must outlive this object.
  35. ///
  36. /// @param key_id
  37. /// The id of the key to use for encryption, as a bson_value containing a
  38. /// UUID (BSON binary subtype 4).
  39. ///
  40. /// @return
  41. /// A reference to this object to facilitate method chaining.
  42. ///
  43. /// @see https://docs.mongodb.com/manual/core/security-client-side-encryption/
  44. ///
  45. encrypt& key_id(bsoncxx::types::bson_value::view_or_value key_id);
  46. ///
  47. /// Sets a name by which to lookup a key from the key vault collection to use
  48. /// for this encryption operation. A key alt name can be used instead of a key id.
  49. ///
  50. /// @param name
  51. /// The name of the key to use for encryption.
  52. ///
  53. /// @return
  54. /// A reference to this obejct to facilitate method chaining.
  55. ///
  56. /// @see https://docs.mongodb.com/manual/reference/method/getClientEncryption/
  57. ///
  58. encrypt& key_alt_name(std::string name);
  59. ///
  60. /// Gets the current key alt name.
  61. ///
  62. /// @return
  63. /// An optional key name.
  64. ///
  65. const stdx::optional<std::string>& key_alt_name() const;
  66. ///
  67. /// Determines which AEAD_AES_256_CBC algorithm to use with HMAC_SHA_512 when
  68. /// encrypting data.
  69. ///
  70. enum class encryption_algorithm : std::uint8_t {
  71. ///
  72. /// Use deterministic encryption.
  73. ///
  74. k_deterministic,
  75. ///
  76. /// Use randomized encryption.
  77. ///
  78. k_random
  79. };
  80. ///
  81. /// Sets the algorithm to use for encryption.
  82. ///
  83. /// @param algorithm
  84. /// An algorithm, either deterministic or random, to use for encryption.
  85. ///
  86. /// @see
  87. /// https://docs.mongodb.com/manual/core/security-client-side-encryption/#encryption-algorithms
  88. ///
  89. encrypt& algorithm(encryption_algorithm algorithm);
  90. ///
  91. /// Gets the current algorithm.
  92. ///
  93. /// @return
  94. /// An optional algorithm.
  95. ///
  96. const stdx::optional<encryption_algorithm>& algorithm() const;
  97. ///
  98. /// Gets the key_id.
  99. ///
  100. /// @return
  101. /// An optional owning bson_value containing the key_id.
  102. ///
  103. const stdx::optional<bsoncxx::types::bson_value::view_or_value>& key_id() const;
  104. private:
  105. friend class mongocxx::client_encryption;
  106. MONGOCXX_PRIVATE void* convert() const;
  107. stdx::optional<bsoncxx::types::bson_value::view_or_value> _key_id;
  108. stdx::optional<std::string> _key_alt_name;
  109. stdx::optional<encryption_algorithm> _algorithm;
  110. };
  111. } // namespace options
  112. MONGOCXX_INLINE_NAMESPACE_END
  113. } // namespace mongocxx
  114. #include <mongocxx/config/postlude.hpp>