summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/tasn_dec.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/asn1/tasn_dec.c')
-rw-r--r--src/lib/libcrypto/asn1/tasn_dec.c392
1 files changed, 195 insertions, 197 deletions
diff --git a/src/lib/libcrypto/asn1/tasn_dec.c b/src/lib/libcrypto/asn1/tasn_dec.c
index 1a06ddc185..e9059df982 100644
--- a/src/lib/libcrypto/asn1/tasn_dec.c
+++ b/src/lib/libcrypto/asn1/tasn_dec.c
@@ -10,7 +10,7 @@
10 * are met: 10 * are met:
11 * 11 *
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 14 *
15 * 2. Redistributions in binary form must reproduce the above copyright 15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in 16 * notice, this list of conditions and the following disclaimer in
@@ -69,45 +69,40 @@ static int asn1_check_eoc(const unsigned char **in, long len);
69static int asn1_find_end(const unsigned char **in, long len, char inf); 69static int asn1_find_end(const unsigned char **in, long len, char inf);
70 70
71static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len, 71static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
72 char inf, int tag, int aclass, int depth); 72 char inf, int tag, int aclass, int depth);
73 73
74static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen); 74static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen);
75 75
76static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass, 76static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
77 char *inf, char *cst, 77 char *inf, char *cst, const unsigned char **in, long len, int exptag,
78 const unsigned char **in, long len, 78 int expclass, char opt, ASN1_TLC *ctx);
79 int exptag, int expclass, char opt, 79
80 ASN1_TLC *ctx); 80static int asn1_template_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
81 81 long len, const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx);
82static int asn1_template_ex_d2i(ASN1_VALUE **pval, 82static int asn1_template_noexp_d2i(ASN1_VALUE **val, const unsigned char **in,
83 const unsigned char **in, long len, 83 long len, const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx);
84 const ASN1_TEMPLATE *tt, char opt, 84static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, const unsigned char **in,
85 ASN1_TLC *ctx); 85 long len, const ASN1_ITEM *it, int tag, int aclass, char opt,
86static int asn1_template_noexp_d2i(ASN1_VALUE **val, 86 ASN1_TLC *ctx);
87 const unsigned char **in, long len,
88 const ASN1_TEMPLATE *tt, char opt,
89 ASN1_TLC *ctx);
90static 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, ASN1_TLC *ctx);
94 87
95/* Table to convert tags to bit values, used for MSTRING type */ 88/* Table to convert tags to bit values, used for MSTRING type */
96static const unsigned long tag2bit[32] = { 89static const unsigned long tag2bit[32] = {
970, 0, 0, B_ASN1_BIT_STRING, /* tags 0 - 3 */ 90 0, 0, 0, B_ASN1_BIT_STRING, /* tags 0 - 3 */
98B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,/* tags 4- 7 */ 91 B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,/* tags 4- 7 */
99B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,/* tags 8-11 */ 92 B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,/* tags 8-11 */
100B_ASN1_UTF8STRING,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,/* tags 12-15 */ 93 B_ASN1_UTF8STRING,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,/* tags 12-15 */
101B_ASN1_SEQUENCE,0,B_ASN1_NUMERICSTRING,B_ASN1_PRINTABLESTRING, /* tags 16-19 */ 94 B_ASN1_SEQUENCE,0,B_ASN1_NUMERICSTRING,B_ASN1_PRINTABLESTRING, /* tags 16-19 */
102B_ASN1_T61STRING,B_ASN1_VIDEOTEXSTRING,B_ASN1_IA5STRING, /* tags 20-22 */ 95 B_ASN1_T61STRING,B_ASN1_VIDEOTEXSTRING,B_ASN1_IA5STRING, /* tags 20-22 */
103B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME, /* tags 23-24 */ 96 B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME, /* tags 23-24 */
104B_ASN1_GRAPHICSTRING,B_ASN1_ISO64STRING,B_ASN1_GENERALSTRING, /* tags 25-27 */ 97 B_ASN1_GRAPHICSTRING,B_ASN1_ISO64STRING,B_ASN1_GENERALSTRING, /* tags 25-27 */
105B_ASN1_UNIVERSALSTRING,B_ASN1_UNKNOWN,B_ASN1_BMPSTRING,B_ASN1_UNKNOWN, /* tags 28-31 */ 98 B_ASN1_UNIVERSALSTRING,B_ASN1_UNKNOWN,B_ASN1_BMPSTRING,B_ASN1_UNKNOWN, /* tags 28-31 */
106}; 99};
107 100
108unsigned long ASN1_tag2bit(int tag) 101unsigned long
102ASN1_tag2bit(int tag)
109{ 103{
110 if ((tag < 0) || (tag > 30)) return 0; 104 if ((tag < 0) || (tag > 30))
105 return 0;
111 return tag2bit[tag]; 106 return tag2bit[tag];
112} 107}
113 108
@@ -117,29 +112,31 @@ unsigned long ASN1_tag2bit(int tag)
117/* Version to avoid compiler warning about 'c' always non-NULL */ 112/* Version to avoid compiler warning about 'c' always non-NULL */
118#define asn1_tlc_clear_nc(c) (c)->valid = 0 113#define asn1_tlc_clear_nc(c) (c)->valid = 0
119 114
120/* Decode an ASN1 item, this currently behaves just 115/* Decode an ASN1 item, this currently behaves just
121 * like a standard 'd2i' function. 'in' points to 116 * like a standard 'd2i' function. 'in' points to
122 * a buffer to read the data from, in future we will 117 * a buffer to read the data from, in future we will
123 * have more advanced versions that can input data 118 * have more advanced versions that can input data
124 * a piece at a time and this will simply be a special 119 * a piece at a time and this will simply be a special
125 * case. 120 * case.
126 */ 121 */
127 122
128ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval, 123ASN1_VALUE *
129 const unsigned char **in, long len, const ASN1_ITEM *it) 124ASN1_item_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
125 const ASN1_ITEM *it)
130{ 126{
131 ASN1_TLC c; 127 ASN1_TLC c;
132 ASN1_VALUE *ptmpval = NULL; 128 ASN1_VALUE *ptmpval = NULL;
133 if (!pval) 129 if (!pval)
134 pval = &ptmpval; 130 pval = &ptmpval;
135 asn1_tlc_clear_nc(&c); 131 asn1_tlc_clear_nc(&c);
136 if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0) 132 if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
137 return *pval; 133 return *pval;
138 return NULL; 134 return NULL;
139} 135}
140 136
141int ASN1_template_d2i(ASN1_VALUE **pval, 137int
142 const unsigned char **in, long len, const ASN1_TEMPLATE *tt) 138ASN1_template_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
139 const ASN1_TEMPLATE *tt)
143{ 140{
144 ASN1_TLC c; 141 ASN1_TLC c;
145 asn1_tlc_clear_nc(&c); 142 asn1_tlc_clear_nc(&c);
@@ -151,9 +148,9 @@ int ASN1_template_d2i(ASN1_VALUE **pval,
151 * If 'opt' set and tag mismatch return -1 to handle OPTIONAL 148 * If 'opt' set and tag mismatch return -1 to handle OPTIONAL
152 */ 149 */
153 150
154int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, 151int
155 const ASN1_ITEM *it, 152ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
156 int tag, int aclass, char opt, ASN1_TLC *ctx) 153 const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx)
157{ 154{
158 const ASN1_TEMPLATE *tt, *errtt = NULL; 155 const ASN1_TEMPLATE *tt, *errtt = NULL;
159 const ASN1_COMPAT_FUNCS *cf; 156 const ASN1_COMPAT_FUNCS *cf;
@@ -161,7 +158,7 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
161 const ASN1_AUX *aux = it->funcs; 158 const ASN1_AUX *aux = it->funcs;
162 ASN1_aux_cb *asn1_cb; 159 ASN1_aux_cb *asn1_cb;
163 const unsigned char *p = NULL, *q; 160 const unsigned char *p = NULL, *q;
164 unsigned char *wp=NULL; /* BIG FAT WARNING! BREAKS CONST WHERE USED */ 161 unsigned char *wp = NULL; /* BIG FAT WARNING! BREAKS CONST WHERE USED */
165 unsigned char imphack = 0, oclass; 162 unsigned char imphack = 0, oclass;
166 char seq_eoc, seq_nolen, cst, isopt; 163 char seq_eoc, seq_nolen, cst, isopt;
167 long tmplen; 164 long tmplen;
@@ -173,9 +170,10 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
173 return 0; 170 return 0;
174 if (aux && aux->asn1_cb) 171 if (aux && aux->asn1_cb)
175 asn1_cb = aux->asn1_cb; 172 asn1_cb = aux->asn1_cb;
176 else asn1_cb = 0; 173 else
174 asn1_cb = 0;
177 175
178 switch(it->itype) { 176 switch (it->itype) {
179 case ASN1_ITYPE_PRIMITIVE: 177 case ASN1_ITYPE_PRIMITIVE:
180 if (it->templates) { 178 if (it->templates) {
181 /* tagging or OPTIONAL is currently illegal on an item 179 /* tagging or OPTIONAL is currently illegal on an item
@@ -186,33 +184,34 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
186 */ 184 */
187 if ((tag != -1) || opt) { 185 if ((tag != -1) || opt) {
188 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 186 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
189 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE); 187 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
190 goto err; 188 goto err;
191 } 189 }
192 return asn1_template_ex_d2i(pval, in, len, 190 return asn1_template_ex_d2i(pval, in, len,
193 it->templates, opt, ctx); 191 it->templates, opt, ctx);
194 } 192 }
195 return asn1_d2i_ex_primitive(pval, in, len, it, 193 return asn1_d2i_ex_primitive(pval, in, len, it,
196 tag, aclass, opt, ctx); 194 tag, aclass, opt, ctx);
197 break; 195 break;
198 196
199 case ASN1_ITYPE_MSTRING: 197 case ASN1_ITYPE_MSTRING:
200 p = *in; 198 p = *in;
201 /* Just read in tag and class */ 199 /* Just read in tag and class */
202 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL, 200 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
203 &p, len, -1, 0, 1, ctx); 201 &p, len, -1, 0, 1, ctx);
204 if (!ret) { 202 if (!ret) {
205 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 203 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
206 ERR_R_NESTED_ASN1_ERROR); 204 ERR_R_NESTED_ASN1_ERROR);
207 goto err; 205 goto err;
208 } 206 }
209 207
210 /* Must be UNIVERSAL class */ 208 /* Must be UNIVERSAL class */
211 if (oclass != V_ASN1_UNIVERSAL) { 209 if (oclass != V_ASN1_UNIVERSAL) {
212 /* If OPTIONAL, assume this is OK */ 210 /* If OPTIONAL, assume this is OK */
213 if (opt) return -1; 211 if (opt)
212 return -1;
214 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 213 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
215 ASN1_R_MSTRING_NOT_UNIVERSAL); 214 ASN1_R_MSTRING_NOT_UNIVERSAL);
216 goto err; 215 goto err;
217 } 216 }
218 /* Check tag matches bit map */ 217 /* Check tag matches bit map */
@@ -221,17 +220,17 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
221 if (opt) 220 if (opt)
222 return -1; 221 return -1;
223 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 222 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
224 ASN1_R_MSTRING_WRONG_TAG); 223 ASN1_R_MSTRING_WRONG_TAG);
225 goto err; 224 goto err;
226 } 225 }
227 return asn1_d2i_ex_primitive(pval, in, len, 226 return asn1_d2i_ex_primitive(pval, in, len,
228 it, otag, 0, 0, ctx); 227 it, otag, 0, 0, ctx);
229 228
230 case ASN1_ITYPE_EXTERN: 229 case ASN1_ITYPE_EXTERN:
231 /* Use new style d2i */ 230 /* Use new style d2i */
232 ef = it->funcs; 231 ef = it->funcs;
233 return ef->asn1_ex_d2i(pval, in, len, 232 return ef->asn1_ex_d2i(pval, in, len,
234 it, tag, aclass, opt, ctx); 233 it, tag, aclass, opt, ctx);
235 234
236 case ASN1_ITYPE_COMPAT: 235 case ASN1_ITYPE_COMPAT:
237 /* we must resort to old style evil hackery */ 236 /* we must resort to old style evil hackery */
@@ -243,15 +242,16 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
243 p = *in; 242 p = *in;
244 if (tag == -1) 243 if (tag == -1)
245 exptag = it->utype; 244 exptag = it->utype;
246 else exptag = tag; 245 else
246 exptag = tag;
247 /* Don't care about anything other than presence 247 /* Don't care about anything other than presence
248 * of expected tag */ 248 * of expected tag */
249 249
250 ret = asn1_check_tlen(NULL, NULL, NULL, NULL, NULL, 250 ret = asn1_check_tlen(NULL, NULL, NULL, NULL, NULL,
251 &p, len, exptag, aclass, 1, ctx); 251 &p, len, exptag, aclass, 1, ctx);
252 if (!ret) { 252 if (!ret) {
253 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 253 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
254 ERR_R_NESTED_ASN1_ERROR); 254 ERR_R_NESTED_ASN1_ERROR);
255 goto err; 255 goto err;
256 } 256 }
257 if (ret == -1) 257 if (ret == -1)
@@ -278,11 +278,11 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
278 imphack = *wp; 278 imphack = *wp;
279 if (p == NULL) { 279 if (p == NULL) {
280 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 280 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
281 ERR_R_NESTED_ASN1_ERROR); 281 ERR_R_NESTED_ASN1_ERROR);
282 goto err; 282 goto err;
283 } 283 }
284 *wp = (unsigned char)((*p & V_ASN1_CONSTRUCTED) 284 *wp = (unsigned char)((*p & V_ASN1_CONSTRUCTED) |
285 | it->utype); 285 it->utype);
286 } 286 }
287 287
288 ptmpval = cf->asn1_d2i(pval, in, len); 288 ptmpval = cf->asn1_d2i(pval, in, len);
@@ -296,20 +296,19 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
296 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR); 296 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
297 goto err; 297 goto err;
298 298
299
300 case ASN1_ITYPE_CHOICE: 299 case ASN1_ITYPE_CHOICE:
301 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL)) 300 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
302 goto auxerr; 301 goto auxerr;
303 302
304 /* Allocate structure */ 303 /* Allocate structure */
305 if (!*pval && !ASN1_item_ex_new(pval, it)) { 304 if (!*pval && !ASN1_item_ex_new(pval, it)) {
306 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 305 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
307 ERR_R_NESTED_ASN1_ERROR); 306 ERR_R_NESTED_ASN1_ERROR);
308 goto err; 307 goto err;
309 } 308 }
310 /* CHOICE type, try each possibility in turn */ 309 /* CHOICE type, try each possibility in turn */
311 p = *in; 310 p = *in;
312 for (i = 0, tt=it->templates; i < it->tcount; i++, tt++) { 311 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
313 pchptr = asn1_get_field_ptr(pval, tt); 312 pchptr = asn1_get_field_ptr(pval, tt);
314 /* We mark field as OPTIONAL so its absence 313 /* We mark field as OPTIONAL so its absence
315 * can be recognised. 314 * can be recognised.
@@ -324,7 +323,7 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
324 /* Otherwise must be an ASN1 parsing error */ 323 /* Otherwise must be an ASN1 parsing error */
325 errtt = tt; 324 errtt = tt;
326 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 325 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
327 ERR_R_NESTED_ASN1_ERROR); 326 ERR_R_NESTED_ASN1_ERROR);
328 goto err; 327 goto err;
329 } 328 }
330 329
@@ -337,14 +336,14 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
337 return -1; 336 return -1;
338 } 337 }
339 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 338 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
340 ASN1_R_NO_MATCHING_CHOICE_TYPE); 339 ASN1_R_NO_MATCHING_CHOICE_TYPE);
341 goto err; 340 goto err;
342 } 341 }
343 342
344 asn1_set_choice_selector(pval, i, it); 343 asn1_set_choice_selector(pval, i, it);
345 *in = p; 344 *in = p;
346 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL)) 345 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
347 goto auxerr; 346 goto auxerr;
348 return 1; 347 return 1;
349 348
350 case ASN1_ITYPE_NDEF_SEQUENCE: 349 case ASN1_ITYPE_NDEF_SEQUENCE:
@@ -359,34 +358,34 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
359 } 358 }
360 /* Get SEQUENCE length and update len, p */ 359 /* Get SEQUENCE length and update len, p */
361 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst, 360 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
362 &p, len, tag, aclass, opt, ctx); 361 &p, len, tag, aclass, opt, ctx);
363 if (!ret) { 362 if (!ret) {
364 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 363 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
365 ERR_R_NESTED_ASN1_ERROR); 364 ERR_R_NESTED_ASN1_ERROR);
366 goto err; 365 goto err;
367 } 366 } else if (ret == -1)
368 else if (ret == -1)
369 return -1; 367 return -1;
370 if (aux && (aux->flags & ASN1_AFLG_BROKEN)) { 368 if (aux && (aux->flags & ASN1_AFLG_BROKEN)) {
371 len = tmplen - (p - *in); 369 len = tmplen - (p - *in);
372 seq_nolen = 1; 370 seq_nolen = 1;
373 } 371 }
374 /* If indefinite we don't do a length check */ 372 /* If indefinite we don't do a length check */
375 else seq_nolen = seq_eoc; 373 else
374 seq_nolen = seq_eoc;
376 if (!cst) { 375 if (!cst) {
377 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 376 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
378 ASN1_R_SEQUENCE_NOT_CONSTRUCTED); 377 ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
379 goto err; 378 goto err;
380 } 379 }
381 380
382 if (!*pval && !ASN1_item_ex_new(pval, it)) { 381 if (!*pval && !ASN1_item_ex_new(pval, it)) {
383 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 382 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
384 ERR_R_NESTED_ASN1_ERROR); 383 ERR_R_NESTED_ASN1_ERROR);
385 goto err; 384 goto err;
386 } 385 }
387 386
388 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL)) 387 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
389 goto auxerr; 388 goto auxerr;
390 389
391 /* Get each field entry */ 390 /* Get each field entry */
392 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) { 391 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
@@ -403,7 +402,7 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
403 if (asn1_check_eoc(&p, len)) { 402 if (asn1_check_eoc(&p, len)) {
404 if (!seq_eoc) { 403 if (!seq_eoc) {
405 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 404 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
406 ASN1_R_UNEXPECTED_EOC); 405 ASN1_R_UNEXPECTED_EOC);
407 goto err; 406 goto err;
408 } 407 }
409 len -= p - q; 408 len -= p - q;
@@ -419,12 +418,13 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
419 */ 418 */
420 if (i == (it->tcount - 1)) 419 if (i == (it->tcount - 1))
421 isopt = 0; 420 isopt = 0;
422 else isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL); 421 else
422 isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
423 /* attempt to read in field, allowing each to be 423 /* attempt to read in field, allowing each to be
424 * OPTIONAL */ 424 * OPTIONAL */
425 425
426 ret = asn1_template_ex_d2i(pseqval, &p, len, 426 ret = asn1_template_ex_d2i(pseqval, &p, len,
427 seqtt, isopt, ctx); 427 seqtt, isopt, ctx);
428 if (!ret) { 428 if (!ret) {
429 errtt = seqtt; 429 errtt = seqtt;
430 goto err; 430 goto err;
@@ -447,7 +447,7 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
447 /* Check all data read */ 447 /* Check all data read */
448 if (!seq_nolen && len) { 448 if (!seq_nolen && len) {
449 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 449 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
450 ASN1_R_SEQUENCE_LENGTH_MISMATCH); 450 ASN1_R_SEQUENCE_LENGTH_MISMATCH);
451 goto err; 451 goto err;
452 } 452 }
453 453
@@ -467,7 +467,7 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
467 } else { 467 } else {
468 errtt = seqtt; 468 errtt = seqtt;
469 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 469 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
470 ASN1_R_FIELD_MISSING); 470 ASN1_R_FIELD_MISSING);
471 goto err; 471 goto err;
472 } 472 }
473 } 473 }
@@ -476,19 +476,20 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
476 goto auxerr; 476 goto auxerr;
477 *in = p; 477 *in = p;
478 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL)) 478 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
479 goto auxerr; 479 goto auxerr;
480 return 1; 480 return 1;
481 481
482 default: 482 default:
483 return 0; 483 return 0;
484 } 484 }
485 auxerr: 485
486auxerr:
486 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_AUX_ERROR); 487 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_AUX_ERROR);
487 err: 488err:
488 ASN1_item_ex_free(pval, it); 489 ASN1_item_ex_free(pval, it);
489 if (errtt) 490 if (errtt)
490 ERR_add_error_data(4, "Field=", errtt->field_name, 491 ERR_add_error_data(4, "Field=", errtt->field_name,
491 ", Type=", it->sname); 492 ", Type=", it->sname);
492 else 493 else
493 ERR_add_error_data(2, "Type=", it->sname); 494 ERR_add_error_data(2, "Type=", it->sname);
494 return 0; 495 return 0;
@@ -498,16 +499,16 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
498 * One handles any EXPLICIT tag and the other handles the rest. 499 * One handles any EXPLICIT tag and the other handles the rest.
499 */ 500 */
500 501
501static int asn1_template_ex_d2i(ASN1_VALUE **val, 502static int
502 const unsigned char **in, long inlen, 503asn1_template_ex_d2i(ASN1_VALUE **val, const unsigned char **in, long inlen,
503 const ASN1_TEMPLATE *tt, char opt, 504 const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx)
504 ASN1_TLC *ctx)
505{ 505{
506 int flags, aclass; 506 int flags, aclass;
507 int ret; 507 int ret;
508 long len; 508 long len;
509 const unsigned char *p, *q; 509 const unsigned char *p, *q;
510 char exp_eoc; 510 char exp_eoc;
511
511 if (!val) 512 if (!val)
512 return 0; 513 return 0;
513 flags = tt->flags; 514 flags = tt->flags;
@@ -523,24 +524,24 @@ static int asn1_template_ex_d2i(ASN1_VALUE **val,
523 * get the info. 524 * get the info.
524 */ 525 */
525 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst, 526 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
526 &p, inlen, tt->tag, aclass, opt, ctx); 527 &p, inlen, tt->tag, aclass, opt, ctx);
527 q = p; 528 q = p;
528 if (!ret) { 529 if (!ret) {
529 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, 530 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
530 ERR_R_NESTED_ASN1_ERROR); 531 ERR_R_NESTED_ASN1_ERROR);
531 return 0; 532 return 0;
532 } else if (ret == -1) 533 } else if (ret == -1)
533 return -1; 534 return -1;
534 if (!cst) { 535 if (!cst) {
535 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, 536 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
536 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED); 537 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
537 return 0; 538 return 0;
538 } 539 }
539 /* We've found the field so it can't be OPTIONAL now */ 540 /* We've found the field so it can't be OPTIONAL now */
540 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx); 541 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
541 if (!ret) { 542 if (!ret) {
542 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, 543 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
543 ERR_R_NESTED_ASN1_ERROR); 544 ERR_R_NESTED_ASN1_ERROR);
544 return 0; 545 return 0;
545 } 546 }
546 /* We read the field in OK so update length */ 547 /* We read the field in OK so update length */
@@ -549,7 +550,7 @@ static int asn1_template_ex_d2i(ASN1_VALUE **val,
549 /* If NDEF we must have an EOC here */ 550 /* If NDEF we must have an EOC here */
550 if (!asn1_check_eoc(&p, len)) { 551 if (!asn1_check_eoc(&p, len)) {
551 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, 552 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
552 ASN1_R_MISSING_EOC); 553 ASN1_R_MISSING_EOC);
553 goto err; 554 goto err;
554 } 555 }
555 } else { 556 } else {
@@ -557,31 +558,29 @@ static int asn1_template_ex_d2i(ASN1_VALUE **val,
557 * an error */ 558 * an error */
558 if (len) { 559 if (len) {
559 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, 560 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
560 ASN1_R_EXPLICIT_LENGTH_MISMATCH); 561 ASN1_R_EXPLICIT_LENGTH_MISMATCH);
561 goto err; 562 goto err;
562 } 563 }
563 } 564 }
564 } 565 } else
565 else 566 return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx);
566 return asn1_template_noexp_d2i(val, in, inlen,
567 tt, opt, ctx);
568 567
569 *in = p; 568 *in = p;
570 return 1; 569 return 1;
571 570
572 err: 571err:
573 ASN1_template_free(val, tt); 572 ASN1_template_free(val, tt);
574 return 0; 573 return 0;
575} 574}
576 575
577static int asn1_template_noexp_d2i(ASN1_VALUE **val, 576static int
578 const unsigned char **in, long len, 577asn1_template_noexp_d2i(ASN1_VALUE **val, const unsigned char **in, long len,
579 const ASN1_TEMPLATE *tt, char opt, 578 const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx)
580 ASN1_TLC *ctx)
581{ 579{
582 int flags, aclass; 580 int flags, aclass;
583 int ret; 581 int ret;
584 const unsigned char *p, *q; 582 const unsigned char *p, *q;
583
585 if (!val) 584 if (!val)
586 return 0; 585 return 0;
587 flags = tt->flags; 586 flags = tt->flags;
@@ -607,43 +606,42 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val,
607 } 606 }
608 /* Get the tag */ 607 /* Get the tag */
609 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL, 608 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
610 &p, len, sktag, skaclass, opt, ctx); 609 &p, len, sktag, skaclass, opt, ctx);
611 if (!ret) { 610 if (!ret) {
612 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 611 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
613 ERR_R_NESTED_ASN1_ERROR); 612 ERR_R_NESTED_ASN1_ERROR);
614 return 0; 613 return 0;
615 } 614 } else if (ret == -1)
616 else if (ret == -1)
617 return -1; 615 return -1;
618 if (!*val) 616 if (!*val)
619 *val = (ASN1_VALUE *)sk_new_null(); 617 *val = (ASN1_VALUE *)sk_new_null();
620 else { 618 else {
621 /* We've got a valid STACK: free up any items present */ 619 /* We've got a valid STACK: free up any items present */
622 STACK_OF(ASN1_VALUE) *sktmp 620 STACK_OF(ASN1_VALUE) *sktmp =
623 = (STACK_OF(ASN1_VALUE) *)*val; 621 (STACK_OF(ASN1_VALUE) *)*val;
624 ASN1_VALUE *vtmp; 622 ASN1_VALUE *vtmp;
625 while(sk_ASN1_VALUE_num(sktmp) > 0) { 623 while (sk_ASN1_VALUE_num(sktmp) > 0) {
626 vtmp = sk_ASN1_VALUE_pop(sktmp); 624 vtmp = sk_ASN1_VALUE_pop(sktmp);
627 ASN1_item_ex_free(&vtmp, 625 ASN1_item_ex_free(&vtmp,
628 ASN1_ITEM_ptr(tt->item)); 626 ASN1_ITEM_ptr(tt->item));
629 } 627 }
630 } 628 }
631 629
632 if (!*val) { 630 if (!*val) {
633 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 631 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
634 ERR_R_MALLOC_FAILURE); 632 ERR_R_MALLOC_FAILURE);
635 goto err; 633 goto err;
636 } 634 }
637 635
638 /* Read as many items as we can */ 636 /* Read as many items as we can */
639 while(len > 0) { 637 while (len > 0) {
640 ASN1_VALUE *skfield; 638 ASN1_VALUE *skfield;
641 q = p; 639 q = p;
642 /* See if EOC found */ 640 /* See if EOC found */
643 if (asn1_check_eoc(&p, len)) { 641 if (asn1_check_eoc(&p, len)) {
644 if (!sk_eoc) { 642 if (!sk_eoc) {
645 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 643 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
646 ASN1_R_UNEXPECTED_EOC); 644 ASN1_R_UNEXPECTED_EOC);
647 goto err; 645 goto err;
648 } 646 }
649 len -= p - q; 647 len -= p - q;
@@ -652,60 +650,57 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val,
652 } 650 }
653 skfield = NULL; 651 skfield = NULL;
654 if (!ASN1_item_ex_d2i(&skfield, &p, len, 652 if (!ASN1_item_ex_d2i(&skfield, &p, len,
655 ASN1_ITEM_ptr(tt->item), 653 ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx)) {
656 -1, 0, 0, ctx)) {
657 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 654 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
658 ERR_R_NESTED_ASN1_ERROR); 655 ERR_R_NESTED_ASN1_ERROR);
659 goto err; 656 goto err;
660 } 657 }
661 len -= p - q; 658 len -= p - q;
662 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, 659 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val,
663 skfield)) { 660 skfield)) {
664 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 661 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
665 ERR_R_MALLOC_FAILURE); 662 ERR_R_MALLOC_FAILURE);
666 goto err; 663 goto err;
667 } 664 }
668 } 665 }
669 if (sk_eoc) { 666 if (sk_eoc) {
670 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC); 667 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
668 ASN1_R_MISSING_EOC);
671 goto err; 669 goto err;
672 } 670 }
673 } else if (flags & ASN1_TFLG_IMPTAG) { 671 } else if (flags & ASN1_TFLG_IMPTAG) {
674 /* IMPLICIT tagging */ 672 /* IMPLICIT tagging */
675 ret = ASN1_item_ex_d2i(val, &p, len, 673 ret = ASN1_item_ex_d2i(val, &p, len,
676 ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt, ctx); 674 ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt, ctx);
677 if (!ret) { 675 if (!ret) {
678 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 676 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
679 ERR_R_NESTED_ASN1_ERROR); 677 ERR_R_NESTED_ASN1_ERROR);
680 goto err; 678 goto err;
681 } 679 } else if (ret == -1)
682 else if (ret == -1)
683 return -1; 680 return -1;
684 } else { 681 } else {
685 /* Nothing special */ 682 /* Nothing special */
686 ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item), 683 ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
687 -1, 0, opt, ctx); 684 -1, 0, opt, ctx);
688 if (!ret) { 685 if (!ret) {
689 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 686 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
690 ERR_R_NESTED_ASN1_ERROR); 687 ERR_R_NESTED_ASN1_ERROR);
691 goto err; 688 goto err;
692 } 689 } else if (ret == -1)
693 else if (ret == -1)
694 return -1; 690 return -1;
695 } 691 }
696 692
697 *in = p; 693 *in = p;
698 return 1; 694 return 1;
699 695
700 err: 696err:
701 ASN1_template_free(val, tt); 697 ASN1_template_free(val, tt);
702 return 0; 698 return 0;
703} 699}
704 700
705static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, 701static int
706 const unsigned char **in, long inlen, 702asn1_d2i_ex_primitive(ASN1_VALUE **pval, const unsigned char **in, long inlen,
707 const ASN1_ITEM *it, 703 const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx)
708 int tag, int aclass, char opt, ASN1_TLC *ctx)
709{ 704{
710 int ret = 0, utype; 705 int ret = 0, utype;
711 long plen; 706 long plen;
@@ -713,7 +708,8 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
713 const unsigned char *p; 708 const unsigned char *p;
714 BUF_MEM buf; 709 BUF_MEM buf;
715 const unsigned char *cont = NULL; 710 const unsigned char *cont = NULL;
716 long len; 711 long len;
712
717 if (!pval) { 713 if (!pval) {
718 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL); 714 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
719 return 0; /* Should never happen */ 715 return 0; /* Should never happen */
@@ -722,8 +718,7 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
722 if (it->itype == ASN1_ITYPE_MSTRING) { 718 if (it->itype == ASN1_ITYPE_MSTRING) {
723 utype = tag; 719 utype = tag;
724 tag = -1; 720 tag = -1;
725 } 721 } else
726 else
727 utype = it->utype; 722 utype = it->utype;
728 723
729 if (utype == V_ASN1_ANY) { 724 if (utype == V_ASN1_ANY) {
@@ -731,20 +726,20 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
731 unsigned char oclass; 726 unsigned char oclass;
732 if (tag >= 0) { 727 if (tag >= 0) {
733 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, 728 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
734 ASN1_R_ILLEGAL_TAGGED_ANY); 729 ASN1_R_ILLEGAL_TAGGED_ANY);
735 return 0; 730 return 0;
736 } 731 }
737 if (opt) { 732 if (opt) {
738 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, 733 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
739 ASN1_R_ILLEGAL_OPTIONAL_ANY); 734 ASN1_R_ILLEGAL_OPTIONAL_ANY);
740 return 0; 735 return 0;
741 } 736 }
742 p = *in; 737 p = *in;
743 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL, 738 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
744 &p, inlen, -1, 0, 0, ctx); 739 &p, inlen, -1, 0, 0, ctx);
745 if (!ret) { 740 if (!ret) {
746 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, 741 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
747 ERR_R_NESTED_ASN1_ERROR); 742 ERR_R_NESTED_ASN1_ERROR);
748 return 0; 743 return 0;
749 } 744 }
750 if (oclass != V_ASN1_UNIVERSAL) 745 if (oclass != V_ASN1_UNIVERSAL)
@@ -757,17 +752,16 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
757 p = *in; 752 p = *in;
758 /* Check header */ 753 /* Check header */
759 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst, 754 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
760 &p, inlen, tag, aclass, opt, ctx); 755 &p, inlen, tag, aclass, opt, ctx);
761 if (!ret) { 756 if (!ret) {
762 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR); 757 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
763 return 0; 758 return 0;
764 } 759 } else if (ret == -1)
765 else if (ret == -1)
766 return -1; 760 return -1;
767 ret = 0; 761 ret = 0;
768 /* SEQUENCE, SET and "OTHER" are left in encoded form */ 762 /* SEQUENCE, SET and "OTHER" are left in encoded form */
769 if ((utype == V_ASN1_SEQUENCE) 763 if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) ||
770 || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) { 764 (utype == V_ASN1_OTHER)) {
771 /* Clear context cache for type OTHER because the auto clear 765 /* Clear context cache for type OTHER because the auto clear
772 * when we have a exact match wont work 766 * when we have a exact match wont work
773 */ 767 */
@@ -777,7 +771,7 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
777 /* SEQUENCE and SET must be constructed */ 771 /* SEQUENCE and SET must be constructed */
778 else if (!cst) { 772 else if (!cst) {
779 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, 773 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
780 ASN1_R_TYPE_NOT_CONSTRUCTED); 774 ASN1_R_TYPE_NOT_CONSTRUCTED);
781 return 0; 775 return 0;
782 } 776 }
783 777
@@ -785,7 +779,7 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
785 /* If indefinite length constructed find the real end */ 779 /* If indefinite length constructed find the real end */
786 if (inf) { 780 if (inf) {
787 if (!asn1_find_end(&p, plen, inf)) 781 if (!asn1_find_end(&p, plen, inf))
788 goto err; 782 goto err;
789 len = p - cont; 783 len = p - cont;
790 } else { 784 } else {
791 len = p - cont + plen; 785 len = p - cont + plen;
@@ -810,7 +804,7 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
810 /* Append a final null to string */ 804 /* Append a final null to string */
811 if (!BUF_MEM_grow_clean(&buf, len + 1)) { 805 if (!BUF_MEM_grow_clean(&buf, len + 1)) {
812 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, 806 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
813 ERR_R_MALLOC_FAILURE); 807 ERR_R_MALLOC_FAILURE);
814 return 0; 808 return 0;
815 } 809 }
816 buf.data[len] = 0; 810 buf.data[len] = 0;
@@ -828,15 +822,18 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
828 822
829 *in = p; 823 *in = p;
830 ret = 1; 824 ret = 1;
831 err: 825
832 if (free_cont && buf.data) free(buf.data); 826err:
827 if (free_cont && buf.data)
828 free(buf.data);
833 return ret; 829 return ret;
834} 830}
835 831
836/* Translate ASN1 content octets into a structure */ 832/* Translate ASN1 content octets into a structure */
837 833
838int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, 834int
839 int utype, char *free_cont, const ASN1_ITEM *it) 835asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, int utype,
836 char *free_cont, const ASN1_ITEM *it)
840{ 837{
841 ASN1_VALUE **opval = NULL; 838 ASN1_VALUE **opval = NULL;
842 ASN1_STRING *stmp; 839 ASN1_STRING *stmp;
@@ -855,8 +852,7 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
855 if (typ == NULL) 852 if (typ == NULL)
856 goto err; 853 goto err;
857 *pval = (ASN1_VALUE *)typ; 854 *pval = (ASN1_VALUE *)typ;
858 } 855 } else
859 else
860 typ = (ASN1_TYPE *)*pval; 856 typ = (ASN1_TYPE *)*pval;
861 857
862 if (utype != typ->type) 858 if (utype != typ->type)
@@ -864,7 +860,7 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
864 opval = pval; 860 opval = pval;
865 pval = &typ->value.asn1_value; 861 pval = &typ->value.asn1_value;
866 } 862 }
867 switch(utype) { 863 switch (utype) {
868 case V_ASN1_OBJECT: 864 case V_ASN1_OBJECT:
869 if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len)) 865 if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
870 goto err; 866 goto err;
@@ -873,7 +869,7 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
873 case V_ASN1_NULL: 869 case V_ASN1_NULL:
874 if (len) { 870 if (len) {
875 ASN1err(ASN1_F_ASN1_EX_C2I, 871 ASN1err(ASN1_F_ASN1_EX_C2I,
876 ASN1_R_NULL_IS_WRONG_LENGTH); 872 ASN1_R_NULL_IS_WRONG_LENGTH);
877 goto err; 873 goto err;
878 } 874 }
879 *pval = (ASN1_VALUE *)1; 875 *pval = (ASN1_VALUE *)1;
@@ -882,7 +878,7 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
882 case V_ASN1_BOOLEAN: 878 case V_ASN1_BOOLEAN:
883 if (len != 1) { 879 if (len != 1) {
884 ASN1err(ASN1_F_ASN1_EX_C2I, 880 ASN1err(ASN1_F_ASN1_EX_C2I,
885 ASN1_R_BOOLEAN_IS_WRONG_LENGTH); 881 ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
886 goto err; 882 goto err;
887 } else { 883 } else {
888 ASN1_BOOLEAN *tbool; 884 ASN1_BOOLEAN *tbool;
@@ -927,12 +923,12 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
927 default: 923 default:
928 if (utype == V_ASN1_BMPSTRING && (len & 1)) { 924 if (utype == V_ASN1_BMPSTRING && (len & 1)) {
929 ASN1err(ASN1_F_ASN1_EX_C2I, 925 ASN1err(ASN1_F_ASN1_EX_C2I,
930 ASN1_R_BMPSTRING_IS_WRONG_LENGTH); 926 ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
931 goto err; 927 goto err;
932 } 928 }
933 if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) { 929 if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
934 ASN1err(ASN1_F_ASN1_EX_C2I, 930 ASN1err(ASN1_F_ASN1_EX_C2I,
935 ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH); 931 ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
936 goto err; 932 goto err;
937 } 933 }
938 /* All based on ASN1_STRING and handled the same */ 934 /* All based on ASN1_STRING and handled the same */
@@ -940,7 +936,7 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
940 stmp = ASN1_STRING_type_new(utype); 936 stmp = ASN1_STRING_type_new(utype);
941 if (!stmp) { 937 if (!stmp) {
942 ASN1err(ASN1_F_ASN1_EX_C2I, 938 ASN1err(ASN1_F_ASN1_EX_C2I,
943 ERR_R_MALLOC_FAILURE); 939 ERR_R_MALLOC_FAILURE);
944 goto err; 940 goto err;
945 } 941 }
946 *pval = (ASN1_VALUE *)stmp; 942 *pval = (ASN1_VALUE *)stmp;
@@ -958,8 +954,8 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
958 } else { 954 } else {
959 if (!ASN1_STRING_set(stmp, cont, len)) { 955 if (!ASN1_STRING_set(stmp, cont, len)) {
960 ASN1err(ASN1_F_ASN1_EX_C2I, 956 ASN1err(ASN1_F_ASN1_EX_C2I,
961 ERR_R_MALLOC_FAILURE); 957 ERR_R_MALLOC_FAILURE);
962 ASN1_STRING_free(stmp); 958 ASN1_STRING_free(stmp);
963 *pval = NULL; 959 *pval = NULL;
964 goto err; 960 goto err;
965 } 961 }
@@ -968,10 +964,11 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
968 } 964 }
969 /* If ASN1_ANY and NULL type fix up value */ 965 /* If ASN1_ANY and NULL type fix up value */
970 if (typ && (utype == V_ASN1_NULL)) 966 if (typ && (utype == V_ASN1_NULL))
971 typ->value.ptr = NULL; 967 typ->value.ptr = NULL;
972 968
973 ret = 1; 969 ret = 1;
974 err: 970
971err:
975 if (!ret) { 972 if (!ret) {
976 ASN1_TYPE_free(typ); 973 ASN1_TYPE_free(typ);
977 if (opval) 974 if (opval)
@@ -987,11 +984,13 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
987 * recurse on each indefinite length header. 984 * recurse on each indefinite length header.
988 */ 985 */
989 986
990static int asn1_find_end(const unsigned char **in, long len, char inf) 987static int
988asn1_find_end(const unsigned char **in, long len, char inf)
991{ 989{
992 int expected_eoc; 990 int expected_eoc;
993 long plen; 991 long plen;
994 const unsigned char *p = *in, *q; 992 const unsigned char *p = *in, *q;
993
995 /* If not indefinite length constructed just add length */ 994 /* If not indefinite length constructed just add length */
996 if (inf == 0) { 995 if (inf == 0) {
997 *in += len; 996 *in += len;
@@ -1004,7 +1003,7 @@ static int asn1_find_end(const unsigned char **in, long len, char inf)
1004 * skip to the end of the data. 1003 * skip to the end of the data.
1005 */ 1004 */
1006 while (len > 0) { 1005 while (len > 0) {
1007 if(asn1_check_eoc(&p, len)) { 1006 if (asn1_check_eoc(&p, len)) {
1008 expected_eoc--; 1007 expected_eoc--;
1009 if (expected_eoc == 0) 1008 if (expected_eoc == 0)
1010 break; 1009 break;
@@ -1013,8 +1012,8 @@ static int asn1_find_end(const unsigned char **in, long len, char inf)
1013 } 1012 }
1014 q = p; 1013 q = p;
1015 /* Just read in a header: only care about the length */ 1014 /* Just read in a header: only care about the length */
1016 if(!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len, 1015 if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
1017 -1, 0, 0, NULL)) { 1016 -1, 0, 0, NULL)) {
1018 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR); 1017 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
1019 return 0; 1018 return 0;
1020 } 1019 }
@@ -1046,13 +1045,14 @@ static int asn1_find_end(const unsigned char **in, long len, char inf)
1046#define ASN1_MAX_STRING_NEST 5 1045#define ASN1_MAX_STRING_NEST 5
1047#endif 1046#endif
1048 1047
1049 1048static int
1050static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len, 1049asn1_collect(BUF_MEM *buf, const unsigned char **in, long len, char inf,
1051 char inf, int tag, int aclass, int depth) 1050 int tag, int aclass, int depth)
1052{ 1051{
1053 const unsigned char *p, *q; 1052 const unsigned char *p, *q;
1054 long plen; 1053 long plen;
1055 char cst, ininf; 1054 char cst, ininf;
1055
1056 p = *in; 1056 p = *in;
1057 inf &= 1; 1057 inf &= 1;
1058 /* If no buffer and not indefinite length constructed just pass over 1058 /* If no buffer and not indefinite length constructed just pass over
@@ -1061,7 +1061,7 @@ static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1061 *in += len; 1061 *in += len;
1062 return 1; 1062 return 1;
1063 } 1063 }
1064 while(len > 0) { 1064 while (len > 0) {
1065 q = p; 1065 q = p;
1066 /* Check for EOC */ 1066 /* Check for EOC */
1067 if (asn1_check_eoc(&p, len)) { 1067 if (asn1_check_eoc(&p, len)) {
@@ -1069,7 +1069,7 @@ static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1069 * constructed form */ 1069 * constructed form */
1070 if (!inf) { 1070 if (!inf) {
1071 ASN1err(ASN1_F_ASN1_COLLECT, 1071 ASN1err(ASN1_F_ASN1_COLLECT,
1072 ASN1_R_UNEXPECTED_EOC); 1072 ASN1_R_UNEXPECTED_EOC);
1073 return 0; 1073 return 0;
1074 } 1074 }
1075 inf = 0; 1075 inf = 0;
@@ -1077,7 +1077,7 @@ static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1077 } 1077 }
1078 1078
1079 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p, 1079 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1080 len, tag, aclass, 0, NULL)) { 1080 len, tag, aclass, 0, NULL)) {
1081 ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR); 1081 ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
1082 return 0; 1082 return 0;
1083 } 1083 }
@@ -1086,14 +1086,13 @@ static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1086 if (cst) { 1086 if (cst) {
1087 if (depth >= ASN1_MAX_STRING_NEST) { 1087 if (depth >= ASN1_MAX_STRING_NEST) {
1088 ASN1err(ASN1_F_ASN1_COLLECT, 1088 ASN1err(ASN1_F_ASN1_COLLECT,
1089 ASN1_R_NESTED_ASN1_STRING); 1089 ASN1_R_NESTED_ASN1_STRING);
1090 return 0; 1090 return 0;
1091 } 1091 }
1092 if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, 1092 if (!asn1_collect(buf, &p, plen, ininf, tag, aclass,
1093 depth + 1)) 1093 depth + 1))
1094 return 0; 1094 return 0;
1095 } 1095 } else if (plen && !collect_data(buf, &p, plen))
1096 else if (plen && !collect_data(buf, &p, plen))
1097 return 0; 1096 return 0;
1098 len -= p - q; 1097 len -= p - q;
1099 } 1098 }
@@ -1105,7 +1104,8 @@ static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1105 return 1; 1104 return 1;
1106} 1105}
1107 1106
1108static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen) 1107static int
1108collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1109{ 1109{
1110 int len; 1110 int len;
1111 if (buf) { 1111 if (buf) {
@@ -1122,10 +1122,13 @@ static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1122 1122
1123/* Check for ASN1 EOC and swallow it if found */ 1123/* Check for ASN1 EOC and swallow it if found */
1124 1124
1125static int asn1_check_eoc(const unsigned char **in, long len) 1125static int
1126asn1_check_eoc(const unsigned char **in, long len)
1126{ 1127{
1127 const unsigned char *p; 1128 const unsigned char *p;
1128 if (len < 2) return 0; 1129
1130 if (len < 2)
1131 return 0;
1129 p = *in; 1132 p = *in;
1130 if (!p[0] && !p[1]) { 1133 if (!p[0] && !p[1]) {
1131 *in += 2; 1134 *in += 2;
@@ -1141,11 +1144,10 @@ static int asn1_check_eoc(const unsigned char **in, long len)
1141 * header length just read. 1144 * header length just read.
1142 */ 1145 */
1143 1146
1144static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass, 1147static int
1145 char *inf, char *cst, 1148asn1_check_tlen(long *olen, int *otag, unsigned char *oclass, char *inf,
1146 const unsigned char **in, long len, 1149 char *cst, const unsigned char **in, long len, int exptag, int expclass,
1147 int exptag, int expclass, char opt, 1150 char opt, ASN1_TLC *ctx)
1148 ASN1_TLC *ctx)
1149{ 1151{
1150 int i; 1152 int i;
1151 int ptag, pclass; 1153 int ptag, pclass;
@@ -1170,11 +1172,11 @@ static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1170 ctx->hdrlen = p - q; 1172 ctx->hdrlen = p - q;
1171 ctx->valid = 1; 1173 ctx->valid = 1;
1172 /* If definite length, and no error, length + 1174 /* If definite length, and no error, length +
1173 * header can't exceed total amount of data available. 1175 * header can't exceed total amount of data available.
1174 */ 1176 */
1175 if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) { 1177 if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) {
1176 ASN1err(ASN1_F_ASN1_CHECK_TLEN, 1178 ASN1err(ASN1_F_ASN1_CHECK_TLEN,
1177 ASN1_R_TOO_LONG); 1179 ASN1_R_TOO_LONG);
1178 asn1_tlc_clear(ctx); 1180 asn1_tlc_clear(ctx);
1179 return 0; 1181 return 0;
1180 } 1182 }
@@ -1191,7 +1193,8 @@ static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1191 /* If type is OPTIONAL, not an error: 1193 /* If type is OPTIONAL, not an error:
1192 * indicate missing type. 1194 * indicate missing type.
1193 */ 1195 */
1194 if (opt) return -1; 1196 if (opt)
1197 return -1;
1195 asn1_tlc_clear(ctx); 1198 asn1_tlc_clear(ctx);
1196 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG); 1199 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
1197 return 0; 1200 return 0;
@@ -1203,19 +1206,14 @@ static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1203 1206
1204 if (i & 1) 1207 if (i & 1)
1205 plen = len - (p - q); 1208 plen = len - (p - q);
1206
1207 if (inf) 1209 if (inf)
1208 *inf = i & 1; 1210 *inf = i & 1;
1209
1210 if (cst) 1211 if (cst)
1211 *cst = i & V_ASN1_CONSTRUCTED; 1212 *cst = i & V_ASN1_CONSTRUCTED;
1212
1213 if (olen) 1213 if (olen)
1214 *olen = plen; 1214 *olen = plen;
1215
1216 if (oclass) 1215 if (oclass)
1217 *oclass = pclass; 1216 *oclass = pclass;
1218
1219 if (otag) 1217 if (otag)
1220 *otag = ptag; 1218 *otag = ptag;
1221 1219