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 #ifndef TINK_INTERNAL_LEGACY_PROTO_KEY_H_ 18 #define TINK_INTERNAL_LEGACY_PROTO_KEY_H_ 19 20 #include <string> 21 22 #include "absl/strings/string_view.h" 23 #include "absl/types/optional.h" 24 #include "tink/internal/proto_key_serialization.h" 25 #include "tink/key.h" 26 #include "tink/parameters.h" 27 #include "tink/secret_key_access_token.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 // Parameters returned by `LegacyProtoKey::GetParameters()` that cannot be used 36 // to create other LegacyProtoKey instances. 37 class UnusableLegacyProtoParameters : public Parameters { 38 public: 39 // Copyable and movable. 40 UnusableLegacyProtoParameters(const UnusableLegacyProtoParameters& other) = 41 default; 42 UnusableLegacyProtoParameters& operator=( 43 const UnusableLegacyProtoParameters& other) = default; 44 UnusableLegacyProtoParameters(UnusableLegacyProtoParameters&& other) = 45 default; 46 UnusableLegacyProtoParameters& operator=( 47 UnusableLegacyProtoParameters&& other) = default; 48 UnusableLegacyProtoParameters(absl::string_view type_url,google::crypto::tink::OutputPrefixType output_prefix_type)49 explicit UnusableLegacyProtoParameters( 50 absl::string_view type_url, 51 google::crypto::tink::OutputPrefixType output_prefix_type) 52 : type_url_(type_url), output_prefix_type_(output_prefix_type) {} 53 HasIdRequirement()54 bool HasIdRequirement() const override { 55 return output_prefix_type_ != google::crypto::tink::OutputPrefixType::RAW; 56 } 57 58 bool operator==(const Parameters& other) const override; 59 60 private: 61 std::string type_url_; 62 google::crypto::tink::OutputPrefixType output_prefix_type_; 63 }; 64 65 // Key type for legacy proto keys. 66 class LegacyProtoKey : public Key { 67 public: 68 // Copyable and movable. 69 LegacyProtoKey(const LegacyProtoKey& other) = default; 70 LegacyProtoKey& operator=(const LegacyProtoKey& other) = default; 71 LegacyProtoKey(LegacyProtoKey&& other) = default; 72 LegacyProtoKey& operator=(LegacyProtoKey&& other) = default; 73 74 // Creates `LegacyProtoKey` object from `serialization`. Requires `token` if 75 // the key material type is either SYMMETRIC or ASYMMETRIC_PRIVATE. 76 static util::StatusOr<LegacyProtoKey> Create( 77 ProtoKeySerialization serialization, 78 absl::optional<SecretKeyAccessToken> token); 79 GetParameters()80 const Parameters& GetParameters() const override { 81 return unusable_proto_parameters_; 82 } 83 GetIdRequirement()84 absl::optional<int> GetIdRequirement() const override { 85 return serialization_.IdRequirement(); 86 } 87 88 bool operator==(const Key& other) const override; 89 90 // Returns `ProtoKeySerialization` pointer for this object. Requires `token` 91 // if the key material type is either SYMMETRIC or ASYMMETRIC_PRIVATE. 92 util::StatusOr<const ProtoKeySerialization*> Serialization( 93 absl::optional<SecretKeyAccessToken> token) const; 94 95 private: LegacyProtoKey(ProtoKeySerialization serialization)96 explicit LegacyProtoKey(ProtoKeySerialization serialization) 97 : serialization_(serialization), 98 unusable_proto_parameters_(serialization.TypeUrl(), 99 serialization.GetOutputPrefixType()) {} 100 101 ProtoKeySerialization serialization_; 102 UnusableLegacyProtoParameters unusable_proto_parameters_; 103 }; 104 105 } // namespace internal 106 } // namespace tink 107 } // namespace crypto 108 109 #endif // TINK_INTERNAL_LEGACY_PROTO_KEY_H_ 110