a_mbstr.c 11 KB

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