tasn_enc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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, len;
  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. skitem = sk_const_ASN1_VALUE_value(sk, i);
  275. len = ASN1_item_ex_i2d(&skitem, NULL, ASN1_ITEM_ptr(tt->item),
  276. -1, iclass);
  277. if (len == -1 || (skcontlen > INT_MAX - len))
  278. return -1;
  279. if (len == 0 && (tt->flags & ASN1_TFLG_OPTIONAL) == 0) {
  280. ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_ZERO_CONTENT);
  281. return -1;
  282. }
  283. skcontlen += len;
  284. }
  285. sklen = ASN1_object_size(ndef, skcontlen, sktag);
  286. if (sklen == -1)
  287. return -1;
  288. /* If EXPLICIT need length of surrounding tag */
  289. if (flags & ASN1_TFLG_EXPTAG)
  290. ret = ASN1_object_size(ndef, sklen, ttag);
  291. else
  292. ret = sklen;
  293. if (!out || ret == -1)
  294. return ret;
  295. /* Now encode this lot... */
  296. /* EXPLICIT tag */
  297. if (flags & ASN1_TFLG_EXPTAG)
  298. ASN1_put_object(out, ndef, sklen, ttag, tclass);
  299. /* SET or SEQUENCE and IMPLICIT tag */
  300. ASN1_put_object(out, ndef, skcontlen, sktag, skaclass);
  301. /* And the stuff itself */
  302. asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item),
  303. isset, iclass);
  304. if (ndef == 2) {
  305. ASN1_put_eoc(out);
  306. if (flags & ASN1_TFLG_EXPTAG)
  307. ASN1_put_eoc(out);
  308. }
  309. return ret;
  310. }
  311. if (flags & ASN1_TFLG_EXPTAG) {
  312. /* EXPLICIT tagging */
  313. /* Find length of tagged item */
  314. i = ASN1_item_ex_i2d(pval, NULL, ASN1_ITEM_ptr(tt->item), -1, iclass);
  315. if (i == 0) {
  316. if ((tt->flags & ASN1_TFLG_OPTIONAL) == 0) {
  317. ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_ZERO_CONTENT);
  318. return -1;
  319. }
  320. return 0;
  321. }
  322. /* Find length of EXPLICIT tag */
  323. ret = ASN1_object_size(ndef, i, ttag);
  324. if (out && ret != -1) {
  325. /* Output tag and item */
  326. ASN1_put_object(out, ndef, i, ttag, tclass);
  327. ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), -1, iclass);
  328. if (ndef == 2)
  329. ASN1_put_eoc(out);
  330. }
  331. return ret;
  332. }
  333. /* Either normal or IMPLICIT tagging: combine class and flags */
  334. len = ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item),
  335. ttag, tclass | iclass);
  336. if (len == 0 && (tt->flags & ASN1_TFLG_OPTIONAL) == 0) {
  337. ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_ZERO_CONTENT);
  338. return -1;
  339. }
  340. return len;
  341. }
  342. /* Temporary structure used to hold DER encoding of items for SET OF */
  343. typedef struct {
  344. unsigned char *data;
  345. int length;
  346. const ASN1_VALUE *field;
  347. } DER_ENC;
  348. static int der_cmp(const void *a, const void *b)
  349. {
  350. const DER_ENC *d1 = a, *d2 = b;
  351. int cmplen, i;
  352. cmplen = (d1->length < d2->length) ? d1->length : d2->length;
  353. i = memcmp(d1->data, d2->data, cmplen);
  354. if (i)
  355. return i;
  356. return d1->length - d2->length;
  357. }
  358. /* Output the content octets of SET OF or SEQUENCE OF */
  359. static int asn1_set_seq_out(STACK_OF(const_ASN1_VALUE) *sk,
  360. unsigned char **out,
  361. int skcontlen, const ASN1_ITEM *item,
  362. int do_sort, int iclass)
  363. {
  364. int i, ret = 0;
  365. const ASN1_VALUE *skitem;
  366. unsigned char *tmpdat = NULL, *p = NULL;
  367. DER_ENC *derlst = NULL, *tder;
  368. if (do_sort) {
  369. /* Don't need to sort less than 2 items */
  370. if (sk_const_ASN1_VALUE_num(sk) < 2)
  371. do_sort = 0;
  372. else {
  373. derlst = OPENSSL_malloc(sk_const_ASN1_VALUE_num(sk)
  374. * sizeof(*derlst));
  375. if (derlst == NULL) {
  376. ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
  377. return 0;
  378. }
  379. tmpdat = OPENSSL_malloc(skcontlen);
  380. if (tmpdat == NULL) {
  381. ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
  382. goto err;
  383. }
  384. }
  385. }
  386. /* If not sorting just output each item */
  387. if (!do_sort) {
  388. for (i = 0; i < sk_const_ASN1_VALUE_num(sk); i++) {
  389. skitem = sk_const_ASN1_VALUE_value(sk, i);
  390. ASN1_item_ex_i2d(&skitem, out, item, -1, iclass);
  391. }
  392. return 1;
  393. }
  394. p = tmpdat;
  395. /* Doing sort: build up a list of each member's DER encoding */
  396. for (i = 0, tder = derlst; i < sk_const_ASN1_VALUE_num(sk); i++, tder++) {
  397. skitem = sk_const_ASN1_VALUE_value(sk, i);
  398. tder->data = p;
  399. tder->length = ASN1_item_ex_i2d(&skitem, &p, item, -1, iclass);
  400. tder->field = skitem;
  401. }
  402. /* Now sort them */
  403. qsort(derlst, sk_const_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp);
  404. /* Output sorted DER encoding */
  405. p = *out;
  406. for (i = 0, tder = derlst; i < sk_const_ASN1_VALUE_num(sk); i++, tder++) {
  407. memcpy(p, tder->data, tder->length);
  408. p += tder->length;
  409. }
  410. *out = p;
  411. /* If do_sort is 2 then reorder the STACK */
  412. if (do_sort == 2) {
  413. for (i = 0, tder = derlst; i < sk_const_ASN1_VALUE_num(sk); i++, tder++)
  414. (void)sk_const_ASN1_VALUE_set(sk, i, tder->field);
  415. }
  416. ret = 1;
  417. err:
  418. OPENSSL_free(derlst);
  419. OPENSSL_free(tmpdat);
  420. return ret;
  421. }
  422. static int asn1_i2d_ex_primitive(const ASN1_VALUE **pval, unsigned char **out,
  423. const ASN1_ITEM *it, int tag, int aclass)
  424. {
  425. int len;
  426. int utype;
  427. int usetag;
  428. int ndef = 0;
  429. utype = it->utype;
  430. /*
  431. * Get length of content octets and maybe find out the underlying type.
  432. */
  433. len = asn1_ex_i2c(pval, NULL, &utype, it);
  434. /*
  435. * If SEQUENCE, SET or OTHER then header is included in pseudo content
  436. * octets so don't include tag+length. We need to check here because the
  437. * call to asn1_ex_i2c() could change utype.
  438. */
  439. if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) ||
  440. (utype == V_ASN1_OTHER))
  441. usetag = 0;
  442. else
  443. usetag = 1;
  444. /* -1 means omit type */
  445. if (len == -1)
  446. return 0;
  447. /* -2 return is special meaning use ndef */
  448. if (len == -2) {
  449. ndef = 2;
  450. len = 0;
  451. }
  452. /* If not implicitly tagged get tag from underlying type */
  453. if (tag == -1)
  454. tag = utype;
  455. /* Output tag+length followed by content octets */
  456. if (out) {
  457. if (usetag)
  458. ASN1_put_object(out, ndef, len, tag, aclass);
  459. asn1_ex_i2c(pval, *out, &utype, it);
  460. if (ndef)
  461. ASN1_put_eoc(out);
  462. else
  463. *out += len;
  464. }
  465. if (usetag)
  466. return ASN1_object_size(ndef, len, tag);
  467. return len;
  468. }
  469. /* Produce content octets from a structure */
  470. static int asn1_ex_i2c(const ASN1_VALUE **pval, unsigned char *cout, int *putype,
  471. const ASN1_ITEM *it)
  472. {
  473. ASN1_BOOLEAN *tbool = NULL;
  474. ASN1_STRING *strtmp;
  475. ASN1_OBJECT *otmp;
  476. int utype;
  477. const unsigned char *cont;
  478. unsigned char c;
  479. int len;
  480. const ASN1_PRIMITIVE_FUNCS *pf;
  481. pf = it->funcs;
  482. if (pf && pf->prim_i2c)
  483. return pf->prim_i2c(pval, cout, putype, it);
  484. /* Should type be omitted? */
  485. if ((it->itype != ASN1_ITYPE_PRIMITIVE)
  486. || (it->utype != V_ASN1_BOOLEAN)) {
  487. if (*pval == NULL)
  488. return -1;
  489. }
  490. if (it->itype == ASN1_ITYPE_MSTRING) {
  491. /* If MSTRING type set the underlying type */
  492. strtmp = (ASN1_STRING *)*pval;
  493. utype = strtmp->type;
  494. *putype = utype;
  495. } else if (it->utype == V_ASN1_ANY) {
  496. /* If ANY set type and pointer to value */
  497. ASN1_TYPE *typ;
  498. typ = (ASN1_TYPE *)*pval;
  499. utype = typ->type;
  500. *putype = utype;
  501. pval = (const ASN1_VALUE **)&typ->value.asn1_value; /* actually is const */
  502. } else
  503. utype = *putype;
  504. switch (utype) {
  505. case V_ASN1_OBJECT:
  506. otmp = (ASN1_OBJECT *)*pval;
  507. cont = otmp->data;
  508. len = otmp->length;
  509. if (cont == NULL || len == 0)
  510. return -1;
  511. break;
  512. case V_ASN1_NULL:
  513. cont = NULL;
  514. len = 0;
  515. break;
  516. case V_ASN1_BOOLEAN:
  517. tbool = (ASN1_BOOLEAN *)pval;
  518. if (*tbool == -1)
  519. return -1;
  520. if (it->utype != V_ASN1_ANY) {
  521. /*
  522. * Default handling if value == size field then omit
  523. */
  524. if (*tbool && (it->size > 0))
  525. return -1;
  526. if (!*tbool && !it->size)
  527. return -1;
  528. }
  529. c = (unsigned char)*tbool;
  530. cont = &c;
  531. len = 1;
  532. break;
  533. case V_ASN1_BIT_STRING:
  534. return ossl_i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval,
  535. cout ? &cout : NULL);
  536. case V_ASN1_INTEGER:
  537. case V_ASN1_ENUMERATED:
  538. /*
  539. * These are all have the same content format as ASN1_INTEGER
  540. */
  541. return ossl_i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval, cout ? &cout : NULL);
  542. case V_ASN1_OCTET_STRING:
  543. case V_ASN1_NUMERICSTRING:
  544. case V_ASN1_PRINTABLESTRING:
  545. case V_ASN1_T61STRING:
  546. case V_ASN1_VIDEOTEXSTRING:
  547. case V_ASN1_IA5STRING:
  548. case V_ASN1_UTCTIME:
  549. case V_ASN1_GENERALIZEDTIME:
  550. case V_ASN1_GRAPHICSTRING:
  551. case V_ASN1_VISIBLESTRING:
  552. case V_ASN1_GENERALSTRING:
  553. case V_ASN1_UNIVERSALSTRING:
  554. case V_ASN1_BMPSTRING:
  555. case V_ASN1_UTF8STRING:
  556. case V_ASN1_SEQUENCE:
  557. case V_ASN1_SET:
  558. default:
  559. /* All based on ASN1_STRING and handled the same */
  560. strtmp = (ASN1_STRING *)*pval;
  561. /* Special handling for NDEF */
  562. if ((it->size == ASN1_TFLG_NDEF)
  563. && (strtmp->flags & ASN1_STRING_FLAG_NDEF)) {
  564. if (cout) {
  565. strtmp->data = cout;
  566. strtmp->length = 0;
  567. }
  568. /* Special return code */
  569. return -2;
  570. }
  571. cont = strtmp->data;
  572. len = strtmp->length;
  573. break;
  574. }
  575. if (cout && len)
  576. memcpy(cout, cont, len);
  577. return len;
  578. }