blob: 3d0fc182e238c30396dbc08df141a4c28c3ef5c3 [file] [log] [blame]
[email protected]03a07b2e2013-02-11 20:13:451// 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 Benjamin5b4410e2017-11-10 21:50:2311#include "base/android/jni_string.h"
[email protected]03a07b2e2013-02-11 20:13:4512#include "base/logging.h"
[email protected]03a07b2e2013-02-11 20:13:4513#include "jni/AndroidKeyStore_jni.h"
14
15using base::android::AttachCurrentThread;
David Benjaminb65b0732018-11-09 20:33:5316using base::android::ConvertJavaStringToUTF8;
David Benjamin5b4410e2017-11-10 21:50:2317using base::android::ConvertUTF8ToJavaString;
[email protected]03a07b2e2013-02-11 20:13:4518using base::android::HasException;
19using base::android::JavaByteArrayToByteVector;
torne1cf2e5dc2016-08-31 16:49:5820using base::android::JavaRef;
[email protected]03a07b2e2013-02-11 20:13:4521using base::android::ScopedJavaLocalRef;
22using base::android::ToJavaByteArray;
[email protected]03a07b2e2013-02-11 20:13:4523
24namespace net {
25namespace android {
26
David Benjaminb65b0732018-11-09 20:33:5327std::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 Benjamin5b4410e2017-11-10 21:50:2334bool 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]03a07b2e2013-02-11 20:13:4538 JNIEnv* env = AttachCurrentThread();
39
David Benjamin5b4410e2017-11-10 21:50:2340 ScopedJavaLocalRef<jstring> algorithm_ref =
41 ConvertUTF8ToJavaString(env, algorithm);
42 DCHECK(!algorithm_ref.is_null());
43
[email protected]03a07b2e2013-02-11 20:13:4544 // Convert message to byte[] array.
David Benjamin5b4410e2017-11-10 21:50:2345 ScopedJavaLocalRef<jbyteArray> input_ref =
jdoerrie068c8572018-03-27 12:51:4046 ToJavaByteArray(env, input.data(), input.size());
David Benjamin5b4410e2017-11-10 21:50:2347 DCHECK(!input_ref.is_null());
[email protected]03a07b2e2013-02-11 20:13:4548
49 // Invoke platform API
50 ScopedJavaLocalRef<jbyteArray> signature_ref =
David Benjamin5b4410e2017-11-10 21:50:2351 Java_AndroidKeyStore_signWithPrivateKey(env, private_key_ref,
52 algorithm_ref, input_ref);
[email protected]03a07b2e2013-02-11 20:13:4553 if (HasException(env) || signature_ref.is_null())
54 return false;
55
56 // Write signature to string.
Torne (Richard Coles)3c22e8302018-10-12 18:34:2257 JavaByteArrayToByteVector(env, signature_ref, signature);
[email protected]03a07b2e2013-02-11 20:13:4558 return true;
59}
60
torne1cf2e5dc2016-08-31 16:49:5861AndroidEVP_PKEY* GetOpenSSLSystemHandleForPrivateKey(
62 const JavaRef<jobject>& private_key_ref) {
[email protected]03a07b2e2013-02-11 20:13:4563 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).
changwane04b8f52015-12-02 06:44:0172 long pkey =
73 Java_AndroidKeyStore_getOpenSSLHandleForPrivateKey(env, private_key_ref);
[email protected]eeff8532014-07-11 22:07:5974 return reinterpret_cast<AndroidEVP_PKEY*>(pkey);
75}
76
77ScopedJavaLocalRef<jobject> GetOpenSSLEngineForPrivateKey(
torne1cf2e5dc2016-08-31 16:49:5878 const JavaRef<jobject>& private_key_ref) {
[email protected]eeff8532014-07-11 22:07:5979 JNIEnv* env = AttachCurrentThread();
80 ScopedJavaLocalRef<jobject> engine =
changwane04b8f52015-12-02 06:44:0181 Java_AndroidKeyStore_getOpenSSLEngineForPrivateKey(env, private_key_ref);
[email protected]eeff8532014-07-11 22:07:5982 return engine;
[email protected]03a07b2e2013-02-11 20:13:4583}
84
[email protected]03a07b2e2013-02-11 20:13:4585} // namespace android
86} // namespace net