blob: a3e577f3a02d7ff869674dfe4df241d7e1f227bb [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"
Mohamed Heikal58800442019-06-22 00:19:4813#include "net/net_jni_headers/AndroidKeyStore_jni.h"
[email protected]03a07b2e2013-02-11 20:13:4514
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 Benjamin54d1ef72019-01-22 17:51:3627std::string GetPrivateKeyClassName(const JavaRef<jobject>& key) {
David Benjaminb65b0732018-11-09 20:33:5328 JNIEnv* env = AttachCurrentThread();
29 ScopedJavaLocalRef<jstring> name =
30 Java_AndroidKeyStore_getPrivateKeyClassName(env, key);
31 return ConvertJavaStringToUTF8(env, name);
32}
33
David Benjamin08d50eb2019-04-03 21:05:3434bool 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
47bool 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 Benjamin54d1ef72019-01-22 17:51:3660bool SignWithPrivateKey(const JavaRef<jobject>& private_key_ref,
David Benjamin5b4410e2017-11-10 21:50:2361 base::StringPiece algorithm,
62 base::span<const uint8_t> input,
63 std::vector<uint8_t>* signature) {
[email protected]03a07b2e2013-02-11 20:13:4564 JNIEnv* env = AttachCurrentThread();
65
David Benjamin5b4410e2017-11-10 21:50:2366 ScopedJavaLocalRef<jstring> algorithm_ref =
67 ConvertUTF8ToJavaString(env, algorithm);
68 DCHECK(!algorithm_ref.is_null());
69
[email protected]03a07b2e2013-02-11 20:13:4570 // Convert message to byte[] array.
David Benjamin5b4410e2017-11-10 21:50:2371 ScopedJavaLocalRef<jbyteArray> input_ref =
jdoerrie068c8572018-03-27 12:51:4072 ToJavaByteArray(env, input.data(), input.size());
David Benjamin5b4410e2017-11-10 21:50:2373 DCHECK(!input_ref.is_null());
[email protected]03a07b2e2013-02-11 20:13:4574
75 // Invoke platform API
76 ScopedJavaLocalRef<jbyteArray> signature_ref =
David Benjamin5b4410e2017-11-10 21:50:2377 Java_AndroidKeyStore_signWithPrivateKey(env, private_key_ref,
78 algorithm_ref, input_ref);
[email protected]03a07b2e2013-02-11 20:13:4579 if (HasException(env) || signature_ref.is_null())
80 return false;
81
82 // Write signature to string.
Torne (Richard Coles)3c22e8302018-10-12 18:34:2283 JavaByteArrayToByteVector(env, signature_ref, signature);
[email protected]03a07b2e2013-02-11 20:13:4584 return true;
85}
86
David Benjamin08d50eb2019-04-03 21:05:3487bool 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]03a07b2e2013-02-11 20:13:45114} // namespace android
115} // namespace net