abd1fa0c3c771510b93475184ab93000e16f3ec3
[platform/upstream/curl.git] / lib / hostcheck.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2013, 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 http://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_SSLEAY) || defined(USE_AXTLS) || defined(USE_QSOSSL) || \
26     defined(USE_GSKIT)
27 /* these backends use functions from this file */
28
29 #include "hostcheck.h"
30 #include "rawstr.h"
31
32 /*
33  * Match a hostname against a wildcard pattern.
34  * E.g.
35  *  "foo.host.com" matches "*.host.com".
36  *
37  * We use the matching rule described in RFC6125, section 6.4.3.
38  * http://tools.ietf.org/html/rfc6125#section-6.4.3
39  */
40
41 static int hostmatch(const char *hostname, const char *pattern)
42 {
43   const char *pattern_label_end, *pattern_wildcard, *hostname_label_end;
44   int wildcard_enabled;
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;
50
51   /* We require at least 2 dots in pattern to avoid too wide wildcard
52      match. */
53   wildcard_enabled = 1;
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)) {
58     wildcard_enabled = 0;
59   }
60   if(!wildcard_enabled)
61     return Curl_raw_equal(pattern, hostname) ?
62       CURL_HOST_MATCH : CURL_HOST_NOMATCH;
63
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;
68
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
71      of the pattern. */
72   if(hostname_label_end - hostname < pattern_label_end - pattern)
73     return CURL_HOST_NOMATCH;
74
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,
79                     suffixlen) ?
80     CURL_HOST_MATCH : CURL_HOST_NOMATCH;
81 }
82
83 int Curl_cert_hostcheck(const char *match_pattern, const char *hostname)
84 {
85   if(!match_pattern || !*match_pattern ||
86       !hostname || !*hostname) /* sanity check */
87     return 0;
88
89   if(Curl_raw_equal(hostname, match_pattern)) /* trivial case */
90     return 1;
91
92   if(hostmatch(hostname,match_pattern) == CURL_HOST_MATCH)
93     return 1;
94   return 0;
95 }
96
97 #endif /* SSLEAY or AXTLS or QSOSSL or GSKIT */