You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

410 lines
12 KiB

  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. static int traverse_string(const unsigned char *p, int len, int inform,
  61. int (*rfunc) (unsigned long value, void *in),
  62. void *arg);
  63. static int in_utf8(unsigned long value, void *arg);
  64. static int out_utf8(unsigned long value, void *arg);
  65. static int type_str(unsigned long value, void *arg);
  66. static int cpy_asc(unsigned long value, void *arg);
  67. static int cpy_bmp(unsigned long value, void *arg);
  68. static int cpy_univ(unsigned long value, void *arg);
  69. static int cpy_utf8(unsigned long value, void *arg);
  70. static int is_printable(unsigned long value);
  71. /*
  72. * These functions take a string in UTF8, ASCII or multibyte form and a mask
  73. * of permissible ASN1 string types. It then works out the minimal type
  74. * (using the order Printable < IA5 < T61 < BMP < Universal < UTF8) and
  75. * creates a string of the correct type with the supplied data. Yes this is
  76. * horrible: it has to be :-( The 'ncopy' form checks minimum and maximum
  77. * 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 = 0;
  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)
  98. len = strlen((const char *)in);
  99. if (!mask)
  100. mask = DIRSTRING_TYPE;
  101. /* First do a string check and work out the number of characters */
  102. switch (inform) {
  103. case MBSTRING_BMP:
  104. if (len & 1) {
  105. OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_BMPSTRING_LENGTH);
  106. return -1;
  107. }
  108. nchar = len >> 1;
  109. break;
  110. case MBSTRING_UNIV:
  111. if (len & 3) {
  112. OPENSSL_PUT_ERROR(ASN1, 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. OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_UTF8STRING);
  123. return -1;
  124. }
  125. break;
  126. case MBSTRING_ASC:
  127. nchar = len;
  128. break;
  129. default:
  130. OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNKNOWN_FORMAT);
  131. return -1;
  132. }
  133. if ((minsize > 0) && (nchar < minsize)) {
  134. OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_SHORT);
  135. BIO_snprintf(strbuf, sizeof strbuf, "%ld", minsize);
  136. ERR_add_error_data(2, "minsize=", strbuf);
  137. return -1;
  138. }
  139. if ((maxsize > 0) && (nchar > maxsize)) {
  140. OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_LONG);
  141. BIO_snprintf(strbuf, sizeof strbuf, "%ld", maxsize);
  142. ERR_add_error_data(2, "maxsize=", strbuf);
  143. return -1;
  144. }
  145. /* Now work out minimal type (if any) */
  146. if (traverse_string(in, len, inform, type_str, &mask) < 0) {
  147. OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_CHARACTERS);
  148. return -1;
  149. }
  150. /* Now work out output format and string type */
  151. outform = MBSTRING_ASC;
  152. if (mask & B_ASN1_PRINTABLESTRING)
  153. str_type = V_ASN1_PRINTABLESTRING;
  154. else if (mask & B_ASN1_IA5STRING)
  155. str_type = V_ASN1_IA5STRING;
  156. else if (mask & B_ASN1_T61STRING)
  157. str_type = V_ASN1_T61STRING;
  158. else if (mask & B_ASN1_BMPSTRING) {
  159. str_type = V_ASN1_BMPSTRING;
  160. outform = MBSTRING_BMP;
  161. } else if (mask & B_ASN1_UNIVERSALSTRING) {
  162. str_type = V_ASN1_UNIVERSALSTRING;
  163. outform = MBSTRING_UNIV;
  164. } else {
  165. str_type = V_ASN1_UTF8STRING;
  166. outform = MBSTRING_UTF8;
  167. }
  168. if (!out)
  169. return str_type;
  170. if (*out) {
  171. free_out = 0;
  172. dest = *out;
  173. if (dest->data) {
  174. dest->length = 0;
  175. OPENSSL_free(dest->data);
  176. dest->data = NULL;
  177. }
  178. dest->type = str_type;
  179. } else {
  180. free_out = 1;
  181. dest = ASN1_STRING_type_new(str_type);
  182. if (!dest) {
  183. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  184. return -1;
  185. }
  186. *out = dest;
  187. }
  188. /* If both the same type just copy across */
  189. if (inform == outform) {
  190. if (!ASN1_STRING_set(dest, in, len)) {
  191. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  192. return -1;
  193. }
  194. return str_type;
  195. }
  196. /* Work out how much space the destination will need */
  197. switch (outform) {
  198. case MBSTRING_ASC:
  199. outlen = nchar;
  200. cpyfunc = cpy_asc;
  201. break;
  202. case MBSTRING_BMP:
  203. outlen = nchar << 1;
  204. cpyfunc = cpy_bmp;
  205. break;
  206. case MBSTRING_UNIV:
  207. outlen = nchar << 2;
  208. cpyfunc = cpy_univ;
  209. break;
  210. case MBSTRING_UTF8:
  211. outlen = 0;
  212. traverse_string(in, len, inform, out_utf8, &outlen);
  213. cpyfunc = cpy_utf8;
  214. break;
  215. }
  216. if (!(p = OPENSSL_malloc(outlen + 1))) {
  217. if (free_out)
  218. ASN1_STRING_free(dest);
  219. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  220. return -1;
  221. }
  222. dest->length = outlen;
  223. dest->data = p;
  224. p[outlen] = 0;
  225. traverse_string(in, len, inform, cpyfunc, &p);
  226. return str_type;
  227. }
  228. /*
  229. * This function traverses a string and passes the value of each character to
  230. * an optional function along with a void * argument.
  231. */
  232. static int traverse_string(const unsigned char *p, int len, int inform,
  233. int (*rfunc) (unsigned long value, void *in),
  234. void *arg)
  235. {
  236. unsigned long value;
  237. int ret;
  238. while (len) {
  239. if (inform == MBSTRING_ASC) {
  240. value = *p++;
  241. len--;
  242. } else if (inform == MBSTRING_BMP) {
  243. value = *p++ << 8;
  244. value |= *p++;
  245. len -= 2;
  246. } else if (inform == MBSTRING_UNIV) {
  247. value = ((unsigned long)*p++) << 24;
  248. value |= ((unsigned long)*p++) << 16;
  249. value |= *p++ << 8;
  250. value |= *p++;
  251. len -= 4;
  252. } else {
  253. ret = UTF8_getc(p, len, &value);
  254. if (ret < 0)
  255. return -1;
  256. len -= ret;
  257. p += ret;
  258. }
  259. if (rfunc) {
  260. ret = rfunc(value, arg);
  261. if (ret <= 0)
  262. return ret;
  263. }
  264. }
  265. return 1;
  266. }
  267. /* Various utility functions for traverse_string */
  268. /* Just count number of characters */
  269. static int in_utf8(unsigned long value, void *arg)
  270. {
  271. int *nchar;
  272. nchar = arg;
  273. (*nchar)++;
  274. return 1;
  275. }
  276. /* Determine size of output as a UTF8 String */
  277. static int out_utf8(unsigned long value, void *arg)
  278. {
  279. int *outlen;
  280. outlen = arg;
  281. *outlen += UTF8_putc(NULL, -1, value);
  282. return 1;
  283. }
  284. /*
  285. * Determine the "type" of a string: check each character against a supplied
  286. * "mask".
  287. */
  288. static int type_str(unsigned long value, void *arg)
  289. {
  290. unsigned long types;
  291. types = *((unsigned long *)arg);
  292. if ((types & B_ASN1_PRINTABLESTRING) && !is_printable(value))
  293. types &= ~B_ASN1_PRINTABLESTRING;
  294. if ((types & B_ASN1_IA5STRING) && (value > 127))
  295. types &= ~B_ASN1_IA5STRING;
  296. if ((types & B_ASN1_T61STRING) && (value > 0xff))
  297. types &= ~B_ASN1_T61STRING;
  298. if ((types & B_ASN1_BMPSTRING) && (value > 0xffff))
  299. types &= ~B_ASN1_BMPSTRING;
  300. if (!types)
  301. return -1;
  302. *((unsigned long *)arg) = types;
  303. return 1;
  304. }
  305. /* Copy one byte per character ASCII like strings */
  306. static int cpy_asc(unsigned long value, void *arg)
  307. {
  308. unsigned char **p, *q;
  309. p = arg;
  310. q = *p;
  311. *q = (unsigned char)value;
  312. (*p)++;
  313. return 1;
  314. }
  315. /* Copy two byte per character BMPStrings */
  316. static int cpy_bmp(unsigned long value, void *arg)
  317. {
  318. unsigned char **p, *q;
  319. p = arg;
  320. q = *p;
  321. *q++ = (unsigned char)((value >> 8) & 0xff);
  322. *q = (unsigned char)(value & 0xff);
  323. *p += 2;
  324. return 1;
  325. }
  326. /* Copy four byte per character UniversalStrings */
  327. static int cpy_univ(unsigned long value, void *arg)
  328. {
  329. unsigned char **p, *q;
  330. p = arg;
  331. q = *p;
  332. *q++ = (unsigned char)((value >> 24) & 0xff);
  333. *q++ = (unsigned char)((value >> 16) & 0xff);
  334. *q++ = (unsigned char)((value >> 8) & 0xff);
  335. *q = (unsigned char)(value & 0xff);
  336. *p += 4;
  337. return 1;
  338. }
  339. /* Copy to a UTF8String */
  340. static int cpy_utf8(unsigned long value, void *arg)
  341. {
  342. unsigned char **p;
  343. int ret;
  344. p = arg;
  345. /* We already know there is enough room so pass 0xff as the length */
  346. ret = UTF8_putc(*p, 0xff, value);
  347. *p += ret;
  348. return 1;
  349. }
  350. /* Return 1 if the character is permitted in a PrintableString */
  351. static int is_printable(unsigned long value)
  352. {
  353. int ch;
  354. if (value > 0x7f)
  355. return 0;
  356. ch = (int)value;
  357. /*
  358. * Note: we can't use 'isalnum' because certain accented characters may
  359. * count as alphanumeric in some environments.
  360. */
  361. if ((ch >= 'a') && (ch <= 'z'))
  362. return 1;
  363. if ((ch >= 'A') && (ch <= 'Z'))
  364. return 1;
  365. if ((ch >= '0') && (ch <= '9'))
  366. return 1;
  367. if ((ch == ' ') || strchr("'()+,-./:=?", ch))
  368. return 1;
  369. return 0;
  370. }