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_KEYSET_HANDLE_BUILDER_ENTRY_H_ 18 #define TINK_INTERNAL_KEYSET_HANDLE_BUILDER_ENTRY_H_ 19 20 #include <memory> 21 #include <utility> 22 23 #include "absl/types/optional.h" 24 #include "tink/key.h" 25 #include "tink/key_status.h" 26 #include "tink/parameters.h" 27 #include "tink/util/statusor.h" 28 #include "proto/tink.pb.h" 29 30 namespace crypto { 31 namespace tink { 32 namespace internal { 33 34 enum class KeyIdStrategyEnum : int { 35 kFixedId = 1, 36 kRandomId = 2, 37 // Added to guard from failures that may be caused by future expansions. 38 kDoNotUseInsteadUseDefaultWhenWritingSwitchStatements = 20, 39 }; 40 41 struct KeyIdStrategy { 42 KeyIdStrategyEnum strategy; 43 absl::optional<int> id_requirement; 44 }; 45 46 // Internal keyset handle builder entry. The public keyset handle builder 47 // entry will delegate its method calls to an instance of this class. 48 class KeysetHandleBuilderEntry { 49 public: 50 KeysetHandleBuilderEntry() = default; 51 virtual ~KeysetHandleBuilderEntry() = default; 52 53 // Sets the key `status` of this entry. SetStatus(KeyStatus status)54 void SetStatus(KeyStatus status) { key_status_ = status; } 55 // Returns key status of this entry. GetStatus()56 KeyStatus GetStatus() const { return key_status_; } 57 58 // Assigns a fixed `id` when this keyset is built. 59 void SetFixedId(int id); 60 // Assigns an unused random id when this keyset is built. 61 void SetRandomId(); 62 63 // Sets this entry as the primary key. SetPrimary()64 void SetPrimary() { is_primary_ = true; } 65 // Unsets this entry as the primary key. UnsetPrimary()66 void UnsetPrimary() { is_primary_ = false; } 67 // Returns whether or not this entry has been marked as a primary. IsPrimary()68 bool IsPrimary() const { return is_primary_; } 69 70 // Returns key id strategy. GetKeyIdStrategy()71 KeyIdStrategy GetKeyIdStrategy() { return strategy_; } 72 // Returns key id strategy enum. GetKeyIdStrategyEnum()73 KeyIdStrategyEnum GetKeyIdStrategyEnum() { return strategy_.strategy; } 74 // Returns key id requirement. GetKeyIdRequirement()75 absl::optional<int> GetKeyIdRequirement() { return strategy_.id_requirement; } 76 77 // Creates a Keyset::Key proto with the specified key `id` from either a 78 // `Key` object or a `Parameters` object. 79 virtual crypto::tink::util::StatusOr<google::crypto::tink::Keyset::Key> 80 CreateKeysetKey(int id) = 0; 81 82 protected: 83 KeyStatus key_status_ = KeyStatus::kDisabled; 84 85 private: 86 bool is_primary_ = false; 87 KeyIdStrategy strategy_ = 88 KeyIdStrategy{KeyIdStrategyEnum::kRandomId, absl::nullopt}; 89 }; 90 91 // Internal keyset handle builder entry constructed from a `Key` object. 92 class KeyEntry : public KeysetHandleBuilderEntry { 93 public: 94 // Movable, but not copyable. 95 KeyEntry(KeyEntry&& other) = default; 96 KeyEntry& operator=(KeyEntry&& other) = default; 97 KeyEntry(const KeyEntry& other) = delete; 98 KeyEntry& operator=(const KeyEntry& other) = delete; 99 KeyEntry(std::shared_ptr<const Key> key)100 explicit KeyEntry(std::shared_ptr<const Key> key) : key_(std::move(key)) {} 101 102 crypto::tink::util::StatusOr<google::crypto::tink::Keyset::Key> 103 CreateKeysetKey(int id) override; 104 105 private: 106 std::shared_ptr<const Key> key_; 107 }; 108 109 // Internal keyset handle builder entry constructed from a `Parameters` object. 110 class ParametersEntry : public KeysetHandleBuilderEntry { 111 public: 112 // Movable, but not copyable. 113 ParametersEntry(ParametersEntry&& other) = default; 114 ParametersEntry& operator=(ParametersEntry&& other) = default; 115 ParametersEntry(const ParametersEntry& other) = delete; 116 ParametersEntry& operator=(const ParametersEntry& other) = delete; 117 ParametersEntry(std::shared_ptr<const Parameters> parameters)118 explicit ParametersEntry(std::shared_ptr<const Parameters> parameters) 119 : parameters_(std::move(parameters)) {} 120 121 crypto::tink::util::StatusOr<google::crypto::tink::Keyset::Key> 122 CreateKeysetKey(int id) override; 123 124 private: 125 std::shared_ptr<const Parameters> parameters_; 126 }; 127 128 } // namespace internal 129 } // namespace tink 130 } // namespace crypto 131 132 #endif // TINK_INTERNAL_KEYSET_HANDLE_BUILDER_ENTRY_H_ 133