Coverage Report

Created: 2024-07-27 06:39

/src/openssl30/crypto/punycode.c
Line
Count
Source (jump to first uncovered line)
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
37.3k
{
46
37.3k
    unsigned int k = 0;
47
48
37.3k
    delta = (firsttime) ? delta / damp : delta / 2;
49
37.3k
    delta = delta + delta / numpoints;
50
51
98.4k
    while (delta > ((base - tmin) * tmax) / 2) {
52
61.1k
        delta = delta / (base - tmin);
53
61.1k
        k = k + base;
54
61.1k
    }
55
56
37.3k
    return k + (((base - tmin + 1) * delta) / (delta + skew));
57
37.3k
}
58
59
static ossl_inline int is_basic(unsigned int a)
60
10.1k
{
61
10.1k
    return (a < 0x80) ? 1 : 0;
62
10.1k
}
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
124k
{
73
124k
    if (a >= 0x41 && a <= 0x5A)
74
11.2k
        return a - 0x41;
75
76
112k
    if (a >= 0x61 && a <= 0x7A)
77
79.5k
        return a - 0x61;
78
79
33.4k
    if (a >= 0x30 && a <= 0x39)
80
33.1k
        return a - 0x30 + 26;
81
82
293
    return -1;
83
33.4k
}
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
6.96k
{
121
6.96k
    unsigned int n = initial_n;
122
6.96k
    unsigned int i = 0;
123
6.96k
    unsigned int bias = initial_bias;
124
6.96k
    size_t processed_in = 0, written_out = 0;
125
6.96k
    unsigned int max_out = *pout_length;
126
6.96k
    unsigned int basic_count = 0;
127
6.96k
    unsigned int loop;
128
129
71.7M
    for (loop = 0; loop < enc_len; loop++) {
130
71.7M
        if (pEncoded[loop] == delimiter)
131
3.34M
            basic_count = loop;
132
71.7M
    }
133
134
6.96k
    if (basic_count > 0) {
135
2.09k
        if (basic_count > max_out)
136
385
            return 0;
137
138
11.7k
        for (loop = 0; loop < basic_count; loop++) {
139
10.1k
            if (is_basic(pEncoded[loop]) == 0)
140
56
                return 0;
141
142
10.0k
            pDecoded[loop] = pEncoded[loop];
143
10.0k
            written_out++;
144
10.0k
        }
145
1.65k
        processed_in = basic_count + 1;
146
1.65k
    }
147
148
43.5k
    for (loop = processed_in; loop < enc_len;) {
149
37.9k
        unsigned int oldi = i;
150
37.9k
        unsigned int w = 1;
151
37.9k
        unsigned int k, t;
152
37.9k
        int digit;
153
154
124k
        for (k = base;; k += base) {
155
124k
            if (loop >= enc_len)
156
229
                return 0;
157
158
124k
            digit = digit_decoded(pEncoded[loop]);
159
124k
            loop++;
160
161
124k
            if (digit < 0)
162
293
                return 0;
163
123k
            if ((unsigned int)digit > (maxint - i) / w)
164
37
                return 0;
165
166
123k
            i = i + digit * w;
167
123k
            t = (k <= bias) ? tmin : (k >= bias + tmax) ? tmax : k - bias;
168
169
123k
            if ((unsigned int)digit < t)
170
37.3k
                break;
171
172
86.4k
            if (w > maxint / (base - t))
173
0
                return 0;
174
86.4k
            w = w * (base - t);
175
86.4k
        }
176
177
37.3k
        bias = adapt(i - oldi, written_out + 1, (oldi == 0));
178
37.3k
        if (i / (written_out + 1) > maxint - n)
179
103
            return 0;
180
37.2k
        n = n + i / (written_out + 1);
181
37.2k
        i %= (written_out + 1);
182
183
37.2k
        if (written_out >= max_out)
184
198
            return 0;
185
186
37.0k
        memmove(pDecoded + i + 1, pDecoded + i,
187
37.0k
                (written_out - i) * sizeof(*pDecoded));
188
37.0k
        pDecoded[i] = n;
189
37.0k
        i++;
190
37.0k
        written_out++;
191
37.0k
    }
192
193
5.65k
    *pout_length = written_out;
194
5.65k
    return 1;
195
6.51k
}
196
197
/*
198
 * Encode a code point using UTF-8
199
 * return number of bytes on success, 0 on failure
200
 * (also produces U+FFFD, which uses 3 bytes on failure)
201
 */
202
static int codepoint2utf8(unsigned char *out, unsigned long utf)
203
19.0k
{
204
19.0k
    if (utf <= 0x7F) {
205
        /* Plain ASCII */
206
6.02k
        out[0] = (unsigned char)utf;
207
6.02k
        out[1] = 0;
208
6.02k
        return 1;
209
13.0k
    } else if (utf <= 0x07FF) {
210
        /* 2-byte unicode */
211
9.79k
        out[0] = (unsigned char)(((utf >> 6) & 0x1F) | 0xC0);
212
9.79k
        out[1] = (unsigned char)(((utf >> 0) & 0x3F) | 0x80);
213
9.79k
        out[2] = 0;
214
9.79k
        return 2;
215
9.79k
    } else if (utf <= 0xFFFF) {
216
        /* 3-byte unicode */
217
1.51k
        out[0] = (unsigned char)(((utf >> 12) & 0x0F) | 0xE0);
218
1.51k
        out[1] = (unsigned char)(((utf >> 6) & 0x3F) | 0x80);
219
1.51k
        out[2] = (unsigned char)(((utf >> 0) & 0x3F) | 0x80);
220
1.51k
        out[3] = 0;
221
1.51k
        return 3;
222
1.74k
    } else if (utf <= 0x10FFFF) {
223
        /* 4-byte unicode */
224
1.56k
        out[0] = (unsigned char)(((utf >> 18) & 0x07) | 0xF0);
225
1.56k
        out[1] = (unsigned char)(((utf >> 12) & 0x3F) | 0x80);
226
1.56k
        out[2] = (unsigned char)(((utf >> 6) & 0x3F) | 0x80);
227
1.56k
        out[3] = (unsigned char)(((utf >> 0) & 0x3F) | 0x80);
228
1.56k
        out[4] = 0;
229
1.56k
        return 4;
230
1.56k
    } else {
231
        /* error - use replacement character */
232
171
        out[0] = (unsigned char)0xEF;
233
171
        out[1] = (unsigned char)0xBF;
234
171
        out[2] = (unsigned char)0xBD;
235
171
        out[3] = 0;
236
171
        return 0;
237
171
    }
238
19.0k
}
239
240
/*-
241
 * Return values:
242
 * 1 - ok, *outlen contains valid buf length
243
 * 0 - ok but buf was too short, *outlen contains valid buf length
244
 * -1 - bad string passed
245
 */
246
247
int ossl_a2ulabel(const char *in, char *out, size_t *outlen)
248
{
249
    /*-
250
     * Domain name has some parts consisting of ASCII chars joined with dot.
251
     * If a part is shorter than 5 chars, it becomes U-label as is.
252
     * If it does not start with xn--,    it becomes U-label as is.
253
     * Otherwise we try to decode it.
254
     */
255
    char *outptr = out;
256
    const char *inptr = in;
257
    size_t size = 0, maxsize;
258
    int result = 1;
259
    unsigned int i, j;
260
    unsigned int buf[LABEL_BUF_SIZE];      /* It's a hostname */
261
262
    if (out == NULL) {
263
        result = 0;
264
        maxsize = 0;
265
    } else {
266
        maxsize = *outlen;
267
    }
268
269
#define PUSHC(c)                    \
270
    do                              \
271
        if (size++ < maxsize)       \
272
            *outptr++ = c;          \
273
        else                        \
274
            result = 0;             \
275
    while (0)
276
277
    while (1) {
278
        char *tmpptr = strchr(inptr, '.');
279
        size_t delta = tmpptr != NULL ? (size_t)(tmpptr - inptr) : strlen(inptr);
280
281
        if (strncmp(inptr, "xn--", 4) != 0) {
282
            for (i = 0; i < delta + 1; i++)
283
                PUSHC(inptr[i]);
284
        } else {
285
            unsigned int bufsize = LABEL_BUF_SIZE;
286
287
            if (ossl_punycode_decode(inptr + 4, delta - 4, buf, &bufsize) <= 0)
288
                return -1;
289
290
            for (i = 0; i < bufsize; i++) {
291
                unsigned char seed[6];
292
                size_t utfsize = codepoint2utf8(seed, buf[i]);
293
294
                if (utfsize == 0)
295
                    return -1;
296
297
                for (j = 0; j < utfsize; j++)
298
                    PUSHC(seed[j]);
299
            }
300
301
            PUSHC(tmpptr != NULL ? '.' : '\0');
302
        }
303
304
        if (tmpptr == NULL)
305
            break;
306
307
        inptr = tmpptr + 1;
308
    }
309
#undef PUSHC
310
311
    *outlen = size;
312
    return result;
313
}
314
315
/*-
316
 * a MUST be A-label
317
 * u MUST be U-label
318
 * Returns 0 if compared values are equal
319
 * 1 if not
320
 * -1 in case of errors
321
 */
322
323
int ossl_a2ucompare(const char *a, const char *u)
324
0
{
325
0
    char a_ulabel[LABEL_BUF_SIZE + 1];
326
0
    size_t a_size = sizeof(a_ulabel);
327
328
0
    if (ossl_a2ulabel(a, a_ulabel, &a_size) <= 0)
329
0
        return -1;
330
331
0
    return strcmp(a_ulabel, u) != 0;
332
0
}