tasn_enc.c 20 KB

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