punycode.c 9.2 KB

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