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