a_strex.c 19 KB

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