Skip to content

Commit b514258

Browse files
committed
Zero private key bytes on release
1 parent 76cbb27 commit b514258

6 files changed

Lines changed: 66 additions & 14 deletions

File tree

native/com_wolfssl_WolfSSLCRL.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,14 +368,15 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLCRL_X509_1CRL_1sign
368368
const WOLFSSL_EVP_MD* md = NULL;
369369
unsigned char* rsaPrivBuf = NULL;
370370
const char* mdName = NULL;
371+
jboolean isCopy = JNI_FALSE;
371372
int ret = WOLFSSL_SUCCESS;
372373
(void)jcl;
373374

374375
if (jenv == NULL || crl == NULL || keyBytes == NULL) {
375376
return WOLFSSL_FAILURE;
376377
}
377378

378-
keyBuf = (byte*)(*jenv)->GetByteArrayElements(jenv, keyBytes, NULL);
379+
keyBuf = (byte*)(*jenv)->GetByteArrayElements(jenv, keyBytes, &isCopy);
379380
if ((*jenv)->ExceptionOccurred(jenv)) {
380381
(*jenv)->ExceptionClear(jenv);
381382
return WOLFSSL_FAILURE;
@@ -471,6 +472,15 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLCRL_X509_1CRL_1sign
471472
(*jenv)->ReleaseStringUTFChars(jenv, digestAlg, mdName);
472473
}
473474
if (keyBuf != NULL) {
475+
/* Only zero if JVM made a copy */
476+
if (isCopy == JNI_TRUE) {
477+
#if (LIBWOLFSSL_VERSION_HEX >= 0x05008004) && \
478+
!defined(WOLFSSL_NO_FORCE_ZERO)
479+
wc_ForceZero(keyBuf, keySz);
480+
#else
481+
XMEMSET(keyBuf, 0, keySz);
482+
#endif
483+
}
474484
(*jenv)->ReleaseByteArrayElements(jenv, keyBytes, (jbyte*)keyBuf,
475485
JNI_ABORT);
476486
}

native/com_wolfssl_WolfSSLCertRequest.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLCertRequest_X509_1REQ_1sign
247247
const WOLFSSL_EVP_MD* md = NULL;
248248
unsigned char* rsaPrivBuf = NULL;
249249
const char* mdName = NULL;
250+
jboolean isCopy = JNI_FALSE;
250251
int ret = WOLFSSL_SUCCESS;
251252
(void)jcl;
252253

@@ -255,7 +256,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLCertRequest_X509_1REQ_1sign
255256
return WOLFSSL_FAILURE;
256257
}
257258

258-
keyBuf = (byte*)(*jenv)->GetByteArrayElements(jenv, keyBytes, NULL);
259+
keyBuf = (byte*)(*jenv)->GetByteArrayElements(jenv, keyBytes, &isCopy);
259260
keySz = (*jenv)->GetArrayLength(jenv, keyBytes);
260261

261262
if (keyBuf == NULL || keySz == 0) {
@@ -343,6 +344,15 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLCertRequest_X509_1REQ_1sign
343344
XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
344345
}
345346
if (keyBuf != NULL) {
347+
/* Only zero if JVM made a copy */
348+
if (isCopy == JNI_TRUE) {
349+
#if (LIBWOLFSSL_VERSION_HEX >= 0x05008004) && \
350+
!defined(WOLFSSL_NO_FORCE_ZERO)
351+
wc_ForceZero(keyBuf, keySz);
352+
#else
353+
XMEMSET(keyBuf, 0, keySz);
354+
#endif
355+
}
346356
(*jenv)->ReleaseByteArrayElements(jenv, keyBytes,
347357
(jbyte*)keyBuf, JNI_ABORT);
348358
}

native/com_wolfssl_WolfSSLCertificate.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLCertificate_X509_1sign
888888
const WOLFSSL_EVP_MD* md = NULL;
889889
unsigned char* rsaPrivBuf = NULL;
890890
const char* mdName = NULL;
891+
jboolean isCopy = JNI_FALSE;
891892

892893
int ret = WOLFSSL_SUCCESS;
893894
(void)jcl;
@@ -897,7 +898,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLCertificate_X509_1sign
897898
return WOLFSSL_FAILURE;
898899
}
899900

900-
fileBuf = (byte*)(*jenv)->GetByteArrayElements(jenv, fileBytes, NULL);
901+
fileBuf = (byte*)(*jenv)->GetByteArrayElements(jenv, fileBytes, &isCopy);
901902
fileSz = (*jenv)->GetArrayLength(jenv, fileBytes);
902903

903904
if (fileBuf == NULL || fileSz == 0) {
@@ -990,6 +991,15 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLCertificate_X509_1sign
990991
XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
991992
}
992993
if (fileBuf != NULL) {
994+
/* Only zero if JVM made a copy */
995+
if (isCopy == JNI_TRUE) {
996+
#if (LIBWOLFSSL_VERSION_HEX >= 0x05008004) && \
997+
!defined(WOLFSSL_NO_FORCE_ZERO)
998+
wc_ForceZero(fileBuf, fileSz);
999+
#else
1000+
XMEMSET(fileBuf, 0, fileSz);
1001+
#endif
1002+
}
9931003
(*jenv)->ReleaseByteArrayElements(jenv, fileBytes,
9941004
(jbyte*)fileBuf, JNI_ABORT);
9951005
}

src/java/com/wolfssl/WolfSSLCRL.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.nio.charset.StandardCharsets;
2929
import java.text.ParseException;
3030
import java.text.SimpleDateFormat;
31+
import java.util.Arrays;
3132
import java.util.Calendar;
3233
import java.util.Date;
3334
import java.util.Locale;
@@ -561,9 +562,13 @@ else if (key instanceof ECPrivateKey) {
561562
"PrivateKey does not support encoding");
562563
}
563564

564-
synchronized (crlLock) {
565-
ret = X509_CRL_sign(this.crlPtr, evpKeyType, encodedKey,
566-
WolfSSL.SSL_FILETYPE_ASN1, digestAlg);
565+
try {
566+
synchronized (crlLock) {
567+
ret = X509_CRL_sign(this.crlPtr, evpKeyType, encodedKey,
568+
WolfSSL.SSL_FILETYPE_ASN1, digestAlg);
569+
}
570+
} finally {
571+
Arrays.fill(encodedKey, (byte)0);
567572
}
568573

569574
return ret;

src/java/com/wolfssl/WolfSSLCertRequest.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.File;
2424
import java.io.IOException;
2525
import java.nio.charset.Charset;
26+
import java.util.Arrays;
2627
import java.security.PublicKey;
2728
import java.security.PrivateKey;
2829
import java.security.interfaces.RSAPublicKey;
@@ -634,7 +635,11 @@ public void signRequest(String filePath, int keyType, int format,
634635
"Failed to read bytes from file: " + filePath);
635636
}
636637

637-
signRequest(fileBytes, keyType, format, digestAlg);
638+
try {
639+
signRequest(fileBytes, keyType, format, digestAlg);
640+
} finally {
641+
Arrays.fill(fileBytes, (byte)0);
642+
}
638643
}
639644

640645
/**
@@ -751,9 +756,13 @@ else if (key instanceof ECPrivateKey) {
751756
throw new WolfSSLException("PrivateKey does not support encoding");
752757
}
753758

754-
synchronized (x509ReqLock) {
755-
ret = X509_REQ_sign(this.x509ReqPtr, evpKeyType, encodedKey,
756-
WolfSSL.SSL_FILETYPE_ASN1, digestAlg);
759+
try {
760+
synchronized (x509ReqLock) {
761+
ret = X509_REQ_sign(this.x509ReqPtr, evpKeyType, encodedKey,
762+
WolfSSL.SSL_FILETYPE_ASN1, digestAlg);
763+
}
764+
} finally {
765+
Arrays.fill(encodedKey, (byte)0);
757766
}
758767

759768
if (ret != WolfSSL.SSL_SUCCESS) {

src/java/com/wolfssl/WolfSSLCertificate.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,7 +1345,11 @@ public void signCert(String filePath, int keyType, int format,
13451345
"Failed to read bytes from file: " + filePath);
13461346
}
13471347

1348-
signCert(fileBytes, keyType, format, digestAlg);
1348+
try {
1349+
signCert(fileBytes, keyType, format, digestAlg);
1350+
} finally {
1351+
Arrays.fill(fileBytes, (byte)0);
1352+
}
13491353
}
13501354

13511355
/**
@@ -1461,9 +1465,13 @@ else if (key instanceof ECPrivateKey) {
14611465
throw new WolfSSLException("PrivateKey does not support encoding");
14621466
}
14631467

1464-
synchronized (x509Lock) {
1465-
ret = X509_sign(this.x509Ptr, evpKeyType, encodedKey,
1466-
WolfSSL.SSL_FILETYPE_ASN1, digestAlg);
1468+
try {
1469+
synchronized (x509Lock) {
1470+
ret = X509_sign(this.x509Ptr, evpKeyType, encodedKey,
1471+
WolfSSL.SSL_FILETYPE_ASN1, digestAlg);
1472+
}
1473+
} finally {
1474+
Arrays.fill(encodedKey, (byte)0);
14671475
}
14681476

14691477
if (ret != WolfSSL.SSL_SUCCESS) {

0 commit comments

Comments
 (0)