Chromium Code Reviews| 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 // | |
|
Ryan Sleevi
2013/02/04 22:53:27
comment nit: Other types of signing may be useful
digit1
2013/02/05 14:31:58
OpenSSL provides many ways to sign messages, not a
| |
| 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 // | |
|
Ryan Sleevi
2013/02/04 22:53:27
comment nit: superflous pretty-formatting is disco
digit1
2013/02/05 14:31:58
Done.
| |
| 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()) { | |
|
Ryan Sleevi
2013/02/04 22:53:27
Are you sure != is correct? I think this should be
digit1
2013/02/05 14:31:58
Done.
| |
| 198 LOG(ERROR) << "RSA Signature size mismatch, actual: " << | |
| 199 result.size() << ", expected " << expected_size; | |
|
Ryan Sleevi
2013/02/04 22:53:27
See http://dev.chromium.org/developers/coding-styl
digit1
2013/02/05 14:31:58
Done.
| |
| 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 // This function modifies |*num| in-place. | |
| 228 // |num| points to the BIGNUM which will be assigned with the new value. | |
| 229 // |new_bytes| is the byte encoding of the new value. | |
| 230 // Returns true on success, false otherwise. On failure, |*num| is | |
| 231 // not modified. | |
| 232 bool CopyBigNumFromBytes(BIGNUM* num, | |
| 233 const std::vector<uint8>& new_bytes) { | |
|
Ryan Sleevi
2013/02/04 22:53:27
STYLE: These functions follow the wrong order, as
digit1
2013/02/05 14:31:58
Done.
| |
| 234 BIGNUM* new_num = BN_bin2bn( | |
| 235 reinterpret_cast<const unsigned char*>(&new_bytes[0]), | |
| 236 static_cast<int>(new_bytes.size()), | |
| 237 NULL); | |
| 238 if (new_num == NULL) | |
| 239 return false; | |
| 240 | |
| 241 BN_copy(num, new_num); | |
| 242 BN_free(new_num); | |
|
Ryan Sleevi
2013/02/04 22:53:27
Why not BN_swap?
Or even more, why not just the
digit1
2013/02/05 14:31:58
Yes, that seems preferrable. Thanks. Done.
| |
| 243 return true; | |
| 244 } | |
| 245 | |
| 246 // Swap the contents a BIGNUM pointer with the value corresponding to | |
| 247 // an encoded big integer. The difference with CopyBigNumFromBytes is | |
| 248 // that the former modifies the target BIGNUM object in place, while | |
| 249 // this function swaps pointer values instead. | |
| 250 // |num_ptr| is the address of a BIGNUM pointer. |*num_ptr| can be NULL. | |
| 251 // |new_bytes| is the byte encoding of the new value. | |
| 252 // Returns true on success, false otherwise. On failure, |*num_ptr| is | |
| 253 // not modified. | |
| 254 bool SwapBigNumPtrFromBytes(BIGNUM** num_ptr, | |
| 255 const std::vector<uint8>& new_bytes) { | |
| 256 BIGNUM* old_num = *num_ptr; | |
| 257 BIGNUM* new_num = BN_bin2bn( | |
| 258 reinterpret_cast<const unsigned char*>(&new_bytes[0]), | |
| 259 static_cast<int>(new_bytes.size()), | |
| 260 NULL); | |
| 261 if (new_num == NULL) | |
| 262 return false; | |
| 263 | |
| 264 *num_ptr = new_num; | |
| 265 BN_free(old_num); | |
| 266 return true; | |
|
Ryan Sleevi
2013/02/04 22:53:27
I'm nervous about the correctness of this, in the
digit1
2013/02/05 14:31:58
I've changed the code to use BN_bin2bn(...,old_num
| |
| 267 } | |
| 268 | |
| 269 // This method only works on Android >= 4.2. | |
| 270 void GetRsaPkeyWrapper(ScopedEVP_PKEY& pkey, | |
|
Ryan Sleevi
2013/02/04 22:53:27
STYLE: non-const references are no good
http://go
digit1
2013/02/05 14:31:58
Done.
| |
| 271 ScopedJavaGlobalRef<jobject>& private_key) { | |
| 272 ScopedRSA rsa(RSA_new()); | |
| 273 RSA_set_method(rsa.get(), &android_rsa_method); | |
| 274 | |
| 275 // HACK: RSA_size() doesn't work with custom RSA_METHODs. To ensure that | |
| 276 // it will return the right value, set the 'n' field of the RSA object | |
| 277 // to match the private key's modulus. | |
| 278 // If this fails, simply exit, since this will let pkey.get() | |
| 279 // unchanged, it will stay at NULL. | |
| 280 std::vector<uint8> modulus; | |
| 281 if (!net::android::GetRSAKeyModulus(private_key.obj(), &modulus)) { | |
| 282 LOG(ERROR) << "Failed to get private key modulus"; | |
| 283 return; | |
| 284 } | |
| 285 if (!SwapBigNumPtrFromBytes(&rsa.get()->n, modulus)) { | |
| 286 LOG(ERROR) << "Failed to decode private key modululs"; | |
| 287 return; | |
| 288 } | |
| 289 | |
| 290 RSA_set_app_data(rsa.get(), private_key.Release()); | |
| 291 EVP_PKEY_assign_RSA(pkey.get(), rsa.release()); | |
| 292 } | |
| 293 | |
| 294 // This method can be used on Android < 4.2 instead. | |
| 295 // It first tries to get the system OpenSSL pointer directly. | |
| 296 void GetRsaLegacyKey(ScopedEVP_PKEY& pkey, | |
| 297 ScopedJavaGlobalRef<jobject>& private_key) { | |
|
Ryan Sleevi
2013/02/04 22:53:27
STYLE: same here re: non-const refs.
digit1
2013/02/05 14:31:58
Done.
| |
| 298 EVP_PKEY* sys_pkey = | |
| 299 net::android::GetOpenSSLSystemHandleForPrivateKey(private_key.obj()); | |
| 300 if (sys_pkey != NULL) { | |
| 301 CRYPTO_add(&sys_pkey->references, 1, CRYPTO_LOCK_EVP_PKEY); | |
| 302 } else { | |
| 303 // GetOpenSSLSystemHandleForPrivateKey() will fail on Android | |
| 304 // 4.0.3 and earlier. However, it is possible to get the key | |
| 305 // content with PrivateKey.getEncoded() on these platforms. | |
| 306 // Note that this method may return NULL on 4.0.4 and later. | |
| 307 std::vector<uint8> encoded; | |
| 308 if (!net::android::GetPrivateKeyEncodedBytes( | |
| 309 private_key.obj(), &encoded)) { | |
|
Ryan Sleevi
2013/02/04 22:53:27
style: indent an additional four spaces.
digit1
2013/02/05 14:31:58
Done.
| |
| 310 LOG(ERROR) << "Can't get private key data!"; | |
| 311 return; | |
| 312 } | |
| 313 const unsigned char* p = | |
| 314 reinterpret_cast<const unsigned char*>(&encoded[0]); | |
| 315 int len = static_cast<int>(encoded.size()); | |
| 316 sys_pkey = d2i_AutoPrivateKey(NULL, &p, len); | |
| 317 if (sys_pkey == NULL) { | |
| 318 LOG(ERROR) << "Can't convert private key data!"; | |
| 319 return; | |
| 320 } | |
| 321 } | |
| 322 pkey.reset(sys_pkey); | |
| 323 } | |
| 324 | |
| 325 /////////////////////////////////////////////////////////////////////////// | |
| 326 // | |
| 327 // Custom DSA_METHOD that uses the platform APIs. | |
| 328 // Note that for now, only signing through DSA_sign() is really supported. | |
| 329 // all other method pointers are either stubs returning errors, or no-ops. | |
| 330 // | |
| 331 // See <openssl/dsa.h> for exact declaration of DSA_METHOD. | |
| 332 // | |
| 333 // Note: There is no DSA_set_app_data() and DSA_get_app_data() functions, | |
| 334 // but RSA_set_app_data() is defined as a simple macro that calls | |
| 335 // RSA_set_ex_data() with a hard-coded index of 0, so this code | |
| 336 // does the same thing here. | |
| 337 | |
| 338 static DSA_SIG* DsaMethodDoSign(const unsigned char* dgst, | |
| 339 int dlen, | |
| 340 DSA* dsa) { | |
| 341 // Extract the JNI reference to the PrivateKey object. | |
| 342 jobject private_key = reinterpret_cast<jobject>(DSA_get_ex_data(dsa, 0)); | |
| 343 if (private_key == NULL) | |
| 344 return NULL; | |
| 345 | |
| 346 // Sign the message with it, calling platform APIs. | |
| 347 std::vector<uint8> signature; | |
| 348 if (!net::android::RawSignDigestWithPrivateKey( | |
| 349 private_key, | |
| 350 base::StringPiece( | |
| 351 reinterpret_cast<const char*>(dgst), | |
| 352 static_cast<size_t>(dlen)), | |
| 353 &signature)) { | |
|
Ryan Sleevi
2013/02/04 22:53:27
style: indent an additional four spaces.
digit1
2013/02/05 14:31:58
Done.
| |
| 354 return NULL; | |
| 355 } | |
| 356 | |
| 357 // Note: With DSA, the actual signature might be smaller than DSA_size(). | |
| 358 size_t max_expected_size = static_cast<size_t>(DSA_size(dsa)); | |
| 359 if (signature.size() > max_expected_size) { | |
| 360 LOG(ERROR) << "DSA Signature size mismatch, actual: " << | |
| 361 signature.size() << ", expected <= " << max_expected_size; | |
|
Ryan Sleevi
2013/02/04 22:53:27
style: same comments as before regarding << wrappi
digit1
2013/02/05 14:31:58
Done.
| |
| 362 return 0; | |
|
Ryan Sleevi
2013/02/04 22:53:27
style: return NULL, not 0.
digit1
2013/02/05 14:31:58
Done.
| |
| 363 } | |
| 364 | |
| 365 // Convert the signature into a DSA_SIG object. | |
| 366 const unsigned char* sigbuf = | |
| 367 reinterpret_cast<const unsigned char*>(&signature[0]); | |
| 368 int siglen = static_cast<size_t>(signature.size()); | |
| 369 DSA_SIG* dsa_sig = d2i_DSA_SIG(NULL, &sigbuf, siglen); | |
| 370 return dsa_sig; | |
| 371 } | |
| 372 | |
| 373 static int DsaMethodSignSetup(DSA* dsa, | |
| 374 BN_CTX* ctx_in, | |
| 375 BIGNUM** kinvp, | |
| 376 BIGNUM** rp) { | |
| 377 NOTIMPLEMENTED(); | |
| 378 DSAerr(DSA_F_DSA_SIGN_SETUP, DSA_R_INVALID_DIGEST_TYPE); | |
| 379 return -1; | |
| 380 } | |
| 381 | |
| 382 static int DsaMethodDoVerify(const unsigned char* dgst, | |
| 383 int dgst_len, | |
| 384 DSA_SIG* sig, | |
| 385 DSA* dsa) { | |
| 386 NOTIMPLEMENTED(); | |
| 387 DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_INVALID_DIGEST_TYPE); | |
| 388 return -1; | |
| 389 } | |
| 390 | |
| 391 static int DsaMethodFinish(DSA* dsa) { | |
| 392 // Free the global JNI reference. | |
| 393 jobject key = reinterpret_cast<jobject>(DSA_get_ex_data(dsa,0)); | |
| 394 if (key != NULL) { | |
| 395 DSA_set_ex_data(dsa, 0, NULL); | |
| 396 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 397 env->DeleteGlobalRef(key); | |
| 398 } | |
| 399 // Actual return value is ignored by OpenSSL. There are no docs | |
| 400 // explaining what this is supposed to be. | |
| 401 return 0; | |
| 402 } | |
| 403 | |
| 404 const DSA_METHOD android_dsa_method = { | |
| 405 /* .name = */ "Android signing-only DSA method", | |
| 406 /* .dsa_do_sign = */ DsaMethodDoSign, | |
| 407 /* .dsa_sign_setup = */ DsaMethodSignSetup, | |
| 408 /* .dsa_do_verify = */ DsaMethodDoVerify, | |
| 409 /* .dsa_mod_exp = */ NULL, | |
| 410 /* .bn_mod_exp = */ NULL, | |
| 411 /* .init = */ NULL, // nothing to do here. | |
| 412 /* .finish = */ DsaMethodFinish, | |
| 413 /* .flags = */ 0, | |
| 414 /* .app_data = */ NULL, | |
| 415 /* .dsa_paramgem = */ NULL, | |
| 416 /* .dsa_keygen = */ NULL | |
| 417 }; | |
| 418 | |
| 419 void GetDsaPkeyWrapper(ScopedEVP_PKEY& pkey, | |
| 420 ScopedJavaGlobalRef<jobject>& private_key) { | |
| 421 ScopedDSA dsa(DSA_new()); | |
| 422 DSA_set_method(dsa.get(), &android_dsa_method); | |
| 423 | |
| 424 // HACK: DSA_size() doesn't work with custom DSA_METHODs. To ensure it | |
| 425 // returns the right value, set the 'q' field in the DSA object to | |
| 426 // match the parameter from the platform key. | |
| 427 std::vector<uint8> q; | |
| 428 if (!net::android::GetDSAKeyParamQ(private_key.obj(), &q)) { | |
| 429 LOG(ERROR) << "Can't extract Q parameter from DSA private key"; | |
| 430 return; | |
| 431 } | |
| 432 if (!SwapBigNumPtrFromBytes(&dsa.get()->q, q)) { | |
| 433 LOG(ERROR) << "Can't decode Q parameter from DSA private key"; | |
| 434 return; | |
| 435 } | |
| 436 | |
| 437 DSA_set_ex_data(dsa.get(), 0, private_key.Release()); | |
| 438 EVP_PKEY_assign_DSA(pkey.get(), dsa.release()); | |
| 439 } | |
| 440 | |
| 441 /////////////////////////////////////////////////////////////////////////// | |
| 442 // | |
| 443 // Custom ECDSA_METHOD that uses the platform APIs. | |
| 444 // Note that for now, only signing through ECDSA_sign() is really supported. | |
| 445 // all other method pointers are either stubs returning errors, or no-ops. | |
| 446 // | |
| 447 // Note: The ECDSA_METHOD structure doesn't have init/finish | |
| 448 // methods. As such, the only way to to ensure the global | |
| 449 // JNI reference is properly released when the EVP_PKEY is | |
| 450 // destroyed is to use a custom EX_DATA type. | |
| 451 | |
| 452 // Used to ensure that the global JNI reference associated with a | |
| 453 // custom EC_KEY + ECDSA_METHOD is released when the key is destroyed. | |
| 454 void ExDataFree(void* parent, | |
| 455 void* ptr, | |
| 456 CRYPTO_EX_DATA* ad, | |
| 457 int idx, | |
| 458 long argl, | |
| 459 void* argp) { | |
| 460 jobject private_key = reinterpret_cast<jobject>(ptr); | |
| 461 if (private_key == NULL) | |
| 462 return; | |
| 463 | |
| 464 CRYPTO_set_ex_data(ad, idx, NULL); | |
| 465 | |
| 466 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 467 env->DeleteGlobalRef(private_key); | |
| 468 } | |
| 469 | |
| 470 int ExDataDup(CRYPTO_EX_DATA* to, | |
| 471 CRYPTO_EX_DATA* from, | |
| 472 void* from_d, | |
| 473 int idx, | |
| 474 long argl, | |
| 475 void* argp) { | |
| 476 // This callback shall never be called with the current OpenSSL | |
| 477 // implementation (the library only ever duplicates EX_DATA items | |
| 478 // for SSL and BIO objects). But provide this to catch regressions | |
| 479 // in the future. | |
|
Ryan Sleevi
2013/02/04 22:53:27
This sort of subtlety leaves me afraid of a double
digit1
2013/02/05 14:31:58
Done.
| |
| 480 NOTIMPLEMENTED(); | |
| 481 LOG(WARNING) << "ExDataDup for was called for ECDSA custom key !?"; | |
| 482 // Return value is currently ignored by OpenSSL. | |
| 483 return 0; | |
| 484 } | |
| 485 | |
| 486 int s_ecdsa_ex_data_index; | |
| 487 | |
| 488 void EcdsaInitExDataIndex(void) { | |
| 489 // Note that OpenSSL does not provide a way to unregister these | |
| 490 // indices, so don't worry about releasing them on program exit. | |
| 491 // There is also no need for an ExDataNew function. | |
| 492 s_ecdsa_ex_data_index = ECDSA_get_ex_new_index(0, // argl | |
| 493 NULL, // argp | |
| 494 NULL, // new_func | |
| 495 ExDataDup, // dup_func | |
| 496 ExDataFree); // free_func | |
| 497 } | |
| 498 | |
| 499 // Returns the index of the custom EX_DATA used to store the JNI reference. | |
| 500 int EcdsaGetExDataIndex(void) { | |
| 501 // The index must be initialized once per processs, and this function | |
| 502 // can be called from distinct threads. Use a pthread_once_t to perform | |
| 503 // proper lazy initialization. | |
| 504 static pthread_once_t once = PTHREAD_ONCE_INIT; | |
| 505 pthread_once(&once, EcdsaInitExDataIndex); | |
| 506 return s_ecdsa_ex_data_index; | |
|
Ryan Sleevi
2013/02/04 22:53:27
So it's clear for general reference, I don't think
digit1
2013/02/05 14:31:58
I'm not sure I understand. Are you saying it's not
Ryan Sleevi
2013/02/06 22:48:02
I'm saying I don't know, but that I hadn't seen th
| |
| 507 } | |
| 508 | |
| 509 ECDSA_SIG* EcdsaMethodDoSign(const unsigned char* dgst, | |
| 510 int dgst_len, | |
| 511 const BIGNUM* inv, | |
| 512 const BIGNUM* rp, | |
| 513 EC_KEY* eckey) { | |
| 514 // Retrieve private key JNI reference. | |
| 515 jobject private_key = reinterpret_cast<jobject>( | |
| 516 ECDSA_get_ex_data(eckey, EcdsaGetExDataIndex())); | |
| 517 if (!private_key) { | |
| 518 LOG(WARNING) << "Null JNI reference passed to EcdsaMethodDoSign!"; | |
| 519 return NULL; | |
| 520 } | |
| 521 // Sign message with it through JNI. | |
| 522 std::vector<uint8> signature; | |
| 523 if (!net::android::RawSignDigestWithPrivateKey( | |
| 524 private_key, | |
| 525 base::StringPiece( | |
| 526 reinterpret_cast<const char*>(dgst), | |
| 527 static_cast<size_t>(dgst_len)), | |
| 528 &signature)) { | |
|
Ryan Sleevi
2013/02/04 22:53:27
style: indents are wrong
digit1
2013/02/05 14:31:58
Done.
| |
| 529 LOG(WARNING) << "Could not sign message in EcdsaMethodDoSign!"; | |
| 530 return NULL; | |
| 531 } | |
| 532 | |
| 533 // Note: With ECDSA, the actual signature may be smaller than | |
| 534 // ECDSA_size(). | |
| 535 size_t max_expected_size = static_cast<size_t>(ECDSA_size(eckey)); | |
| 536 if (signature.size() > max_expected_size) { | |
| 537 LOG(ERROR) << "ECDSA Signature size mismatch, actual: " << | |
| 538 signature.size() << ", expected <= " << max_expected_size; | |
|
Ryan Sleevi
2013/02/04 22:53:27
style: << is wrong
digit1
2013/02/05 14:31:58
Done.
| |
| 539 return 0; | |
| 540 } | |
| 541 | |
| 542 // Convert signature to ECDSA_SIG object | |
| 543 const unsigned char* sigbuf = | |
| 544 reinterpret_cast<const unsigned char*>(&signature[0]); | |
| 545 long siglen = static_cast<long>(signature.size()); | |
| 546 return d2i_ECDSA_SIG(NULL, &sigbuf, siglen); | |
| 547 } | |
| 548 | |
| 549 int EcdsaMethodSignSetup(EC_KEY* eckey, | |
| 550 BN_CTX* ctx, | |
| 551 BIGNUM** kinv, | |
| 552 BIGNUM** r) { | |
| 553 NOTIMPLEMENTED(); | |
| 554 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ECDSA_R_ERR_EC_LIB); | |
| 555 return -1; | |
| 556 } | |
| 557 | |
| 558 int EcdsaMethodDoVerify(const unsigned char* dgst, | |
| 559 int dgst_len, | |
| 560 const ECDSA_SIG* sig, | |
| 561 EC_KEY* eckey) { | |
| 562 NOTIMPLEMENTED(); | |
| 563 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_ERR_EC_LIB); | |
| 564 return -1; | |
| 565 } | |
| 566 | |
| 567 const ECDSA_METHOD android_ecdsa_method = { | |
| 568 /* .name = */ "Android signing-only ECDSA method", | |
| 569 /* .ecdsa_do_sign = */ EcdsaMethodDoSign, | |
| 570 /* .ecdsa_sign_setup = */ EcdsaMethodSignSetup, | |
| 571 /* .ecdsa_do_verify = */ EcdsaMethodDoVerify, | |
| 572 /* .flags = */ 0, | |
| 573 /* .app_data = */ NULL, | |
| 574 }; | |
| 575 | |
| 576 void GetEcdsaPkeyWrapper(ScopedEVP_PKEY& pkey, | |
| 577 ScopedJavaGlobalRef<jobject>& private_key) { | |
| 578 ScopedEC_KEY eckey(EC_KEY_new()); | |
| 579 ECDSA_set_method(eckey.get(), &android_ecdsa_method); | |
| 580 | |
| 581 // To ensure that ECDSA_size() works properly, craft a custom EC_GROUP | |
| 582 // that has the same order than the private key. | |
| 583 // In case of error, return directly, since pkey will be left untouched | |
| 584 // (and still set to NULL). | |
| 585 { | |
| 586 std::vector<uint8> order; | |
| 587 if (!net::android::GetECKeyOrder(private_key.obj(), &order)) { | |
| 588 LOG(ERROR) << "Can't extract order parameter from EC private key"; | |
| 589 return; | |
| 590 } | |
| 591 ScopedEC_GROUP group(EC_GROUP_new(EC_GFp_nist_method())); | |
| 592 if (!group.get()) { | |
| 593 LOG(ERROR) << "Can't create new EC_GROUP"; | |
| 594 return; | |
| 595 } | |
| 596 if (!CopyBigNumFromBytes(&group.get()->order, order)) { | |
| 597 LOG(ERROR) << "Can't decode order from PrivateKey"; | |
| 598 return; | |
| 599 } | |
| 600 EC_KEY_set_group(eckey.get(), group.release()); | |
| 601 } | |
|
Ryan Sleevi
2013/02/04 22:53:27
style: Don't use the extra function-level block. I
digit1
2013/02/05 14:31:58
Done.
| |
| 602 | |
| 603 ECDSA_set_ex_data(eckey.get(), | |
| 604 EcdsaGetExDataIndex(), | |
| 605 private_key.Release()); | |
| 606 | |
| 607 EVP_PKEY_assign_EC_KEY(pkey.get(), eckey.release()); | |
| 608 } | |
| 609 | |
| 610 } // namespace | |
| 611 | |
| 612 | |
| 613 namespace net { | |
| 614 namespace android { | |
| 615 | |
| 616 EVP_PKEY* GetOpenSSLPrivateKeyWrapper(jobject private_key) { | |
| 617 // Create scoped JNI global reference from the private key. | |
| 618 // This ensure that it will be usable from any thread, not only | |
| 619 // from the caller's. | |
| 620 // | |
| 621 // Note: "ScopedJavaGlobalRef<jobject> ref(a_jobject)" doesn't | |
| 622 // compile. Route around this by creating an empty object first, | |
| 623 // then resetting it. | |
| 624 ScopedJavaGlobalRef<jobject> global_key; | |
| 625 global_key.Reset(NULL, private_key); | |
| 626 if (global_key.is_null()) | |
| 627 return NULL; | |
| 628 | |
| 629 // Create new empty EVP_PKEY instance. | |
| 630 ScopedEVP_PKEY pkey(EVP_PKEY_new()); | |
| 631 if (!pkey.get()) | |
| 632 return NULL; | |
| 633 | |
| 634 // Create sub key type, depending on private key's algorithm type. | |
| 635 PrivateKeyType key_type = GetPrivateKeyType(global_key.obj()); | |
| 636 switch (key_type) { | |
| 637 case PRIVATE_KEY_TYPE_RSA: | |
| 638 { | |
| 639 // Route around platform bug: if Android < 4.2, then | |
| 640 // base::android::RawSignDigestWithPrivateKey() cannot work, so | |
| 641 // instead, obtain a raw EVP_PKEY* to the system object | |
| 642 // backing this PrivateKey object. | |
| 643 const int kAndroid42ApiLevel = 17; | |
| 644 if (base::android::BuildInfo::GetInstance()->sdk_int() < | |
| 645 kAndroid42ApiLevel) { | |
| 646 GetRsaLegacyKey(pkey, global_key); | |
| 647 } else { | |
| 648 // Running on Android 4.2. | |
| 649 GetRsaPkeyWrapper(pkey, global_key); | |
| 650 } | |
| 651 } | |
| 652 break; | |
| 653 case PRIVATE_KEY_TYPE_DSA: | |
| 654 GetDsaPkeyWrapper(pkey, global_key); | |
| 655 break; | |
| 656 case PRIVATE_KEY_TYPE_ECDSA: | |
| 657 GetEcdsaPkeyWrapper(pkey, global_key); | |
| 658 break; | |
| 659 default: | |
| 660 LOG(WARNING) | |
| 661 << "GetOpenSSLPrivateKeyWrapper() called with invalid key type"; | |
| 662 return NULL; | |
| 663 } | |
| 664 | |
| 665 return pkey.release(); | |
| 666 } | |
| 667 | |
| 668 } // namespace android | |
| 669 } // namespace net | |
| OLD | NEW |