tasn_dec.c 40 KB

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