Coverage Report

Created: 2026-08-14 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnutls/lib/x509/hostname-verify.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2003-2016 Free Software Foundation, Inc.
3
 * Copyright (C) 2015-2016 Red Hat, Inc.
4
 * Copyright (C) 2002 Andrew McDonald
5
 *
6
 * This file is part of GnuTLS.
7
 *
8
 * The GnuTLS is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public License
10
 * as published by the Free Software Foundation; either version 2.1 of
11
 * the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful, but
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
20
 *
21
 */
22
23
#include "gnutls_int.h"
24
#include "str.h"
25
#include "x509_int.h"
26
#include "common.h"
27
#include "errors.h"
28
#include "system.h"
29
#include <netinet/in.h>
30
#include <arpa/inet.h>
31
32
/**
33
 * gnutls_x509_crt_check_hostname:
34
 * @cert: should contain an gnutls_x509_crt_t type
35
 * @hostname: A null terminated string that contains a DNS name
36
 *
37
 * This function will check if the given certificate's subject matches
38
 * the given hostname.  This is a basic implementation of the matching
39
 * described in RFC6125, and takes into account wildcards,
40
 * and the DNSName/IPAddress subject alternative name PKIX extension.
41
 *
42
 * For details see also gnutls_x509_crt_check_hostname2().
43
 *
44
 * Returns: non-zero for a successful match, and zero on failure.
45
 **/
46
unsigned gnutls_x509_crt_check_hostname(gnutls_x509_crt_t cert,
47
          const char *hostname)
48
0
{
49
0
  return gnutls_x509_crt_check_hostname2(cert, hostname, 0);
50
0
}
51
52
static int check_ip(gnutls_x509_crt_t cert, const void *ip, unsigned ip_size)
53
0
{
54
0
  char temp[16];
55
0
  size_t temp_size;
56
0
  unsigned i;
57
0
  int ret = 0;
58
59
  /* try matching against:
60
   *  1) a IPaddress alternative name (subjectAltName) extension
61
   *     in the certificate
62
   */
63
64
  /* Check through all included subjectAltName extensions, comparing
65
   * against all those of type IPAddress.
66
   */
67
0
  for (i = 0; !(ret < 0); i++) {
68
0
    temp_size = sizeof(temp);
69
0
    ret = gnutls_x509_crt_get_subject_alt_name(cert, i, temp,
70
0
                 &temp_size, NULL);
71
72
0
    if (ret == GNUTLS_SAN_IPADDRESS) {
73
0
      if (temp_size == ip_size && memeq(temp, ip, ip_size))
74
0
        return 1;
75
0
    } else if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) {
76
0
      ret = 0;
77
0
    }
78
0
  }
79
80
  /* not found a matching IP
81
   */
82
0
  return 0;
83
0
}
84
85
/**
86
 * gnutls_x509_crt_check_ip:
87
 * @cert: should contain an gnutls_x509_crt_t type
88
 * @ip: A pointer to the raw IP address
89
 * @ip_size: the number of bytes in ip (4 or 16)
90
 * @flags: should be zero
91
 *
92
 * This function will check if the IP allowed IP addresses in 
93
 * the certificate's subject alternative name match the provided
94
 * IP address.
95
 *
96
 * Returns: non-zero for a successful match, and zero on failure.
97
 **/
98
unsigned gnutls_x509_crt_check_ip(gnutls_x509_crt_t cert,
99
          const unsigned char *ip, unsigned int ip_size,
100
          unsigned int flags)
101
0
{
102
0
  return check_ip(cert, ip, ip_size);
103
0
}
104
105
/* whether gnutls_x509_crt_check_hostname2() will consider these
106
 * alternative name types. This is to satisfy RFC6125 requirement
107
 * that we do not fallback to CN-ID if we encounter a supported name
108
 * type.
109
 */
110
#define PRECLUDES_CN_FALLBACK(san)                                   \
111
0
  (san == GNUTLS_SAN_DNSNAME || san == GNUTLS_SAN_IPADDRESS || \
112
0
   san == GNUTLS_SAN_URI || san == GNUTLS_SAN_OTHERNAME_SRV)
113
114
/**
115
 * gnutls_x509_crt_check_hostname2:
116
 * @cert: should contain an gnutls_x509_crt_t type
117
 * @hostname: A null terminated string that contains a DNS name
118
 * @flags: gnutls_certificate_verify_flags
119
 *
120
 * This function will check if the given certificate's subject matches
121
 * the given hostname.  This is a basic implementation of the matching
122
 * described in RFC6125, and takes into account wildcards,
123
 * and the DNSName/IPAddress subject alternative name PKIX extension.
124
 *
125
 * IPv4 addresses are accepted by this function in the dotted-decimal
126
 * format (e.g, ddd.ddd.ddd.ddd), and IPv6 addresses in the hexadecimal
127
 * x:x:x:x:x:x:x:x format. For them the IPAddress subject alternative
128
 * name extension is consulted. Previous versions to 3.6.0 of GnuTLS
129
 * in case of a non-match would consult (in a non-standard extension)
130
 * the DNSname and CN fields. This is no longer the case.
131
 *
132
 * When the flag %GNUTLS_VERIFY_DO_NOT_ALLOW_WILDCARDS is specified no
133
 * wildcards are considered. Otherwise they are only considered if the
134
 * domain name consists of three components or more, and the wildcard
135
 * starts at the leftmost position.
136
137
 * When the flag %GNUTLS_VERIFY_DO_NOT_ALLOW_IP_MATCHES is specified,
138
 * the input will be treated as a DNS name, and matching of textual IP addresses
139
 * against the IPAddress part of the alternative name will not be allowed.
140
 *
141
 * The function gnutls_x509_crt_check_ip() is available for matching
142
 * IP addresses.
143
 *
144
 * Returns: non-zero for a successful match, and zero on failure.
145
 *
146
 * Since: 3.3.0
147
 **/
148
unsigned gnutls_x509_crt_check_hostname2(gnutls_x509_crt_t cert,
149
           const char *hostname,
150
           unsigned int flags)
151
0
{
152
0
  char dnsname[MAX_CN];
153
0
  size_t dnsnamesize;
154
0
  int ret = 0;
155
0
  int i = 0;
156
0
  struct in_addr ipv4;
157
0
  const char *p = NULL;
158
0
  char *a_hostname;
159
0
  bool cn_fallback_allowed = true;
160
0
  gnutls_datum_t out;
161
162
  /* check whether @hostname is an ip address */
163
0
  if (!(flags & GNUTLS_VERIFY_DO_NOT_ALLOW_IP_MATCHES) &&
164
0
      ((p = strchr(hostname, ':')) != NULL ||
165
0
       inet_pton(AF_INET, hostname, &ipv4) != 0)) {
166
0
    if (p != NULL) {
167
0
      struct in6_addr ipv6;
168
169
0
      ret = inet_pton(AF_INET6, hostname, &ipv6);
170
0
      if (ret == 0) {
171
0
        gnutls_assert();
172
0
        goto hostname_fallback;
173
0
      }
174
0
      ret = check_ip(cert, &ipv6, 16);
175
0
    } else {
176
0
      ret = check_ip(cert, &ipv4, 4);
177
0
    }
178
179
    /* Prior to 3.6.0 we were accepting misconfigured servers, that place their IP
180
     * in the DNS field of subjectAlternativeName. That is no longer the case. */
181
0
    return ret;
182
0
  }
183
184
0
hostname_fallback:
185
  /* convert the provided hostname to ACE-Labels domain. */
186
0
  ret = gnutls_idna_map(hostname, strlen(hostname), &out, 0);
187
0
  if (ret < 0) {
188
0
    _gnutls_debug_log(
189
0
      "unable to convert hostname %s to IDNA format\n",
190
0
      hostname);
191
0
    a_hostname = (char *)hostname;
192
0
  } else {
193
0
    a_hostname = (char *)out.data;
194
0
  }
195
196
  /* try matching against:
197
   *  1) a DNS name as an alternative name (subjectAltName) extension
198
   *     in the certificate
199
   *  2) the common name (CN) in the certificate, if the certificate is acceptable for TLS_WWW_SERVER purpose
200
   *
201
   *  either of these may be of the form: *.domain.tld
202
   *
203
   *  only try (2) if there is no subjectAltName extension of
204
   *  type dNSName, and there is a single CN.
205
   */
206
207
  /* Check through all included subjectAltName extensions, comparing
208
   * against all those of type dNSName.
209
   */
210
0
  for (i = 0; !(ret < 0); i++) {
211
0
    dnsnamesize = sizeof(dnsname);
212
0
    ret = gnutls_x509_crt_get_subject_alt_name(cert, i, dnsname,
213
0
                 &dnsnamesize, NULL);
214
215
0
    if (ret < 0) {
216
0
      if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) {
217
        /* oversized SAN; proceed without CN fallback */
218
0
        _gnutls_debug_log("oversized SAN ignored, "
219
0
              "disabling CN fallback\n");
220
0
        cn_fallback_allowed = false;
221
0
        ret = 0;
222
0
        continue;
223
0
      }
224
0
      if (ret != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
225
0
        gnutls_assert();
226
0
      break;
227
0
    }
228
229
0
    if (PRECLUDES_CN_FALLBACK(ret))
230
0
      cn_fallback_allowed = false;
231
232
0
    if (ret == GNUTLS_SAN_DNSNAME) {
233
0
      if (memchr(dnsname, '\0', dnsnamesize)) {
234
0
        _gnutls_debug_log(
235
0
          "certificate has %s with embedded null in name\n",
236
0
          dnsname);
237
0
        continue;
238
0
      }
239
240
0
      if (!_gnutls_str_is_print(dnsname, dnsnamesize)) {
241
0
        _gnutls_debug_log(
242
0
          "invalid (non-ASCII) name in certificate %.*s\n",
243
0
          (int)dnsnamesize, dnsname);
244
0
        continue;
245
0
      }
246
247
0
      ret = _gnutls_hostname_compare(dnsname, dnsnamesize,
248
0
                   a_hostname, flags);
249
0
      if (ret != 0) {
250
0
        ret = 1;
251
0
        goto cleanup;
252
0
      }
253
0
    }
254
0
  }
255
256
0
  if (cn_fallback_allowed &&
257
0
      _gnutls_check_key_purpose(cert, GNUTLS_KP_TLS_WWW_SERVER, 0) != 0) {
258
    /* did not get the necessary extension, use CN instead, if the
259
     * certificate would have been acceptable for a TLS WWW server purpose.
260
     * That is because only for that purpose the CN is a valid field to
261
     * store the hostname.
262
     */
263
264
    /* enforce the RFC6125 (ยง1.8) requirement that only
265
     * a single CN must be present */
266
0
    dnsnamesize = sizeof(dnsname);
267
0
    ret = gnutls_x509_crt_get_dn_by_oid(cert, OID_X520_COMMON_NAME,
268
0
                1, 0, dnsname,
269
0
                &dnsnamesize);
270
0
    if (ret != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) {
271
0
      ret = 0;
272
0
      goto cleanup;
273
0
    }
274
275
0
    dnsnamesize = sizeof(dnsname);
276
0
    ret = gnutls_x509_crt_get_dn_by_oid(cert, OID_X520_COMMON_NAME,
277
0
                0, 0, dnsname,
278
0
                &dnsnamesize);
279
0
    if (ret < 0) {
280
0
      ret = 0;
281
0
      goto cleanup;
282
0
    }
283
284
0
    if (memchr(dnsname, '\0', dnsnamesize)) {
285
0
      _gnutls_debug_log(
286
0
        "certificate has CN %s with embedded null in name\n",
287
0
        dnsname);
288
0
      ret = 0;
289
0
      goto cleanup;
290
0
    }
291
292
0
    if (!_gnutls_str_is_print(dnsname, dnsnamesize)) {
293
0
      _gnutls_debug_log(
294
0
        "invalid (non-ASCII) name in certificate CN %.*s\n",
295
0
        (int)dnsnamesize, dnsname);
296
0
      ret = 0;
297
0
      goto cleanup;
298
0
    }
299
300
0
    ret = _gnutls_hostname_compare(dnsname, dnsnamesize, a_hostname,
301
0
                 flags);
302
0
    if (ret != 0) {
303
0
      ret = 1;
304
0
      goto cleanup;
305
0
    }
306
0
  }
307
308
  /* not found a matching name
309
   */
310
0
  ret = 0;
311
0
cleanup:
312
0
  if (a_hostname != hostname) {
313
    gnutls_free(a_hostname);
314
0
  }
315
0
  return ret;
316
0
}