• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2015, 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_OPENSSL) || defined(USE_AXTLS) || defined(USE_GSKIT)
26 /* these backends use functions from this file */
27 
28 #ifdef HAVE_NETINET_IN_H
29 #include <netinet/in.h>
30 #endif
31 
32 #include "hostcheck.h"
33 #include "rawstr.h"
34 #include "inet_pton.h"
35 
36 #include "curl_memory.h"
37 /* The last #include file should be: */
38 #include "memdebug.h"
39 
40 /*
41  * Match a hostname against a wildcard pattern.
42  * E.g.
43  *  "foo.host.com" matches "*.host.com".
44  *
45  * We use the matching rule described in RFC6125, section 6.4.3.
46  * http://tools.ietf.org/html/rfc6125#section-6.4.3
47  *
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.
50  *
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.
55  *
56  * NOTE: hostmatch() gets called with copied buffers so that it can modify the
57  * contents at will.
58  */
59 
hostmatch(char * hostname,char * pattern)60 static int hostmatch(char *hostname, char *pattern)
61 {
62   const char *pattern_label_end, *pattern_wildcard, *hostname_label_end;
63   int wildcard_enabled;
64   size_t prefixlen, suffixlen;
65   struct in_addr ignored;
66 #ifdef ENABLE_IPV6
67   struct sockaddr_in6 si6;
68 #endif
69 
70   /* normalize pattern and hostname by stripping off trailing dots */
71   size_t len = strlen(hostname);
72   if(hostname[len-1]=='.')
73     hostname[len-1]=0;
74   len = strlen(pattern);
75   if(pattern[len-1]=='.')
76     pattern[len-1]=0;
77 
78   pattern_wildcard = strchr(pattern, '*');
79   if(pattern_wildcard == NULL)
80     return Curl_raw_equal(pattern, hostname) ?
81       CURL_HOST_MATCH : CURL_HOST_NOMATCH;
82 
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;
86 #ifdef ENABLE_IPV6
87   else if(Curl_inet_pton(AF_INET6, hostname, &si6.sin6_addr) > 0)
88     return CURL_HOST_NOMATCH;
89 #endif
90 
91   /* We require at least 2 dots in pattern to avoid too wide wildcard
92      match. */
93   wildcard_enabled = 1;
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)) {
98     wildcard_enabled = 0;
99   }
100   if(!wildcard_enabled)
101     return Curl_raw_equal(pattern, hostname) ?
102       CURL_HOST_MATCH : CURL_HOST_NOMATCH;
103 
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;
108 
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
111      of the pattern. */
112   if(hostname_label_end - hostname < pattern_label_end - pattern)
113     return CURL_HOST_NOMATCH;
114 
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,
119                     suffixlen) ?
120     CURL_HOST_MATCH : CURL_HOST_NOMATCH;
121 }
122 
Curl_cert_hostcheck(const char * match_pattern,const char * hostname)123 int Curl_cert_hostcheck(const char *match_pattern, const char *hostname)
124 {
125   char *matchp;
126   char *hostp;
127   int res = 0;
128   if(!match_pattern || !*match_pattern ||
129       !hostname || !*hostname) /* sanity check */
130     ;
131   else {
132     matchp = strdup(match_pattern);
133     if(matchp) {
134       hostp = strdup(hostname);
135       if(hostp) {
136         if(hostmatch(hostp, matchp) == CURL_HOST_MATCH)
137           res= 1;
138         free(hostp);
139       }
140       free(matchp);
141     }
142   }
143 
144   return res;
145 }
146 
147 #endif /* OPENSSL or AXTLS or GSKIT */
148