xref: /aosp_15_r20/external/tink/cc/aead/kms_aead_key_manager.h (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2019 Google LLC
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 //
15 ///////////////////////////////////////////////////////////////////////////////
16 #ifndef TINK_AEAD_KMS_AEAD_KEY_MANAGER_H_
17 #define TINK_AEAD_KMS_AEAD_KEY_MANAGER_H_
18 
19 #include <stdint.h>
20 
21 #include <memory>
22 #include <string>
23 
24 #include "absl/memory/memory.h"
25 #include "absl/status/status.h"
26 #include "absl/strings/str_cat.h"
27 #include "tink/aead.h"
28 #include "tink/core/key_type_manager.h"
29 #include "tink/core/template_util.h"
30 #include "tink/kms_client.h"
31 #include "tink/kms_clients.h"
32 #include "tink/util/constants.h"
33 #include "tink/util/status.h"
34 #include "tink/util/statusor.h"
35 #include "tink/util/validation.h"
36 #include "proto/kms_aead.pb.h"
37 #include "proto/tink.pb.h"
38 
39 namespace crypto {
40 namespace tink {
41 
42 class KmsAeadKeyManager
43     : public KeyTypeManager<google::crypto::tink::KmsAeadKey,
44                             google::crypto::tink::KmsAeadKeyFormat,
45                             List<Aead>> {
46  public:
47   class AeadFactory : public PrimitiveFactory<Aead> {
Create(const google::crypto::tink::KmsAeadKey & kms_aead_key)48     crypto::tink::util::StatusOr<std::unique_ptr<Aead>> Create(
49         const google::crypto::tink::KmsAeadKey& kms_aead_key) const override {
50       const auto& key_uri = kms_aead_key.params().key_uri();
51       auto kms_client_result = KmsClients::Get(key_uri);
52       if (!kms_client_result.ok()) return kms_client_result.status();
53       return kms_client_result.value()->GetAead(key_uri);
54     }
55   };
56 
KmsAeadKeyManager()57   KmsAeadKeyManager() : KeyTypeManager(absl::make_unique<AeadFactory>()) {}
58 
get_version()59   uint32_t get_version() const override { return 0; }
60 
key_material_type()61   google::crypto::tink::KeyData::KeyMaterialType key_material_type()
62       const override {
63     return google::crypto::tink::KeyData::REMOTE;
64   }
65 
get_key_type()66   const std::string& get_key_type() const override { return key_type_; }
67 
ValidateKey(const google::crypto::tink::KmsAeadKey & key)68   crypto::tink::util::Status ValidateKey(
69       const google::crypto::tink::KmsAeadKey& key) const override {
70     crypto::tink::util::Status status =
71         ValidateVersion(key.version(), get_version());
72     if (!status.ok()) return status;
73     return ValidateKeyFormat(key.params());
74   }
75 
ValidateKeyFormat(const google::crypto::tink::KmsAeadKeyFormat & key_format)76   crypto::tink::util::Status ValidateKeyFormat(
77       const google::crypto::tink::KmsAeadKeyFormat& key_format) const override {
78     if (key_format.key_uri().empty()) {
79       return crypto::tink::util::Status(absl::StatusCode::kInvalidArgument,
80                                         "Missing key_uri.");
81     }
82     return util::OkStatus();
83   }
84 
CreateKey(const google::crypto::tink::KmsAeadKeyFormat & key_format)85   crypto::tink::util::StatusOr<google::crypto::tink::KmsAeadKey> CreateKey(
86       const google::crypto::tink::KmsAeadKeyFormat& key_format) const override {
87     google::crypto::tink::KmsAeadKey kms_aead_key;
88     kms_aead_key.set_version(get_version());
89     *(kms_aead_key.mutable_params()) = key_format;
90     return kms_aead_key;
91   }
92 
93  private:
94   const std::string key_type_ = absl::StrCat(
95       kTypeGoogleapisCom, google::crypto::tink::KmsAeadKey().GetTypeName());
96 };
97 
98 }  // namespace tink
99 }  // namespace crypto
100 
101 #endif  // TINK_AEAD_KMS_AEAD_KEY_MANAGER_H_
102