@@ -26117,6 +26117,143 @@ static void initDefaultName(void)
2611726117#endif /* WOLFSSL_CERT_EXT */
2611826118#endif /* WOLFSSL_CERT_GEN */
2611926119
26120+ #if defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
26121+ defined(WOLFSSL_ASN_TEMPLATE) && \
26122+ (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
26123+ defined(OPENSSL_EXTRA_X509_SMALL) || defined(WOLFSSL_PUBLIC_ASN))
26124+ /* Exercise the public wc_SetDNSEntry() + wc_FlattenAltNames() pair: build an
26125+ * alt-name list and encode it into a GeneralNames SEQUENCE. The order entries
26126+ * land in depends on build config (OPENSSL_EXTRA appends, otherwise prepends),
26127+ * so presence checks are order-independent. Also exercise the
26128+ * wc_SetAltNamesFromList() convenience that encodes straight into a Cert. */
26129+ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t flattenAltNames_test(void)
26130+ {
26131+ wc_test_ret_t ret = 0;
26132+ DNS_entry* list = NULL;
26133+ Cert* cert = NULL;
26134+ byte out[256];
26135+ int len;
26136+ /* dNSName "example.com" -> [2] IMPLICIT IA5String */
26137+ static const byte dnsTlv[] = {
26138+ 0x82, 0x0B, 'e','x','a','m','p','l','e','.','c','o','m'
26139+ };
26140+ /* iPAddress 10.0.0.7 -> [7] IMPLICIT OCTET STRING */
26141+ static const byte ipTlv[] = { 0x87, 0x04, 0x0A, 0x00, 0x00, 0x07 };
26142+ static const byte ip[] = { 0x0A, 0x00, 0x00, 0x07 };
26143+ const int innerSz = (int)sizeof(dnsTlv) + (int)sizeof(ipTlv); /* 19 */
26144+ const int expSz = 2 + innerSz; /* 0x30,len + body */
26145+ int i, foundDns = 0, foundIp = 0;
26146+
26147+ WOLFSSL_ENTER("flattenAltNames_test");
26148+
26149+ /* A NULL list encodes to nothing. */
26150+ len = wc_FlattenAltNames(out, sizeof(out), NULL);
26151+ if (len != 0)
26152+ ret = WC_TEST_RET_ENC_EC(len);
26153+
26154+ if (ret == 0) {
26155+ ret = wc_SetDNSEntry(HEAP_HINT, "example.com", 11, ASN_DNS_TYPE, &list);
26156+ if (ret != 0)
26157+ ret = WC_TEST_RET_ENC_EC(ret);
26158+ }
26159+ if (ret == 0) {
26160+ ret = wc_SetDNSEntry(HEAP_HINT, (const char*)ip, (int)sizeof(ip),
26161+ ASN_IP_TYPE, &list);
26162+ if (ret != 0)
26163+ ret = WC_TEST_RET_ENC_EC(ret);
26164+ }
26165+ if (ret == 0) {
26166+ len = wc_FlattenAltNames(out, sizeof(out), list);
26167+ if (len != expSz)
26168+ ret = WC_TEST_RET_ENC_EC(len);
26169+ }
26170+ if (ret == 0 && (out[0] != ASN_SEQUENCE + ASN_CONSTRUCTED ||
26171+ out[1] != (byte)innerSz))
26172+ ret = WC_TEST_RET_ENC_NC;
26173+ /* Both GeneralName TLVs must be present, regardless of order. */
26174+ for (i = 0; ret == 0 && i + (int)sizeof(dnsTlv) <= len; i++) {
26175+ if (XMEMCMP(out + i, dnsTlv, sizeof(dnsTlv)) == 0)
26176+ foundDns = 1;
26177+ }
26178+ for (i = 0; ret == 0 && i + (int)sizeof(ipTlv) <= len; i++) {
26179+ if (XMEMCMP(out + i, ipTlv, sizeof(ipTlv)) == 0)
26180+ foundIp = 1;
26181+ }
26182+ if (ret == 0 && (!foundDns || !foundIp))
26183+ ret = WC_TEST_RET_ENC_NC;
26184+ /* NULL output is rejected. */
26185+ if (ret == 0) {
26186+ len = wc_FlattenAltNames(NULL, sizeof(out), list);
26187+ if (len != WC_NO_ERR_TRACE(BAD_FUNC_ARG))
26188+ ret = WC_TEST_RET_ENC_EC(len);
26189+ }
26190+ /* Output one byte too small is rejected with BUFFER_E. */
26191+ if (ret == 0) {
26192+ len = wc_FlattenAltNames(out, (word32)expSz - 1, list);
26193+ if (len != WC_NO_ERR_TRACE(BUFFER_E))
26194+ ret = WC_TEST_RET_ENC_EC(len);
26195+ }
26196+
26197+ /* wc_SetAltNamesFromList() encodes the same list straight into a Cert and
26198+ * records the length; the result must match the standalone encoding. */
26199+ if (ret == 0) {
26200+ cert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
26201+ if (cert == NULL)
26202+ ret = WC_TEST_RET_ENC_EC(MEMORY_E);
26203+ }
26204+ if (ret == 0) {
26205+ ret = wc_InitCert_ex(cert, HEAP_HINT, devId);
26206+ if (ret != 0)
26207+ ret = WC_TEST_RET_ENC_EC(ret);
26208+ }
26209+ if (ret == 0) {
26210+ ret = wc_SetAltNamesFromList(cert, list);
26211+ if (ret != 0)
26212+ ret = WC_TEST_RET_ENC_EC(ret);
26213+ }
26214+ if (ret == 0 && (cert->altNamesSz != expSz ||
26215+ XMEMCMP(cert->altNames, out, (size_t)expSz) != 0))
26216+ ret = WC_TEST_RET_ENC_NC;
26217+ /* NULL cert is rejected. */
26218+ if (ret == 0) {
26219+ int r = wc_SetAltNamesFromList(NULL, list);
26220+ if (r != WC_NO_ERR_TRACE(BAD_FUNC_ARG))
26221+ ret = WC_TEST_RET_ENC_EC(r);
26222+ }
26223+ /* A NULL names list encodes to nothing: returns 0 and zeroes altNamesSz. */
26224+ if (ret == 0) {
26225+ cert->altNamesSz = 1; /* poison so we can see it get cleared */
26226+ if (wc_SetAltNamesFromList(cert, NULL) != 0 || cert->altNamesSz != 0)
26227+ ret = WC_TEST_RET_ENC_NC;
26228+ }
26229+
26230+ /* wc_SetDNSEntry() rejects invalid arguments at the public boundary. */
26231+ if (ret == 0) {
26232+ DNS_entry* badList = NULL;
26233+ /* NULL str */
26234+ if (wc_SetDNSEntry(HEAP_HINT, NULL, 1, ASN_DNS_TYPE, &badList)
26235+ != WC_NO_ERR_TRACE(BAD_FUNC_ARG))
26236+ ret = WC_TEST_RET_ENC_NC;
26237+ /* NULL entries */
26238+ else if (wc_SetDNSEntry(HEAP_HINT, "x", 1, ASN_DNS_TYPE, NULL)
26239+ != WC_NO_ERR_TRACE(BAD_FUNC_ARG))
26240+ ret = WC_TEST_RET_ENC_NC;
26241+ /* negative strLen */
26242+ else if (wc_SetDNSEntry(HEAP_HINT, "x", -1, ASN_DNS_TYPE, &badList)
26243+ != WC_NO_ERR_TRACE(BAD_FUNC_ARG))
26244+ ret = WC_TEST_RET_ENC_NC;
26245+ if (badList != NULL)
26246+ FreeAltNames(badList, HEAP_HINT);
26247+ }
26248+
26249+ XFREE(cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
26250+ FreeAltNames(list, HEAP_HINT);
26251+ return ret;
26252+ }
26253+ #endif /* WOLFSSL_CERT_GEN && WOLFSSL_ALT_NAMES && WOLFSSL_ASN_TEMPLATE &&
26254+ * (WOLFSSL_TEST_CERT || OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL ||
26255+ * WOLFSSL_PUBLIC_ASN) */
26256+
2612026257#ifndef NO_RSA
2612126258
2612226259/* Run an RSA async-capable operation: loops while the call returns
@@ -27111,143 +27248,6 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t decodedCertCache_test(void)
2711127248#endif /* defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) &&
2711227249 defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) */
2711327250
27114- #if defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
27115- defined(WOLFSSL_ASN_TEMPLATE) && \
27116- (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
27117- defined(OPENSSL_EXTRA_X509_SMALL) || defined(WOLFSSL_PUBLIC_ASN))
27118- /* Exercise the public wc_SetDNSEntry() + wc_FlattenAltNames() pair: build an
27119- * alt-name list and encode it into a GeneralNames SEQUENCE. The order entries
27120- * land in depends on build config (OPENSSL_EXTRA appends, otherwise prepends),
27121- * so presence checks are order-independent. Also exercise the
27122- * wc_SetAltNamesFromList() convenience that encodes straight into a Cert. */
27123- WOLFSSL_TEST_SUBROUTINE wc_test_ret_t flattenAltNames_test(void)
27124- {
27125- wc_test_ret_t ret = 0;
27126- DNS_entry* list = NULL;
27127- Cert* cert = NULL;
27128- byte out[256];
27129- int len;
27130- /* dNSName "example.com" -> [2] IMPLICIT IA5String */
27131- static const byte dnsTlv[] = {
27132- 0x82, 0x0B, 'e','x','a','m','p','l','e','.','c','o','m'
27133- };
27134- /* iPAddress 10.0.0.7 -> [7] IMPLICIT OCTET STRING */
27135- static const byte ipTlv[] = { 0x87, 0x04, 0x0A, 0x00, 0x00, 0x07 };
27136- static const byte ip[] = { 0x0A, 0x00, 0x00, 0x07 };
27137- const int innerSz = (int)sizeof(dnsTlv) + (int)sizeof(ipTlv); /* 19 */
27138- const int expSz = 2 + innerSz; /* 0x30,len + body */
27139- int i, foundDns = 0, foundIp = 0;
27140-
27141- WOLFSSL_ENTER("flattenAltNames_test");
27142-
27143- /* A NULL list encodes to nothing. */
27144- len = wc_FlattenAltNames(out, sizeof(out), NULL);
27145- if (len != 0)
27146- ret = WC_TEST_RET_ENC_EC(len);
27147-
27148- if (ret == 0) {
27149- ret = wc_SetDNSEntry(HEAP_HINT, "example.com", 11, ASN_DNS_TYPE, &list);
27150- if (ret != 0)
27151- ret = WC_TEST_RET_ENC_EC(ret);
27152- }
27153- if (ret == 0) {
27154- ret = wc_SetDNSEntry(HEAP_HINT, (const char*)ip, (int)sizeof(ip),
27155- ASN_IP_TYPE, &list);
27156- if (ret != 0)
27157- ret = WC_TEST_RET_ENC_EC(ret);
27158- }
27159- if (ret == 0) {
27160- len = wc_FlattenAltNames(out, sizeof(out), list);
27161- if (len != expSz)
27162- ret = WC_TEST_RET_ENC_EC(len);
27163- }
27164- if (ret == 0 && (out[0] != ASN_SEQUENCE + ASN_CONSTRUCTED ||
27165- out[1] != (byte)innerSz))
27166- ret = WC_TEST_RET_ENC_NC;
27167- /* Both GeneralName TLVs must be present, regardless of order. */
27168- for (i = 0; ret == 0 && i + (int)sizeof(dnsTlv) <= len; i++) {
27169- if (XMEMCMP(out + i, dnsTlv, sizeof(dnsTlv)) == 0)
27170- foundDns = 1;
27171- }
27172- for (i = 0; ret == 0 && i + (int)sizeof(ipTlv) <= len; i++) {
27173- if (XMEMCMP(out + i, ipTlv, sizeof(ipTlv)) == 0)
27174- foundIp = 1;
27175- }
27176- if (ret == 0 && (!foundDns || !foundIp))
27177- ret = WC_TEST_RET_ENC_NC;
27178- /* NULL output is rejected. */
27179- if (ret == 0) {
27180- len = wc_FlattenAltNames(NULL, sizeof(out), list);
27181- if (len != WC_NO_ERR_TRACE(BAD_FUNC_ARG))
27182- ret = WC_TEST_RET_ENC_EC(len);
27183- }
27184- /* Output one byte too small is rejected with BUFFER_E. */
27185- if (ret == 0) {
27186- len = wc_FlattenAltNames(out, (word32)expSz - 1, list);
27187- if (len != WC_NO_ERR_TRACE(BUFFER_E))
27188- ret = WC_TEST_RET_ENC_EC(len);
27189- }
27190-
27191- /* wc_SetAltNamesFromList() encodes the same list straight into a Cert and
27192- * records the length; the result must match the standalone encoding. */
27193- if (ret == 0) {
27194- cert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
27195- if (cert == NULL)
27196- ret = WC_TEST_RET_ENC_EC(MEMORY_E);
27197- }
27198- if (ret == 0) {
27199- ret = wc_InitCert_ex(cert, HEAP_HINT, devId);
27200- if (ret != 0)
27201- ret = WC_TEST_RET_ENC_EC(ret);
27202- }
27203- if (ret == 0) {
27204- ret = wc_SetAltNamesFromList(cert, list);
27205- if (ret != 0)
27206- ret = WC_TEST_RET_ENC_EC(ret);
27207- }
27208- if (ret == 0 && (cert->altNamesSz != expSz ||
27209- XMEMCMP(cert->altNames, out, (size_t)expSz) != 0))
27210- ret = WC_TEST_RET_ENC_NC;
27211- /* NULL cert is rejected. */
27212- if (ret == 0) {
27213- int r = wc_SetAltNamesFromList(NULL, list);
27214- if (r != WC_NO_ERR_TRACE(BAD_FUNC_ARG))
27215- ret = WC_TEST_RET_ENC_EC(r);
27216- }
27217- /* A NULL names list encodes to nothing: returns 0 and zeroes altNamesSz. */
27218- if (ret == 0) {
27219- cert->altNamesSz = 1; /* poison so we can see it get cleared */
27220- if (wc_SetAltNamesFromList(cert, NULL) != 0 || cert->altNamesSz != 0)
27221- ret = WC_TEST_RET_ENC_NC;
27222- }
27223-
27224- /* wc_SetDNSEntry() rejects invalid arguments at the public boundary. */
27225- if (ret == 0) {
27226- DNS_entry* badList = NULL;
27227- /* NULL str */
27228- if (wc_SetDNSEntry(HEAP_HINT, NULL, 1, ASN_DNS_TYPE, &badList)
27229- != WC_NO_ERR_TRACE(BAD_FUNC_ARG))
27230- ret = WC_TEST_RET_ENC_NC;
27231- /* NULL entries */
27232- else if (wc_SetDNSEntry(HEAP_HINT, "x", 1, ASN_DNS_TYPE, NULL)
27233- != WC_NO_ERR_TRACE(BAD_FUNC_ARG))
27234- ret = WC_TEST_RET_ENC_NC;
27235- /* negative strLen */
27236- else if (wc_SetDNSEntry(HEAP_HINT, "x", -1, ASN_DNS_TYPE, &badList)
27237- != WC_NO_ERR_TRACE(BAD_FUNC_ARG))
27238- ret = WC_TEST_RET_ENC_NC;
27239- if (badList != NULL)
27240- FreeAltNames(badList, HEAP_HINT);
27241- }
27242-
27243- XFREE(cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
27244- FreeAltNames(list, HEAP_HINT);
27245- return ret;
27246- }
27247- #endif /* WOLFSSL_CERT_GEN && WOLFSSL_ALT_NAMES && WOLFSSL_ASN_TEMPLATE &&
27248- * (WOLFSSL_TEST_CERT || OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL ||
27249- * WOLFSSL_PUBLIC_ASN) */
27250-
2725127251#define RSA_TEST_BYTES (RSA_MAX_SIZE / 8)
2725227252
2725327253#if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
0 commit comments