1 // Copyright 2018 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 #include "tink/key_manager.h" 17 18 #include <memory> 19 20 #include "absl/memory/memory.h" 21 22 namespace crypto { 23 namespace tink { 24 25 // A key factory which always fails. 26 class AlwaysFailingKeyFactory : public KeyFactory { 27 public: 28 AlwaysFailingKeyFactory() = delete; AlwaysFailingKeyFactory(const crypto::tink::util::Status & status)29 explicit AlwaysFailingKeyFactory(const crypto::tink::util::Status& status) 30 : status_(status) {} 31 32 crypto::tink::util::StatusOr<std::unique_ptr<portable_proto::MessageLite>> NewKey(const portable_proto::MessageLite & key_format) const33 NewKey(const portable_proto::MessageLite& key_format) const override { 34 return status_; 35 } 36 37 crypto::tink::util::StatusOr<std::unique_ptr<portable_proto::MessageLite>> NewKey(absl::string_view serialized_key_format) const38 NewKey(absl::string_view serialized_key_format) const override { 39 return status_; 40 } 41 42 crypto::tink::util::StatusOr<std::unique_ptr<google::crypto::tink::KeyData>> NewKeyData(absl::string_view serialized_key_format) const43 NewKeyData(absl::string_view serialized_key_format) const override { 44 return status_; 45 } 46 47 private: 48 crypto::tink::util::Status status_; 49 }; AlwaysFailingFactory(const crypto::tink::util::Status & status)50std::unique_ptr<KeyFactory> KeyFactory::AlwaysFailingFactory( 51 const crypto::tink::util::Status& status) { 52 return absl::make_unique<AlwaysFailingKeyFactory>(status); 53 } 54 } // namespace tink 55 } // namespace crypto 56