xref: /aosp_15_r20/external/tink/cc/cleartext_keyset_handle.h (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2017 Google 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 //
15 ///////////////////////////////////////////////////////////////////////////////
16 
17 #ifndef TINK_CLEARTEXT_KEYSET_HANDLE_H_
18 #define TINK_CLEARTEXT_KEYSET_HANDLE_H_
19 
20 #include <istream>
21 #include <memory>
22 #include <sstream>
23 #include <string>
24 
25 #include "absl/container/flat_hash_map.h"
26 #include "tink/keyset_handle.h"
27 #include "tink/keyset_reader.h"
28 #include "tink/util/statusor.h"
29 #include "proto/tink.pb.h"
30 
31 namespace crypto {
32 namespace tink {
33 
34 // Creates keyset handles from cleartext keysets. This API allows
35 // loading cleartext keysets, thus its usage should be restricted.
36 class CleartextKeysetHandle {
37  public:
38   // Creates a KeysetHandle with a keyset obtained via `reader`. Optionally
39   // allows to pass monitoring_annotations to attach additional data to the
40   // resulting KeysetHandle, which will be used for monitoring.
41   static crypto::tink::util::StatusOr<std::unique_ptr<KeysetHandle>> Read(
42       std::unique_ptr<KeysetReader> reader,
43       const absl::flat_hash_map<std::string, std::string>&
44           monitoring_annotations = {});
45 
46   // Writes the keyset in the given `keyset_handle` to the `writer` which must
47   // be non-null.
48   static crypto::tink::util::Status Write(KeysetWriter* writer,
49                                           const KeysetHandle& keyset_handle);
50 
51   // Creates a KeysetHandle object for the given 'keyset'.
52   static std::unique_ptr<KeysetHandle> GetKeysetHandle(
53       const google::crypto::tink::Keyset& keyset);
54 
55   // Returns a Keyset-proto from the given 'keyset_handle'.
56   static const google::crypto::tink::Keyset& GetKeyset(
57       const KeysetHandle& keyset_handle);
58 
59  private:
CleartextKeysetHandle()60   CleartextKeysetHandle() {}
61 };
62 
63 }  // namespace tink
64 }  // namespace crypto
65 
66 #endif  // TINK_CLEARTEXT_KEYSET_HANDLE_H_
67