| 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.h" | 5 #include "net/android/keystore.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_array.h" | 10 #include "base/android/jni_array.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "jni/AndroidKeyStore_jni.h" | 12 #include "jni/AndroidKeyStore_jni.h" |
| 13 | 13 |
| 14 using base::android::AttachCurrentThread; | 14 using base::android::AttachCurrentThread; |
| 15 using base::android::HasException; | 15 using base::android::HasException; |
| 16 using base::android::JavaArrayOfByteArrayToStringVector; |
| 16 using base::android::JavaByteArrayToByteVector; | 17 using base::android::JavaByteArrayToByteVector; |
| 18 using base::android::JavaRef; |
| 17 using base::android::ScopedJavaLocalRef; | 19 using base::android::ScopedJavaLocalRef; |
| 18 using base::android::ToJavaByteArray; | 20 using base::android::ToJavaByteArray; |
| 19 using base::android::JavaArrayOfByteArrayToStringVector; | |
| 20 | 21 |
| 21 namespace net { | 22 namespace net { |
| 22 namespace android { | 23 namespace android { |
| 23 | 24 |
| 24 bool GetRSAKeyModulus(jobject private_key_ref, std::vector<uint8_t>* result) { | 25 bool GetRSAKeyModulus(const JavaRef<jobject>& private_key_ref, |
| 26 std::vector<uint8_t>* result) { |
| 25 JNIEnv* env = AttachCurrentThread(); | 27 JNIEnv* env = AttachCurrentThread(); |
| 26 | 28 |
| 27 ScopedJavaLocalRef<jbyteArray> modulus_ref = | 29 ScopedJavaLocalRef<jbyteArray> modulus_ref = |
| 28 Java_AndroidKeyStore_getRSAKeyModulus(env, private_key_ref); | 30 Java_AndroidKeyStore_getRSAKeyModulus(env, private_key_ref); |
| 29 if (modulus_ref.is_null()) | 31 if (modulus_ref.is_null()) |
| 30 return false; | 32 return false; |
| 31 | 33 |
| 32 JavaByteArrayToByteVector(env, modulus_ref.obj(), result); | 34 JavaByteArrayToByteVector(env, modulus_ref.obj(), result); |
| 33 return true; | 35 return true; |
| 34 } | 36 } |
| 35 | 37 |
| 36 bool GetECKeyOrder(jobject private_key_ref, std::vector<uint8_t>* result) { | 38 bool GetECKeyOrder(const JavaRef<jobject>& private_key_ref, |
| 39 std::vector<uint8_t>* result) { |
| 37 JNIEnv* env = AttachCurrentThread(); | 40 JNIEnv* env = AttachCurrentThread(); |
| 38 | 41 |
| 39 ScopedJavaLocalRef<jbyteArray> order_ref = | 42 ScopedJavaLocalRef<jbyteArray> order_ref = |
| 40 Java_AndroidKeyStore_getECKeyOrder(env, private_key_ref); | 43 Java_AndroidKeyStore_getECKeyOrder(env, private_key_ref); |
| 41 | 44 |
| 42 if (order_ref.is_null()) | 45 if (order_ref.is_null()) |
| 43 return false; | 46 return false; |
| 44 | 47 |
| 45 JavaByteArrayToByteVector(env, order_ref.obj(), result); | 48 JavaByteArrayToByteVector(env, order_ref.obj(), result); |
| 46 return true; | 49 return true; |
| 47 } | 50 } |
| 48 | 51 |
| 49 bool RawSignDigestWithPrivateKey(jobject private_key_ref, | 52 bool RawSignDigestWithPrivateKey(const JavaRef<jobject>& private_key_ref, |
| 50 const base::StringPiece& digest, | 53 const base::StringPiece& digest, |
| 51 std::vector<uint8_t>* signature) { | 54 std::vector<uint8_t>* signature) { |
| 52 JNIEnv* env = AttachCurrentThread(); | 55 JNIEnv* env = AttachCurrentThread(); |
| 53 | 56 |
| 54 // Convert message to byte[] array. | 57 // Convert message to byte[] array. |
| 55 ScopedJavaLocalRef<jbyteArray> digest_ref = ToJavaByteArray( | 58 ScopedJavaLocalRef<jbyteArray> digest_ref = ToJavaByteArray( |
| 56 env, reinterpret_cast<const uint8_t*>(digest.data()), digest.length()); | 59 env, reinterpret_cast<const uint8_t*>(digest.data()), digest.length()); |
| 57 DCHECK(!digest_ref.is_null()); | 60 DCHECK(!digest_ref.is_null()); |
| 58 | 61 |
| 59 // Invoke platform API | 62 // Invoke platform API |
| 60 ScopedJavaLocalRef<jbyteArray> signature_ref = | 63 ScopedJavaLocalRef<jbyteArray> signature_ref = |
| 61 Java_AndroidKeyStore_rawSignDigestWithPrivateKey(env, private_key_ref, | 64 Java_AndroidKeyStore_rawSignDigestWithPrivateKey(env, private_key_ref, |
| 62 digest_ref); | 65 digest_ref); |
| 63 if (HasException(env) || signature_ref.is_null()) | 66 if (HasException(env) || signature_ref.is_null()) |
| 64 return false; | 67 return false; |
| 65 | 68 |
| 66 // Write signature to string. | 69 // Write signature to string. |
| 67 JavaByteArrayToByteVector(env, signature_ref.obj(), signature); | 70 JavaByteArrayToByteVector(env, signature_ref.obj(), signature); |
| 68 return true; | 71 return true; |
| 69 } | 72 } |
| 70 | 73 |
| 71 PrivateKeyType GetPrivateKeyType(jobject private_key_ref) { | 74 PrivateKeyType GetPrivateKeyType(const JavaRef<jobject>& private_key_ref) { |
| 72 JNIEnv* env = AttachCurrentThread(); | 75 JNIEnv* env = AttachCurrentThread(); |
| 73 int type = Java_AndroidKeyStore_getPrivateKeyType(env, private_key_ref); | 76 int type = Java_AndroidKeyStore_getPrivateKeyType(env, private_key_ref); |
| 74 return static_cast<PrivateKeyType>(type); | 77 return static_cast<PrivateKeyType>(type); |
| 75 } | 78 } |
| 76 | 79 |
| 77 AndroidEVP_PKEY* GetOpenSSLSystemHandleForPrivateKey(jobject private_key_ref) { | 80 AndroidEVP_PKEY* GetOpenSSLSystemHandleForPrivateKey( |
| 81 const JavaRef<jobject>& private_key_ref) { |
| 78 JNIEnv* env = AttachCurrentThread(); | 82 JNIEnv* env = AttachCurrentThread(); |
| 79 // Note: the pointer is passed as a jint here because that's how it | 83 // Note: the pointer is passed as a jint here because that's how it |
| 80 // is stored in the Java object. Java doesn't have a primitive type | 84 // is stored in the Java object. Java doesn't have a primitive type |
| 81 // like intptr_t that matches the size of pointers on the host | 85 // like intptr_t that matches the size of pointers on the host |
| 82 // machine, and Android only runs on 32-bit CPUs. | 86 // machine, and Android only runs on 32-bit CPUs. |
| 83 // | 87 // |
| 84 // Given that this routine shall only be called on Android < 4.2, | 88 // Given that this routine shall only be called on Android < 4.2, |
| 85 // this won't be a problem in the far future (e.g. when Android gets | 89 // this won't be a problem in the far future (e.g. when Android gets |
| 86 // ported to 64-bit environments, if ever). | 90 // ported to 64-bit environments, if ever). |
| 87 long pkey = | 91 long pkey = |
| 88 Java_AndroidKeyStore_getOpenSSLHandleForPrivateKey(env, private_key_ref); | 92 Java_AndroidKeyStore_getOpenSSLHandleForPrivateKey(env, private_key_ref); |
| 89 return reinterpret_cast<AndroidEVP_PKEY*>(pkey); | 93 return reinterpret_cast<AndroidEVP_PKEY*>(pkey); |
| 90 } | 94 } |
| 91 | 95 |
| 92 ScopedJavaLocalRef<jobject> GetOpenSSLEngineForPrivateKey( | 96 ScopedJavaLocalRef<jobject> GetOpenSSLEngineForPrivateKey( |
| 93 jobject private_key_ref) { | 97 const JavaRef<jobject>& private_key_ref) { |
| 94 JNIEnv* env = AttachCurrentThread(); | 98 JNIEnv* env = AttachCurrentThread(); |
| 95 ScopedJavaLocalRef<jobject> engine = | 99 ScopedJavaLocalRef<jobject> engine = |
| 96 Java_AndroidKeyStore_getOpenSSLEngineForPrivateKey(env, private_key_ref); | 100 Java_AndroidKeyStore_getOpenSSLEngineForPrivateKey(env, private_key_ref); |
| 97 return engine; | 101 return engine; |
| 98 } | 102 } |
| 99 | 103 |
| 100 } // namespace android | 104 } // namespace android |
| 101 } // namespace net | 105 } // namespace net |
| OLD | NEW |