a_mbstr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/asn1.h>
  57. #include <string.h>
  58. #include <openssl/err.h>
  59. #include <openssl/mem.h>
  60. #include "asn1_locl.h"
  61. static int traverse_string(const unsigned char *p, int len, int inform,
  62. int (*rfunc) (uint32_t value, void *in),
  63. void *arg);
  64. static int in_utf8(uint32_t value, void *arg);
  65. static int out_utf8(uint32_t value, void *arg);
  66. static int type_str(uint32_t value, void *arg);
  67. static int cpy_asc(uint32_t value, void *arg);
  68. static int cpy_bmp(uint32_t value, void *arg);
  69. static int cpy_univ(uint32_t value, void *arg);
  70. static int cpy_utf8(uint32_t value, void *arg);
  71. static int is_printable(uint32_t value);
  72. /*
  73. * These functions take a string in UTF8, ASCII or multibyte form and a mask
  74. * of permissible ASN1 string types. It then works out the minimal type
  75. * (using the order Printable < IA5 < T61 < BMP < Universal < UTF8) and
  76. * creates a string of the correct type with the supplied data. Yes this is
  77. * horrible: it has to be :-( The 'ncopy' form checks minimum and maximum
  78. * size limits too.
  79. */
  80. int ASN1_mbstring_copy(ASN1_STRING **out, const unsigned char *in, int len,
  81. int inform, unsigned long mask)
  82. {
  83. return ASN1_mbstring_ncopy(out, in, len, inform, mask, 0, 0);
  84. }
  85. int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
  86. int inform, unsigned long mask,
  87. long minsize, long maxsize)
  88. {
  89. int str_type;
  90. int ret;
  91. char free_out;
  92. int outform, outlen = 0;
  93. ASN1_STRING *dest;
  94. unsigned char *p;
  95. int nchar;
  96. char strbuf[32];
  97. int (*cpyfunc) (uint32_t, void *) = NULL;
  98. if (len == -1)
  99. len = strlen((const char *)in);
  100. if (!mask)
  101. mask = DIRSTRING_TYPE;
  102. /* First do a string check and work out the number of characters */
  103. switch (inform) {
  104. case MBSTRING_BMP:
  105. if (len & 1) {
  106. OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_BMPSTRING_LENGTH);
  107. return -1;
  108. }
  109. nchar = len >> 1;
  110. break;
  111. case MBSTRING_UNIV:
  112. if (len & 3) {
  113. OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_UNIVERSALSTRING_LENGTH);
  114. return -1;
  115. }
  116. nchar = len >> 2;
  117. break;
  118. case MBSTRING_UTF8:
  119. nchar = 0;
  120. /* This counts the characters and does utf8 syntax checking */
  121. ret = traverse_string(in, len, MBSTRING_UTF8, in_utf8, &nchar);
  122. if (ret < 0) {
  123. OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_UTF8STRING);
  124. return -1;
  125. }
  126. break;
  127. case MBSTRING_ASC:
  128. nchar = len;
  129. break;
  130. default:
  131. OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNKNOWN_FORMAT);
  132. return -1;
  133. }
  134. if ((minsize > 0) && (nchar < minsize)) {
  135. OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_SHORT);
  136. BIO_snprintf(strbuf, sizeof strbuf, "%ld", minsize);
  137. ERR_add_error_data(2, "minsize=", strbuf);
  138. return -1;
  139. }
  140. if ((maxsize > 0) && (nchar > maxsize)) {
  141. OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_LONG);
  142. BIO_snprintf(strbuf, sizeof strbuf, "%ld", maxsize);
  143. ERR_add_error_data(2, "maxsize=", strbuf);
  144. return -1;
  145. }
  146. /* Now work out minimal type (if any) */
  147. if (traverse_string(in, len, inform, type_str, &mask) < 0) {
  148. OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_CHARACTERS);
  149. return -1;
  150. }
  151. /* Now work out output format and string type */
  152. outform = MBSTRING_ASC;
  153. if (mask & B_ASN1_PRINTABLESTRING)
  154. str_type = V_ASN1_PRINTABLESTRING;
  155. else if (mask & B_ASN1_IA5STRING)
  156. str_type = V_ASN1_IA5STRING;
  157. else if (mask & B_ASN1_T61STRING)
  158. str_type = V_ASN1_T61STRING;
  159. else if (mask & B_ASN1_BMPSTRING) {
  160. str_type = V_ASN1_BMPSTRING;
  161. outform = MBSTRING_BMP;
  162. } else if (mask & B_ASN1_UNIVERSALSTRING) {
  163. str_type = V_ASN1_UNIVERSALSTRING;
  164. outform = MBSTRING_UNIV;
  165. } else {
  166. str_type = V_ASN1_UTF8STRING;
  167. outform = MBSTRING_UTF8;
  168. }
  169. if (!out)
  170. return str_type;
  171. if (*out) {
  172. free_out = 0;
  173. dest = *out;
  174. if (dest->data) {
  175. dest->length = 0;
  176. OPENSSL_free(dest->data);
  177. dest->data = NULL;
  178. }
  179. dest->type = str_type;
  180. } else {
  181. free_out = 1;
  182. dest = ASN1_STRING_type_new(str_type);
  183. if (!dest) {
  184. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  185. return -1;
  186. }
  187. *out = dest;
  188. }
  189. /* If both the same type just copy across */
  190. if (inform == outform) {
  191. if (!ASN1_STRING_set(dest, in, len)) {
  192. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  193. return -1;
  194. }
  195. return str_type;
  196. }
  197. /* Work out how much space the destination will need */
  198. switch (outform) {
  199. case MBSTRING_ASC:
  200. outlen = nchar;
  201. cpyfunc = cpy_asc;
  202. break;
  203. case MBSTRING_BMP:
  204. outlen = nchar << 1;
  205. cpyfunc = cpy_bmp;
  206. break;
  207. case MBSTRING_UNIV:
  208. outlen = nchar << 2;
  209. cpyfunc = cpy_univ;
  210. break;
  211. case MBSTRING_UTF8:
  212. outlen = 0;
  213. traverse_string(in, len, inform, out_utf8, &outlen);
  214. cpyfunc = cpy_utf8;
  215. break;
  216. }
  217. if (!(p = OPENSSL_malloc(outlen + 1))) {
  218. if (free_out)
  219. ASN1_STRING_free(dest);
  220. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  221. return -1;
  222. }
  223. dest->length = outlen;
  224. dest->data = p;
  225. p[outlen] = 0;
  226. traverse_string(in, len, inform, cpyfunc, &p);
  227. return str_type;
  228. }
  229. /*
  230. * This function traverses a string and passes the value of each character to
  231. * an optional function along with a void * argument.
  232. */
  233. static int traverse_string(const unsigned char *p, int len, int inform,
  234. int (*rfunc) (uint32_t value, void *in),
  235. void *arg)
  236. {
  237. uint32_t value;
  238. int ret;
  239. while (len) {
  240. if (inform == MBSTRING_ASC) {
  241. value = *p++;
  242. len--;
  243. } else if (inform == MBSTRING_BMP) {
  244. value = *p++ << 8;
  245. value |= *p++;
  246. len -= 2;
  247. } else if (inform == MBSTRING_UNIV) {
  248. value = ((uint32_t)*p++) << 24;
  249. value |= ((uint32_t)*p++) << 16;
  250. value |= *p++ << 8;
  251. value |= *p++;
  252. len -= 4;
  253. } else {
  254. ret = UTF8_getc(p, len, &value);
  255. if (ret < 0)
  256. return -1;
  257. len -= ret;
  258. p += ret;
  259. }
  260. if (rfunc) {
  261. ret = rfunc(value, arg);
  262. if (ret <= 0)
  263. return ret;
  264. }
  265. }
  266. return 1;
  267. }
  268. /* Various utility functions for traverse_string */
  269. /* Just count number of characters */
  270. static int in_utf8(uint32_t value, void *arg)
  271. {
  272. int *nchar;
  273. nchar = arg;
  274. (*nchar)++;
  275. return 1;
  276. }
  277. /* Determine size of output as a UTF8 String */
  278. static int out_utf8(uint32_t value, void *arg)
  279. {
  280. int *outlen;
  281. outlen = arg;
  282. *outlen += UTF8_putc(NULL, -1, value);
  283. return 1;
  284. }
  285. /*
  286. * Determine the "type" of a string: check each character against a supplied
  287. * "mask".
  288. */
  289. static int type_str(uint32_t value, void *arg)
  290. {
  291. unsigned long types;
  292. types = *((unsigned long *)arg);
  293. if ((types & B_ASN1_PRINTABLESTRING) && !is_printable(value))
  294. types &= ~B_ASN1_PRINTABLESTRING;
  295. if ((types & B_ASN1_IA5STRING) && (value > 127))
  296. types &= ~B_ASN1_IA5STRING;
  297. if ((types & B_ASN1_T61STRING) && (value > 0xff))
  298. types &= ~B_ASN1_T61STRING;
  299. if ((types & B_ASN1_BMPSTRING) && (value > 0xffff))
  300. types &= ~B_ASN1_BMPSTRING;
  301. if (!types)
  302. return -1;
  303. *((unsigned long *)arg) = types;
  304. return 1;
  305. }
  306. /* Copy one byte per character ASCII like strings */
  307. static int cpy_asc(uint32_t value, void *arg)
  308. {
  309. unsigned char **p, *q;
  310. p = arg;
  311. q = *p;
  312. *q = (unsigned char)value;
  313. (*p)++;
  314. return 1;
  315. }
  316. /* Copy two byte per character BMPStrings */
  317. static int cpy_bmp(uint32_t value, void *arg)
  318. {
  319. unsigned char **p, *q;
  320. p = arg;
  321. q = *p;
  322. *q++ = (unsigned char)((value >> 8) & 0xff);
  323. *q = (unsigned char)(value & 0xff);
  324. *p += 2;
  325. return 1;
  326. }
  327. /* Copy four byte per character UniversalStrings */
  328. static int cpy_univ(uint32_t value, void *arg)
  329. {
  330. unsigned char **p, *q;
  331. p = arg;
  332. q = *p;
  333. *q++ = (unsigned char)((value >> 24) & 0xff);
  334. *q++ = (unsigned char)((value >> 16) & 0xff);
  335. *q++ = (unsigned char)((value >> 8) & 0xff);
  336. *q = (unsigned char)(value & 0xff);
  337. *p += 4;
  338. return 1;
  339. }
  340. /* Copy to a UTF8String */
  341. static int cpy_utf8(uint32_t value, void *arg)
  342. {
  343. unsigned char **p;
  344. int ret;
  345. p = arg;
  346. /* We already know there is enough room so pass 0xff as the length */
  347. ret = UTF8_putc(*p, 0xff, value);
  348. *p += ret;
  349. return 1;
  350. }
  351. /* Return 1 if the character is permitted in a PrintableString */
  352. static int is_printable(uint32_t value)
  353. {
  354. int ch;
  355. if (value > 0x7f)
  356. return 0;
  357. ch = (int)value;
  358. /*
  359. * Note: we can't use 'isalnum' because certain accented characters may
  360. * count as alphanumeric in some environments.
  361. */
  362. if ((ch >= 'a') && (ch <= 'z'))
  363. return 1;
  364. if ((ch >= 'A') && (ch <= 'Z'))
  365. return 1;
  366. if ((ch >= '0') && (ch <= '9'))
  367. return 1;
  368. if ((ch == ' ') || strchr("'()+,-./:=?", ch))
  369. return 1;
  370. return 0;
  371. }