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