1 // Copyright 2013 The Chromium Authors
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 <string_view>
8 #include <vector>
9
10 #include "base/android/jni_android.h"
11 #include "base/android/jni_array.h"
12 #include "base/android/jni_string.h"
13 #include "base/check.h"
14 #include "net/net_jni_headers/AndroidKeyStore_jni.h"
15
16 using base::android::AttachCurrentThread;
17 using base::android::ConvertJavaStringToUTF8;
18 using base::android::ConvertUTF8ToJavaString;
19 using base::android::HasException;
20 using base::android::JavaByteArrayToByteVector;
21 using base::android::JavaRef;
22 using base::android::ScopedJavaLocalRef;
23 using base::android::ToJavaByteArray;
24
25 namespace net::android {
26
GetPrivateKeyClassName(const JavaRef<jobject> & key)27 std::string GetPrivateKeyClassName(const JavaRef<jobject>& key) {
28 JNIEnv* env = AttachCurrentThread();
29 ScopedJavaLocalRef<jstring> name =
30 Java_AndroidKeyStore_getPrivateKeyClassName(env, key);
31 return ConvertJavaStringToUTF8(env, name);
32 }
33
PrivateKeySupportsSignature(const base::android::JavaRef<jobject> & key,std::string_view algorithm)34 bool PrivateKeySupportsSignature(const base::android::JavaRef<jobject>& key,
35 std::string_view algorithm) {
36 JNIEnv* env = AttachCurrentThread();
37
38 ScopedJavaLocalRef<jstring> algorithm_ref =
39 ConvertUTF8ToJavaString(env, algorithm);
40 DCHECK(!algorithm_ref.is_null());
41
42 jboolean result =
43 Java_AndroidKeyStore_privateKeySupportsSignature(env, key, algorithm_ref);
44 return !HasException(env) && result;
45 }
46
PrivateKeySupportsCipher(const base::android::JavaRef<jobject> & key,std::string_view algorithm)47 bool PrivateKeySupportsCipher(const base::android::JavaRef<jobject>& key,
48 std::string_view algorithm) {
49 JNIEnv* env = AttachCurrentThread();
50
51 ScopedJavaLocalRef<jstring> algorithm_ref =
52 ConvertUTF8ToJavaString(env, algorithm);
53 DCHECK(!algorithm_ref.is_null());
54
55 jboolean result =
56 Java_AndroidKeyStore_privateKeySupportsCipher(env, key, algorithm_ref);
57 return !HasException(env) && result;
58 }
59
SignWithPrivateKey(const JavaRef<jobject> & private_key_ref,std::string_view algorithm,base::span<const uint8_t> input,std::vector<uint8_t> * signature)60 bool SignWithPrivateKey(const JavaRef<jobject>& private_key_ref,
61 std::string_view algorithm,
62 base::span<const uint8_t> input,
63 std::vector<uint8_t>* signature) {
64 JNIEnv* env = AttachCurrentThread();
65
66 ScopedJavaLocalRef<jstring> algorithm_ref =
67 ConvertUTF8ToJavaString(env, algorithm);
68 DCHECK(!algorithm_ref.is_null());
69
70 // Convert message to byte[] array.
71 ScopedJavaLocalRef<jbyteArray> input_ref = ToJavaByteArray(env, input);
72 DCHECK(!input_ref.is_null());
73
74 // Invoke platform API
75 ScopedJavaLocalRef<jbyteArray> signature_ref =
76 Java_AndroidKeyStore_signWithPrivateKey(env, private_key_ref,
77 algorithm_ref, input_ref);
78 if (HasException(env) || signature_ref.is_null())
79 return false;
80
81 // Write signature to string.
82 JavaByteArrayToByteVector(env, signature_ref, signature);
83 return true;
84 }
85
EncryptWithPrivateKey(const JavaRef<jobject> & private_key_ref,std::string_view algorithm,base::span<const uint8_t> input,std::vector<uint8_t> * ciphertext)86 bool EncryptWithPrivateKey(const JavaRef<jobject>& private_key_ref,
87 std::string_view algorithm,
88 base::span<const uint8_t> input,
89 std::vector<uint8_t>* ciphertext) {
90 JNIEnv* env = AttachCurrentThread();
91
92 ScopedJavaLocalRef<jstring> algorithm_ref =
93 ConvertUTF8ToJavaString(env, algorithm);
94 DCHECK(!algorithm_ref.is_null());
95
96 // Convert message to byte[] array.
97 ScopedJavaLocalRef<jbyteArray> input_ref = ToJavaByteArray(env, input);
98 DCHECK(!input_ref.is_null());
99
100 // Invoke platform API
101 ScopedJavaLocalRef<jbyteArray> ciphertext_ref =
102 Java_AndroidKeyStore_encryptWithPrivateKey(env, private_key_ref,
103 algorithm_ref, input_ref);
104 if (HasException(env) || ciphertext_ref.is_null())
105 return false;
106
107 // Write ciphertext to string.
108 JavaByteArrayToByteVector(env, ciphertext_ref, ciphertext);
109 return true;
110 }
111
112 } // namespace net::android
113