// Copyright 2015 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef NET_SSL_THREADED_SSL_PRIVATE_KEY_H_ #define NET_SSL_THREADED_SSL_PRIVATE_KEY_H_ #include #include #include #include #include "base/containers/span.h" #include "base/memory/scoped_refptr.h" #include "base/memory/weak_ptr.h" #include "net/base/net_export.h" #include "net/ssl/ssl_private_key.h" namespace base { class SingleThreadTaskRunner; } namespace net { // An SSLPrivateKey implementation which offloads key operations to a background // task runner. class NET_EXPORT ThreadedSSLPrivateKey : public SSLPrivateKey { public: // Interface for consumers to implement to perform the actual signing // operation. class Delegate { public: Delegate() = default; Delegate(const Delegate&) = delete; Delegate& operator=(const Delegate&) = delete; virtual ~Delegate() = default; // Returns a human-readable name of the provider that backs this // SSLPrivateKey, for debugging. If not applicable or available, return the // empty string. // // This method must be efficiently callable on any thread. virtual std::string GetProviderName() = 0; // Returns the algorithms that are supported by the key in decreasing // preference for TLS 1.2 and later. // // This method must be efficiently callable on any thread. virtual std::vector GetAlgorithmPreferences() = 0; // Signs an |input| with the specified TLS signing algorithm. |input| is the // unhashed message to be signed. On success it returns OK and sets // |signature| to the resulting signature. Otherwise it returns a net error // code. // // This method will only be called on the task runner passed to the owning // ThreadedSSLPrivateKey. virtual Error Sign(uint16_t algorithm, base::span input, std::vector* signature) = 0; }; ThreadedSSLPrivateKey( std::unique_ptr delegate, scoped_refptr task_runner); ThreadedSSLPrivateKey(const ThreadedSSLPrivateKey&) = delete; ThreadedSSLPrivateKey& operator=(const ThreadedSSLPrivateKey&) = delete; // SSLPrivateKey implementation. std::string GetProviderName() override; std::vector GetAlgorithmPreferences() override; void Sign(uint16_t algorithm, base::span input, SignCallback callback) override; private: ~ThreadedSSLPrivateKey() override; class Core; scoped_refptr core_; scoped_refptr task_runner_; base::WeakPtrFactory weak_factory_{this}; }; } // namespace net #endif // NET_SSL_THREADED_SSL_PRIVATE_KEY_H_