a_mbstr.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 "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. if (dest->data) {
  179. dest->length = 0;
  180. OPENSSL_free(dest->data);
  181. dest->data = NULL;
  182. }
  183. dest->type = str_type;
  184. } else {
  185. free_out = 1;
  186. dest = ASN1_STRING_type_new(str_type);
  187. if (!dest) {
  188. ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ERR_R_MALLOC_FAILURE);
  189. return -1;
  190. }
  191. *out = dest;
  192. }
  193. /* If both the same type just copy across */
  194. if (inform == outform) {
  195. if (!ASN1_STRING_set(dest, in, len)) {
  196. ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ERR_R_MALLOC_FAILURE);
  197. return -1;
  198. }
  199. return str_type;
  200. }
  201. /* Work out how much space the destination will need */
  202. switch (outform) {
  203. case MBSTRING_ASC:
  204. outlen = nchar;
  205. cpyfunc = cpy_asc;
  206. break;
  207. case MBSTRING_BMP:
  208. outlen = nchar << 1;
  209. cpyfunc = cpy_bmp;
  210. break;
  211. case MBSTRING_UNIV:
  212. outlen = nchar << 2;
  213. cpyfunc = cpy_univ;
  214. break;
  215. case MBSTRING_UTF8:
  216. outlen = 0;
  217. traverse_string(in, len, inform, out_utf8, &outlen);
  218. cpyfunc = cpy_utf8;
  219. break;
  220. }
  221. if (!(p = OPENSSL_malloc(outlen + 1))) {
  222. if (free_out)
  223. ASN1_STRING_free(dest);
  224. ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ERR_R_MALLOC_FAILURE);
  225. return -1;
  226. }
  227. dest->length = outlen;
  228. dest->data = p;
  229. p[outlen] = 0;
  230. traverse_string(in, len, inform, cpyfunc, &p);
  231. return str_type;
  232. }
  233. /*
  234. * This function traverses a string and passes the value of each character to
  235. * an optional function along with a void * argument.
  236. */
  237. static int traverse_string(const unsigned char *p, int len, int inform,
  238. int (*rfunc) (unsigned long value, void *in),
  239. void *arg)
  240. {
  241. unsigned long value;
  242. int ret;
  243. while (len) {
  244. if (inform == MBSTRING_ASC) {
  245. value = *p++;
  246. len--;
  247. } else if (inform == MBSTRING_BMP) {
  248. value = *p++ << 8;
  249. value |= *p++;
  250. len -= 2;
  251. } else if (inform == MBSTRING_UNIV) {
  252. value = ((unsigned long)*p++) << 24;
  253. value |= ((unsigned long)*p++) << 16;
  254. value |= *p++ << 8;
  255. value |= *p++;
  256. len -= 4;
  257. } else {
  258. ret = UTF8_getc(p, len, &value);
  259. if (ret < 0)
  260. return -1;
  261. len -= ret;
  262. p += ret;
  263. }
  264. if (rfunc) {
  265. ret = rfunc(value, arg);
  266. if (ret <= 0)
  267. return ret;
  268. }
  269. }
  270. return 1;
  271. }
  272. /* Various utility functions for traverse_string */
  273. /* Just count number of characters */
  274. static int in_utf8(unsigned long value, void *arg)
  275. {
  276. int *nchar;
  277. nchar = arg;
  278. (*nchar)++;
  279. return 1;
  280. }
  281. /* Determine size of output as a UTF8 String */
  282. static int out_utf8(unsigned long value, void *arg)
  283. {
  284. int *outlen;
  285. outlen = arg;
  286. *outlen += UTF8_putc(NULL, -1, value);
  287. return 1;
  288. }
  289. /*
  290. * Determine the "type" of a string: check each character against a supplied
  291. * "mask".
  292. */
  293. static int type_str(unsigned long value, void *arg)
  294. {
  295. unsigned long types;
  296. types = *((unsigned long *)arg);
  297. if ((types & B_ASN1_PRINTABLESTRING) && !is_printable(value))
  298. types &= ~B_ASN1_PRINTABLESTRING;
  299. if ((types & B_ASN1_IA5STRING) && (value > 127))
  300. types &= ~B_ASN1_IA5STRING;
  301. if ((types & B_ASN1_T61STRING) && (value > 0xff))
  302. types &= ~B_ASN1_T61STRING;
  303. if ((types & B_ASN1_BMPSTRING) && (value > 0xffff))
  304. types &= ~B_ASN1_BMPSTRING;
  305. if (!types)
  306. return -1;
  307. *((unsigned long *)arg) = types;
  308. return 1;
  309. }
  310. /* Copy one byte per character ASCII like strings */
  311. static int cpy_asc(unsigned long value, void *arg)
  312. {
  313. unsigned char **p, *q;
  314. p = arg;
  315. q = *p;
  316. *q = (unsigned char)value;
  317. (*p)++;
  318. return 1;
  319. }
  320. /* Copy two byte per character BMPStrings */
  321. static int cpy_bmp(unsigned long value, void *arg)
  322. {
  323. unsigned char **p, *q;
  324. p = arg;
  325. q = *p;
  326. *q++ = (unsigned char)((value >> 8) & 0xff);
  327. *q = (unsigned char)(value & 0xff);
  328. *p += 2;
  329. return 1;
  330. }
  331. /* Copy four byte per character UniversalStrings */
  332. static int cpy_univ(unsigned long value, void *arg)
  333. {
  334. unsigned char **p, *q;
  335. p = arg;
  336. q = *p;
  337. *q++ = (unsigned char)((value >> 24) & 0xff);
  338. *q++ = (unsigned char)((value >> 16) & 0xff);
  339. *q++ = (unsigned char)((value >> 8) & 0xff);
  340. *q = (unsigned char)(value & 0xff);
  341. *p += 4;
  342. return 1;
  343. }
  344. /* Copy to a UTF8String */
  345. static int cpy_utf8(unsigned long value, void *arg)
  346. {
  347. unsigned char **p;
  348. int ret;
  349. p = arg;
  350. /* We already know there is enough room so pass 0xff as the length */
  351. ret = UTF8_putc(*p, 0xff, value);
  352. *p += ret;
  353. return 1;
  354. }
  355. /* Return 1 if the character is permitted in a PrintableString */
  356. static int is_printable(unsigned long value)
  357. {
  358. int ch;
  359. if (value > 0x7f)
  360. return 0;
  361. ch = (int)value;
  362. /*
  363. * Note: we can't use 'isalnum' because certain accented characters may
  364. * count as alphanumeric in some environments.
  365. */
  366. #ifndef CHARSET_EBCDIC
  367. if ((ch >= 'a') && (ch <= 'z'))
  368. return 1;
  369. if ((ch >= 'A') && (ch <= 'Z'))
  370. return 1;
  371. if ((ch >= '0') && (ch <= '9'))
  372. return 1;
  373. if ((ch == ' ') || strchr("'()+,-./:=?", ch))
  374. return 1;
  375. #else /* CHARSET_EBCDIC */
  376. if ((ch >= os_toascii['a']) && (ch <= os_toascii['z']))
  377. return 1;
  378. if ((ch >= os_toascii['A']) && (ch <= os_toascii['Z']))
  379. return 1;
  380. if ((ch >= os_toascii['0']) && (ch <= os_toascii['9']))
  381. return 1;
  382. if ((ch == os_toascii[' ']) || strchr("'()+,-./:=?", os_toebcdic[ch]))
  383. return 1;
  384. #endif /* CHARSET_EBCDIC */
  385. return 0;
  386. }