Coverage Report

Created: 2025-12-04 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/crypto/punycode.c
Line
Count
Source
1
/*
2
 * Copyright 2019-2023 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
21.7k
#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
88.6k
{
47
88.6k
    unsigned int k = 0;
48
49
88.6k
    delta = (firsttime) ? delta / damp : delta / 2;
50
88.6k
    delta = delta + delta / numpoints;
51
52
160k
    while (delta > ((base - tmin) * tmax) / 2) {
53
71.9k
        delta = delta / (base - tmin);
54
71.9k
        k = k + base;
55
71.9k
    }
56
57
88.6k
    return k + (((base - tmin + 1) * delta) / (delta + skew));
58
88.6k
}
59
60
static ossl_inline int is_basic(unsigned int a)
61
35.7k
{
62
35.7k
    return (a < 0x80) ? 1 : 0;
63
35.7k
}
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
203k
{
74
203k
    if (a >= 0x41 && a <= 0x5A)
75
55.8k
        return a - 0x41;
76
77
148k
    if (a >= 0x61 && a <= 0x7A)
78
102k
        return a - 0x61;
79
80
45.1k
    if (a >= 0x30 && a <= 0x39)
81
44.5k
        return a - 0x30 + 26;
82
83
624
    return -1;
84
45.1k
}
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
19.1k
{
122
19.1k
    unsigned int n = initial_n;
123
19.1k
    unsigned int i = 0;
124
19.1k
    unsigned int bias = initial_bias;
125
19.1k
    size_t processed_in = 0, written_out = 0;
126
19.1k
    unsigned int max_out = *pout_length;
127
19.1k
    unsigned int basic_count = 0;
128
19.1k
    unsigned int loop;
129
130
113M
    for (loop = 0; loop < enc_len; loop++) {
131
113M
        if (pEncoded[loop] == delimiter)
132
5.98M
            basic_count = loop;
133
113M
    }
134
135
19.1k
    if (basic_count > 0) {
136
5.94k
        if (basic_count > max_out)
137
504
            return 0;
138
139
32.4k
        for (loop = 0; loop < basic_count; loop++) {
140
27.0k
            if (is_basic(pEncoded[loop]) == 0)
141
53
                return 0;
142
143
27.0k
            pDecoded[loop] = pEncoded[loop];
144
27.0k
            written_out++;
145
27.0k
        }
146
5.38k
        processed_in = basic_count + 1;
147
5.38k
    }
148
149
79.0k
    for (loop = processed_in; loop < enc_len;) {
150
61.6k
        unsigned int oldi = i;
151
61.6k
        unsigned int w = 1;
152
61.6k
        unsigned int k, t;
153
61.6k
        int digit;
154
155
138k
        for (k = base;; k += base) {
156
138k
            if (loop >= enc_len)
157
295
                return 0;
158
159
138k
            digit = digit_decoded(pEncoded[loop]);
160
138k
            loop++;
161
162
138k
            if (digit < 0)
163
405
                return 0;
164
137k
            if ((unsigned int)digit > (maxint - i) / w)
165
42
                return 0;
166
167
137k
            i = i + digit * w;
168
137k
            t = (k <= bias) ? tmin : (k >= bias + tmax) ? tmax : k - bias;
169
170
137k
            if ((unsigned int)digit < t)
171
60.9k
                break;
172
173
76.9k
            if (w > maxint / (base - t))
174
0
                return 0;
175
76.9k
            w = w * (base - t);
176
76.9k
        }
177
178
60.9k
        bias = adapt(i - oldi, written_out + 1, (oldi == 0));
179
60.9k
        if (i / (written_out + 1) > maxint - n)
180
130
            return 0;
181
60.7k
        n = n + i / (written_out + 1);
182
60.7k
        i %= (written_out + 1);
183
184
60.7k
        if (written_out >= max_out)
185
285
            return 0;
186
187
60.4k
        memmove(pDecoded + i + 1, pDecoded + i,
188
60.4k
                (written_out - i) * sizeof(*pDecoded));
189
60.4k
        pDecoded[i] = n;
190
60.4k
        i++;
191
60.4k
        written_out++;
192
60.4k
    }
193
194
17.4k
    *pout_length = written_out;
195
17.4k
    return 1;
196
18.5k
}
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
72.2k
{
205
72.2k
    if (utf <= 0x7F) {
206
        /* Plain ASCII */
207
26.9k
        out[0] = (unsigned char)utf;
208
26.9k
        out[1] = 0;
209
26.9k
        return 1;
210
45.3k
    } else if (utf <= 0x07FF) {
211
        /* 2-byte unicode */
212
30.3k
        out[0] = (unsigned char)(((utf >> 6) & 0x1F) | 0xC0);
213
30.3k
        out[1] = (unsigned char)(((utf >> 0) & 0x3F) | 0x80);
214
30.3k
        out[2] = 0;
215
30.3k
        return 2;
216
30.3k
    } else if (utf <= 0xFFFF) {
217
        /* 3-byte unicode */
218
11.7k
        out[0] = (unsigned char)(((utf >> 12) & 0x0F) | 0xE0);
219
11.7k
        out[1] = (unsigned char)(((utf >> 6) & 0x3F) | 0x80);
220
11.7k
        out[2] = (unsigned char)(((utf >> 0) & 0x3F) | 0x80);
221
11.7k
        out[3] = 0;
222
11.7k
        return 3;
223
11.7k
    } else if (utf <= 0x10FFFF) {
224
        /* 4-byte unicode */
225
2.91k
        out[0] = (unsigned char)(((utf >> 18) & 0x07) | 0xF0);
226
2.91k
        out[1] = (unsigned char)(((utf >> 12) & 0x3F) | 0x80);
227
2.91k
        out[2] = (unsigned char)(((utf >> 6) & 0x3F) | 0x80);
228
2.91k
        out[3] = (unsigned char)(((utf >> 0) & 0x3F) | 0x80);
229
2.91k
        out[4] = 0;
230
2.91k
        return 4;
231
2.91k
    } else {
232
        /* error - use replacement character */
233
317
        out[0] = (unsigned char)0xEF;
234
317
        out[1] = (unsigned char)0xBF;
235
317
        out[2] = (unsigned char)0xBD;
236
317
        out[3] = 0;
237
317
        return 0;
238
317
    }
239
72.2k
}
240
241
/*-
242
 * Return values:
243
 * 1 - ok
244
 * 0 - ok but buf was too short
245
 * -1 - bad string passed or other error
246
 */
247
248
int ossl_a2ulabel(const char *in, char *out, size_t outlen)
249
3.83k
{
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
3.83k
    const char *inptr = in;
257
3.83k
    int result = 1;
258
3.83k
    unsigned int i;
259
3.83k
    unsigned int buf[LABEL_BUF_SIZE];      /* It's a hostname */
260
3.83k
    WPACKET pkt;
261
262
    /* Internal API, so should not fail */
263
3.83k
    if (!ossl_assert(out != NULL))
264
0
        return -1;
265
266
3.83k
    if (!WPACKET_init_static_len(&pkt, (unsigned char *)out, outlen, 0))
267
0
        return -1;
268
269
55.2k
    while (1) {
270
55.2k
        char *tmpptr = strchr(inptr, '.');
271
55.2k
        size_t delta = tmpptr != NULL ? (size_t)(tmpptr - inptr) : strlen(inptr);
272
273
55.2k
        if (!HAS_PREFIX(inptr, "xn--")) {
274
33.5k
            if (!WPACKET_memcpy(&pkt, inptr, delta))
275
22.4k
                result = 0;
276
33.5k
        } else {
277
21.7k
            unsigned int bufsize = LABEL_BUF_SIZE;
278
279
21.7k
            if (ossl_punycode_decode(inptr + 4, delta - 4, buf, &bufsize) <= 0) {
280
313
                result = -1;
281
313
                goto end;
282
313
            }
283
284
93.3k
            for (i = 0; i < bufsize; i++) {
285
72.2k
                unsigned char seed[6];
286
72.2k
                size_t utfsize = codepoint2utf8(seed, buf[i]);
287
288
72.2k
                if (utfsize == 0) {
289
317
                    result = -1;
290
317
                    goto end;
291
317
                }
292
293
71.9k
                if (!WPACKET_memcpy(&pkt, seed, utfsize))
294
66.5k
                    result = 0;
295
71.9k
            }
296
21.4k
        }
297
298
54.6k
        if (tmpptr == NULL)
299
3.20k
            break;
300
301
51.4k
        if (!WPACKET_put_bytes_u8(&pkt, '.'))
302
48.5k
            result = 0;
303
304
51.4k
        inptr = tmpptr + 1;
305
51.4k
    }
306
307
3.20k
    if (!WPACKET_put_bytes_u8(&pkt, '\0'))
308
611
        result = 0;
309
3.83k
 end:
310
3.83k
    WPACKET_cleanup(&pkt);
311
3.83k
    return result;
312
3.20k
}