2 * Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
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
13 #include <openssl/e_os2.h>
14 #include "crypto/punycode.h"
15 #include "internal/cryptlib.h"
16 #include "internal/packet.h" /* for WPACKET */
18 static const unsigned int base = 36;
19 static const unsigned int tmin = 1;
20 static const unsigned int tmax = 26;
21 static const unsigned int skew = 38;
22 static const unsigned int damp = 700;
23 static const unsigned int initial_bias = 72;
24 static const unsigned int initial_n = 0x80;
25 static const unsigned int maxint = 0xFFFFFFFF;
26 static const char delimiter = '-';
28 #define LABEL_BUF_SIZE 512
33 * function adapt(delta,numpoints,firsttime):
34 * if firsttime then let delta = delta div damp
35 * else let delta = delta div 2
36 * let delta = delta + (delta div numpoints)
38 * while delta > ((base - tmin) * tmax) div 2 do begin
39 * let delta = delta div (base - tmin)
42 * return k + (((base - tmin + 1) * delta) div (delta + skew))
45 static int adapt(unsigned int delta, unsigned int numpoints,
46 unsigned int firsttime)
50 delta = (firsttime) ? delta / damp : delta / 2;
51 delta = delta + delta / numpoints;
53 while (delta > ((base - tmin) * tmax) / 2) {
54 delta = delta / (base - tmin);
58 return k + (((base - tmin + 1) * delta) / (delta + skew));
61 static ossl_inline int is_basic(unsigned int a)
63 return (a < 0x80) ? 1 : 0;
67 * code points digit-values
68 * ------------ ----------------------
69 * 41..5A (A-Z) = 0 to 25, respectively
70 * 61..7A (a-z) = 0 to 25, respectively
71 * 30..39 (0-9) = 26 to 35, respectively
73 static ossl_inline int digit_decoded(const unsigned char a)
75 if (a >= 0x41 && a <= 0x5A)
78 if (a >= 0x61 && a <= 0x7A)
81 if (a >= 0x30 && a <= 0x39)
90 * function ossl_punycode_decode
93 * let bias = initial_bias
94 * let output = an empty string indexed from 0
95 * consume all code points before the last delimiter (if there is one)
96 * and copy them to output, fail on any non-basic code point
97 * if more than zero code points were consumed then consume one more
98 * (which will be the last delimiter)
99 * while the input is not exhausted do begin
102 * for k = base to infinity in steps of base do begin
103 * consume a code point, or fail if there was none to consume
104 * let digit = the code point's digit-value, fail if it has none
105 * let i = i + digit * w, fail on overflow
106 * let t = tmin if k <= bias {+ tmin}, or
107 * tmax if k >= bias + tmax, or k - bias otherwise
108 * if digit < t then break
109 * let w = w * (base - t), fail on overflow
111 * let bias = adapt(i - oldi, length(output) + 1, test oldi is 0?)
112 * let n = n + i div (length(output) + 1), fail on overflow
113 * let i = i mod (length(output) + 1)
114 * {if n is a basic code point then fail}
115 * insert n into output at position i
120 int ossl_punycode_decode(const char *pEncoded, const size_t enc_len,
121 unsigned int *pDecoded, unsigned int *pout_length)
123 unsigned int n = initial_n;
125 unsigned int bias = initial_bias;
126 size_t processed_in = 0, written_out = 0;
127 unsigned int max_out = *pout_length;
128 unsigned int basic_count = 0;
131 for (loop = 0; loop < enc_len; loop++) {
132 if (pEncoded[loop] == delimiter)
136 if (basic_count > 0) {
137 if (basic_count > max_out)
140 for (loop = 0; loop < basic_count; loop++) {
141 if (is_basic(pEncoded[loop]) == 0)
144 pDecoded[loop] = pEncoded[loop];
147 processed_in = basic_count + 1;
150 for (loop = processed_in; loop < enc_len;) {
151 unsigned int oldi = i;
156 for (k = base;; k += base) {
160 digit = digit_decoded(pEncoded[loop]);
165 if ((unsigned int)digit > (maxint - i) / w)
169 t = (k <= bias) ? tmin : (k >= bias + tmax) ? tmax : k - bias;
171 if ((unsigned int)digit < t)
174 if (w > maxint / (base - t))
179 bias = adapt(i - oldi, written_out + 1, (oldi == 0));
180 if (i / (written_out + 1) > maxint - n)
182 n = n + i / (written_out + 1);
183 i %= (written_out + 1);
185 if (written_out >= max_out)
188 memmove(pDecoded + i + 1, pDecoded + i,
189 (written_out - i) * sizeof(*pDecoded));
195 *pout_length = written_out;
200 * Encode a code point using UTF-8
201 * return number of bytes on success, 0 on failure
202 * (also produces U+FFFD, which uses 3 bytes on failure)
204 static int codepoint2utf8(unsigned char *out, unsigned long utf)
208 out[0] = (unsigned char)utf;
211 } else if (utf <= 0x07FF) {
213 out[0] = (unsigned char)(((utf >> 6) & 0x1F) | 0xC0);
214 out[1] = (unsigned char)(((utf >> 0) & 0x3F) | 0x80);
217 } else if (utf <= 0xFFFF) {
219 out[0] = (unsigned char)(((utf >> 12) & 0x0F) | 0xE0);
220 out[1] = (unsigned char)(((utf >> 6) & 0x3F) | 0x80);
221 out[2] = (unsigned char)(((utf >> 0) & 0x3F) | 0x80);
224 } else if (utf <= 0x10FFFF) {
226 out[0] = (unsigned char)(((utf >> 18) & 0x07) | 0xF0);
227 out[1] = (unsigned char)(((utf >> 12) & 0x3F) | 0x80);
228 out[2] = (unsigned char)(((utf >> 6) & 0x3F) | 0x80);
229 out[3] = (unsigned char)(((utf >> 0) & 0x3F) | 0x80);
233 /* error - use replacement character */
234 out[0] = (unsigned char)0xEF;
235 out[1] = (unsigned char)0xBF;
236 out[2] = (unsigned char)0xBD;
245 * 0 - ok but buf was too short
246 * -1 - bad string passed or other error
249 int ossl_a2ulabel(const char *in, char *out, size_t outlen)
252 * Domain name has some parts consisting of ASCII chars joined with dot.
253 * If a part is shorter than 5 chars, it becomes U-label as is.
254 * If it does not start with xn--, it becomes U-label as is.
255 * Otherwise we try to decode it.
257 const char *inptr = in;
260 unsigned int buf[LABEL_BUF_SIZE]; /* It's a hostname */
263 /* Internal API, so should not fail */
264 if (!ossl_assert(out != NULL))
267 if (!WPACKET_init_static_len(&pkt, (unsigned char *)out, outlen, 0))
271 char *tmpptr = strchr(inptr, '.');
272 size_t delta = tmpptr != NULL ? (size_t)(tmpptr - inptr) : strlen(inptr);
274 if (strncmp(inptr, "xn--", 4) != 0) {
275 if (!WPACKET_memcpy(&pkt, inptr, delta))
278 unsigned int bufsize = LABEL_BUF_SIZE;
280 if (ossl_punycode_decode(inptr + 4, delta - 4, buf, &bufsize) <= 0) {
285 for (i = 0; i < bufsize; i++) {
286 unsigned char seed[6];
287 size_t utfsize = codepoint2utf8(seed, buf[i]);
294 if (!WPACKET_memcpy(&pkt, seed, utfsize))
302 if (!WPACKET_put_bytes_u8(&pkt, '.'))
308 if (!WPACKET_put_bytes_u8(&pkt, '\0'))
311 WPACKET_cleanup(&pkt);
318 * Returns 0 if compared values are equal
320 * -1 in case of errors
323 int ossl_a2ucompare(const char *a, const char *u)
325 char a_ulabel[LABEL_BUF_SIZE + 1];
326 size_t a_size = sizeof(a_ulabel);
328 if (ossl_a2ulabel(a, a_ulabel, a_size) <= 0)
331 return strcmp(a_ulabel, u) != 0;