1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
// Copyright 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///////////////////////////////////////////////////////////////////////////////
#include "tink/cleartext_keyset_handle.h"
#include <istream>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "absl/container/flat_hash_map.h"
#include "absl/status/status.h"
#include "tink/keyset_handle.h"
#include "tink/keyset_reader.h"
#include "tink/util/errors.h"
#include "tink/util/status.h"
#include "tink/util/statusor.h"
#include "proto/tink.pb.h"
using google::crypto::tink::Keyset;
namespace crypto {
namespace tink {
// static
util::StatusOr<std::unique_ptr<KeysetHandle>> CleartextKeysetHandle::Read(
std::unique_ptr<KeysetReader> reader,
const absl::flat_hash_map<std::string, std::string>&
monitoring_annotations) {
util::StatusOr<std::unique_ptr<Keyset>> keyset_result = reader->Read();
if (!keyset_result.ok()) {
return ToStatusF(absl::StatusCode::kInvalidArgument,
"Error reading keyset data: %s",
keyset_result.status().message());
}
util::StatusOr<std::vector<std::shared_ptr<const KeysetHandle::Entry>>>
entries = KeysetHandle::GetEntriesFromKeyset(**keyset_result);
if (!entries.ok()) {
return entries.status();
}
if (entries->size() != (*keyset_result)->key_size()) {
return util::Status(absl::StatusCode::kInternal,
"Error converting keyset proto into key entries.");
}
std::unique_ptr<KeysetHandle> handle(new KeysetHandle(
std::move(keyset_result.value()), *entries, monitoring_annotations));
return std::move(handle);
}
// static
crypto::tink::util::Status CleartextKeysetHandle::Write(
KeysetWriter* writer, const KeysetHandle& keyset_handle) {
if (!writer) {
return util::Status(absl::StatusCode::kInvalidArgument,
"Error KeysetWriter cannot be null");
}
return writer->Write(keyset_handle.get_keyset());
}
// static
std::unique_ptr<KeysetHandle> CleartextKeysetHandle::GetKeysetHandle(
const Keyset& keyset) {
auto unique_keyset = absl::make_unique<Keyset>(keyset);
std::unique_ptr<KeysetHandle> handle =
absl::WrapUnique(new KeysetHandle(std::move(unique_keyset)));
return handle;
}
// static
const Keyset& CleartextKeysetHandle::GetKeyset(
const KeysetHandle& keyset_handle) {
return keyset_handle.get_keyset();
}
} // namespace tink
} // namespace crypto
|