xref: /aosp_15_r20/external/tink/cc/aead/kms_envelope_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_ENVELOPE_AEAD_KEY_MANAGER_H_
17 #define TINK_AEAD_KMS_ENVELOPE_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/aead/internal/aead_util.h"
29 #include "tink/core/key_type_manager.h"
30 #include "tink/core/template_util.h"
31 #include "tink/internal/fips_utils.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_envelope.pb.h"
37 #include "proto/tink.pb.h"
38 
39 namespace crypto {
40 namespace tink {
41 
42 class KmsEnvelopeAeadKeyManager
43     : public KeyTypeManager<google::crypto::tink::KmsEnvelopeAeadKey,
44                             google::crypto::tink::KmsEnvelopeAeadKeyFormat,
45                             List<Aead>> {
46  public:
47   class AeadFactory : public PrimitiveFactory<Aead> {
48     crypto::tink::util::StatusOr<std::unique_ptr<Aead>> Create(
49         const google::crypto::tink::KmsEnvelopeAeadKey& key) const override;
50   };
51 
KmsEnvelopeAeadKeyManager()52   KmsEnvelopeAeadKeyManager()
53       : KeyTypeManager(absl::make_unique<AeadFactory>()) {}
54 
get_version()55   uint32_t get_version() const override { return 0; }
56 
key_material_type()57   google::crypto::tink::KeyData::KeyMaterialType key_material_type()
58       const override {
59     return google::crypto::tink::KeyData::REMOTE;
60   }
61 
get_key_type()62   const std::string& get_key_type() const override { return key_type_; }
63 
ValidateKey(const google::crypto::tink::KmsEnvelopeAeadKey & key)64   crypto::tink::util::Status ValidateKey(
65       const google::crypto::tink::KmsEnvelopeAeadKey& key) const override {
66     crypto::tink::util::Status status =
67         ValidateVersion(key.version(), get_version());
68     if (!status.ok()) return status;
69     return ValidateKeyFormat(key.params());
70   }
71 
ValidateKeyFormat(const google::crypto::tink::KmsEnvelopeAeadKeyFormat & format)72   crypto::tink::util::Status ValidateKeyFormat(
73       const google::crypto::tink::KmsEnvelopeAeadKeyFormat& format)
74       const override {
75     if (format.kek_uri().empty()) {
76       return crypto::tink::util::Status(absl::StatusCode::kInvalidArgument,
77                                         "Missing kek_uri.");
78     }
79     if (!internal::IsSupportedKmsEnvelopeAeadDekKeyType(
80             format.dek_template().type_url())) {
81       return util::Status(absl::StatusCode::kInvalidArgument,
82                           "unsupported dek key type");
83     }
84     return util::OkStatus();
85   }
86 
87   crypto::tink::util::StatusOr<google::crypto::tink::KmsEnvelopeAeadKey>
CreateKey(const google::crypto::tink::KmsEnvelopeAeadKeyFormat & key_format)88   CreateKey(const google::crypto::tink::KmsEnvelopeAeadKeyFormat& key_format)
89       const override {
90     google::crypto::tink::KmsEnvelopeAeadKey key;
91     key.set_version(get_version());
92     *(key.mutable_params()) = key_format;
93     return key;
94   }
95 
FipsStatus()96   internal::FipsCompatibility FipsStatus() const override {
97     return internal::FipsCompatibility::kNotFips;
98   }
99 
100  private:
101   const std::string key_type_ =
102       absl::StrCat(kTypeGoogleapisCom,
103                    google::crypto::tink::KmsEnvelopeAeadKey().GetTypeName());
104 };
105 
106 }  // namespace tink
107 }  // namespace crypto
108 
109 #endif  // TINK_AEAD_KMS_ENVELOPE_AEAD_KEY_MANAGER_H_
110