xref: /aosp_15_r20/external/tink/cc/integration/awskms/aws_kms_client.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 
17 #ifndef TINK_INTEGRATION_AWSKMS_AWS_KMS_CLIENT_H_
18 #define TINK_INTEGRATION_AWSKMS_AWS_KMS_CLIENT_H_
19 
20 #include <memory>
21 
22 #include "aws/core/auth/AWSCredentialsProvider.h"
23 #include "aws/kms/KMSClient.h"
24 #include "absl/strings/string_view.h"
25 #include "absl/synchronization/mutex.h"
26 #include "tink/aead.h"
27 #include "tink/kms_client.h"
28 #include "tink/kms_clients.h"
29 #include "tink/util/status.h"
30 #include "tink/util/statusor.h"
31 
32 namespace crypto {
33 namespace tink {
34 namespace integration {
35 namespace awskms {
36 
37 // AwsKmsClient is an implementation of KmsClient for AWS KMS
38 // (https://aws.amazon.com/kms/).
39 class AwsKmsClient : public crypto::tink::KmsClient {
40  public:
41   // Move only.
42   AwsKmsClient(AwsKmsClient&& other) = default;
43   AwsKmsClient& operator=(AwsKmsClient&& other) = default;
44   AwsKmsClient(const AwsKmsClient&) = delete;
45   AwsKmsClient& operator=(const AwsKmsClient&) = delete;
46 
47   // Creates a new AwsKmsClient that is bound to the key specified in `key_uri`,
48   // if not empty, and that uses the credentials in `credentials_path`, if not
49   // empty, or the default ones to authenticate to the KMS.
50   //
51   // If `key_uri` is empty, then the client is not bound to any particular key.
52   static crypto::tink::util::StatusOr<std::unique_ptr<AwsKmsClient>> New(
53       absl::string_view key_uri, absl::string_view credentials_path);
54 
55   // Creates a new client and registers it in KMSClients.
56   static crypto::tink::util::Status RegisterNewClient(
57       absl::string_view key_uri, absl::string_view credentials_path);
58 
59   // Returns true if: (1) `key_uri` is a valid AWS KMS key URI, and (2) the
60   // resulting AWS key ARN is equals to key_arn_, in case this client is bound
61   // to a specific key.
62   bool DoesSupport(absl::string_view key_uri) const override;
63 
64   crypto::tink::util::StatusOr<std::unique_ptr<Aead>> GetAead(
65       absl::string_view key_uri) const override;
66 
67  private:
AwsKmsClient(absl::string_view key_arn,Aws::Auth::AWSCredentials credentials)68   AwsKmsClient(absl::string_view key_arn, Aws::Auth::AWSCredentials credentials)
69       : key_arn_(key_arn), credentials_(credentials) {}
AwsKmsClient(Aws::Auth::AWSCredentials credentials)70   AwsKmsClient(Aws::Auth::AWSCredentials credentials)
71       : credentials_(credentials) {}
72 
73   std::string key_arn_;
74   Aws::Auth::AWSCredentials credentials_;
75   std::shared_ptr<Aws::KMS::KMSClient> aws_client_;
76 };
77 
78 }  // namespace awskms
79 }  // namespace integration
80 }  // namespace tink
81 }  // namespace crypto
82 
83 #endif  // TINK_INTEGRATION_AWSKMS_AWS_KMS_CLIENT_H_
84