a_mbstr.c 13 KB

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