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 <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
13 #include "net/ssl/ssl_platform_key_util.h" | 13 #include "net/ssl/ssl_platform_key_util.h" |
14 #include "net/ssl/ssl_private_key.h" | 14 #include "net/ssl/ssl_private_key.h" |
15 #include "net/ssl/threaded_ssl_private_key.h" | 15 #include "net/ssl/threaded_ssl_private_key.h" |
16 #include "third_party/boringssl/src/include/openssl/digest.h" | 16 #include "third_party/boringssl/src/include/openssl/digest.h" |
17 #include "third_party/boringssl/src/include/openssl/ec.h" | 17 #include "third_party/boringssl/src/include/openssl/ec.h" |
18 #include "third_party/boringssl/src/include/openssl/evp.h" | 18 #include "third_party/boringssl/src/include/openssl/evp.h" |
19 #include "third_party/boringssl/src/include/openssl/rsa.h" | 19 #include "third_party/boringssl/src/include/openssl/rsa.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(bssl::UniquePtr<EVP_PKEY> key, SSLPrivateKey::Type type) | 27 explicit TestSSLPlatformKey(bssl::UniquePtr<EVP_PKEY> key) |
28 : key_(std::move(key)), type_(type) {} | 28 : key_(std::move(key)) {} |
29 | 29 |
30 ~TestSSLPlatformKey() override {} | 30 ~TestSSLPlatformKey() override {} |
31 | 31 |
32 SSLPrivateKey::Type GetType() override { return type_; } | |
33 | |
34 std::vector<SSLPrivateKey::Hash> GetDigestPreferences() override { | 32 std::vector<SSLPrivateKey::Hash> GetDigestPreferences() override { |
35 static const SSLPrivateKey::Hash kHashes[] = { | 33 static const SSLPrivateKey::Hash kHashes[] = { |
36 SSLPrivateKey::Hash::SHA512, SSLPrivateKey::Hash::SHA384, | 34 SSLPrivateKey::Hash::SHA512, SSLPrivateKey::Hash::SHA384, |
37 SSLPrivateKey::Hash::SHA256, SSLPrivateKey::Hash::SHA1}; | 35 SSLPrivateKey::Hash::SHA256, SSLPrivateKey::Hash::SHA1}; |
38 return std::vector<SSLPrivateKey::Hash>(kHashes, | 36 return std::vector<SSLPrivateKey::Hash>(kHashes, |
39 kHashes + arraysize(kHashes)); | 37 kHashes + arraysize(kHashes)); |
40 } | 38 } |
41 | 39 |
42 size_t GetMaxSignatureLengthInBytes() override { | |
43 return EVP_PKEY_size(key_.get()); | |
44 } | |
45 | |
46 Error SignDigest(SSLPrivateKey::Hash hash, | 40 Error SignDigest(SSLPrivateKey::Hash hash, |
47 const base::StringPiece& input, | 41 const base::StringPiece& input, |
48 std::vector<uint8_t>* signature) override { | 42 std::vector<uint8_t>* signature) override { |
49 bssl::UniquePtr<EVP_PKEY_CTX> ctx(EVP_PKEY_CTX_new(key_.get(), nullptr)); | 43 bssl::UniquePtr<EVP_PKEY_CTX> ctx(EVP_PKEY_CTX_new(key_.get(), nullptr)); |
50 if (!ctx) | 44 if (!ctx) |
51 return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED; | 45 return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED; |
52 if (!EVP_PKEY_sign_init(ctx.get())) | 46 if (!EVP_PKEY_sign_init(ctx.get())) |
53 return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED; | 47 return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED; |
54 | 48 |
55 if (type_ == SSLPrivateKey::Type::RSA) { | 49 if (EVP_PKEY_id(key_.get()) == EVP_PKEY_RSA) { |
56 const EVP_MD* digest = nullptr; | 50 const EVP_MD* digest = nullptr; |
57 switch (hash) { | 51 switch (hash) { |
58 case SSLPrivateKey::Hash::MD5_SHA1: | 52 case SSLPrivateKey::Hash::MD5_SHA1: |
59 digest = EVP_md5_sha1(); | 53 digest = EVP_md5_sha1(); |
60 break; | 54 break; |
61 case SSLPrivateKey::Hash::SHA1: | 55 case SSLPrivateKey::Hash::SHA1: |
62 digest = EVP_sha1(); | 56 digest = EVP_sha1(); |
63 break; | 57 break; |
64 case SSLPrivateKey::Hash::SHA256: | 58 case SSLPrivateKey::Hash::SHA256: |
65 digest = EVP_sha256(); | 59 digest = EVP_sha256(); |
(...skipping 25 matching lines...) Expand all Loading... |
91 return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED; | 85 return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED; |
92 } | 86 } |
93 | 87 |
94 signature->resize(sig_len); | 88 signature->resize(sig_len); |
95 | 89 |
96 return OK; | 90 return OK; |
97 } | 91 } |
98 | 92 |
99 private: | 93 private: |
100 bssl::UniquePtr<EVP_PKEY> key_; | 94 bssl::UniquePtr<EVP_PKEY> key_; |
101 SSLPrivateKey::Type type_; | |
102 | 95 |
103 DISALLOW_COPY_AND_ASSIGN(TestSSLPlatformKey); | 96 DISALLOW_COPY_AND_ASSIGN(TestSSLPlatformKey); |
104 }; | 97 }; |
105 | 98 |
106 } // namespace | 99 } // namespace |
107 | 100 |
108 scoped_refptr<SSLPrivateKey> WrapOpenSSLPrivateKey( | 101 scoped_refptr<SSLPrivateKey> WrapOpenSSLPrivateKey( |
109 bssl::UniquePtr<EVP_PKEY> key) { | 102 bssl::UniquePtr<EVP_PKEY> key) { |
110 if (!key) | 103 if (!key) |
111 return nullptr; | 104 return nullptr; |
112 | 105 |
113 SSLPrivateKey::Type type; | |
114 switch (EVP_PKEY_id(key.get())) { | |
115 case EVP_PKEY_RSA: | |
116 type = SSLPrivateKey::Type::RSA; | |
117 break; | |
118 case EVP_PKEY_EC: { | |
119 EC_KEY* ec_key = EVP_PKEY_get0_EC_KEY(key.get()); | |
120 int curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)); | |
121 switch (curve) { | |
122 case NID_X9_62_prime256v1: | |
123 type = SSLPrivateKey::Type::ECDSA_P256; | |
124 break; | |
125 case NID_secp384r1: | |
126 type = SSLPrivateKey::Type::ECDSA_P384; | |
127 break; | |
128 case NID_secp521r1: | |
129 type = SSLPrivateKey::Type::ECDSA_P384; | |
130 break; | |
131 default: | |
132 LOG(ERROR) << "Unknown curve: " << curve; | |
133 return nullptr; | |
134 } | |
135 break; | |
136 } | |
137 default: | |
138 LOG(ERROR) << "Unknown key type: " << EVP_PKEY_id(key.get()); | |
139 return nullptr; | |
140 } | |
141 return make_scoped_refptr(new ThreadedSSLPrivateKey( | 106 return make_scoped_refptr(new ThreadedSSLPrivateKey( |
142 base::MakeUnique<TestSSLPlatformKey>(std::move(key), type), | 107 base::MakeUnique<TestSSLPlatformKey>(std::move(key)), |
143 GetSSLPlatformKeyTaskRunner())); | 108 GetSSLPlatformKeyTaskRunner())); |
144 } | 109 } |
145 | 110 |
146 } // namespace net | 111 } // namespace net |
OLD | NEW |