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.
 
 
 
 
 
 

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