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