2012-11-03 05:06:51 +04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2018-10-30 12:00:00 +03:00
|
|
|
* Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2012-11-03 05:06:51 +04:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-03 02:19:02 +03:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2012-11-03 05:06:51 +04: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.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
2013-01-06 22:06:49 +04:00
|
|
|
#include "curl_setup.h"
|
2012-11-03 05:06:51 +04:00
|
|
|
|
2016-09-10 00:56:02 +03:00
|
|
|
#if defined(USE_OPENSSL) \
|
|
|
|
|| defined(USE_GSKIT) \
|
schannel: add support for CURLOPT_CAINFO
- Move verify_certificate functionality in schannel.c into a new
file called schannel_verify.c. Additionally, some structure defintions
from schannel.c have been moved to schannel.h to allow them to be
used in schannel_verify.c.
- Make verify_certificate functionality for Schannel available on
all versions of Windows instead of just Windows CE. verify_certificate
will be invoked on Windows CE or when the user specifies
CURLOPT_CAINFO and CURLOPT_SSL_VERIFYPEER.
- In verify_certificate, create a custom certificate chain engine that
exclusively trusts the certificate store backed by the CURLOPT_CAINFO
file.
- doc updates of --cacert/CAINFO support for schannel
- Use CERT_NAME_SEARCH_ALL_NAMES_FLAG when invoking CertGetNameString
when available. This implements a TODO in schannel.c to improve
handling of multiple SANs in a certificate. In particular, all SANs
will now be searched instead of just the first name.
- Update tool_operate.c to not search for the curl-ca-bundle.crt file
when using Schannel to maintain backward compatibility. Previously,
any curl-ca-bundle.crt file found in that search would have been
ignored by Schannel. But, with CAINFO support, the file found by
that search would have been used as the certificate store and
could cause issues for any users that have curl-ca-bundle.crt in
the search path.
- Update url.c to not set the build time CURL_CA_BUNDLE if the selected
SSL backend is Schannel. We allow setting CA location for schannel
only when explicitly specified by the user via CURLOPT_CAINFO /
--cacert.
- Add new test cases 3000 and 3001. These test cases check that the first
and last SAN, respectively, matches the connection hostname. New test
certificates have been added for these cases. For 3000, the certificate
prefix is Server-localhost-firstSAN and for 3001, the certificate
prefix is Server-localhost-secondSAN.
- Remove TODO 15.2 (Add support for custom server certificate
validation), this commit addresses it.
Closes https://github.com/curl/curl/pull/1325
2017-03-10 23:27:30 +03:00
|
|
|
|| defined(USE_SCHANNEL)
|
2013-07-15 20:16:13 +04:00
|
|
|
/* these backends use functions from this file */
|
2012-11-09 01:37:53 +04:00
|
|
|
|
2014-03-27 01:27:34 +04:00
|
|
|
#ifdef HAVE_NETINET_IN_H
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#endif
|
2017-12-05 19:41:27 +03:00
|
|
|
#ifdef HAVE_NETINET_IN6_H
|
|
|
|
#include <netinet/in6.h>
|
|
|
|
#endif
|
2014-03-27 01:27:34 +04:00
|
|
|
|
2013-01-04 05:50:28 +04:00
|
|
|
#include "hostcheck.h"
|
2016-09-30 19:54:02 +03:00
|
|
|
#include "strcase.h"
|
2014-03-03 14:46:36 +04:00
|
|
|
#include "inet_pton.h"
|
2012-11-03 05:06:51 +04:00
|
|
|
|
2014-03-03 14:55:23 +04:00
|
|
|
#include "curl_memory.h"
|
|
|
|
/* The last #include file should be: */
|
|
|
|
#include "memdebug.h"
|
|
|
|
|
2012-11-03 05:06:51 +04:00
|
|
|
/*
|
|
|
|
* 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-02-03 07:09:25 +03:00
|
|
|
* https://tools.ietf.org/html/rfc6125#section-6.4.3
|
2014-03-03 14:55:23 +04: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.
|
2012-11-03 05:06:51 +04:00
|
|
|
*/
|
|
|
|
|
2014-03-03 14:55:23 +04:00
|
|
|
static int hostmatch(char *hostname, char *pattern)
|
2012-11-03 05:06:51 +04:00
|
|
|
{
|
|
|
|
const char *pattern_label_end, *pattern_wildcard, *hostname_label_end;
|
|
|
|
int wildcard_enabled;
|
|
|
|
size_t prefixlen, suffixlen;
|
2014-03-03 14:46:36 +04:00
|
|
|
struct in_addr ignored;
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
struct sockaddr_in6 si6;
|
|
|
|
#endif
|
2014-03-03 14:55:23 +04:00
|
|
|
|
|
|
|
/* normalize pattern and hostname by stripping off trailing dots */
|
|
|
|
size_t len = strlen(hostname);
|
|
|
|
if(hostname[len-1]=='.')
|
2017-09-10 00:09:06 +03:00
|
|
|
hostname[len-1] = 0;
|
2014-03-03 14:55:23 +04:00
|
|
|
len = strlen(pattern);
|
|
|
|
if(pattern[len-1]=='.')
|
2017-09-10 00:09:06 +03:00
|
|
|
pattern[len-1] = 0;
|
2014-03-03 14:55:23 +04:00
|
|
|
|
2012-11-03 05:06:51 +04:00
|
|
|
pattern_wildcard = strchr(pattern, '*');
|
|
|
|
if(pattern_wildcard == NULL)
|
2016-09-30 19:54:02 +03:00
|
|
|
return strcasecompare(pattern, hostname) ?
|
2012-11-03 05:06:51 +04:00
|
|
|
CURL_HOST_MATCH : CURL_HOST_NOMATCH;
|
|
|
|
|
2014-03-03 14:46:36 +04:00
|
|
|
/* 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-03-10 16:28:37 +03:00
|
|
|
if(Curl_inet_pton(AF_INET6, hostname, &si6.sin6_addr) > 0)
|
2014-03-03 14:46:36 +04:00
|
|
|
return CURL_HOST_NOMATCH;
|
|
|
|
#endif
|
|
|
|
|
2012-11-03 05:06:51 +04:00
|
|
|
/* We require at least 2 dots in pattern to avoid too wide wildcard
|
|
|
|
match. */
|
|
|
|
wildcard_enabled = 1;
|
|
|
|
pattern_label_end = strchr(pattern, '.');
|
2017-09-10 00:55:08 +03:00
|
|
|
if(pattern_label_end == NULL || strchr(pattern_label_end + 1, '.') == NULL ||
|
2012-11-03 05:06:51 +04:00
|
|
|
pattern_wildcard > pattern_label_end ||
|
2016-09-30 19:54:02 +03:00
|
|
|
strncasecompare(pattern, "xn--", 4)) {
|
2012-11-03 05:06:51 +04:00
|
|
|
wildcard_enabled = 0;
|
|
|
|
}
|
|
|
|
if(!wildcard_enabled)
|
2016-09-30 19:54:02 +03:00
|
|
|
return strcasecompare(pattern, hostname) ?
|
2012-11-03 05:06:51 +04:00
|
|
|
CURL_HOST_MATCH : CURL_HOST_NOMATCH;
|
|
|
|
|
|
|
|
hostname_label_end = strchr(hostname, '.');
|
|
|
|
if(hostname_label_end == NULL ||
|
2016-09-30 19:54:02 +03:00
|
|
|
!strcasecompare(pattern_label_end, hostname_label_end))
|
2012-11-03 05:06:51 +04: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;
|
2017-09-10 00:55:08 +03:00
|
|
|
suffixlen = pattern_label_end - (pattern_wildcard + 1);
|
2016-09-30 19:54:02 +03:00
|
|
|
return strncasecompare(pattern, hostname, prefixlen) &&
|
2017-09-10 00:55:08 +03:00
|
|
|
strncasecompare(pattern_wildcard + 1, hostname_label_end - suffixlen,
|
2012-11-03 05:06:51 +04:00
|
|
|
suffixlen) ?
|
|
|
|
CURL_HOST_MATCH : CURL_HOST_NOMATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Curl_cert_hostcheck(const char *match_pattern, const char *hostname)
|
|
|
|
{
|
2014-03-03 14:55:23 +04:00
|
|
|
char *matchp;
|
|
|
|
char *hostp;
|
|
|
|
int res = 0;
|
2012-11-03 05:06:51 +04:00
|
|
|
if(!match_pattern || !*match_pattern ||
|
|
|
|
!hostname || !*hostname) /* sanity check */
|
2014-03-03 14:55:23 +04:00
|
|
|
;
|
|
|
|
else {
|
|
|
|
matchp = strdup(match_pattern);
|
|
|
|
if(matchp) {
|
|
|
|
hostp = strdup(hostname);
|
|
|
|
if(hostp) {
|
|
|
|
if(hostmatch(hostp, matchp) == CURL_HOST_MATCH)
|
2017-09-10 00:09:06 +03:00
|
|
|
res = 1;
|
2014-03-03 14:55:23 +04:00
|
|
|
free(hostp);
|
|
|
|
}
|
|
|
|
free(matchp);
|
|
|
|
}
|
|
|
|
}
|
2012-11-03 05:06:51 +04:00
|
|
|
|
2014-03-03 14:55:23 +04:00
|
|
|
return res;
|
2012-11-03 05:06:51 +04:00
|
|
|
}
|
2012-11-09 01:37:53 +04:00
|
|
|
|
2018-10-30 12:00:00 +03:00
|
|
|
#endif /* OPENSSL, GSKIT or schannel+wince */
|