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/hybrid/hybrid_decrypt_wrapper.h"
18
19 #include <memory>
20 #include <string>
21 #include <utility>
22
23 #include "absl/status/status.h"
24 #include "tink/crypto_format.h"
25 #include "tink/hybrid_decrypt.h"
26 #include "tink/internal/monitoring_util.h"
27 #include "tink/internal/registry_impl.h"
28 #include "tink/internal/util.h"
29 #include "tink/monitoring/monitoring.h"
30 #include "tink/primitive_set.h"
31 #include "tink/util/status.h"
32 #include "tink/util/statusor.h"
33
34 namespace crypto {
35 namespace tink {
36
37 namespace {
38
39 constexpr absl::string_view kPrimitive = "hybrid_decrypt";
40 constexpr absl::string_view kDecryptApi = "decrypt";
41
42 class HybridDecryptSetWrapper : public HybridDecrypt {
43 public:
HybridDecryptSetWrapper(std::unique_ptr<PrimitiveSet<HybridDecrypt>> hybrid_decrypt_set,std::unique_ptr<MonitoringClient> monitoring_decryption_client=nullptr)44 explicit HybridDecryptSetWrapper(
45 std::unique_ptr<PrimitiveSet<HybridDecrypt>> hybrid_decrypt_set,
46 std::unique_ptr<MonitoringClient> monitoring_decryption_client = nullptr)
47 : hybrid_decrypt_set_(std::move(hybrid_decrypt_set)),
48 monitoring_decryption_client_(std::move(monitoring_decryption_client)) {
49 }
50
51 crypto::tink::util::StatusOr<std::string> Decrypt(
52 absl::string_view ciphertext,
53 absl::string_view context_info) const override;
54
55 ~HybridDecryptSetWrapper() override = default;
56
57 private:
58 std::unique_ptr<PrimitiveSet<HybridDecrypt>> hybrid_decrypt_set_;
59 std::unique_ptr<MonitoringClient> monitoring_decryption_client_;
60 };
61
Decrypt(absl::string_view ciphertext,absl::string_view context_info) const62 util::StatusOr<std::string> HybridDecryptSetWrapper::Decrypt(
63 absl::string_view ciphertext, absl::string_view context_info) const {
64 // BoringSSL expects a non-null pointer for context_info,
65 // regardless of whether the size is 0.
66 context_info = internal::EnsureStringNonNull(context_info);
67
68 if (ciphertext.length() > CryptoFormat::kNonRawPrefixSize) {
69 absl::string_view key_id =
70 ciphertext.substr(0, CryptoFormat::kNonRawPrefixSize);
71 auto primitives_result = hybrid_decrypt_set_->get_primitives(key_id);
72 if (primitives_result.ok()) {
73 absl::string_view raw_ciphertext =
74 ciphertext.substr(CryptoFormat::kNonRawPrefixSize);
75 for (auto& hybrid_decrypt_entry : *(primitives_result.value())) {
76 HybridDecrypt& hybrid_decrypt = hybrid_decrypt_entry->get_primitive();
77 auto decrypt_result =
78 hybrid_decrypt.Decrypt(raw_ciphertext, context_info);
79 if (decrypt_result.ok()) {
80 if (monitoring_decryption_client_ != nullptr) {
81 monitoring_decryption_client_->Log(
82 hybrid_decrypt_entry->get_key_id(), ciphertext.size());
83 }
84 return std::move(decrypt_result.value());
85 } else {
86 // LOG that a matching key didn't decrypt the ciphertext.
87 }
88 }
89 }
90 }
91
92 // No matching key succeeded with decryption, try all RAW keys.
93 auto raw_primitives_result = hybrid_decrypt_set_->get_raw_primitives();
94 if (raw_primitives_result.ok()) {
95 for (auto& hybrid_decrypt_entry : *(raw_primitives_result.value())) {
96 HybridDecrypt& hybrid_decrypt = hybrid_decrypt_entry->get_primitive();
97 auto decrypt_result = hybrid_decrypt.Decrypt(ciphertext, context_info);
98 if (decrypt_result.ok()) {
99 return std::move(decrypt_result.value());
100 }
101 }
102 }
103 if (monitoring_decryption_client_ != nullptr) {
104 monitoring_decryption_client_->LogFailure();
105 }
106 return util::Status(absl::StatusCode::kInvalidArgument, "decryption failed");
107 }
108
Validate(PrimitiveSet<HybridDecrypt> * hybrid_decrypt_set)109 util::Status Validate(PrimitiveSet<HybridDecrypt>* hybrid_decrypt_set) {
110 if (hybrid_decrypt_set == nullptr) {
111 return util::Status(absl::StatusCode::kInternal,
112 "hybrid_decrypt_set must be non-NULL");
113 }
114 if (hybrid_decrypt_set->get_primary() == nullptr) {
115 return util::Status(absl::StatusCode::kInvalidArgument,
116 "hybrid_decrypt_set has no primary");
117 }
118 return util::OkStatus();
119 }
120
121 } // anonymous namespace
122
123 // static
Wrap(std::unique_ptr<PrimitiveSet<HybridDecrypt>> primitive_set) const124 util::StatusOr<std::unique_ptr<HybridDecrypt>> HybridDecryptWrapper::Wrap(
125 std::unique_ptr<PrimitiveSet<HybridDecrypt>> primitive_set) const {
126 util::Status status = Validate(primitive_set.get());
127 if (!status.ok()) return status;
128
129 MonitoringClientFactory* const monitoring_factory =
130 internal::RegistryImpl::GlobalInstance().GetMonitoringClientFactory();
131
132 // Monitoring is not enabled. Create a wrapper without monitoring clients.
133 if (monitoring_factory == nullptr) {
134 return {
135 absl::make_unique<HybridDecryptSetWrapper>(std::move(primitive_set))};
136 }
137
138 util::StatusOr<MonitoringKeySetInfo> keyset_info =
139 internal::MonitoringKeySetInfoFromPrimitiveSet(*primitive_set);
140 if (!keyset_info.ok()) {
141 return keyset_info.status();
142 }
143
144 util::StatusOr<std::unique_ptr<MonitoringClient>>
145 monitoring_decryption_client = monitoring_factory->New(
146 MonitoringContext(kPrimitive, kDecryptApi, *keyset_info));
147 if (!monitoring_decryption_client.ok()) {
148 return monitoring_decryption_client.status();
149 }
150
151 return {absl::make_unique<HybridDecryptSetWrapper>(
152 std::move(primitive_set), *std::move(monitoring_decryption_client))};
153 }
154
155 } // namespace tink
156 } // namespace crypto
157