Chromium Code Reviews| Index: net/android/keystore.cc |
| diff --git a/net/android/keystore.cc b/net/android/keystore.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..448dc69979948972f3e9843bb72968a8a25d66c2 |
| --- /dev/null |
| +++ b/net/android/keystore.cc |
| @@ -0,0 +1,69 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "net/android/keystore.h" |
| + |
| +#include <vector> |
| + |
| +#include "base/android/jni_android.h" |
| +#include "base/android/jni_array.h" |
| +#include "base/logging.h" |
| + |
| +#include "jni/AndroidKeyStore_jni.h" |
| + |
| +using base::android::AttachCurrentThread; |
| +using base::android::HasException; |
| +using base::android::JavaByteArrayToByteVector; |
| +using base::android::ScopedJavaLocalRef; |
| +using base::android::ToJavaByteArray; |
| +using base::android::JavaArrayOfByteArrayToStringVector; |
| + |
| +namespace net { |
| +namespace android { |
| + |
| +bool SignWithPrivateKey( |
| + jobject private_key_ref, |
| + const base::StringPiece& message, |
| + std::vector<uint8>* signature) { |
| + JNIEnv* env = AttachCurrentThread(); |
| + |
| + // Convert message to byte[] array. |
| + ScopedJavaLocalRef<jbyteArray> message_ref = |
| + ToJavaByteArray(env, |
| + reinterpret_cast<const uint8*>(message.data()), |
| + message.length()); |
| + DCHECK(!message_ref.is_null()); |
| + |
| + // Invoke platform API |
| + ScopedJavaLocalRef<jbyteArray> signature_ref = |
| + Java_AndroidKeyStore_signWithPrivateKey( |
| + env, private_key_ref, message_ref.obj()); |
| + if (HasException(env) || signature_ref.is_null()) |
| + return false; |
| + |
| + // Write signature to string. |
| + JavaByteArrayToByteVector(env, signature_ref.obj(), signature); |
|
Ryan Sleevi
2013/01/31 03:09:53
Is this guaranteed to be no-fail?
digit1
2013/01/31 17:44:30
Yes.
|
| + return true; |
| +} |
| + |
| +PrivateKeyType GetPrivateKeyType(jobject private_key) { |
| + JNIEnv* env = AttachCurrentThread(); |
| + int type = Java_AndroidKeyStore_getPrivateKeyType( |
| + env, private_key); |
| + return static_cast<PrivateKeyType>(type); |
| +} |
| + |
| +EVP_PKEY* GetOpenSSLSystemHandleForPrivateKey(jobject private_key) { |
| + JNIEnv* env = AttachCurrentThread(); |
| + int pkey = |
| + Java_AndroidKeyStore_getOpenSSLHandleForPrivateKey(env, private_key); |
| + return reinterpret_cast<EVP_PKEY*>(pkey); |
|
Ryan Sleevi
2013/01/31 03:09:53
Please make sure palmer is happy with this. I'm su
digit1
2013/01/31 17:44:30
I've added a technical note. The address is stored
|
| +} |
| + |
| +bool RegisterKeyStore(JNIEnv* env) { |
| + return RegisterNativesImpl(env); |
| +} |
| + |
| +} // namespace android |
| +} // namespace net |