punycode.c 9.1 KB

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