From 09bf70604b79cac6b3f0465b0263ef1246365335 Mon Sep 17 00:00:00 2001 From: Eric Blankenhorn Date: Wed, 15 Jul 2026 15:48:41 -0500 Subject: [PATCH 1/4] Parse x500UniqueIdentifier (OID 2.5.4.45) in certificate DN --- src/ssl_asn1.c | 2 + tests/api.c | 155 ++++++++++++++++++++++++++++++++++++++++ wolfcrypt/src/asn.c | 36 +++++++++- wolfssl/wolfcrypt/asn.h | 10 +++ 4 files changed, 201 insertions(+), 2 deletions(-) diff --git a/src/ssl_asn1.c b/src/ssl_asn1.c index 9768eb4e0d..b228266ea1 100644 --- a/src/ssl_asn1.c +++ b/src/ssl_asn1.c @@ -5068,6 +5068,8 @@ const WOLFSSL_ObjectInfo wolfssl_object_info[] = { /* Rarely used and legacy attributes. */ { WC_NID_userId, WC_NAME_USER_ID_OID, oidCertNameType, "UID", "userId"}, + { WC_NID_x500UniqueIdentifier, WC_NID_x500UniqueIdentifier, oidCertNameType, + "x500UniqueIdentifier", "X500 unique identifier"}, { WC_NID_title, WC_NAME_TITLE_OID, oidCertNameType, "title", "title"}, { WC_NID_description, WC_NAME_DESCRIPTION_OID, oidCertNameType, "description", "description"}, diff --git a/tests/api.c b/tests/api.c index 14f10ca874..008b9acee1 100644 --- a/tests/api.c +++ b/tests/api.c @@ -671,6 +671,160 @@ defined(USE_CERT_BUFFERS_2048) return EXPECT_RESULT(); } +/* Regression test for parsing an x500UniqueIdentifier (OID 2.5.4.45) in a + * subject/issuer DN. Its value is a BIT STRING, which the RDN parser used to + * reject with ASN_PARSE_E. */ +static int test_wc_ParseCert_uniqueIdentifier(void) +{ + EXPECT_DECLS; +#if !defined(NO_CERTS) && defined(WOLFSSL_ASN_TEMPLATE) && defined(HAVE_ECC) + /* Self-signed ECDSA P-256 certificate whose subject and issuer DN each + * hold an x500UniqueIdentifier (OID 2.5.4.45) encoded as a BIT STRING, + * as issued for LEAF physical-access credentials. Built with asn1crypto + * and pyca/cryptography (verifies clean with OpenSSL), so the encoding is + * an independent oracle, not produced by wolfSSL. */ + static const byte leafUniqueIdDer[] = { + 0x30, 0x82, 0x01, 0x8C, 0x30, 0x82, 0x01, 0x32, 0xA0, 0x03, 0x02, 0x01, + 0x02, 0x02, 0x06, 0x0E, 0xAF, 0x12, 0x34, 0x56, 0x78, 0x30, 0x0A, 0x06, + 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30, 0x4D, 0x31, + 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, + 0x31, 0x11, 0x30, 0x0F, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x08, 0x54, + 0x65, 0x73, 0x74, 0x20, 0x4F, 0x72, 0x67, 0x31, 0x17, 0x30, 0x15, 0x06, + 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0E, 0x4C, 0x45, 0x41, 0x46, 0x20, 0x54, + 0x65, 0x73, 0x74, 0x20, 0x43, 0x61, 0x72, 0x64, 0x31, 0x12, 0x30, 0x10, + 0x06, 0x03, 0x55, 0x04, 0x2D, 0x03, 0x09, 0x00, 0x01, 0x23, 0x45, 0x67, + 0x89, 0xAB, 0xCD, 0xEF, 0x30, 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x31, + 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5A, 0x17, 0x0D, 0x34, + 0x39, 0x31, 0x32, 0x33, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5A, + 0x30, 0x4D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, + 0x02, 0x55, 0x53, 0x31, 0x11, 0x30, 0x0F, 0x06, 0x03, 0x55, 0x04, 0x0A, + 0x0C, 0x08, 0x54, 0x65, 0x73, 0x74, 0x20, 0x4F, 0x72, 0x67, 0x31, 0x17, + 0x30, 0x15, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0E, 0x4C, 0x45, 0x41, + 0x46, 0x20, 0x54, 0x65, 0x73, 0x74, 0x20, 0x43, 0x61, 0x72, 0x64, 0x31, + 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x2D, 0x03, 0x09, 0x00, 0x01, + 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x30, 0x59, 0x30, 0x13, 0x06, + 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, + 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x6A, 0x0B, + 0x1A, 0xB0, 0x0F, 0x64, 0x3F, 0xF6, 0x3A, 0x23, 0xAD, 0x01, 0x15, 0x9B, + 0xF1, 0x8D, 0xA0, 0x70, 0xCF, 0xD3, 0x1F, 0x37, 0x1D, 0xB8, 0x34, 0x13, + 0x1E, 0x22, 0xF6, 0xB7, 0x34, 0x6E, 0xC7, 0xDA, 0xDA, 0x6D, 0xA0, 0xC7, + 0xD0, 0x3A, 0x42, 0xA3, 0x39, 0xFA, 0xFE, 0x12, 0x7E, 0x50, 0xB0, 0x49, + 0xD9, 0x5A, 0xB6, 0x78, 0x72, 0x3B, 0x00, 0x9E, 0x0C, 0x19, 0x67, 0x91, + 0x11, 0xA2, 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, + 0x03, 0x02, 0x03, 0x48, 0x00, 0x30, 0x45, 0x02, 0x20, 0x56, 0x24, 0x2F, + 0xAD, 0xA4, 0x9D, 0xA0, 0x02, 0xFC, 0x2E, 0x61, 0x79, 0x93, 0x88, 0x48, + 0x6B, 0x07, 0x99, 0xF2, 0x34, 0x6E, 0x59, 0x39, 0x12, 0xB5, 0xC7, 0x36, + 0x2D, 0x87, 0xED, 0xD9, 0xF4, 0x02, 0x21, 0x00, 0xD3, 0x8B, 0x88, 0xD4, + 0xE0, 0x60, 0x8C, 0x6D, 0x13, 0x49, 0xE0, 0xDB, 0x65, 0xB7, 0xA9, 0x84, + 0xEF, 0xC4, 0xDF, 0x89, 0x08, 0x45, 0xEC, 0x8E, 0x09, 0x41, 0x6A, 0x24, + 0x2B, 0x98, 0x4A, 0xFE + }; + static const byte expUniqueId[] = { + 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF + }; + /* Same certificate shape but with an x500UniqueIdentifier value that holds + * an embedded 0x00. The subject display string truncates at the NUL, but + * the length-based WOLFSSL_X509_NAME entry must carry the full value. */ + static const byte leafEmbeddedNulDer[] = { + 0x30, 0x82, 0x01, 0x84, 0x30, 0x82, 0x01, 0x2A, 0xA0, 0x03, 0x02, 0x01, + 0x02, 0x02, 0x06, 0x0E, 0xAF, 0x12, 0x34, 0x56, 0x78, 0x30, 0x0A, 0x06, + 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30, 0x49, 0x31, + 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, + 0x31, 0x11, 0x30, 0x0F, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x08, 0x54, + 0x65, 0x73, 0x74, 0x20, 0x4F, 0x72, 0x67, 0x31, 0x17, 0x30, 0x15, 0x06, + 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0E, 0x4C, 0x45, 0x41, 0x46, 0x20, 0x54, + 0x65, 0x73, 0x74, 0x20, 0x43, 0x61, 0x72, 0x64, 0x31, 0x0E, 0x30, 0x0C, + 0x06, 0x03, 0x55, 0x04, 0x2D, 0x03, 0x05, 0x00, 0xAB, 0x00, 0xCD, 0xEF, + 0x30, 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x5A, 0x17, 0x0D, 0x34, 0x39, 0x31, 0x32, 0x33, + 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5A, 0x30, 0x49, 0x31, 0x0B, + 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, + 0x11, 0x30, 0x0F, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x08, 0x54, 0x65, + 0x73, 0x74, 0x20, 0x4F, 0x72, 0x67, 0x31, 0x17, 0x30, 0x15, 0x06, 0x03, + 0x55, 0x04, 0x03, 0x0C, 0x0E, 0x4C, 0x45, 0x41, 0x46, 0x20, 0x54, 0x65, + 0x73, 0x74, 0x20, 0x43, 0x61, 0x72, 0x64, 0x31, 0x0E, 0x30, 0x0C, 0x06, + 0x03, 0x55, 0x04, 0x2D, 0x03, 0x05, 0x00, 0xAB, 0x00, 0xCD, 0xEF, 0x30, + 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, + 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x42, + 0x00, 0x04, 0xF4, 0xB3, 0x36, 0x65, 0xF2, 0xB7, 0x33, 0x37, 0x8F, 0xB3, + 0x75, 0x1A, 0x79, 0x64, 0x18, 0xED, 0x14, 0x9D, 0xEE, 0x03, 0x3B, 0x35, + 0x6E, 0xEA, 0x4B, 0x58, 0x86, 0xCC, 0x0C, 0x6A, 0x76, 0x39, 0xBF, 0x01, + 0xA1, 0xA7, 0x78, 0xA1, 0x77, 0xF7, 0xCC, 0x12, 0x5E, 0xDF, 0x2F, 0x90, + 0x35, 0x02, 0xE2, 0x51, 0x3F, 0x7A, 0x08, 0x85, 0xF5, 0x93, 0x2A, 0xE1, + 0xE0, 0x5A, 0x5D, 0x8F, 0x90, 0x50, 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, + 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x03, 0x48, 0x00, 0x30, 0x45, 0x02, + 0x21, 0x00, 0xF8, 0xF0, 0x02, 0x98, 0xE2, 0xA5, 0x22, 0x23, 0x80, 0x6B, + 0x73, 0xE3, 0x9F, 0xB8, 0xC6, 0x05, 0x64, 0xE9, 0x56, 0x57, 0xC2, 0xEB, + 0x02, 0x50, 0x93, 0xA1, 0x0B, 0x57, 0x06, 0x61, 0x55, 0x88, 0x02, 0x20, + 0x28, 0xED, 0xDA, 0x90, 0xF2, 0xB0, 0x94, 0x7E, 0xDB, 0xC3, 0x60, 0x6C, + 0x87, 0xC1, 0x51, 0x75, 0xC1, 0x04, 0xA7, 0x70, 0xEC, 0x8D, 0x60, 0xA8, + 0x28, 0xC2, 0x3E, 0x08, 0x42, 0x64, 0x9C, 0x5B + }; + static const byte expEmbeddedNul[] = { + 0xAB, 0x00, 0xCD, 0xEF + }; + DecodedCert cert; + const char* p = NULL; +#ifdef OPENSSL_EXTRA + WOLFSSL_X509* x509 = NULL; + WOLFSSL_X509_NAME* name = NULL; + WOLFSSL_X509_NAME_ENTRY* entry = NULL; + WOLFSSL_ASN1_STRING* str = NULL; + int idx = -1; +#endif + + wc_InitDecodedCert(&cert, leafUniqueIdDer, (word32)sizeof(leafUniqueIdDer), + NULL); + /* Parse must succeed; before the fix this returned ASN_PARSE_E. */ + ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL), 0); + /* Value is recorded in the full subject string (BIT STRING unused-bits + * count octet stripped). */ + ExpectNotNull(p = XSTRSTR(cert.subject, "/x500UniqueIdentifier=")); + if (p != NULL) { + p += XSTRLEN("/x500UniqueIdentifier="); + ExpectIntEQ(XMEMCMP(p, expUniqueId, sizeof(expUniqueId)), 0); + } + wc_FreeDecodedCert(&cert); + + /* A value with an embedded 0x00 must still parse successfully. */ + wc_InitDecodedCert(&cert, leafEmbeddedNulDer, + (word32)sizeof(leafEmbeddedNulDer), NULL); + ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL), 0); + wc_FreeDecodedCert(&cert); + +#ifdef OPENSSL_EXTRA + /* The value is also reachable by NID through the X509_NAME entries. */ + ExpectNotNull(x509 = wolfSSL_X509_d2i(NULL, leafUniqueIdDer, + (int)sizeof(leafUniqueIdDer))); + ExpectNotNull(name = wolfSSL_X509_get_subject_name(x509)); + ExpectIntGE(idx = wolfSSL_X509_NAME_get_index_by_NID(name, + WC_NID_x500UniqueIdentifier, -1), 0); + ExpectNotNull(entry = wolfSSL_X509_NAME_get_entry(name, idx)); + ExpectNotNull(str = wolfSSL_X509_NAME_ENTRY_get_data(entry)); + ExpectIntEQ(wolfSSL_ASN1_STRING_length(str), (int)sizeof(expUniqueId)); + ExpectIntEQ(XMEMCMP(wolfSSL_ASN1_STRING_get0_data(str), expUniqueId, + sizeof(expUniqueId)), 0); + wolfSSL_X509_free(x509); + x509 = NULL; + + /* The embedded-0x00 value survives intact through the length-based entry + * even though it would truncate the subject display string. */ + ExpectNotNull(x509 = wolfSSL_X509_d2i(NULL, leafEmbeddedNulDer, + (int)sizeof(leafEmbeddedNulDer))); + ExpectNotNull(name = wolfSSL_X509_get_subject_name(x509)); + ExpectIntGE(idx = wolfSSL_X509_NAME_get_index_by_NID(name, + WC_NID_x500UniqueIdentifier, -1), 0); + ExpectNotNull(entry = wolfSSL_X509_NAME_get_entry(name, idx)); + ExpectNotNull(str = wolfSSL_X509_NAME_ENTRY_get_data(entry)); + ExpectIntEQ(wolfSSL_ASN1_STRING_length(str), (int)sizeof(expEmbeddedNul)); + ExpectIntEQ(XMEMCMP(wolfSSL_ASN1_STRING_get0_data(str), expEmbeddedNul, + sizeof(expEmbeddedNul)), 0); + wolfSSL_X509_free(x509); +#endif /* OPENSSL_EXTRA */ +#endif + return EXPECT_RESULT(); +} + /*----------------------------------------------------------------------------* | Method Allocators *----------------------------------------------------------------------------*/ @@ -37958,6 +38112,7 @@ TEST_CASE testCases[] = { TEST_DECL(test_wc_LoadStaticMemory_CTX), TEST_DECL(test_wc_FreeCertList), + TEST_DECL(test_wc_ParseCert_uniqueIdentifier), /* Locking with Compat Mutex */ TEST_DECL(test_wc_SetMutexCb), TEST_DECL(test_wc_LockMutex_ex), diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index f8466f8855..6789eb3bd7 100644 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -14684,10 +14684,12 @@ enum { /* Supported types of encodings (tags) for RDN strings. * X.509: RFC 5280, 4.1.2.4 - DirectoryString * (IA5 String not listed in RFC but required for alternative types) + * (BIT STRING not a DirectoryString but required for x500UniqueIdentifier, + * X.520 attribute OID 2.5.4.45) */ static const byte rdnChoice[] = { ASN_PRINTABLE_STRING, ASN_IA5_STRING, ASN_UTF8STRING, ASN_T61STRING, - ASN_UNIVERSALSTRING, ASN_BMPSTRING, 0 + ASN_UNIVERSALSTRING, ASN_BMPSTRING, ASN_BIT_STRING, 0 }; #endif @@ -15177,6 +15179,15 @@ static int GetRDN(DecodedCert* cert, char* full, word32* idx, int* nid, *nid = GetCertNameSubjectNID(id); #endif } + else if (id == ASN_X500_UNIQUE_ID) { + /* x500UniqueIdentifier (OID 2.5.4.45), value is a BIT STRING. + * Not in the table as its id is outside the contiguous range. */ + typeStr = WOLFSSL_X500_UNIQUE_ID; + typeStrLen = sizeof(WOLFSSL_X500_UNIQUE_ID) - 1; + #ifdef WOLFSSL_X509_NAME_AVAILABLE + *nid = WC_NID_x500UniqueIdentifier; + #endif + } } else if (oidSz == sizeof(attrEmailOid) && XMEMCMP(oid, attrEmailOid, oidSz) == 0) { /* Set the email id, type string, length and NID. */ @@ -15275,6 +15286,13 @@ static int GetRDN(DecodedCert* cert, char* full, word32* idx, int* nid, /* Get the string reference and length. */ GetASN_GetRef(&dataASN[RDNASN_IDX_ATTR_VAL], &str, &strLen); + /* A BIT STRING value (e.g. x500UniqueIdentifier) begins with an + * unused-bits count octet that is not part of the value. */ + if ((tag == ASN_BIT_STRING) && (strLen > 0)) { + str++; + strLen--; + } + if (isSubject) { /* Store subject field components. */ ret = SetSubject(cert, id, str, (int)strLen, tag); @@ -15291,7 +15309,11 @@ static int GetRDN(DecodedCert* cert, char* full, word32* idx, int* nid, * terminating NUL character. */ if ((typeStrLen + strLen) < (word32)(WC_ASN_NAME_MAX - *idx)) { - /* Add RDN to full string. */ + /* Add RDN to full string. The value is copied verbatim, so for + * a binary attribute (e.g. x500UniqueIdentifier's BIT STRING) + * this display string is best-effort and may be truncated at an + * embedded NUL; read the value from the WOLFSSL_X509_NAME entry + * (by NID) for the authoritative bytes. */ XMEMCPY(&full[*idx], typeStr, typeStrLen); *idx += typeStrLen; XMEMCPY(&full[*idx], str, strLen); @@ -15427,6 +15449,13 @@ static int GetCertName(DecodedCert* cert, char* full, byte* hash, int nameType, } #endif + /* A BIT STRING value (e.g. x500UniqueIdentifier) begins with + * an unused-bits count octet that is not part of the value. */ + if ((tag == ASN_BIT_STRING) && (strLen > 0)) { + str++; + strLen--; + } + /* Convert BER tag to a OpenSSL type. */ switch (tag) { case CTC_UTF8: @@ -15435,6 +15464,9 @@ static int GetCertName(DecodedCert* cert, char* full, byte* hash, int nameType, case CTC_PRINTABLE: enc = WOLFSSL_V_ASN1_PRINTABLESTRING; break; + case ASN_BIT_STRING: + enc = WOLFSSL_V_ASN1_BIT_STRING; + break; default: WOLFSSL_MSG("Unknown encoding type, default UTF8"); enc = WOLFSSL_MBSTRING_UTF8; diff --git a/wolfssl/wolfcrypt/asn.h b/wolfssl/wolfcrypt/asn.h index 8259f0861f..85ef022ac1 100644 --- a/wolfssl/wolfcrypt/asn.h +++ b/wolfssl/wolfcrypt/asn.h @@ -813,6 +813,7 @@ enum DN_Tags { ASN_BUS_CAT = 0x0f, /* businessCategory */ ASN_POSTAL_CODE = 0x11, /* postalCode */ ASN_USER_ID = 0x12, /* UserID */ + ASN_X500_UNIQUE_ID = 0x2d, /* x500UniqueIdentifier (2.5.4.45) */ #ifdef WOLFSSL_CERT_NAME_ALL ASN_NAME = 0x29, /* name */ ASN_GIVEN_NAME = 0x2a, /* GN */ @@ -883,6 +884,7 @@ extern const WOLFSSL_ObjectInfo wolfssl_object_info[]; #define WOLFSSL_RFC822_MAILBOX "/rfc822Mailbox=" #define WOLFSSL_FAVOURITE_DRINK "/favouriteDrink=" #define WOLFSSL_CONTENT_TYPE "/contentType=" +#define WOLFSSL_X500_UNIQUE_ID "/x500UniqueIdentifier=" #if defined(WOLFSSL_APACHE_HTTPD) /* otherName strings */ @@ -1092,6 +1094,10 @@ extern const WOLFSSL_ObjectInfo wolfssl_object_info[]; #define WC_LN_userId "userId" #define WC_NID_userId 458 +#define WC_SN_x500UniqueIdentifier "x500UniqueIdentifier" +#define WC_LN_x500UniqueIdentifier "X500 unique identifier" +#define WC_NID_x500UniqueIdentifier 503 /* 2.5.4.45 */ + #define WC_LN_registeredAddress "registeredAddress" #define WC_NID_registeredAddress 870 @@ -1229,6 +1235,10 @@ extern const WOLFSSL_ObjectInfo wolfssl_object_info[]; #define LN_userId WC_LN_userId #define NID_userId WC_NID_userId +#define SN_x500UniqueIdentifier WC_SN_x500UniqueIdentifier +#define LN_x500UniqueIdentifier WC_LN_x500UniqueIdentifier +#define NID_x500UniqueIdentifier WC_NID_x500UniqueIdentifier + #define LN_registeredAddress WC_LN_registeredAddress #define NID_registeredAddress WC_NID_registeredAddress From 1bf812856c67ab7b0affe135829ea484b37f9115 Mon Sep 17 00:00:00 2001 From: Eric Blankenhorn Date: Tue, 28 Jul 2026 09:19:52 -0500 Subject: [PATCH 2/4] Fix from review --- scripts/asn1_oid_sum.pl | 2 ++ src/ssl_asn1.c | 2 +- tests/api.c | 49 ++++++++++++++++++++++++++++++++++--- wolfcrypt/src/asn.c | 48 +++++++++++++++++++++++++++--------- wolfssl/wolfcrypt/oid_sum.h | 6 +++++ 5 files changed, 91 insertions(+), 16 deletions(-) diff --git a/scripts/asn1_oid_sum.pl b/scripts/asn1_oid_sum.pl index 6b4cdac4b2..b746a4e0a8 100755 --- a/scripts/asn1_oid_sum.pl +++ b/scripts/asn1_oid_sum.pl @@ -1287,6 +1287,7 @@ sub print_footer { my @name_description = ( 2, 5, 4, 13 ); my @name_business_cat = ( 2, 5, 4, 15 ); my @name_postal_code = ( 2, 5, 4, 17 ); +my @name_x500_unique_id = ( 2, 5, 4, 45 ); my @name_pkcs9_email = ( 1, 2, 840, 113549, 1, 9, 1 );; my @name_rfc822_mailbox = ( 0, 9, 2342, 19200300, 100, 1, 3 ); my @name_fav_drink = ( 0, 9, 2342, 19200300, 100, 1, 5 ); @@ -1317,6 +1318,7 @@ sub print_footer { same => 1 }, { name => "WC_NAME_INITIALIS", oid => \@csr_initials, same => 1 }, + { name => "WC_NAME_X500_UNIQUE_ID", oid => \@name_x500_unique_id }, { name => "WC_NAME_EMAIL_ADDRESS", oid => \@name_pkcs9_email }, { name => "WC_NAME_USER_ID", oid => \@csr_user_id, same => 1 }, diff --git a/src/ssl_asn1.c b/src/ssl_asn1.c index b228266ea1..756b785109 100644 --- a/src/ssl_asn1.c +++ b/src/ssl_asn1.c @@ -5068,7 +5068,7 @@ const WOLFSSL_ObjectInfo wolfssl_object_info[] = { /* Rarely used and legacy attributes. */ { WC_NID_userId, WC_NAME_USER_ID_OID, oidCertNameType, "UID", "userId"}, - { WC_NID_x500UniqueIdentifier, WC_NID_x500UniqueIdentifier, oidCertNameType, + { WC_NID_x500UniqueIdentifier, WC_NAME_X500_UNIQUE_ID_OID, oidCertNameType, "x500UniqueIdentifier", "X500 unique identifier"}, { WC_NID_title, WC_NAME_TITLE_OID, oidCertNameType, "title", "title"}, { WC_NID_description, WC_NAME_DESCRIPTION_OID, oidCertNameType, diff --git a/tests/api.c b/tests/api.c index 008b9acee1..e675956bcf 100644 --- a/tests/api.c +++ b/tests/api.c @@ -760,12 +760,15 @@ static int test_wc_ParseCert_uniqueIdentifier(void) 0x87, 0xC1, 0x51, 0x75, 0xC1, 0x04, 0xA7, 0x70, 0xEC, 0x8D, 0x60, 0xA8, 0x28, 0xC2, 0x3E, 0x08, 0x42, 0x64, 0x9C, 0x5B }; - static const byte expEmbeddedNul[] = { - 0xAB, 0x00, 0xCD, 0xEF - }; DecodedCert cert; const char* p = NULL; + byte badDer[sizeof(leafUniqueIdDer)]; + word32 i; + int flipped = 0; #ifdef OPENSSL_EXTRA + static const byte expEmbeddedNul[] = { + 0xAB, 0x00, 0xCD, 0xEF + }; WOLFSSL_X509* x509 = NULL; WOLFSSL_X509_NAME* name = NULL; WOLFSSL_X509_NAME_ENTRY* entry = NULL; @@ -792,6 +795,46 @@ static int test_wc_ParseCert_uniqueIdentifier(void) ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL), 0); wc_FreeDecodedCert(&cert); + /* Two malformed variants derived from the valid cert exercise the parser's + * BIT STRING guards. Each keeps all byte lengths unchanged, so the DER + * stays structurally valid and only the targeted guard rejects it. */ + + /* (1) A non-byte-aligned value (non-zero unused-bits count) for the + * x500UniqueIdentifier attribute must be rejected. */ + XMEMCPY(badDer, leafUniqueIdDer, sizeof(leafUniqueIdDer)); + flipped = 0; + for (i = 0; (i + 6) <= (word32)sizeof(badDer); i++) { + /* OID 2.5.4.45 (55 04 2d), BIT STRING (03), len 9 (09), 0 unused (00) */ + if ((badDer[i] == 0x55) && (badDer[i + 1] == 0x04) && + (badDer[i + 2] == 0x2D) && (badDer[i + 3] == 0x03) && + (badDer[i + 4] == 0x09) && (badDer[i + 5] == 0x00)) { + badDer[i + 5] = 0x03; /* 3 unused bits - not byte aligned */ + flipped++; + } + } + ExpectIntGT(flipped, 0); /* sanity: the mutation was applied */ + wc_InitDecodedCert(&cert, badDer, (word32)sizeof(badDer), NULL); + ExpectIntNE(wc_ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL), 0); + wc_FreeDecodedCert(&cert); + + /* (2) A BIT STRING value is only valid for x500UniqueIdentifier; the same + * encoding under another attribute (here commonName, 2.5.4.3) must be + * rejected rather than accepted as a DirectoryString. */ + XMEMCPY(badDer, leafUniqueIdDer, sizeof(leafUniqueIdDer)); + flipped = 0; + for (i = 0; (i + 6) <= (word32)sizeof(badDer); i++) { + if ((badDer[i] == 0x55) && (badDer[i + 1] == 0x04) && + (badDer[i + 2] == 0x2D) && (badDer[i + 3] == 0x03) && + (badDer[i + 4] == 0x09) && (badDer[i + 5] == 0x00)) { + badDer[i + 2] = 0x03; /* 2.5.4.45 -> 2.5.4.3 (commonName) */ + flipped++; + } + } + ExpectIntGT(flipped, 0); + wc_InitDecodedCert(&cert, badDer, (word32)sizeof(badDer), NULL); + ExpectIntNE(wc_ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL), 0); + wc_FreeDecodedCert(&cert); + #ifdef OPENSSL_EXTRA /* The value is also reachable by NID through the X509_NAME entries. */ ExpectNotNull(x509 = wolfSSL_X509_d2i(NULL, leafUniqueIdDer, diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index 6789eb3bd7..218908bfcd 100644 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -15286,21 +15286,36 @@ static int GetRDN(DecodedCert* cert, char* full, word32* idx, int* nid, /* Get the string reference and length. */ GetASN_GetRef(&dataASN[RDNASN_IDX_ATTR_VAL], &str, &strLen); - /* A BIT STRING value (e.g. x500UniqueIdentifier) begins with an - * unused-bits count octet that is not part of the value. */ - if ((tag == ASN_BIT_STRING) && (strLen > 0)) { - str++; - strLen--; + /* A BIT STRING value is only valid for x500UniqueIdentifier; every + * other RDN attribute must be a DirectoryString (RFC 5280 4.1.2.4). + * The value begins with an unused-bits count octet that is not part of + * the value, and only a byte-aligned value (zero unused bits) can be + * represented, as the WOLFSSL_X509_NAME entry stores raw bytes with no + * unused-bits count. */ + if (tag == ASN_BIT_STRING) { + if (id != ASN_X500_UNIQUE_ID) { + WOLFSSL_MSG("BIT STRING RDN value only valid for " + "x500UniqueIdentifier"); + ret = ASN_PARSE_E; + } + else if ((strLen == 0) || (str[0] != 0)) { + WOLFSSL_MSG("BIT STRING RDN value not byte-aligned"); + ret = ASN_PARSE_E; + } + else { + str++; + strLen--; + } } - if (isSubject) { + if ((ret == 0) && isSubject) { /* Store subject field components. */ ret = SetSubject(cert, id, str, (int)strLen, tag); } #if (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_CERT_EXT)) && \ defined(WOLFSSL_HAVE_ISSUER_NAMES) /* Put issuer common name string and encoding into certificate. */ - else { + else if (ret == 0) { ret = SetIssuer(cert, id, str, (int)strLen, tag); } #endif @@ -15450,10 +15465,19 @@ static int GetCertName(DecodedCert* cert, char* full, byte* hash, int nameType, #endif /* A BIT STRING value (e.g. x500UniqueIdentifier) begins with - * an unused-bits count octet that is not part of the value. */ - if ((tag == ASN_BIT_STRING) && (strLen > 0)) { - str++; - strLen--; + * an unused-bits count octet that is not part of the value. + * Only a byte-aligned value (zero unused bits) can be + * represented, as the WOLFSSL_X509_NAME entry stores raw bytes + * with no unused-bits count. */ + if (tag == ASN_BIT_STRING) { + if ((strLen == 0) || (str[0] != 0)) { + WOLFSSL_MSG("BIT STRING RDN value not byte-aligned"); + ret = ASN_PARSE_E; + } + else { + str++; + strLen--; + } } /* Convert BER tag to a OpenSSL type. */ @@ -15471,7 +15495,7 @@ static int GetCertName(DecodedCert* cert, char* full, byte* hash, int nameType, WOLFSSL_MSG("Unknown encoding type, default UTF8"); enc = WOLFSSL_MBSTRING_UTF8; } - if (nid != 0) { + if ((ret == 0) && (nid != 0)) { /* Add an entry to the X509_NAME. */ if (wolfSSL_X509_NAME_add_entry_by_NID(dName, nid, enc, str, (int)strLen, -1, -1) != WOLFSSL_SUCCESS) { diff --git a/wolfssl/wolfcrypt/oid_sum.h b/wolfssl/wolfcrypt/oid_sum.h index 1a650deed3..7647b05dea 100644 --- a/wolfssl/wolfcrypt/oid_sum.h +++ b/wolfssl/wolfcrypt/oid_sum.h @@ -1942,6 +1942,9 @@ enum CertName_Sum { /* 0x55,0x04,0x2b */ /* 2.5.4.43 */ WC_NAME_INITIALIS_OID = 132, + /* 0x55,0x04,0x2d */ + /* 2.5.4.45 */ + WC_NAME_X500_UNIQUE_ID_OID = 134, /* 0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x09,0x01 */ /* 1.2.840.113549.1.9.1 */ WC_NAME_EMAIL_ADDRESS_OID = 653, @@ -2012,6 +2015,9 @@ enum CertName_Sum { /* 0x55,0x04,0x2b */ /* 2.5.4.43 */ WC_NAME_INITIALIS_OID = 0x7fd404aa, + /* 0x55,0x04,0x2d */ + /* 2.5.4.45 */ + WC_NAME_X500_UNIQUE_ID_OID = 0x7fd204aa, /* 0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x09,0x01 */ /* 1.2.840.113549.1.9.1 */ WC_NAME_EMAIL_ADDRESS_OID = 0x70b67423, From 1c9936a5de8f44c1987ae76e246b3004036709d6 Mon Sep 17 00:00:00 2001 From: Eric Blankenhorn Date: Tue, 28 Jul 2026 10:24:04 -0500 Subject: [PATCH 3/4] Fix from review --- tests/api.c | 18 ++++++++++++++++++ wolfcrypt/src/asn.c | 42 ++++++++++++++++++++++++++---------------- 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/tests/api.c b/tests/api.c index e675956bcf..c570b67774 100644 --- a/tests/api.c +++ b/tests/api.c @@ -835,6 +835,24 @@ static int test_wc_ParseCert_uniqueIdentifier(void) ExpectIntNE(wc_ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL), 0); wc_FreeDecodedCert(&cert); + /* (3) A BIT STRING under an unrecognized attribute OID (typeStr/nid stay + * unset in GetRDN) must also be rejected, not silently accepted. Remap the + * OID to an unassigned 2.5.4.123 arc, keeping the BIT STRING value. */ + XMEMCPY(badDer, leafUniqueIdDer, sizeof(leafUniqueIdDer)); + flipped = 0; + for (i = 0; (i + 6) <= (word32)sizeof(badDer); i++) { + if ((badDer[i] == 0x55) && (badDer[i + 1] == 0x04) && + (badDer[i + 2] == 0x2D) && (badDer[i + 3] == 0x03) && + (badDer[i + 4] == 0x09) && (badDer[i + 5] == 0x00)) { + badDer[i + 2] = 0x7B; /* 2.5.4.45 -> 2.5.4.123 (unassigned) */ + flipped++; + } + } + ExpectIntGT(flipped, 0); + wc_InitDecodedCert(&cert, badDer, (word32)sizeof(badDer), NULL); + ExpectIntNE(wc_ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL), 0); + wc_FreeDecodedCert(&cert); + #ifdef OPENSSL_EXTRA /* The value is also reachable by NID through the X509_NAME entries. */ ExpectNotNull(x509 = wolfSSL_X509_d2i(NULL, leafUniqueIdDer, diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index 218908bfcd..4ebbcf3af0 100644 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -15277,6 +15277,17 @@ static int GetRDN(DecodedCert* cert, char* full, word32* idx, int* nid, } } + /* A BIT STRING value is only valid for x500UniqueIdentifier; every other + * RDN attribute must be a DirectoryString (RFC 5280 4.1.2.4). The shared + * rdnChoice[] table accepts a BIT STRING tag for any attribute OID, so + * reject it here unconditionally - including for an unrecognized OID, where + * typeStr stays NULL and the block below is skipped. */ + if ((ret == 0) && (dataASN[RDNASN_IDX_ATTR_VAL].tag == ASN_BIT_STRING) && + (id != ASN_X500_UNIQUE_ID)) { + WOLFSSL_MSG("BIT STRING RDN value only valid for x500UniqueIdentifier"); + ret = ASN_PARSE_E; + } + if ((ret == 0) && (typeStr != NULL)) { /* OID type to store for subject name and add to full string. */ const byte* str; @@ -15286,19 +15297,12 @@ static int GetRDN(DecodedCert* cert, char* full, word32* idx, int* nid, /* Get the string reference and length. */ GetASN_GetRef(&dataASN[RDNASN_IDX_ATTR_VAL], &str, &strLen); - /* A BIT STRING value is only valid for x500UniqueIdentifier; every - * other RDN attribute must be a DirectoryString (RFC 5280 4.1.2.4). - * The value begins with an unused-bits count octet that is not part of - * the value, and only a byte-aligned value (zero unused bits) can be - * represented, as the WOLFSSL_X509_NAME entry stores raw bytes with no - * unused-bits count. */ + /* The x500UniqueIdentifier value is a BIT STRING that begins with an + * unused-bits count octet not part of the value. Only a byte-aligned + * value (zero unused bits) can be represented, as the WOLFSSL_X509_NAME + * entry stores raw bytes with no unused-bits count. */ if (tag == ASN_BIT_STRING) { - if (id != ASN_X500_UNIQUE_ID) { - WOLFSSL_MSG("BIT STRING RDN value only valid for " - "x500UniqueIdentifier"); - ret = ASN_PARSE_E; - } - else if ((strLen == 0) || (str[0] != 0)) { + if ((strLen == 0) || (str[0] != 0)) { WOLFSSL_MSG("BIT STRING RDN value not byte-aligned"); ret = ASN_PARSE_E; } @@ -15464,13 +15468,19 @@ static int GetCertName(DecodedCert* cert, char* full, byte* hash, int nameType, } #endif - /* A BIT STRING value (e.g. x500UniqueIdentifier) begins with - * an unused-bits count octet that is not part of the value. - * Only a byte-aligned value (zero unused bits) can be + /* A BIT STRING value is only valid for x500UniqueIdentifier + * (also rejected for any other attribute in GetRDN above). It + * begins with an unused-bits count octet not part of the value; + * only a byte-aligned value (zero unused bits) can be * represented, as the WOLFSSL_X509_NAME entry stores raw bytes * with no unused-bits count. */ if (tag == ASN_BIT_STRING) { - if ((strLen == 0) || (str[0] != 0)) { + if (nid != WC_NID_x500UniqueIdentifier) { + WOLFSSL_MSG("BIT STRING RDN value only valid for " + "x500UniqueIdentifier"); + ret = ASN_PARSE_E; + } + else if ((strLen == 0) || (str[0] != 0)) { WOLFSSL_MSG("BIT STRING RDN value not byte-aligned"); ret = ASN_PARSE_E; } From d27586b19d51f8a4058b3fb6a9f35c72c3d5a29c Mon Sep 17 00:00:00 2001 From: Eric Blankenhorn Date: Fri, 31 Jul 2026 08:44:55 -0500 Subject: [PATCH 4/4] Fix from review --- tests/api.c | 48 ++++++++++++++++++--------------------------- wolfcrypt/src/asn.c | 37 ++++++++++++---------------------- 2 files changed, 32 insertions(+), 53 deletions(-) diff --git a/tests/api.c b/tests/api.c index c570b67774..9c9b1f1ce0 100644 --- a/tests/api.c +++ b/tests/api.c @@ -671,18 +671,15 @@ defined(USE_CERT_BUFFERS_2048) return EXPECT_RESULT(); } -/* Regression test for parsing an x500UniqueIdentifier (OID 2.5.4.45) in a - * subject/issuer DN. Its value is a BIT STRING, which the RDN parser used to - * reject with ASN_PARSE_E. */ +/* Test parsing an x500UniqueIdentifier (OID 2.5.4.45) in a subject/issuer DN. + * Its value is a BIT STRING, which the RDN parser used to reject. */ static int test_wc_ParseCert_uniqueIdentifier(void) { EXPECT_DECLS; #if !defined(NO_CERTS) && defined(WOLFSSL_ASN_TEMPLATE) && defined(HAVE_ECC) - /* Self-signed ECDSA P-256 certificate whose subject and issuer DN each - * hold an x500UniqueIdentifier (OID 2.5.4.45) encoded as a BIT STRING, - * as issued for LEAF physical-access credentials. Built with asn1crypto - * and pyca/cryptography (verifies clean with OpenSSL), so the encoding is - * an independent oracle, not produced by wolfSSL. */ + /* Self-signed ECDSA P-256 certificate with an x500UniqueIdentifier BIT + * STRING in both subject and issuer DN. Built with asn1crypto and + * pyca/cryptography, verified with OpenSSL. */ static const byte leafUniqueIdDer[] = { 0x30, 0x82, 0x01, 0x8C, 0x30, 0x82, 0x01, 0x32, 0xA0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x06, 0x0E, 0xAF, 0x12, 0x34, 0x56, 0x78, 0x30, 0x0A, 0x06, @@ -722,9 +719,9 @@ static int test_wc_ParseCert_uniqueIdentifier(void) static const byte expUniqueId[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }; - /* Same certificate shape but with an x500UniqueIdentifier value that holds - * an embedded 0x00. The subject display string truncates at the NUL, but - * the length-based WOLFSSL_X509_NAME entry must carry the full value. */ + /* Same certificate but with an embedded 0x00 in the value. The subject + * display string truncates at the NUL, but the WOLFSSL_X509_NAME entry + * must carry the full value. */ static const byte leafEmbeddedNulDer[] = { 0x30, 0x82, 0x01, 0x84, 0x30, 0x82, 0x01, 0x2A, 0xA0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x06, 0x0E, 0xAF, 0x12, 0x34, 0x56, 0x78, 0x30, 0x0A, 0x06, @@ -778,10 +775,8 @@ static int test_wc_ParseCert_uniqueIdentifier(void) wc_InitDecodedCert(&cert, leafUniqueIdDer, (word32)sizeof(leafUniqueIdDer), NULL); - /* Parse must succeed; before the fix this returned ASN_PARSE_E. */ ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL), 0); - /* Value is recorded in the full subject string (BIT STRING unused-bits - * count octet stripped). */ + /* Value is in the subject string with the unused-bits octet stripped. */ ExpectNotNull(p = XSTRSTR(cert.subject, "/x500UniqueIdentifier=")); if (p != NULL) { p += XSTRLEN("/x500UniqueIdentifier="); @@ -795,12 +790,10 @@ static int test_wc_ParseCert_uniqueIdentifier(void) ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL), 0); wc_FreeDecodedCert(&cert); - /* Two malformed variants derived from the valid cert exercise the parser's - * BIT STRING guards. Each keeps all byte lengths unchanged, so the DER - * stays structurally valid and only the targeted guard rejects it. */ + /* Malformed variants exercise the parser's BIT STRING guards. Each keeps + * all byte lengths unchanged so the DER stays structurally valid. */ - /* (1) A non-byte-aligned value (non-zero unused-bits count) for the - * x500UniqueIdentifier attribute must be rejected. */ + /* (1) Non-zero unused-bits count must be rejected. */ XMEMCPY(badDer, leafUniqueIdDer, sizeof(leafUniqueIdDer)); flipped = 0; for (i = 0; (i + 6) <= (word32)sizeof(badDer); i++) { @@ -812,14 +805,12 @@ static int test_wc_ParseCert_uniqueIdentifier(void) flipped++; } } - ExpectIntGT(flipped, 0); /* sanity: the mutation was applied */ + ExpectIntGT(flipped, 0); /* sanity: mutation was applied */ wc_InitDecodedCert(&cert, badDer, (word32)sizeof(badDer), NULL); ExpectIntNE(wc_ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL), 0); wc_FreeDecodedCert(&cert); - /* (2) A BIT STRING value is only valid for x500UniqueIdentifier; the same - * encoding under another attribute (here commonName, 2.5.4.3) must be - * rejected rather than accepted as a DirectoryString. */ + /* (2) A BIT STRING under another known attribute must be rejected. */ XMEMCPY(badDer, leafUniqueIdDer, sizeof(leafUniqueIdDer)); flipped = 0; for (i = 0; (i + 6) <= (word32)sizeof(badDer); i++) { @@ -835,9 +826,8 @@ static int test_wc_ParseCert_uniqueIdentifier(void) ExpectIntNE(wc_ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL), 0); wc_FreeDecodedCert(&cert); - /* (3) A BIT STRING under an unrecognized attribute OID (typeStr/nid stay - * unset in GetRDN) must also be rejected, not silently accepted. Remap the - * OID to an unassigned 2.5.4.123 arc, keeping the BIT STRING value. */ + /* (3) A BIT STRING under an unrecognized attribute OID must be rejected, + * not silently accepted. */ XMEMCPY(badDer, leafUniqueIdDer, sizeof(leafUniqueIdDer)); flipped = 0; for (i = 0; (i + 6) <= (word32)sizeof(badDer); i++) { @@ -854,7 +844,7 @@ static int test_wc_ParseCert_uniqueIdentifier(void) wc_FreeDecodedCert(&cert); #ifdef OPENSSL_EXTRA - /* The value is also reachable by NID through the X509_NAME entries. */ + /* Value is also reachable by NID through the X509_NAME entries. */ ExpectNotNull(x509 = wolfSSL_X509_d2i(NULL, leafUniqueIdDer, (int)sizeof(leafUniqueIdDer))); ExpectNotNull(name = wolfSSL_X509_get_subject_name(x509)); @@ -868,8 +858,8 @@ static int test_wc_ParseCert_uniqueIdentifier(void) wolfSSL_X509_free(x509); x509 = NULL; - /* The embedded-0x00 value survives intact through the length-based entry - * even though it would truncate the subject display string. */ + /* Embedded 0x00 survives intact in the entry even though it truncates the + * subject display string. */ ExpectNotNull(x509 = wolfSSL_X509_d2i(NULL, leafEmbeddedNulDer, (int)sizeof(leafEmbeddedNulDer))); ExpectNotNull(name = wolfSSL_X509_get_subject_name(x509)); diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index 4ebbcf3af0..f75692f0f3 100644 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -14684,8 +14684,7 @@ enum { /* Supported types of encodings (tags) for RDN strings. * X.509: RFC 5280, 4.1.2.4 - DirectoryString * (IA5 String not listed in RFC but required for alternative types) - * (BIT STRING not a DirectoryString but required for x500UniqueIdentifier, - * X.520 attribute OID 2.5.4.45) + * (BIT STRING only for x500UniqueIdentifier - OID 2.5.4.45) */ static const byte rdnChoice[] = { ASN_PRINTABLE_STRING, ASN_IA5_STRING, ASN_UTF8STRING, ASN_T61STRING, @@ -15180,8 +15179,7 @@ static int GetRDN(DecodedCert* cert, char* full, word32* idx, int* nid, #endif } else if (id == ASN_X500_UNIQUE_ID) { - /* x500UniqueIdentifier (OID 2.5.4.45), value is a BIT STRING. - * Not in the table as its id is outside the contiguous range. */ + /* Not in the table as its id is outside the contiguous range. */ typeStr = WOLFSSL_X500_UNIQUE_ID; typeStrLen = sizeof(WOLFSSL_X500_UNIQUE_ID) - 1; #ifdef WOLFSSL_X509_NAME_AVAILABLE @@ -15277,11 +15275,9 @@ static int GetRDN(DecodedCert* cert, char* full, word32* idx, int* nid, } } - /* A BIT STRING value is only valid for x500UniqueIdentifier; every other - * RDN attribute must be a DirectoryString (RFC 5280 4.1.2.4). The shared - * rdnChoice[] table accepts a BIT STRING tag for any attribute OID, so - * reject it here unconditionally - including for an unrecognized OID, where - * typeStr stays NULL and the block below is skipped. */ + /* rdnChoice[] allows a BIT STRING tag for any attribute OID but only + * x500UniqueIdentifier may use it - all others are DirectoryString + * (RFC 5280, 4.1.2.4). Checked here to also catch unrecognized OIDs. */ if ((ret == 0) && (dataASN[RDNASN_IDX_ATTR_VAL].tag == ASN_BIT_STRING) && (id != ASN_X500_UNIQUE_ID)) { WOLFSSL_MSG("BIT STRING RDN value only valid for x500UniqueIdentifier"); @@ -15297,10 +15293,8 @@ static int GetRDN(DecodedCert* cert, char* full, word32* idx, int* nid, /* Get the string reference and length. */ GetASN_GetRef(&dataASN[RDNASN_IDX_ATTR_VAL], &str, &strLen); - /* The x500UniqueIdentifier value is a BIT STRING that begins with an - * unused-bits count octet not part of the value. Only a byte-aligned - * value (zero unused bits) can be represented, as the WOLFSSL_X509_NAME - * entry stores raw bytes with no unused-bits count. */ + /* Strip the BIT STRING unused-bits count octet. Only byte-aligned + * values are supported as the count isn't stored. */ if (tag == ASN_BIT_STRING) { if ((strLen == 0) || (str[0] != 0)) { WOLFSSL_MSG("BIT STRING RDN value not byte-aligned"); @@ -15328,11 +15322,9 @@ static int GetRDN(DecodedCert* cert, char* full, word32* idx, int* nid, * terminating NUL character. */ if ((typeStrLen + strLen) < (word32)(WC_ASN_NAME_MAX - *idx)) { - /* Add RDN to full string. The value is copied verbatim, so for - * a binary attribute (e.g. x500UniqueIdentifier's BIT STRING) - * this display string is best-effort and may be truncated at an - * embedded NUL; read the value from the WOLFSSL_X509_NAME entry - * (by NID) for the authoritative bytes. */ + /* Add RDN to full string. Binary values are copied verbatim, + * so this display string may truncate at an embedded NUL - use + * the WOLFSSL_X509_NAME entry for the full value. */ XMEMCPY(&full[*idx], typeStr, typeStrLen); *idx += typeStrLen; XMEMCPY(&full[*idx], str, strLen); @@ -15468,12 +15460,9 @@ static int GetCertName(DecodedCert* cert, char* full, byte* hash, int nameType, } #endif - /* A BIT STRING value is only valid for x500UniqueIdentifier - * (also rejected for any other attribute in GetRDN above). It - * begins with an unused-bits count octet not part of the value; - * only a byte-aligned value (zero unused bits) can be - * represented, as the WOLFSSL_X509_NAME entry stores raw bytes - * with no unused-bits count. */ + /* Only x500UniqueIdentifier may be a BIT STRING. Strip the + * unused-bits count octet - only byte-aligned values are + * supported as the count isn't stored. */ if (tag == ASN_BIT_STRING) { if (nid != WC_NID_x500UniqueIdentifier) { WOLFSSL_MSG("BIT STRING RDN value only valid for "