| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/ssl/test_ssl_private_key.h" | 5 #include "net/ssl/test_ssl_private_key.h" |
| 6 | 6 |
| 7 #include <openssl/digest.h> | 7 #include <openssl/digest.h> |
| 8 #include <openssl/evp.h> | 8 #include <openssl/evp.h> |
| 9 #include <openssl/rsa.h> |
| 9 | 10 |
| 10 #include <utility> | 11 #include <utility> |
| 11 | 12 |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 #include "base/macros.h" | 14 #include "base/macros.h" |
| 14 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 15 #include "crypto/scoped_openssl_types.h" | |
| 16 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 17 #include "net/ssl/ssl_platform_key_task_runner.h" | 17 #include "net/ssl/ssl_platform_key_task_runner.h" |
| 18 #include "net/ssl/ssl_private_key.h" | 18 #include "net/ssl/ssl_private_key.h" |
| 19 #include "net/ssl/threaded_ssl_private_key.h" | 19 #include "net/ssl/threaded_ssl_private_key.h" |
| 20 | 20 |
| 21 namespace net { | 21 namespace net { |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 class TestSSLPlatformKey : public ThreadedSSLPrivateKey::Delegate { | 25 class TestSSLPlatformKey : public ThreadedSSLPrivateKey::Delegate { |
| 26 public: | 26 public: |
| 27 TestSSLPlatformKey(crypto::ScopedEVP_PKEY key, SSLPrivateKey::Type type) | 27 TestSSLPlatformKey(bssl::UniquePtr<EVP_PKEY> key, SSLPrivateKey::Type type) |
| 28 : key_(std::move(key)), type_(type) {} | 28 : key_(std::move(key)), type_(type) {} |
| 29 | 29 |
| 30 ~TestSSLPlatformKey() override {} | 30 ~TestSSLPlatformKey() override {} |
| 31 | 31 |
| 32 SSLPrivateKey::Type GetType() override { return type_; } | 32 SSLPrivateKey::Type GetType() override { return type_; } |
| 33 | 33 |
| 34 std::vector<SSLPrivateKey::Hash> GetDigestPreferences() override { | 34 std::vector<SSLPrivateKey::Hash> GetDigestPreferences() override { |
| 35 static const SSLPrivateKey::Hash kHashes[] = { | 35 static const SSLPrivateKey::Hash kHashes[] = { |
| 36 SSLPrivateKey::Hash::SHA512, SSLPrivateKey::Hash::SHA384, | 36 SSLPrivateKey::Hash::SHA512, SSLPrivateKey::Hash::SHA384, |
| 37 SSLPrivateKey::Hash::SHA256, SSLPrivateKey::Hash::SHA1}; | 37 SSLPrivateKey::Hash::SHA256, SSLPrivateKey::Hash::SHA1}; |
| 38 return std::vector<SSLPrivateKey::Hash>(kHashes, | 38 return std::vector<SSLPrivateKey::Hash>(kHashes, |
| 39 kHashes + arraysize(kHashes)); | 39 kHashes + arraysize(kHashes)); |
| 40 } | 40 } |
| 41 | 41 |
| 42 size_t GetMaxSignatureLengthInBytes() override { | 42 size_t GetMaxSignatureLengthInBytes() override { |
| 43 return EVP_PKEY_size(key_.get()); | 43 return EVP_PKEY_size(key_.get()); |
| 44 } | 44 } |
| 45 | 45 |
| 46 Error SignDigest(SSLPrivateKey::Hash hash, | 46 Error SignDigest(SSLPrivateKey::Hash hash, |
| 47 const base::StringPiece& input, | 47 const base::StringPiece& input, |
| 48 std::vector<uint8_t>* signature) override { | 48 std::vector<uint8_t>* signature) override { |
| 49 crypto::ScopedEVP_PKEY_CTX ctx = | 49 bssl::UniquePtr<EVP_PKEY_CTX> ctx(EVP_PKEY_CTX_new(key_.get(), nullptr)); |
| 50 crypto::ScopedEVP_PKEY_CTX(EVP_PKEY_CTX_new(key_.get(), NULL)); | |
| 51 if (!ctx) | 50 if (!ctx) |
| 52 return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED; | 51 return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED; |
| 53 if (!EVP_PKEY_sign_init(ctx.get())) | 52 if (!EVP_PKEY_sign_init(ctx.get())) |
| 54 return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED; | 53 return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED; |
| 55 | 54 |
| 56 if (type_ == SSLPrivateKey::Type::RSA) { | 55 if (type_ == SSLPrivateKey::Type::RSA) { |
| 57 const EVP_MD* digest = nullptr; | 56 const EVP_MD* digest = nullptr; |
| 58 switch (hash) { | 57 switch (hash) { |
| 59 case SSLPrivateKey::Hash::MD5_SHA1: | 58 case SSLPrivateKey::Hash::MD5_SHA1: |
| 60 digest = EVP_md5_sha1(); | 59 digest = EVP_md5_sha1(); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 91 input_len)) { | 90 input_len)) { |
| 92 return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED; | 91 return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED; |
| 93 } | 92 } |
| 94 | 93 |
| 95 signature->resize(sig_len); | 94 signature->resize(sig_len); |
| 96 | 95 |
| 97 return OK; | 96 return OK; |
| 98 } | 97 } |
| 99 | 98 |
| 100 private: | 99 private: |
| 101 crypto::ScopedEVP_PKEY key_; | 100 bssl::UniquePtr<EVP_PKEY> key_; |
| 102 SSLPrivateKey::Type type_; | 101 SSLPrivateKey::Type type_; |
| 103 | 102 |
| 104 DISALLOW_COPY_AND_ASSIGN(TestSSLPlatformKey); | 103 DISALLOW_COPY_AND_ASSIGN(TestSSLPlatformKey); |
| 105 }; | 104 }; |
| 106 | 105 |
| 107 } // namespace | 106 } // namespace |
| 108 | 107 |
| 109 scoped_refptr<SSLPrivateKey> WrapOpenSSLPrivateKey(crypto::ScopedEVP_PKEY key) { | 108 scoped_refptr<SSLPrivateKey> WrapOpenSSLPrivateKey( |
| 109 bssl::UniquePtr<EVP_PKEY> key) { |
| 110 if (!key) | 110 if (!key) |
| 111 return nullptr; | 111 return nullptr; |
| 112 | 112 |
| 113 SSLPrivateKey::Type type; | 113 SSLPrivateKey::Type type; |
| 114 switch (EVP_PKEY_id(key.get())) { | 114 switch (EVP_PKEY_id(key.get())) { |
| 115 case EVP_PKEY_RSA: | 115 case EVP_PKEY_RSA: |
| 116 type = SSLPrivateKey::Type::RSA; | 116 type = SSLPrivateKey::Type::RSA; |
| 117 break; | 117 break; |
| 118 case EVP_PKEY_EC: | 118 case EVP_PKEY_EC: |
| 119 type = SSLPrivateKey::Type::ECDSA; | 119 type = SSLPrivateKey::Type::ECDSA; |
| 120 break; | 120 break; |
| 121 default: | 121 default: |
| 122 LOG(ERROR) << "Unknown key type: " << EVP_PKEY_id(key.get()); | 122 LOG(ERROR) << "Unknown key type: " << EVP_PKEY_id(key.get()); |
| 123 return nullptr; | 123 return nullptr; |
| 124 } | 124 } |
| 125 return make_scoped_refptr(new ThreadedSSLPrivateKey( | 125 return make_scoped_refptr(new ThreadedSSLPrivateKey( |
| 126 base::MakeUnique<TestSSLPlatformKey>(std::move(key), type), | 126 base::MakeUnique<TestSSLPlatformKey>(std::move(key), type), |
| 127 GetSSLPlatformKeyTaskRunner())); | 127 GetSSLPlatformKeyTaskRunner())); |
| 128 } | 128 } |
| 129 | 129 |
| 130 } // namespace net | 130 } // namespace net |
| OLD | NEW |