2
0

tasn_enc.c 19 KB

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