1 | /***************************************************************************
|
---|
2 | * _ _ ____ _
|
---|
3 | * Project ___| | | | _ \| |
|
---|
4 | * / __| | | | |_) | |
|
---|
5 | * | (__| |_| | _ <| |___
|
---|
6 | * \___|\___/|_| \_\_____|
|
---|
7 | *
|
---|
8 | * Copyright (C) 1998 - 2017, 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.haxx.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_AXTLS) \
|
---|
27 | || defined(USE_GSKIT) \
|
---|
28 | || (defined(USE_SCHANNEL) && defined(_WIN32_WCE))
|
---|
29 | /* these backends use functions from this file */
|
---|
30 |
|
---|
31 | #ifdef HAVE_NETINET_IN_H
|
---|
32 | #include <netinet/in.h>
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | #include "hostcheck.h"
|
---|
36 | #include "strcase.h"
|
---|
37 | #include "inet_pton.h"
|
---|
38 |
|
---|
39 | #include "curl_memory.h"
|
---|
40 | /* The last #include file should be: */
|
---|
41 | #include "memdebug.h"
|
---|
42 |
|
---|
43 | /*
|
---|
44 | * Match a hostname against a wildcard pattern.
|
---|
45 | * E.g.
|
---|
46 | * "foo.host.com" matches "*.host.com".
|
---|
47 | *
|
---|
48 | * We use the matching rule described in RFC6125, section 6.4.3.
|
---|
49 | * https://tools.ietf.org/html/rfc6125#section-6.4.3
|
---|
50 | *
|
---|
51 | * In addition: ignore trailing dots in the host names and wildcards, so that
|
---|
52 | * the names are used normalized. This is what the browsers do.
|
---|
53 | *
|
---|
54 | * Do not allow wildcard matching on IP numbers. There are apparently
|
---|
55 | * certificates being used with an IP address in the CN field, thus making no
|
---|
56 | * apparent distinction between a name and an IP. We need to detect the use of
|
---|
57 | * an IP address and not wildcard match on such names.
|
---|
58 | *
|
---|
59 | * NOTE: hostmatch() gets called with copied buffers so that it can modify the
|
---|
60 | * contents at will.
|
---|
61 | */
|
---|
62 |
|
---|
63 | static int hostmatch(char *hostname, char *pattern)
|
---|
64 | {
|
---|
65 | const char *pattern_label_end, *pattern_wildcard, *hostname_label_end;
|
---|
66 | int wildcard_enabled;
|
---|
67 | size_t prefixlen, suffixlen;
|
---|
68 | struct in_addr ignored;
|
---|
69 | #ifdef ENABLE_IPV6
|
---|
70 | struct sockaddr_in6 si6;
|
---|
71 | #endif
|
---|
72 |
|
---|
73 | /* normalize pattern and hostname by stripping off trailing dots */
|
---|
74 | size_t len = strlen(hostname);
|
---|
75 | if(hostname[len-1]=='.')
|
---|
76 | hostname[len-1] = 0;
|
---|
77 | len = strlen(pattern);
|
---|
78 | if(pattern[len-1]=='.')
|
---|
79 | pattern[len-1] = 0;
|
---|
80 |
|
---|
81 | pattern_wildcard = strchr(pattern, '*');
|
---|
82 | if(pattern_wildcard == NULL)
|
---|
83 | return strcasecompare(pattern, hostname) ?
|
---|
84 | CURL_HOST_MATCH : CURL_HOST_NOMATCH;
|
---|
85 |
|
---|
86 | /* detect IP address as hostname and fail the match if so */
|
---|
87 | if(Curl_inet_pton(AF_INET, hostname, &ignored) > 0)
|
---|
88 | return CURL_HOST_NOMATCH;
|
---|
89 | #ifdef ENABLE_IPV6
|
---|
90 | if(Curl_inet_pton(AF_INET6, hostname, &si6.sin6_addr) > 0)
|
---|
91 | return CURL_HOST_NOMATCH;
|
---|
92 | #endif
|
---|
93 |
|
---|
94 | /* We require at least 2 dots in pattern to avoid too wide wildcard
|
---|
95 | match. */
|
---|
96 | wildcard_enabled = 1;
|
---|
97 | pattern_label_end = strchr(pattern, '.');
|
---|
98 | if(pattern_label_end == NULL || strchr(pattern_label_end + 1, '.') == NULL ||
|
---|
99 | pattern_wildcard > pattern_label_end ||
|
---|
100 | strncasecompare(pattern, "xn--", 4)) {
|
---|
101 | wildcard_enabled = 0;
|
---|
102 | }
|
---|
103 | if(!wildcard_enabled)
|
---|
104 | return strcasecompare(pattern, hostname) ?
|
---|
105 | CURL_HOST_MATCH : CURL_HOST_NOMATCH;
|
---|
106 |
|
---|
107 | hostname_label_end = strchr(hostname, '.');
|
---|
108 | if(hostname_label_end == NULL ||
|
---|
109 | !strcasecompare(pattern_label_end, hostname_label_end))
|
---|
110 | return CURL_HOST_NOMATCH;
|
---|
111 |
|
---|
112 | /* The wildcard must match at least one character, so the left-most
|
---|
113 | label of the hostname is at least as large as the left-most label
|
---|
114 | of the pattern. */
|
---|
115 | if(hostname_label_end - hostname < pattern_label_end - pattern)
|
---|
116 | return CURL_HOST_NOMATCH;
|
---|
117 |
|
---|
118 | prefixlen = pattern_wildcard - pattern;
|
---|
119 | suffixlen = pattern_label_end - (pattern_wildcard + 1);
|
---|
120 | return strncasecompare(pattern, hostname, prefixlen) &&
|
---|
121 | strncasecompare(pattern_wildcard + 1, hostname_label_end - suffixlen,
|
---|
122 | suffixlen) ?
|
---|
123 | CURL_HOST_MATCH : CURL_HOST_NOMATCH;
|
---|
124 | }
|
---|
125 |
|
---|
126 | int Curl_cert_hostcheck(const char *match_pattern, const char *hostname)
|
---|
127 | {
|
---|
128 | char *matchp;
|
---|
129 | char *hostp;
|
---|
130 | int res = 0;
|
---|
131 | if(!match_pattern || !*match_pattern ||
|
---|
132 | !hostname || !*hostname) /* sanity check */
|
---|
133 | ;
|
---|
134 | else {
|
---|
135 | matchp = strdup(match_pattern);
|
---|
136 | if(matchp) {
|
---|
137 | hostp = strdup(hostname);
|
---|
138 | if(hostp) {
|
---|
139 | if(hostmatch(hostp, matchp) == CURL_HOST_MATCH)
|
---|
140 | res = 1;
|
---|
141 | free(hostp);
|
---|
142 | }
|
---|
143 | free(matchp);
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | return res;
|
---|
148 | }
|
---|
149 |
|
---|
150 | #endif /* OPENSSL, AXTLS, GSKIT or schannel+wince */
|
---|