Coverage Report

Created: 2026-02-14 07:20

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