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 #include <openssl/dsa.h> | 9 #include <openssl/dsa.h> |
10 #include <openssl/ec.h> | 10 #include <openssl/ec.h> |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 int ExDataDup(CRYPTO_EX_DATA* to, | 88 int ExDataDup(CRYPTO_EX_DATA* to, |
89 const CRYPTO_EX_DATA* from, | 89 const CRYPTO_EX_DATA* from, |
90 void** from_d, | 90 void** from_d, |
91 int index, | 91 int index, |
92 long argl, | 92 long argl, |
93 void* argp) { | 93 void* argp) { |
94 CHECK_EQ((void*)NULL, *from_d); | 94 CHECK_EQ((void*)NULL, *from_d); |
95 return 0; | 95 return 0; |
96 } | 96 } |
97 | 97 |
98 // ExDataFree is called when one of the RSA, DSA or EC_KEY object is freed. | 98 // ExDataFree is called when one of the RSA, DSA or EC_KEY objects is freed. |
99 void ExDataFree(void* parent, | 99 void ExDataFree(void* parent, |
100 void* ptr, | 100 void* ptr, |
101 CRYPTO_EX_DATA* ad, | 101 CRYPTO_EX_DATA* ad, |
102 int index, | 102 int index, |
103 long argl, | 103 long argl, |
104 void* argp) { | 104 void* argp) { |
105 // Ensure the global JNI reference created with this wrapper is | 105 // Ensure the global JNI reference created with this wrapper is |
106 // properly destroyed with it. | 106 // properly destroyed with it. |
107 KeyExData *ex_data = reinterpret_cast<KeyExData*>(ptr); | 107 KeyExData *ex_data = reinterpret_cast<KeyExData*>(ptr); |
108 if (ex_data != NULL) { | 108 if (ex_data != NULL) { |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 // the same Android version as the "NONEwithRSA" | 197 // the same Android version as the "NONEwithRSA" |
198 // java.security.Signature algorithm, so the same version checks | 198 // java.security.Signature algorithm, so the same version checks |
199 // for GetRsaLegacyKey should work. | 199 // for GetRsaLegacyKey should work. |
200 OPENSSL_PUT_ERROR(RSA, sign_raw, RSA_R_UNKNOWN_PADDING_TYPE); | 200 OPENSSL_PUT_ERROR(RSA, sign_raw, RSA_R_UNKNOWN_PADDING_TYPE); |
201 return 0; | 201 return 0; |
202 } | 202 } |
203 | 203 |
204 // Retrieve private key JNI reference. | 204 // Retrieve private key JNI reference. |
205 const KeyExData *ex_data = RsaGetExData(rsa); | 205 const KeyExData *ex_data = RsaGetExData(rsa); |
206 if (!ex_data || !ex_data->private_key) { | 206 if (!ex_data || !ex_data->private_key) { |
207 LOG(WARNING) << "Null JNI reference passed to RsaMethodPrivEnc!"; | 207 LOG(WARNING) << "Null JNI reference passed to RsaMethodSignRaw!"; |
208 OPENSSL_PUT_ERROR(RSA, sign_raw, ERR_R_INTERNAL_ERROR); | 208 OPENSSL_PUT_ERROR(RSA, sign_raw, ERR_R_INTERNAL_ERROR); |
209 return 0; | 209 return 0; |
210 } | 210 } |
211 | 211 |
212 // Pre-4.2 legacy codepath. | 212 // Pre-4.2 legacy codepath. |
213 if (ex_data->legacy_rsa) { | 213 if (ex_data->legacy_rsa) { |
214 int ret = ex_data->legacy_rsa->meth->rsa_priv_enc( | 214 int ret = ex_data->legacy_rsa->meth->rsa_priv_enc( |
215 in_len, in, out, ex_data->legacy_rsa, ANDROID_RSA_PKCS1_PADDING); | 215 in_len, in, out, ex_data->legacy_rsa, ANDROID_RSA_PKCS1_PADDING); |
216 if (ret < 0) { | 216 if (ret < 0) { |
217 LOG(WARNING) << "Could not sign message in RsaMethodPrivEnc!"; | 217 LOG(WARNING) << "Could not sign message in RsaMethodSignRaw!"; |
218 // System OpenSSL will use a separate error queue, so it is still | 218 // System OpenSSL will use a separate error queue, so it is still |
219 // necessary to push a new error. | 219 // necessary to push a new error. |
220 // | 220 // |
221 // TODO(davidben): It would be good to also clear the system error queue | 221 // TODO(davidben): It would be good to also clear the system error queue |
222 // if there were some way to convince Java to do it. (Without going | 222 // if there were some way to convince Java to do it. (Without going |
223 // through Java, it's difficult to get a handle on a system OpenSSL | 223 // through Java, it's difficult to get a handle on a system OpenSSL |
224 // function; dlopen loads a second copy.) | 224 // function; dlopen loads a second copy.) |
225 OPENSSL_PUT_ERROR(RSA, sign_raw, ERR_R_INTERNAL_ERROR); | 225 OPENSSL_PUT_ERROR(RSA, sign_raw, ERR_R_INTERNAL_ERROR); |
226 return 0; | 226 return 0; |
227 } | 227 } |
228 *out_len = ret; | 228 *out_len = ret; |
229 return 1; | 229 return 1; |
230 } | 230 } |
231 | 231 |
232 base::StringPiece from_piece(reinterpret_cast<const char*>(in), in_len); | 232 base::StringPiece from_piece(reinterpret_cast<const char*>(in), in_len); |
233 std::vector<uint8> result; | 233 std::vector<uint8> result; |
234 // For RSA keys, this function behaves as RSA_private_encrypt with | 234 // For RSA keys, this function behaves as RSA_private_encrypt with |
235 // PKCS#1 padding. | 235 // PKCS#1 padding. |
236 if (!RawSignDigestWithPrivateKey(ex_data->private_key, from_piece, &result)) { | 236 if (!RawSignDigestWithPrivateKey(ex_data->private_key, from_piece, &result)) { |
237 LOG(WARNING) << "Could not sign message in RsaMethodPrivEnc!"; | 237 LOG(WARNING) << "Could not sign message in RsaMethodSignRaw!"; |
238 OPENSSL_PUT_ERROR(RSA, sign_raw, ERR_R_INTERNAL_ERROR); | 238 OPENSSL_PUT_ERROR(RSA, sign_raw, ERR_R_INTERNAL_ERROR); |
239 return 0; | 239 return 0; |
240 } | 240 } |
241 | 241 |
242 size_t expected_size = static_cast<size_t>(RSA_size(rsa)); | 242 size_t expected_size = static_cast<size_t>(RSA_size(rsa)); |
243 if (result.size() > expected_size) { | 243 if (result.size() > expected_size) { |
244 LOG(ERROR) << "RSA Signature size mismatch, actual: " | 244 LOG(ERROR) << "RSA Signature size mismatch, actual: " |
245 << result.size() << ", expected <= " << expected_size; | 245 << result.size() << ", expected <= " << expected_size; |
246 OPENSSL_PUT_ERROR(RSA, sign_raw, ERR_R_INTERNAL_ERROR); | 246 OPENSSL_PUT_ERROR(RSA, sign_raw, ERR_R_INTERNAL_ERROR); |
247 return 0; | 247 return 0; |
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
574 return GetEcdsaPkeyWrapper(private_key); | 574 return GetEcdsaPkeyWrapper(private_key); |
575 default: | 575 default: |
576 LOG(WARNING) | 576 LOG(WARNING) |
577 << "GetOpenSSLPrivateKeyWrapper() called with invalid key type"; | 577 << "GetOpenSSLPrivateKeyWrapper() called with invalid key type"; |
578 return crypto::ScopedEVP_PKEY(); | 578 return crypto::ScopedEVP_PKEY(); |
579 } | 579 } |
580 } | 580 } |
581 | 581 |
582 } // namespace android | 582 } // namespace android |
583 } // namespace net | 583 } // namespace net |
OLD | NEW |