Coverage Report

Created: 2025-08-26 07:08

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