xref: /aosp_15_r20/external/cronet/net/ssl/test_ssl_private_key.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 "crypto/rsa_private_key.h"
11 #include "net/base/net_errors.h"
12 #include "net/ssl/openssl_private_key.h"
13 #include "net/ssl/ssl_platform_key_util.h"
14 #include "net/ssl/ssl_private_key.h"
15 #include "net/ssl/threaded_ssl_private_key.h"
16 #include "third_party/boringssl/src/include/openssl/base.h"
17 #include "third_party/boringssl/src/include/openssl/evp.h"
18 #include "third_party/boringssl/src/include/openssl/rsa.h"
19 
20 namespace net {
21 
22 namespace {
23 
24 class FailingSSLPlatformKey : public ThreadedSSLPrivateKey::Delegate {
25  public:
26   FailingSSLPlatformKey() = default;
27 
28   FailingSSLPlatformKey(const FailingSSLPlatformKey&) = delete;
29   FailingSSLPlatformKey& operator=(const FailingSSLPlatformKey&) = delete;
30 
31   ~FailingSSLPlatformKey() override = default;
32 
GetProviderName()33   std::string GetProviderName() override { return "FailingSSLPlatformKey"; }
34 
GetAlgorithmPreferences()35   std::vector<uint16_t> GetAlgorithmPreferences() override {
36     return SSLPrivateKey::DefaultAlgorithmPreferences(EVP_PKEY_RSA,
37                                                       true /* supports PSS */);
38   }
39 
Sign(uint16_t algorithm,base::span<const uint8_t> input,std::vector<uint8_t> * signature)40   Error Sign(uint16_t algorithm,
41              base::span<const uint8_t> input,
42              std::vector<uint8_t>* signature) override {
43     return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED;
44   }
45 };
46 
47 }  // namespace
48 
WrapRSAPrivateKey(crypto::RSAPrivateKey * rsa_private_key)49 scoped_refptr<SSLPrivateKey> WrapRSAPrivateKey(
50     crypto::RSAPrivateKey* rsa_private_key) {
51   return net::WrapOpenSSLPrivateKey(bssl::UpRef(rsa_private_key->key()));
52 }
53 
CreateFailSigningSSLPrivateKey()54 scoped_refptr<SSLPrivateKey> CreateFailSigningSSLPrivateKey() {
55   return base::MakeRefCounted<ThreadedSSLPrivateKey>(
56       std::make_unique<FailingSSLPlatformKey>(), GetSSLPlatformKeyTaskRunner());
57 }
58 
59 }  // namespace net
60