xref: /freebsd/contrib/libevent/sample/hostcheck.c (revision c43e99fd)
1*c43e99fdSEd Maste /***************************************************************************
2*c43e99fdSEd Maste  *                                  _   _ ____  _
3*c43e99fdSEd Maste  *  Project                     ___| | | |  _ \| |
4*c43e99fdSEd Maste  *                             / __| | | | |_) | |
5*c43e99fdSEd Maste  *                            | (__| |_| |  _ <| |___
6*c43e99fdSEd Maste  *                             \___|\___/|_| \_\_____|
7*c43e99fdSEd Maste  *
8*c43e99fdSEd Maste  * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
9*c43e99fdSEd Maste  *
10*c43e99fdSEd Maste  * This software is licensed as described in the file COPYING, which
11*c43e99fdSEd Maste  * you should have received as part of this distribution. The terms
12*c43e99fdSEd Maste  * are also available at http://curl.haxx.se/docs/copyright.html.
13*c43e99fdSEd Maste  *
14*c43e99fdSEd Maste  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15*c43e99fdSEd Maste  * copies of the Software, and permit persons to whom the Software is
16*c43e99fdSEd Maste  * furnished to do so, under the terms of the COPYING file.
17*c43e99fdSEd Maste  *
18*c43e99fdSEd Maste  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19*c43e99fdSEd Maste  * KIND, either express or implied.
20*c43e99fdSEd Maste  *
21*c43e99fdSEd Maste  ***************************************************************************/
22*c43e99fdSEd Maste 
23*c43e99fdSEd Maste /* This file is an amalgamation of hostcheck.c and most of rawstr.c
24*c43e99fdSEd Maste    from cURL.  The contents of the COPYING file mentioned above are:
25*c43e99fdSEd Maste 
26*c43e99fdSEd Maste COPYRIGHT AND PERMISSION NOTICE
27*c43e99fdSEd Maste 
28*c43e99fdSEd Maste Copyright (c) 1996 - 2013, Daniel Stenberg, <daniel@haxx.se>.
29*c43e99fdSEd Maste 
30*c43e99fdSEd Maste All rights reserved.
31*c43e99fdSEd Maste 
32*c43e99fdSEd Maste Permission to use, copy, modify, and distribute this software for any purpose
33*c43e99fdSEd Maste with or without fee is hereby granted, provided that the above copyright
34*c43e99fdSEd Maste notice and this permission notice appear in all copies.
35*c43e99fdSEd Maste 
36*c43e99fdSEd Maste THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37*c43e99fdSEd Maste IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38*c43e99fdSEd Maste FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN
39*c43e99fdSEd Maste NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
40*c43e99fdSEd Maste DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
41*c43e99fdSEd Maste OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
42*c43e99fdSEd Maste OR OTHER DEALINGS IN THE SOFTWARE.
43*c43e99fdSEd Maste 
44*c43e99fdSEd Maste Except as contained in this notice, the name of a copyright holder shall not
45*c43e99fdSEd Maste be used in advertising or otherwise to promote the sale, use or other dealings
46*c43e99fdSEd Maste in this Software without prior written authorization of the copyright holder.
47*c43e99fdSEd Maste */
48*c43e99fdSEd Maste 
49*c43e99fdSEd Maste #include "hostcheck.h"
50*c43e99fdSEd Maste #include <string.h>
51*c43e99fdSEd Maste 
52*c43e99fdSEd Maste /* Portable, consistent toupper (remember EBCDIC). Do not use toupper() because
53*c43e99fdSEd Maste    its behavior is altered by the current locale. */
Curl_raw_toupper(char in)54*c43e99fdSEd Maste static char Curl_raw_toupper(char in)
55*c43e99fdSEd Maste {
56*c43e99fdSEd Maste   switch (in) {
57*c43e99fdSEd Maste   case 'a':
58*c43e99fdSEd Maste     return 'A';
59*c43e99fdSEd Maste   case 'b':
60*c43e99fdSEd Maste     return 'B';
61*c43e99fdSEd Maste   case 'c':
62*c43e99fdSEd Maste     return 'C';
63*c43e99fdSEd Maste   case 'd':
64*c43e99fdSEd Maste     return 'D';
65*c43e99fdSEd Maste   case 'e':
66*c43e99fdSEd Maste     return 'E';
67*c43e99fdSEd Maste   case 'f':
68*c43e99fdSEd Maste     return 'F';
69*c43e99fdSEd Maste   case 'g':
70*c43e99fdSEd Maste     return 'G';
71*c43e99fdSEd Maste   case 'h':
72*c43e99fdSEd Maste     return 'H';
73*c43e99fdSEd Maste   case 'i':
74*c43e99fdSEd Maste     return 'I';
75*c43e99fdSEd Maste   case 'j':
76*c43e99fdSEd Maste     return 'J';
77*c43e99fdSEd Maste   case 'k':
78*c43e99fdSEd Maste     return 'K';
79*c43e99fdSEd Maste   case 'l':
80*c43e99fdSEd Maste     return 'L';
81*c43e99fdSEd Maste   case 'm':
82*c43e99fdSEd Maste     return 'M';
83*c43e99fdSEd Maste   case 'n':
84*c43e99fdSEd Maste     return 'N';
85*c43e99fdSEd Maste   case 'o':
86*c43e99fdSEd Maste     return 'O';
87*c43e99fdSEd Maste   case 'p':
88*c43e99fdSEd Maste     return 'P';
89*c43e99fdSEd Maste   case 'q':
90*c43e99fdSEd Maste     return 'Q';
91*c43e99fdSEd Maste   case 'r':
92*c43e99fdSEd Maste     return 'R';
93*c43e99fdSEd Maste   case 's':
94*c43e99fdSEd Maste     return 'S';
95*c43e99fdSEd Maste   case 't':
96*c43e99fdSEd Maste     return 'T';
97*c43e99fdSEd Maste   case 'u':
98*c43e99fdSEd Maste     return 'U';
99*c43e99fdSEd Maste   case 'v':
100*c43e99fdSEd Maste     return 'V';
101*c43e99fdSEd Maste   case 'w':
102*c43e99fdSEd Maste     return 'W';
103*c43e99fdSEd Maste   case 'x':
104*c43e99fdSEd Maste     return 'X';
105*c43e99fdSEd Maste   case 'y':
106*c43e99fdSEd Maste     return 'Y';
107*c43e99fdSEd Maste   case 'z':
108*c43e99fdSEd Maste     return 'Z';
109*c43e99fdSEd Maste   }
110*c43e99fdSEd Maste   return in;
111*c43e99fdSEd Maste }
112*c43e99fdSEd Maste 
113*c43e99fdSEd Maste /*
114*c43e99fdSEd Maste  * Curl_raw_equal() is for doing "raw" case insensitive strings. This is meant
115*c43e99fdSEd Maste  * to be locale independent and only compare strings we know are safe for
116*c43e99fdSEd Maste  * this.  See http://daniel.haxx.se/blog/2008/10/15/strcasecmp-in-turkish/ for
117*c43e99fdSEd Maste  * some further explanation to why this function is necessary.
118*c43e99fdSEd Maste  *
119*c43e99fdSEd Maste  * The function is capable of comparing a-z case insensitively even for
120*c43e99fdSEd Maste  * non-ascii.
121*c43e99fdSEd Maste  */
122*c43e99fdSEd Maste 
Curl_raw_equal(const char * first,const char * second)123*c43e99fdSEd Maste static int Curl_raw_equal(const char *first, const char *second)
124*c43e99fdSEd Maste {
125*c43e99fdSEd Maste   while(*first && *second) {
126*c43e99fdSEd Maste     if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second))
127*c43e99fdSEd Maste       /* get out of the loop as soon as they don't match */
128*c43e99fdSEd Maste       break;
129*c43e99fdSEd Maste     first++;
130*c43e99fdSEd Maste     second++;
131*c43e99fdSEd Maste   }
132*c43e99fdSEd Maste   /* we do the comparison here (possibly again), just to make sure that if the
133*c43e99fdSEd Maste      loop above is skipped because one of the strings reached zero, we must not
134*c43e99fdSEd Maste      return this as a successful match */
135*c43e99fdSEd Maste   return (Curl_raw_toupper(*first) == Curl_raw_toupper(*second));
136*c43e99fdSEd Maste }
137*c43e99fdSEd Maste 
Curl_raw_nequal(const char * first,const char * second,size_t max)138*c43e99fdSEd Maste static int Curl_raw_nequal(const char *first, const char *second, size_t max)
139*c43e99fdSEd Maste {
140*c43e99fdSEd Maste   while(*first && *second && max) {
141*c43e99fdSEd Maste     if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second)) {
142*c43e99fdSEd Maste       break;
143*c43e99fdSEd Maste     }
144*c43e99fdSEd Maste     max--;
145*c43e99fdSEd Maste     first++;
146*c43e99fdSEd Maste     second++;
147*c43e99fdSEd Maste   }
148*c43e99fdSEd Maste   if(0 == max)
149*c43e99fdSEd Maste     return 1; /* they are equal this far */
150*c43e99fdSEd Maste 
151*c43e99fdSEd Maste   return Curl_raw_toupper(*first) == Curl_raw_toupper(*second);
152*c43e99fdSEd Maste }
153*c43e99fdSEd Maste 
154*c43e99fdSEd Maste /*
155*c43e99fdSEd Maste  * Match a hostname against a wildcard pattern.
156*c43e99fdSEd Maste  * E.g.
157*c43e99fdSEd Maste  *  "foo.host.com" matches "*.host.com".
158*c43e99fdSEd Maste  *
159*c43e99fdSEd Maste  * We use the matching rule described in RFC6125, section 6.4.3.
160*c43e99fdSEd Maste  * http://tools.ietf.org/html/rfc6125#section-6.4.3
161*c43e99fdSEd Maste  */
162*c43e99fdSEd Maste 
hostmatch(const char * hostname,const char * pattern)163*c43e99fdSEd Maste static int hostmatch(const char *hostname, const char *pattern)
164*c43e99fdSEd Maste {
165*c43e99fdSEd Maste   const char *pattern_label_end, *pattern_wildcard, *hostname_label_end;
166*c43e99fdSEd Maste   int wildcard_enabled;
167*c43e99fdSEd Maste   size_t prefixlen, suffixlen;
168*c43e99fdSEd Maste   pattern_wildcard = strchr(pattern, '*');
169*c43e99fdSEd Maste   if(pattern_wildcard == NULL)
170*c43e99fdSEd Maste     return Curl_raw_equal(pattern, hostname) ?
171*c43e99fdSEd Maste       CURL_HOST_MATCH : CURL_HOST_NOMATCH;
172*c43e99fdSEd Maste 
173*c43e99fdSEd Maste   /* We require at least 2 dots in pattern to avoid too wide wildcard
174*c43e99fdSEd Maste      match. */
175*c43e99fdSEd Maste   wildcard_enabled = 1;
176*c43e99fdSEd Maste   pattern_label_end = strchr(pattern, '.');
177*c43e99fdSEd Maste   if(pattern_label_end == NULL || strchr(pattern_label_end+1, '.') == NULL ||
178*c43e99fdSEd Maste      pattern_wildcard > pattern_label_end ||
179*c43e99fdSEd Maste      Curl_raw_nequal(pattern, "xn--", 4)) {
180*c43e99fdSEd Maste     wildcard_enabled = 0;
181*c43e99fdSEd Maste   }
182*c43e99fdSEd Maste   if(!wildcard_enabled)
183*c43e99fdSEd Maste     return Curl_raw_equal(pattern, hostname) ?
184*c43e99fdSEd Maste       CURL_HOST_MATCH : CURL_HOST_NOMATCH;
185*c43e99fdSEd Maste 
186*c43e99fdSEd Maste   hostname_label_end = strchr(hostname, '.');
187*c43e99fdSEd Maste   if(hostname_label_end == NULL ||
188*c43e99fdSEd Maste      !Curl_raw_equal(pattern_label_end, hostname_label_end))
189*c43e99fdSEd Maste     return CURL_HOST_NOMATCH;
190*c43e99fdSEd Maste 
191*c43e99fdSEd Maste   /* The wildcard must match at least one character, so the left-most
192*c43e99fdSEd Maste      label of the hostname is at least as large as the left-most label
193*c43e99fdSEd Maste      of the pattern. */
194*c43e99fdSEd Maste   if(hostname_label_end - hostname < pattern_label_end - pattern)
195*c43e99fdSEd Maste     return CURL_HOST_NOMATCH;
196*c43e99fdSEd Maste 
197*c43e99fdSEd Maste   prefixlen = pattern_wildcard - pattern;
198*c43e99fdSEd Maste   suffixlen = pattern_label_end - (pattern_wildcard+1);
199*c43e99fdSEd Maste   return Curl_raw_nequal(pattern, hostname, prefixlen) &&
200*c43e99fdSEd Maste     Curl_raw_nequal(pattern_wildcard+1, hostname_label_end - suffixlen,
201*c43e99fdSEd Maste                     suffixlen) ?
202*c43e99fdSEd Maste     CURL_HOST_MATCH : CURL_HOST_NOMATCH;
203*c43e99fdSEd Maste }
204*c43e99fdSEd Maste 
Curl_cert_hostcheck(const char * match_pattern,const char * hostname)205*c43e99fdSEd Maste int Curl_cert_hostcheck(const char *match_pattern, const char *hostname)
206*c43e99fdSEd Maste {
207*c43e99fdSEd Maste   if(!match_pattern || !*match_pattern ||
208*c43e99fdSEd Maste       !hostname || !*hostname) /* sanity check */
209*c43e99fdSEd Maste     return 0;
210*c43e99fdSEd Maste 
211*c43e99fdSEd Maste   if(Curl_raw_equal(hostname, match_pattern)) /* trivial case */
212*c43e99fdSEd Maste     return 1;
213*c43e99fdSEd Maste 
214*c43e99fdSEd Maste   if(hostmatch(hostname,match_pattern) == CURL_HOST_MATCH)
215*c43e99fdSEd Maste     return 1;
216*c43e99fdSEd Maste   return 0;
217*c43e99fdSEd Maste }
218