Coverage Report

Created: 2024-05-20 06:23

/src/nss/lib/ssl/tls13echv.c
Line
Count
Source (jump to first uncovered line)
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
0
{
16
0
    PRUint8 v = 0xff;
17
0
    if (c >= '0' && c <= '9') {
18
0
        v = c - '0';
19
0
    } else if (radix > 10) {
20
0
        if (c >= 'a' && c <= 'f') {
21
0
            v = c - 'a';
22
0
        } else if (c >= 'A' && c <= 'F') {
23
0
            v = c - 'A';
24
0
        }
25
0
    }
26
0
    if (v >= radix) {
27
0
        return SECFailure;
28
0
    }
29
0
    *d = v;
30
0
    return SECSuccess;
31
0
}
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
0
{
44
0
    if (*i == len || str[*i] == '.') {
45
0
        return SECFailure;
46
0
    }
47
0
    if (str[*i] == '0') {
48
0
        (*i)++;
49
0
        if (*i < len && (str[*i] == 'x' || str[*i] == 'X')) {
50
0
            (*i)++;
51
0
            if (*i == len || str[*i] == '.') {
52
0
                return SECFailure;
53
0
            }
54
0
            *radix = 16;
55
0
        } else {
56
0
            *radix = 8;
57
0
        }
58
0
    } else {
59
0
        *radix = 10;
60
0
    }
61
0
    return SECSuccess;
62
0
}
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
0
{
73
0
    PRUint8 radix;
74
0
    SECStatus rv = tls13_IpRadix(str, len, i, &radix);
75
0
    if (rv != SECSuccess) {
76
0
        return SECFailure;
77
0
    }
78
0
    PRUint64 part = 0;
79
0
    while (*i < len) {
80
0
        PRUint8 d;
81
0
        rv = tls13_IpDigit(str[*i], radix, &d);
82
0
        if (rv != SECSuccess) {
83
0
            if (str[*i] != '.') {
84
0
                return SECFailure;
85
0
            }
86
0
            break;
87
0
        }
88
0
        part = part * radix + d;
89
0
        if (part > PR_UINT32_MAX) {
90
0
            return SECFailure;
91
0
        }
92
0
        (*i)++;
93
0
    }
94
0
    *v = part;
95
0
    return SECSuccess;
96
0
}
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
0
{
104
0
    if (!end) {
105
0
        return PR_FALSE;
106
0
    }
107
0
    return v <= limit;
108
0
}
109
110
/* Returns true if `str` contains an IPv4 address. */
111
PRBool
112
tls13_IsIp(const PRUint8 *str, unsigned int len)
113
0
{
114
0
    PRUint32 part;
115
0
    PRUint32 v;
116
0
    unsigned int i = 0;
117
0
    for (part = 0; part < 4; part++) {
118
0
        SECStatus rv = tls13_IpValue(str, len, &i, &v);
119
0
        if (rv != SECSuccess) {
120
0
            return PR_FALSE;
121
0
        }
122
0
        if (v > 0xff || i == len) {
123
0
            return tls13_IpLastPart(i == len, v, PR_UINT32_MAX >> (part * 8));
124
0
        }
125
0
        PORT_Assert(str[i] == '.');
126
0
        i++;
127
0
    }
128
129
0
    return tls13_IpLastPart(i == len, v, 0xff);
130
0
}
131
132
static PRBool
133
tls13_IsLD(PRUint8 c)
134
0
{
135
0
    return (c >= 'a' && c <= 'z') ||
136
0
           (c >= 'A' && c <= 'Z') ||
137
0
           (c >= '0' && c <= '9') ||
138
0
           c == '_'; /* not in spec, but in the world; bug 1136616 */
139
0
}
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
0
{
147
0
    unsigned int i = 0;
148
0
    while (i < len && tls13_IsLD(str[i])) {
149
0
        unsigned int labelEnd = PR_MIN(len, i + 63);
150
0
        i++;
151
0
        while (i < labelEnd && (tls13_IsLD(str[i]) || str[i] == '-')) {
152
0
            i++;
153
0
        }
154
0
        if (str[i - 1] == '-') {
155
            /* labels cannot end in a hyphen */
156
0
            return PR_FALSE;
157
0
        }
158
0
        if (i == len) {
159
0
            return PR_TRUE;
160
0
        }
161
0
        if (str[i] != '.') {
162
0
            return PR_FALSE;
163
0
        }
164
0
        i++;
165
0
    }
166
0
    return PR_FALSE;
167
0
}