Skip to content

Commit 0eb9615

Browse files
committed
JNI: clarify exception thrown when privateKey handling fails in DhGenerateKeyPair()
add cleanup label
1 parent dae76c5 commit 0eb9615

1 file changed

Lines changed: 31 additions & 33 deletions

File tree

jni/jni_dh.c

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ Java_com_wolfssl_wolfcrypt_Dh_wc_1DhGenerateKeyPair(
190190
word32 pubSz = size;
191191
int lBitPriv = 0, lBitPub = 0;
192192
byte lBit[1] = { 0x00 };
193-
int exceptionThrown = 0;
194193

195194
key = (DhKey*) getNativeStruct(env, this);
196195
if ((*env)->ExceptionOccurred(env)) {
@@ -245,44 +244,42 @@ Java_com_wolfssl_wolfcrypt_Dh_wc_1DhGenerateKeyPair(
245244
}
246245

247246
jbyteArray privateKey = (*env)->NewByteArray(env, lBitPriv + privSz);
247+
if (!privateKey) {
248+
(*env)->ExceptionClear(env);
249+
throwOutOfMemoryException(env, "Failed to allocate privateKey");
250+
goto cleanup;
251+
}
248252
jbyteArray publicKey = (*env)->NewByteArray(env, lBitPub + pubSz);
249-
250-
if (privateKey) {
251-
if (lBitPriv) {
252-
(*env)->SetByteArrayRegion(env, privateKey, 0, 1,
253-
(const jbyte*)lBit);
254-
(*env)->SetByteArrayRegion(env, privateKey, 1, privSz,
255-
(const jbyte*)priv);
256-
} else {
257-
(*env)->SetByteArrayRegion(env, privateKey, 0, privSz,
258-
(const jbyte*)priv);
259-
}
260-
261-
setByteArrayMember(env, this, "privateKey", privateKey);
262-
if ((*env)->ExceptionOccurred(env)) {
263-
/* if exception raised, skip any additional JNI functions */
264-
exceptionThrown = 1;
265-
}
266-
267-
} else {
268-
throwWolfCryptException(env, "Failed to allocate privateKey");
269-
exceptionThrown = 1;
253+
if (!publicKey) {
254+
(*env)->ExceptionClear(env);
255+
throwOutOfMemoryException(env, "Failed to allocate publicKey");
256+
goto cleanup;
270257
}
258+
if (lBitPriv) {
259+
(*env)->SetByteArrayRegion(env, privateKey, 0, 1,
260+
(const jbyte*)lBit);
261+
(*env)->SetByteArrayRegion(env, privateKey, 1, privSz,
262+
(const jbyte*)priv);
263+
}
264+
else {
265+
(*env)->SetByteArrayRegion(env, privateKey, 0, privSz,
266+
(const jbyte*)priv);
267+
}
268+
setByteArrayMember(env, this, "privateKey", privateKey);
271269

272-
if (publicKey && (exceptionThrown == 0)) {
270+
/* if exception raised, skip any additional JNI functions */
271+
if (!(*env)->ExceptionOccurred(env)) {
273272
if (lBitPub) {
274-
(*env)->SetByteArrayRegion(env, publicKey, 0, 1,
275-
(const jbyte*)lBit);
276-
(*env)->SetByteArrayRegion(env, publicKey, 1, pubSz,
277-
(const jbyte*)pub);
278-
} else {
279-
(*env)->SetByteArrayRegion(env, publicKey, 0, pubSz,
280-
(const jbyte*)pub);
273+
(*env)->SetByteArrayRegion(env, publicKey, 0,
274+
1, (const jbyte*)lBit);
275+
(*env)->SetByteArrayRegion(env, publicKey, 1,
276+
pubSz, (const jbyte*)pub);
277+
}
278+
else {
279+
(*env)->SetByteArrayRegion(env, publicKey, 0,
280+
pubSz, (const jbyte*)pub);
281281
}
282-
283282
setByteArrayMember(env, this, "publicKey", publicKey);
284-
} else {
285-
throwWolfCryptException(env, "Failed to allocate publicKey");
286283
}
287284
} else {
288285
throwWolfCryptExceptionFromError(env, ret);
@@ -295,6 +292,7 @@ Java_com_wolfssl_wolfcrypt_Dh_wc_1DhGenerateKeyPair(
295292
LogStr("public[%u]: [%p]\n", pubSz, pub);
296293
LogHex(pub, 0, pubSz);
297294

295+
cleanup:
298296
if (priv != NULL) {
299297
#if (LIBWOLFSSL_VERSION_HEX >= 0x05008004) && \
300298
!defined(WOLFSSL_NO_FORCE_ZERO)

0 commit comments

Comments
 (0)