You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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