Coverage Report

Created: 2026-01-09 07:25

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
#include "../curl_setup.h"
25
26
#if defined(USE_OPENSSL) || defined(USE_SCHANNEL)
27
/* these backends use functions from this file */
28
29
#ifdef HAVE_NETINET_IN_H
30
#include <netinet/in.h>
31
#endif
32
#ifdef HAVE_NETINET_IN6_H
33
#include <netinet/in6.h>
34
#endif
35
#include "../curl_memrchr.h"
36
#include "hostcheck.h"
37
#include "../hostip.h"
38
39
/* check the two input strings with given length, but do not
40
   assume they end in nul-bytes */
41
static bool pmatch(const char *hostname, size_t hostlen,
42
                   const char *pattern, size_t patternlen)
43
0
{
44
0
  if(hostlen != patternlen)
45
0
    return FALSE;
46
0
  return curl_strnequal(hostname, pattern, hostlen);
47
0
}
48
49
/*
50
 * Match a hostname against a wildcard pattern.
51
 * E.g.
52
 *  "foo.host.com" matches "*.host.com".
53
 *
54
 * We use the matching rule described in RFC6125, section 6.4.3.
55
 * https://datatracker.ietf.org/doc/html/rfc6125#section-6.4.3
56
 *
57
 * In addition: ignore trailing dots in the hostnames and wildcards, so that
58
 * the names are used normalized. This is what the browsers do.
59
 *
60
 * Do not allow wildcard matching on IP numbers. There are apparently
61
 * certificates being used with an IP address in the CN field, thus making no
62
 * apparent distinction between a name and an IP. We need to detect the use of
63
 * an IP address and not wildcard match on such names.
64
 *
65
 * Only match on "*" being used for the leftmost label, not "a*", "a*b" nor
66
 * "*b".
67
 *
68
 * Return TRUE on a match. FALSE if not.
69
 *
70
 * @unittest: 1397
71
 */
72
73
static bool hostmatch(const char *hostname,
74
                      size_t hostlen,
75
                      const char *pattern,
76
                      size_t patternlen)
77
0
{
78
0
  const char *pattern_label_end;
79
80
0
  DEBUGASSERT(pattern);
81
0
  DEBUGASSERT(patternlen);
82
0
  DEBUGASSERT(hostname);
83
0
  DEBUGASSERT(hostlen);
84
85
  /* normalize pattern and hostname by stripping off trailing dots */
86
0
  if(hostname[hostlen - 1] == '.')
87
0
    hostlen--;
88
0
  if(pattern[patternlen - 1] == '.')
89
0
    patternlen--;
90
91
0
  if(strncmp(pattern, "*.", 2))
92
0
    return pmatch(hostname, hostlen, pattern, patternlen);
93
94
  /* detect host as IP address or starting with a dot and fail if so */
95
0
  else if(Curl_host_is_ipnum(hostname) || (hostname[0] == '.'))
96
0
    return FALSE;
97
98
  /* We require at least 2 dots in the pattern to avoid too wide wildcard
99
     match. */
100
0
  pattern_label_end = memchr(pattern, '.', patternlen);
101
0
  if(!pattern_label_end ||
102
0
     (memrchr(pattern, '.', patternlen) == pattern_label_end))
103
0
    return pmatch(hostname, hostlen, pattern, patternlen);
104
0
  else {
105
0
    const char *hostname_label_end = memchr(hostname, '.', hostlen);
106
0
    if(hostname_label_end) {
107
0
      size_t skiphost = hostname_label_end - hostname;
108
0
      size_t skiplen = pattern_label_end - pattern;
109
0
      return pmatch(hostname_label_end, hostlen - skiphost,
110
0
                    pattern_label_end, patternlen - skiplen);
111
0
    }
112
0
  }
113
0
  return FALSE;
114
0
}
115
116
/*
117
 * Curl_cert_hostcheck() returns TRUE if a match and FALSE if not.
118
 */
119
bool Curl_cert_hostcheck(const char *match, size_t matchlen,
120
                         const char *hostname, size_t hostlen)
121
0
{
122
0
  if(match && *match && hostname && *hostname)
123
0
    return hostmatch(hostname, hostlen, match, matchlen);
124
0
  return FALSE;
125
0
}
126
127
#endif /* USE_OPENSSL || USE_SCHANNEL */