tasn_enc.c 20 KB

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