tasn_dec.c 39 KB

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