1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2013, 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 http://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_SSLEAY) || defined(USE_AXTLS) || defined(USE_QSOSSL) || \
27 /* these backends use functions from this file */
29 #include "hostcheck.h"
33 * Match a hostname against a wildcard pattern.
35 * "foo.host.com" matches "*.host.com".
37 * We use the matching rule described in RFC6125, section 6.4.3.
38 * http://tools.ietf.org/html/rfc6125#section-6.4.3
41 static int hostmatch(const char *hostname, const char *pattern)
43 const char *pattern_label_end, *pattern_wildcard, *hostname_label_end;
45 size_t prefixlen, suffixlen;
46 pattern_wildcard = strchr(pattern, '*');
47 if(pattern_wildcard == NULL)
48 return Curl_raw_equal(pattern, hostname) ?
49 CURL_HOST_MATCH : CURL_HOST_NOMATCH;
51 /* We require at least 2 dots in pattern to avoid too wide wildcard
54 pattern_label_end = strchr(pattern, '.');
55 if(pattern_label_end == NULL || strchr(pattern_label_end+1, '.') == NULL ||
56 pattern_wildcard > pattern_label_end ||
57 Curl_raw_nequal(pattern, "xn--", 4)) {
61 return Curl_raw_equal(pattern, hostname) ?
62 CURL_HOST_MATCH : CURL_HOST_NOMATCH;
64 hostname_label_end = strchr(hostname, '.');
65 if(hostname_label_end == NULL ||
66 !Curl_raw_equal(pattern_label_end, hostname_label_end))
67 return CURL_HOST_NOMATCH;
69 /* The wildcard must match at least one character, so the left-most
70 label of the hostname is at least as large as the left-most label
72 if(hostname_label_end - hostname < pattern_label_end - pattern)
73 return CURL_HOST_NOMATCH;
75 prefixlen = pattern_wildcard - pattern;
76 suffixlen = pattern_label_end - (pattern_wildcard+1);
77 return Curl_raw_nequal(pattern, hostname, prefixlen) &&
78 Curl_raw_nequal(pattern_wildcard+1, hostname_label_end - suffixlen,
80 CURL_HOST_MATCH : CURL_HOST_NOMATCH;
83 int Curl_cert_hostcheck(const char *match_pattern, const char *hostname)
85 if(!match_pattern || !*match_pattern ||
86 !hostname || !*hostname) /* sanity check */
89 if(Curl_raw_equal(hostname, match_pattern)) /* trivial case */
92 if(hostmatch(hostname,match_pattern) == CURL_HOST_MATCH)
97 #endif /* SSLEAY or AXTLS or QSOSSL or GSKIT */