Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors. All rights reserved. |
| 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/logging.h" |
| 11 | #include "base/macros.h" |
| 12 | #include "crypto/rsa_private_key.h" |
| 13 | #include "net/base/net_errors.h" |
| 14 | #include "net/ssl/ssl_platform_key_util.h" |
| 15 | #include "net/ssl/ssl_private_key.h" |
| 16 | #include "net/ssl/threaded_ssl_private_key.h" |
| 17 | #include "third_party/boringssl/src/include/openssl/digest.h" |
| 18 | #include "third_party/boringssl/src/include/openssl/ec.h" |
| 19 | #include "third_party/boringssl/src/include/openssl/evp.h" |
| 20 | #include "third_party/boringssl/src/include/openssl/rsa.h" |
| 21 | #include "third_party/boringssl/src/include/openssl/ssl.h" |
| 22 | |
| 23 | namespace net { |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | class TestSSLPlatformKey : public ThreadedSSLPrivateKey::Delegate { |
| 28 | public: |
| 29 | explicit TestSSLPlatformKey(bssl::UniquePtr<EVP_PKEY> key) |
| 30 | : key_(std::move(key)) {} |
| 31 | |
| 32 | ~TestSSLPlatformKey() override = default; |
| 33 | |
| 34 | std::vector<uint16_t> GetAlgorithmPreferences() override { |
| 35 | return SSLPrivateKey::DefaultAlgorithmPreferences(EVP_PKEY_id(key_.get()), |
| 36 | true /* supports PSS */); |
| 37 | } |
| 38 | |
| 39 | Error Sign(uint16_t algorithm, |
| 40 | base::span<const uint8_t> input, |
| 41 | std::vector<uint8_t>* signature) override { |
| 42 | bssl::ScopedEVP_MD_CTX ctx; |
| 43 | EVP_PKEY_CTX* pctx; |
| 44 | if (!EVP_DigestSignInit(ctx.get(), &pctx, |
| 45 | SSL_get_signature_algorithm_digest(algorithm), |
| 46 | nullptr, key_.get())) { |
| 47 | return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED; |
| 48 | } |
| 49 | if (SSL_is_signature_algorithm_rsa_pss(algorithm)) { |
| 50 | if (!EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) || |
| 51 | !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1 /* hash length */)) { |
| 52 | return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED; |
| 53 | } |
| 54 | } |
| 55 | size_t sig_len = 0; |
| 56 | if (!EVP_DigestSign(ctx.get(), NULL, &sig_len, input.data(), input.size())) |
| 57 | return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED; |
| 58 | signature->resize(sig_len); |
| 59 | if (!EVP_DigestSign(ctx.get(), signature->data(), &sig_len, input.data(), |
| 60 | input.size())) { |
| 61 | return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED; |
| 62 | } |
| 63 | signature->resize(sig_len); |
| 64 | return OK; |
| 65 | } |
| 66 | |
| 67 | private: |
| 68 | bssl::UniquePtr<EVP_PKEY> key_; |
| 69 | |
| 70 | DISALLOW_COPY_AND_ASSIGN(TestSSLPlatformKey); |
| 71 | }; |
| 72 | |
| 73 | } // namespace |
| 74 | |
| 75 | scoped_refptr<SSLPrivateKey> WrapOpenSSLPrivateKey( |
| 76 | bssl::UniquePtr<EVP_PKEY> key) { |
| 77 | if (!key) |
| 78 | return nullptr; |
| 79 | |
| 80 | return base::MakeRefCounted<ThreadedSSLPrivateKey>( |
| 81 | std::make_unique<TestSSLPlatformKey>(std::move(key)), |
| 82 | GetSSLPlatformKeyTaskRunner()); |
| 83 | } |
| 84 | |
| 85 | scoped_refptr<SSLPrivateKey> WrapRSAPrivateKey( |
| 86 | crypto::RSAPrivateKey* rsa_private_key) { |
| 87 | return net::WrapOpenSSLPrivateKey(bssl::UpRef(rsa_private_key->key())); |
| 88 | } |
| 89 | |
| 90 | } // namespace net |