1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22
23 #include "curl_setup.h"
24
25 #if defined(USE_OPENSSL) \
26 || defined(USE_GSKIT) \
27 || defined(USE_SCHANNEL)
28 /* these backends use functions from this file */
29
30 #ifdef HAVE_NETINET_IN_H
31 #include <netinet/in.h>
32 #endif
33 #ifdef HAVE_NETINET_IN6_H
34 #include <netinet/in6.h>
35 #endif
36
37 #include "hostcheck.h"
38 #include "strcase.h"
39 #include "hostip.h"
40
41 #include "curl_memory.h"
42 /* The last #include file should be: */
43 #include "memdebug.h"
44
45 /*
46 * Match a hostname against a wildcard pattern.
47 * E.g.
48 * "foo.host.com" matches "*.host.com".
49 *
50 * We use the matching rule described in RFC6125, section 6.4.3.
51 * https://tools.ietf.org/html/rfc6125#section-6.4.3
52 *
53 * In addition: ignore trailing dots in the host names and wildcards, so that
54 * the names are used normalized. This is what the browsers do.
55 *
56 * Do not allow wildcard matching on IP numbers. There are apparently
57 * certificates being used with an IP address in the CN field, thus making no
58 * apparent distinction between a name and an IP. We need to detect the use of
59 * an IP address and not wildcard match on such names.
60 *
61 * NOTE: hostmatch() gets called with copied buffers so that it can modify the
62 * contents at will.
63 */
64
hostmatch(char * hostname,char * pattern)65 static int hostmatch(char *hostname, char *pattern)
66 {
67 const char *pattern_label_end, *pattern_wildcard, *hostname_label_end;
68 int wildcard_enabled;
69 size_t prefixlen, suffixlen;
70
71 /* normalize pattern and hostname by stripping off trailing dots */
72 size_t len = strlen(hostname);
73 if(hostname[len-1]=='.')
74 hostname[len-1] = 0;
75 len = strlen(pattern);
76 if(pattern[len-1]=='.')
77 pattern[len-1] = 0;
78
79 pattern_wildcard = strchr(pattern, '*');
80 if(!pattern_wildcard)
81 return strcasecompare(pattern, hostname) ?
82 CURL_HOST_MATCH : CURL_HOST_NOMATCH;
83
84 /* detect IP address as hostname and fail the match if so */
85 if(Curl_host_is_ipnum(hostname))
86 return CURL_HOST_NOMATCH;
87
88 /* We require at least 2 dots in pattern to avoid too wide wildcard
89 match. */
90 wildcard_enabled = 1;
91 pattern_label_end = strchr(pattern, '.');
92 if(!pattern_label_end || strchr(pattern_label_end + 1, '.') == NULL ||
93 pattern_wildcard > pattern_label_end ||
94 strncasecompare(pattern, "xn--", 4)) {
95 wildcard_enabled = 0;
96 }
97 if(!wildcard_enabled)
98 return strcasecompare(pattern, hostname) ?
99 CURL_HOST_MATCH : CURL_HOST_NOMATCH;
100
101 hostname_label_end = strchr(hostname, '.');
102 if(!hostname_label_end ||
103 !strcasecompare(pattern_label_end, hostname_label_end))
104 return CURL_HOST_NOMATCH;
105
106 /* The wildcard must match at least one character, so the left-most
107 label of the hostname is at least as large as the left-most label
108 of the pattern. */
109 if(hostname_label_end - hostname < pattern_label_end - pattern)
110 return CURL_HOST_NOMATCH;
111
112 prefixlen = pattern_wildcard - pattern;
113 suffixlen = pattern_label_end - (pattern_wildcard + 1);
114 return strncasecompare(pattern, hostname, prefixlen) &&
115 strncasecompare(pattern_wildcard + 1, hostname_label_end - suffixlen,
116 suffixlen) ?
117 CURL_HOST_MATCH : CURL_HOST_NOMATCH;
118 }
119
Curl_cert_hostcheck(const char * match_pattern,const char * hostname)120 int Curl_cert_hostcheck(const char *match_pattern, const char *hostname)
121 {
122 int res = 0;
123 if(!match_pattern || !*match_pattern ||
124 !hostname || !*hostname) /* sanity check */
125 ;
126 else {
127 char *matchp = strdup(match_pattern);
128 if(matchp) {
129 char *hostp = strdup(hostname);
130 if(hostp) {
131 if(hostmatch(hostp, matchp) == CURL_HOST_MATCH)
132 res = 1;
133 free(hostp);
134 }
135 free(matchp);
136 }
137 }
138
139 return res;
140 }
141
142 #endif /* OPENSSL, GSKIT or schannel+wince */
143