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