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