| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/android/keystore_openssl.h" | 5 #include "net/android/keystore_openssl.h" |
| 6 | 6 |
| 7 #include <jni.h> | 7 #include <jni.h> |
| 8 #include <openssl/bn.h> | 8 #include <openssl/bn.h> |
| 9 // This include is required to get the ECDSA_METHOD structure definition | 9 // This include is required to get the ECDSA_METHOD structure definition |
| 10 // which isn't currently part of the OpenSSL official ABI. This should | 10 // which isn't currently part of the OpenSSL official ABI. This should |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 | 21 |
| 22 #include "base/android/build_info.h" | 22 #include "base/android/build_info.h" |
| 23 #include "base/android/jni_android.h" | 23 #include "base/android/jni_android.h" |
| 24 #include "base/android/scoped_java_ref.h" | 24 #include "base/android/scoped_java_ref.h" |
| 25 #include "base/basictypes.h" | 25 #include "base/basictypes.h" |
| 26 #include "base/lazy_instance.h" | 26 #include "base/lazy_instance.h" |
| 27 #include "base/logging.h" | 27 #include "base/logging.h" |
| 28 #include "crypto/openssl_util.h" | 28 #include "crypto/openssl_util.h" |
| 29 #include "crypto/scoped_openssl_types.h" | 29 #include "crypto/scoped_openssl_types.h" |
| 30 #include "net/android/keystore.h" | 30 #include "net/android/keystore.h" |
| 31 #include "net/android/legacy_openssl.h" |
| 31 #include "net/ssl/ssl_client_cert_type.h" | 32 #include "net/ssl/ssl_client_cert_type.h" |
| 32 | 33 |
| 33 // IMPORTANT NOTE: The following code will currently only work when used | 34 // IMPORTANT NOTE: The following code will currently only work when used |
| 34 // to implement client certificate support with OpenSSL. That's because | 35 // to implement client certificate support with OpenSSL. That's because |
| 35 // only the signing operations used in this use case are implemented here. | 36 // only the signing operations used in this use case are implemented here. |
| 36 // | 37 // |
| 37 // Generally speaking, OpenSSL provides many different ways to sign | 38 // Generally speaking, OpenSSL provides many different ways to sign |
| 38 // digests. This code doesn't support all these cases, only the ones that | 39 // digests. This code doesn't support all these cases, only the ones that |
| 39 // are required to sign the digest during the OpenSSL handshake for TLS. | 40 // are required to sign the digest during the OpenSSL handshake for TLS. |
| 40 // | 41 // |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 // DSA_size() and ECDSA_size() work properly with the wrapper EVP_PKEY. | 90 // DSA_size() and ECDSA_size() work properly with the wrapper EVP_PKEY. |
| 90 // | 91 // |
| 91 // Note that there is no need to define an OpenSSL ENGINE here. These | 92 // Note that there is no need to define an OpenSSL ENGINE here. These |
| 92 // are objects that can be used to expose custom methods (i.e. either | 93 // are objects that can be used to expose custom methods (i.e. either |
| 93 // RSA_METHOD, DSA_METHOD, ECDSA_METHOD, and a large number of other ones | 94 // RSA_METHOD, DSA_METHOD, ECDSA_METHOD, and a large number of other ones |
| 94 // for types not related to this source file), and make them used by | 95 // for types not related to this source file), and make them used by |
| 95 // default for a lot of operations. Very fortunately, this is not needed | 96 // default for a lot of operations. Very fortunately, this is not needed |
| 96 // here, which saves a lot of complexity. | 97 // here, which saves a lot of complexity. |
| 97 | 98 |
| 98 using base::android::ScopedJavaGlobalRef; | 99 using base::android::ScopedJavaGlobalRef; |
| 100 using base::android::ScopedJavaLocalRef; |
| 99 | 101 |
| 100 namespace net { | 102 namespace net { |
| 101 namespace android { | 103 namespace android { |
| 102 | 104 |
| 103 namespace { | 105 namespace { |
| 104 | 106 |
| 105 typedef crypto::ScopedOpenSSL<EC_GROUP, EC_GROUP_free>::Type ScopedEC_GROUP; | 107 typedef crypto::ScopedOpenSSL<EC_GROUP, EC_GROUP_free>::Type ScopedEC_GROUP; |
| 106 | 108 |
| 107 // Custom RSA_METHOD that uses the platform APIs. | 109 // Custom RSA_METHOD that uses the platform APIs. |
| 108 // Note that for now, only signing through RSA_sign() is really supported. | 110 // Note that for now, only signing through RSA_sign() is really supported. |
| 109 // all other method pointers are either stubs returning errors, or no-ops. | 111 // all other method pointers are either stubs returning errors, or no-ops. |
| 110 // See <openssl/rsa.h> for exact declaration of RSA_METHOD. | 112 // See <openssl/rsa.h> for exact declaration of RSA_METHOD. |
| 111 | 113 |
| 114 struct RsaAppData { |
| 115 jobject private_key; |
| 116 AndroidRSA* legacy_rsa; |
| 117 }; |
| 118 |
| 112 int RsaMethodPubEnc(int flen, | 119 int RsaMethodPubEnc(int flen, |
| 113 const unsigned char* from, | 120 const unsigned char* from, |
| 114 unsigned char* to, | 121 unsigned char* to, |
| 115 RSA* rsa, | 122 RSA* rsa, |
| 116 int padding) { | 123 int padding) { |
| 117 NOTIMPLEMENTED(); | 124 NOTIMPLEMENTED(); |
| 118 RSAerr(RSA_F_RSA_PUBLIC_ENCRYPT, RSA_R_RSA_OPERATIONS_NOT_SUPPORTED); | 125 RSAerr(RSA_F_RSA_PUBLIC_ENCRYPT, RSA_R_RSA_OPERATIONS_NOT_SUPPORTED); |
| 119 return -1; | 126 return -1; |
| 120 } | 127 } |
| 121 | 128 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 144 // "RSA/ECB/NoPadding" or "RSA/ECB/PKCS1Padding" transformation as | 151 // "RSA/ECB/NoPadding" or "RSA/ECB/PKCS1Padding" transformation as |
| 145 // appropriate. I believe support for both of these was added in | 152 // appropriate. I believe support for both of these was added in |
| 146 // the same Android version as the "NONEwithRSA" | 153 // the same Android version as the "NONEwithRSA" |
| 147 // java.security.Signature algorithm, so the same version checks | 154 // java.security.Signature algorithm, so the same version checks |
| 148 // for GetRsaLegacyKey should work. | 155 // for GetRsaLegacyKey should work. |
| 149 RSAerr(RSA_F_RSA_PRIVATE_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE); | 156 RSAerr(RSA_F_RSA_PRIVATE_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE); |
| 150 return -1; | 157 return -1; |
| 151 } | 158 } |
| 152 | 159 |
| 153 // Retrieve private key JNI reference. | 160 // Retrieve private key JNI reference. |
| 154 jobject private_key = reinterpret_cast<jobject>(RSA_get_app_data(rsa)); | 161 RsaAppData* app_data = static_cast<RsaAppData*>(RSA_get_app_data(rsa)); |
| 155 if (!private_key) { | 162 if (!app_data || !app_data->private_key) { |
| 156 LOG(WARNING) << "Null JNI reference passed to RsaMethodPrivEnc!"; | 163 LOG(WARNING) << "Null JNI reference passed to RsaMethodPrivEnc!"; |
| 157 RSAerr(RSA_F_RSA_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR); | 164 RSAerr(RSA_F_RSA_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR); |
| 158 return -1; | 165 return -1; |
| 159 } | 166 } |
| 160 | 167 |
| 168 // Pre-4.2 legacy codepath. |
| 169 if (app_data->legacy_rsa) { |
| 170 int ret = app_data->legacy_rsa->meth->rsa_priv_enc( |
| 171 flen, from, to, app_data->legacy_rsa, ANDROID_RSA_PKCS1_PADDING); |
| 172 if (ret < 0) { |
| 173 LOG(WARNING) << "Could not sign message in RsaMethodPrivEnc!"; |
| 174 // System OpenSSL will use a separate error queue, so it is still |
| 175 // necessary to push a new error. |
| 176 // |
| 177 // TODO(davidben): It would be good to also clear the system error queue |
| 178 // if there were some way to convince Java to do it. (Without going |
| 179 // through Java, it's difficult to get a handle on a system OpenSSL |
| 180 // function; dlopen loads a second copy.) |
| 181 RSAerr(RSA_F_RSA_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR); |
| 182 return -1; |
| 183 } |
| 184 return ret; |
| 185 } |
| 186 |
| 161 base::StringPiece from_piece(reinterpret_cast<const char*>(from), flen); | 187 base::StringPiece from_piece(reinterpret_cast<const char*>(from), flen); |
| 162 std::vector<uint8> result; | 188 std::vector<uint8> result; |
| 163 // For RSA keys, this function behaves as RSA_private_encrypt with | 189 // For RSA keys, this function behaves as RSA_private_encrypt with |
| 164 // PKCS#1 padding. | 190 // PKCS#1 padding. |
| 165 if (!RawSignDigestWithPrivateKey(private_key, from_piece, &result)) { | 191 if (!RawSignDigestWithPrivateKey(app_data->private_key, |
| 192 from_piece, &result)) { |
| 166 LOG(WARNING) << "Could not sign message in RsaMethodPrivEnc!"; | 193 LOG(WARNING) << "Could not sign message in RsaMethodPrivEnc!"; |
| 167 RSAerr(RSA_F_RSA_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR); | 194 RSAerr(RSA_F_RSA_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR); |
| 168 return -1; | 195 return -1; |
| 169 } | 196 } |
| 170 | 197 |
| 171 size_t expected_size = static_cast<size_t>(RSA_size(rsa)); | 198 size_t expected_size = static_cast<size_t>(RSA_size(rsa)); |
| 172 if (result.size() > expected_size) { | 199 if (result.size() > expected_size) { |
| 173 LOG(ERROR) << "RSA Signature size mismatch, actual: " | 200 LOG(ERROR) << "RSA Signature size mismatch, actual: " |
| 174 << result.size() << ", expected <= " << expected_size; | 201 << result.size() << ", expected <= " << expected_size; |
| 175 RSAerr(RSA_F_RSA_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR); | 202 RSAerr(RSA_F_RSA_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 195 return -1; | 222 return -1; |
| 196 } | 223 } |
| 197 | 224 |
| 198 int RsaMethodInit(RSA* rsa) { | 225 int RsaMethodInit(RSA* rsa) { |
| 199 return 0; | 226 return 0; |
| 200 } | 227 } |
| 201 | 228 |
| 202 int RsaMethodFinish(RSA* rsa) { | 229 int RsaMethodFinish(RSA* rsa) { |
| 203 // Ensure the global JNI reference created with this wrapper is | 230 // Ensure the global JNI reference created with this wrapper is |
| 204 // properly destroyed with it. | 231 // properly destroyed with it. |
| 205 jobject key = reinterpret_cast<jobject>(RSA_get_app_data(rsa)); | 232 RsaAppData* app_data = static_cast<RsaAppData*>(RSA_get_app_data(rsa)); |
| 206 if (key != NULL) { | 233 if (app_data != NULL) { |
| 207 RSA_set_app_data(rsa, NULL); | 234 RSA_set_app_data(rsa, NULL); |
| 208 ReleaseKey(key); | 235 ReleaseKey(app_data->private_key); |
| 236 delete app_data; |
| 209 } | 237 } |
| 210 // Actual return value is ignored by OpenSSL. There are no docs | 238 // Actual return value is ignored by OpenSSL. There are no docs |
| 211 // explaining what this is supposed to be. | 239 // explaining what this is supposed to be. |
| 212 return 0; | 240 return 0; |
| 213 } | 241 } |
| 214 | 242 |
| 215 const RSA_METHOD android_rsa_method = { | 243 const RSA_METHOD android_rsa_method = { |
| 216 /* .name = */ "Android signing-only RSA method", | 244 /* .name = */ "Android signing-only RSA method", |
| 217 /* .rsa_pub_enc = */ RsaMethodPubEnc, | 245 /* .rsa_pub_enc = */ RsaMethodPubEnc, |
| 218 /* .rsa_pub_dec = */ RsaMethodPubDec, | 246 /* .rsa_pub_dec = */ RsaMethodPubDec, |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 if (new_num == NULL) | 293 if (new_num == NULL) |
| 266 return false; | 294 return false; |
| 267 | 295 |
| 268 if (old_num == NULL) | 296 if (old_num == NULL) |
| 269 *num_ptr = new_num; | 297 *num_ptr = new_num; |
| 270 return true; | 298 return true; |
| 271 } | 299 } |
| 272 | 300 |
| 273 // Setup an EVP_PKEY to wrap an existing platform RSA PrivateKey object. | 301 // Setup an EVP_PKEY to wrap an existing platform RSA PrivateKey object. |
| 274 // |private_key| is the JNI reference (local or global) to the object. | 302 // |private_key| is the JNI reference (local or global) to the object. |
| 303 // |legacy_rsa|, if non-NULL, is a pointer to the system OpenSSL RSA object |
| 304 // backing |private_key|. This parameter is only used for Android < 4.2 to |
| 305 // implement key operations not exposed by the platform. |
| 275 // |pkey| is the EVP_PKEY to setup as a wrapper. | 306 // |pkey| is the EVP_PKEY to setup as a wrapper. |
| 276 // Returns true on success, false otherwise. | 307 // Returns true on success, false otherwise. |
| 277 // On success, this creates a new global JNI reference to the object | 308 // On success, this creates a new global JNI reference to the object |
| 278 // that is owned by and destroyed with the EVP_PKEY. I.e. caller can | 309 // that is owned by and destroyed with the EVP_PKEY. I.e. caller can |
| 279 // free |private_key| after the call. | 310 // free |private_key| after the call. |
| 280 // IMPORTANT: The EVP_PKEY will *only* work on Android >= 4.2. For older | 311 bool GetRsaPkeyWrapper(jobject private_key, |
| 281 // platforms, use GetRsaLegacyKey() instead. | 312 AndroidRSA* legacy_rsa, |
| 282 bool GetRsaPkeyWrapper(jobject private_key, EVP_PKEY* pkey) { | 313 EVP_PKEY* pkey) { |
| 283 crypto::ScopedRSA rsa(RSA_new()); | 314 crypto::ScopedRSA rsa(RSA_new()); |
| 284 RSA_set_method(rsa.get(), &android_rsa_method); | 315 RSA_set_method(rsa.get(), &android_rsa_method); |
| 285 | 316 |
| 286 // HACK: RSA_size() doesn't work with custom RSA_METHODs. To ensure that | 317 // HACK: RSA_size() doesn't work with custom RSA_METHODs. To ensure that |
| 287 // it will return the right value, set the 'n' field of the RSA object | 318 // it will return the right value, set the 'n' field of the RSA object |
| 288 // to match the private key's modulus. | 319 // to match the private key's modulus. |
| 320 // |
| 321 // TODO(davidben): After switching to BoringSSL, consider making RSA_size call |
| 322 // into an RSA_METHOD hook. |
| 289 std::vector<uint8> modulus; | 323 std::vector<uint8> modulus; |
| 290 if (!GetRSAKeyModulus(private_key, &modulus)) { | 324 if (!GetRSAKeyModulus(private_key, &modulus)) { |
| 291 LOG(ERROR) << "Failed to get private key modulus"; | 325 LOG(ERROR) << "Failed to get private key modulus"; |
| 292 return false; | 326 return false; |
| 293 } | 327 } |
| 294 if (!SwapBigNumPtrFromBytes(modulus, &rsa.get()->n)) { | 328 if (!SwapBigNumPtrFromBytes(modulus, &rsa.get()->n)) { |
| 295 LOG(ERROR) << "Failed to decode private key modulus"; | 329 LOG(ERROR) << "Failed to decode private key modulus"; |
| 296 return false; | 330 return false; |
| 297 } | 331 } |
| 298 | 332 |
| 299 ScopedJavaGlobalRef<jobject> global_key; | 333 ScopedJavaGlobalRef<jobject> global_key; |
| 300 global_key.Reset(NULL, private_key); | 334 global_key.Reset(NULL, private_key); |
| 301 if (global_key.is_null()) { | 335 if (global_key.is_null()) { |
| 302 LOG(ERROR) << "Could not create global JNI reference"; | 336 LOG(ERROR) << "Could not create global JNI reference"; |
| 303 return false; | 337 return false; |
| 304 } | 338 } |
| 305 RSA_set_app_data(rsa.get(), global_key.Release()); | 339 RsaAppData* app_data = new RsaAppData(); |
| 340 app_data->private_key = global_key.Release(); |
| 341 app_data->legacy_rsa = legacy_rsa; |
| 342 RSA_set_app_data(rsa.get(), app_data); |
| 306 EVP_PKEY_assign_RSA(pkey, rsa.release()); | 343 EVP_PKEY_assign_RSA(pkey, rsa.release()); |
| 307 return true; | 344 return true; |
| 308 } | 345 } |
| 309 | 346 |
| 310 // On Android < 4.2, the libkeystore.so ENGINE uses CRYPTO_EX_DATA and is not | 347 // On Android < 4.2, the libkeystore.so ENGINE uses CRYPTO_EX_DATA and is not |
| 311 // added to the global engine list. If all references to it are dropped, OpenSSL | 348 // added to the global engine list. If all references to it are dropped, OpenSSL |
| 312 // will dlclose the module, leaving a dangling function pointer in the RSA | 349 // will dlclose the module, leaving a dangling function pointer in the RSA |
| 313 // CRYPTO_EX_DATA class. To work around this, leak an extra reference to the | 350 // CRYPTO_EX_DATA class. To work around this, leak an extra reference to the |
| 314 // ENGINE we extract in GetRsaLegacyKey. | 351 // ENGINE we extract in GetRsaLegacyKey. |
| 315 // | 352 // |
| 316 // In 4.2, this change avoids the problem: | 353 // In 4.2, this change avoids the problem: |
| 317 // https://android.googlesource.com/platform/libcore/+/106a8928fb4249f2f3d4dba1d
ddbe73ca5cb3d61 | 354 // https://android.googlesource.com/platform/libcore/+/106a8928fb4249f2f3d4dba1d
ddbe73ca5cb3d61 |
| 318 // | 355 // |
| 319 // https://crbug.com/381465 | 356 // https://crbug.com/381465 |
| 320 class KeystoreEngineWorkaround { | 357 class KeystoreEngineWorkaround { |
| 321 public: | 358 public: |
| 322 KeystoreEngineWorkaround() : leaked_engine_(false) {} | 359 KeystoreEngineWorkaround() {} |
| 323 | 360 |
| 324 void LeakRsaEngine(EVP_PKEY* pkey) { | 361 void LeakEngine(jobject private_key) { |
| 325 if (leaked_engine_) | 362 if (!engine_.is_null()) |
| 326 return; | 363 return; |
| 327 crypto::ScopedRSA rsa(EVP_PKEY_get1_RSA(pkey)); | 364 ScopedJavaLocalRef<jobject> engine = |
| 328 if (!rsa.get() || | 365 GetOpenSSLEngineForPrivateKey(private_key); |
| 329 !rsa.get()->engine || | 366 if (engine.is_null()) { |
| 330 strcmp(ENGINE_get_id(rsa.get()->engine), "keystore") || | |
| 331 !ENGINE_init(rsa.get()->engine)) { | |
| 332 NOTREACHED(); | 367 NOTREACHED(); |
| 333 return; | 368 return; |
| 334 } | 369 } |
| 335 leaked_engine_ = true; | 370 engine_.Reset(engine); |
| 336 } | 371 } |
| 337 | 372 |
| 338 private: | 373 private: |
| 339 bool leaked_engine_; | 374 ScopedJavaGlobalRef<jobject> engine_; |
| 340 }; | 375 }; |
| 341 | 376 |
| 342 void LeakRsaEngine(EVP_PKEY* pkey) { | 377 void LeakEngine(jobject private_key) { |
| 343 static base::LazyInstance<KeystoreEngineWorkaround>::Leaky s_instance = | 378 static base::LazyInstance<KeystoreEngineWorkaround>::Leaky s_instance = |
| 344 LAZY_INSTANCE_INITIALIZER; | 379 LAZY_INSTANCE_INITIALIZER; |
| 345 s_instance.Get().LeakRsaEngine(pkey); | 380 s_instance.Get().LeakEngine(private_key); |
| 346 } | 381 } |
| 347 | 382 |
| 348 // Setup an EVP_PKEY to wrap an existing platform RSA PrivateKey object | 383 // Setup an EVP_PKEY to wrap an existing platform RSA PrivateKey object |
| 349 // for Android 4.0 to 4.1.x. Must only be used on Android < 4.2. | 384 // for Android 4.0 to 4.1.x. Must only be used on Android < 4.2. |
| 350 // |private_key| is a JNI reference (local or global) to the object. | 385 // |private_key| is a JNI reference (local or global) to the object. |
| 351 // |pkey| is the EVP_PKEY to setup as a wrapper. | 386 // |pkey| is the EVP_PKEY to setup as a wrapper. |
| 352 // Returns true on success, false otherwise. | 387 // Returns true on success, false otherwise. |
| 353 EVP_PKEY* GetRsaLegacyKey(jobject private_key) { | 388 EVP_PKEY* GetRsaLegacyKey(jobject private_key) { |
| 354 EVP_PKEY* sys_pkey = | 389 AndroidEVP_PKEY* sys_pkey = |
| 355 GetOpenSSLSystemHandleForPrivateKey(private_key); | 390 GetOpenSSLSystemHandleForPrivateKey(private_key); |
| 356 if (sys_pkey != NULL) { | 391 if (sys_pkey != NULL) { |
| 357 CRYPTO_add(&sys_pkey->references, 1, CRYPTO_LOCK_EVP_PKEY); | 392 if (sys_pkey->type != ANDROID_EVP_PKEY_RSA) { |
| 358 LeakRsaEngine(sys_pkey); | 393 LOG(ERROR) << "Private key has wrong type!"; |
| 359 } else { | |
| 360 // GetOpenSSLSystemHandleForPrivateKey() will fail on Android | |
| 361 // 4.0.3 and earlier. However, it is possible to get the key | |
| 362 // content with PrivateKey.getEncoded() on these platforms. | |
| 363 // Note that this method may return NULL on 4.0.4 and later. | |
| 364 std::vector<uint8> encoded; | |
| 365 if (!GetPrivateKeyEncodedBytes(private_key, &encoded)) { | |
| 366 LOG(ERROR) << "Can't get private key data!"; | |
| 367 return NULL; | 394 return NULL; |
| 368 } | 395 } |
| 369 const unsigned char* p = | 396 |
| 370 reinterpret_cast<const unsigned char*>(&encoded[0]); | 397 AndroidRSA* sys_rsa = sys_pkey->pkey.rsa; |
| 371 int len = static_cast<int>(encoded.size()); | 398 if (sys_rsa->engine) { |
| 372 sys_pkey = d2i_AutoPrivateKey(NULL, &p, len); | 399 // |private_key| may not have an engine if the PrivateKey did not come |
| 373 if (sys_pkey == NULL) { | 400 // from the key store, such as in unit tests. |
| 374 LOG(ERROR) << "Can't convert private key data!"; | 401 if (!strcmp(sys_rsa->engine->id, "keystore")) { |
| 402 LeakEngine(private_key); |
| 403 } else { |
| 404 NOTREACHED(); |
| 405 } |
| 406 } |
| 407 |
| 408 crypto::ScopedEVP_PKEY pkey(EVP_PKEY_new()); |
| 409 if (!GetRsaPkeyWrapper(private_key, sys_rsa, pkey.get())) |
| 375 return NULL; | 410 return NULL; |
| 376 } | 411 return pkey.release(); |
| 377 } | 412 } |
| 378 return sys_pkey; | 413 |
| 414 // GetOpenSSLSystemHandleForPrivateKey() will fail on Android 4.0.3 and |
| 415 // earlier. However, it is possible to get the key content with |
| 416 // PrivateKey.getEncoded() on these platforms. Note that this method may |
| 417 // return NULL on 4.0.4 and later. |
| 418 std::vector<uint8> encoded; |
| 419 if (!GetPrivateKeyEncodedBytes(private_key, &encoded)) { |
| 420 LOG(ERROR) << "Can't get private key data!"; |
| 421 return NULL; |
| 422 } |
| 423 const unsigned char* p = |
| 424 reinterpret_cast<const unsigned char*>(&encoded[0]); |
| 425 int len = static_cast<int>(encoded.size()); |
| 426 EVP_PKEY* pkey = d2i_AutoPrivateKey(NULL, &p, len); |
| 427 if (pkey == NULL) { |
| 428 LOG(ERROR) << "Can't convert private key data!"; |
| 429 return NULL; |
| 430 } |
| 431 return pkey; |
| 379 } | 432 } |
| 380 | 433 |
| 381 // Custom DSA_METHOD that uses the platform APIs. | 434 // Custom DSA_METHOD that uses the platform APIs. |
| 382 // Note that for now, only signing through DSA_sign() is really supported. | 435 // Note that for now, only signing through DSA_sign() is really supported. |
| 383 // all other method pointers are either stubs returning errors, or no-ops. | 436 // all other method pointers are either stubs returning errors, or no-ops. |
| 384 // See <openssl/dsa.h> for exact declaration of DSA_METHOD. | 437 // See <openssl/dsa.h> for exact declaration of DSA_METHOD. |
| 385 // | 438 // |
| 386 // Note: There is no DSA_set_app_data() and DSA_get_app_data() functions, | 439 // Note: There is no DSA_set_app_data() and DSA_get_app_data() functions, |
| 387 // but RSA_set_app_data() is defined as a simple macro that calls | 440 // but RSA_set_app_data() is defined as a simple macro that calls |
| 388 // RSA_set_ex_data() with a hard-coded index of 0, so this code | 441 // RSA_set_ex_data() with a hard-coded index of 0, so this code |
| (...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 700 // backing this PrivateKey object. | 753 // backing this PrivateKey object. |
| 701 const int kAndroid42ApiLevel = 17; | 754 const int kAndroid42ApiLevel = 17; |
| 702 if (base::android::BuildInfo::GetInstance()->sdk_int() < | 755 if (base::android::BuildInfo::GetInstance()->sdk_int() < |
| 703 kAndroid42ApiLevel) { | 756 kAndroid42ApiLevel) { |
| 704 EVP_PKEY* legacy_key = GetRsaLegacyKey(private_key); | 757 EVP_PKEY* legacy_key = GetRsaLegacyKey(private_key); |
| 705 if (legacy_key == NULL) | 758 if (legacy_key == NULL) |
| 706 return NULL; | 759 return NULL; |
| 707 pkey.reset(legacy_key); | 760 pkey.reset(legacy_key); |
| 708 } else { | 761 } else { |
| 709 // Running on Android 4.2. | 762 // Running on Android 4.2. |
| 710 if (!GetRsaPkeyWrapper(private_key, pkey.get())) | 763 if (!GetRsaPkeyWrapper(private_key, NULL, pkey.get())) |
| 711 return NULL; | 764 return NULL; |
| 712 } | 765 } |
| 713 } | 766 } |
| 714 break; | 767 break; |
| 715 case PRIVATE_KEY_TYPE_DSA: | 768 case PRIVATE_KEY_TYPE_DSA: |
| 716 if (!GetDsaPkeyWrapper(private_key, pkey.get())) | 769 if (!GetDsaPkeyWrapper(private_key, pkey.get())) |
| 717 return NULL; | 770 return NULL; |
| 718 break; | 771 break; |
| 719 case PRIVATE_KEY_TYPE_ECDSA: | 772 case PRIVATE_KEY_TYPE_ECDSA: |
| 720 if (!GetEcdsaPkeyWrapper(private_key, pkey.get())) | 773 if (!GetEcdsaPkeyWrapper(private_key, pkey.get())) |
| 721 return NULL; | 774 return NULL; |
| 722 break; | 775 break; |
| 723 default: | 776 default: |
| 724 LOG(WARNING) | 777 LOG(WARNING) |
| 725 << "GetOpenSSLPrivateKeyWrapper() called with invalid key type"; | 778 << "GetOpenSSLPrivateKeyWrapper() called with invalid key type"; |
| 726 return NULL; | 779 return NULL; |
| 727 } | 780 } |
| 728 return pkey.release(); | 781 return pkey.release(); |
| 729 } | 782 } |
| 730 | 783 |
| 731 } // namespace android | 784 } // namespace android |
| 732 } // namespace net | 785 } // namespace net |
| OLD | NEW |