tasn_dec.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. /*
  2. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  3. * 2000.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2000-2005 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 <openssl/asn1.h>
  61. #include <openssl/asn1t.h>
  62. #include <openssl/objects.h>
  63. #include <openssl/buffer.h>
  64. #include <openssl/err.h>
  65. #include "asn1_locl.h"
  66. static int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in,
  67. long len, const ASN1_ITEM *it,
  68. int tag, int aclass, char opt, ASN1_TLC *ctx);
  69. static int asn1_check_eoc(const unsigned char **in, long len);
  70. static int asn1_find_end(const unsigned char **in, long len, char inf);
  71. static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
  72. char inf, int tag, int aclass, int depth);
  73. static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen);
  74. static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
  75. char *inf, char *cst,
  76. const unsigned char **in, long len,
  77. int exptag, int expclass, char opt, ASN1_TLC *ctx);
  78. static int asn1_template_ex_d2i(ASN1_VALUE **pval,
  79. const unsigned char **in, long len,
  80. const ASN1_TEMPLATE *tt, char opt,
  81. ASN1_TLC *ctx);
  82. static int asn1_template_noexp_d2i(ASN1_VALUE **val,
  83. const unsigned char **in, long len,
  84. const ASN1_TEMPLATE *tt, char opt,
  85. ASN1_TLC *ctx);
  86. static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
  87. const unsigned char **in, long len,
  88. const ASN1_ITEM *it,
  89. int tag, int aclass, char opt,
  90. ASN1_TLC *ctx);
  91. static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
  92. int utype, char *free_cont, const ASN1_ITEM *it);
  93. /* Table to convert tags to bit values, used for MSTRING type */
  94. static const unsigned long tag2bit[32] = {
  95. /* tags 0 - 3 */
  96. 0, 0, 0, B_ASN1_BIT_STRING,
  97. /* tags 4- 7 */
  98. B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,
  99. /* tags 8-11 */
  100. B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,
  101. /* tags 12-15 */
  102. B_ASN1_UTF8STRING, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,
  103. /* tags 16-19 */
  104. B_ASN1_SEQUENCE, 0, B_ASN1_NUMERICSTRING, B_ASN1_PRINTABLESTRING,
  105. /* tags 20-22 */
  106. B_ASN1_T61STRING, B_ASN1_VIDEOTEXSTRING, B_ASN1_IA5STRING,
  107. /* tags 23-24 */
  108. B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME,
  109. /* tags 25-27 */
  110. B_ASN1_GRAPHICSTRING, B_ASN1_ISO64STRING, B_ASN1_GENERALSTRING,
  111. /* tags 28-31 */
  112. B_ASN1_UNIVERSALSTRING, B_ASN1_UNKNOWN, B_ASN1_BMPSTRING, B_ASN1_UNKNOWN,
  113. };
  114. unsigned long ASN1_tag2bit(int tag)
  115. {
  116. if ((tag < 0) || (tag > 30))
  117. return 0;
  118. return tag2bit[tag];
  119. }
  120. /* Macro to initialize and invalidate the cache */
  121. #define asn1_tlc_clear(c) if (c) (c)->valid = 0
  122. /* Version to avoid compiler warning about 'c' always non-NULL */
  123. #define asn1_tlc_clear_nc(c) (c)->valid = 0
  124. /*
  125. * Decode an ASN1 item, this currently behaves just like a standard 'd2i'
  126. * function. 'in' points to a buffer to read the data from, in future we
  127. * will have more advanced versions that can input data a piece at a time and
  128. * this will simply be a special case.
  129. */
  130. ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
  131. const unsigned char **in, long len,
  132. const ASN1_ITEM *it)
  133. {
  134. ASN1_TLC c;
  135. ASN1_VALUE *ptmpval = NULL;
  136. if (!pval)
  137. pval = &ptmpval;
  138. asn1_tlc_clear_nc(&c);
  139. if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
  140. return *pval;
  141. return NULL;
  142. }
  143. int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
  144. const ASN1_ITEM *it,
  145. int tag, int aclass, char opt, ASN1_TLC *ctx)
  146. {
  147. int rv;
  148. rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx);
  149. if (rv <= 0)
  150. ASN1_item_ex_free(pval, it);
  151. return rv;
  152. }
  153. /*
  154. * Decode an item, taking care of IMPLICIT tagging, if any. If 'opt' set and
  155. * tag mismatch return -1 to handle OPTIONAL
  156. */
  157. static int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in,
  158. long len, const ASN1_ITEM *it,
  159. int tag, int aclass, char opt, ASN1_TLC *ctx)
  160. {
  161. const ASN1_TEMPLATE *tt, *errtt = NULL;
  162. const ASN1_EXTERN_FUNCS *ef;
  163. const ASN1_AUX *aux = it->funcs;
  164. ASN1_aux_cb *asn1_cb;
  165. const unsigned char *p = NULL, *q;
  166. unsigned char oclass;
  167. char seq_eoc, seq_nolen, cst, isopt;
  168. long tmplen;
  169. int i;
  170. int otag;
  171. int ret = 0;
  172. ASN1_VALUE **pchptr;
  173. if (!pval)
  174. return 0;
  175. if (aux && aux->asn1_cb)
  176. asn1_cb = aux->asn1_cb;
  177. else
  178. asn1_cb = 0;
  179. switch (it->itype) {
  180. case ASN1_ITYPE_PRIMITIVE:
  181. if (it->templates) {
  182. /*
  183. * tagging or OPTIONAL is currently illegal on an item template
  184. * because the flags can't get passed down. In practice this
  185. * isn't a problem: we include the relevant flags from the item
  186. * template in the template itself.
  187. */
  188. if ((tag != -1) || opt) {
  189. ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I,
  190. ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
  191. goto err;
  192. }
  193. return asn1_template_ex_d2i(pval, in, len,
  194. it->templates, opt, ctx);
  195. }
  196. return asn1_d2i_ex_primitive(pval, in, len, it,
  197. tag, aclass, opt, ctx);
  198. case ASN1_ITYPE_MSTRING:
  199. p = *in;
  200. /* Just read in tag and class */
  201. ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
  202. &p, len, -1, 0, 1, ctx);
  203. if (!ret) {
  204. ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
  205. goto err;
  206. }
  207. /* Must be UNIVERSAL class */
  208. if (oclass != V_ASN1_UNIVERSAL) {
  209. /* If OPTIONAL, assume this is OK */
  210. if (opt)
  211. return -1;
  212. ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MSTRING_NOT_UNIVERSAL);
  213. goto err;
  214. }
  215. /* Check tag matches bit map */
  216. if (!(ASN1_tag2bit(otag) & it->utype)) {
  217. /* If OPTIONAL, assume this is OK */
  218. if (opt)
  219. return -1;
  220. ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MSTRING_WRONG_TAG);
  221. goto err;
  222. }
  223. return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
  224. case ASN1_ITYPE_EXTERN:
  225. /* Use new style d2i */
  226. ef = it->funcs;
  227. return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
  228. case ASN1_ITYPE_CHOICE:
  229. if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
  230. goto auxerr;
  231. if (*pval) {
  232. /* Free up and zero CHOICE value if initialised */
  233. i = asn1_get_choice_selector(pval, it);
  234. if ((i >= 0) && (i < it->tcount)) {
  235. tt = it->templates + i;
  236. pchptr = asn1_get_field_ptr(pval, tt);
  237. asn1_template_free(pchptr, tt);
  238. asn1_set_choice_selector(pval, -1, it);
  239. }
  240. } else if (!ASN1_item_ex_new(pval, it)) {
  241. ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
  242. goto err;
  243. }
  244. /* CHOICE type, try each possibility in turn */
  245. p = *in;
  246. for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
  247. pchptr = asn1_get_field_ptr(pval, tt);
  248. /*
  249. * We mark field as OPTIONAL so its absence can be recognised.
  250. */
  251. ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx);
  252. /* If field not present, try the next one */
  253. if (ret == -1)
  254. continue;
  255. /* If positive return, read OK, break loop */
  256. if (ret > 0)
  257. break;
  258. /* Otherwise must be an ASN1 parsing error */
  259. errtt = tt;
  260. ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
  261. goto err;
  262. }
  263. /* Did we fall off the end without reading anything? */
  264. if (i == it->tcount) {
  265. /* If OPTIONAL, this is OK */
  266. if (opt) {
  267. /* Free and zero it */
  268. ASN1_item_ex_free(pval, it);
  269. return -1;
  270. }
  271. ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_NO_MATCHING_CHOICE_TYPE);
  272. goto err;
  273. }
  274. asn1_set_choice_selector(pval, i, it);
  275. if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
  276. goto auxerr;
  277. *in = p;
  278. return 1;
  279. case ASN1_ITYPE_NDEF_SEQUENCE:
  280. case ASN1_ITYPE_SEQUENCE:
  281. p = *in;
  282. tmplen = len;
  283. /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
  284. if (tag == -1) {
  285. tag = V_ASN1_SEQUENCE;
  286. aclass = V_ASN1_UNIVERSAL;
  287. }
  288. /* Get SEQUENCE length and update len, p */
  289. ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
  290. &p, len, tag, aclass, opt, ctx);
  291. if (!ret) {
  292. ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
  293. goto err;
  294. } else if (ret == -1)
  295. return -1;
  296. if (aux && (aux->flags & ASN1_AFLG_BROKEN)) {
  297. len = tmplen - (p - *in);
  298. seq_nolen = 1;
  299. }
  300. /* If indefinite we don't do a length check */
  301. else
  302. seq_nolen = seq_eoc;
  303. if (!cst) {
  304. ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
  305. goto err;
  306. }
  307. if (!*pval && !ASN1_item_ex_new(pval, it)) {
  308. ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
  309. goto err;
  310. }
  311. if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
  312. goto auxerr;
  313. /* Free up and zero any ADB found */
  314. for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
  315. if (tt->flags & ASN1_TFLG_ADB_MASK) {
  316. const ASN1_TEMPLATE *seqtt;
  317. ASN1_VALUE **pseqval;
  318. seqtt = asn1_do_adb(pval, tt, 1);
  319. pseqval = asn1_get_field_ptr(pval, seqtt);
  320. asn1_template_free(pseqval, seqtt);
  321. }
  322. }
  323. /* Get each field entry */
  324. for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
  325. const ASN1_TEMPLATE *seqtt;
  326. ASN1_VALUE **pseqval;
  327. seqtt = asn1_do_adb(pval, tt, 1);
  328. if (!seqtt)
  329. goto err;
  330. pseqval = asn1_get_field_ptr(pval, seqtt);
  331. /* Have we ran out of data? */
  332. if (!len)
  333. break;
  334. q = p;
  335. if (asn1_check_eoc(&p, len)) {
  336. if (!seq_eoc) {
  337. ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_UNEXPECTED_EOC);
  338. goto err;
  339. }
  340. len -= p - q;
  341. seq_eoc = 0;
  342. q = p;
  343. break;
  344. }
  345. /*
  346. * This determines the OPTIONAL flag value. The field cannot be
  347. * omitted if it is the last of a SEQUENCE and there is still
  348. * data to be read. This isn't strictly necessary but it
  349. * increases efficiency in some cases.
  350. */
  351. if (i == (it->tcount - 1))
  352. isopt = 0;
  353. else
  354. isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
  355. /*
  356. * attempt to read in field, allowing each to be OPTIONAL
  357. */
  358. ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx);
  359. if (!ret) {
  360. errtt = seqtt;
  361. goto err;
  362. } else if (ret == -1) {
  363. /*
  364. * OPTIONAL component absent. Free and zero the field.
  365. */
  366. asn1_template_free(pseqval, seqtt);
  367. continue;
  368. }
  369. /* Update length */
  370. len -= p - q;
  371. }
  372. /* Check for EOC if expecting one */
  373. if (seq_eoc && !asn1_check_eoc(&p, len)) {
  374. ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MISSING_EOC);
  375. goto err;
  376. }
  377. /* Check all data read */
  378. if (!seq_nolen && len) {
  379. ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
  380. goto err;
  381. }
  382. /*
  383. * If we get here we've got no more data in the SEQUENCE, however we
  384. * may not have read all fields so check all remaining are OPTIONAL
  385. * and clear any that are.
  386. */
  387. for (; i < it->tcount; tt++, i++) {
  388. const ASN1_TEMPLATE *seqtt;
  389. seqtt = asn1_do_adb(pval, tt, 1);
  390. if (!seqtt)
  391. goto err;
  392. if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
  393. ASN1_VALUE **pseqval;
  394. pseqval = asn1_get_field_ptr(pval, seqtt);
  395. asn1_template_free(pseqval, seqtt);
  396. } else {
  397. errtt = seqtt;
  398. ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_FIELD_MISSING);
  399. goto err;
  400. }
  401. }
  402. /* Save encoding */
  403. if (!asn1_enc_save(pval, *in, p - *in, it))
  404. goto auxerr;
  405. if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
  406. goto auxerr;
  407. *in = p;
  408. return 1;
  409. default:
  410. return 0;
  411. }
  412. auxerr:
  413. ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_AUX_ERROR);
  414. err:
  415. if (errtt)
  416. ERR_add_error_data(4, "Field=", errtt->field_name,
  417. ", Type=", it->sname);
  418. else
  419. ERR_add_error_data(2, "Type=", it->sname);
  420. return 0;
  421. }
  422. /*
  423. * Templates are handled with two separate functions. One handles any
  424. * EXPLICIT tag and the other handles the rest.
  425. */
  426. static int asn1_template_ex_d2i(ASN1_VALUE **val,
  427. const unsigned char **in, long inlen,
  428. const ASN1_TEMPLATE *tt, char opt,
  429. ASN1_TLC *ctx)
  430. {
  431. int flags, aclass;
  432. int ret;
  433. long len;
  434. const unsigned char *p, *q;
  435. char exp_eoc;
  436. if (!val)
  437. return 0;
  438. flags = tt->flags;
  439. aclass = flags & ASN1_TFLG_TAG_CLASS;
  440. p = *in;
  441. /* Check if EXPLICIT tag expected */
  442. if (flags & ASN1_TFLG_EXPTAG) {
  443. char cst;
  444. /*
  445. * Need to work out amount of data available to the inner content and
  446. * where it starts: so read in EXPLICIT header to get the info.
  447. */
  448. ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
  449. &p, inlen, tt->tag, aclass, opt, ctx);
  450. q = p;
  451. if (!ret) {
  452. ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
  453. return 0;
  454. } else if (ret == -1)
  455. return -1;
  456. if (!cst) {
  457. ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
  458. ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
  459. return 0;
  460. }
  461. /* We've found the field so it can't be OPTIONAL now */
  462. ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
  463. if (!ret) {
  464. ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
  465. return 0;
  466. }
  467. /* We read the field in OK so update length */
  468. len -= p - q;
  469. if (exp_eoc) {
  470. /* If NDEF we must have an EOC here */
  471. if (!asn1_check_eoc(&p, len)) {
  472. ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ASN1_R_MISSING_EOC);
  473. goto err;
  474. }
  475. } else {
  476. /*
  477. * Otherwise we must hit the EXPLICIT tag end or its an error
  478. */
  479. if (len) {
  480. ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
  481. ASN1_R_EXPLICIT_LENGTH_MISMATCH);
  482. goto err;
  483. }
  484. }
  485. } else
  486. return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx);
  487. *in = p;
  488. return 1;
  489. err:
  490. return 0;
  491. }
  492. static int asn1_template_noexp_d2i(ASN1_VALUE **val,
  493. const unsigned char **in, long len,
  494. const ASN1_TEMPLATE *tt, char opt,
  495. ASN1_TLC *ctx)
  496. {
  497. int flags, aclass;
  498. int ret;
  499. ASN1_VALUE *tval;
  500. const unsigned char *p, *q;
  501. if (!val)
  502. return 0;
  503. flags = tt->flags;
  504. aclass = flags & ASN1_TFLG_TAG_CLASS;
  505. p = *in;
  506. q = p;
  507. /*
  508. * If field is embedded then val needs fixing so it is a pointer to
  509. * a pointer to a field.
  510. */
  511. if (tt->flags & ASN1_TFLG_EMBED) {
  512. tval = (ASN1_VALUE *)val;
  513. val = &tval;
  514. }
  515. if (flags & ASN1_TFLG_SK_MASK) {
  516. /* SET OF, SEQUENCE OF */
  517. int sktag, skaclass;
  518. char sk_eoc;
  519. /* First work out expected inner tag value */
  520. if (flags & ASN1_TFLG_IMPTAG) {
  521. sktag = tt->tag;
  522. skaclass = aclass;
  523. } else {
  524. skaclass = V_ASN1_UNIVERSAL;
  525. if (flags & ASN1_TFLG_SET_OF)
  526. sktag = V_ASN1_SET;
  527. else
  528. sktag = V_ASN1_SEQUENCE;
  529. }
  530. /* Get the tag */
  531. ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
  532. &p, len, sktag, skaclass, opt, ctx);
  533. if (!ret) {
  534. ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
  535. return 0;
  536. } else if (ret == -1)
  537. return -1;
  538. if (!*val)
  539. *val = (ASN1_VALUE *)sk_new_null();
  540. else {
  541. /*
  542. * We've got a valid STACK: free up any items present
  543. */
  544. STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
  545. ASN1_VALUE *vtmp;
  546. while (sk_ASN1_VALUE_num(sktmp) > 0) {
  547. vtmp = sk_ASN1_VALUE_pop(sktmp);
  548. ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item));
  549. }
  550. }
  551. if (!*val) {
  552. ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_MALLOC_FAILURE);
  553. goto err;
  554. }
  555. /* Read as many items as we can */
  556. while (len > 0) {
  557. ASN1_VALUE *skfield;
  558. q = p;
  559. /* See if EOC found */
  560. if (asn1_check_eoc(&p, len)) {
  561. if (!sk_eoc) {
  562. ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
  563. ASN1_R_UNEXPECTED_EOC);
  564. goto err;
  565. }
  566. len -= p - q;
  567. sk_eoc = 0;
  568. break;
  569. }
  570. skfield = NULL;
  571. if (!asn1_item_embed_d2i(&skfield, &p, len,
  572. ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx)) {
  573. ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
  574. ERR_R_NESTED_ASN1_ERROR);
  575. goto err;
  576. }
  577. len -= p - q;
  578. if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) {
  579. ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_MALLOC_FAILURE);
  580. ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
  581. goto err;
  582. }
  583. }
  584. if (sk_eoc) {
  585. ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC);
  586. goto err;
  587. }
  588. } else if (flags & ASN1_TFLG_IMPTAG) {
  589. /* IMPLICIT tagging */
  590. ret = asn1_item_embed_d2i(val, &p, len,
  591. ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
  592. ctx);
  593. if (!ret) {
  594. ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
  595. goto err;
  596. } else if (ret == -1)
  597. return -1;
  598. } else {
  599. /* Nothing special */
  600. ret = asn1_item_embed_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
  601. -1, 0, opt, ctx);
  602. if (!ret) {
  603. ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
  604. goto err;
  605. } else if (ret == -1)
  606. return -1;
  607. }
  608. *in = p;
  609. return 1;
  610. err:
  611. return 0;
  612. }
  613. static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
  614. const unsigned char **in, long inlen,
  615. const ASN1_ITEM *it,
  616. int tag, int aclass, char opt, ASN1_TLC *ctx)
  617. {
  618. int ret = 0, utype;
  619. long plen;
  620. char cst, inf, free_cont = 0;
  621. const unsigned char *p;
  622. BUF_MEM buf = { 0, NULL, 0, 0 };
  623. const unsigned char *cont = NULL;
  624. long len;
  625. if (!pval) {
  626. ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
  627. return 0; /* Should never happen */
  628. }
  629. if (it->itype == ASN1_ITYPE_MSTRING) {
  630. utype = tag;
  631. tag = -1;
  632. } else
  633. utype = it->utype;
  634. if (utype == V_ASN1_ANY) {
  635. /* If type is ANY need to figure out type from tag */
  636. unsigned char oclass;
  637. if (tag >= 0) {
  638. ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_TAGGED_ANY);
  639. return 0;
  640. }
  641. if (opt) {
  642. ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
  643. ASN1_R_ILLEGAL_OPTIONAL_ANY);
  644. return 0;
  645. }
  646. p = *in;
  647. ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
  648. &p, inlen, -1, 0, 0, ctx);
  649. if (!ret) {
  650. ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
  651. return 0;
  652. }
  653. if (oclass != V_ASN1_UNIVERSAL)
  654. utype = V_ASN1_OTHER;
  655. }
  656. if (tag == -1) {
  657. tag = utype;
  658. aclass = V_ASN1_UNIVERSAL;
  659. }
  660. p = *in;
  661. /* Check header */
  662. ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
  663. &p, inlen, tag, aclass, opt, ctx);
  664. if (!ret) {
  665. ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
  666. return 0;
  667. } else if (ret == -1)
  668. return -1;
  669. ret = 0;
  670. /* SEQUENCE, SET and "OTHER" are left in encoded form */
  671. if ((utype == V_ASN1_SEQUENCE)
  672. || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) {
  673. /*
  674. * Clear context cache for type OTHER because the auto clear when we
  675. * have a exact match wont work
  676. */
  677. if (utype == V_ASN1_OTHER) {
  678. asn1_tlc_clear(ctx);
  679. }
  680. /* SEQUENCE and SET must be constructed */
  681. else if (!cst) {
  682. ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
  683. ASN1_R_TYPE_NOT_CONSTRUCTED);
  684. return 0;
  685. }
  686. cont = *in;
  687. /* If indefinite length constructed find the real end */
  688. if (inf) {
  689. if (!asn1_find_end(&p, plen, inf))
  690. goto err;
  691. len = p - cont;
  692. } else {
  693. len = p - cont + plen;
  694. p += plen;
  695. }
  696. } else if (cst) {
  697. if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
  698. || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
  699. || utype == V_ASN1_ENUMERATED) {
  700. ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_TYPE_NOT_PRIMITIVE);
  701. return 0;
  702. }
  703. /* Free any returned 'buf' content */
  704. free_cont = 1;
  705. /*
  706. * Should really check the internal tags are correct but some things
  707. * may get this wrong. The relevant specs say that constructed string
  708. * types should be OCTET STRINGs internally irrespective of the type.
  709. * So instead just check for UNIVERSAL class and ignore the tag.
  710. */
  711. if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
  712. goto err;
  713. }
  714. len = buf.length;
  715. /* Append a final null to string */
  716. if (!BUF_MEM_grow_clean(&buf, len + 1)) {
  717. ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_MALLOC_FAILURE);
  718. goto err;
  719. }
  720. buf.data[len] = 0;
  721. cont = (const unsigned char *)buf.data;
  722. } else {
  723. cont = p;
  724. len = plen;
  725. p += plen;
  726. }
  727. /* We now have content length and type: translate into a structure */
  728. /* asn1_ex_c2i may reuse allocated buffer, and so sets free_cont to 0 */
  729. if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
  730. goto err;
  731. *in = p;
  732. ret = 1;
  733. err:
  734. if (free_cont)
  735. OPENSSL_free(buf.data);
  736. return ret;
  737. }
  738. /* Translate ASN1 content octets into a structure */
  739. static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
  740. int utype, char *free_cont, const ASN1_ITEM *it)
  741. {
  742. ASN1_VALUE **opval = NULL;
  743. ASN1_STRING *stmp;
  744. ASN1_TYPE *typ = NULL;
  745. int ret = 0;
  746. const ASN1_PRIMITIVE_FUNCS *pf;
  747. ASN1_INTEGER **tint;
  748. pf = it->funcs;
  749. if (pf && pf->prim_c2i)
  750. return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
  751. /* If ANY type clear type and set pointer to internal value */
  752. if (it->utype == V_ASN1_ANY) {
  753. if (!*pval) {
  754. typ = ASN1_TYPE_new();
  755. if (typ == NULL)
  756. goto err;
  757. *pval = (ASN1_VALUE *)typ;
  758. } else
  759. typ = (ASN1_TYPE *)*pval;
  760. if (utype != typ->type)
  761. ASN1_TYPE_set(typ, utype, NULL);
  762. opval = pval;
  763. pval = &typ->value.asn1_value;
  764. }
  765. switch (utype) {
  766. case V_ASN1_OBJECT:
  767. if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
  768. goto err;
  769. break;
  770. case V_ASN1_NULL:
  771. if (len) {
  772. ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_NULL_IS_WRONG_LENGTH);
  773. goto err;
  774. }
  775. *pval = (ASN1_VALUE *)1;
  776. break;
  777. case V_ASN1_BOOLEAN:
  778. if (len != 1) {
  779. ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
  780. goto err;
  781. } else {
  782. ASN1_BOOLEAN *tbool;
  783. tbool = (ASN1_BOOLEAN *)pval;
  784. *tbool = *cont;
  785. }
  786. break;
  787. case V_ASN1_BIT_STRING:
  788. if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
  789. goto err;
  790. break;
  791. case V_ASN1_INTEGER:
  792. case V_ASN1_NEG_INTEGER:
  793. case V_ASN1_ENUMERATED:
  794. case V_ASN1_NEG_ENUMERATED:
  795. tint = (ASN1_INTEGER **)pval;
  796. if (!c2i_ASN1_INTEGER(tint, &cont, len))
  797. goto err;
  798. /* Fixup type to match the expected form */
  799. (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
  800. break;
  801. case V_ASN1_OCTET_STRING:
  802. case V_ASN1_NUMERICSTRING:
  803. case V_ASN1_PRINTABLESTRING:
  804. case V_ASN1_T61STRING:
  805. case V_ASN1_VIDEOTEXSTRING:
  806. case V_ASN1_IA5STRING:
  807. case V_ASN1_UTCTIME:
  808. case V_ASN1_GENERALIZEDTIME:
  809. case V_ASN1_GRAPHICSTRING:
  810. case V_ASN1_VISIBLESTRING:
  811. case V_ASN1_GENERALSTRING:
  812. case V_ASN1_UNIVERSALSTRING:
  813. case V_ASN1_BMPSTRING:
  814. case V_ASN1_UTF8STRING:
  815. case V_ASN1_OTHER:
  816. case V_ASN1_SET:
  817. case V_ASN1_SEQUENCE:
  818. default:
  819. if (utype == V_ASN1_BMPSTRING && (len & 1)) {
  820. ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
  821. goto err;
  822. }
  823. if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
  824. ASN1err(ASN1_F_ASN1_EX_C2I,
  825. ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
  826. goto err;
  827. }
  828. /* All based on ASN1_STRING and handled the same */
  829. if (!*pval) {
  830. stmp = ASN1_STRING_type_new(utype);
  831. if (stmp == NULL) {
  832. ASN1err(ASN1_F_ASN1_EX_C2I, ERR_R_MALLOC_FAILURE);
  833. goto err;
  834. }
  835. *pval = (ASN1_VALUE *)stmp;
  836. } else {
  837. stmp = (ASN1_STRING *)*pval;
  838. stmp->type = utype;
  839. }
  840. /* If we've already allocated a buffer use it */
  841. if (*free_cont) {
  842. OPENSSL_free(stmp->data);
  843. stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
  844. stmp->length = len;
  845. *free_cont = 0;
  846. } else {
  847. if (!ASN1_STRING_set(stmp, cont, len)) {
  848. ASN1err(ASN1_F_ASN1_EX_C2I, ERR_R_MALLOC_FAILURE);
  849. ASN1_STRING_free(stmp);
  850. *pval = NULL;
  851. goto err;
  852. }
  853. }
  854. break;
  855. }
  856. /* If ASN1_ANY and NULL type fix up value */
  857. if (typ && (utype == V_ASN1_NULL))
  858. typ->value.ptr = NULL;
  859. ret = 1;
  860. err:
  861. if (!ret) {
  862. ASN1_TYPE_free(typ);
  863. if (opval)
  864. *opval = NULL;
  865. }
  866. return ret;
  867. }
  868. /*
  869. * This function finds the end of an ASN1 structure when passed its maximum
  870. * length, whether it is indefinite length and a pointer to the content. This
  871. * is more efficient than calling asn1_collect because it does not recurse on
  872. * each indefinite length header.
  873. */
  874. static int asn1_find_end(const unsigned char **in, long len, char inf)
  875. {
  876. int expected_eoc;
  877. long plen;
  878. const unsigned char *p = *in, *q;
  879. /* If not indefinite length constructed just add length */
  880. if (inf == 0) {
  881. *in += len;
  882. return 1;
  883. }
  884. expected_eoc = 1;
  885. /*
  886. * Indefinite length constructed form. Find the end when enough EOCs are
  887. * found. If more indefinite length constructed headers are encountered
  888. * increment the expected eoc count otherwise just skip to the end of the
  889. * data.
  890. */
  891. while (len > 0) {
  892. if (asn1_check_eoc(&p, len)) {
  893. expected_eoc--;
  894. if (expected_eoc == 0)
  895. break;
  896. len -= 2;
  897. continue;
  898. }
  899. q = p;
  900. /* Just read in a header: only care about the length */
  901. if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
  902. -1, 0, 0, NULL)) {
  903. ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
  904. return 0;
  905. }
  906. if (inf)
  907. expected_eoc++;
  908. else
  909. p += plen;
  910. len -= p - q;
  911. }
  912. if (expected_eoc) {
  913. ASN1err(ASN1_F_ASN1_FIND_END, ASN1_R_MISSING_EOC);
  914. return 0;
  915. }
  916. *in = p;
  917. return 1;
  918. }
  919. /*
  920. * This function collects the asn1 data from a constructred string type into
  921. * a buffer. The values of 'in' and 'len' should refer to the contents of the
  922. * constructed type and 'inf' should be set if it is indefinite length.
  923. */
  924. #ifndef ASN1_MAX_STRING_NEST
  925. /*
  926. * This determines how many levels of recursion are permitted in ASN1 string
  927. * types. If it is not limited stack overflows can occur. If set to zero no
  928. * recursion is allowed at all. Although zero should be adequate examples
  929. * exist that require a value of 1. So 5 should be more than enough.
  930. */
  931. # define ASN1_MAX_STRING_NEST 5
  932. #endif
  933. static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
  934. char inf, int tag, int aclass, int depth)
  935. {
  936. const unsigned char *p, *q;
  937. long plen;
  938. char cst, ininf;
  939. p = *in;
  940. inf &= 1;
  941. /*
  942. * If no buffer and not indefinite length constructed just pass over the
  943. * encoded data
  944. */
  945. if (!buf && !inf) {
  946. *in += len;
  947. return 1;
  948. }
  949. while (len > 0) {
  950. q = p;
  951. /* Check for EOC */
  952. if (asn1_check_eoc(&p, len)) {
  953. /*
  954. * EOC is illegal outside indefinite length constructed form
  955. */
  956. if (!inf) {
  957. ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_UNEXPECTED_EOC);
  958. return 0;
  959. }
  960. inf = 0;
  961. break;
  962. }
  963. if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
  964. len, tag, aclass, 0, NULL)) {
  965. ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
  966. return 0;
  967. }
  968. /* If indefinite length constructed update max length */
  969. if (cst) {
  970. if (depth >= ASN1_MAX_STRING_NEST) {
  971. ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_NESTED_ASN1_STRING);
  972. return 0;
  973. }
  974. if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
  975. return 0;
  976. } else if (plen && !collect_data(buf, &p, plen))
  977. return 0;
  978. len -= p - q;
  979. }
  980. if (inf) {
  981. ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
  982. return 0;
  983. }
  984. *in = p;
  985. return 1;
  986. }
  987. static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
  988. {
  989. int len;
  990. if (buf) {
  991. len = buf->length;
  992. if (!BUF_MEM_grow_clean(buf, len + plen)) {
  993. ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE);
  994. return 0;
  995. }
  996. memcpy(buf->data + len, *p, plen);
  997. }
  998. *p += plen;
  999. return 1;
  1000. }
  1001. /* Check for ASN1 EOC and swallow it if found */
  1002. static int asn1_check_eoc(const unsigned char **in, long len)
  1003. {
  1004. const unsigned char *p;
  1005. if (len < 2)
  1006. return 0;
  1007. p = *in;
  1008. if (!p[0] && !p[1]) {
  1009. *in += 2;
  1010. return 1;
  1011. }
  1012. return 0;
  1013. }
  1014. /*
  1015. * Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the
  1016. * length for indefinite length constructed form, we don't know the exact
  1017. * length but we can set an upper bound to the amount of data available minus
  1018. * the header length just read.
  1019. */
  1020. static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
  1021. char *inf, char *cst,
  1022. const unsigned char **in, long len,
  1023. int exptag, int expclass, char opt, ASN1_TLC *ctx)
  1024. {
  1025. int i;
  1026. int ptag, pclass;
  1027. long plen;
  1028. const unsigned char *p, *q;
  1029. p = *in;
  1030. q = p;
  1031. if (ctx && ctx->valid) {
  1032. i = ctx->ret;
  1033. plen = ctx->plen;
  1034. pclass = ctx->pclass;
  1035. ptag = ctx->ptag;
  1036. p += ctx->hdrlen;
  1037. } else {
  1038. i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
  1039. if (ctx) {
  1040. ctx->ret = i;
  1041. ctx->plen = plen;
  1042. ctx->pclass = pclass;
  1043. ctx->ptag = ptag;
  1044. ctx->hdrlen = p - q;
  1045. ctx->valid = 1;
  1046. /*
  1047. * If definite length, and no error, length + header can't exceed
  1048. * total amount of data available.
  1049. */
  1050. if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) {
  1051. ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_TOO_LONG);
  1052. asn1_tlc_clear(ctx);
  1053. return 0;
  1054. }
  1055. }
  1056. }
  1057. if (i & 0x80) {
  1058. ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
  1059. asn1_tlc_clear(ctx);
  1060. return 0;
  1061. }
  1062. if (exptag >= 0) {
  1063. if ((exptag != ptag) || (expclass != pclass)) {
  1064. /*
  1065. * If type is OPTIONAL, not an error: indicate missing type.
  1066. */
  1067. if (opt)
  1068. return -1;
  1069. asn1_tlc_clear(ctx);
  1070. ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
  1071. return 0;
  1072. }
  1073. /*
  1074. * We have a tag and class match: assume we are going to do something
  1075. * with it
  1076. */
  1077. asn1_tlc_clear(ctx);
  1078. }
  1079. if (i & 1)
  1080. plen = len - (p - q);
  1081. if (inf)
  1082. *inf = i & 1;
  1083. if (cst)
  1084. *cst = i & V_ASN1_CONSTRUCTED;
  1085. if (olen)
  1086. *olen = plen;
  1087. if (oclass)
  1088. *oclass = pclass;
  1089. if (otag)
  1090. *otag = ptag;
  1091. *in = p;
  1092. return 1;
  1093. }