a_strex.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /* a_strex.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  4. * 2000.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2000 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. * licensing@OpenSSL.org.
  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. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  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;
  183. unsigned char orflags, *p, *q;
  184. unsigned long c;
  185. p = buf;
  186. q = buf + buflen;
  187. outlen = 0;
  188. while (p != q) {
  189. if (p == buf && flags & ASN1_STRFLGS_ESC_2253)
  190. orflags = CHARTYPE_FIRST_ESC_2253;
  191. else
  192. orflags = 0;
  193. switch (type & BUF_TYPE_WIDTH_MASK) {
  194. case 4:
  195. c = ((unsigned long)*p++) << 24;
  196. c |= ((unsigned long)*p++) << 16;
  197. c |= ((unsigned long)*p++) << 8;
  198. c |= *p++;
  199. break;
  200. case 2:
  201. c = ((unsigned long)*p++) << 8;
  202. c |= *p++;
  203. break;
  204. case 1:
  205. c = *p++;
  206. break;
  207. case 0:
  208. i = UTF8_getc(p, buflen, &c);
  209. if (i < 0)
  210. return -1; /* Invalid UTF8String */
  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. 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. }