[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" |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 13 | #include "jni/AndroidKeyStore_jni.h" |
| 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 | b65b073 | 2018-11-09 20:33:53 | [diff] [blame^] | 27 | std::string GetPrivateKeyClassName(const base::android::JavaRef<jobject>& key) { |
| 28 | JNIEnv* env = AttachCurrentThread(); |
| 29 | ScopedJavaLocalRef<jstring> name = |
| 30 | Java_AndroidKeyStore_getPrivateKeyClassName(env, key); |
| 31 | return ConvertJavaStringToUTF8(env, name); |
| 32 | } |
| 33 | |
David Benjamin | 5b4410e | 2017-11-10 21:50:23 | [diff] [blame] | 34 | bool SignWithPrivateKey(const base::android::JavaRef<jobject>& private_key_ref, |
| 35 | base::StringPiece algorithm, |
| 36 | base::span<const uint8_t> input, |
| 37 | std::vector<uint8_t>* signature) { |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 38 | JNIEnv* env = AttachCurrentThread(); |
| 39 | |
David Benjamin | 5b4410e | 2017-11-10 21:50:23 | [diff] [blame] | 40 | ScopedJavaLocalRef<jstring> algorithm_ref = |
| 41 | ConvertUTF8ToJavaString(env, algorithm); |
| 42 | DCHECK(!algorithm_ref.is_null()); |
| 43 | |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 44 | // Convert message to byte[] array. |
David Benjamin | 5b4410e | 2017-11-10 21:50:23 | [diff] [blame] | 45 | ScopedJavaLocalRef<jbyteArray> input_ref = |
jdoerrie | 068c857 | 2018-03-27 12:51:40 | [diff] [blame] | 46 | ToJavaByteArray(env, input.data(), input.size()); |
David Benjamin | 5b4410e | 2017-11-10 21:50:23 | [diff] [blame] | 47 | DCHECK(!input_ref.is_null()); |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 48 | |
| 49 | // Invoke platform API |
| 50 | ScopedJavaLocalRef<jbyteArray> signature_ref = |
David Benjamin | 5b4410e | 2017-11-10 21:50:23 | [diff] [blame] | 51 | Java_AndroidKeyStore_signWithPrivateKey(env, private_key_ref, |
| 52 | algorithm_ref, input_ref); |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 53 | if (HasException(env) || signature_ref.is_null()) |
| 54 | return false; |
| 55 | |
| 56 | // Write signature to string. |
Torne (Richard Coles) | 3c22e830 | 2018-10-12 18:34:22 | [diff] [blame] | 57 | JavaByteArrayToByteVector(env, signature_ref, signature); |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 58 | return true; |
| 59 | } |
| 60 | |
torne | 1cf2e5dc | 2016-08-31 16:49:58 | [diff] [blame] | 61 | AndroidEVP_PKEY* GetOpenSSLSystemHandleForPrivateKey( |
| 62 | const JavaRef<jobject>& private_key_ref) { |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 63 | JNIEnv* env = AttachCurrentThread(); |
| 64 | // Note: the pointer is passed as a jint here because that's how it |
| 65 | // is stored in the Java object. Java doesn't have a primitive type |
| 66 | // like intptr_t that matches the size of pointers on the host |
| 67 | // machine, and Android only runs on 32-bit CPUs. |
| 68 | // |
| 69 | // Given that this routine shall only be called on Android < 4.2, |
| 70 | // this won't be a problem in the far future (e.g. when Android gets |
| 71 | // ported to 64-bit environments, if ever). |
changwan | e04b8f5 | 2015-12-02 06:44:01 | [diff] [blame] | 72 | long pkey = |
| 73 | Java_AndroidKeyStore_getOpenSSLHandleForPrivateKey(env, private_key_ref); |
[email protected] | eeff853 | 2014-07-11 22:07:59 | [diff] [blame] | 74 | return reinterpret_cast<AndroidEVP_PKEY*>(pkey); |
| 75 | } |
| 76 | |
| 77 | ScopedJavaLocalRef<jobject> GetOpenSSLEngineForPrivateKey( |
torne | 1cf2e5dc | 2016-08-31 16:49:58 | [diff] [blame] | 78 | const JavaRef<jobject>& private_key_ref) { |
[email protected] | eeff853 | 2014-07-11 22:07:59 | [diff] [blame] | 79 | JNIEnv* env = AttachCurrentThread(); |
| 80 | ScopedJavaLocalRef<jobject> engine = |
changwan | e04b8f5 | 2015-12-02 06:44:01 | [diff] [blame] | 81 | Java_AndroidKeyStore_getOpenSSLEngineForPrivateKey(env, private_key_ref); |
[email protected] | eeff853 | 2014-07-11 22:07:59 | [diff] [blame] | 82 | return engine; |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 83 | } |
| 84 | |
[email protected] | 03a07b2e | 2013-02-11 20:13:45 | [diff] [blame] | 85 | } // namespace android |
| 86 | } // namespace net |