Coverage Report

Created: 2025-12-03 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/vtls/hostcheck.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
25
#include "../curl_setup.h"
26
27
#if defined(USE_OPENSSL) || defined(USE_SCHANNEL)
28
/* these backends use functions from this file */
29
30
#ifdef HAVE_NETINET_IN_H
31
#include <netinet/in.h>
32
#endif
33
#ifdef HAVE_NETINET_IN6_H
34
#include <netinet/in6.h>
35
#endif
36
#include "../curl_memrchr.h"
37
#include "hostcheck.h"
38
#include "../hostip.h"
39
40
/* check the two input strings with given length, but do not
41
   assume they end in nul-bytes */
42
static bool pmatch(const char *hostname, size_t hostlen,
43
                   const char *pattern, size_t patternlen)
44
0
{
45
0
  if(hostlen != patternlen)
46
0
    return FALSE;
47
0
  return curl_strnequal(hostname, pattern, hostlen);
48
0
}
49
50
/*
51
 * Match a hostname against a wildcard pattern.
52
 * E.g.
53
 *  "foo.host.com" matches "*.host.com".
54
 *
55
 * We use the matching rule described in RFC6125, section 6.4.3.
56
 * https://datatracker.ietf.org/doc/html/rfc6125#section-6.4.3
57
 *
58
 * In addition: ignore trailing dots in the hostnames and wildcards, so that
59
 * the names are used normalized. This is what the browsers do.
60
 *
61
 * Do not allow wildcard matching on IP numbers. There are apparently
62
 * certificates being used with an IP address in the CN field, thus making no
63
 * apparent distinction between a name and an IP. We need to detect the use of
64
 * an IP address and not wildcard match on such names.
65
 *
66
 * Only match on "*" being used for the leftmost label, not "a*", "a*b" nor
67
 * "*b".
68
 *
69
 * Return TRUE on a match. FALSE if not.
70
 *
71
 * @unittest: 1397
72
 */
73
74
static bool hostmatch(const char *hostname,
75
                      size_t hostlen,
76
                      const char *pattern,
77
                      size_t patternlen)
78
0
{
79
0
  const char *pattern_label_end;
80
81
0
  DEBUGASSERT(pattern);
82
0
  DEBUGASSERT(patternlen);
83
0
  DEBUGASSERT(hostname);
84
0
  DEBUGASSERT(hostlen);
85
86
  /* normalize pattern and hostname by stripping off trailing dots */
87
0
  if(hostname[hostlen - 1] == '.')
88
0
    hostlen--;
89
0
  if(pattern[patternlen - 1] == '.')
90
0
    patternlen--;
91
92
0
  if(strncmp(pattern, "*.", 2))
93
0
    return pmatch(hostname, hostlen, pattern, patternlen);
94
95
  /* detect IP address as hostname and fail the match if so */
96
0
  else if(Curl_host_is_ipnum(hostname))
97
0
    return FALSE;
98
99
  /* We require at least 2 dots in the pattern to avoid too wide wildcard
100
     match. */
101
0
  pattern_label_end = memchr(pattern, '.', patternlen);
102
0
  if(!pattern_label_end ||
103
0
     (memrchr(pattern, '.', patternlen) == pattern_label_end))
104
0
    return pmatch(hostname, hostlen, pattern, patternlen);
105
0
  else {
106
0
    const char *hostname_label_end = memchr(hostname, '.', hostlen);
107
0
    if(hostname_label_end) {
108
0
      size_t skiphost = hostname_label_end - hostname;
109
0
      size_t skiplen = pattern_label_end - pattern;
110
0
      return pmatch(hostname_label_end, hostlen - skiphost,
111
0
                    pattern_label_end, patternlen - skiplen);
112
0
    }
113
0
  }
114
0
  return FALSE;
115
0
}
116
117
/*
118
 * Curl_cert_hostcheck() returns TRUE if a match and FALSE if not.
119
 */
120
bool Curl_cert_hostcheck(const char *match, size_t matchlen,
121
                         const char *hostname, size_t hostlen)
122
0
{
123
0
  if(match && *match && hostname && *hostname)
124
0
    return hostmatch(hostname, hostlen, match, matchlen);
125
0
  return FALSE;
126
0
}
127
128
#endif /* OPENSSL or SCHANNEL */