build: fix circular header inclusion with other packages
[platform/upstream/curl.git] / lib / hostcheck.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2012, 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)
26 /* these two backends use functions from this file */
27
28 #include "hostcheck.h"
29 #include "rawstr.h"
30
31 /*
32  * Match a hostname against a wildcard pattern.
33  * E.g.
34  *  "foo.host.com" matches "*.host.com".
35  *
36  * We use the matching rule described in RFC6125, section 6.4.3.
37  * http://tools.ietf.org/html/rfc6125#section-6.4.3
38  */
39
40 static int hostmatch(const char *hostname, const char *pattern)
41 {
42   const char *pattern_label_end, *pattern_wildcard, *hostname_label_end;
43   int wildcard_enabled;
44   size_t prefixlen, suffixlen;
45   pattern_wildcard = strchr(pattern, '*');
46   if(pattern_wildcard == NULL)
47     return Curl_raw_equal(pattern, hostname) ?
48       CURL_HOST_MATCH : CURL_HOST_NOMATCH;
49
50   /* We require at least 2 dots in pattern to avoid too wide wildcard
51      match. */
52   wildcard_enabled = 1;
53   pattern_label_end = strchr(pattern, '.');
54   if(pattern_label_end == NULL || strchr(pattern_label_end+1, '.') == NULL ||
55      pattern_wildcard > pattern_label_end ||
56      Curl_raw_nequal(pattern, "xn--", 4)) {
57     wildcard_enabled = 0;
58   }
59   if(!wildcard_enabled)
60     return Curl_raw_equal(pattern, hostname) ?
61       CURL_HOST_MATCH : CURL_HOST_NOMATCH;
62
63   hostname_label_end = strchr(hostname, '.');
64   if(hostname_label_end == NULL ||
65      !Curl_raw_equal(pattern_label_end, hostname_label_end))
66     return CURL_HOST_NOMATCH;
67
68   /* The wildcard must match at least one character, so the left-most
69      label of the hostname is at least as large as the left-most label
70      of the pattern. */
71   if(hostname_label_end - hostname < pattern_label_end - pattern)
72     return CURL_HOST_NOMATCH;
73
74   prefixlen = pattern_wildcard - pattern;
75   suffixlen = pattern_label_end - (pattern_wildcard+1);
76   return Curl_raw_nequal(pattern, hostname, prefixlen) &&
77     Curl_raw_nequal(pattern_wildcard+1, hostname_label_end - suffixlen,
78                     suffixlen) ?
79     CURL_HOST_MATCH : CURL_HOST_NOMATCH;
80 }
81
82 int Curl_cert_hostcheck(const char *match_pattern, const char *hostname)
83 {
84   if(!match_pattern || !*match_pattern ||
85       !hostname || !*hostname) /* sanity check */
86     return 0;
87
88   if(Curl_raw_equal(hostname, match_pattern)) /* trivial case */
89     return 1;
90
91   if(hostmatch(hostname,match_pattern) == CURL_HOST_MATCH)
92     return 1;
93   return 0;
94 }
95
96 #endif /* SSLEAY or AXTLS */