Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added certs/test/cert-ext-nc-empty-dns.der
Binary file not shown.
25 changes: 25 additions & 0 deletions certs/test/cert-ext-nc-empty-dns.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-----BEGIN CERTIFICATE-----
MIIEOjCCAyKgAwIBAgIUfXRbq72kTP0Y2LdmBbT7cx6Z3H8wDQYJKoZIhvcNAQEL
BQAwezELMAkGA1UEBhMCQVUxEzARBgNVBAgMClF1ZWVuc2xhbmQxETAPBgNVBAcM
CEJyaXNiYW5lMRQwEgYDVQQKDAt3b2xmU1NMIEluYzEUMBIGA1UECwwLRW5naW5l
ZXJpbmcxGDAWBgNVBAMMD3d3dy53b2xmc3NsLmNvbTAeFw0yNjExMDkxMzQ2NTRa
Fw0yOTA4MDUxMzQ2NTRaMHsxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApRdWVlbnNs
YW5kMREwDwYDVQQHDAhCcmlzYmFuZTEUMBIGA1UECgwLd29sZlNTTCBJbmMxFDAS
BgNVBAsMC0VuZ2luZWVyaW5nMRgwFgYDVQQDDA93d3cud29sZnNzbC5jb20wggEi
MA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDAlQjhV0HycW230kVBJwFlxkWu
8rwkMLiVzi9O1vYciLx8n/uoZ3/+XJxRdfeKygfnNS+P4b17wC98q2SoF/zKXXu6
4CHlci5vLobYlXParBtTuV8/1xkNJU/hY2NRiwtkP61DuKUcXDSzrgCgY8X2fwtZ
aHhzpowYqQJtr8MZAS64EOPGzEC0aaNGM2mHbsS7F6bz6N2tc7x7LyG1/WZRDL1U
s+FtXxy8I3PRCQOJFNIQuWTDKtChlkq84dQaW8egwMFjeA9ENzAyloAyI5Whd7oT
0pdz4l0lyWoNwzlgpLSwaUJCCenYCLwzILNYIqeq68Th5mGDxdKW39nQT63XAgMB
AAGjgbUwgbIwHQYDVR0OBBYEFLMRMsmSmITiyfjQO24DQsofDo48MB8GA1UdIwQY
MBaAFLMRMsmSmITiyfjQO24DQsofDo48MBIGA1UdEwEB/wQIMAYBAf8CAQAwDgYD
VR0PAQH/BAQDAgGGMBIGA1UdHgEB/wQIMAahBDACggAwOAYJYIZIAYb4QgENBCsW
KVRlc3RpbmcgZW1wdHkgZXhjbHVkZWQgZE5TTmFtZSBjb25zdHJhaW50MA0GCSqG
SIb3DQEBCwUAA4IBAQCZ0NlgvDZOuCSuS5fzO3PXLxm3Ar4w1UR290wIsSQhvOHW
WQLE2z5JezwfYE6Qq74qpUXNnxvkD1Zov2L0zm91Ch+jYpK7Br8pmI+xhIO9Osuz
jLihRWXhIDLGSKPVIPy1XUfBGHVbHHDepXuf+K56gl36QJCmpL0nh8G9PH85F1D/
i7eLZy5oiQw9DMFvm5PifOkL1YEqnhC3pTI/8eYPt7gMeBfJzuCblKM7+Xi0wKFO
hywvnxZ9Pu53ME4l9jtU6G8Bt/dcnT3I4JISBDC9yXsb9iZBJOxAb1DbEQ/J/2bj
qeAyMn0AyAg/7RtJ0MGR85qOYkOjwY12BY0zSjO5
-----END CERTIFICATE-----
32 changes: 32 additions & 0 deletions certs/test/gen-ext-certs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,38 @@ EOF
gen_cert
rm -f ./certs/test/cert-ext-ncdns.cfg

# excluded;DNS: with a zero-length base, the CA/Browser Forum encoding for a
# CA that may not issue DNS names. The OpenSSL config syntax cannot express an
# empty dNSName, so the extension is given as DER.
OUT=certs/test/cert-ext-nc-empty-dns
KEYFILE=certs/test/cert-ext-nc-empty-dns-key.der
CONFIG=certs/test/cert-ext-nc-empty-dns.cfg
tee >$CONFIG <<EOF
[ req ]
distinguished_name = req_distinguished_name
prompt = no
x509_extensions = v3_ca

[ req_distinguished_name ]
C = AU
ST = Queensland
L = Brisbane
O = wolfSSL Inc
OU = Engineering
CN = www.wolfssl.com

[ v3_ca ]
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
basicConstraints = critical, CA:true, pathlen:0
keyUsage = critical, digitalSignature, cRLSign, keyCertSign
nameConstraints = critical,DER:30:06:A1:04:30:02:82:00
nsComment = "Testing empty excluded dNSName constraint"

EOF
gen_cert
rm -f ./certs/test/cert-ext-nc-empty-dns.cfg

OUT=certs/test/cert-ext-nc-combined
KEYFILE=certs/test/cert-ext-nc-combined-key.der
CONFIG=certs/test/cert-ext-nc-combined.cfg
Expand Down
2 changes: 2 additions & 0 deletions certs/test/include.am
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ EXTRA_DIST += \
certs/test/cert-ext-ncrid.pem \
certs/test/cert-ext-ncdns.der \
certs/test/cert-ext-ncdns.pem \
certs/test/cert-ext-nc-empty-dns.der \
certs/test/cert-ext-nc-empty-dns.pem \
certs/test/cert-ext-ncmulti.der \
certs/test/cert-ext-ncmulti.pem \
certs/test/cert-ext-ncmixed.der \
Expand Down
14 changes: 11 additions & 3 deletions src/x509.c
Original file line number Diff line number Diff line change
Expand Up @@ -6070,7 +6070,8 @@ static int MatchIpName(const char* name, int nameSz, WOLFSSL_GENERAL_NAME* gn)
/* Helper to check if name string matches a single GENERAL_NAME constraint.
* permitted selects subtree semantics for wildcard DNS names: containment
* for permitted subtrees, intersection for excluded subtrees.
* Returns 1 if matches, 0 if not. */
* Returns 1 if matches, 0 if not, and a negative error if the name cannot be
* compared, which the caller must treat as failing the constraint. */
static int MatchNameConstraint(int type, const char* name, int nameSz,
WOLFSSL_GENERAL_NAME* gn, int permitted)
{
Expand All @@ -6093,6 +6094,11 @@ static int MatchNameConstraint(int type, const char* name, int nameSz,
}
baseStr = (const char*)wolfSSL_ASN1_STRING_get0_data(gn->d.ia5);
baseLen = wolfSSL_ASN1_STRING_length(gn->d.ia5);
if (type == WOLFSSL_GEN_DNS && baseLen == 0) {
/* RFC 5280 4.2.1.10: an empty dNSName base covers every DNS
* name. */
return 1;
}
if (baseStr == NULL || baseLen <= 0) {
return 0;
}
Expand Down Expand Up @@ -6194,7 +6200,8 @@ int wolfSSL_NAME_CONSTRAINTS_check_name(WOLFSSL_NAME_CONSTRAINTS* nc,
}
hasPermittedType = 1;

if (MatchNameConstraint(type, name, nameSz, gn, 1)) {
/* A name that cannot be compared is not shown to be permitted. */
if (MatchNameConstraint(type, name, nameSz, gn, 1) == 1) {
matchedPermitted = 1;
break;
}
Expand All @@ -6221,7 +6228,8 @@ int wolfSSL_NAME_CONSTRAINTS_check_name(WOLFSSL_NAME_CONSTRAINTS* nc,
continue;
}

if (MatchNameConstraint(type, name, nameSz, gn, 0)) {
/* A name that cannot be compared is not shown to be outside. */
if (MatchNameConstraint(type, name, nameSz, gn, 0) != 0) {
WOLFSSL_MSG("Name in excluded subtrees");
return 0;
}
Expand Down
85 changes: 71 additions & 14 deletions tests/api/test_asn.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
#include <wolfssl/wolfcrypt/asn_public.h>
#include <wolfssl/wolfcrypt/random.h>
#include <wolfssl/wolfcrypt/rsa.h>

/* A lone "." dNSName constraint base covers every DNS name, except with
* OPENSSL_COMPATIBLE_DEFAULTS where it matches no name. */
#ifdef OPENSSL_COMPATIBLE_DEFAULTS
#define NC_DNS_DOT_BASE_MATCH 0
#else
#define NC_DNS_DOT_BASE_MATCH 1
#endif
#ifdef HAVE_ED25519
#include <wolfssl/wolfcrypt/ed25519.h>
#endif
Expand Down Expand Up @@ -1048,21 +1056,52 @@ int test_wolfssl_local_MatchBaseName(void)
/* user@sub.domain.com should NOT match domain.com (exact domain only) */
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
"user@sub.domain.com", 19, "domain.com", 10), 0);
/* @ at start is invalid */
/* A mailbox without a local-part or host-part cannot be compared */
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
"@domain.com", 11, ".domain.com", 11),
WC_NO_ERR_TRACE(ASN_PARSE_E));
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
"user@", 5, ".domain.com", 11),
WC_NO_ERR_TRACE(ASN_PARSE_E));
/* Validated before the name-shorter-than-base return */
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
"u@", 2, "evil.com", 8), WC_NO_ERR_TRACE(ASN_PARSE_E));
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
"x@evil.com@", 11, "evil.com", 8),
WC_NO_ERR_TRACE(ASN_PARSE_E));
/* A host-part starting with '.' cannot be compared */
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
"x@.evil.com", 11, "evil.com", 8),
WC_NO_ERR_TRACE(ASN_PARSE_E));
/* A local-part starting with '.' is judged by its host-part */
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
".x@evil.com", 11, "evil.com", 8), 1);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
"@domain.com", 11, ".domain.com", 11), 0);
/* @ at end is invalid */
".x@good.com", 11, "evil.com", 8), 0);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
"user@", 5, ".domain.com", 11), 0);
/* double @ is invalid */
".x@sub.evil.com", 15, ".evil.com", 9), 1);
/* An '@' outside a quoted local-part cannot be compared */
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
"user@@domain.com", 16, ".domain.com", 11), 0);
/* multiple @ is invalid */
"user@@domain.com", 16, ".domain.com", 11),
WC_NO_ERR_TRACE(ASN_PARSE_E));
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
"user@domain@extra.com", 21, ".domain.com", 11),
WC_NO_ERR_TRACE(ASN_PARSE_E));
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
"user@domain@extra.com", 21, "extra.com", 9),
WC_NO_ERR_TRACE(ASN_PARSE_E));
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
"x@evil.com@good.com", 19, "good.com", 8),
WC_NO_ERR_TRACE(ASN_PARSE_E));
/* RFC 5322 quoted local-part containing @ */
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
"user@domain@extra.com", 21, ".domain.com", 11), 0);
"\"x@y\"@evil.com", 14, "evil.com", 8), 1);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
"\"x@y\"@good.com", 14, "evil.com", 8), 0);
/* No @ in email name */
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
"userdomain.com", 14, ".domain.com", 11), 0);
"userdomain.com", 14, ".domain.com", 11),
WC_NO_ERR_TRACE(ASN_PARSE_E));
/* Email domain doesn't match constraint */
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
"user@other.com", 14, ".domain.com", 11), 0);
Expand Down Expand Up @@ -1725,14 +1764,26 @@ int test_wolfssl_local_MatchBaseName(void)
/* Empty/zero size */
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE,
"", 0, "domain.com", 10), 0);
/* RFC 5280 4.2.1.10: an empty dNSName base covers every DNS name. */
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE,
"domain.com", 10, "", 0), 1);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE,
"domain.com", 10, "", 0), 0);
"domain.com", 10, ".", 1), NC_DNS_DOT_BASE_MATCH);
ExpectIntEQ(wolfssl_local_MatchDnsConstraintWildcard(
"*.domain.com", 12, "", 0, 0), 1);
ExpectIntEQ(wolfssl_local_MatchDnsConstraintWildcard(
"*.domain.com", 12, ".", 1, 1), NC_DNS_DOT_BASE_MATCH);
/* An empty rfc822Name base names no host. */
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_RFC822_TYPE,
"user@domain.com", 15, "", 0), 0);
/* Invalid type */
ExpectIntEQ(wolfssl_local_MatchBaseName(0xFF,
"domain.com", 10, "domain.com", 10), 0);
/* Name starting with dot */
/* Name starting with dot, whatever the base */
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE,
".", 1, ".", 1), 0);
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE,
".domain.com", 11, "", 0), 0);
/* Name shorter than base */
ExpectIntEQ(wolfssl_local_MatchBaseName(ASN_DNS_TYPE,
"a.com", 5, "domain.com", 10), 0);
Expand Down Expand Up @@ -1858,13 +1909,16 @@ int test_wolfssl_local_MatchDnsConstraintWildcard(void)
NULL, 3, 1), 0);
ExpectIntEQ(wolfssl_local_MatchDnsConstraintWildcard("*.com", 0,
"com", 3, 1), 0);
/* A zero-length base covers every DNS name (RFC 5280 4.2.1.10). */
ExpectIntEQ(wolfssl_local_MatchDnsConstraintWildcard("*.com", 5,
"com", 0, 1), 0);
"com", 0, 1), 1);
/* Name beginning with a dot is invalid. */
ExpectIntEQ(dnsWildPermitted(".x.com", "com"), 0);
ExpectIntEQ(dnsWildExcluded(".x.com", "com"), 0);
/* Base that is only dots collapses to nothing. */
ExpectIntEQ(dnsWildExcluded("*.example.com", "."), 0);
/* A lone dot is the empty base unless OpenSSL compatible. */
ExpectIntEQ(dnsWildExcluded("*.example.com", "."),
NC_DNS_DOT_BASE_MATCH);
/* A base of more than one dot has an empty label. */
ExpectIntEQ(dnsWildExcluded("*.example.com", ".."), 0);
/* SAN has an empty interior label ("*..com"), but only the right-most
* "com" label overlaps the base "com" -- the empty label sits outside the
Expand Down Expand Up @@ -1892,6 +1946,9 @@ int test_wolfssl_local_MatchUriNameConstraint(void)
* Exact host match (no leading dot in the constraint).
*/
ExpectIntEQ(uriNC("https://host.com/path", "host.com"), 1);
/* A lone "." base is a leading-dot base with no labels. */
ExpectIntEQ(uriNC("https://host.com/path", "."),
NC_DNS_DOT_BASE_MATCH);
ExpectIntEQ(uriNC("https://host.com", "host.com"), 1);
ExpectIntEQ(uriNC("https://host.com:8443/x", "host.com"), 1);
ExpectIntEQ(uriNC("ftp://user@host.com/x", "host.com"), 1);
Expand Down
Loading
Loading