Skip to content

Commit 3fd0b1b

Browse files
committed
WolfSSLCertificate: let Java do the modified UTF-8 conversion for toString()
1 parent 963f556 commit 3fd0b1b

3 files changed

Lines changed: 38 additions & 10 deletions

File tree

native/com_wolfssl_WolfSSLCertificate.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -418,12 +418,13 @@ JNIEXPORT jstring JNICALL Java_com_wolfssl_WolfSSLCertificate_X509_1get_1signatu
418418
return (*jenv)->NewStringUTF(jenv, oid);
419419
}
420420

421-
JNIEXPORT jstring JNICALL Java_com_wolfssl_WolfSSLCertificate_X509_1print
421+
JNIEXPORT jbyteArray JNICALL Java_com_wolfssl_WolfSSLCertificate_X509_1print
422422
(JNIEnv* jenv, jclass jcl, jlong x509Ptr)
423423
{
424424
WOLFSSL_BIO* bio;
425-
jstring ret = NULL;
425+
int sz = 0;
426426
const char* mem = NULL;
427+
jbyteArray memArr = NULL;
427428
WOLFSSL_X509* x509 = (WOLFSSL_X509*)(uintptr_t)x509Ptr;
428429
(void)jcl;
429430

@@ -441,12 +442,25 @@ JNIEXPORT jstring JNICALL Java_com_wolfssl_WolfSSLCertificate_X509_1print
441442
return NULL;
442443
}
443444

444-
wolfSSL_BIO_get_mem_data(bio, &mem);
445-
if (mem != NULL) {
446-
ret = (*jenv)->NewStringUTF(jenv, mem);
445+
sz = wolfSSL_BIO_get_mem_data(bio, &mem);
446+
if (sz > 0 && mem != NULL) {
447+
448+
memArr = (*jenv)->NewByteArray(jenv, sz);
449+
if (memArr == NULL) {
450+
wolfSSL_BIO_free(bio);
451+
return NULL;
452+
}
453+
454+
(*jenv)->SetByteArrayRegion(jenv, memArr, 0, sz, (jbyte*)mem);
455+
if ((*jenv)->ExceptionOccurred(jenv)) {
456+
/* failed to set byte region */
457+
(*jenv)->DeleteLocalRef(jenv, memArr);
458+
wolfSSL_BIO_free(bio);
459+
return NULL;
460+
}
447461
}
448462
wolfSSL_BIO_free(bio);
449-
return ret;
463+
return memArr;
450464
}
451465

452466
JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLCertificate_X509_1get_1isCA

native/com_wolfssl_WolfSSLCertificate.h

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/java/com/wolfssl/WolfSSLCertificate.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.io.IOException;
2626
import java.io.InputStream;
2727
import java.io.ByteArrayInputStream;
28+
import java.nio.charset.Charset;
2829
import java.math.BigInteger;
2930
import java.text.ParseException;
3031
import java.text.SimpleDateFormat;
@@ -62,7 +63,7 @@ public class WolfSSLCertificate {
6263
static native byte[] X509_get_signature(long x509);
6364
static native String X509_get_signature_type(long x509);
6465
static native String X509_get_signature_OID(long x509);
65-
static native String X509_print(long x509);
66+
static native byte[] X509_print(long x509);
6667
static native int X509_get_isCA(long x509);
6768
static native String X509_get_subject_name(long x509);
6869
static native String X509_get_issuer_name(long x509);
@@ -600,7 +601,20 @@ public X509Certificate getX509Certificate()
600601

601602
@Override
602603
public String toString() {
603-
return X509_print(this.x509Ptr);
604+
605+
byte[] x509Text;
606+
607+
if (this.active == false) {
608+
return super.toString();
609+
}
610+
611+
x509Text = X509_print(this.x509Ptr);
612+
if (x509Text != null) {
613+
/* let Java do the modified UTF-8 conversion */
614+
return new String(x509Text, Charset.forName("UTF-8"));
615+
}
616+
617+
return super.toString();
604618
}
605619

606620
/**

0 commit comments

Comments
 (0)