tasn_dec.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  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. /* 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, 1);
  368. pseqval = asn1_get_field_ptr(pval, seqtt);
  369. ASN1_template_free(pseqval, seqtt);
  370. }
  371. }
  372. /* Get each field entry */
  373. for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
  374. const ASN1_TEMPLATE *seqtt;
  375. ASN1_VALUE **pseqval;
  376. seqtt = asn1_do_adb(pval, tt, 1);
  377. if (!seqtt)
  378. goto err;
  379. pseqval = asn1_get_field_ptr(pval, seqtt);
  380. /* Have we ran out of data? */
  381. if (!len)
  382. break;
  383. q = p;
  384. if (asn1_check_eoc(&p, len)) {
  385. if (!seq_eoc) {
  386. ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_UNEXPECTED_EOC);
  387. goto err;
  388. }
  389. len -= p - q;
  390. seq_eoc = 0;
  391. q = p;
  392. break;
  393. }
  394. /*
  395. * This determines the OPTIONAL flag value. The field cannot be
  396. * omitted if it is the last of a SEQUENCE and there is still
  397. * data to be read. This isn't strictly necessary but it
  398. * increases efficiency in some cases.
  399. */
  400. if (i == (it->tcount - 1))
  401. isopt = 0;
  402. else
  403. isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
  404. /*
  405. * attempt to read in field, allowing each to be OPTIONAL
  406. */
  407. ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx);
  408. if (!ret) {
  409. errtt = seqtt;
  410. goto err;
  411. } else if (ret == -1) {
  412. /*
  413. * OPTIONAL component absent. Free and zero the field.
  414. */
  415. ASN1_template_free(pseqval, seqtt);
  416. continue;
  417. }
  418. /* Update length */
  419. len -= p - q;
  420. }
  421. /* Check for EOC if expecting one */
  422. if (seq_eoc && !asn1_check_eoc(&p, len)) {
  423. ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MISSING_EOC);
  424. goto err;
  425. }
  426. /* Check all data read */
  427. if (!seq_nolen && len) {
  428. ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
  429. goto err;
  430. }
  431. /*
  432. * If we get here we've got no more data in the SEQUENCE, however we
  433. * may not have read all fields so check all remaining are OPTIONAL
  434. * and clear any that are.
  435. */
  436. for (; i < it->tcount; tt++, i++) {
  437. const ASN1_TEMPLATE *seqtt;
  438. seqtt = asn1_do_adb(pval, tt, 1);
  439. if (!seqtt)
  440. goto err;
  441. if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
  442. ASN1_VALUE **pseqval;
  443. pseqval = asn1_get_field_ptr(pval, seqtt);
  444. ASN1_template_free(pseqval, seqtt);
  445. } else {
  446. errtt = seqtt;
  447. ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_FIELD_MISSING);
  448. goto err;
  449. }
  450. }
  451. /* Save encoding */
  452. if (!asn1_enc_save(pval, *in, p - *in, it))
  453. goto auxerr;
  454. if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
  455. goto auxerr;
  456. *in = p;
  457. return 1;
  458. default:
  459. return 0;
  460. }
  461. auxerr:
  462. ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_AUX_ERROR);
  463. err:
  464. if (combine == 0)
  465. ASN1_item_ex_free(pval, it);
  466. if (errtt)
  467. ERR_add_error_data(4, "Field=", errtt->field_name,
  468. ", Type=", it->sname);
  469. else
  470. ERR_add_error_data(2, "Type=", it->sname);
  471. return 0;
  472. }
  473. /*
  474. * Templates are handled with two separate functions. One handles any
  475. * EXPLICIT tag and the other handles the rest.
  476. */
  477. static int asn1_template_ex_d2i(ASN1_VALUE **val,
  478. const unsigned char **in, long inlen,
  479. const ASN1_TEMPLATE *tt, char opt,
  480. ASN1_TLC *ctx)
  481. {
  482. int flags, aclass;
  483. int ret;
  484. long len;
  485. const unsigned char *p, *q;
  486. char exp_eoc;
  487. if (!val)
  488. return 0;
  489. flags = tt->flags;
  490. aclass = flags & ASN1_TFLG_TAG_CLASS;
  491. p = *in;
  492. /* Check if EXPLICIT tag expected */
  493. if (flags & ASN1_TFLG_EXPTAG) {
  494. char cst;
  495. /*
  496. * Need to work out amount of data available to the inner content and
  497. * where it starts: so read in EXPLICIT header to get the info.
  498. */
  499. ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
  500. &p, inlen, tt->tag, aclass, opt, ctx);
  501. q = p;
  502. if (!ret) {
  503. ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
  504. return 0;
  505. } else if (ret == -1)
  506. return -1;
  507. if (!cst) {
  508. ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
  509. ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
  510. return 0;
  511. }
  512. /* We've found the field so it can't be OPTIONAL now */
  513. ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
  514. if (!ret) {
  515. ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
  516. return 0;
  517. }
  518. /* We read the field in OK so update length */
  519. len -= p - q;
  520. if (exp_eoc) {
  521. /* If NDEF we must have an EOC here */
  522. if (!asn1_check_eoc(&p, len)) {
  523. ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ASN1_R_MISSING_EOC);
  524. goto err;
  525. }
  526. } else {
  527. /*
  528. * Otherwise we must hit the EXPLICIT tag end or its an error
  529. */
  530. if (len) {
  531. ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
  532. ASN1_R_EXPLICIT_LENGTH_MISMATCH);
  533. goto err;
  534. }
  535. }
  536. } else
  537. return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx);
  538. *in = p;
  539. return 1;
  540. err:
  541. ASN1_template_free(val, tt);
  542. return 0;
  543. }
  544. static int asn1_template_noexp_d2i(ASN1_VALUE **val,
  545. const unsigned char **in, long len,
  546. const ASN1_TEMPLATE *tt, char opt,
  547. ASN1_TLC *ctx)
  548. {
  549. int flags, aclass;
  550. int ret;
  551. const unsigned char *p, *q;
  552. if (!val)
  553. return 0;
  554. flags = tt->flags;
  555. aclass = flags & ASN1_TFLG_TAG_CLASS;
  556. p = *in;
  557. q = p;
  558. if (flags & ASN1_TFLG_SK_MASK) {
  559. /* SET OF, SEQUENCE OF */
  560. int sktag, skaclass;
  561. char sk_eoc;
  562. /* First work out expected inner tag value */
  563. if (flags & ASN1_TFLG_IMPTAG) {
  564. sktag = tt->tag;
  565. skaclass = aclass;
  566. } else {
  567. skaclass = V_ASN1_UNIVERSAL;
  568. if (flags & ASN1_TFLG_SET_OF)
  569. sktag = V_ASN1_SET;
  570. else
  571. sktag = V_ASN1_SEQUENCE;
  572. }
  573. /* Get the tag */
  574. ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
  575. &p, len, sktag, skaclass, opt, ctx);
  576. if (!ret) {
  577. ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
  578. return 0;
  579. } else if (ret == -1)
  580. return -1;
  581. if (!*val)
  582. *val = (ASN1_VALUE *)sk_new_null();
  583. else {
  584. /*
  585. * We've got a valid STACK: free up any items present
  586. */
  587. STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
  588. ASN1_VALUE *vtmp;
  589. while (sk_ASN1_VALUE_num(sktmp) > 0) {
  590. vtmp = sk_ASN1_VALUE_pop(sktmp);
  591. ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item));
  592. }
  593. }
  594. if (!*val) {
  595. ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_MALLOC_FAILURE);
  596. goto err;
  597. }
  598. /* Read as many items as we can */
  599. while (len > 0) {
  600. ASN1_VALUE *skfield;
  601. q = p;
  602. /* See if EOC found */
  603. if (asn1_check_eoc(&p, len)) {
  604. if (!sk_eoc) {
  605. ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
  606. ASN1_R_UNEXPECTED_EOC);
  607. goto err;
  608. }
  609. len -= p - q;
  610. sk_eoc = 0;
  611. break;
  612. }
  613. skfield = NULL;
  614. if (!ASN1_item_ex_d2i(&skfield, &p, len,
  615. ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx)) {
  616. ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
  617. ERR_R_NESTED_ASN1_ERROR);
  618. goto err;
  619. }
  620. len -= p - q;
  621. if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) {
  622. ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_MALLOC_FAILURE);
  623. goto err;
  624. }
  625. }
  626. if (sk_eoc) {
  627. ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC);
  628. goto err;
  629. }
  630. } else if (flags & ASN1_TFLG_IMPTAG) {
  631. /* IMPLICIT tagging */
  632. ret = ASN1_item_ex_d2i(val, &p, len,
  633. ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
  634. ctx);
  635. if (!ret) {
  636. ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
  637. goto err;
  638. } else if (ret == -1)
  639. return -1;
  640. } else {
  641. /* Nothing special */
  642. ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
  643. -1, tt->flags & ASN1_TFLG_COMBINE, opt, ctx);
  644. if (!ret) {
  645. ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
  646. goto err;
  647. } else if (ret == -1)
  648. return -1;
  649. }
  650. *in = p;
  651. return 1;
  652. err:
  653. ASN1_template_free(val, tt);
  654. return 0;
  655. }
  656. static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
  657. const unsigned char **in, long inlen,
  658. const ASN1_ITEM *it,
  659. int tag, int aclass, char opt, ASN1_TLC *ctx)
  660. {
  661. int ret = 0, utype;
  662. long plen;
  663. char cst, inf, free_cont = 0;
  664. const unsigned char *p;
  665. BUF_MEM buf = { 0, NULL, 0 };
  666. const unsigned char *cont = NULL;
  667. long len;
  668. if (!pval) {
  669. ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
  670. return 0; /* Should never happen */
  671. }
  672. if (it->itype == ASN1_ITYPE_MSTRING) {
  673. utype = tag;
  674. tag = -1;
  675. } else
  676. utype = it->utype;
  677. if (utype == V_ASN1_ANY) {
  678. /* If type is ANY need to figure out type from tag */
  679. unsigned char oclass;
  680. if (tag >= 0) {
  681. ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_TAGGED_ANY);
  682. return 0;
  683. }
  684. if (opt) {
  685. ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
  686. ASN1_R_ILLEGAL_OPTIONAL_ANY);
  687. return 0;
  688. }
  689. p = *in;
  690. ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
  691. &p, inlen, -1, 0, 0, ctx);
  692. if (!ret) {
  693. ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
  694. return 0;
  695. }
  696. if (oclass != V_ASN1_UNIVERSAL)
  697. utype = V_ASN1_OTHER;
  698. }
  699. if (tag == -1) {
  700. tag = utype;
  701. aclass = V_ASN1_UNIVERSAL;
  702. }
  703. p = *in;
  704. /* Check header */
  705. ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
  706. &p, inlen, tag, aclass, opt, ctx);
  707. if (!ret) {
  708. ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
  709. return 0;
  710. } else if (ret == -1)
  711. return -1;
  712. ret = 0;
  713. /* SEQUENCE, SET and "OTHER" are left in encoded form */
  714. if ((utype == V_ASN1_SEQUENCE)
  715. || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) {
  716. /*
  717. * Clear context cache for type OTHER because the auto clear when we
  718. * have a exact match wont work
  719. */
  720. if (utype == V_ASN1_OTHER) {
  721. asn1_tlc_clear(ctx);
  722. }
  723. /* SEQUENCE and SET must be constructed */
  724. else if (!cst) {
  725. ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
  726. ASN1_R_TYPE_NOT_CONSTRUCTED);
  727. return 0;
  728. }
  729. cont = *in;
  730. /* If indefinite length constructed find the real end */
  731. if (inf) {
  732. if (!asn1_find_end(&p, plen, inf))
  733. goto err;
  734. len = p - cont;
  735. } else {
  736. len = p - cont + plen;
  737. p += plen;
  738. }
  739. } else if (cst) {
  740. if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
  741. || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
  742. || utype == V_ASN1_ENUMERATED) {
  743. ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_TYPE_NOT_PRIMITIVE);
  744. return 0;
  745. }
  746. /* Free any returned 'buf' content */
  747. free_cont = 1;
  748. /*
  749. * Should really check the internal tags are correct but some things
  750. * may get this wrong. The relevant specs say that constructed string
  751. * types should be OCTET STRINGs internally irrespective of the type.
  752. * So instead just check for UNIVERSAL class and ignore the tag.
  753. */
  754. if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
  755. goto err;
  756. }
  757. len = buf.length;
  758. /* Append a final null to string */
  759. if (!BUF_MEM_grow_clean(&buf, len + 1)) {
  760. ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_MALLOC_FAILURE);
  761. goto err;
  762. }
  763. buf.data[len] = 0;
  764. cont = (const unsigned char *)buf.data;
  765. } else {
  766. cont = p;
  767. len = plen;
  768. p += plen;
  769. }
  770. /* We now have content length and type: translate into a structure */
  771. /* asn1_ex_c2i may reuse allocated buffer, and so sets free_cont to 0 */
  772. if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
  773. goto err;
  774. *in = p;
  775. ret = 1;
  776. err:
  777. if (free_cont && buf.data)
  778. OPENSSL_free(buf.data);
  779. return ret;
  780. }
  781. /* Translate ASN1 content octets into a structure */
  782. int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
  783. int utype, char *free_cont, const ASN1_ITEM *it)
  784. {
  785. ASN1_VALUE **opval = NULL;
  786. ASN1_STRING *stmp;
  787. ASN1_TYPE *typ = NULL;
  788. int ret = 0;
  789. const ASN1_PRIMITIVE_FUNCS *pf;
  790. ASN1_INTEGER **tint;
  791. pf = it->funcs;
  792. if (pf && pf->prim_c2i)
  793. return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
  794. /* If ANY type clear type and set pointer to internal value */
  795. if (it->utype == V_ASN1_ANY) {
  796. if (!*pval) {
  797. typ = ASN1_TYPE_new();
  798. if (typ == NULL)
  799. goto err;
  800. *pval = (ASN1_VALUE *)typ;
  801. } else
  802. typ = (ASN1_TYPE *)*pval;
  803. if (utype != typ->type)
  804. ASN1_TYPE_set(typ, utype, NULL);
  805. opval = pval;
  806. pval = &typ->value.asn1_value;
  807. }
  808. switch (utype) {
  809. case V_ASN1_OBJECT:
  810. if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
  811. goto err;
  812. break;
  813. case V_ASN1_NULL:
  814. if (len) {
  815. ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_NULL_IS_WRONG_LENGTH);
  816. goto err;
  817. }
  818. *pval = (ASN1_VALUE *)1;
  819. break;
  820. case V_ASN1_BOOLEAN:
  821. if (len != 1) {
  822. ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
  823. goto err;
  824. } else {
  825. ASN1_BOOLEAN *tbool;
  826. tbool = (ASN1_BOOLEAN *)pval;
  827. *tbool = *cont;
  828. }
  829. break;
  830. case V_ASN1_BIT_STRING:
  831. if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
  832. goto err;
  833. break;
  834. case V_ASN1_INTEGER:
  835. case V_ASN1_NEG_INTEGER:
  836. case V_ASN1_ENUMERATED:
  837. case V_ASN1_NEG_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. }