Coverage Report

Created: 2026-10-13 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/nss/lib/ssl/tls13echv.c
Line
Count
Source
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
 * License, v. 2.0. If a copy of the MPL was not distributed with this
4
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
/* Validation functions for ECH public names. */
7
8
#include "seccomon.h"
9
10
/* Convert a single character `c` into a number `*d` with the given radix.
11
 * Fails if the character isn't valid for the radix.
12
 */
13
static SECStatus
14
tls13_IpDigit(PRUint8 c, PRUint8 radix, PRUint8 *d)
15
34.3k
{
16
34.3k
    PRUint8 v = 0xff;
17
34.3k
    if (c >= '0' && c <= '9') {
18
13.6k
        v = c - '0';
19
20.6k
    } else if (radix > 10) {
20
6.48k
        if (c >= 'a' && c <= 'f') {
21
1.15k
            v = c - 'a';
22
5.32k
        } else if (c >= 'A' && c <= 'F') {
23
2.69k
            v = c - 'A';
24
2.69k
        }
25
6.48k
    }
26
34.3k
    if (v >= radix) {
27
16.8k
        return SECFailure;
28
16.8k
    }
29
17.4k
    *d = v;
30
17.4k
    return SECSuccess;
31
34.3k
}
32
33
/* This function takes the first couple of characters from `str`, starting at offset
34
 * `*i` and calculates a radix.  If it starts with "0x" or "0X", then `*i` is moved up
35
 * by two and `*radix` is set to 16 (hexadecimal).  If it starts with "0", then `*i` is
36
 * moved up by one and `*radix` is set to 8 (octal).  Otherwise, `*i` is left alone and
37
 * `*radix` is set to 10 (decimal).
38
 * Fails if there are no characters remaining or the next character is '.', either at
39
 * the start or after "0x".
40
 */
41
static SECStatus
42
tls13_IpRadix(const PRUint8 *str, unsigned int len, unsigned int *i, PRUint8 *radix)
43
18.6k
{
44
18.6k
    if (*i == len || str[*i] == '.') {
45
0
        return SECFailure;
46
0
    }
47
18.6k
    if (str[*i] == '0') {
48
6.66k
        (*i)++;
49
6.66k
        if (*i < len && (str[*i] == 'x' || str[*i] == 'X')) {
50
4.29k
            (*i)++;
51
4.29k
            if (*i == len || str[*i] == '.') {
52
973
                return SECFailure;
53
973
            }
54
3.32k
            *radix = 16;
55
3.32k
        } else {
56
2.36k
            *radix = 8;
57
2.36k
        }
58
12.0k
    } else {
59
12.0k
        *radix = 10;
60
12.0k
    }
61
17.7k
    return SECSuccess;
62
18.6k
}
63
64
/* Take a number from `str` from offset `*i` and put the value in `*v`.
65
 * This calculates the radix and returns a value between 0 and 2^32-1, using all
66
 * of the digits up to the end of the string (determined by `len`) or a period ('.').
67
 * Fails if there is no value, if there a non-digit characters, or if the value is
68
 * too large.
69
 */
70
static SECStatus
71
tls13_IpValue(const PRUint8 *str, unsigned int len, unsigned int *i, PRUint32 *v)
72
18.6k
{
73
18.6k
    PRUint8 radix;
74
18.6k
    SECStatus rv = tls13_IpRadix(str, len, i, &radix);
75
18.6k
    if (rv != SECSuccess) {
76
973
        return SECFailure;
77
973
    }
78
17.7k
    PRUint64 part = 0;
79
34.9k
    while (*i < len) {
80
34.3k
        PRUint8 d;
81
34.3k
        rv = tls13_IpDigit(str[*i], radix, &d);
82
34.3k
        if (rv != SECSuccess) {
83
16.8k
            if (str[*i] != '.') {
84
11.3k
                return SECFailure;
85
11.3k
            }
86
5.52k
            break;
87
16.8k
        }
88
17.4k
        part = part * radix + d;
89
17.4k
        if (part > PR_UINT32_MAX) {
90
233
            return SECFailure;
91
233
        }
92
17.2k
        (*i)++;
93
17.2k
    }
94
6.15k
    *v = part;
95
6.15k
    return SECSuccess;
96
17.7k
}
97
98
/* Returns true if `end` is true and `v` is within the `limit`. Used to validate the
99
 * last part of an IPv4 address, which can hold larger numbers if there are fewer then
100
 * four parts. */
101
static PRBool
102
tls13_IpLastPart(PRBool end, PRUint32 v, PRUint32 limit)
103
1.53k
{
104
1.53k
    if (!end) {
105
908
        return PR_FALSE;
106
908
    }
107
631
    return v <= limit;
108
1.53k
}
109
110
/* Returns true if `str` contains an IPv4 address. */
111
PRBool
112
tls13_IsIp(const PRUint8 *str, unsigned int len)
113
14.0k
{
114
14.0k
    PRUint32 part;
115
14.0k
    PRUint32 v;
116
14.0k
    unsigned int i = 0;
117
19.1k
    for (part = 0; part < 4; part++) {
118
18.6k
        SECStatus rv = tls13_IpValue(str, len, &i, &v);
119
18.6k
        if (rv != SECSuccess) {
120
12.5k
            return PR_FALSE;
121
12.5k
        }
122
6.15k
        if (v > 0xff || i == len) {
123
1.12k
            return tls13_IpLastPart(i == len, v, PR_UINT32_MAX >> (part * 8));
124
1.12k
        }
125
5.03k
        PORT_Assert(str[i] == '.');
126
5.03k
        i++;
127
5.03k
    }
128
129
419
    return tls13_IpLastPart(i == len, v, 0xff);
130
14.0k
}
131
132
static PRBool
133
tls13_IsLD(PRUint8 c)
134
208k
{
135
208k
    return (c >= 'a' && c <= 'z') ||
136
65.8k
           (c >= 'A' && c <= 'Z') ||
137
55.0k
           (c >= '0' && c <= '9') ||
138
26.5k
           c == '_'; /* not in spec, but in the world; bug 1136616 */
139
208k
}
140
141
/* Is this a valid dotted LDH string (that is, an A-Label domain name)?
142
 * This does not tolerate a trailing '.', where the DNS generally does.
143
 */
144
PRBool
145
tls13_IsLDH(const PRUint8 *str, unsigned int len)
146
14.1k
{
147
14.1k
    unsigned int i = 0;
148
31.1k
    while (i < len && tls13_IsLD(str[i])) {
149
31.0k
        unsigned int labelEnd = PR_MIN(len, i + 63);
150
31.0k
        i++;
151
191k
        while (i < labelEnd && (tls13_IsLD(str[i]) || str[i] == '-')) {
152
160k
            i++;
153
160k
        }
154
31.0k
        if (str[i - 1] == '-') {
155
            /* labels cannot end in a hyphen */
156
11
            return PR_FALSE;
157
11
        }
158
31.0k
        if (i == len) {
159
14.0k
            return PR_TRUE;
160
14.0k
        }
161
17.0k
        if (str[i] != '.') {
162
47
            return PR_FALSE;
163
47
        }
164
16.9k
        i++;
165
16.9k
    }
166
47
    return PR_FALSE;
167
14.1k
}