2
0

tasn_enc.c 18 KB

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