cmake/Utilities/cmcurl/lib/hostcheck.c

151 lines
4.8 KiB
C
Raw Normal View History

2015-04-27 22:25:09 +02:00
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
2020-08-30 11:54:41 +02:00
* Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
2015-04-27 22:25:09 +02:00
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
2016-09-11 17:22:32 +02:00
* are also available at https://curl.haxx.se/docs/copyright.html.
2015-04-27 22:25:09 +02:00
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
***************************************************************************/
#include "curl_setup.h"
2017-07-20 19:35:53 +02:00
#if defined(USE_OPENSSL) \
|| defined(USE_GSKIT) \
2018-08-09 18:06:22 +02:00
|| defined(USE_SCHANNEL)
2015-04-27 22:25:09 +02:00
/* these backends use functions from this file */
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
2018-04-23 21:13:27 +02:00
#ifdef HAVE_NETINET_IN6_H
#include <netinet/in6.h>
#endif
2015-04-27 22:25:09 +02:00
#include "hostcheck.h"
2017-04-14 19:02:05 +02:00
#include "strcase.h"
2015-04-27 22:25:09 +02:00
#include "inet_pton.h"
#include "curl_memory.h"
/* The last #include file should be: */
#include "memdebug.h"
/*
* Match a hostname against a wildcard pattern.
* E.g.
* "foo.host.com" matches "*.host.com".
*
* We use the matching rule described in RFC6125, section 6.4.3.
2016-09-11 17:22:32 +02:00
* https://tools.ietf.org/html/rfc6125#section-6.4.3
2015-04-27 22:25:09 +02:00
*
* In addition: ignore trailing dots in the host names and wildcards, so that
* the names are used normalized. This is what the browsers do.
*
* Do not allow wildcard matching on IP numbers. There are apparently
* certificates being used with an IP address in the CN field, thus making no
* apparent distinction between a name and an IP. We need to detect the use of
* an IP address and not wildcard match on such names.
*
* NOTE: hostmatch() gets called with copied buffers so that it can modify the
* contents at will.
*/
static int hostmatch(char *hostname, char *pattern)
{
const char *pattern_label_end, *pattern_wildcard, *hostname_label_end;
int wildcard_enabled;
size_t prefixlen, suffixlen;
struct in_addr ignored;
#ifdef ENABLE_IPV6
struct sockaddr_in6 si6;
#endif
/* normalize pattern and hostname by stripping off trailing dots */
size_t len = strlen(hostname);
if(hostname[len-1]=='.')
2018-01-26 17:06:56 +01:00
hostname[len-1] = 0;
2015-04-27 22:25:09 +02:00
len = strlen(pattern);
if(pattern[len-1]=='.')
2018-01-26 17:06:56 +01:00
pattern[len-1] = 0;
2015-04-27 22:25:09 +02:00
pattern_wildcard = strchr(pattern, '*');
if(pattern_wildcard == NULL)
2017-04-14 19:02:05 +02:00
return strcasecompare(pattern, hostname) ?
2015-04-27 22:25:09 +02:00
CURL_HOST_MATCH : CURL_HOST_NOMATCH;
/* detect IP address as hostname and fail the match if so */
if(Curl_inet_pton(AF_INET, hostname, &ignored) > 0)
return CURL_HOST_NOMATCH;
#ifdef ENABLE_IPV6
2017-07-20 19:35:53 +02:00
if(Curl_inet_pton(AF_INET6, hostname, &si6.sin6_addr) > 0)
2015-04-27 22:25:09 +02:00
return CURL_HOST_NOMATCH;
#endif
/* We require at least 2 dots in pattern to avoid too wide wildcard
match. */
wildcard_enabled = 1;
pattern_label_end = strchr(pattern, '.');
2018-01-26 17:06:56 +01:00
if(pattern_label_end == NULL || strchr(pattern_label_end + 1, '.') == NULL ||
2015-04-27 22:25:09 +02:00
pattern_wildcard > pattern_label_end ||
2017-04-14 19:02:05 +02:00
strncasecompare(pattern, "xn--", 4)) {
2015-04-27 22:25:09 +02:00
wildcard_enabled = 0;
}
if(!wildcard_enabled)
2017-04-14 19:02:05 +02:00
return strcasecompare(pattern, hostname) ?
2015-04-27 22:25:09 +02:00
CURL_HOST_MATCH : CURL_HOST_NOMATCH;
hostname_label_end = strchr(hostname, '.');
if(hostname_label_end == NULL ||
2017-04-14 19:02:05 +02:00
!strcasecompare(pattern_label_end, hostname_label_end))
2015-04-27 22:25:09 +02:00
return CURL_HOST_NOMATCH;
/* The wildcard must match at least one character, so the left-most
label of the hostname is at least as large as the left-most label
of the pattern. */
if(hostname_label_end - hostname < pattern_label_end - pattern)
return CURL_HOST_NOMATCH;
prefixlen = pattern_wildcard - pattern;
2018-01-26 17:06:56 +01:00
suffixlen = pattern_label_end - (pattern_wildcard + 1);
2017-04-14 19:02:05 +02:00
return strncasecompare(pattern, hostname, prefixlen) &&
2018-01-26 17:06:56 +01:00
strncasecompare(pattern_wildcard + 1, hostname_label_end - suffixlen,
2015-04-27 22:25:09 +02:00
suffixlen) ?
CURL_HOST_MATCH : CURL_HOST_NOMATCH;
}
int Curl_cert_hostcheck(const char *match_pattern, const char *hostname)
{
int res = 0;
if(!match_pattern || !*match_pattern ||
!hostname || !*hostname) /* sanity check */
;
else {
2019-11-11 23:01:05 +01:00
char *matchp = strdup(match_pattern);
2015-04-27 22:25:09 +02:00
if(matchp) {
2019-11-11 23:01:05 +01:00
char *hostp = strdup(hostname);
2015-04-27 22:25:09 +02:00
if(hostp) {
if(hostmatch(hostp, matchp) == CURL_HOST_MATCH)
2018-01-26 17:06:56 +01:00
res = 1;
2015-04-27 22:25:09 +02:00
free(hostp);
}
free(matchp);
}
}
return res;
}
2019-11-11 23:01:05 +01:00
#endif /* OPENSSL, GSKIT or schannel+wince */