[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 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" |
David Benjamin | 5b4410e | 2017-11-10 21:50:23 | [diff] [blame] | 11 | #include "base/android/jni_string.h" |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 12 | #include "base/logging.h" |
Mohamed Heikal | 5880044 | 2019-06-22 00:19:48 | [diff] [blame] | 13 | #include "net/net_jni_headers/AndroidKeyStore_jni.h" |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 14 | |
| 15 | using base::android::AttachCurrentThread; |
David Benjamin | b65b073 | 2018-11-09 20:33:53 | [diff] [blame] | 16 | using base::android::ConvertJavaStringToUTF8; |
David Benjamin | 5b4410e | 2017-11-10 21:50:23 | [diff] [blame] | 17 | using base::android::ConvertUTF8ToJavaString; |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 18 | using base::android::HasException; |
| 19 | using base::android::JavaByteArrayToByteVector; |
torne | 1cf2e5dc | 2016-08-31 16:49:58 | [diff] [blame] | 20 | using base::android::JavaRef; |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 21 | using base::android::ScopedJavaLocalRef; |
| 22 | using base::android::ToJavaByteArray; |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 23 | |
| 24 | namespace net { |
| 25 | namespace android { |
| 26 | |
David Benjamin | 54d1ef7 | 2019-01-22 17:51:36 | [diff] [blame] | 27 | std::string GetPrivateKeyClassName(const JavaRef<jobject>& key) { |
David Benjamin | b65b073 | 2018-11-09 20:33:53 | [diff] [blame] | 28 | JNIEnv* env = AttachCurrentThread(); |
| 29 | ScopedJavaLocalRef<jstring> name = |
| 30 | Java_AndroidKeyStore_getPrivateKeyClassName(env, key); |
| 31 | return ConvertJavaStringToUTF8(env, name); |
| 32 | } |
| 33 | |
David Benjamin | 08d50eb | 2019-04-03 21:05:34 | [diff] [blame] | 34 | bool PrivateKeySupportsSignature(const base::android::JavaRef<jobject>& key, |
| 35 | base::StringPiece 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 | |
| 47 | bool PrivateKeySupportsCipher(const base::android::JavaRef<jobject>& key, |
| 48 | base::StringPiece 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 | |
David Benjamin | 54d1ef7 | 2019-01-22 17:51:36 | [diff] [blame] | 60 | bool SignWithPrivateKey(const JavaRef<jobject>& private_key_ref, |
David Benjamin | 5b4410e | 2017-11-10 21:50:23 | [diff] [blame] | 61 | base::StringPiece algorithm, |
| 62 | base::span<const uint8_t> input, |
| 63 | std::vector<uint8_t>* signature) { |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 64 | JNIEnv* env = AttachCurrentThread(); |
| 65 | |
David Benjamin | 5b4410e | 2017-11-10 21:50:23 | [diff] [blame] | 66 | ScopedJavaLocalRef<jstring> algorithm_ref = |
| 67 | ConvertUTF8ToJavaString(env, algorithm); |
| 68 | DCHECK(!algorithm_ref.is_null()); |
| 69 | |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 70 | // Convert message to byte[] array. |
David Benjamin | 5b4410e | 2017-11-10 21:50:23 | [diff] [blame] | 71 | ScopedJavaLocalRef<jbyteArray> input_ref = |
jdoerrie | 068c857 | 2018-03-27 12:51:40 | [diff] [blame] | 72 | ToJavaByteArray(env, input.data(), input.size()); |
David Benjamin | 5b4410e | 2017-11-10 21:50:23 | [diff] [blame] | 73 | DCHECK(!input_ref.is_null()); |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 74 | |
| 75 | // Invoke platform API |
| 76 | ScopedJavaLocalRef<jbyteArray> signature_ref = |
David Benjamin | 5b4410e | 2017-11-10 21:50:23 | [diff] [blame] | 77 | Java_AndroidKeyStore_signWithPrivateKey(env, private_key_ref, |
| 78 | algorithm_ref, input_ref); |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 79 | if (HasException(env) || signature_ref.is_null()) |
| 80 | return false; |
| 81 | |
| 82 | // Write signature to string. |
Torne (Richard Coles) | 3c22e830 | 2018-10-12 18:34:22 | [diff] [blame] | 83 | JavaByteArrayToByteVector(env, signature_ref, signature); |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 84 | return true; |
| 85 | } |
| 86 | |
David Benjamin | 08d50eb | 2019-04-03 21:05:34 | [diff] [blame] | 87 | bool EncryptWithPrivateKey(const JavaRef<jobject>& private_key_ref, |
| 88 | base::StringPiece algorithm, |
| 89 | base::span<const uint8_t> input, |
| 90 | std::vector<uint8_t>* ciphertext) { |
| 91 | JNIEnv* env = AttachCurrentThread(); |
| 92 | |
| 93 | ScopedJavaLocalRef<jstring> algorithm_ref = |
| 94 | ConvertUTF8ToJavaString(env, algorithm); |
| 95 | DCHECK(!algorithm_ref.is_null()); |
| 96 | |
| 97 | // Convert message to byte[] array. |
| 98 | ScopedJavaLocalRef<jbyteArray> input_ref = |
| 99 | ToJavaByteArray(env, input.data(), input.size()); |
| 100 | DCHECK(!input_ref.is_null()); |
| 101 | |
| 102 | // Invoke platform API |
| 103 | ScopedJavaLocalRef<jbyteArray> ciphertext_ref = |
| 104 | Java_AndroidKeyStore_encryptWithPrivateKey(env, private_key_ref, |
| 105 | algorithm_ref, input_ref); |
| 106 | if (HasException(env) || ciphertext_ref.is_null()) |
| 107 | return false; |
| 108 | |
| 109 | // Write ciphertext to string. |
| 110 | JavaByteArrayToByteVector(env, ciphertext_ref, ciphertext); |
| 111 | return true; |
| 112 | } |
| 113 | |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 114 | } // namespace android |
| 115 | } // namespace net |