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