25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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