• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 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/test_ssl_private_key.h"
6 
7 #include <memory>
8 #include <utility>
9 
10 #include "base/containers/contains.h"
11 #include "base/containers/to_vector.h"
12 #include "base/task/sequenced_task_runner.h"
13 #include "crypto/rsa_private_key.h"
14 #include "net/base/net_errors.h"
15 #include "net/ssl/openssl_private_key.h"
16 #include "net/ssl/ssl_platform_key_util.h"
17 #include "net/ssl/ssl_private_key.h"
18 #include "net/ssl/threaded_ssl_private_key.h"
19 #include "third_party/boringssl/src/include/openssl/base.h"
20 #include "third_party/boringssl/src/include/openssl/evp.h"
21 #include "third_party/boringssl/src/include/openssl/rsa.h"
22 
23 namespace net {
24 
25 namespace {
26 
27 class FailingSSLPlatformKey : public ThreadedSSLPrivateKey::Delegate {
28  public:
29   FailingSSLPlatformKey() = default;
30 
31   FailingSSLPlatformKey(const FailingSSLPlatformKey&) = delete;
32   FailingSSLPlatformKey& operator=(const FailingSSLPlatformKey&) = delete;
33 
34   ~FailingSSLPlatformKey() override = default;
35 
GetProviderName()36   std::string GetProviderName() override { return "FailingSSLPlatformKey"; }
37 
GetAlgorithmPreferences()38   std::vector<uint16_t> GetAlgorithmPreferences() override {
39     return SSLPrivateKey::DefaultAlgorithmPreferences(EVP_PKEY_RSA,
40                                                       true /* supports PSS */);
41   }
42 
Sign(uint16_t algorithm,base::span<const uint8_t> input,std::vector<uint8_t> * signature)43   Error Sign(uint16_t algorithm,
44              base::span<const uint8_t> input,
45              std::vector<uint8_t>* signature) override {
46     return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED;
47   }
48 };
49 
50 class SSLPrivateKeyWithPreferences : public SSLPrivateKey {
51  public:
SSLPrivateKeyWithPreferences(scoped_refptr<SSLPrivateKey> key,base::span<const uint16_t> prefs)52   SSLPrivateKeyWithPreferences(scoped_refptr<SSLPrivateKey> key,
53                                base::span<const uint16_t> prefs)
54       : key_(std::move(key)), prefs_(base::ToVector(prefs)) {}
55 
GetProviderName()56   std::string GetProviderName() override { return key_->GetProviderName(); }
57 
GetAlgorithmPreferences()58   std::vector<uint16_t> GetAlgorithmPreferences() override { return prefs_; }
59 
Sign(uint16_t algorithm,base::span<const uint8_t> input,SignCallback callback)60   void Sign(uint16_t algorithm,
61             base::span<const uint8_t> input,
62             SignCallback callback) override {
63     if (!base::Contains(prefs_, algorithm)) {
64       base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
65           FROM_HERE, base::BindOnce(std::move(callback),
66                                     ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED,
67                                     std::vector<uint8_t>()));
68       return;
69     }
70 
71     key_->Sign(algorithm, input, std::move(callback));
72   }
73 
74  private:
75   friend class base::RefCountedThreadSafe<SSLPrivateKeyWithPreferences>;
76   ~SSLPrivateKeyWithPreferences() override = default;
77 
78   scoped_refptr<SSLPrivateKey> key_;
79   std::vector<uint16_t> prefs_;
80 };
81 
82 }  // namespace
83 
WrapRSAPrivateKey(crypto::RSAPrivateKey * rsa_private_key)84 scoped_refptr<SSLPrivateKey> WrapRSAPrivateKey(
85     crypto::RSAPrivateKey* rsa_private_key) {
86   return net::WrapOpenSSLPrivateKey(bssl::UpRef(rsa_private_key->key()));
87 }
88 
CreateFailSigningSSLPrivateKey()89 scoped_refptr<SSLPrivateKey> CreateFailSigningSSLPrivateKey() {
90   return base::MakeRefCounted<ThreadedSSLPrivateKey>(
91       std::make_unique<FailingSSLPlatformKey>(), GetSSLPlatformKeyTaskRunner());
92 }
93 
WrapSSLPrivateKeyWithPreferences(scoped_refptr<SSLPrivateKey> key,base::span<const uint16_t> prefs)94 scoped_refptr<SSLPrivateKey> WrapSSLPrivateKeyWithPreferences(
95     scoped_refptr<SSLPrivateKey> key,
96     base::span<const uint16_t> prefs) {
97   return base::MakeRefCounted<SSLPrivateKeyWithPreferences>(std::move(key),
98                                                             prefs);
99 }
100 
101 }  // namespace net
102