xref: /aosp_15_r20/external/tink/cc/internal/key_type_info_store.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2018 Google LLC
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/internal/key_type_info_store.h"
18 
19 #include <memory>
20 #include <typeindex>
21 #include <utility>
22 
23 #include "absl/status/status.h"
24 #include "tink/util/status.h"
25 #include "tink/util/statusor.h"
26 
27 namespace crypto {
28 namespace tink {
29 namespace internal {
30 
Get(absl::string_view type_url) const31 util::StatusOr<KeyTypeInfoStore::Info*> KeyTypeInfoStore::Get(
32     absl::string_view type_url) const {
33   auto it = type_url_to_info_.find(type_url);
34   if (it == type_url_to_info_.end()) {
35     return ToStatusF(absl::StatusCode::kNotFound,
36                      "No manager for type '%s' has been registered.", type_url);
37   }
38   return it->second.get();
39 }
40 
IsInsertable(absl::string_view type_url,const std::type_index & key_manager_type_index,bool new_key_allowed) const41 util::Status KeyTypeInfoStore::IsInsertable(
42     absl::string_view type_url, const std::type_index& key_manager_type_index,
43     bool new_key_allowed) const {
44   auto it = type_url_to_info_.find(type_url);
45   if (it == type_url_to_info_.end()) {
46     return crypto::tink::util::OkStatus();
47   }
48   if (it->second->key_manager_type_index() != key_manager_type_index) {
49     return ToStatusF(absl::StatusCode::kAlreadyExists,
50                      "A manager for type '%s' has been already registered.",
51                      type_url);
52   }
53   if (!it->second->new_key_allowed() && new_key_allowed) {
54     return ToStatusF(absl::StatusCode::kAlreadyExists,
55                      "A manager for type '%s' has been already registered "
56                      "with forbidden new key operation.",
57                      type_url);
58   }
59   return util::OkStatus();
60 }
61 
62 }  // namespace internal
63 }  // namespace tink
64 }  // namespace crypto
65