25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

a_strex.c 16 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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/x509.h>
  57. #include <string.h>
  58. #include <openssl/asn1.h>
  59. #include <openssl/mem.h>
  60. #include <openssl/obj.h>
  61. #include "charmap.h"
  62. /* ASN1_STRING_print_ex() and X509_NAME_print_ex().
  63. * Enhanced string and name printing routines handling
  64. * multibyte characters, RFC2253 and a host of other
  65. * options.
  66. */
  67. #define CHARTYPE_BS_ESC (ASN1_STRFLGS_ESC_2253 | CHARTYPE_FIRST_ESC_2253 | CHARTYPE_LAST_ESC_2253)
  68. #define ESC_FLAGS (ASN1_STRFLGS_ESC_2253 | \
  69. ASN1_STRFLGS_ESC_QUOTE | \
  70. ASN1_STRFLGS_ESC_CTRL | \
  71. ASN1_STRFLGS_ESC_MSB)
  72. static int send_bio_chars(void *arg, const void *buf, int len)
  73. {
  74. if(!arg) return 1;
  75. if(BIO_write(arg, buf, len) != len) return 0;
  76. return 1;
  77. }
  78. static int send_fp_chars(void *arg, const void *buf, int len)
  79. {
  80. if(!arg) return 1;
  81. if(fwrite(buf, 1, len, arg) != (unsigned int)len) return 0;
  82. return 1;
  83. }
  84. typedef int char_io(void *arg, const void *buf, int len);
  85. /* This function handles display of
  86. * strings, one character at a time.
  87. * It is passed an unsigned long for each
  88. * character because it could come from 2 or even
  89. * 4 byte forms.
  90. */
  91. #define HEX_SIZE(type) (sizeof(type)*2)
  92. static int do_esc_char(unsigned long c, unsigned char flags, char *do_quotes, char_io *io_ch, void *arg)
  93. {
  94. unsigned char chflgs, chtmp;
  95. char tmphex[HEX_SIZE(long)+3];
  96. if(c > 0xffffffffL)
  97. return -1;
  98. if(c > 0xffff) {
  99. BIO_snprintf(tmphex, sizeof tmphex, "\\W%08lX", c);
  100. if(!io_ch(arg, tmphex, 10)) return -1;
  101. return 10;
  102. }
  103. if(c > 0xff) {
  104. BIO_snprintf(tmphex, sizeof tmphex, "\\U%04lX", c);
  105. if(!io_ch(arg, tmphex, 6)) return -1;
  106. return 6;
  107. }
  108. chtmp = (unsigned char)c;
  109. if(chtmp > 0x7f) chflgs = flags & ASN1_STRFLGS_ESC_MSB;
  110. else chflgs = char_type[chtmp] & flags;
  111. if(chflgs & CHARTYPE_BS_ESC) {
  112. /* If we don't escape with quotes, signal we need quotes */
  113. if(chflgs & ASN1_STRFLGS_ESC_QUOTE) {
  114. if(do_quotes) *do_quotes = 1;
  115. if(!io_ch(arg, &chtmp, 1)) return -1;
  116. return 1;
  117. }
  118. if(!io_ch(arg, "\\", 1)) return -1;
  119. if(!io_ch(arg, &chtmp, 1)) return -1;
  120. return 2;
  121. }
  122. if(chflgs & (ASN1_STRFLGS_ESC_CTRL|ASN1_STRFLGS_ESC_MSB)) {
  123. BIO_snprintf(tmphex, 11, "\\%02X", chtmp);
  124. if(!io_ch(arg, tmphex, 3)) return -1;
  125. return 3;
  126. }
  127. /* If we get this far and do any escaping at all must escape
  128. * the escape character itself: backslash.
  129. */
  130. if (chtmp == '\\' && flags & ESC_FLAGS) {
  131. if(!io_ch(arg, "\\\\", 2)) return -1;
  132. return 2;
  133. }
  134. if(!io_ch(arg, &chtmp, 1)) return -1;
  135. return 1;
  136. }
  137. #define BUF_TYPE_WIDTH_MASK 0x7
  138. #define BUF_TYPE_CONVUTF8 0x8
  139. /* This function sends each character in a buffer to
  140. * do_esc_char(). It interprets the content formats
  141. * and converts to or from UTF8 as appropriate.
  142. */
  143. static int do_buf(unsigned char *buf, int buflen,
  144. int type, unsigned char flags, char *quotes, char_io *io_ch, void *arg)
  145. {
  146. int i, outlen, len;
  147. unsigned char orflags, *p, *q;
  148. unsigned long c;
  149. p = buf;
  150. q = buf + buflen;
  151. outlen = 0;
  152. while(p != q) {
  153. if(p == buf && flags & ASN1_STRFLGS_ESC_2253) orflags = CHARTYPE_FIRST_ESC_2253;
  154. else orflags = 0;
  155. switch(type & BUF_TYPE_WIDTH_MASK) {
  156. case 4:
  157. c = ((unsigned long)*p++) << 24;
  158. c |= ((unsigned long)*p++) << 16;
  159. c |= ((unsigned long)*p++) << 8;
  160. c |= *p++;
  161. break;
  162. case 2:
  163. c = ((unsigned long)*p++) << 8;
  164. c |= *p++;
  165. break;
  166. case 1:
  167. c = *p++;
  168. break;
  169. case 0:
  170. i = UTF8_getc(p, buflen, &c);
  171. if(i < 0) return -1; /* Invalid UTF8String */
  172. p += i;
  173. break;
  174. default:
  175. return -1; /* invalid width */
  176. }
  177. if (p == q && flags & ASN1_STRFLGS_ESC_2253) orflags = CHARTYPE_LAST_ESC_2253;
  178. if(type & BUF_TYPE_CONVUTF8) {
  179. unsigned char utfbuf[6];
  180. int utflen;
  181. utflen = UTF8_putc(utfbuf, sizeof utfbuf, c);
  182. for(i = 0; i < utflen; i++) {
  183. /* We don't need to worry about setting orflags correctly
  184. * because if utflen==1 its value will be correct anyway
  185. * otherwise each character will be > 0x7f and so the
  186. * character will never be escaped on first and last.
  187. */
  188. len = do_esc_char(utfbuf[i], (unsigned char)(flags | orflags), quotes, io_ch, arg);
  189. if(len < 0) return -1;
  190. outlen += len;
  191. }
  192. } else {
  193. len = do_esc_char(c, (unsigned char)(flags | orflags), quotes, io_ch, arg);
  194. if(len < 0) return -1;
  195. outlen += len;
  196. }
  197. }
  198. return outlen;
  199. }
  200. /* This function hex dumps a buffer of characters */
  201. static int do_hex_dump(char_io *io_ch, void *arg, unsigned char *buf, int buflen)
  202. {
  203. static const char hexdig[] = "0123456789ABCDEF";
  204. unsigned char *p, *q;
  205. char hextmp[2];
  206. if(arg) {
  207. p = buf;
  208. q = buf + buflen;
  209. while(p != q) {
  210. hextmp[0] = hexdig[*p >> 4];
  211. hextmp[1] = hexdig[*p & 0xf];
  212. if(!io_ch(arg, hextmp, 2)) return -1;
  213. p++;
  214. }
  215. }
  216. return buflen << 1;
  217. }
  218. /* "dump" a string. This is done when the type is unknown,
  219. * or the flags request it. We can either dump the content
  220. * octets or the entire DER encoding. This uses the RFC2253
  221. * #01234 format.
  222. */
  223. static int do_dump(unsigned long lflags, char_io *io_ch, void *arg, ASN1_STRING *str)
  224. {
  225. /* Placing the ASN1_STRING in a temp ASN1_TYPE allows
  226. * the DER encoding to readily obtained
  227. */
  228. ASN1_TYPE t;
  229. unsigned char *der_buf, *p;
  230. int outlen, der_len;
  231. if(!io_ch(arg, "#", 1)) return -1;
  232. /* If we don't dump DER encoding just dump content octets */
  233. if(!(lflags & ASN1_STRFLGS_DUMP_DER)) {
  234. outlen = do_hex_dump(io_ch, arg, str->data, str->length);
  235. if(outlen < 0) return -1;
  236. return outlen + 1;
  237. }
  238. t.type = str->type;
  239. t.value.ptr = (char *)str;
  240. der_len = i2d_ASN1_TYPE(&t, NULL);
  241. der_buf = OPENSSL_malloc(der_len);
  242. if(!der_buf) return -1;
  243. p = der_buf;
  244. i2d_ASN1_TYPE(&t, &p);
  245. outlen = do_hex_dump(io_ch, arg, der_buf, der_len);
  246. OPENSSL_free(der_buf);
  247. if(outlen < 0) return -1;
  248. return outlen + 1;
  249. }
  250. /* Lookup table to convert tags to character widths,
  251. * 0 = UTF8 encoded, -1 is used for non string types
  252. * otherwise it is the number of bytes per character
  253. */
  254. static const signed char tag2nbyte[] = {
  255. -1, -1, -1, -1, -1, /* 0-4 */
  256. -1, -1, -1, -1, -1, /* 5-9 */
  257. -1, -1, 0, -1, /* 10-13 */
  258. -1, -1, -1, -1, /* 15-17 */
  259. -1, 1, 1, /* 18-20 */
  260. -1, 1, 1, 1, /* 21-24 */
  261. -1, 1, -1, /* 25-27 */
  262. 4, -1, 2 /* 28-30 */
  263. };
  264. /* This is the main function, print out an
  265. * ASN1_STRING taking note of various escape
  266. * and display options. Returns number of
  267. * characters written or -1 if an error
  268. * occurred.
  269. */
  270. static int do_print_ex(char_io *io_ch, void *arg, unsigned long lflags, ASN1_STRING *str)
  271. {
  272. int outlen, len;
  273. int type;
  274. char quotes;
  275. unsigned char flags;
  276. quotes = 0;
  277. /* Keep a copy of escape flags */
  278. flags = (unsigned char)(lflags & ESC_FLAGS);
  279. type = str->type;
  280. outlen = 0;
  281. if(lflags & ASN1_STRFLGS_SHOW_TYPE) {
  282. const char *tagname;
  283. tagname = ASN1_tag2str(type);
  284. outlen += strlen(tagname);
  285. if(!io_ch(arg, tagname, outlen) || !io_ch(arg, ":", 1)) return -1;
  286. outlen++;
  287. }
  288. /* Decide what to do with type, either dump content or display it */
  289. /* Dump everything */
  290. if(lflags & ASN1_STRFLGS_DUMP_ALL) type = -1;
  291. /* Ignore the string type */
  292. else if(lflags & ASN1_STRFLGS_IGNORE_TYPE) type = 1;
  293. else {
  294. /* Else determine width based on type */
  295. if((type > 0) && (type < 31)) type = tag2nbyte[type];
  296. else type = -1;
  297. if((type == -1) && !(lflags & ASN1_STRFLGS_DUMP_UNKNOWN)) type = 1;
  298. }
  299. if(type == -1) {
  300. len = do_dump(lflags, io_ch, arg, str);
  301. if(len < 0) return -1;
  302. outlen += len;
  303. return outlen;
  304. }
  305. if(lflags & ASN1_STRFLGS_UTF8_CONVERT) {
  306. /* Note: if string is UTF8 and we want
  307. * to convert to UTF8 then we just interpret
  308. * it as 1 byte per character to avoid converting
  309. * twice.
  310. */
  311. if(!type) type = 1;
  312. else type |= BUF_TYPE_CONVUTF8;
  313. }
  314. len = do_buf(str->data, str->length, type, flags, &quotes, io_ch, NULL);
  315. if(len < 0) return -1;
  316. outlen += len;
  317. if(quotes) outlen += 2;
  318. if(!arg) return outlen;
  319. if(quotes && !io_ch(arg, "\"", 1)) return -1;
  320. if(do_buf(str->data, str->length, type, flags, NULL, io_ch, arg) < 0)
  321. return -1;
  322. if(quotes && !io_ch(arg, "\"", 1)) return -1;
  323. return outlen;
  324. }
  325. /* Used for line indenting: print 'indent' spaces */
  326. static int do_indent(char_io *io_ch, void *arg, int indent)
  327. {
  328. int i;
  329. for(i = 0; i < indent; i++)
  330. if(!io_ch(arg, " ", 1)) return 0;
  331. return 1;
  332. }
  333. #define FN_WIDTH_LN 25
  334. #define FN_WIDTH_SN 10
  335. static int do_name_ex(char_io *io_ch, void *arg, X509_NAME *n,
  336. int indent, unsigned long flags)
  337. {
  338. int i, prev = -1, orflags, cnt;
  339. int fn_opt, fn_nid;
  340. ASN1_OBJECT *fn;
  341. ASN1_STRING *val;
  342. X509_NAME_ENTRY *ent;
  343. char objtmp[80];
  344. const char *objbuf;
  345. int outlen, len;
  346. const char *sep_dn, *sep_mv, *sep_eq;
  347. int sep_dn_len, sep_mv_len, sep_eq_len;
  348. if(indent < 0) indent = 0;
  349. outlen = indent;
  350. if(!do_indent(io_ch, arg, indent)) return -1;
  351. switch (flags & XN_FLAG_SEP_MASK)
  352. {
  353. case XN_FLAG_SEP_MULTILINE:
  354. sep_dn = "\n";
  355. sep_dn_len = 1;
  356. sep_mv = " + ";
  357. sep_mv_len = 3;
  358. break;
  359. case XN_FLAG_SEP_COMMA_PLUS:
  360. sep_dn = ",";
  361. sep_dn_len = 1;
  362. sep_mv = "+";
  363. sep_mv_len = 1;
  364. indent = 0;
  365. break;
  366. case XN_FLAG_SEP_CPLUS_SPC:
  367. sep_dn = ", ";
  368. sep_dn_len = 2;
  369. sep_mv = " + ";
  370. sep_mv_len = 3;
  371. indent = 0;
  372. break;
  373. case XN_FLAG_SEP_SPLUS_SPC:
  374. sep_dn = "; ";
  375. sep_dn_len = 2;
  376. sep_mv = " + ";
  377. sep_mv_len = 3;
  378. indent = 0;
  379. break;
  380. default:
  381. return -1;
  382. }
  383. if(flags & XN_FLAG_SPC_EQ) {
  384. sep_eq = " = ";
  385. sep_eq_len = 3;
  386. } else {
  387. sep_eq = "=";
  388. sep_eq_len = 1;
  389. }
  390. fn_opt = flags & XN_FLAG_FN_MASK;
  391. cnt = X509_NAME_entry_count(n);
  392. for(i = 0; i < cnt; i++) {
  393. if(flags & XN_FLAG_DN_REV)
  394. ent = X509_NAME_get_entry(n, cnt - i - 1);
  395. else ent = X509_NAME_get_entry(n, i);
  396. if(prev != -1) {
  397. if(prev == ent->set) {
  398. if(!io_ch(arg, sep_mv, sep_mv_len)) return -1;
  399. outlen += sep_mv_len;
  400. } else {
  401. if(!io_ch(arg, sep_dn, sep_dn_len)) return -1;
  402. outlen += sep_dn_len;
  403. if(!do_indent(io_ch, arg, indent)) return -1;
  404. outlen += indent;
  405. }
  406. }
  407. prev = ent->set;
  408. fn = X509_NAME_ENTRY_get_object(ent);
  409. val = X509_NAME_ENTRY_get_data(ent);
  410. fn_nid = OBJ_obj2nid(fn);
  411. if(fn_opt != XN_FLAG_FN_NONE) {
  412. int objlen, fld_len;
  413. if((fn_opt == XN_FLAG_FN_OID) || (fn_nid==NID_undef) ) {
  414. OBJ_obj2txt(objtmp, sizeof objtmp, fn, 1);
  415. fld_len = 0; /* XXX: what should this be? */
  416. objbuf = objtmp;
  417. } else {
  418. if(fn_opt == XN_FLAG_FN_SN) {
  419. fld_len = FN_WIDTH_SN;
  420. objbuf = OBJ_nid2sn(fn_nid);
  421. } else if(fn_opt == XN_FLAG_FN_LN) {
  422. fld_len = FN_WIDTH_LN;
  423. objbuf = OBJ_nid2ln(fn_nid);
  424. } else {
  425. fld_len = 0; /* XXX: what should this be? */
  426. objbuf = "";
  427. }
  428. }
  429. objlen = strlen(objbuf);
  430. if(!io_ch(arg, objbuf, objlen)) return -1;
  431. if ((objlen < fld_len) && (flags & XN_FLAG_FN_ALIGN)) {
  432. if (!do_indent(io_ch, arg, fld_len - objlen)) return -1;
  433. outlen += fld_len - objlen;
  434. }
  435. if(!io_ch(arg, sep_eq, sep_eq_len)) return -1;
  436. outlen += objlen + sep_eq_len;
  437. }
  438. /* If the field name is unknown then fix up the DER dump
  439. * flag. We might want to limit this further so it will
  440. * DER dump on anything other than a few 'standard' fields.
  441. */
  442. if((fn_nid == NID_undef) && (flags & XN_FLAG_DUMP_UNKNOWN_FIELDS))
  443. orflags = ASN1_STRFLGS_DUMP_ALL;
  444. else orflags = 0;
  445. len = do_print_ex(io_ch, arg, flags | orflags, val);
  446. if(len < 0) return -1;
  447. outlen += len;
  448. }
  449. return outlen;
  450. }
  451. /* Wrappers round the main functions */
  452. int X509_NAME_print_ex(BIO *out, X509_NAME *nm, int indent, unsigned long flags)
  453. {
  454. if(flags == XN_FLAG_COMPAT)
  455. return X509_NAME_print(out, nm, indent);
  456. return do_name_ex(send_bio_chars, out, nm, indent, flags);
  457. }
  458. #ifndef OPENSSL_NO_FP_API
  459. int X509_NAME_print_ex_fp(FILE *fp, X509_NAME *nm, int indent, unsigned long flags)
  460. {
  461. if(flags == XN_FLAG_COMPAT)
  462. {
  463. BIO *btmp;
  464. int ret;
  465. btmp = BIO_new_fp(fp, BIO_NOCLOSE);
  466. if(!btmp) return -1;
  467. ret = X509_NAME_print(btmp, nm, indent);
  468. BIO_free(btmp);
  469. return ret;
  470. }
  471. return do_name_ex(send_fp_chars, fp, nm, indent, flags);
  472. }
  473. #endif
  474. int ASN1_STRING_print_ex(BIO *out, ASN1_STRING *str, unsigned long flags)
  475. {
  476. return do_print_ex(send_bio_chars, out, flags, str);
  477. }
  478. #ifndef OPENSSL_NO_FP_API
  479. int ASN1_STRING_print_ex_fp(FILE *fp, ASN1_STRING *str, unsigned long flags)
  480. {
  481. return do_print_ex(send_fp_chars, fp, flags, str);
  482. }
  483. #endif
  484. /* Utility function: convert any string type to UTF8, returns number of bytes
  485. * in output string or a negative error code
  486. */
  487. int ASN1_STRING_to_UTF8(unsigned char **out, ASN1_STRING *in)
  488. {
  489. ASN1_STRING stmp, *str = &stmp;
  490. int mbflag, type, ret;
  491. if(!in) return -1;
  492. type = in->type;
  493. if((type < 0) || (type > 30)) return -1;
  494. mbflag = tag2nbyte[type];
  495. if(mbflag == -1) return -1;
  496. mbflag |= MBSTRING_FLAG;
  497. stmp.data = NULL;
  498. stmp.length = 0;
  499. stmp.flags = 0;
  500. ret = ASN1_mbstring_copy(&str, in->data, in->length, mbflag, B_ASN1_UTF8STRING);
  501. if(ret < 0) return ret;
  502. *out = stmp.data;
  503. return stmp.length;
  504. }