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.
5 #include "net/android/keystore.h"
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_array.h"
11 #include "base/logging.h"
12 #include "jni/AndroidKeyStore_jni.h"
13 #include "net/android/android_private_key.h"
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
;
25 bool GetRSAKeyModulus(jobject private_key_ref
, std::vector
<uint8_t>* result
) {
26 JNIEnv
* env
= AttachCurrentThread();
28 ScopedJavaLocalRef
<jbyteArray
> modulus_ref
=
29 Java_AndroidKeyStore_getRSAKeyModulus(env
,
30 GetKeyStore(private_key_ref
).obj(),
32 if (modulus_ref
.is_null())
35 JavaByteArrayToByteVector(env
, modulus_ref
.obj(), result
);
39 bool GetECKeyOrder(jobject private_key_ref
, std::vector
<uint8_t>* result
) {
40 JNIEnv
* env
= AttachCurrentThread();
42 ScopedJavaLocalRef
<jbyteArray
> order_ref
=
43 Java_AndroidKeyStore_getECKeyOrder(
45 GetKeyStore(private_key_ref
).obj(),
48 if (order_ref
.is_null())
51 JavaByteArrayToByteVector(env
, order_ref
.obj(), result
);
55 bool RawSignDigestWithPrivateKey(jobject private_key_ref
,
56 const base::StringPiece
& digest
,
57 std::vector
<uint8_t>* signature
) {
58 JNIEnv
* env
= AttachCurrentThread();
60 // Convert message to byte[] array.
61 ScopedJavaLocalRef
<jbyteArray
> digest_ref
= ToJavaByteArray(
62 env
, reinterpret_cast<const uint8_t*>(digest
.data()), digest
.length());
63 DCHECK(!digest_ref
.is_null());
65 // Invoke platform API
66 ScopedJavaLocalRef
<jbyteArray
> signature_ref
=
67 Java_AndroidKeyStore_rawSignDigestWithPrivateKey(
69 GetKeyStore(private_key_ref
).obj(),
72 if (HasException(env
) || signature_ref
.is_null())
75 // Write signature to string.
76 JavaByteArrayToByteVector(env
, signature_ref
.obj(), signature
);
80 PrivateKeyType
GetPrivateKeyType(jobject private_key_ref
) {
81 JNIEnv
* env
= AttachCurrentThread();
82 int type
= Java_AndroidKeyStore_getPrivateKeyType(
84 GetKeyStore(private_key_ref
).obj(),
86 return static_cast<PrivateKeyType
>(type
);
89 AndroidEVP_PKEY
* GetOpenSSLSystemHandleForPrivateKey(jobject private_key_ref
) {
90 JNIEnv
* env
= AttachCurrentThread();
91 // 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
93 // like intptr_t that matches the size of pointers on the host
94 // machine, and Android only runs on 32-bit CPUs.
96 // 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
98 // ported to 64-bit environments, if ever).
99 long pkey
= Java_AndroidKeyStore_getOpenSSLHandleForPrivateKey(
101 GetKeyStore(private_key_ref
).obj(),
103 return reinterpret_cast<AndroidEVP_PKEY
*>(pkey
);
106 ScopedJavaLocalRef
<jobject
> GetOpenSSLEngineForPrivateKey(
107 jobject private_key_ref
) {
108 JNIEnv
* env
= AttachCurrentThread();
109 ScopedJavaLocalRef
<jobject
> engine
=
110 Java_AndroidKeyStore_getOpenSSLEngineForPrivateKey(
112 GetKeyStore(private_key_ref
).obj(),
117 void ReleaseKey(jobject private_key_ref
) {
118 JNIEnv
* env
= AttachCurrentThread();
119 Java_AndroidKeyStore_releaseKey(env
,
120 GetKeyStore(private_key_ref
).obj(),
122 env
->DeleteGlobalRef(private_key_ref
);
125 bool RegisterKeyStore(JNIEnv
* env
) {
126 return RegisterNativesImpl(env
);
129 } // namespace android