您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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