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