JNI: fix native exception handling in DH
parent
a3adaef65b
commit
f227a09c75
95
jni/jni_dh.c
95
jni/jni_dh.c
|
@ -56,6 +56,10 @@ Java_com_wolfssl_wolfcrypt_Dh_wc_1InitDhKey(
|
||||||
{
|
{
|
||||||
#ifndef NO_DH
|
#ifndef NO_DH
|
||||||
DhKey* key = (DhKey*) getNativeStruct(env, this);
|
DhKey* key = (DhKey*) getNativeStruct(env, this);
|
||||||
|
if ((*env)->ExceptionOccurred(env)) {
|
||||||
|
/* getNativeStruct may throw exception */
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
wc_InitDhKey(key);
|
wc_InitDhKey(key);
|
||||||
|
|
||||||
|
@ -71,6 +75,10 @@ Java_com_wolfssl_wolfcrypt_Dh_wc_1FreeDhKey(
|
||||||
{
|
{
|
||||||
#ifndef NO_DH
|
#ifndef NO_DH
|
||||||
DhKey* key = (DhKey*) getNativeStruct(env, this);
|
DhKey* key = (DhKey*) getNativeStruct(env, this);
|
||||||
|
if ((*env)->ExceptionOccurred(env)) {
|
||||||
|
/* getNativeStruct may throw exception */
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
wc_FreeDhKey(key);
|
wc_FreeDhKey(key);
|
||||||
|
|
||||||
|
@ -86,11 +94,21 @@ Java_com_wolfssl_wolfcrypt_Dh_wc_1DhSetKey(
|
||||||
{
|
{
|
||||||
#ifndef NO_DH
|
#ifndef NO_DH
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
DhKey* key = (DhKey*) getNativeStruct(env, this);
|
DhKey* key = NULL;
|
||||||
byte* p = getByteArray(env, p_object);
|
byte* p = NULL;
|
||||||
word32 pSz = getByteArrayLength(env, p_object);
|
byte* g = NULL;
|
||||||
byte* g = getByteArray(env, g_object);
|
word32 pSz = 0, gSz = 0;
|
||||||
word32 gSz = getByteArrayLength(env, g_object);
|
|
||||||
|
key = (DhKey*) getNativeStruct(env, this);
|
||||||
|
if ((*env)->ExceptionOccurred(env)) {
|
||||||
|
/* getNativeStruct may throw exception, prevent throwing another */
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
p = getByteArray(env, p_object);
|
||||||
|
pSz = getByteArrayLength(env, p_object);
|
||||||
|
g = getByteArray(env, g_object);
|
||||||
|
gSz = getByteArrayLength(env, g_object);
|
||||||
|
|
||||||
ret = (!key || !p || !g)
|
ret = (!key || !p || !g)
|
||||||
? BAD_FUNC_ARG
|
? BAD_FUNC_ARG
|
||||||
|
@ -118,14 +136,27 @@ Java_com_wolfssl_wolfcrypt_Dh_wc_1DhGenerateKeyPair(
|
||||||
{
|
{
|
||||||
#ifndef NO_DH
|
#ifndef NO_DH
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
DhKey* key = (DhKey*) getNativeStruct(env, this);
|
DhKey* key = NULL;
|
||||||
RNG* rng = (RNG*) getNativeStruct(env, rng_object);
|
RNG* rng = NULL;
|
||||||
byte* priv = NULL;
|
byte* priv = NULL;
|
||||||
|
byte* pub = NULL;
|
||||||
word32 privSz = size;
|
word32 privSz = size;
|
||||||
byte* pub = NULL;
|
word32 pubSz = size;
|
||||||
word32 pubSz = size;
|
|
||||||
int lBitPriv = 0, lBitPub = 0;
|
int lBitPriv = 0, lBitPub = 0;
|
||||||
byte lBit[1] = { 0x00 };
|
byte lBit[1] = { 0x00 };
|
||||||
|
int exceptionThrown = 0;
|
||||||
|
|
||||||
|
key = (DhKey*) getNativeStruct(env, this);
|
||||||
|
if ((*env)->ExceptionOccurred(env)) {
|
||||||
|
/* getNativeStruct may throw exception, prevent throwing another */
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
rng = (RNG*) getNativeStruct(env, rng_object);
|
||||||
|
if ((*env)->ExceptionOccurred(env)) {
|
||||||
|
/* getNativeStruct may throw exception, prevent throwing another */
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!key || !rng || (size < 0))
|
if (!key || !rng || (size < 0))
|
||||||
ret = BAD_FUNC_ARG;
|
ret = BAD_FUNC_ARG;
|
||||||
|
@ -173,12 +204,18 @@ Java_com_wolfssl_wolfcrypt_Dh_wc_1DhGenerateKeyPair(
|
||||||
(*env)->SetByteArrayRegion(env, privateKey, 0, privSz,
|
(*env)->SetByteArrayRegion(env, privateKey, 0, privSz,
|
||||||
(const jbyte*)priv);
|
(const jbyte*)priv);
|
||||||
}
|
}
|
||||||
|
|
||||||
setByteArrayMember(env, this, "privateKey", privateKey);
|
setByteArrayMember(env, this, "privateKey", privateKey);
|
||||||
|
if ((*env)->ExceptionOccurred(env)) {
|
||||||
|
/* if exception raised, skip any additional JNI functions */
|
||||||
|
exceptionThrown = 1;
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
throwWolfCryptException(env, "Failed to allocate privateKey");
|
throwWolfCryptException(env, "Failed to allocate privateKey");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (publicKey) {
|
if (publicKey && (exceptionThrown == 0)) {
|
||||||
if (lBitPub) {
|
if (lBitPub) {
|
||||||
(*env)->SetByteArrayRegion(env, publicKey, 0, 1,
|
(*env)->SetByteArrayRegion(env, publicKey, 0, 1,
|
||||||
(const jbyte*)lBit);
|
(const jbyte*)lBit);
|
||||||
|
@ -188,6 +225,7 @@ Java_com_wolfssl_wolfcrypt_Dh_wc_1DhGenerateKeyPair(
|
||||||
(*env)->SetByteArrayRegion(env, publicKey, 0, pubSz,
|
(*env)->SetByteArrayRegion(env, publicKey, 0, pubSz,
|
||||||
(const jbyte*)pub);
|
(const jbyte*)pub);
|
||||||
}
|
}
|
||||||
|
|
||||||
setByteArrayMember(env, this, "publicKey", publicKey);
|
setByteArrayMember(env, this, "publicKey", publicKey);
|
||||||
} else {
|
} else {
|
||||||
throwWolfCryptException(env, "Failed to allocate publicKey");
|
throwWolfCryptException(env, "Failed to allocate publicKey");
|
||||||
|
@ -218,9 +256,18 @@ Java_com_wolfssl_wolfcrypt_Dh_wc_1DhCheckPubKey(
|
||||||
{
|
{
|
||||||
#ifndef NO_DH
|
#ifndef NO_DH
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
DhKey* key = (DhKey*) getNativeStruct(env, this);
|
DhKey* key = NULL;
|
||||||
byte* pub = getByteArray(env, pub_object);
|
byte* pub = NULL;
|
||||||
word32 pubSz = getByteArrayLength(env, pub_object);
|
word32 pubSz = 0;
|
||||||
|
|
||||||
|
key = (DhKey*) getNativeStruct(env, this);
|
||||||
|
if ((*env)->ExceptionOccurred(env)) {
|
||||||
|
/* getNativeStruct may throw exception, prevent throwing another */
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub = getByteArray(env, pub_object);
|
||||||
|
pubSz = getByteArrayLength(env, pub_object);
|
||||||
|
|
||||||
ret = (!key || !pub)
|
ret = (!key || !pub)
|
||||||
? BAD_FUNC_ARG
|
? BAD_FUNC_ARG
|
||||||
|
@ -247,13 +294,23 @@ Java_com_wolfssl_wolfcrypt_Dh_wc_1DhAgree(
|
||||||
|
|
||||||
#ifndef NO_DH
|
#ifndef NO_DH
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
DhKey* key = (DhKey*) getNativeStruct(env, this);
|
DhKey* key = NULL;
|
||||||
byte* priv = getByteArray(env, priv_object);
|
byte* priv = NULL;
|
||||||
word32 privSz = getByteArrayLength(env, priv_object);
|
byte* pub = NULL;
|
||||||
byte* pub = getByteArray(env, pub_object);
|
|
||||||
word32 pubSz = getByteArrayLength(env, pub_object);
|
|
||||||
byte* secret = NULL;
|
byte* secret = NULL;
|
||||||
word32 secretSz = pubSz;
|
word32 privSz = 0, pubSz = 0, secretSz = 0;
|
||||||
|
|
||||||
|
key = (DhKey*) getNativeStruct(env, this);
|
||||||
|
if ((*env)->ExceptionOccurred(env)) {
|
||||||
|
/* getNativeStruct may throw exception, prevent throwing another */
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
priv = getByteArray(env, priv_object);
|
||||||
|
privSz = getByteArrayLength(env, priv_object);
|
||||||
|
pub = getByteArray(env, pub_object);
|
||||||
|
pubSz = getByteArrayLength(env, pub_object);
|
||||||
|
secretSz = pubSz;
|
||||||
|
|
||||||
secret = XMALLOC(pubSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
secret = XMALLOC(pubSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
if (secret == NULL) {
|
if (secret == NULL) {
|
||||||
|
|
Loading…
Reference in New Issue