From 2cace25fba3d0606540f0c187f77f3b979af195b Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Wed, 17 Oct 2018 22:29:01 -0400 Subject: add functions for dealing with private keys directly --- android/olm-sdk/src/main/jni/olm_pk.cpp | 124 ++++++++++++++++++++++++++++++++ android/olm-sdk/src/main/jni/olm_pk.h | 2 + 2 files changed, 126 insertions(+) (limited to 'android/olm-sdk/src/main/jni') diff --git a/android/olm-sdk/src/main/jni/olm_pk.cpp b/android/olm-sdk/src/main/jni/olm_pk.cpp index dce62d8..eff57a5 100644 --- a/android/olm-sdk/src/main/jni/olm_pk.cpp +++ b/android/olm-sdk/src/main/jni/olm_pk.cpp @@ -364,6 +364,79 @@ JNIEXPORT void OLM_PK_DECRYPTION_FUNC_DEF(releasePkDecryptionJni)(JNIEnv *env, j } } +JNIEXPORT jbyteArray OLM_PK_DECRYPTION_FUNC_DEF(setPrivateKeyJni)(JNIEnv *env, jobject thiz, jbyteArray key) +{ + jbyteArray publicKeyRet = 0; + jbyte *keyPtr = NULL; + jboolean keyWasCopied = JNI_FALSE; + + const char* errorMessage = NULL; + + OlmPkDecryption* decryptionPtr = getPkDecryptionInstanceId(env, thiz); + + if (!decryptionPtr) + { + LOGE(" ## pkSetPrivateKeyJni(): failure - invalid Decryption ptr=NULL"); + } + else if (!key) + { + LOGE(" ## pkSetPrivateKeyJni(): failure - invalid key"); + errorMessage = "invalid key"; + } + else if (!(keyPtr = env->GetByteArrayElements(key, &keyWasCopied))) + { + LOGE(" ## pkSetPrivateKeyJni(): failure - key JNI allocation OOM"); + errorMessage = "key JNI allocation OOM"; + } + else + { + size_t publicKeyLength = olm_pk_key_length(); + uint8_t *publicKeyPtr = NULL; + size_t keyLength = (size_t)env->GetArrayLength(key); + if (!(publicKeyPtr = (uint8_t*)malloc(publicKeyLength))) + { + LOGE("## pkSetPrivateKeyJni(): failure - public key JNI allocation OOM"); + errorMessage = "public key JNI allocation OOM"; + } + else + { + size_t returnValue = olm_pk_key_from_private( + decryptionPtr, + publicKeyPtr, publicKeyLength, + keyPtr, keyLength + ); + if (returnValue == olm_error()) + { + errorMessage = olm_pk_decryption_last_error(decryptionPtr); + LOGE(" ## pkSetPrivateKeyJni(): failure - olm_pk_key_from_private Msg=%s", errorMessage); + } + else + { + publicKeyRet = env->NewByteArray(publicKeyLength); + env->SetByteArrayRegion( + publicKeyRet, 0, publicKeyLength, (jbyte*)publicKeyPtr + ); + } + } + } + + if (keyPtr) + { + if (keyWasCopied) + { + memset(keyPtr, 0, (size_t)env->GetArrayLength(key)); + } + env->ReleaseByteArrayElements(key, keyPtr, JNI_ABORT); + } + + if (errorMessage) + { + env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); + } + + return publicKeyRet; +} + JNIEXPORT jbyteArray OLM_PK_DECRYPTION_FUNC_DEF(generateKeyJni)(JNIEnv *env, jobject thiz) { size_t randomLength = olm_pk_private_key_length(); @@ -420,6 +493,57 @@ JNIEXPORT jbyteArray OLM_PK_DECRYPTION_FUNC_DEF(generateKeyJni)(JNIEnv *env, job return publicKeyRet; } +JNIEXPORT jbyteArray OLM_PK_DECRYPTION_FUNC_DEF(privateKeyJni)(JNIEnv *env, jobject thiz) +{ + jbyteArray privateKeyRet = 0; + + const char* errorMessage = NULL; + + OlmPkDecryption* decryptionPtr = getPkDecryptionInstanceId(env, thiz); + + if (!decryptionPtr) + { + LOGE(" ## pkPrivateKeyJni(): failure - invalid Decryption ptr=NULL"); + } + else + { + size_t privateKeyLength = olm_pk_private_key_length(); + uint8_t *privateKeyPtr = NULL; + if (!(privateKeyPtr = (uint8_t*)malloc(privateKeyLength))) + { + LOGE("## pkPrivateKeyJni(): failure - private key JNI allocation OOM"); + errorMessage = "private key JNI allocation OOM"; + } + else + { + size_t returnValue = olm_pk_get_private_key( + decryptionPtr, + privateKeyPtr, privateKeyLength + ); + if (returnValue == olm_error()) + { + errorMessage = olm_pk_decryption_last_error(decryptionPtr); + LOGE(" ## pkPrivateKeyJni(): failure - olm_pk_get_private_key Msg=%s", errorMessage); + } + else + { + privateKeyRet = env->NewByteArray(privateKeyLength); + env->SetByteArrayRegion( + privateKeyRet, 0, privateKeyLength, (jbyte*)privateKeyPtr + ); + memset(privateKeyPtr, 0, privateKeyLength); + } + } + } + + if (errorMessage) + { + env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); + } + + return privateKeyRet; +} + JNIEXPORT jbyteArray OLM_PK_DECRYPTION_FUNC_DEF(decryptJni)( JNIEnv *env, jobject thiz, jobject aEncryptedMsg ) { diff --git a/android/olm-sdk/src/main/jni/olm_pk.h b/android/olm-sdk/src/main/jni/olm_pk.h index 984c5f8..5f45462 100644 --- a/android/olm-sdk/src/main/jni/olm_pk.h +++ b/android/olm-sdk/src/main/jni/olm_pk.h @@ -35,7 +35,9 @@ JNIEXPORT jbyteArray OLM_PK_ENCRYPTION_FUNC_DEF(encryptJni)(JNIEnv *env, jobject JNIEXPORT jlong OLM_PK_DECRYPTION_FUNC_DEF(createNewPkDecryptionJni)(JNIEnv *env, jobject thiz); JNIEXPORT void OLM_PK_DECRYPTION_FUNC_DEF(releasePkDecryptionJni)(JNIEnv *env, jobject thiz); +JNIEXPORT jbyteArray OLM_PK_DECRYPTION_FUNC_DEF(setPrivateKeyJni)(JNIEnv *env, jobject thiz, jbyteArray key); JNIEXPORT jbyteArray OLM_PK_DECRYPTION_FUNC_DEF(generateKeyJni)(JNIEnv *env, jobject thiz); +JNIEXPORT jbyteArray OLM_PK_DECRYPTION_FUNC_DEF(privateKeyJni)(JNIEnv *env, jobject thiz); JNIEXPORT jbyteArray OLM_PK_DECRYPTION_FUNC_DEF(decryptJni)(JNIEnv *env, jobject thiz, jobject aEncryptedMsg); #ifdef __cplusplus -- cgit v1.2.3