2
0

tasn_enc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. /*
  2. * Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stddef.h>
  10. #include <string.h>
  11. #include "internal/cryptlib.h"
  12. #include <openssl/asn1.h>
  13. #include <openssl/asn1t.h>
  14. #include <openssl/objects.h>
  15. #include "crypto/asn1.h"
  16. #include "asn1_local.h"
  17. static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
  18. const ASN1_ITEM *it, int tag, int aclass);
  19. static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
  20. int skcontlen, const ASN1_ITEM *item,
  21. int do_sort, int iclass);
  22. static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
  23. const ASN1_TEMPLATE *tt, int tag, int aclass);
  24. static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
  25. const ASN1_ITEM *it, int flags);
  26. static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
  27. const ASN1_ITEM *it);
  28. /*
  29. * Top level i2d equivalents: the 'ndef' variant instructs the encoder to use
  30. * indefinite length constructed encoding, where appropriate
  31. */
  32. int ASN1_item_ndef_i2d(ASN1_VALUE *val, unsigned char **out,
  33. const ASN1_ITEM *it)
  34. {
  35. return asn1_item_flags_i2d(val, out, it, ASN1_TFLG_NDEF);
  36. }
  37. int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it)
  38. {
  39. return asn1_item_flags_i2d(val, out, it, 0);
  40. }
  41. /*
  42. * Encode an ASN1 item, this is use by the standard 'i2d' function. 'out'
  43. * points to a buffer to output the data to. The new i2d has one additional
  44. * feature. If the output buffer is NULL (i.e. *out == NULL) then a buffer is
  45. * allocated and populated with the encoding.
  46. */
  47. static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
  48. const ASN1_ITEM *it, int flags)
  49. {
  50. if (out && !*out) {
  51. unsigned char *p, *buf;
  52. int len;
  53. len = ASN1_item_ex_i2d(&val, NULL, it, -1, flags);
  54. if (len <= 0)
  55. return len;
  56. if ((buf = OPENSSL_malloc(len)) == NULL) {
  57. ASN1err(ASN1_F_ASN1_ITEM_FLAGS_I2D, ERR_R_MALLOC_FAILURE);
  58. return -1;
  59. }
  60. p = buf;
  61. ASN1_item_ex_i2d(&val, &p, it, -1, flags);
  62. *out = buf;
  63. return len;
  64. }
  65. return ASN1_item_ex_i2d(&val, out, it, -1, flags);
  66. }
  67. /*
  68. * Encode an item, taking care of IMPLICIT tagging (if any). This function
  69. * performs the normal item handling: it can be used in external types.
  70. */
  71. int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
  72. const ASN1_ITEM *it, int tag, int aclass)
  73. {
  74. const ASN1_TEMPLATE *tt = NULL;
  75. int i, seqcontlen, seqlen, ndef = 1;
  76. const ASN1_EXTERN_FUNCS *ef;
  77. const ASN1_AUX *aux = it->funcs;
  78. ASN1_aux_cb *asn1_cb = 0;
  79. if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
  80. return 0;
  81. if (aux && aux->asn1_cb)
  82. asn1_cb = aux->asn1_cb;
  83. switch (it->itype) {
  84. case ASN1_ITYPE_PRIMITIVE:
  85. if (it->templates)
  86. return asn1_template_ex_i2d(pval, out, it->templates,
  87. tag, aclass);
  88. return asn1_i2d_ex_primitive(pval, out, it, tag, aclass);
  89. case ASN1_ITYPE_MSTRING:
  90. /*
  91. * It never makes sense for multi-strings to have implicit tagging, so
  92. * if tag != -1, then this looks like an error in the template.
  93. */
  94. if (tag != -1) {
  95. ASN1err(ASN1_F_ASN1_ITEM_EX_I2D, ASN1_R_BAD_TEMPLATE);
  96. return -1;
  97. }
  98. return asn1_i2d_ex_primitive(pval, out, it, -1, aclass);
  99. case ASN1_ITYPE_CHOICE:
  100. /*
  101. * It never makes sense for CHOICE types to have implicit tagging, so
  102. * if tag != -1, then this looks like an error in the template.
  103. */
  104. if (tag != -1) {
  105. ASN1err(ASN1_F_ASN1_ITEM_EX_I2D, ASN1_R_BAD_TEMPLATE);
  106. return -1;
  107. }
  108. if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
  109. return 0;
  110. i = asn1_get_choice_selector(pval, it);
  111. if ((i >= 0) && (i < it->tcount)) {
  112. ASN1_VALUE **pchval;
  113. const ASN1_TEMPLATE *chtt;
  114. chtt = it->templates + i;
  115. pchval = asn1_get_field_ptr(pval, chtt);
  116. return asn1_template_ex_i2d(pchval, out, chtt, -1, aclass);
  117. }
  118. /* Fixme: error condition if selector out of range */
  119. if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
  120. return 0;
  121. break;
  122. case ASN1_ITYPE_EXTERN:
  123. /* If new style i2d it does all the work */
  124. ef = it->funcs;
  125. return ef->asn1_ex_i2d(pval, out, it, tag, aclass);
  126. case ASN1_ITYPE_NDEF_SEQUENCE:
  127. /* Use indefinite length constructed if requested */
  128. if (aclass & ASN1_TFLG_NDEF)
  129. ndef = 2;
  130. /* fall through */
  131. case ASN1_ITYPE_SEQUENCE:
  132. i = asn1_enc_restore(&seqcontlen, out, pval, it);
  133. /* An error occurred */
  134. if (i < 0)
  135. return 0;
  136. /* We have a valid cached encoding... */
  137. if (i > 0)
  138. return seqcontlen;
  139. /* Otherwise carry on */
  140. seqcontlen = 0;
  141. /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
  142. if (tag == -1) {
  143. tag = V_ASN1_SEQUENCE;
  144. /* Retain any other flags in aclass */
  145. aclass = (aclass & ~ASN1_TFLG_TAG_CLASS)
  146. | V_ASN1_UNIVERSAL;
  147. }
  148. if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
  149. return 0;
  150. /* First work out sequence content length */
  151. for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
  152. const ASN1_TEMPLATE *seqtt;
  153. ASN1_VALUE **pseqval;
  154. int tmplen;
  155. seqtt = asn1_do_adb(pval, tt, 1);
  156. if (!seqtt)
  157. return 0;
  158. pseqval = asn1_get_field_ptr(pval, seqtt);
  159. tmplen = asn1_template_ex_i2d(pseqval, NULL, seqtt, -1, aclass);
  160. if (tmplen == -1 || (tmplen > INT_MAX - seqcontlen))
  161. return -1;
  162. seqcontlen += tmplen;
  163. }
  164. seqlen = ASN1_object_size(ndef, seqcontlen, tag);
  165. if (!out || seqlen == -1)
  166. return seqlen;
  167. /* Output SEQUENCE header */
  168. ASN1_put_object(out, ndef, seqcontlen, tag, aclass);
  169. for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
  170. const ASN1_TEMPLATE *seqtt;
  171. ASN1_VALUE **pseqval;
  172. seqtt = asn1_do_adb(pval, tt, 1);
  173. if (!seqtt)
  174. return 0;
  175. pseqval = asn1_get_field_ptr(pval, seqtt);
  176. /* FIXME: check for errors in enhanced version */
  177. asn1_template_ex_i2d(pseqval, out, seqtt, -1, aclass);
  178. }
  179. if (ndef == 2)
  180. ASN1_put_eoc(out);
  181. if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
  182. return 0;
  183. return seqlen;
  184. default:
  185. return 0;
  186. }
  187. return 0;
  188. }
  189. static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
  190. const ASN1_TEMPLATE *tt, int tag, int iclass)
  191. {
  192. int i, ret, flags, ttag, tclass, ndef;
  193. ASN1_VALUE *tval;
  194. flags = tt->flags;
  195. /*
  196. * If field is embedded then val needs fixing so it is a pointer to
  197. * a pointer to a field.
  198. */
  199. if (flags & ASN1_TFLG_EMBED) {
  200. tval = (ASN1_VALUE *)pval;
  201. pval = &tval;
  202. }
  203. /*
  204. * Work out tag and class to use: tagging may come either from the
  205. * template or the arguments, not both because this would create
  206. * ambiguity. Additionally the iclass argument may contain some
  207. * additional flags which should be noted and passed down to other
  208. * levels.
  209. */
  210. if (flags & ASN1_TFLG_TAG_MASK) {
  211. /* Error if argument and template tagging */
  212. if (tag != -1)
  213. /* FIXME: error code here */
  214. return -1;
  215. /* Get tagging from template */
  216. ttag = tt->tag;
  217. tclass = flags & ASN1_TFLG_TAG_CLASS;
  218. } else if (tag != -1) {
  219. /* No template tagging, get from arguments */
  220. ttag = tag;
  221. tclass = iclass & ASN1_TFLG_TAG_CLASS;
  222. } else {
  223. ttag = -1;
  224. tclass = 0;
  225. }
  226. /*
  227. * Remove any class mask from iflag.
  228. */
  229. iclass &= ~ASN1_TFLG_TAG_CLASS;
  230. /*
  231. * At this point 'ttag' contains the outer tag to use, 'tclass' is the
  232. * class and iclass is any flags passed to this function.
  233. */
  234. /* if template and arguments require ndef, use it */
  235. if ((flags & ASN1_TFLG_NDEF) && (iclass & ASN1_TFLG_NDEF))
  236. ndef = 2;
  237. else
  238. ndef = 1;
  239. if (flags & ASN1_TFLG_SK_MASK) {
  240. /* SET OF, SEQUENCE OF */
  241. STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
  242. int isset, sktag, skaclass;
  243. int skcontlen, sklen;
  244. ASN1_VALUE *skitem;
  245. if (!*pval)
  246. return 0;
  247. if (flags & ASN1_TFLG_SET_OF) {
  248. isset = 1;
  249. /* 2 means we reorder */
  250. if (flags & ASN1_TFLG_SEQUENCE_OF)
  251. isset = 2;
  252. } else
  253. isset = 0;
  254. /*
  255. * Work out inner tag value: if EXPLICIT or no tagging use underlying
  256. * type.
  257. */
  258. if ((ttag != -1) && !(flags & ASN1_TFLG_EXPTAG)) {
  259. sktag = ttag;
  260. skaclass = tclass;
  261. } else {
  262. skaclass = V_ASN1_UNIVERSAL;
  263. if (isset)
  264. sktag = V_ASN1_SET;
  265. else
  266. sktag = V_ASN1_SEQUENCE;
  267. }
  268. /* Determine total length of items */
  269. skcontlen = 0;
  270. for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
  271. int tmplen;
  272. skitem = sk_ASN1_VALUE_value(sk, i);
  273. tmplen = ASN1_item_ex_i2d(&skitem, NULL, ASN1_ITEM_ptr(tt->item),
  274. -1, iclass);
  275. if (tmplen == -1 || (skcontlen > INT_MAX - tmplen))
  276. return -1;
  277. skcontlen += tmplen;
  278. }
  279. sklen = ASN1_object_size(ndef, skcontlen, sktag);
  280. if (sklen == -1)
  281. return -1;
  282. /* If EXPLICIT need length of surrounding tag */
  283. if (flags & ASN1_TFLG_EXPTAG)
  284. ret = ASN1_object_size(ndef, sklen, ttag);
  285. else
  286. ret = sklen;
  287. if (!out || ret == -1)
  288. return ret;
  289. /* Now encode this lot... */
  290. /* EXPLICIT tag */
  291. if (flags & ASN1_TFLG_EXPTAG)
  292. ASN1_put_object(out, ndef, sklen, ttag, tclass);
  293. /* SET or SEQUENCE and IMPLICIT tag */
  294. ASN1_put_object(out, ndef, skcontlen, sktag, skaclass);
  295. /* And the stuff itself */
  296. asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item),
  297. isset, iclass);
  298. if (ndef == 2) {
  299. ASN1_put_eoc(out);
  300. if (flags & ASN1_TFLG_EXPTAG)
  301. ASN1_put_eoc(out);
  302. }
  303. return ret;
  304. }
  305. if (flags & ASN1_TFLG_EXPTAG) {
  306. /* EXPLICIT tagging */
  307. /* Find length of tagged item */
  308. i = ASN1_item_ex_i2d(pval, NULL, ASN1_ITEM_ptr(tt->item), -1, iclass);
  309. if (!i)
  310. return 0;
  311. /* Find length of EXPLICIT tag */
  312. ret = ASN1_object_size(ndef, i, ttag);
  313. if (out && ret != -1) {
  314. /* Output tag and item */
  315. ASN1_put_object(out, ndef, i, ttag, tclass);
  316. ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), -1, iclass);
  317. if (ndef == 2)
  318. ASN1_put_eoc(out);
  319. }
  320. return ret;
  321. }
  322. /* Either normal or IMPLICIT tagging: combine class and flags */
  323. return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item),
  324. ttag, tclass | iclass);
  325. }
  326. /* Temporary structure used to hold DER encoding of items for SET OF */
  327. typedef struct {
  328. unsigned char *data;
  329. int length;
  330. ASN1_VALUE *field;
  331. } DER_ENC;
  332. static int der_cmp(const void *a, const void *b)
  333. {
  334. const DER_ENC *d1 = a, *d2 = b;
  335. int cmplen, i;
  336. cmplen = (d1->length < d2->length) ? d1->length : d2->length;
  337. i = memcmp(d1->data, d2->data, cmplen);
  338. if (i)
  339. return i;
  340. return d1->length - d2->length;
  341. }
  342. /* Output the content octets of SET OF or SEQUENCE OF */
  343. static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
  344. int skcontlen, const ASN1_ITEM *item,
  345. int do_sort, int iclass)
  346. {
  347. int i;
  348. ASN1_VALUE *skitem;
  349. unsigned char *tmpdat = NULL, *p = NULL;
  350. DER_ENC *derlst = NULL, *tder;
  351. if (do_sort) {
  352. /* Don't need to sort less than 2 items */
  353. if (sk_ASN1_VALUE_num(sk) < 2)
  354. do_sort = 0;
  355. else {
  356. derlst = OPENSSL_malloc(sk_ASN1_VALUE_num(sk)
  357. * sizeof(*derlst));
  358. if (derlst == NULL)
  359. return 0;
  360. tmpdat = OPENSSL_malloc(skcontlen);
  361. if (tmpdat == NULL) {
  362. OPENSSL_free(derlst);
  363. return 0;
  364. }
  365. }
  366. }
  367. /* If not sorting just output each item */
  368. if (!do_sort) {
  369. for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
  370. skitem = sk_ASN1_VALUE_value(sk, i);
  371. ASN1_item_ex_i2d(&skitem, out, item, -1, iclass);
  372. }
  373. return 1;
  374. }
  375. p = tmpdat;
  376. /* Doing sort: build up a list of each member's DER encoding */
  377. for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) {
  378. skitem = sk_ASN1_VALUE_value(sk, i);
  379. tder->data = p;
  380. tder->length = ASN1_item_ex_i2d(&skitem, &p, item, -1, iclass);
  381. tder->field = skitem;
  382. }
  383. /* Now sort them */
  384. qsort(derlst, sk_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp);
  385. /* Output sorted DER encoding */
  386. p = *out;
  387. for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) {
  388. memcpy(p, tder->data, tder->length);
  389. p += tder->length;
  390. }
  391. *out = p;
  392. /* If do_sort is 2 then reorder the STACK */
  393. if (do_sort == 2) {
  394. for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++)
  395. (void)sk_ASN1_VALUE_set(sk, i, tder->field);
  396. }
  397. OPENSSL_free(derlst);
  398. OPENSSL_free(tmpdat);
  399. return 1;
  400. }
  401. static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
  402. const ASN1_ITEM *it, int tag, int aclass)
  403. {
  404. int len;
  405. int utype;
  406. int usetag;
  407. int ndef = 0;
  408. utype = it->utype;
  409. /*
  410. * Get length of content octets and maybe find out the underlying type.
  411. */
  412. len = asn1_ex_i2c(pval, NULL, &utype, it);
  413. /*
  414. * If SEQUENCE, SET or OTHER then header is included in pseudo content
  415. * octets so don't include tag+length. We need to check here because the
  416. * call to asn1_ex_i2c() could change utype.
  417. */
  418. if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) ||
  419. (utype == V_ASN1_OTHER))
  420. usetag = 0;
  421. else
  422. usetag = 1;
  423. /* -1 means omit type */
  424. if (len == -1)
  425. return 0;
  426. /* -2 return is special meaning use ndef */
  427. if (len == -2) {
  428. ndef = 2;
  429. len = 0;
  430. }
  431. /* If not implicitly tagged get tag from underlying type */
  432. if (tag == -1)
  433. tag = utype;
  434. /* Output tag+length followed by content octets */
  435. if (out) {
  436. if (usetag)
  437. ASN1_put_object(out, ndef, len, tag, aclass);
  438. asn1_ex_i2c(pval, *out, &utype, it);
  439. if (ndef)
  440. ASN1_put_eoc(out);
  441. else
  442. *out += len;
  443. }
  444. if (usetag)
  445. return ASN1_object_size(ndef, len, tag);
  446. return len;
  447. }
  448. /* Produce content octets from a structure */
  449. static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
  450. const ASN1_ITEM *it)
  451. {
  452. ASN1_BOOLEAN *tbool = NULL;
  453. ASN1_STRING *strtmp;
  454. ASN1_OBJECT *otmp;
  455. int utype;
  456. const unsigned char *cont;
  457. unsigned char c;
  458. int len;
  459. const ASN1_PRIMITIVE_FUNCS *pf;
  460. pf = it->funcs;
  461. if (pf && pf->prim_i2c)
  462. return pf->prim_i2c(pval, cout, putype, it);
  463. /* Should type be omitted? */
  464. if ((it->itype != ASN1_ITYPE_PRIMITIVE)
  465. || (it->utype != V_ASN1_BOOLEAN)) {
  466. if (!*pval)
  467. return -1;
  468. }
  469. if (it->itype == ASN1_ITYPE_MSTRING) {
  470. /* If MSTRING type set the underlying type */
  471. strtmp = (ASN1_STRING *)*pval;
  472. utype = strtmp->type;
  473. *putype = utype;
  474. } else if (it->utype == V_ASN1_ANY) {
  475. /* If ANY set type and pointer to value */
  476. ASN1_TYPE *typ;
  477. typ = (ASN1_TYPE *)*pval;
  478. utype = typ->type;
  479. *putype = utype;
  480. pval = &typ->value.asn1_value;
  481. } else
  482. utype = *putype;
  483. switch (utype) {
  484. case V_ASN1_OBJECT:
  485. otmp = (ASN1_OBJECT *)*pval;
  486. cont = otmp->data;
  487. len = otmp->length;
  488. if (cont == NULL || len == 0)
  489. return -1;
  490. break;
  491. case V_ASN1_NULL:
  492. cont = NULL;
  493. len = 0;
  494. break;
  495. case V_ASN1_BOOLEAN:
  496. tbool = (ASN1_BOOLEAN *)pval;
  497. if (*tbool == -1)
  498. return -1;
  499. if (it->utype != V_ASN1_ANY) {
  500. /*
  501. * Default handling if value == size field then omit
  502. */
  503. if (*tbool && (it->size > 0))
  504. return -1;
  505. if (!*tbool && !it->size)
  506. return -1;
  507. }
  508. c = (unsigned char)*tbool;
  509. cont = &c;
  510. len = 1;
  511. break;
  512. case V_ASN1_BIT_STRING:
  513. return i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval,
  514. cout ? &cout : NULL);
  515. case V_ASN1_INTEGER:
  516. case V_ASN1_ENUMERATED:
  517. /*
  518. * These are all have the same content format as ASN1_INTEGER
  519. */
  520. return i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval, cout ? &cout : NULL);
  521. case V_ASN1_OCTET_STRING:
  522. case V_ASN1_NUMERICSTRING:
  523. case V_ASN1_PRINTABLESTRING:
  524. case V_ASN1_T61STRING:
  525. case V_ASN1_VIDEOTEXSTRING:
  526. case V_ASN1_IA5STRING:
  527. case V_ASN1_UTCTIME:
  528. case V_ASN1_GENERALIZEDTIME:
  529. case V_ASN1_GRAPHICSTRING:
  530. case V_ASN1_VISIBLESTRING:
  531. case V_ASN1_GENERALSTRING:
  532. case V_ASN1_UNIVERSALSTRING:
  533. case V_ASN1_BMPSTRING:
  534. case V_ASN1_UTF8STRING:
  535. case V_ASN1_SEQUENCE:
  536. case V_ASN1_SET:
  537. default:
  538. /* All based on ASN1_STRING and handled the same */
  539. strtmp = (ASN1_STRING *)*pval;
  540. /* Special handling for NDEF */
  541. if ((it->size == ASN1_TFLG_NDEF)
  542. && (strtmp->flags & ASN1_STRING_FLAG_NDEF)) {
  543. if (cout) {
  544. strtmp->data = cout;
  545. strtmp->length = 0;
  546. }
  547. /* Special return code */
  548. return -2;
  549. }
  550. cont = strtmp->data;
  551. len = strtmp->length;
  552. break;
  553. }
  554. if (cout && len)
  555. memcpy(cout, cont, len);
  556. return len;
  557. }