a_strex.c 15 KB

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