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.
 
 
 
 
 
 

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