Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / android / keystore.cc
blob4fa8dbf6329a2f4bed4a6abc7c51eb1a0a3b28d2
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"
7 #include <vector>
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;
22 namespace net {
23 namespace android {
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(),
31 private_key_ref);
32 if (modulus_ref.is_null())
33 return false;
35 JavaByteArrayToByteVector(env, modulus_ref.obj(), result);
36 return true;
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(
44 env,
45 GetKeyStore(private_key_ref).obj(),
46 private_key_ref);
48 if (order_ref.is_null())
49 return false;
51 JavaByteArrayToByteVector(env, order_ref.obj(), result);
52 return true;
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(
68 env,
69 GetKeyStore(private_key_ref).obj(),
70 private_key_ref,
71 digest_ref.obj());
72 if (HasException(env) || signature_ref.is_null())
73 return false;
75 // Write signature to string.
76 JavaByteArrayToByteVector(env, signature_ref.obj(), signature);
77 return true;
80 PrivateKeyType GetPrivateKeyType(jobject private_key_ref) {
81 JNIEnv* env = AttachCurrentThread();
82 int type = Java_AndroidKeyStore_getPrivateKeyType(
83 env,
84 GetKeyStore(private_key_ref).obj(),
85 private_key_ref);
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(
100 env,
101 GetKeyStore(private_key_ref).obj(),
102 private_key_ref);
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(
111 env,
112 GetKeyStore(private_key_ref).obj(),
113 private_key_ref);
114 return engine;
117 void ReleaseKey(jobject private_key_ref) {
118 JNIEnv* env = AttachCurrentThread();
119 Java_AndroidKeyStore_releaseKey(env,
120 GetKeyStore(private_key_ref).obj(),
121 private_key_ref);
122 env->DeleteGlobalRef(private_key_ref);
125 bool RegisterKeyStore(JNIEnv* env) {
126 return RegisterNativesImpl(env);
129 } // namespace android
130 } // namespace net