1 // Copyright 2017 Google Inc. 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_KEY_MANAGER_H_ 18 #define TINK_KEY_MANAGER_H_ 19 20 #include <memory> 21 #include <string> 22 23 #include "absl/memory/memory.h" 24 #include "absl/strings/str_cat.h" 25 #include "tink/util/errors.h" 26 #include "tink/util/protobuf_helper.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 34 // Auxiliary containers for methods that generate or extract key material. 35 // These methods are grouped separately, as their functionality 36 // is independent of the primitive of the corresponding KeyManager. 37 class KeyFactory { 38 public: 39 // Helper function which creates a factory which always fails with a specified 40 // status. 41 static std::unique_ptr<KeyFactory> AlwaysFailingFactory( 42 const crypto::tink::util::Status& status); 43 44 // Generates a new random key, based on the specified 'key_format'. 45 virtual crypto::tink::util::StatusOr< 46 std::unique_ptr<portable_proto::MessageLite>> 47 NewKey(const portable_proto::MessageLite& key_format) const = 0; 48 49 // Generates a new random key, based on the specified 'serialized_key_format'. 50 virtual crypto::tink::util::StatusOr< 51 std::unique_ptr<portable_proto::MessageLite>> 52 NewKey(absl::string_view serialized_key_format) const = 0; 53 54 // Generates a new random key, based on the specified 'serialized_key_format', 55 // and wraps it in a KeyData-proto. 56 virtual crypto::tink::util::StatusOr< 57 std::unique_ptr<google::crypto::tink::KeyData>> 58 NewKeyData(absl::string_view serialized_key_format) const = 0; 59 60 virtual ~KeyFactory() = default; 61 }; 62 63 class PrivateKeyFactory : public virtual KeyFactory { 64 public: 65 // Returns public key data extracted from the given serialized_private_key. 66 virtual crypto::tink::util::StatusOr< 67 std::unique_ptr<google::crypto::tink::KeyData>> 68 GetPublicKeyData(absl::string_view serialized_private_key) const = 0; 69 70 ~PrivateKeyFactory() override = default; 71 }; 72 73 /** 74 * KeyManager "understands" keys of a specific key types: it can 75 * generate keys of a supported type and create primitives for 76 * supported keys. A key type is identified by the global name of the 77 * protocol buffer that holds the corresponding key material, and is 78 * given by type_url-field of KeyData-protocol buffer. 79 * 80 * - P: the primitive implemented by keys understood by this manager 81 */ 82 class KeyManagerBase { 83 public: 84 // Returns the type_url identifying the key type handled by this manager. 85 virtual const std::string& get_key_type() const = 0; 86 87 // Returns the version of this key manager. 88 virtual uint32_t get_version() const = 0; 89 90 // Returns a factory that generates keys of the key type 91 // handled by this manager. 92 virtual const KeyFactory& get_key_factory() const = 0; 93 DoesSupport(absl::string_view key_type)94 bool DoesSupport(absl::string_view key_type) const { 95 return (key_type == get_key_type()); 96 } 97 98 virtual ~KeyManagerBase() = default; 99 }; 100 101 template <class P> 102 class KeyManager : public KeyManagerBase { 103 public: 104 // Constructs an instance of P for the given 'key_data'. 105 virtual crypto::tink::util::StatusOr<std::unique_ptr<P>> GetPrimitive( 106 const google::crypto::tink::KeyData& key_data) const = 0; 107 108 // Constructs an instance of P for the given 'key'. 109 virtual crypto::tink::util::StatusOr<std::unique_ptr<P>> GetPrimitive( 110 const portable_proto::MessageLite& key) const = 0; 111 }; 112 113 } // namespace tink 114 } // namespace crypto 115 116 #endif // TINK_KEY_MANAGER_H_ 117