tasn_dec.c 37 KB

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