OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "net/android/keystore_openssl.h" | |
6 | |
7 #include <jni.h> | |
8 #include <openssl/bn.h> | |
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 | |
11 // not be a concern for Chromium which always links against its own | |
12 // version of the library on Android. | |
13 #include <openssl/crypto/ecdsa/ecs_locl.h> | |
14 // And this one is needed for the EC_GROUP definition. | |
15 #include <openssl/crypto/ec/ec_lcl.h> | |
16 #include <openssl/dsa.h> | |
17 #include <openssl/ec.h> | |
18 #include <openssl/engine.h> | |
19 #include <openssl/evp.h> | |
20 #include <openssl/rsa.h> | |
21 | |
22 #include <pthread.h> | |
23 | |
24 #include "base/android/build_info.h" | |
25 #include "base/android/jni_android.h" | |
26 #include "base/android/scoped_java_ref.h" | |
27 #include "base/basictypes.h" | |
28 #include "base/logging.h" | |
29 #include "crypto/openssl_util.h" | |
30 #include "net/android/keystore.h" | |
31 #include "net/base/ssl_client_cert_type.h" | |
32 | |
33 // IMPORTANT NOTE: The following code will currently only work when used | |
34 // to implement client certificate support with OpenSSL. That's because | |
35 // only signing is implemented here. | |
36 // | |
37 | |
38 // The OpenSSL EVP_PKEY type is a generic wrapper around key pairs. | |
39 // Internally, it can hold a pointer to a RSA, DSA or ECDSA structure, | |
40 // which model keypair implementations of each respective crypto | |
41 // algorithm. | |
42 // | |
43 // The RSA type has a 'method' field pointer to a vtable-like structure | |
44 // called a RSA_METHOD. This contains several function pointers that | |
45 // correspond to operations on RSA keys (e.g. decode/encode with public | |
46 // key, decode/encode with private key, signing, validation), as well as | |
47 // a few flags. | |
48 // | |
49 // For example, the RSA_sign() function will call "method->rsa_sign()" if | |
50 // method->rsa_sign is not NULL, otherwise, it will perform a regular | |
51 // signing operation using the other fields in the RSA structure (which | |
52 // are used to hold the typical modulus / exponent / parameters for the | |
53 // key pair). | |
54 // | |
55 // This source file thus defines a custom RSA_METHOD structure, which | |
56 // fields points to static methods used to implement the corresponding | |
57 // RSA operation using platform Android APIs. | |
58 // | |
59 // However, the platform APIs require a jobject JNI reference to work. | |
60 // It must be stored in the RSA instance, or made accessible when the | |
61 // custom RSA methods are called. This is done by using RSA_set_app_data() | |
62 // and RSA_get_app_data(). | |
63 // | |
64 // One can thus _directly_ create a new EVP_PKEY that uses a custom RSA | |
65 // object with the following: | |
66 // | |
67 // RSA* rsa = RSA_new() | |
68 // RSA_set_method(&custom_rsa_method); | |
69 // RSA_set_app_data(rsa, jni_private_key); | |
70 // | |
71 // EVP_PKEY* pkey = EVP_PKEY_new(); | |
72 // EVP_PKEY_assign_RSA(pkey, rsa); | |
73 // | |
74 // Note that because EVP_PKEY_assign_RSA() is used, instead of | |
75 // EVP_PKEY_set1_RSA(), the new EVP_PKEY now owns the RSA object, and | |
76 // will destroy it when it is itself destroyed. | |
77 // | |
78 // Similarly, custom DSA_METHOD and ECDSA_METHOD are defined by this source | |
79 // file. | |
80 // | |
81 // Note that there is no need to define an OpenSSL ENGINE here. These | |
82 // are objects that can be used to expose custom methods (i.e. either | |
83 // RSA_METHOD, DSA_METHOD, ECDSA_METHOD, and a large number of other ones | |
84 // for types not related to this source file), and make them used by | |
85 // default for a lot of operations. Very fortunately, this is not needed | |
86 // here, which saves a lot of complexity. | |
87 | |
88 using base::android::ScopedJavaGlobalRef; | |
89 | |
90 namespace { | |
91 | |
92 typedef crypto::ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> ScopedEVP_PKEY; | |
93 typedef crypto::ScopedOpenSSL<RSA, RSA_free> ScopedRSA; | |
94 typedef crypto::ScopedOpenSSL<DSA, DSA_free> ScopedDSA; | |
95 typedef crypto::ScopedOpenSSL<EC_KEY, EC_KEY_free> ScopedEC_KEY; | |
96 typedef crypto::ScopedOpenSSL<EC_GROUP, EC_GROUP_free> ScopedEC_GROUP; | |
97 | |
98 /////////////////////////////////////////////////////////////////////////// | |
99 // | |
100 // Custom RSA_METHOD that uses the platform APIs. | |
101 // Note that for now, only signing through RSA_sign() is really supported. | |
102 // all other method pointers are either stubs returning errors, or no-ops. | |
103 // | |
104 // See <openssl/rsa.h> for exact declaration of RSA_METHOD. | |
105 // | |
106 | |
107 int RsaMethodPubEnc(int flen, | |
108 const unsigned char* from, | |
109 unsigned char* to, | |
110 RSA* rsa, | |
111 int padding) { | |
112 NOTIMPLEMENTED(); | |
113 RSAerr(RSA_F_RSA_PUBLIC_ENCRYPT, RSA_R_RSA_OPERATIONS_NOT_SUPPORTED); | |
114 return -1; | |
115 } | |
116 | |
117 int RsaMethodPubDec(int flen, | |
118 const unsigned char* from, | |
119 unsigned char* to, | |
120 RSA* rsa, | |
121 int padding) { | |
122 NOTIMPLEMENTED(); | |
123 RSAerr(RSA_F_RSA_PUBLIC_DECRYPT, RSA_R_RSA_OPERATIONS_NOT_SUPPORTED); | |
124 return -1; | |
125 } | |
126 | |
127 int RsaMethodPrivEnc(int flen, | |
128 const unsigned char *from, | |
129 unsigned char *to, | |
130 RSA *rsa, | |
131 int padding) { | |
132 NOTIMPLEMENTED(); | |
133 RSAerr(RSA_F_RSA_PRIVATE_ENCRYPT, RSA_R_RSA_OPERATIONS_NOT_SUPPORTED); | |
134 return -1; | |
135 } | |
136 | |
137 int RsaMethodPrivDec(int flen, | |
138 const unsigned char* from, | |
139 unsigned char* to, | |
140 RSA* rsa, | |
141 int padding) { | |
142 NOTIMPLEMENTED(); | |
143 RSAerr(RSA_F_RSA_PRIVATE_DECRYPT, RSA_R_RSA_OPERATIONS_NOT_SUPPORTED); | |
144 return -1; | |
145 } | |
146 | |
147 int RsaMethodInit(RSA* rsa) { | |
148 // Required to ensure that RsaMethodSign will be called. | |
149 rsa->flags |= RSA_FLAG_SIGN_VER; | |
150 return 0; | |
151 } | |
152 | |
153 int RsaMethodFinish(RSA* rsa) { | |
154 // Ensure the global JNI reference is destroyed with this key. | |
155 jobject key = reinterpret_cast<jobject>(RSA_get_app_data(rsa)); | |
156 if (key != NULL) { | |
157 RSA_set_app_data(rsa, NULL); | |
158 JNIEnv* env = base::android::AttachCurrentThread(); | |
159 env->DeleteGlobalRef(key); | |
160 } | |
161 // Actual return value is ignored by OpenSSL. There are no docs | |
162 // explaining what this is supposed to be. | |
163 return 0; | |
164 } | |
165 | |
166 int RsaMethodSign(int type, | |
167 const unsigned char* message, | |
168 unsigned int message_len, | |
169 unsigned char* signature, | |
170 unsigned int* signature_len, | |
171 const RSA* rsa) { | |
172 // This is only used for client certificate support, which | |
173 // will always pass the NID_md5_sha1 |type| value. | |
174 DCHECK_EQ(NID_md5_sha1, type); | |
175 if (type != NID_md5_sha1) { | |
176 RSAerr(RSA_F_RSA_SIGN, RSA_R_UNKNOWN_ALGORITHM_TYPE); | |
177 return 0; | |
178 } | |
179 // Retrieve private key JNI reference. | |
180 jobject private_key = reinterpret_cast<jobject>(RSA_get_app_data(rsa)); | |
181 if (!private_key) { | |
182 LOG(WARNING) << "Null JNI reference passed to RsaMethodSign!"; | |
183 return 0; | |
184 } | |
185 // Sign message with it through JNI. | |
186 base::StringPiece message_piece(reinterpret_cast<const char*>(message), | |
187 static_cast<size_t>(message_len)); | |
188 std::vector<uint8> result; | |
189 | |
190 if (!net::android::RawSignDigestWithPrivateKey( | |
191 private_key, message_piece, &result)) { | |
192 LOG(WARNING) << "Could not sign message in RsaMethodSign!"; | |
193 return 0; | |
194 } | |
195 | |
196 size_t expected_size = static_cast<size_t>(RSA_size(rsa)); | |
197 if (expected_size != result.size()) { | |
198 LOG(ERROR) << "RSA Signature size mismatch, actual: " << | |
199 result.size() << ", expected " << expected_size; | |
200 return 0; | |
201 } | |
202 | |
203 // Copy result to OpenSSL-provided buffer | |
204 memcpy(signature, &result[0], result.size()); | |
205 *signature_len = static_cast<unsigned int>(result.size()); | |
206 return 1; | |
207 } | |
208 | |
209 const RSA_METHOD android_rsa_method = { | |
210 /* .name = */ "Android signing-only RSA method", | |
211 /* .rsa_pub_enc = */ RsaMethodPubEnc, | |
212 /* .rsa_pub_dec = */ RsaMethodPubDec, | |
213 /* .rsa_priv_enc = */ RsaMethodPrivEnc, | |
214 /* .rsa_priv_dec = */ RsaMethodPrivDec, | |
215 /* .rsa_mod_exp = */ NULL, | |
216 /* .bn_mod_exp = */ NULL, | |
217 /* .init = */ RsaMethodInit, | |
218 /* .finish = */ RsaMethodFinish, | |
219 /* .flags = */ RSA_FLAG_SIGN_VER, // indicates that rsa_sign is usable. | |
220 /* .app_data = */ NULL, | |
221 /* .rsa_sign = */ RsaMethodSign, | |
222 /* .rsa_verify = */ NULL, | |
223 /* .rsa_keygen = */ NULL, | |
224 }; | |
225 | |
226 // Copy the contents of an encoded big integer into an existing BIGNUM. | |
227 // |num| is the BIGNUM object which will be assigned with the new value. | |
228 // |new_bytes| is the byte encoding of the new value. | |
229 // Returns true on success, false otherwise. On failure, |*pnum| is | |
230 // not modified. | |
231 bool CopyBigNumFromBytes(BIGNUM& num, | |
Ryan Sleevi
2013/02/01 21:50:58
STYLE: Passing non-const references like this is n
digit1
2013/02/01 22:50:17
ok, I've fixed that. Thanks for the pointer.
| |
232 const std::vector<uint8>& new_bytes) { | |
233 BIGNUM* new_num = BN_bin2bn( | |
234 reinterpret_cast<const unsigned char*>(&new_bytes[0]), | |
235 static_cast<int>(new_bytes.size()), | |
236 NULL); | |
237 if (new_num == NULL) | |
238 return false; | |
239 | |
240 BN_copy(&num, new_num); | |
241 BN_free(new_num); | |
242 return true; | |
243 } | |
244 | |
245 // Swap the contents a BIGNUM pointer with the value corresponding to | |
246 // an encoded big integer. | |
247 // |num| is a reference to a BIGNUM pointer. | |
248 // |new_bytes| is the byte encoding of the new value. | |
249 // Returns true on success, false otherwise. On failure, |*num| is | |
250 // not modified. | |
251 bool SwapBigNumPtrFromBytes(BIGNUM*& num, | |
252 const std::vector<uint8>& new_bytes) { | |
Ryan Sleevi
2013/02/01 21:50:58
Here, both reference & indent
digit1
2013/02/01 22:50:17
Done.
| |
253 BIGNUM* old_num = num; | |
254 BIGNUM* new_num = BN_bin2bn( | |
255 reinterpret_cast<const unsigned char*>(&new_bytes[0]), | |
256 static_cast<int>(new_bytes.size()), | |
257 NULL); | |
258 if (new_num == NULL) | |
259 return false; | |
260 | |
261 num = new_num; | |
262 BN_free(old_num); | |
263 return true; | |
264 } | |
265 | |
266 // This method only works on Android >= 4.2. | |
267 void GetRsaPkeyWrapper(ScopedEVP_PKEY& pkey, | |
268 ScopedJavaGlobalRef<jobject>& private_key) { | |
269 ScopedRSA rsa(RSA_new()); | |
270 RSA_set_method(rsa.get(), &android_rsa_method); | |
271 | |
272 // HACK: RSA_size() doesn't work with custom RSA_METHODs. To ensure that | |
273 // it will return the right value, set the 'n' field of the RSA object | |
274 // to match the private key's modulus. | |
275 // If this fails, simply exit, since this will let pkey.get() | |
276 // unchanged, it will stay at NULL. | |
277 std::vector<uint8> modulus; | |
278 if (!net::android::GetRSAKeyModulus(private_key.obj(), &modulus)) { | |
279 LOG(ERROR) << "Failed to get private key modulus"; | |
280 return; | |
281 } | |
282 if (!SwapBigNumPtrFromBytes(rsa.get()->n, modulus)) { | |
283 LOG(ERROR) << "Failed to decode private key modululs"; | |
284 return; | |
285 } | |
286 | |
287 RSA_set_app_data(rsa.get(), private_key.Release()); | |
288 EVP_PKEY_assign_RSA(pkey.get(), rsa.release()); | |
289 } | |
290 | |
291 // This method can be used on Android < 4.2 instead. | |
292 // It first tries to get the system OpenSSL pointer directly. | |
293 void GetRsaLegacyKey(ScopedEVP_PKEY& pkey, | |
294 ScopedJavaGlobalRef<jobject>& private_key) { | |
295 EVP_PKEY* sys_pkey = | |
296 net::android::GetOpenSSLSystemHandleForPrivateKey(private_key.obj()); | |
297 if (sys_pkey != NULL) { | |
298 CRYPTO_add(&sys_pkey->references, 1, CRYPTO_LOCK_EVP_PKEY); | |
299 } else { | |
300 // GetOpenSSLSystemHandleForPrivateKey() will fail on Android | |
301 // 4.0.3 and earlier. However, it is possible to get the key | |
302 // content with PrivateKey.getEncoded() on these platforms. | |
303 // Note that this method may return NULL on 4.0.4 and later. | |
304 std::vector<uint8> encoded; | |
305 if (!net::android::GetPrivateKeyEncodedBytes( | |
306 private_key.obj(), &encoded)) { | |
307 LOG(ERROR) << "Can't get private key data!"; | |
308 return; | |
309 } | |
310 const unsigned char* p = | |
311 reinterpret_cast<const unsigned char*>(&encoded[0]); | |
312 int len = static_cast<int>(encoded.size()); | |
313 sys_pkey = d2i_AutoPrivateKey(NULL, &p, len); | |
314 if (sys_pkey == NULL) { | |
315 LOG(ERROR) << "Can't convert private key data!"; | |
316 return; | |
317 } | |
318 } | |
319 pkey.reset(sys_pkey); | |
320 } | |
321 | |
322 /////////////////////////////////////////////////////////////////////////// | |
323 // | |
324 // Custom DSA_METHOD that uses the platform APIs. | |
325 // Note that for now, only signing through DSA_sign() is really supported. | |
326 // all other method pointers are either stubs returning errors, or no-ops. | |
327 // | |
328 // See <openssl/dsa.h> for exact declaration of DSA_METHOD. | |
329 // | |
330 // Note: There is no DSA_set_app_data() and DSA_get_app_data() functions, | |
331 // but RSA_set_app_data() is defined as a simple macro that calls | |
332 // RSA_set_ex_data() with a hard-coded index of 0, so this code | |
333 // does the same thing here. | |
334 | |
335 static DSA_SIG* DsaMethodDoSign(const unsigned char* dgst, | |
336 int dlen, | |
337 DSA* dsa) { | |
338 // Extract the JNI reference to the PrivateKey object. | |
339 jobject private_key = reinterpret_cast<jobject>(DSA_get_ex_data(dsa, 0)); | |
340 if (private_key == NULL) | |
341 return NULL; | |
342 | |
343 // Sign the message with it, calling platform APIs. | |
344 std::vector<uint8> signature; | |
345 if (!net::android::RawSignDigestWithPrivateKey( | |
346 private_key, | |
347 base::StringPiece( | |
348 reinterpret_cast<const char*>(dgst), | |
349 static_cast<size_t>(dlen)), | |
350 &signature)) { | |
351 return NULL; | |
352 } | |
353 | |
354 // Note: With DSA, the actual signature might be smaller than DSA_size(). | |
Ryan Sleevi
2013/02/01 21:50:58
Correct. It should be within +/- 2, IIRC (dependin
| |
355 size_t max_expected_size = static_cast<size_t>(DSA_size(dsa)); | |
356 if (signature.size() > max_expected_size) { | |
357 LOG(ERROR) << "DSA Signature size mismatch, actual: " << | |
358 signature.size() << ", expected <= " << max_expected_size; | |
359 return 0; | |
360 } | |
361 | |
362 // Convert the signature into a DSA_SIG object. | |
363 const unsigned char* sigbuf = | |
364 reinterpret_cast<const unsigned char*>(&signature[0]); | |
365 int siglen = static_cast<size_t>(signature.size()); | |
366 DSA_SIG* dsa_sig = d2i_DSA_SIG(NULL, &sigbuf, siglen); | |
367 return dsa_sig; | |
368 } | |
369 | |
370 static int DsaMethodSignSetup(DSA* dsa, | |
371 BN_CTX* ctx_in, | |
372 BIGNUM** kinvp, | |
373 BIGNUM** rp) { | |
374 NOTIMPLEMENTED(); | |
375 DSAerr(DSA_F_DSA_SIGN_SETUP, DSA_R_INVALID_DIGEST_TYPE); | |
376 return -1; | |
377 } | |
378 | |
379 static int DsaMethodDoVerify(const unsigned char* dgst, | |
380 int dgst_len, | |
381 DSA_SIG* sig, | |
382 DSA* dsa) { | |
383 NOTIMPLEMENTED(); | |
384 DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_INVALID_DIGEST_TYPE); | |
385 return -1; | |
386 } | |
387 | |
388 static int DsaMethodFinish(DSA* dsa) { | |
389 // Free the global JNI reference. | |
390 jobject key = reinterpret_cast<jobject>(DSA_get_ex_data(dsa,0)); | |
391 if (key != NULL) { | |
392 DSA_set_ex_data(dsa, 0, NULL); | |
393 JNIEnv* env = base::android::AttachCurrentThread(); | |
394 env->DeleteGlobalRef(key); | |
395 } | |
396 // Actual return value is ignored by OpenSSL. There are no docs | |
397 // explaining what this is supposed to be. | |
398 return 0; | |
399 } | |
400 | |
401 const DSA_METHOD android_dsa_method = { | |
402 /* .name = */ "Android signing-only DSA method", | |
403 /* .dsa_do_sign = */ DsaMethodDoSign, | |
404 /* .dsa_sign_setup = */ DsaMethodSignSetup, | |
405 /* .dsa_do_verify = */ DsaMethodDoVerify, | |
406 /* .dsa_mod_exp = */ NULL, | |
407 /* .bn_mod_exp = */ NULL, | |
408 /* .init = */ NULL, // nothing to do here. | |
409 /* .finish = */ DsaMethodFinish, | |
410 /* .flags = */ 0, | |
411 /* .app_data = */ NULL, | |
412 /* .dsa_paramgem = */ NULL, | |
413 /* .dsa_keygen = */ NULL | |
414 }; | |
415 | |
416 void GetDsaPkeyWrapper(ScopedEVP_PKEY& pkey, | |
417 ScopedJavaGlobalRef<jobject>& private_key) { | |
418 ScopedDSA dsa(DSA_new()); | |
419 DSA_set_method(dsa.get(), &android_dsa_method); | |
420 | |
421 // HACK: DSA_size() doesn't work with custom DSA_METHODs. To ensure it | |
422 // returns the right value, set the 'q' field in the DSA object to | |
423 // match the parameter from the platform key. | |
424 std::vector<uint8> q; | |
425 if (!net::android::GetDSAKeyParamQ(private_key.obj(), &q)) { | |
426 LOG(ERROR) << "Can't extract Q parameter from DSA private key"; | |
427 return; | |
428 } | |
429 if (!SwapBigNumPtrFromBytes(dsa.get()->q, q)) { | |
430 LOG(ERROR) << "Can't decode Q parameter from DSA private key"; | |
431 return; | |
432 } | |
433 | |
434 DSA_set_ex_data(dsa.get(), 0, private_key.Release()); | |
435 EVP_PKEY_assign_DSA(pkey.get(), dsa.release()); | |
436 } | |
437 | |
438 /////////////////////////////////////////////////////////////////////////// | |
439 // | |
440 // Custom ECDSA_METHOD that uses the platform APIs. | |
441 // Note that for now, only signing through ECDSA_sign() is really supported. | |
442 // all other method pointers are either stubs returning errors, or no-ops. | |
443 // | |
444 // Note: The ECDSA_METHOD structure doesn't have init/finish | |
445 // methods. As such, the only way to to ensure the global | |
446 // JNI reference is properly released when the EVP_PKEY is | |
447 // destroyed is to use a custom EX_DATA type. | |
448 | |
449 // Used to ensure that the global JNI reference associated with a | |
450 // custom EC_KEY + ECDSA_METHOD is released when the key is destroyed. | |
451 void ExDataFree(void* parent, | |
452 void* ptr, | |
453 CRYPTO_EX_DATA* ad, | |
454 int idx, | |
455 long argl, | |
456 void* argp) { | |
457 jobject private_key = reinterpret_cast<jobject>(ptr); | |
458 if (private_key == NULL) | |
459 return; | |
460 | |
461 CRYPTO_set_ex_data(ad, idx, NULL); | |
462 | |
463 JNIEnv* env = base::android::AttachCurrentThread(); | |
464 env->DeleteGlobalRef(private_key); | |
465 } | |
466 | |
467 int ExDataDup(CRYPTO_EX_DATA* to, | |
468 CRYPTO_EX_DATA* from, | |
469 void* from_d, | |
470 int idx, | |
471 long argl, | |
472 void* argp) { | |
473 // This callback shall never be called with the current OpenSSL | |
474 // implementation (the library only ever duplicates EX_DATA items | |
475 // for SSL and BIO objects). But provide this to catch regressions | |
476 // in the future. | |
477 NOTIMPLEMENTED(); | |
478 LOG(WARNING) << "ExDataDup for was called for ECDSA custom key !?"; | |
479 // Return value is currently ignored by OpenSSL. | |
480 return 0; | |
481 } | |
482 | |
483 int s_ecdsa_ex_data_index; | |
484 | |
485 void EcdsaInitExDataIndex(void) { | |
486 // Note that OpenSSL does not provide a way to unregister these | |
487 // indices, so don't worry about releasing them on program exit. | |
488 // There is also no need for an ExDataNew function. | |
489 s_ecdsa_ex_data_index = ECDSA_get_ex_new_index(0, // argl | |
490 NULL, // argp | |
491 NULL, // new_func | |
492 ExDataDup, // dup_func | |
493 ExDataFree); // free_func | |
494 } | |
495 | |
496 // Returns the index of the custom EX_DATA used to store the JNI reference. | |
497 int EcdsaGetExDataIndex(void) { | |
498 // The index must be initialized once per processs, and this function | |
499 // can be called from distinct threads. Use a pthread_once_t to perform | |
500 // proper lazy initialization. | |
501 static pthread_once_t once = PTHREAD_ONCE_INIT; | |
502 pthread_once(&once, EcdsaInitExDataIndex); | |
503 return s_ecdsa_ex_data_index; | |
504 } | |
505 | |
506 ECDSA_SIG* EcdsaMethodDoSign(const unsigned char* dgst, | |
507 int dgst_len, | |
508 const BIGNUM* inv, | |
509 const BIGNUM* rp, | |
510 EC_KEY* eckey) { | |
511 // Retrieve private key JNI reference. | |
512 jobject private_key = reinterpret_cast<jobject>( | |
513 ECDSA_get_ex_data(eckey, EcdsaGetExDataIndex())); | |
514 if (!private_key) { | |
515 LOG(WARNING) << "Null JNI reference passed to EcdsaMethodDoSign!"; | |
516 return NULL; | |
517 } | |
518 // Sign message with it through JNI. | |
519 std::vector<uint8> signature; | |
520 if (!net::android::RawSignDigestWithPrivateKey( | |
521 private_key, | |
522 base::StringPiece( | |
523 reinterpret_cast<const char*>(dgst), | |
524 static_cast<size_t>(dgst_len)), | |
525 &signature)) { | |
526 LOG(WARNING) << "Could not sign message in EcdsaMethodDoSign!"; | |
527 return NULL; | |
528 } | |
529 | |
530 // Note: With ECDSA, the actual signature may be smaller than | |
531 // ECDSA_size(). | |
532 size_t max_expected_size = static_cast<size_t>(ECDSA_size(eckey)); | |
533 if (signature.size() > max_expected_size) { | |
534 LOG(ERROR) << "ECDSA Signature size mismatch, actual: " << | |
535 signature.size() << ", expected <= " << max_expected_size; | |
536 return 0; | |
537 } | |
538 | |
539 // Convert signature to ECDSA_SIG object | |
540 const unsigned char* sigbuf = | |
541 reinterpret_cast<const unsigned char*>(&signature[0]); | |
542 long siglen = static_cast<long>(signature.size()); | |
543 return d2i_ECDSA_SIG(NULL, &sigbuf, siglen); | |
544 } | |
545 | |
546 int EcdsaMethodSignSetup(EC_KEY* eckey, | |
547 BN_CTX* ctx, | |
548 BIGNUM** kinv, | |
549 BIGNUM** r) { | |
550 NOTIMPLEMENTED(); | |
551 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ECDSA_R_ERR_EC_LIB); | |
552 return -1; | |
553 } | |
554 | |
555 int EcdsaMethodDoVerify(const unsigned char* dgst, | |
556 int dgst_len, | |
557 const ECDSA_SIG* sig, | |
558 EC_KEY* eckey) { | |
559 NOTIMPLEMENTED(); | |
560 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_ERR_EC_LIB); | |
561 return -1; | |
562 } | |
563 | |
564 const ECDSA_METHOD android_ecdsa_method = { | |
565 /* .name = */ "Android signing-only ECDSA method", | |
566 /* .ecdsa_do_sign = */ EcdsaMethodDoSign, | |
567 /* .ecdsa_sign_setup = */ EcdsaMethodSignSetup, | |
568 /* .ecdsa_do_verify = */ EcdsaMethodDoVerify, | |
569 /* .flags = */ 0, | |
570 /* .app_data = */ NULL, | |
571 }; | |
572 | |
573 void GetEcdsaPkeyWrapper(ScopedEVP_PKEY& pkey, | |
574 ScopedJavaGlobalRef<jobject>& private_key) { | |
575 ScopedEC_KEY eckey(EC_KEY_new()); | |
576 ECDSA_set_method(eckey.get(), &android_ecdsa_method); | |
577 | |
578 // To ensure that ECDSA_size() works properly, craft a custom EC_GROUP | |
579 // that has the same order than the private key. | |
580 // In case of error, return directly, since pkey will be left untouched | |
581 // (and still set to NULL). | |
582 { | |
583 std::vector<uint8> order; | |
584 if (!net::android::GetECKeyOrder(private_key.obj(), &order)) { | |
585 LOG(ERROR) << "Can't extract order parameter from EC private key"; | |
586 return; | |
587 } | |
588 ScopedEC_GROUP group(EC_GROUP_new(EC_GFp_nist_method())); | |
Ryan Sleevi
2013/02/01 21:50:58
Adam should double check this
| |
589 if (!group.get()) { | |
590 LOG(ERROR) << "Can't create new EC_GROUP"; | |
591 return; | |
592 } | |
593 if (!CopyBigNumFromBytes(group.get()->order, order)) { | |
594 LOG(ERROR) << "Can't decode order from PrivateKey"; | |
595 return; | |
596 } | |
597 EC_KEY_set_group(eckey.get(), group.release()); | |
598 } | |
599 | |
600 ECDSA_set_ex_data(eckey.get(), | |
601 EcdsaGetExDataIndex(), | |
602 private_key.Release()); | |
603 | |
604 EVP_PKEY_assign_EC_KEY(pkey.get(), eckey.release()); | |
605 } | |
606 | |
607 } // namespace | |
608 | |
609 | |
610 namespace net { | |
611 namespace android { | |
612 | |
613 EVP_PKEY* GetOpenSSLPrivateKeyWrapper(jobject private_key) { | |
614 // Create scoped JNI global reference from the private key. | |
615 // This ensure that it will be usable from any thread, not only | |
616 // from the caller's. | |
617 // | |
618 // Note: "ScopedJavaGlobalRef<jobject> ref(a_jobject)" doesn't | |
619 // compile. Route around this by creating an empty object first, | |
620 // then resetting it. | |
621 ScopedJavaGlobalRef<jobject> global_key; | |
622 global_key.Reset(NULL, private_key); | |
623 if (global_key.is_null()) | |
624 return NULL; | |
625 | |
626 // Create new empty EVP_PKEY instance. | |
627 ScopedEVP_PKEY pkey(EVP_PKEY_new()); | |
628 if (!pkey.get()) | |
629 return NULL; | |
630 | |
631 // Create sub key type, depending on private key's algorithm type. | |
632 PrivateKeyType key_type = GetPrivateKeyType(global_key.obj()); | |
633 switch (key_type) { | |
634 case PRIVATE_KEY_TYPE_RSA: | |
635 { | |
636 // Route around platform bug: if Android < 4.2, then | |
637 // base::android::RawSignDigestWithPrivateKey() cannot work, so | |
638 // instead, obtain a raw EVP_PKEY* to the system object | |
639 // backing this PrivateKey object. | |
640 const int kAndroid42ApiLevel = 17; | |
641 if (base::android::BuildInfo::GetInstance()->sdk_int() < | |
642 kAndroid42ApiLevel) { | |
643 GetRsaLegacyKey(pkey, global_key); | |
644 } else { | |
645 // Running on Android 4.2. | |
646 GetRsaPkeyWrapper(pkey, global_key); | |
647 } | |
648 } | |
649 break; | |
650 case PRIVATE_KEY_TYPE_DSA: | |
651 GetDsaPkeyWrapper(pkey, global_key); | |
652 break; | |
653 case PRIVATE_KEY_TYPE_ECDSA: | |
654 GetEcdsaPkeyWrapper(pkey, global_key); | |
655 break; | |
656 default: | |
657 LOG(WARNING) | |
658 << "GetOpenSSLPrivateKeyWrapper() called with invalid key type"; | |
659 return NULL; | |
660 } | |
661 | |
662 return pkey.release(); | |
663 } | |
664 | |
665 } // namespace android | |
666 } // namespace net | |
OLD | NEW |