Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/punycode.c
Line
Count
Source
1
/*
2
 * Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <stddef.h>
11
#include <string.h>
12
#include <stdio.h>
13
#include <openssl/e_os2.h>
14
#include "crypto/punycode.h"
15
16
static const unsigned int base = 36;
17
static const unsigned int tmin = 1;
18
static const unsigned int tmax = 26;
19
static const unsigned int skew = 38;
20
static const unsigned int damp = 700;
21
static const unsigned int initial_bias = 72;
22
static const unsigned int initial_n = 0x80;
23
static const unsigned int maxint = 0xFFFFFFFF;
24
static const char delimiter = '-';
25
26
#define LABEL_BUF_SIZE 512
27
28
/*-
29
 * Pseudocode:
30
 *
31
 * function adapt(delta,numpoints,firsttime):
32
 *  if firsttime then let delta = delta div damp
33
 *  else let delta = delta div 2
34
 *  let delta = delta + (delta div numpoints)
35
 *  let k = 0
36
 *  while delta > ((base - tmin) * tmax) div 2 do begin
37
 *    let delta = delta div (base - tmin)
38
 *    let k = k + base
39
 *  end
40
 *  return k + (((base - tmin + 1) * delta) div (delta + skew))
41
 */
42
43
static int adapt(unsigned int delta, unsigned int numpoints,
44
    unsigned int firsttime)
45
78.5k
{
46
78.5k
    unsigned int k = 0;
47
48
78.5k
    delta = (firsttime) ? delta / damp : delta / 2;
49
78.5k
    delta = delta + delta / numpoints;
50
51
136k
    while (delta > ((base - tmin) * tmax) / 2) {
52
58.3k
        delta = delta / (base - tmin);
53
58.3k
        k = k + base;
54
58.3k
    }
55
56
78.5k
    return k + (((base - tmin + 1) * delta) / (delta + skew));
57
78.5k
}
58
59
static ossl_inline int is_basic(unsigned int a)
60
21.2k
{
61
21.2k
    return (a < 0x80) ? 1 : 0;
62
21.2k
}
63
64
/*-
65
 * code points    digit-values
66
 * ------------   ----------------------
67
 * 41..5A (A-Z) =  0 to 25, respectively
68
 * 61..7A (a-z) =  0 to 25, respectively
69
 * 30..39 (0-9) = 26 to 35, respectively
70
 */
71
static ossl_inline int digit_decoded(const unsigned char a)
72
170k
{
73
170k
    if (a >= 0x41 && a <= 0x5A)
74
53.1k
        return a - 0x41;
75
76
117k
    if (a >= 0x61 && a <= 0x7A)
77
99.6k
        return a - 0x61;
78
79
17.9k
    if (a >= 0x30 && a <= 0x39)
80
17.3k
        return a - 0x30 + 26;
81
82
544
    return -1;
83
17.9k
}
84
85
/*-
86
 * Pseudocode:
87
 *
88
 * function ossl_punycode_decode
89
 *  let n = initial_n
90
 *  let i = 0
91
 *  let bias = initial_bias
92
 *  let output = an empty string indexed from 0
93
 *  consume all code points before the last delimiter (if there is one)
94
 *    and copy them to output, fail on any non-basic code point
95
 *  if more than zero code points were consumed then consume one more
96
 *    (which will be the last delimiter)
97
 *  while the input is not exhausted do begin
98
 *    let oldi = i
99
 *    let w = 1
100
 *    for k = base to infinity in steps of base do begin
101
 *      consume a code point, or fail if there was none to consume
102
 *      let digit = the code point's digit-value, fail if it has none
103
 *      let i = i + digit * w, fail on overflow
104
 *      let t = tmin if k <= bias {+ tmin}, or
105
 *              tmax if k >= bias + tmax, or k - bias otherwise
106
 *      if digit < t then break
107
 *      let w = w * (base - t), fail on overflow
108
 *    end
109
 *    let bias = adapt(i - oldi, length(output) + 1, test oldi is 0?)
110
 *    let n = n + i div (length(output) + 1), fail on overflow
111
 *    let i = i mod (length(output) + 1)
112
 *    {if n is a basic code point then fail}
113
 *    insert n into output at position i
114
 *    increment i
115
 *  end
116
 */
117
118
int ossl_punycode_decode(const char *pEncoded, const size_t enc_len,
119
    unsigned int *pDecoded, unsigned int *pout_length)
120
13.8k
{
121
13.8k
    unsigned int n = initial_n;
122
13.8k
    unsigned int i = 0;
123
13.8k
    unsigned int bias = initial_bias;
124
13.8k
    size_t processed_in = 0, written_out = 0;
125
13.8k
    unsigned int max_out = *pout_length;
126
13.8k
    unsigned int basic_count = 0;
127
13.8k
    unsigned int loop;
128
129
83.1M
    for (loop = 0; loop < enc_len; loop++) {
130
83.1M
        if (pEncoded[loop] == delimiter)
131
9.45M
            basic_count = loop;
132
83.1M
    }
133
134
13.8k
    if (basic_count > 0) {
135
2.66k
        if (basic_count > max_out)
136
367
            return 0;
137
138
14.3k
        for (loop = 0; loop < basic_count; loop++) {
139
12.1k
            if (is_basic(pEncoded[loop]) == 0)
140
50
                return 0;
141
142
12.0k
            pDecoded[loop] = pEncoded[loop];
143
12.0k
            written_out++;
144
12.0k
        }
145
2.24k
        processed_in = basic_count + 1;
146
2.24k
    }
147
148
57.7k
    for (loop = processed_in; loop < enc_len;) {
149
45.1k
        unsigned int oldi = i;
150
45.1k
        unsigned int w = 1;
151
45.1k
        unsigned int k, t;
152
45.1k
        int digit;
153
154
94.5k
        for (k = base;; k += base) {
155
94.5k
            if (loop >= enc_len)
156
225
                return 0;
157
158
94.3k
            digit = digit_decoded(pEncoded[loop]);
159
94.3k
            loop++;
160
161
94.3k
            if (digit < 0)
162
323
                return 0;
163
94.0k
            if ((unsigned int)digit > (maxint - i) / w)
164
33
                return 0;
165
166
94.0k
            i = i + digit * w;
167
94.0k
            t = (k <= bias) ? tmin : (k >= bias + tmax) ? tmax
168
52.0k
                                                        : k - bias;
169
170
94.0k
            if ((unsigned int)digit < t)
171
44.6k
                break;
172
173
49.4k
            if (w > maxint / (base - t))
174
0
                return 0;
175
49.4k
            w = w * (base - t);
176
49.4k
        }
177
178
44.6k
        bias = adapt(i - oldi, written_out + 1, (oldi == 0));
179
44.6k
        if (i / (written_out + 1) > maxint - n)
180
95
            return 0;
181
44.5k
        n = n + i / (written_out + 1);
182
44.5k
        i %= (written_out + 1);
183
184
44.5k
        if (written_out >= max_out)
185
208
            return 0;
186
187
44.3k
        memmove(pDecoded + i + 1, pDecoded + i,
188
44.3k
            (written_out - i) * sizeof(*pDecoded));
189
44.3k
        pDecoded[i] = n;
190
44.3k
        i++;
191
44.3k
        written_out++;
192
44.3k
    }
193
194
12.5k
    *pout_length = written_out;
195
12.5k
    return 1;
196
13.4k
}
197
198
/*
199
 * Encode a code point using UTF-8
200
 * return number of bytes on success, 0 on failure
201
 * (also produces U+FFFD, which uses 3 bytes on failure)
202
 */
203
static int codepoint2utf8(unsigned char *out, unsigned long utf)
204
53.6k
{
205
53.6k
    if (utf <= 0x7F) {
206
        /* Plain ASCII */
207
13.9k
        out[0] = (unsigned char)utf;
208
13.9k
        out[1] = 0;
209
13.9k
        return 1;
210
39.6k
    } else if (utf <= 0x07FF) {
211
        /* 2-byte unicode */
212
25.8k
        out[0] = (unsigned char)(((utf >> 6) & 0x1F) | 0xC0);
213
25.8k
        out[1] = (unsigned char)(((utf >> 0) & 0x3F) | 0x80);
214
25.8k
        out[2] = 0;
215
25.8k
        return 2;
216
25.8k
    } else if (utf <= 0xFFFF) {
217
        /* 3-byte unicode */
218
11.1k
        out[0] = (unsigned char)(((utf >> 12) & 0x0F) | 0xE0);
219
11.1k
        out[1] = (unsigned char)(((utf >> 6) & 0x3F) | 0x80);
220
11.1k
        out[2] = (unsigned char)(((utf >> 0) & 0x3F) | 0x80);
221
11.1k
        out[3] = 0;
222
11.1k
        return 3;
223
11.1k
    } else if (utf <= 0x10FFFF) {
224
        /* 4-byte unicode */
225
2.44k
        out[0] = (unsigned char)(((utf >> 18) & 0x07) | 0xF0);
226
2.44k
        out[1] = (unsigned char)(((utf >> 12) & 0x3F) | 0x80);
227
2.44k
        out[2] = (unsigned char)(((utf >> 6) & 0x3F) | 0x80);
228
2.44k
        out[3] = (unsigned char)(((utf >> 0) & 0x3F) | 0x80);
229
2.44k
        out[4] = 0;
230
2.44k
        return 4;
231
2.44k
    } else {
232
        /* error - use replacement character */
233
274
        out[0] = (unsigned char)0xEF;
234
274
        out[1] = (unsigned char)0xBF;
235
274
        out[2] = (unsigned char)0xBD;
236
274
        out[3] = 0;
237
274
        return 0;
238
274
    }
239
53.6k
}
240
241
/*-
242
 * Return values:
243
 * 1 - ok, *outlen contains valid buf length
244
 * 0 - ok but buf was too short, *outlen contains valid buf length
245
 * -1 - bad string passed
246
 */
247
248
int ossl_a2ulabel(const char *in, char *out, size_t *outlen)
249
{
250
    /*-
251
     * Domain name has some parts consisting of ASCII chars joined with dot.
252
     * If a part is shorter than 5 chars, it becomes U-label as is.
253
     * If it does not start with xn--,    it becomes U-label as is.
254
     * Otherwise we try to decode it.
255
     */
256
    char *outptr = out;
257
    const char *inptr = in;
258
    size_t size = 0, maxsize;
259
    int result = 1;
260
    unsigned int i, j;
261
    unsigned int buf[LABEL_BUF_SIZE]; /* It's a hostname */
262
263
    if (out == NULL) {
264
        result = 0;
265
        maxsize = 0;
266
    } else {
267
        maxsize = *outlen;
268
    }
269
270
#define PUSHC(c)              \
271
    do                        \
272
        if (size++ < maxsize) \
273
            *outptr++ = c;    \
274
        else                  \
275
            result = 0;       \
276
    while (0)
277
278
    while (1) {
279
        char *tmpptr = strchr(inptr, '.');
280
        size_t delta = tmpptr != NULL ? (size_t)(tmpptr - inptr) : strlen(inptr);
281
282
        if (strncmp(inptr, "xn--", 4) != 0) {
283
            for (i = 0; i < delta + 1; i++)
284
                PUSHC(inptr[i]);
285
        } else {
286
            unsigned int bufsize = LABEL_BUF_SIZE;
287
288
            if (ossl_punycode_decode(inptr + 4, delta - 4, buf, &bufsize) <= 0)
289
                return -1;
290
291
            for (i = 0; i < bufsize; i++) {
292
                unsigned char seed[6];
293
                size_t utfsize = codepoint2utf8(seed, buf[i]);
294
295
                if (utfsize == 0)
296
                    return -1;
297
298
                for (j = 0; j < utfsize; j++)
299
                    PUSHC(seed[j]);
300
            }
301
302
            PUSHC(tmpptr != NULL ? '.' : '\0');
303
        }
304
305
        if (tmpptr == NULL)
306
            break;
307
308
        inptr = tmpptr + 1;
309
    }
310
#undef PUSHC
311
312
    *outlen = size;
313
    return result;
314
}
315
316
/*-
317
 * a MUST be A-label
318
 * u MUST be U-label
319
 * Returns 0 if compared values are equal
320
 * 1 if not
321
 * -1 in case of errors
322
 */
323
324
int ossl_a2ucompare(const char *a, const char *u)
325
0
{
326
0
    char a_ulabel[LABEL_BUF_SIZE + 1];
327
0
    size_t a_size = sizeof(a_ulabel);
328
329
0
    if (ossl_a2ulabel(a, a_ulabel, &a_size) <= 0)
330
0
        return -1;
331
332
0
    return strcmp(a_ulabel, u) != 0;
333
0
}