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_key_serialization.h"
18
19 #include <string>
20 #include <utility>
21
22 #include "absl/status/status.h"
23 #include "absl/strings/string_view.h"
24 #include "absl/types/optional.h"
25 #include "tink/internal/util.h"
26 #include "tink/restricted_data.h"
27 #include "tink/util/status.h"
28 #include "tink/util/statusor.h"
29 #include "proto/tink.pb.h"
30
31 namespace crypto {
32 namespace tink {
33 namespace internal {
34
35 using ::google::crypto::tink::KeyData;
36 using ::google::crypto::tink::OutputPrefixType;
37
Create(absl::string_view type_url,RestrictedData serialized_key,KeyData::KeyMaterialType key_material_type,OutputPrefixType output_prefix_type,absl::optional<int> id_requirement)38 util::StatusOr<ProtoKeySerialization> ProtoKeySerialization::Create(
39 absl::string_view type_url, RestrictedData serialized_key,
40 KeyData::KeyMaterialType key_material_type,
41 OutputPrefixType output_prefix_type, absl::optional<int> id_requirement) {
42 if (!IsPrintableAscii(type_url)) {
43 return util::Status(absl::StatusCode::kInvalidArgument,
44 "Non-printable ASCII character in type URL.");
45 }
46 if (output_prefix_type == OutputPrefixType::RAW &&
47 id_requirement.has_value()) {
48 return util::Status(absl::StatusCode::kInvalidArgument,
49 "Keys with a RAW output prefix type should not have an "
50 "ID requirement.");
51 }
52 if (output_prefix_type != OutputPrefixType::RAW &&
53 !id_requirement.has_value()) {
54 return util::Status(
55 absl::StatusCode::kInvalidArgument,
56 "Keys without a RAW output prefix type should have an ID requirement.");
57 }
58 return ProtoKeySerialization(type_url, type_url, std::move(serialized_key),
59 key_material_type, output_prefix_type,
60 id_requirement);
61 }
62
EqualsWithPotentialFalseNegatives(const ProtoKeySerialization & other) const63 bool ProtoKeySerialization::EqualsWithPotentialFalseNegatives(
64 const ProtoKeySerialization& other) const {
65 if (type_url_ != other.type_url_) return false;
66 if (object_identifier_ != other.object_identifier_) return false;
67 if (key_material_type_ != other.key_material_type_) return false;
68 if (output_prefix_type_ != other.output_prefix_type_) return false;
69 if (id_requirement_ != other.id_requirement_) return false;
70 // RestrictedData::operator== is a constant-time comparison.
71 return serialized_key_ == other.serialized_key_;
72 }
73
74 } // namespace internal
75 } // namespace tink
76 } // namespace crypto
77