hostcheck.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 
00023 #include "curl_setup.h"
00024 
00025 #if defined(USE_OPENSSL)                                \
00026   || defined(USE_AXTLS)                                 \
00027   || defined(USE_GSKIT)                                 \
00028   || (defined(USE_SCHANNEL) && defined(_WIN32_WCE))
00029 /* these backends use functions from this file */
00030 
00031 #ifdef HAVE_NETINET_IN_H
00032 #include <netinet/in.h>
00033 #endif
00034 
00035 #include "hostcheck.h"
00036 #include "strcase.h"
00037 #include "inet_pton.h"
00038 
00039 #include "curl_memory.h"
00040 /* The last #include file should be: */
00041 #include "memdebug.h"
00042 
00043 /*
00044  * Match a hostname against a wildcard pattern.
00045  * E.g.
00046  *  "foo.host.com" matches "*.host.com".
00047  *
00048  * We use the matching rule described in RFC6125, section 6.4.3.
00049  * https://tools.ietf.org/html/rfc6125#section-6.4.3
00050  *
00051  * In addition: ignore trailing dots in the host names and wildcards, so that
00052  * the names are used normalized. This is what the browsers do.
00053  *
00054  * Do not allow wildcard matching on IP numbers. There are apparently
00055  * certificates being used with an IP address in the CN field, thus making no
00056  * apparent distinction between a name and an IP. We need to detect the use of
00057  * an IP address and not wildcard match on such names.
00058  *
00059  * NOTE: hostmatch() gets called with copied buffers so that it can modify the
00060  * contents at will.
00061  */
00062 
00063 static int hostmatch(char *hostname, char *pattern)
00064 {
00065   const char *pattern_label_end, *pattern_wildcard, *hostname_label_end;
00066   int wildcard_enabled;
00067   size_t prefixlen, suffixlen;
00068   struct in_addr ignored;
00069 #ifdef ENABLE_IPV6
00070   struct sockaddr_in6 si6;
00071 #endif
00072 
00073   /* normalize pattern and hostname by stripping off trailing dots */
00074   size_t len = strlen(hostname);
00075   if(hostname[len-1]=='.')
00076     hostname[len-1]=0;
00077   len = strlen(pattern);
00078   if(pattern[len-1]=='.')
00079     pattern[len-1]=0;
00080 
00081   pattern_wildcard = strchr(pattern, '*');
00082   if(pattern_wildcard == NULL)
00083     return strcasecompare(pattern, hostname) ?
00084       CURL_HOST_MATCH : CURL_HOST_NOMATCH;
00085 
00086   /* detect IP address as hostname and fail the match if so */
00087   if(Curl_inet_pton(AF_INET, hostname, &ignored) > 0)
00088     return CURL_HOST_NOMATCH;
00089 #ifdef ENABLE_IPV6
00090   else if(Curl_inet_pton(AF_INET6, hostname, &si6.sin6_addr) > 0)
00091     return CURL_HOST_NOMATCH;
00092 #endif
00093 
00094   /* We require at least 2 dots in pattern to avoid too wide wildcard
00095      match. */
00096   wildcard_enabled = 1;
00097   pattern_label_end = strchr(pattern, '.');
00098   if(pattern_label_end == NULL || strchr(pattern_label_end+1, '.') == NULL ||
00099      pattern_wildcard > pattern_label_end ||
00100      strncasecompare(pattern, "xn--", 4)) {
00101     wildcard_enabled = 0;
00102   }
00103   if(!wildcard_enabled)
00104     return strcasecompare(pattern, hostname) ?
00105       CURL_HOST_MATCH : CURL_HOST_NOMATCH;
00106 
00107   hostname_label_end = strchr(hostname, '.');
00108   if(hostname_label_end == NULL ||
00109      !strcasecompare(pattern_label_end, hostname_label_end))
00110     return CURL_HOST_NOMATCH;
00111 
00112   /* The wildcard must match at least one character, so the left-most
00113      label of the hostname is at least as large as the left-most label
00114      of the pattern. */
00115   if(hostname_label_end - hostname < pattern_label_end - pattern)
00116     return CURL_HOST_NOMATCH;
00117 
00118   prefixlen = pattern_wildcard - pattern;
00119   suffixlen = pattern_label_end - (pattern_wildcard+1);
00120   return strncasecompare(pattern, hostname, prefixlen) &&
00121     strncasecompare(pattern_wildcard+1, hostname_label_end - suffixlen,
00122                     suffixlen) ?
00123     CURL_HOST_MATCH : CURL_HOST_NOMATCH;
00124 }
00125 
00126 int Curl_cert_hostcheck(const char *match_pattern, const char *hostname)
00127 {
00128   char *matchp;
00129   char *hostp;
00130   int res = 0;
00131   if(!match_pattern || !*match_pattern ||
00132       !hostname || !*hostname) /* sanity check */
00133     ;
00134   else {
00135     matchp = strdup(match_pattern);
00136     if(matchp) {
00137       hostp = strdup(hostname);
00138       if(hostp) {
00139         if(hostmatch(hostp, matchp) == CURL_HOST_MATCH)
00140           res= 1;
00141         free(hostp);
00142       }
00143       free(matchp);
00144     }
00145   }
00146 
00147   return res;
00148 }
00149 
00150 #endif /* OPENSSL, AXTLS, GSKIT or schannel+wince */


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:04