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