1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
25 #if defined(USE_OPENSSL) || defined(USE_AXTLS) || defined(USE_GSKIT)
26 /* these backends use functions from this file */
28 #ifdef HAVE_NETINET_IN_H
29 #include <netinet/in.h>
32 #include "hostcheck.h"
34 #include "inet_pton.h"
36 #include "curl_memory.h"
37 /* The last #include file should be: */
41 * Match a hostname against a wildcard pattern.
43 * "foo.host.com" matches "*.host.com".
45 * We use the matching rule described in RFC6125, section 6.4.3.
46 * https://tools.ietf.org/html/rfc6125#section-6.4.3
48 * In addition: ignore trailing dots in the host names and wildcards, so that
49 * the names are used normalized. This is what the browsers do.
51 * Do not allow wildcard matching on IP numbers. There are apparently
52 * certificates being used with an IP address in the CN field, thus making no
53 * apparent distinction between a name and an IP. We need to detect the use of
54 * an IP address and not wildcard match on such names.
56 * NOTE: hostmatch() gets called with copied buffers so that it can modify the
60 static int hostmatch(char *hostname, char *pattern)
62 const char *pattern_label_end, *pattern_wildcard, *hostname_label_end;
64 size_t prefixlen, suffixlen;
65 struct in_addr ignored;
67 struct sockaddr_in6 si6;
70 /* normalize pattern and hostname by stripping off trailing dots */
71 size_t len = strlen(hostname);
72 if(hostname[len-1]=='.')
74 len = strlen(pattern);
75 if(pattern[len-1]=='.')
78 pattern_wildcard = strchr(pattern, '*');
79 if(pattern_wildcard == NULL)
80 return Curl_raw_equal(pattern, hostname) ?
81 CURL_HOST_MATCH : CURL_HOST_NOMATCH;
83 /* detect IP address as hostname and fail the match if so */
84 if(Curl_inet_pton(AF_INET, hostname, &ignored) > 0)
85 return CURL_HOST_NOMATCH;
87 else if(Curl_inet_pton(AF_INET6, hostname, &si6.sin6_addr) > 0)
88 return CURL_HOST_NOMATCH;
91 /* We require at least 2 dots in pattern to avoid too wide wildcard
94 pattern_label_end = strchr(pattern, '.');
95 if(pattern_label_end == NULL || strchr(pattern_label_end+1, '.') == NULL ||
96 pattern_wildcard > pattern_label_end ||
97 Curl_raw_nequal(pattern, "xn--", 4)) {
100 if(!wildcard_enabled)
101 return Curl_raw_equal(pattern, hostname) ?
102 CURL_HOST_MATCH : CURL_HOST_NOMATCH;
104 hostname_label_end = strchr(hostname, '.');
105 if(hostname_label_end == NULL ||
106 !Curl_raw_equal(pattern_label_end, hostname_label_end))
107 return CURL_HOST_NOMATCH;
109 /* The wildcard must match at least one character, so the left-most
110 label of the hostname is at least as large as the left-most label
112 if(hostname_label_end - hostname < pattern_label_end - pattern)
113 return CURL_HOST_NOMATCH;
115 prefixlen = pattern_wildcard - pattern;
116 suffixlen = pattern_label_end - (pattern_wildcard+1);
117 return Curl_raw_nequal(pattern, hostname, prefixlen) &&
118 Curl_raw_nequal(pattern_wildcard+1, hostname_label_end - suffixlen,
120 CURL_HOST_MATCH : CURL_HOST_NOMATCH;
123 int Curl_cert_hostcheck(const char *match_pattern, const char *hostname)
128 if(!match_pattern || !*match_pattern ||
129 !hostname || !*hostname) /* sanity check */
132 matchp = strdup(match_pattern);
134 hostp = strdup(hostname);
136 if(hostmatch(hostp, matchp) == CURL_HOST_MATCH)
147 #endif /* OPENSSL or AXTLS or GSKIT */