tasn_enc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /* tasn_enc.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  4. * 2000.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2000-2004 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <stddef.h>
  60. #include <string.h>
  61. #include "cryptlib.h"
  62. #include <openssl/asn1.h>
  63. #include <openssl/asn1t.h>
  64. #include <openssl/objects.h>
  65. static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
  66. const ASN1_ITEM *it, int tag, int aclass);
  67. static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
  68. int skcontlen, const ASN1_ITEM *item,
  69. int do_sort, int iclass);
  70. static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
  71. const ASN1_TEMPLATE *tt, int tag, int aclass);
  72. static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
  73. const ASN1_ITEM *it, int flags);
  74. /*
  75. * Top level i2d equivalents: the 'ndef' variant instructs the encoder to use
  76. * indefinite length constructed encoding, where appropriate
  77. */
  78. int ASN1_item_ndef_i2d(ASN1_VALUE *val, unsigned char **out,
  79. const ASN1_ITEM *it)
  80. {
  81. return asn1_item_flags_i2d(val, out, it, ASN1_TFLG_NDEF);
  82. }
  83. int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it)
  84. {
  85. return asn1_item_flags_i2d(val, out, it, 0);
  86. }
  87. /*
  88. * Encode an ASN1 item, this is use by the standard 'i2d' function. 'out'
  89. * points to a buffer to output the data to. The new i2d has one additional
  90. * feature. If the output buffer is NULL (i.e. *out == NULL) then a buffer is
  91. * allocated and populated with the encoding.
  92. */
  93. static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
  94. const ASN1_ITEM *it, int flags)
  95. {
  96. if (out && !*out) {
  97. unsigned char *p, *buf;
  98. int len;
  99. len = ASN1_item_ex_i2d(&val, NULL, it, -1, flags);
  100. if (len <= 0)
  101. return len;
  102. buf = OPENSSL_malloc(len);
  103. if (!buf)
  104. return -1;
  105. p = buf;
  106. ASN1_item_ex_i2d(&val, &p, it, -1, flags);
  107. *out = buf;
  108. return len;
  109. }
  110. return ASN1_item_ex_i2d(&val, out, it, -1, flags);
  111. }
  112. /*
  113. * Encode an item, taking care of IMPLICIT tagging (if any). This function
  114. * performs the normal item handling: it can be used in external types.
  115. */
  116. int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
  117. const ASN1_ITEM *it, int tag, int aclass)
  118. {
  119. const ASN1_TEMPLATE *tt = NULL;
  120. unsigned char *p = NULL;
  121. int i, seqcontlen, seqlen, ndef = 1;
  122. const ASN1_COMPAT_FUNCS *cf;
  123. const ASN1_EXTERN_FUNCS *ef;
  124. const ASN1_AUX *aux = it->funcs;
  125. ASN1_aux_cb *asn1_cb = 0;
  126. if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
  127. return 0;
  128. if (aux && aux->asn1_cb)
  129. asn1_cb = aux->asn1_cb;
  130. switch (it->itype) {
  131. case ASN1_ITYPE_PRIMITIVE:
  132. if (it->templates)
  133. return asn1_template_ex_i2d(pval, out, it->templates,
  134. tag, aclass);
  135. return asn1_i2d_ex_primitive(pval, out, it, tag, aclass);
  136. break;
  137. case ASN1_ITYPE_MSTRING:
  138. return asn1_i2d_ex_primitive(pval, out, it, -1, aclass);
  139. case ASN1_ITYPE_CHOICE:
  140. if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it))
  141. return 0;
  142. i = asn1_get_choice_selector(pval, it);
  143. if ((i >= 0) && (i < it->tcount)) {
  144. ASN1_VALUE **pchval;
  145. const ASN1_TEMPLATE *chtt;
  146. chtt = it->templates + i;
  147. pchval = asn1_get_field_ptr(pval, chtt);
  148. return asn1_template_ex_i2d(pchval, out, chtt, -1, aclass);
  149. }
  150. /* Fixme: error condition if selector out of range */
  151. if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it))
  152. return 0;
  153. break;
  154. case ASN1_ITYPE_EXTERN:
  155. /* If new style i2d it does all the work */
  156. ef = it->funcs;
  157. return ef->asn1_ex_i2d(pval, out, it, tag, aclass);
  158. case ASN1_ITYPE_COMPAT:
  159. /* old style hackery... */
  160. cf = it->funcs;
  161. if (out)
  162. p = *out;
  163. i = cf->asn1_i2d(*pval, out);
  164. /*
  165. * Fixup for IMPLICIT tag: note this messes up for tags > 30, but so
  166. * did the old code. Tags > 30 are very rare anyway.
  167. */
  168. if (out && (tag != -1))
  169. *p = aclass | tag | (*p & V_ASN1_CONSTRUCTED);
  170. return i;
  171. case ASN1_ITYPE_NDEF_SEQUENCE:
  172. /* Use indefinite length constructed if requested */
  173. if (aclass & ASN1_TFLG_NDEF)
  174. ndef = 2;
  175. /* fall through */
  176. case ASN1_ITYPE_SEQUENCE:
  177. i = asn1_enc_restore(&seqcontlen, out, pval, it);
  178. /* An error occurred */
  179. if (i < 0)
  180. return 0;
  181. /* We have a valid cached encoding... */
  182. if (i > 0)
  183. return seqcontlen;
  184. /* Otherwise carry on */
  185. seqcontlen = 0;
  186. /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
  187. if (tag == -1) {
  188. tag = V_ASN1_SEQUENCE;
  189. /* Retain any other flags in aclass */
  190. aclass = (aclass & ~ASN1_TFLG_TAG_CLASS)
  191. | V_ASN1_UNIVERSAL;
  192. }
  193. if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it))
  194. return 0;
  195. /* First work out sequence content length */
  196. for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
  197. const ASN1_TEMPLATE *seqtt;
  198. ASN1_VALUE **pseqval;
  199. seqtt = asn1_do_adb(pval, tt, 1);
  200. if (!seqtt)
  201. return 0;
  202. pseqval = asn1_get_field_ptr(pval, seqtt);
  203. /* FIXME: check for errors in enhanced version */
  204. seqcontlen += asn1_template_ex_i2d(pseqval, NULL, seqtt,
  205. -1, aclass);
  206. }
  207. seqlen = ASN1_object_size(ndef, seqcontlen, tag);
  208. if (!out)
  209. return seqlen;
  210. /* Output SEQUENCE header */
  211. ASN1_put_object(out, ndef, seqcontlen, tag, aclass);
  212. for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
  213. const ASN1_TEMPLATE *seqtt;
  214. ASN1_VALUE **pseqval;
  215. seqtt = asn1_do_adb(pval, tt, 1);
  216. if (!seqtt)
  217. return 0;
  218. pseqval = asn1_get_field_ptr(pval, seqtt);
  219. /* FIXME: check for errors in enhanced version */
  220. asn1_template_ex_i2d(pseqval, out, seqtt, -1, aclass);
  221. }
  222. if (ndef == 2)
  223. ASN1_put_eoc(out);
  224. if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it))
  225. return 0;
  226. return seqlen;
  227. default:
  228. return 0;
  229. }
  230. return 0;
  231. }
  232. int ASN1_template_i2d(ASN1_VALUE **pval, unsigned char **out,
  233. const ASN1_TEMPLATE *tt)
  234. {
  235. return asn1_template_ex_i2d(pval, out, tt, -1, 0);
  236. }
  237. static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
  238. const ASN1_TEMPLATE *tt, int tag, int iclass)
  239. {
  240. int i, ret, flags, ttag, tclass, ndef;
  241. flags = tt->flags;
  242. /*
  243. * Work out tag and class to use: tagging may come either from the
  244. * template or the arguments, not both because this would create
  245. * ambiguity. Additionally the iclass argument may contain some
  246. * additional flags which should be noted and passed down to other
  247. * levels.
  248. */
  249. if (flags & ASN1_TFLG_TAG_MASK) {
  250. /* Error if argument and template tagging */
  251. if (tag != -1)
  252. /* FIXME: error code here */
  253. return -1;
  254. /* Get tagging from template */
  255. ttag = tt->tag;
  256. tclass = flags & ASN1_TFLG_TAG_CLASS;
  257. } else if (tag != -1) {
  258. /* No template tagging, get from arguments */
  259. ttag = tag;
  260. tclass = iclass & ASN1_TFLG_TAG_CLASS;
  261. } else {
  262. ttag = -1;
  263. tclass = 0;
  264. }
  265. /*
  266. * Remove any class mask from iflag.
  267. */
  268. iclass &= ~ASN1_TFLG_TAG_CLASS;
  269. /*
  270. * At this point 'ttag' contains the outer tag to use, 'tclass' is the
  271. * class and iclass is any flags passed to this function.
  272. */
  273. /* if template and arguments require ndef, use it */
  274. if ((flags & ASN1_TFLG_NDEF) && (iclass & ASN1_TFLG_NDEF))
  275. ndef = 2;
  276. else
  277. ndef = 1;
  278. if (flags & ASN1_TFLG_SK_MASK) {
  279. /* SET OF, SEQUENCE OF */
  280. STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
  281. int isset, sktag, skaclass;
  282. int skcontlen, sklen;
  283. ASN1_VALUE *skitem;
  284. if (!*pval)
  285. return 0;
  286. if (flags & ASN1_TFLG_SET_OF) {
  287. isset = 1;
  288. /* 2 means we reorder */
  289. if (flags & ASN1_TFLG_SEQUENCE_OF)
  290. isset = 2;
  291. } else
  292. isset = 0;
  293. /*
  294. * Work out inner tag value: if EXPLICIT or no tagging use underlying
  295. * type.
  296. */
  297. if ((ttag != -1) && !(flags & ASN1_TFLG_EXPTAG)) {
  298. sktag = ttag;
  299. skaclass = tclass;
  300. } else {
  301. skaclass = V_ASN1_UNIVERSAL;
  302. if (isset)
  303. sktag = V_ASN1_SET;
  304. else
  305. sktag = V_ASN1_SEQUENCE;
  306. }
  307. /* Determine total length of items */
  308. skcontlen = 0;
  309. for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
  310. skitem = sk_ASN1_VALUE_value(sk, i);
  311. skcontlen += ASN1_item_ex_i2d(&skitem, NULL,
  312. ASN1_ITEM_ptr(tt->item),
  313. -1, iclass);
  314. }
  315. sklen = ASN1_object_size(ndef, skcontlen, sktag);
  316. /* If EXPLICIT need length of surrounding tag */
  317. if (flags & ASN1_TFLG_EXPTAG)
  318. ret = ASN1_object_size(ndef, sklen, ttag);
  319. else
  320. ret = sklen;
  321. if (!out)
  322. return ret;
  323. /* Now encode this lot... */
  324. /* EXPLICIT tag */
  325. if (flags & ASN1_TFLG_EXPTAG)
  326. ASN1_put_object(out, ndef, sklen, ttag, tclass);
  327. /* SET or SEQUENCE and IMPLICIT tag */
  328. ASN1_put_object(out, ndef, skcontlen, sktag, skaclass);
  329. /* And the stuff itself */
  330. asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item),
  331. isset, iclass);
  332. if (ndef == 2) {
  333. ASN1_put_eoc(out);
  334. if (flags & ASN1_TFLG_EXPTAG)
  335. ASN1_put_eoc(out);
  336. }
  337. return ret;
  338. }
  339. if (flags & ASN1_TFLG_EXPTAG) {
  340. /* EXPLICIT tagging */
  341. /* Find length of tagged item */
  342. i = ASN1_item_ex_i2d(pval, NULL, ASN1_ITEM_ptr(tt->item), -1, iclass);
  343. if (!i)
  344. return 0;
  345. /* Find length of EXPLICIT tag */
  346. ret = ASN1_object_size(ndef, i, ttag);
  347. if (out) {
  348. /* Output tag and item */
  349. ASN1_put_object(out, ndef, i, ttag, tclass);
  350. ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), -1, iclass);
  351. if (ndef == 2)
  352. ASN1_put_eoc(out);
  353. }
  354. return ret;
  355. }
  356. /* Either normal or IMPLICIT tagging: combine class and flags */
  357. return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item),
  358. ttag, tclass | iclass);
  359. }
  360. /* Temporary structure used to hold DER encoding of items for SET OF */
  361. typedef struct {
  362. unsigned char *data;
  363. int length;
  364. ASN1_VALUE *field;
  365. } DER_ENC;
  366. static int der_cmp(const void *a, const void *b)
  367. {
  368. const DER_ENC *d1 = a, *d2 = b;
  369. int cmplen, i;
  370. cmplen = (d1->length < d2->length) ? d1->length : d2->length;
  371. i = memcmp(d1->data, d2->data, cmplen);
  372. if (i)
  373. return i;
  374. return d1->length - d2->length;
  375. }
  376. /* Output the content octets of SET OF or SEQUENCE OF */
  377. static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
  378. int skcontlen, const ASN1_ITEM *item,
  379. int do_sort, int iclass)
  380. {
  381. int i;
  382. ASN1_VALUE *skitem;
  383. unsigned char *tmpdat = NULL, *p = NULL;
  384. DER_ENC *derlst = NULL, *tder;
  385. if (do_sort) {
  386. /* Don't need to sort less than 2 items */
  387. if (sk_ASN1_VALUE_num(sk) < 2)
  388. do_sort = 0;
  389. else {
  390. derlst = OPENSSL_malloc(sk_ASN1_VALUE_num(sk)
  391. * sizeof(*derlst));
  392. if (!derlst)
  393. return 0;
  394. tmpdat = OPENSSL_malloc(skcontlen);
  395. if (!tmpdat) {
  396. OPENSSL_free(derlst);
  397. return 0;
  398. }
  399. }
  400. }
  401. /* If not sorting just output each item */
  402. if (!do_sort) {
  403. for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
  404. skitem = sk_ASN1_VALUE_value(sk, i);
  405. ASN1_item_ex_i2d(&skitem, out, item, -1, iclass);
  406. }
  407. return 1;
  408. }
  409. p = tmpdat;
  410. /* Doing sort: build up a list of each member's DER encoding */
  411. for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) {
  412. skitem = sk_ASN1_VALUE_value(sk, i);
  413. tder->data = p;
  414. tder->length = ASN1_item_ex_i2d(&skitem, &p, item, -1, iclass);
  415. tder->field = skitem;
  416. }
  417. /* Now sort them */
  418. qsort(derlst, sk_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp);
  419. /* Output sorted DER encoding */
  420. p = *out;
  421. for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) {
  422. memcpy(p, tder->data, tder->length);
  423. p += tder->length;
  424. }
  425. *out = p;
  426. /* If do_sort is 2 then reorder the STACK */
  427. if (do_sort == 2) {
  428. for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++)
  429. (void)sk_ASN1_VALUE_set(sk, i, tder->field);
  430. }
  431. OPENSSL_free(derlst);
  432. OPENSSL_free(tmpdat);
  433. return 1;
  434. }
  435. static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
  436. const ASN1_ITEM *it, int tag, int aclass)
  437. {
  438. int len;
  439. int utype;
  440. int usetag;
  441. int ndef = 0;
  442. utype = it->utype;
  443. /*
  444. * Get length of content octets and maybe find out the underlying type.
  445. */
  446. len = asn1_ex_i2c(pval, NULL, &utype, it);
  447. /*
  448. * If SEQUENCE, SET or OTHER then header is included in pseudo content
  449. * octets so don't include tag+length. We need to check here because the
  450. * call to asn1_ex_i2c() could change utype.
  451. */
  452. if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) ||
  453. (utype == V_ASN1_OTHER))
  454. usetag = 0;
  455. else
  456. usetag = 1;
  457. /* -1 means omit type */
  458. if (len == -1)
  459. return 0;
  460. /* -2 return is special meaning use ndef */
  461. if (len == -2) {
  462. ndef = 2;
  463. len = 0;
  464. }
  465. /* If not implicitly tagged get tag from underlying type */
  466. if (tag == -1)
  467. tag = utype;
  468. /* Output tag+length followed by content octets */
  469. if (out) {
  470. if (usetag)
  471. ASN1_put_object(out, ndef, len, tag, aclass);
  472. asn1_ex_i2c(pval, *out, &utype, it);
  473. if (ndef)
  474. ASN1_put_eoc(out);
  475. else
  476. *out += len;
  477. }
  478. if (usetag)
  479. return ASN1_object_size(ndef, len, tag);
  480. return len;
  481. }
  482. /* Produce content octets from a structure */
  483. int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
  484. const ASN1_ITEM *it)
  485. {
  486. ASN1_BOOLEAN *tbool = NULL;
  487. ASN1_STRING *strtmp;
  488. ASN1_OBJECT *otmp;
  489. int utype;
  490. unsigned char *cont, c;
  491. int len;
  492. const ASN1_PRIMITIVE_FUNCS *pf;
  493. pf = it->funcs;
  494. if (pf && pf->prim_i2c)
  495. return pf->prim_i2c(pval, cout, putype, it);
  496. /* Should type be omitted? */
  497. if ((it->itype != ASN1_ITYPE_PRIMITIVE)
  498. || (it->utype != V_ASN1_BOOLEAN)) {
  499. if (!*pval)
  500. return -1;
  501. }
  502. if (it->itype == ASN1_ITYPE_MSTRING) {
  503. /* If MSTRING type set the underlying type */
  504. strtmp = (ASN1_STRING *)*pval;
  505. utype = strtmp->type;
  506. *putype = utype;
  507. } else if (it->utype == V_ASN1_ANY) {
  508. /* If ANY set type and pointer to value */
  509. ASN1_TYPE *typ;
  510. typ = (ASN1_TYPE *)*pval;
  511. utype = typ->type;
  512. *putype = utype;
  513. pval = &typ->value.asn1_value;
  514. } else
  515. utype = *putype;
  516. switch (utype) {
  517. case V_ASN1_OBJECT:
  518. otmp = (ASN1_OBJECT *)*pval;
  519. cont = otmp->data;
  520. len = otmp->length;
  521. break;
  522. case V_ASN1_NULL:
  523. cont = NULL;
  524. len = 0;
  525. break;
  526. case V_ASN1_BOOLEAN:
  527. tbool = (ASN1_BOOLEAN *)pval;
  528. if (*tbool == -1)
  529. return -1;
  530. if (it->utype != V_ASN1_ANY) {
  531. /*
  532. * Default handling if value == size field then omit
  533. */
  534. if (*tbool && (it->size > 0))
  535. return -1;
  536. if (!*tbool && !it->size)
  537. return -1;
  538. }
  539. c = (unsigned char)*tbool;
  540. cont = &c;
  541. len = 1;
  542. break;
  543. case V_ASN1_BIT_STRING:
  544. return i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval,
  545. cout ? &cout : NULL);
  546. break;
  547. case V_ASN1_INTEGER:
  548. case V_ASN1_NEG_INTEGER:
  549. case V_ASN1_ENUMERATED:
  550. case V_ASN1_NEG_ENUMERATED:
  551. /*
  552. * These are all have the same content format as ASN1_INTEGER
  553. */
  554. return i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval, cout ? &cout : NULL);
  555. break;
  556. case V_ASN1_OCTET_STRING:
  557. case V_ASN1_NUMERICSTRING:
  558. case V_ASN1_PRINTABLESTRING:
  559. case V_ASN1_T61STRING:
  560. case V_ASN1_VIDEOTEXSTRING:
  561. case V_ASN1_IA5STRING:
  562. case V_ASN1_UTCTIME:
  563. case V_ASN1_GENERALIZEDTIME:
  564. case V_ASN1_GRAPHICSTRING:
  565. case V_ASN1_VISIBLESTRING:
  566. case V_ASN1_GENERALSTRING:
  567. case V_ASN1_UNIVERSALSTRING:
  568. case V_ASN1_BMPSTRING:
  569. case V_ASN1_UTF8STRING:
  570. case V_ASN1_SEQUENCE:
  571. case V_ASN1_SET:
  572. default:
  573. /* All based on ASN1_STRING and handled the same */
  574. strtmp = (ASN1_STRING *)*pval;
  575. /* Special handling for NDEF */
  576. if ((it->size == ASN1_TFLG_NDEF)
  577. && (strtmp->flags & ASN1_STRING_FLAG_NDEF)) {
  578. if (cout) {
  579. strtmp->data = cout;
  580. strtmp->length = 0;
  581. }
  582. /* Special return code */
  583. return -2;
  584. }
  585. cont = strtmp->data;
  586. len = strtmp->length;
  587. break;
  588. }
  589. if (cout && len)
  590. memcpy(cout, cont, len);
  591. return len;
  592. }