xref: /aosp_15_r20/external/tink/cc/internal/proto_parameters_serialization.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2022 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/proto_parameters_serialization.h"
18 
19 #include <string>
20 
21 #include "absl/status/status.h"
22 #include "absl/strings/string_view.h"
23 #include "tink/internal/util.h"
24 #include "tink/util/status.h"
25 #include "tink/util/statusor.h"
26 #include "proto/tink.pb.h"
27 
28 namespace crypto {
29 namespace tink {
30 namespace internal {
31 
32 using ::google::crypto::tink::KeyTemplate;
33 using ::google::crypto::tink::OutputPrefixType;
34 
35 util::StatusOr<ProtoParametersSerialization>
Create(absl::string_view type_url,OutputPrefixType output_prefix_type,absl::string_view serialized_proto)36 ProtoParametersSerialization::Create(absl::string_view type_url,
37                                      OutputPrefixType output_prefix_type,
38                                      absl::string_view serialized_proto) {
39   if (!IsPrintableAscii(type_url)) {
40     return util::Status(absl::StatusCode::kInvalidArgument,
41                         "Non-printable ASCII character in type URL.");
42   }
43   KeyTemplate key_template;
44   key_template.set_type_url(std::string(type_url));
45   key_template.set_output_prefix_type(output_prefix_type);
46   key_template.set_value(std::string(serialized_proto));
47   return ProtoParametersSerialization(key_template);
48 }
49 
50 util::StatusOr<ProtoParametersSerialization>
Create(KeyTemplate key_template)51 ProtoParametersSerialization::Create(KeyTemplate key_template) {
52   if (!IsPrintableAscii(key_template.type_url())) {
53     return util::Status(absl::StatusCode::kInvalidArgument,
54                         "Non-printable ASCII character in type URL.");
55   }
56   return ProtoParametersSerialization(key_template);
57 }
58 
EqualsWithPotentialFalseNegatives(const ProtoParametersSerialization & other) const59 bool ProtoParametersSerialization::EqualsWithPotentialFalseNegatives(
60     const ProtoParametersSerialization& other) const {
61   const ProtoParametersSerialization* that =
62       dynamic_cast<const ProtoParametersSerialization*>(&other);
63   if (that == nullptr) {
64     return false;
65   }
66   if (key_template_.type_url() != that->key_template_.type_url()) {
67     return false;
68   }
69   if (key_template_.output_prefix_type() !=
70       that->key_template_.output_prefix_type()) {
71     return false;
72   }
73   if (key_template_.value() != that->key_template_.value()) {
74     return false;
75   }
76   if (object_identifier_ != that->object_identifier_) {
77     return false;
78   }
79   return true;
80 }
81 
82 }  // namespace internal
83 }  // namespace tink
84 }  // namespace crypto
85