Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(774)

Side by Side Diff: net/ssl/ssl_platform_key_android.cc

Issue 2391213002: Report curve types in ECDSA SSLPrivateKeys. (Closed)
Patch Set: rebase Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/ssl/ssl_platform_key_android.h ('k') | net/ssl/ssl_platform_key_android_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/ssl_platform_key_android.h" 5 #include "net/ssl/ssl_platform_key_android.h"
6 6
7 #include <openssl/ecdsa.h> 7 #include <openssl/ecdsa.h>
8 #include <openssl/mem.h> 8 #include <openssl/mem.h>
9 #include <openssl/nid.h> 9 #include <openssl/nid.h>
10 #include <openssl/rsa.h> 10 #include <openssl/rsa.h>
11 #include <strings.h> 11 #include <strings.h>
12 12
13 #include <memory> 13 #include <memory>
14 #include <utility> 14 #include <utility>
15 #include <vector> 15 #include <vector>
16 16
17 #include "base/android/build_info.h" 17 #include "base/android/build_info.h"
18 #include "base/android/scoped_java_ref.h" 18 #include "base/android/scoped_java_ref.h"
19 #include "base/lazy_instance.h" 19 #include "base/lazy_instance.h"
20 #include "base/logging.h" 20 #include "base/logging.h"
21 #include "base/macros.h" 21 #include "base/macros.h"
22 #include "base/memory/ptr_util.h" 22 #include "base/memory/ptr_util.h"
23 #include "net/android/keystore.h" 23 #include "net/android/keystore.h"
24 #include "net/android/legacy_openssl.h" 24 #include "net/android/legacy_openssl.h"
25 #include "net/base/net_errors.h" 25 #include "net/base/net_errors.h"
26 #include "net/ssl/openssl_client_key_store.h" 26 #include "net/ssl/openssl_client_key_store.h"
27 #include "net/ssl/ssl_platform_key.h" 27 #include "net/ssl/ssl_platform_key.h"
28 #include "net/ssl/ssl_platform_key_task_runner.h" 28 #include "net/ssl/ssl_platform_key_util.h"
29 #include "net/ssl/threaded_ssl_private_key.h" 29 #include "net/ssl/threaded_ssl_private_key.h"
30 30
31 using base::android::JavaRef; 31 using base::android::JavaRef;
32 using base::android::ScopedJavaGlobalRef; 32 using base::android::ScopedJavaGlobalRef;
33 using base::android::ScopedJavaLocalRef; 33 using base::android::ScopedJavaLocalRef;
34 34
35 namespace net { 35 namespace net {
36 36
37 namespace { 37 namespace {
38 38
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 169
170 private: 170 private:
171 SSLPrivateKey::Type type_; 171 SSLPrivateKey::Type type_;
172 ScopedJavaGlobalRef<jobject> key_; 172 ScopedJavaGlobalRef<jobject> key_;
173 size_t max_length_; 173 size_t max_length_;
174 android::AndroidRSA* legacy_rsa_; 174 android::AndroidRSA* legacy_rsa_;
175 175
176 DISALLOW_COPY_AND_ASSIGN(SSLPlatformKeyAndroid); 176 DISALLOW_COPY_AND_ASSIGN(SSLPlatformKeyAndroid);
177 }; 177 };
178 178
179 // VectorBignumSize returns the number of bytes needed to represent the bignum 179 } // namespace
180 // given in |v|, i.e. the length of |v| less any leading zero bytes.
181 size_t VectorBignumSize(const std::vector<uint8_t>& v) {
182 size_t size = v.size();
183 // Ignore any leading zero bytes.
184 for (size_t i = 0; i < v.size() && v[i] == 0; i++) {
185 size--;
186 }
187 return size;
188 }
189 180
190 std::unique_ptr<SSLPlatformKeyAndroid> CreateRsaKey( 181 scoped_refptr<SSLPrivateKey> WrapJavaPrivateKey(
182 const X509Certificate* certificate,
191 const JavaRef<jobject>& key) { 183 const JavaRef<jobject>& key) {
184 SSLPrivateKey::Type type;
185 size_t max_length;
186 if (!GetClientCertInfo(certificate, &type, &max_length))
187 return nullptr;
188
192 android::AndroidRSA* sys_rsa = nullptr; 189 android::AndroidRSA* sys_rsa = nullptr;
193 const int kAndroid42ApiLevel = 17; 190 if (type == SSLPrivateKey::Type::RSA) {
194 if (base::android::BuildInfo::GetInstance()->sdk_int() < kAndroid42ApiLevel) { 191 const int kAndroid42ApiLevel = 17;
195 // Route around platform limitations: if Android < 4.2, then 192 if (base::android::BuildInfo::GetInstance()->sdk_int() <
196 // base::android::RawSignDigestWithPrivateKey() cannot work, so try to get 193 kAndroid42ApiLevel) {
197 // the system OpenSSL's EVP_PKEY backing this PrivateKey object. 194 // Route around platform limitations: if Android < 4.2, then
198 android::AndroidEVP_PKEY* sys_pkey = 195 // base::android::RawSignDigestWithPrivateKey() cannot work, so try to get
199 android::GetOpenSSLSystemHandleForPrivateKey(key); 196 // the system OpenSSL's EVP_PKEY backing this PrivateKey object.
200 if (!sys_pkey) 197 android::AndroidEVP_PKEY* sys_pkey =
201 return nullptr; 198 android::GetOpenSSLSystemHandleForPrivateKey(key);
199 if (!sys_pkey)
200 return nullptr;
202 201
203 if (sys_pkey->type != android::ANDROID_EVP_PKEY_RSA) { 202 if (sys_pkey->type != android::ANDROID_EVP_PKEY_RSA) {
204 LOG(ERROR) << "Private key has wrong type!"; 203 LOG(ERROR) << "Private key has wrong type!";
205 return nullptr; 204 return nullptr;
206 } 205 }
207 206
208 sys_rsa = sys_pkey->pkey.rsa; 207 sys_rsa = sys_pkey->pkey.rsa;
209 if (sys_rsa->engine) { 208 if (sys_rsa->engine) {
210 // |private_key| may not have an engine if the PrivateKey did not come 209 // |private_key| may not have an engine if the PrivateKey did not come
211 // from the key store, such as in unit tests. 210 // from the key store, such as in unit tests.
212 if (strcmp(sys_rsa->engine->id, "keystore") == 0) { 211 if (strcmp(sys_rsa->engine->id, "keystore") == 0) {
213 LeakEngine(key); 212 LeakEngine(key);
214 } else { 213 } else {
215 NOTREACHED(); 214 NOTREACHED();
215 }
216 } 216 }
217 } 217 }
218 } 218 }
219 219
220 std::vector<uint8_t> modulus;
221 if (!android::GetRSAKeyModulus(key, &modulus)) {
222 LOG(ERROR) << "Failed to get private key modulus";
223 return nullptr;
224 }
225
226 return base::MakeUnique<SSLPlatformKeyAndroid>(
227 SSLPrivateKey::Type::RSA, key, VectorBignumSize(modulus), sys_rsa);
228 }
229
230 std::unique_ptr<SSLPlatformKeyAndroid> CreateEcdsaKey(
231 const JavaRef<jobject>& key) {
232 std::vector<uint8_t> order;
233 if (!android::GetECKeyOrder(key, &order)) {
234 LOG(ERROR) << "Can't extract order parameter from EC private key";
235 return nullptr;
236 }
237
238 return base::MakeUnique<SSLPlatformKeyAndroid>(
239 SSLPrivateKey::Type::ECDSA, key,
240 ECDSA_SIG_max_len(VectorBignumSize(order)), nullptr);
241 }
242
243 } // namespace
244
245 scoped_refptr<SSLPrivateKey> WrapJavaPrivateKey(const JavaRef<jobject>& key) {
246 std::unique_ptr<SSLPlatformKeyAndroid> delegate;
247 switch (android::GetPrivateKeyType(key)) {
248 case android::PRIVATE_KEY_TYPE_RSA:
249 delegate = CreateRsaKey(key);
250 break;
251 case android::PRIVATE_KEY_TYPE_ECDSA:
252 delegate = CreateEcdsaKey(key);
253 break;
254 default:
255 LOG(WARNING) << "GetPrivateKeyType() returned invalid type";
256 return nullptr;
257 }
258
259 return make_scoped_refptr(new ThreadedSSLPrivateKey( 220 return make_scoped_refptr(new ThreadedSSLPrivateKey(
260 std::move(delegate), GetSSLPlatformKeyTaskRunner())); 221 base::MakeUnique<SSLPlatformKeyAndroid>(type, key, max_length, sys_rsa),
222 GetSSLPlatformKeyTaskRunner()));
261 } 223 }
262 224
263 scoped_refptr<SSLPrivateKey> FetchClientCertPrivateKey( 225 scoped_refptr<SSLPrivateKey> FetchClientCertPrivateKey(
264 X509Certificate* certificate) { 226 X509Certificate* certificate) {
265 return OpenSSLClientKeyStore::GetInstance()->FetchClientCertPrivateKey( 227 return OpenSSLClientKeyStore::GetInstance()->FetchClientCertPrivateKey(
266 certificate); 228 certificate);
267 } 229 }
268 230
269 } // namespace net 231 } // namespace net
OLDNEW
« no previous file with comments | « net/ssl/ssl_platform_key_android.h ('k') | net/ssl/ssl_platform_key_android_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698