Coverage Report

Created: 2023-09-25 06:45

/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
12.6k
{
46
12.6k
    unsigned int k = 0;
47
48
12.6k
    delta = (firsttime) ? delta / damp : delta / 2;
49
12.6k
    delta = delta + delta / numpoints;
50
51
33.8k
    while (delta > ((base - tmin) * tmax) / 2) {
52
21.1k
        delta = delta / (base - tmin);
53
21.1k
        k = k + base;
54
21.1k
    }
55
56
12.6k
    return k + (((base - tmin + 1) * delta) / (delta + skew));
57
12.6k
}
58
59
static ossl_inline int is_basic(unsigned int a)
60
2.80k
{
61
2.80k
    return (a < 0x80) ? 1 : 0;
62
2.80k
}
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
41.2k
{
73
41.2k
    if (a >= 0x41 && a <= 0x5A)
74
2.59k
        return a - 0x41;
75
76
38.6k
    if (a >= 0x61 && a <= 0x7A)
77
36.0k
        return a - 0x61;
78
79
2.56k
    if (a >= 0x30 && a <= 0x39)
80
2.46k
        return a - 0x30 + 26;
81
82
100
    return -1;
83
2.56k
}
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
2.07k
{
121
2.07k
    unsigned int n = initial_n;
122
2.07k
    unsigned int i = 0;
123
2.07k
    unsigned int bias = initial_bias;
124
2.07k
    size_t processed_in = 0, written_out = 0;
125
2.07k
    unsigned int max_out = *pout_length;
126
2.07k
    unsigned int basic_count = 0;
127
2.07k
    unsigned int loop;
128
129
25.1M
    for (loop = 0; loop < enc_len; loop++) {
130
25.1M
        if (pEncoded[loop] == delimiter)
131
392k
            basic_count = loop;
132
25.1M
    }
133
134
2.07k
    if (basic_count > 0) {
135
751
        if (basic_count > max_out)
136
132
            return 0;
137
138
3.40k
        for (loop = 0; loop < basic_count; loop++) {
139
2.80k
            if (is_basic(pEncoded[loop]) == 0)
140
16
                return 0;
141
142
2.78k
            pDecoded[loop] = pEncoded[loop];
143
2.78k
            written_out++;
144
2.78k
        }
145
603
        processed_in = basic_count + 1;
146
603
    }
147
148
14.4k
    for (loop = processed_in; loop < enc_len;) {
149
12.8k
        unsigned int oldi = i;
150
12.8k
        unsigned int w = 1;
151
12.8k
        unsigned int k, t;
152
12.8k
        int digit;
153
154
41.3k
        for (k = base;; k += base) {
155
41.3k
            if (loop >= enc_len)
156
81
                return 0;
157
158
41.2k
            digit = digit_decoded(pEncoded[loop]);
159
41.2k
            loop++;
160
161
41.2k
            if (digit < 0)
162
100
                return 0;
163
41.1k
            if ((unsigned int)digit > (maxint - i) / w)
164
13
                return 0;
165
166
41.1k
            i = i + digit * w;
167
41.1k
            t = (k <= bias) ? tmin : (k >= bias + tmax) ? tmax : k - bias;
168
169
41.1k
            if ((unsigned int)digit < t)
170
12.6k
                break;
171
172
28.4k
            if (w > maxint / (base - t))
173
0
                return 0;
174
28.4k
            w = w * (base - t);
175
28.4k
        }
176
177
12.6k
        bias = adapt(i - oldi, written_out + 1, (oldi == 0));
178
12.6k
        if (i / (written_out + 1) > maxint - n)
179
30
            return 0;
180
12.5k
        n = n + i / (written_out + 1);
181
12.5k
        i %= (written_out + 1);
182
183
12.5k
        if (written_out >= max_out)
184
72
            return 0;
185
186
12.5k
        memmove(pDecoded + i + 1, pDecoded + i,
187
12.5k
                (written_out - i) * sizeof(*pDecoded));
188
12.5k
        pDecoded[i] = n;
189
12.5k
        i++;
190
12.5k
        written_out++;
191
12.5k
    }
192
193
1.62k
    *pout_length = written_out;
194
1.62k
    return 1;
195
1.92k
}
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
5.63k
{
204
5.63k
    if (utf <= 0x7F) {
205
        /* Plain ASCII */
206
1.26k
        out[0] = (unsigned char)utf;
207
1.26k
        out[1] = 0;
208
1.26k
        return 1;
209
4.36k
    } else if (utf <= 0x07FF) {
210
        /* 2-byte unicode */
211
3.31k
        out[0] = (unsigned char)(((utf >> 6) & 0x1F) | 0xC0);
212
3.31k
        out[1] = (unsigned char)(((utf >> 0) & 0x3F) | 0x80);
213
3.31k
        out[2] = 0;
214
3.31k
        return 2;
215
3.31k
    } else if (utf <= 0xFFFF) {
216
        /* 3-byte unicode */
217
386
        out[0] = (unsigned char)(((utf >> 12) & 0x0F) | 0xE0);
218
386
        out[1] = (unsigned char)(((utf >> 6) & 0x3F) | 0x80);
219
386
        out[2] = (unsigned char)(((utf >> 0) & 0x3F) | 0x80);
220
386
        out[3] = 0;
221
386
        return 3;
222
669
    } else if (utf <= 0x10FFFF) {
223
        /* 4-byte unicode */
224
612
        out[0] = (unsigned char)(((utf >> 18) & 0x07) | 0xF0);
225
612
        out[1] = (unsigned char)(((utf >> 12) & 0x3F) | 0x80);
226
612
        out[2] = (unsigned char)(((utf >> 6) & 0x3F) | 0x80);
227
612
        out[3] = (unsigned char)(((utf >> 0) & 0x3F) | 0x80);
228
612
        out[4] = 0;
229
612
        return 4;
230
612
    } else {
231
        /* error - use replacement character */
232
57
        out[0] = (unsigned char)0xEF;
233
57
        out[1] = (unsigned char)0xBF;
234
57
        out[2] = (unsigned char)0xBD;
235
57
        out[3] = 0;
236
57
        return 0;
237
57
    }
238
5.63k
}
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
}