a_strex.c 14 KB

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