• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/ssl/threaded_ssl_private_key.h"
6 
7 #include <string>
8 #include <tuple>
9 #include <utility>
10 
11 #include "base/functional/bind.h"
12 #include "base/location.h"
13 #include "base/task/single_thread_task_runner.h"
14 
15 namespace net {
16 
17 namespace {
18 
DoCallback(const base::WeakPtr<ThreadedSSLPrivateKey> & key,SSLPrivateKey::SignCallback callback,std::tuple<Error,std::unique_ptr<std::vector<uint8_t>>> result)19 void DoCallback(
20     const base::WeakPtr<ThreadedSSLPrivateKey>& key,
21     SSLPrivateKey::SignCallback callback,
22     std::tuple<Error, std::unique_ptr<std::vector<uint8_t>>> result) {
23   auto [error, signature] = std::move(result);
24   if (!key)
25     return;
26   std::move(callback).Run(error, *signature);
27 }
28 
29 }  // anonymous namespace
30 
31 class ThreadedSSLPrivateKey::Core
32     : public base::RefCountedThreadSafe<ThreadedSSLPrivateKey::Core> {
33  public:
Core(std::unique_ptr<ThreadedSSLPrivateKey::Delegate> delegate)34   explicit Core(std::unique_ptr<ThreadedSSLPrivateKey::Delegate> delegate)
35       : delegate_(std::move(delegate)) {}
36 
delegate()37   ThreadedSSLPrivateKey::Delegate* delegate() { return delegate_.get(); }
38 
Sign(uint16_t algorithm,base::span<const uint8_t> input)39   std::tuple<Error, std::unique_ptr<std::vector<uint8_t>>> Sign(
40       uint16_t algorithm,
41       base::span<const uint8_t> input) {
42     auto signature = std::make_unique<std::vector<uint8_t>>();
43     auto error = delegate_->Sign(algorithm, input, signature.get());
44     return std::make_tuple(error, std::move(signature));
45   }
46 
47  private:
48   friend class base::RefCountedThreadSafe<Core>;
49   ~Core() = default;
50 
51   std::unique_ptr<ThreadedSSLPrivateKey::Delegate> delegate_;
52 };
53 
ThreadedSSLPrivateKey(std::unique_ptr<ThreadedSSLPrivateKey::Delegate> delegate,scoped_refptr<base::SingleThreadTaskRunner> task_runner)54 ThreadedSSLPrivateKey::ThreadedSSLPrivateKey(
55     std::unique_ptr<ThreadedSSLPrivateKey::Delegate> delegate,
56     scoped_refptr<base::SingleThreadTaskRunner> task_runner)
57     : core_(base::MakeRefCounted<Core>(std::move(delegate))),
58       task_runner_(std::move(task_runner)) {}
59 
GetProviderName()60 std::string ThreadedSSLPrivateKey::GetProviderName() {
61   return core_->delegate()->GetProviderName();
62 }
63 
GetAlgorithmPreferences()64 std::vector<uint16_t> ThreadedSSLPrivateKey::GetAlgorithmPreferences() {
65   return core_->delegate()->GetAlgorithmPreferences();
66 }
67 
Sign(uint16_t algorithm,base::span<const uint8_t> input,SSLPrivateKey::SignCallback callback)68 void ThreadedSSLPrivateKey::Sign(uint16_t algorithm,
69                                  base::span<const uint8_t> input,
70                                  SSLPrivateKey::SignCallback callback) {
71   task_runner_->PostTaskAndReplyWithResult(
72       FROM_HERE,
73       base::BindOnce(&ThreadedSSLPrivateKey::Core::Sign, core_, algorithm,
74                      std::vector<uint8_t>(input.begin(), input.end())),
75       base::BindOnce(&DoCallback, weak_factory_.GetWeakPtr(),
76                      std::move(callback)));
77 }
78 
79 ThreadedSSLPrivateKey::~ThreadedSSLPrivateKey() = default;
80 
81 }  // namespace net
82