a_strex.c 18 KB

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