tasn_dec.c 39 KB

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