A local copy of OpenSSL from GitHub
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.

589 lines
16 KiB

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