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_PARAMETERS_SERIALIZER_H_ 18 #define TINK_INTERNAL_PARAMETERS_SERIALIZER_H_ 19 20 #include <functional> 21 #include <memory> 22 #include <string> 23 #include <typeindex> 24 #include <utility> 25 26 #include "absl/status/status.h" 27 #include "absl/strings/string_view.h" 28 #include "tink/internal/serialization.h" 29 #include "tink/internal/serializer_index.h" 30 #include "tink/parameters.h" 31 #include "tink/util/status.h" 32 #include "tink/util/statusor.h" 33 34 namespace crypto { 35 namespace tink { 36 namespace internal { 37 38 // Non-template base class that can be used with internal registry map. 39 class ParametersSerializer { 40 public: 41 // Returns the serialization of `parameters`. 42 virtual util::StatusOr<std::unique_ptr<Serialization>> SerializeParameters( 43 const Parameters& parameters) const = 0; 44 45 // Returns the object identifier for this serialization, which is only valid 46 // for the lifetime of this object. 47 // 48 // The object identifier is a unique identifier per registry for this object 49 // (in the standard proto serialization, it is the type URL). In other words, 50 // when registering a `ParametersSerializer`, the registry will invoke this to 51 // get the handled object identifier. In order to serialize an object of 52 // `ParametersT`, the registry will then obtain the object identifier of 53 // this serialization object, and call the serializer corresponding to this 54 // object. 55 virtual absl::string_view ObjectIdentifier() const = 0; 56 57 // Returns an index that can be used to look up the `ParametersSerializer` 58 // object registered for the `ParametersT` type in a registry. 59 virtual SerializerIndex Index() const = 0; 60 61 virtual ~ParametersSerializer() = default; 62 }; 63 64 // Serializes `ParametersT` objects into `SerializationT` objects. 65 template <typename ParametersT, typename SerializationT> 66 class ParametersSerializerImpl : public ParametersSerializer { 67 public: ParametersSerializerImpl(absl::string_view object_identifier,const std::function<util::StatusOr<SerializationT> (ParametersT)> & function)68 explicit ParametersSerializerImpl( 69 absl::string_view object_identifier, 70 const std::function<util::StatusOr<SerializationT>(ParametersT)>& 71 function) 72 : object_identifier_(object_identifier), function_(function) {} 73 SerializeParameters(const Parameters & parameters)74 util::StatusOr<std::unique_ptr<Serialization>> SerializeParameters( 75 const Parameters& parameters) const override { 76 const ParametersT* pt = dynamic_cast<const ParametersT*>(¶meters); 77 if (pt == nullptr) { 78 return util::Status( 79 absl::StatusCode::kInvalidArgument, 80 "Invalid parameters type for this parameters serializer."); 81 } 82 util::StatusOr<SerializationT> serialization = function_(*pt); 83 if (!serialization.ok()) return serialization.status(); 84 return {absl::make_unique<SerializationT>(std::move(*serialization))}; 85 } 86 ObjectIdentifier()87 absl::string_view ObjectIdentifier() const override { 88 return object_identifier_; 89 } 90 Index()91 SerializerIndex Index() const override { 92 return SerializerIndex::Create<ParametersT, SerializationT>(); 93 } 94 95 private: 96 std::string object_identifier_; 97 std::function<util::StatusOr<SerializationT>(ParametersT)> function_; 98 }; 99 100 } // namespace internal 101 } // namespace tink 102 } // namespace crypto 103 104 #endif // TINK_INTERNAL_PARAMETERS_SERIALIZER_H_ 105