xref: /aosp_15_r20/external/tink/cc/util/fake_kms_client.h (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2020 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_UTIL_FAKE_KMS_CLIENT_H_
18 #define TINK_UTIL_FAKE_KMS_CLIENT_H_
19 
20 #include <memory>
21 #include <string>
22 
23 #include "absl/strings/string_view.h"
24 #include "tink/aead.h"
25 #include "tink/keyset_handle.h"
26 #include "tink/kms_client.h"
27 #include "tink/kms_clients.h"
28 #include "tink/util/status.h"
29 #include "tink/util/statusor.h"
30 
31 namespace crypto {
32 namespace tink {
33 namespace test {
34 
35 // FakeKmsClient is a fake implementation of KmsClient.
36 //
37 // Normally, the 'key_uri' identifies a key that is stored remotely by the KMS,
38 // and every operation is executed remotely using a RPC call to the KMS, since
39 // the key should not be sent to the client.
40 // In this fake implementation we want to avoid these RPC calls. We achieve this
41 // by encoding the key in the 'key_uri'. So the client simply needs to decode
42 // the key and generate an AEAD out of it. This is of course insecure and should
43 // only be used in testing.
44 class FakeKmsClient : public crypto::tink::KmsClient {
45  public:
46   // Creates a new FakeKmsClient that is bound to the key specified in
47   // 'key_uri'.
48   //
49   // Either of arguments can be empty.
50   // If 'key_uri' is empty, then the client is not bound to any particular key.
51   // credentials_path is ignored at the moment.
52   static crypto::tink::util::StatusOr<std::unique_ptr<FakeKmsClient>> 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 a new, random fake key_uri.
60   static crypto::tink::util::StatusOr<std::string> CreateFakeKeyUri();
61 
62   // Returns true iff this client does support KMS key specified by 'key_uri'.
63   bool DoesSupport(absl::string_view key_uri) const override;
64 
65   // Returns an Aead-primitive backed by KMS key specified by 'key_uri',
66   // provided that this KmsClient does support 'key_uri'.
67   crypto::tink::util::StatusOr<std::unique_ptr<Aead>>
68   GetAead(absl::string_view key_uri) const override;
69 
70  private:
FakeKmsClient()71   FakeKmsClient() {}
72   std::string encoded_keyset_;
73 };
74 
75 }  // namespace test
76 }  // namespace tink
77 }  // namespace crypto
78 
79 #endif  // TINK_UTIL_FAKE_KMS_CLIENT_H_
80