test_ssl_private_key.cc 4.0 KB

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