1 | /***************************************************************************
|
---|
2 | * _ _ ____ _
|
---|
3 | * Project ___| | | | _ \| |
|
---|
4 | * / __| | | | |_) | |
|
---|
5 | * | (__| |_| | _ <| |___
|
---|
6 | * \___|\___/|_| \_\_____|
|
---|
7 | *
|
---|
8 | * Copyright (C) 1998 - 2022, Daniel Stenberg, <[email protected]>, 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 | #include "curl_memrchr.h"
|
---|
37 |
|
---|
38 | #include "hostcheck.h"
|
---|
39 | #include "strcase.h"
|
---|
40 | #include "hostip.h"
|
---|
41 |
|
---|
42 | #include "curl_memory.h"
|
---|
43 | /* The last #include file should be: */
|
---|
44 | #include "memdebug.h"
|
---|
45 |
|
---|
46 | /* check the two input strings with given length, but do not
|
---|
47 | assume they end in nul-bytes */
|
---|
48 | static bool pmatch(const char *hostname, size_t hostlen,
|
---|
49 | const char *pattern, size_t patternlen)
|
---|
50 | {
|
---|
51 | if(hostlen != patternlen)
|
---|
52 | return FALSE;
|
---|
53 | return strncasecompare(hostname, pattern, hostlen);
|
---|
54 | }
|
---|
55 |
|
---|
56 | /*
|
---|
57 | * Match a hostname against a wildcard pattern.
|
---|
58 | * E.g.
|
---|
59 | * "foo.host.com" matches "*.host.com".
|
---|
60 | *
|
---|
61 | * We use the matching rule described in RFC6125, section 6.4.3.
|
---|
62 | * https://datatracker.ietf.org/doc/html/rfc6125#section-6.4.3
|
---|
63 | *
|
---|
64 | * In addition: ignore trailing dots in the host names and wildcards, so that
|
---|
65 | * the names are used normalized. This is what the browsers do.
|
---|
66 | *
|
---|
67 | * Do not allow wildcard matching on IP numbers. There are apparently
|
---|
68 | * certificates being used with an IP address in the CN field, thus making no
|
---|
69 | * apparent distinction between a name and an IP. We need to detect the use of
|
---|
70 | * an IP address and not wildcard match on such names.
|
---|
71 | *
|
---|
72 | * Return TRUE on a match. FALSE if not.
|
---|
73 | */
|
---|
74 |
|
---|
75 | static bool hostmatch(const char *hostname,
|
---|
76 | size_t hostlen,
|
---|
77 | const char *pattern,
|
---|
78 | size_t patternlen)
|
---|
79 | {
|
---|
80 | const char *pattern_label_end, *wildcard, *hostname_label_end;
|
---|
81 | size_t prefixlen, suffixlen;
|
---|
82 |
|
---|
83 | /* normalize pattern and hostname by stripping off trailing dots */
|
---|
84 | DEBUGASSERT(patternlen);
|
---|
85 | if(hostname[hostlen-1]=='.')
|
---|
86 | hostlen--;
|
---|
87 | if(pattern[patternlen-1]=='.')
|
---|
88 | patternlen--;
|
---|
89 |
|
---|
90 | wildcard = memchr(pattern, '*', patternlen);
|
---|
91 | if(!wildcard)
|
---|
92 | return pmatch(hostname, hostlen, pattern, patternlen);
|
---|
93 |
|
---|
94 | /* detect IP address as hostname and fail the match if so */
|
---|
95 | if(Curl_host_is_ipnum(hostname))
|
---|
96 | return FALSE;
|
---|
97 |
|
---|
98 | /* We require at least 2 dots in the pattern to avoid too wide wildcard
|
---|
99 | match. */
|
---|
100 | pattern_label_end = memchr(pattern, '.', patternlen);
|
---|
101 | if(!pattern_label_end ||
|
---|
102 | (memrchr(pattern, '.', patternlen) == pattern_label_end) ||
|
---|
103 | strncasecompare(pattern, "xn--", 4))
|
---|
104 | return pmatch(hostname, hostlen, pattern, patternlen);
|
---|
105 |
|
---|
106 | hostname_label_end = memchr(hostname, '.', hostlen);
|
---|
107 | if(!hostname_label_end)
|
---|
108 | return FALSE;
|
---|
109 | else {
|
---|
110 | size_t skiphost = hostname_label_end - hostname;
|
---|
111 | size_t skiplen = pattern_label_end - pattern;
|
---|
112 | if(!pmatch(hostname_label_end, hostlen - skiphost,
|
---|
113 | pattern_label_end, patternlen - skiplen))
|
---|
114 | return FALSE;
|
---|
115 | }
|
---|
116 | /* The wildcard must match at least one character, so the left-most
|
---|
117 | label of the hostname is at least as large as the left-most label
|
---|
118 | of the pattern. */
|
---|
119 | if(hostname_label_end - hostname < pattern_label_end - pattern)
|
---|
120 | return FALSE;
|
---|
121 |
|
---|
122 | prefixlen = wildcard - pattern;
|
---|
123 | suffixlen = pattern_label_end - (wildcard + 1);
|
---|
124 | return strncasecompare(pattern, hostname, prefixlen) &&
|
---|
125 | strncasecompare(wildcard + 1, hostname_label_end - suffixlen,
|
---|
126 | suffixlen) ? TRUE : FALSE;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /*
|
---|
130 | * Curl_cert_hostcheck() returns TRUE if a match and FALSE if not.
|
---|
131 | */
|
---|
132 | bool Curl_cert_hostcheck(const char *match, size_t matchlen,
|
---|
133 | const char *hostname, size_t hostlen)
|
---|
134 | {
|
---|
135 | if(match && *match && hostname && *hostname)
|
---|
136 | return hostmatch(hostname, hostlen, match, matchlen);
|
---|
137 | return FALSE;
|
---|
138 | }
|
---|
139 |
|
---|
140 | #endif /* OPENSSL, GSKIT or schannel+wince */
|
---|