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