xref: /aosp_15_r20/external/tink/cc/json_keyset_reader.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_JSON_KEYSET_READER_H_
18 #define TINK_JSON_KEYSET_READER_H_
19 
20 #include <istream>
21 #include <memory>
22 #include <string>
23 #include <utility>
24 
25 #include "absl/strings/string_view.h"
26 #include "tink/keyset_reader.h"
27 #include "tink/util/statusor.h"
28 #include "proto/tink.pb.h"
29 
30 namespace crypto {
31 namespace tink {
32 
33 // A KeysetReader that can read from some source cleartext or
34 // encrypted keysets in proto JSON wire format, cf.
35 // https://developers.google.com/protocol-buffers/docs/encoding
36 class JsonKeysetReader : public KeysetReader {
37  public:
38   static crypto::tink::util::StatusOr<std::unique_ptr<KeysetReader>> New(
39       std::unique_ptr<std::istream> keyset_stream);
40   static crypto::tink::util::StatusOr<std::unique_ptr<KeysetReader>> New(
41       absl::string_view serialized_keyset);
42 
43   crypto::tink::util::StatusOr<std::unique_ptr<google::crypto::tink::Keyset>>
44   Read() override;
45 
46   crypto::tink::util::StatusOr<
47     std::unique_ptr<google::crypto::tink::EncryptedKeyset>>
48   ReadEncrypted() override;
49 
50  private:
JsonKeysetReader(std::unique_ptr<std::istream> keyset_stream)51   explicit JsonKeysetReader(std::unique_ptr<std::istream> keyset_stream)
52       : serialized_keyset_(""), keyset_stream_(std::move(keyset_stream)) {}
JsonKeysetReader(absl::string_view serialized_keyset)53   explicit JsonKeysetReader(absl::string_view serialized_keyset)
54       : serialized_keyset_(serialized_keyset), keyset_stream_(nullptr) {}
55 
56   std::string serialized_keyset_;
57   std::unique_ptr<std::istream> keyset_stream_;
58 };
59 
60 }  // namespace tink
61 }  // namespace crypto
62 
63 #endif  // TINK_JSON_KEYSET_READER_H_
64