F-13477: implement WolfSSLX509Name.toString via wolfSSL_X509_NAME_oneline

pull/420/head
Chris Conlon 2026-09-18 11:48:00 -06:00
parent 53368ea5d9
commit 32c2137341
4 changed files with 70 additions and 3 deletions

View File

@ -129,3 +129,34 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLX509Name_X509_1NAME_1add_1entry_1
#endif
}
JNIEXPORT jstring JNICALL Java_com_wolfssl_WolfSSLX509Name_X509_1NAME_1oneline
(JNIEnv* jenv, jclass jcl, jlong x509NamePtr)
{
#if !defined(WOLFCRYPT_ONLY) && !defined(NO_CERTS) && \
(defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL))
WOLFSSL_X509_NAME* ptr = (WOLFSSL_X509_NAME*)(uintptr_t)x509NamePtr;
char* name = NULL;
jstring ret = NULL;
(void)jcl;
if (jenv == NULL || ptr == NULL) {
return NULL;
}
name = wolfSSL_X509_NAME_oneline(ptr, NULL, 0);
if (name == NULL) {
return NULL;
}
ret = (*jenv)->NewStringUTF(jenv, name);
XFREE(name, NULL, DYNAMIC_TYPE_OPENSSL);
return ret;
#else
(void)jenv;
(void)jcl;
(void)x509NamePtr;
return NULL;
#endif
}

View File

@ -33,6 +33,14 @@ JNIEXPORT void JNICALL Java_com_wolfssl_WolfSSLX509Name_X509_1NAME_1free
JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLX509Name_X509_1NAME_1add_1entry_1by_1txt
(JNIEnv *, jclass, jlong, jstring, jint, jbyteArray, jint, jint, jint);
/*
* Class: com_wolfssl_WolfSSLX509Name
* Method: X509_NAME_oneline
* Signature: (J)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_wolfssl_WolfSSLX509Name_X509_1NAME_1oneline
(JNIEnv *, jclass, jlong);
#ifdef __cplusplus
}
#endif

View File

@ -68,6 +68,7 @@ public class WolfSSLX509Name {
static native void X509_NAME_free(long x509Name);
static native int X509_NAME_add_entry_by_txt(long x509Name, String field,
int type, byte[] entry, int len, int loc, int set);
static native String X509_NAME_oneline(long x509Name);
/**
* Create new empty WolfSSLX509Name object.
@ -1414,10 +1415,12 @@ public class WolfSSLX509Name {
if (this.active == false) {
return "";
}
}
/* TODO: wrap wolfSSL_X509_NAME_oneline() */
return "";
synchronized (x509NameLock) {
String name = X509_NAME_oneline(this.x509NamePtr);
return (name == null) ? "" : name;
}
}
}
/**

View File

@ -28,6 +28,7 @@ import org.junit.Test;
import org.junit.rules.TestRule;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
@ -106,6 +107,30 @@ public class WolfSSLX509NameTest {
}
}
@Test
public void test_toString_ReturnsPopulatedName()
throws WolfSSLException {
WolfSSLX509Name name =
new WolfSSLX509Name("CN=wolfssl.com,O=wolfSSL Inc.,C=US");
try {
String str = name.toString();
assertNotNull(str);
assertTrue("toString() returned empty for a populated name",
!str.isEmpty());
assertTrue("toString() missing commonName: " + str,
str.contains("wolfssl.com"));
assertTrue("toString() missing organizationName: " + str,
str.contains("wolfSSL Inc."));
assertTrue("toString() missing countryName: " + str,
str.contains("US"));
} finally {
name.free();
}
}
@Test
public void test_String_RFC2253_ExtendedAttributes()
throws WolfSSLException {