This source file includes following definitions.
- GetRSAKeyModulus
- GetDSAKeyParamQ
- GetECKeyOrder
- GetPrivateKeyEncodedBytes
- RawSignDigestWithPrivateKey
- GetPrivateKeyType
- GetOpenSSLSystemHandleForPrivateKey
- ReleaseKey
- RegisterKeyStore
#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"
#include "net/android/android_private_key.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 GetRSAKeyModulus(
jobject private_key_ref,
std::vector<uint8>* result) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jbyteArray> modulus_ref =
Java_AndroidKeyStore_getRSAKeyModulus(env,
GetKeyStore(private_key_ref).obj(),
private_key_ref);
if (modulus_ref.is_null())
return false;
JavaByteArrayToByteVector(env, modulus_ref.obj(), result);
return true;
}
bool GetDSAKeyParamQ(jobject private_key_ref,
std::vector<uint8>* result) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jbyteArray> q_ref =
Java_AndroidKeyStore_getDSAKeyParamQ(
env,
GetKeyStore(private_key_ref).obj(),
private_key_ref);
if (q_ref.is_null())
return false;
JavaByteArrayToByteVector(env, q_ref.obj(), result);
return true;
}
bool GetECKeyOrder(jobject private_key_ref,
std::vector<uint8>* result) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jbyteArray> order_ref =
Java_AndroidKeyStore_getECKeyOrder(
env,
GetKeyStore(private_key_ref).obj(),
private_key_ref);
if (order_ref.is_null())
return false;
JavaByteArrayToByteVector(env, order_ref.obj(), result);
return true;
}
bool GetPrivateKeyEncodedBytes(jobject private_key_ref,
std::vector<uint8>* result) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jbyteArray> encoded_ref =
Java_AndroidKeyStore_getPrivateKeyEncodedBytes(
env,
GetKeyStore(private_key_ref).obj(),
private_key_ref);
if (encoded_ref.is_null())
return false;
JavaByteArrayToByteVector(env, encoded_ref.obj(), result);
return true;
}
bool RawSignDigestWithPrivateKey(
jobject private_key_ref,
const base::StringPiece& digest,
std::vector<uint8>* signature) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jbyteArray> digest_ref =
ToJavaByteArray(env,
reinterpret_cast<const uint8*>(digest.data()),
digest.length());
DCHECK(!digest_ref.is_null());
ScopedJavaLocalRef<jbyteArray> signature_ref =
Java_AndroidKeyStore_rawSignDigestWithPrivateKey(
env,
GetKeyStore(private_key_ref).obj(),
private_key_ref,
digest_ref.obj());
if (HasException(env) || signature_ref.is_null())
return false;
JavaByteArrayToByteVector(env, signature_ref.obj(), signature);
return true;
}
PrivateKeyType GetPrivateKeyType(jobject private_key_ref) {
JNIEnv* env = AttachCurrentThread();
int type = Java_AndroidKeyStore_getPrivateKeyType(
env,
GetKeyStore(private_key_ref).obj(),
private_key_ref);
return static_cast<PrivateKeyType>(type);
}
EVP_PKEY* GetOpenSSLSystemHandleForPrivateKey(jobject private_key_ref) {
JNIEnv* env = AttachCurrentThread();
long pkey = Java_AndroidKeyStore_getOpenSSLHandleForPrivateKey(
env,
GetKeyStore(private_key_ref).obj(),
private_key_ref);
return reinterpret_cast<EVP_PKEY*>(pkey);
}
void ReleaseKey(jobject private_key_ref) {
JNIEnv* env = AttachCurrentThread();
Java_AndroidKeyStore_releaseKey(env,
GetKeyStore(private_key_ref).obj(),
private_key_ref);
env->DeleteGlobalRef(private_key_ref);
}
bool RegisterKeyStore(JNIEnv* env) {
return RegisterNativesImpl(env);
}
}
}