• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "tink/cleartext_keyset_handle.h"
18 
19 #include <istream>
20 #include <memory>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 #include "absl/container/flat_hash_map.h"
26 #include "absl/status/status.h"
27 #include "tink/keyset_handle.h"
28 #include "tink/keyset_reader.h"
29 #include "tink/util/errors.h"
30 #include "tink/util/status.h"
31 #include "tink/util/statusor.h"
32 #include "proto/tink.pb.h"
33 
34 using google::crypto::tink::Keyset;
35 
36 
37 namespace crypto {
38 namespace tink {
39 
40 // static
Read(std::unique_ptr<KeysetReader> reader,const absl::flat_hash_map<std::string,std::string> & monitoring_annotations)41 util::StatusOr<std::unique_ptr<KeysetHandle>> CleartextKeysetHandle::Read(
42     std::unique_ptr<KeysetReader> reader,
43     const absl::flat_hash_map<std::string, std::string>&
44         monitoring_annotations) {
45   util::StatusOr<std::unique_ptr<Keyset>> keyset_result = reader->Read();
46   if (!keyset_result.ok()) {
47     return ToStatusF(absl::StatusCode::kInvalidArgument,
48                      "Error reading keyset data: %s",
49                      keyset_result.status().message());
50   }
51   util::StatusOr<std::vector<std::shared_ptr<const KeysetHandle::Entry>>>
52       entries = KeysetHandle::GetEntriesFromKeyset(**keyset_result);
53   if (!entries.ok()) {
54     return entries.status();
55   }
56   if (entries->size() != (*keyset_result)->key_size()) {
57     return util::Status(absl::StatusCode::kInternal,
58                         "Error converting keyset proto into key entries.");
59   }
60   std::unique_ptr<KeysetHandle> handle(new KeysetHandle(
61       std::move(keyset_result.value()), *entries, monitoring_annotations));
62   return std::move(handle);
63 }
64 
65 // static
Write(KeysetWriter * writer,const KeysetHandle & keyset_handle)66 crypto::tink::util::Status CleartextKeysetHandle::Write(
67     KeysetWriter* writer, const KeysetHandle& keyset_handle) {
68   if (!writer) {
69     return util::Status(absl::StatusCode::kInvalidArgument,
70                         "Error KeysetWriter cannot be null");
71   }
72   return writer->Write(keyset_handle.get_keyset());
73 }
74 
75 // static
GetKeysetHandle(const Keyset & keyset)76 std::unique_ptr<KeysetHandle> CleartextKeysetHandle::GetKeysetHandle(
77     const Keyset& keyset) {
78   auto unique_keyset = absl::make_unique<Keyset>(keyset);
79   std::unique_ptr<KeysetHandle> handle =
80       absl::WrapUnique(new KeysetHandle(std::move(unique_keyset)));
81   return handle;
82 }
83 
84 // static
GetKeyset(const KeysetHandle & keyset_handle)85 const Keyset& CleartextKeysetHandle::GetKeyset(
86     const KeysetHandle& keyset_handle) {
87   return keyset_handle.get_keyset();
88 }
89 
90 }  // namespace tink
91 }  // namespace crypto
92