Skip to content

Commit

Permalink
add support for spaces around '=' with x509 name print
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobBarthelmeh committed May 31, 2024
1 parent ff76264 commit 2caee1c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
23 changes: 18 additions & 5 deletions src/x509.c
Original file line number Diff line number Diff line change
Expand Up @@ -13051,13 +13051,16 @@ static int wolfSSL_EscapeString_RFC2253(char* in, word32 inSz,
* RFC22523 currently implemented.
* XN_FLAG_DN_REV - print name reversed. Automatically done by
* XN_FLAG_RFC2253.
* XN_FLAG_SPC_EQ - spaces before and after '=' character
*
* Returns WOLFSSL_SUCCESS (1) on success, WOLFSSL_FAILURE (0) on failure.
*/
int wolfSSL_X509_NAME_print_ex(WOLFSSL_BIO* bio, WOLFSSL_X509_NAME* name,
int indent, unsigned long flags)
{
int i, count = 0, nameStrSz = 0, escapeSz = 0;
int eqSpace = 0;
char eqStr[4];
char* tmp = NULL;
char* nameStr = NULL;
const char *buf = NULL;
Expand All @@ -13070,6 +13073,15 @@ int wolfSSL_X509_NAME_print_ex(WOLFSSL_BIO* bio, WOLFSSL_X509_NAME* name,
if ((name == NULL) || (name->sz == 0) || (bio == NULL))
return WOLFSSL_FAILURE;

XMEMSET(eqStr, 0, sizeof(eqStr));
if (flags & XN_FLAG_SPC_EQ) {
eqSpace = 2;
XSTRNCPY(eqStr, " = ", 4);
}
else {
XSTRNCPY(eqStr, "=", 4);
}

for (i = 0; i < indent; i++) {
if (wolfSSL_BIO_write(bio, " ", 1) != 1)
return WOLFSSL_FAILURE;
Expand Down Expand Up @@ -13114,32 +13126,33 @@ int wolfSSL_X509_NAME_print_ex(WOLFSSL_BIO* bio, WOLFSSL_X509_NAME* name,
if (len == 0 || buf == NULL)
return WOLFSSL_FAILURE;

tmpSz = nameStrSz + len + 4; /* + 4 for '=', comma space and '\0'*/
/* + 4 for '=', comma space and '\0'*/
tmpSz = nameStrSz + len + 4 + eqSpace;
tmp = (char*)XMALLOC(tmpSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (tmp == NULL) {
return WOLFSSL_FAILURE;
}

if (i < count - 1) {
if (XSNPRINTF(tmp, (size_t)tmpSz, "%s=%s, ", buf, nameStr)
if (XSNPRINTF(tmp, (size_t)tmpSz, "%s%s%s, ", buf, eqStr, nameStr)
>= tmpSz)
{
WOLFSSL_MSG("buffer overrun");
XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return WOLFSSL_FAILURE;
}

tmpSz = len + nameStrSz + 3; /* 3 for '=', comma space */
tmpSz = len + nameStrSz + 3 + eqSpace; /* 3 for '=', comma space */
}
else {
if (XSNPRINTF(tmp, (size_t)tmpSz, "%s=%s", buf, nameStr)
if (XSNPRINTF(tmp, (size_t)tmpSz, "%s%s%s", buf, eqStr, nameStr)
>= tmpSz)
{
WOLFSSL_MSG("buffer overrun");
XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return WOLFSSL_FAILURE;
}
tmpSz = len + nameStrSz + 1; /* 1 for '=' */
tmpSz = len + nameStrSz + 1 + eqSpace; /* 1 for '=' */
if (bio->type != WOLFSSL_BIO_FILE && bio->type != WOLFSSL_BIO_MEMORY)
++tmpSz; /* include the terminating null when not writing to a
* file.
Expand Down
12 changes: 12 additions & 0 deletions tests/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -33467,6 +33467,7 @@ static int test_wolfSSL_X509_NAME_print_ex(void)
X509_NAME* name = NULL;

const char* expNormal = "C=US, CN=wolfssl.com";
const char* expEqSpace = "C = US, CN = wolfssl.com";
const char* expReverse = "CN=wolfssl.com, C=US";

const char* expNotEscaped = "C= US,+\"\\ , CN=#wolfssl.com<>;";
Expand Down Expand Up @@ -33524,6 +33525,17 @@ static int test_wolfSSL_X509_NAME_print_ex(void)
BIO_free(membio);
membio = NULL;

/* Test with XN_FLAG_ONELINE which should enable XN_FLAG_SPC_EQ for
spaces aroun '=' */
ExpectNotNull(membio = BIO_new(BIO_s_mem()));
ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, XN_FLAG_ONELINE),
WOLFSSL_SUCCESS);
ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
ExpectIntEQ(memSz, XSTRLEN(expEqSpace));
ExpectIntEQ(XSTRNCMP((char*)mem, expEqSpace, XSTRLEN(expEqSpace)), 0);
BIO_free(membio);
membio = NULL;

/* Test flags: XN_FLAG_RFC2253 - should be reversed */
ExpectNotNull(membio = BIO_new(BIO_s_mem()));
ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
Expand Down
2 changes: 1 addition & 1 deletion wolfssl/openssl/x509.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
#define X509_FLAG_NO_IDS (1UL << 12)

#define XN_FLAG_FN_SN 0
#define XN_FLAG_ONELINE 0
#define XN_FLAG_COMPAT 0
#define XN_FLAG_RFC2253 1
#define XN_FLAG_SEP_COMMA_PLUS (1 << 16)
Expand All @@ -68,6 +67,7 @@
#define XN_FLAG_FN_ALIGN (1 << 25)

#define XN_FLAG_MULTILINE 0xFFFF
#define XN_FLAG_ONELINE (XN_FLAG_SEP_CPLUS_SPC | XN_FLAG_SPC_EQ | XN_FLAG_FN_SN)

/*
* All of these aren't actually used in wolfSSL. Some are included to
Expand Down

0 comments on commit 2caee1c

Please sign in to comment.