Skip to content

Commit 3ec882c

Browse files
authored
Merge pull request #9380 from julek-wolfssl/ip-addr-check
Improve domain and IP address matching in certificate verification
2 parents aba0246 + f95cb4e commit 3ec882c

File tree

4 files changed

+26
-18
lines changed

4 files changed

+26
-18
lines changed

.github/workflows/curl.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ jobs:
5252
- name: Install test dependencies
5353
run: |
5454
sudo apt-get update
55-
sudo apt-get install nghttp2 libpsl5 libpsl-dev python3-impacket
55+
sudo apt-get install nghttp2 libpsl5 libpsl-dev python3-impacket apache2 apache2-dev
5656
5757
- name: Download lib
5858
uses: actions/download-artifact@v4
@@ -68,9 +68,9 @@ jobs:
6868
repository: curl/curl
6969
path: curl
7070
ref: ${{ matrix.curl_ref }}
71-
configure: --with-wolfssl=$GITHUB_WORKSPACE/build-dir
71+
configure: --with-wolfssl=$GITHUB_WORKSPACE/build-dir --with-test-httpd=yes
7272
check: false
7373

7474
- name: Test curl
7575
working-directory: curl
76-
run: make -j $(nproc) test-ci
76+
run: make -j $(nproc) test-nonflaky

src/internal.c

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13103,12 +13103,14 @@ int MatchDomainName(const char* pattern, int patternLen, const char* str,
1310313103
* domain Domain name to compare against.
1310413104
* domainLen Length of the domain name.
1310513105
* checkCN Whether to check the common name.
13106+
* flags Matching flags.
13107+
* isIP Whether the domain is an IP address.
1310613108
* returns 1 : match was found.
1310713109
* 0 : no match found.
1310813110
* -1 : No matches and wild pattern match failed.
1310913111
*/
1311013112
int CheckForAltNames(DecodedCert* dCert, const char* domain, word32 domainLen,
13111-
int* checkCN, unsigned int flags)
13113+
int* checkCN, unsigned int flags, byte isIP)
1311213114
{
1311313115
int match = 0;
1311413116
DNS_entry* altName = NULL;
@@ -13117,14 +13119,13 @@ int CheckForAltNames(DecodedCert* dCert, const char* domain, word32 domainLen,
1311713119

1311813120
WOLFSSL_MSG("Checking AltNames");
1311913121

13120-
if (dCert)
13122+
if (dCert != NULL)
1312113123
altName = dCert->altNames;
1312213124

13123-
if (checkCN != NULL) {
13125+
if (checkCN != NULL)
1312413126
*checkCN = (altName == NULL) ? 1 : 0;
13125-
}
1312613127

13127-
while (altName) {
13128+
for (; altName != NULL; altName = altName->next) {
1312813129
WOLFSSL_MSG("\tindividual AltName check");
1312913130

1313013131
#ifdef WOLFSSL_IP_ALT_NAME
@@ -13139,6 +13140,12 @@ int CheckForAltNames(DecodedCert* dCert, const char* domain, word32 domainLen,
1313913140
len = (word32)altName->len;
1314013141
}
1314113142

13143+
if ((isIP && (altName->type != ASN_IP_TYPE)) ||
13144+
(!isIP && (altName->type == ASN_IP_TYPE))) {
13145+
WOLFSSL_MSG("\tAltName type mismatch, continue");
13146+
continue;
13147+
}
13148+
1314213149
if (MatchDomainName(buf, (int)len, domain, domainLen, flags)) {
1314313150
match = 1;
1314413151
if (checkCN != NULL) {
@@ -13152,8 +13159,6 @@ int CheckForAltNames(DecodedCert* dCert, const char* domain, word32 domainLen,
1315213159
match = -1;
1315313160
WOLFSSL_MSG("\twildcard match failed");
1315413161
}
13155-
13156-
altName = altName->next;
1315713162
}
1315813163

1315913164
return match;
@@ -13166,16 +13171,18 @@ int CheckForAltNames(DecodedCert* dCert, const char* domain, word32 domainLen,
1316613171
* dcert Decoded certificate.
1316713172
* domainName The domain name.
1316813173
* domainNameLen The length of the domain name.
13174+
* flags Matching flags.
13175+
* isIP Whether the domain name is an IP address.
1316913176
* returns DOMAIN_NAME_MISMATCH when no match found and 0 on success.
1317013177
*/
1317113178
int CheckHostName(DecodedCert* dCert, const char *domainName,
13172-
size_t domainNameLen, unsigned int flags)
13179+
size_t domainNameLen, unsigned int flags, byte isIP)
1317313180
{
1317413181
int checkCN;
1317513182
int ret = WC_NO_ERR_TRACE(DOMAIN_NAME_MISMATCH);
1317613183

1317713184
if (CheckForAltNames(dCert, domainName, (word32)domainNameLen,
13178-
&checkCN, flags) != 1) {
13185+
&checkCN, flags, isIP) != 1) {
1317913186
ret = DOMAIN_NAME_MISMATCH;
1318013187
WOLFSSL_MSG("DomainName match on alt names failed");
1318113188
}
@@ -13203,7 +13210,7 @@ int CheckIPAddr(DecodedCert* dCert, const char* ipasc)
1320313210
{
1320413211
WOLFSSL_MSG("Checking IPAddr");
1320513212

13206-
return CheckHostName(dCert, ipasc, (size_t)XSTRLEN(ipasc), 0);
13213+
return CheckHostName(dCert, ipasc, (size_t)XSTRLEN(ipasc), 0, 1);
1320713214
}
1320813215

1320913216

@@ -14413,7 +14420,7 @@ int DoVerifyCallback(WOLFSSL_CERT_MANAGER* cm, WOLFSSL* ssl, int cert_err,
1441314420
/* If altNames names is present, then subject common name is ignored */
1441414421
if (args->dCert->altNames != NULL) {
1441514422
if (CheckForAltNames(args->dCert, ssl->param->hostName,
14416-
(word32)XSTRLEN(ssl->param->hostName), NULL, 0) != 1) {
14423+
(word32)XSTRLEN(ssl->param->hostName), NULL, 0, 0) != 1) {
1441714424
if (cert_err == 0) {
1441814425
ret = DOMAIN_NAME_MISMATCH;
1441914426
WOLFSSL_ERROR_VERBOSE(ret);
@@ -16452,7 +16459,7 @@ int ProcessPeerCerts(WOLFSSL* ssl, byte* input, word32* inOutIdx,
1645216459
(ssl->buffers.domainName.buffer == NULL ? 0 :
1645316460
(word32)XSTRLEN(
1645416461
(const char *)ssl->buffers.domainName.buffer)),
16455-
NULL, 0) != 1) {
16462+
NULL, 0, 0) != 1) {
1645616463
WOLFSSL_MSG("DomainName match on alt names failed");
1645716464
/* try to get peer key still */
1645816465
ret = DOMAIN_NAME_MISMATCH;

src/x509.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14190,7 +14190,7 @@ int wolfSSL_X509_check_host(WOLFSSL_X509 *x, const char *chk, size_t chklen,
1419014190
chklen--;
1419114191
}
1419214192

14193-
ret = CheckHostName(dCert, (char *)chk, chklen, flags);
14193+
ret = CheckHostName(dCert, (char *)chk, chklen, flags, 0);
1419414194

1419514195
out:
1419614196

wolfssl/internal.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,7 +2210,7 @@ WOLFSSL_LOCAL int MatchDomainName(const char* pattern, int len,
22102210
#if !defined(NO_CERTS) && !defined(NO_ASN)
22112211
WOLFSSL_LOCAL int CheckForAltNames(DecodedCert* dCert, const char* domain,
22122212
word32 domainLen, int* checkCN,
2213-
unsigned int flags);
2213+
unsigned int flags, byte isIP);
22142214
WOLFSSL_LOCAL int CheckIPAddr(DecodedCert* dCert, const char* ipasc);
22152215
WOLFSSL_LOCAL void CopyDecodedName(WOLFSSL_X509_NAME* name, DecodedCert* dCert, int nameType);
22162216
#endif
@@ -6388,7 +6388,8 @@ WOLFSSL_TEST_VIS void wolfSSL_ResourceFree(WOLFSSL* ssl); /* Micrium uses */
63886388

63896389
#ifndef NO_ASN
63906390
WOLFSSL_LOCAL int CheckHostName(DecodedCert* dCert, const char *domainName,
6391-
size_t domainNameLen, unsigned int flags);
6391+
size_t domainNameLen, unsigned int flags,
6392+
byte isIP);
63926393
#endif
63936394
#endif
63946395

0 commit comments

Comments
 (0)