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.c1059
1 files changed, 679 insertions, 380 deletions
diff --git a/src/lib/libcrypto/asn1/tasn_dec.c b/src/lib/libcrypto/asn1/tasn_dec.c
index b7e916ef36..0ee406231e 100644
--- a/src/lib/libcrypto/asn1/tasn_dec.c
+++ b/src/lib/libcrypto/asn1/tasn_dec.c
@@ -3,7 +3,7 @@
3 * project 2000. 3 * project 2000.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
6 * Copyright (c) 2000 The OpenSSL Project. All rights reserved. 6 * Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved.
7 * 7 *
8 * Redistribution and use in source and binary forms, with or without 8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions 9 * modification, are permitted provided that the following conditions
@@ -65,24 +65,40 @@
65#include <openssl/buffer.h> 65#include <openssl/buffer.h>
66#include <openssl/err.h> 66#include <openssl/err.h>
67 67
68static int asn1_check_eoc(unsigned char **in, long len); 68static int asn1_check_eoc(const unsigned char **in, long len);
69static int asn1_find_end(unsigned char **in, long len, char inf); 69static int asn1_find_end(const unsigned char **in, long len, char inf);
70static int asn1_collect(BUF_MEM *buf, unsigned char **in, long len, char inf, int tag, int aclass); 70
71static int collect_data(BUF_MEM *buf, unsigned char **p, long plen); 71static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
72static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass, char *inf, char *cst, 72 char inf, int tag, int aclass);
73 unsigned char **in, long len, int exptag, int expclass, char opt, ASN1_TLC *ctx); 73
74static int asn1_template_ex_d2i(ASN1_VALUE **pval, unsigned char **in, long len, const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx); 74static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen);
75static int asn1_template_noexp_d2i(ASN1_VALUE **val, unsigned char **in, long len, const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx); 75
76static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, unsigned char **in, long len, 76static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
77 const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx); 77 char *inf, char *cst,
78 const unsigned char **in, long len,
79 int exptag, int expclass, char opt,
80 ASN1_TLC *ctx);
81
82static 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);
86static 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);
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);
78 94
79/* Table to convert tags to bit values, used for MSTRING type */ 95/* Table to convert tags to bit values, used for MSTRING type */
80static unsigned long tag2bit[32]={ 96static const unsigned long tag2bit[32] = {
810, 0, 0, B_ASN1_BIT_STRING, /* tags 0 - 3 */ 970, 0, 0, B_ASN1_BIT_STRING, /* tags 0 - 3 */
82B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,/* tags 4- 7 */ 98B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,/* tags 4- 7 */
83B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,/* tags 8-11 */ 99B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,/* tags 8-11 */
84B_ASN1_UTF8STRING,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,/* tags 12-15 */ 100B_ASN1_UTF8STRING,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,/* tags 12-15 */
850, 0, B_ASN1_NUMERICSTRING,B_ASN1_PRINTABLESTRING, /* tags 16-19 */ 101B_ASN1_SEQUENCE,0,B_ASN1_NUMERICSTRING,B_ASN1_PRINTABLESTRING, /* tags 16-19 */
86B_ASN1_T61STRING,B_ASN1_VIDEOTEXSTRING,B_ASN1_IA5STRING, /* tags 20-22 */ 102B_ASN1_T61STRING,B_ASN1_VIDEOTEXSTRING,B_ASN1_IA5STRING, /* tags 20-22 */
87B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME, /* tags 23-24 */ 103B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME, /* tags 23-24 */
88B_ASN1_GRAPHICSTRING,B_ASN1_ISO64STRING,B_ASN1_GENERALSTRING, /* tags 25-27 */ 104B_ASN1_GRAPHICSTRING,B_ASN1_ISO64STRING,B_ASN1_GENERALSTRING, /* tags 25-27 */
@@ -90,14 +106,14 @@ B_ASN1_UNIVERSALSTRING,B_ASN1_UNKNOWN,B_ASN1_BMPSTRING,B_ASN1_UNKNOWN, /* tags 2
90 }; 106 };
91 107
92unsigned long ASN1_tag2bit(int tag) 108unsigned long ASN1_tag2bit(int tag)
93{ 109 {
94 if((tag < 0) || (tag > 30)) return 0; 110 if ((tag < 0) || (tag > 30)) return 0;
95 return tag2bit[tag]; 111 return tag2bit[tag];
96} 112 }
97 113
98/* Macro to initialize and invalidate the cache */ 114/* Macro to initialize and invalidate the cache */
99 115
100#define asn1_tlc_clear(c) if(c) (c)->valid = 0 116#define asn1_tlc_clear(c) if (c) (c)->valid = 0
101 117
102/* Decode an ASN1 item, this currently behaves just 118/* Decode an ASN1 item, this currently behaves just
103 * like a standard 'd2i' function. 'in' points to 119 * like a standard 'd2i' function. 'in' points to
@@ -107,113 +123,147 @@ unsigned long ASN1_tag2bit(int tag)
107 * case. 123 * case.
108 */ 124 */
109 125
110ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval, unsigned char **in, long len, const ASN1_ITEM *it) 126ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
111{ 127 const unsigned char **in, long len, const ASN1_ITEM *it)
128 {
112 ASN1_TLC c; 129 ASN1_TLC c;
113 ASN1_VALUE *ptmpval = NULL; 130 ASN1_VALUE *ptmpval = NULL;
114 if(!pval) pval = &ptmpval; 131 if (!pval)
115 asn1_tlc_clear(&c); 132 pval = &ptmpval;
116 if(ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0) 133 c.valid = 0;
134 if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
117 return *pval; 135 return *pval;
118 return NULL; 136 return NULL;
119} 137 }
120 138
121int ASN1_template_d2i(ASN1_VALUE **pval, unsigned char **in, long len, const ASN1_TEMPLATE *tt) 139int ASN1_template_d2i(ASN1_VALUE **pval,
122{ 140 const unsigned char **in, long len, const ASN1_TEMPLATE *tt)
141 {
123 ASN1_TLC c; 142 ASN1_TLC c;
124 asn1_tlc_clear(&c); 143 c.valid = 0;
125 return asn1_template_ex_d2i(pval, in, len, tt, 0, &c); 144 return asn1_template_ex_d2i(pval, in, len, tt, 0, &c);
126} 145 }
127 146
128 147
129/* Decode an item, taking care of IMPLICIT tagging, if any. 148/* Decode an item, taking care of IMPLICIT tagging, if any.
130 * If 'opt' set and tag mismatch return -1 to handle OPTIONAL 149 * If 'opt' set and tag mismatch return -1 to handle OPTIONAL
131 */ 150 */
132 151
133int ASN1_item_ex_d2i(ASN1_VALUE **pval, unsigned char **in, long len, const ASN1_ITEM *it, 152int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
134 int tag, int aclass, char opt, ASN1_TLC *ctx) 153 const ASN1_ITEM *it,
135{ 154 int tag, int aclass, char opt, ASN1_TLC *ctx)
155 {
136 const ASN1_TEMPLATE *tt, *errtt = NULL; 156 const ASN1_TEMPLATE *tt, *errtt = NULL;
137 const ASN1_COMPAT_FUNCS *cf; 157 const ASN1_COMPAT_FUNCS *cf;
138 const ASN1_EXTERN_FUNCS *ef; 158 const ASN1_EXTERN_FUNCS *ef;
139 const ASN1_AUX *aux = it->funcs; 159 const ASN1_AUX *aux = it->funcs;
140 ASN1_aux_cb *asn1_cb; 160 ASN1_aux_cb *asn1_cb;
141 unsigned char *p, *q, imphack = 0, oclass; 161 const unsigned char *p = NULL, *q;
162 unsigned char *wp=NULL; /* BIG FAT WARNING! BREAKS CONST WHERE USED */
163 unsigned char imphack = 0, oclass;
142 char seq_eoc, seq_nolen, cst, isopt; 164 char seq_eoc, seq_nolen, cst, isopt;
143 long tmplen; 165 long tmplen;
144 int i; 166 int i;
145 int otag; 167 int otag;
146 int ret = 0; 168 int ret = 0;
147 ASN1_VALUE *pchval, **pchptr, *ptmpval; 169 ASN1_VALUE *pchval, **pchptr, *ptmpval;
148 if(!pval) return 0; 170 if (!pval)
149 if(aux && aux->asn1_cb) asn1_cb = aux->asn1_cb; 171 return 0;
172 if (aux && aux->asn1_cb)
173 asn1_cb = aux->asn1_cb;
150 else asn1_cb = 0; 174 else asn1_cb = 0;
151 175
152 switch(it->itype) { 176 switch(it->itype)
153 177 {
154 case ASN1_ITYPE_PRIMITIVE: 178 case ASN1_ITYPE_PRIMITIVE:
155 if(it->templates) { 179 if (it->templates)
156 /* tagging or OPTIONAL is currently illegal on an item template 180 {
157 * because the flags can't get passed down. In practice this isn't 181 /* tagging or OPTIONAL is currently illegal on an item
158 * a problem: we include the relevant flags from the item template 182 * template because the flags can't get passed down.
159 * in the template itself. 183 * In practice this isn't a problem: we include the
184 * relevant flags from the item template in the
185 * template itself.
160 */ 186 */
161 if ((tag != -1) || opt) { 187 if ((tag != -1) || opt)
162 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE); 188 {
189 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
190 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
163 goto err; 191 goto err;
164 } 192 }
165 return asn1_template_ex_d2i(pval, in, len, it->templates, opt, ctx); 193 return asn1_template_ex_d2i(pval, in, len,
194 it->templates, opt, ctx);
166 } 195 }
167 return asn1_d2i_ex_primitive(pval, in, len, it, tag, aclass, opt, ctx); 196 return asn1_d2i_ex_primitive(pval, in, len, it,
197 tag, aclass, opt, ctx);
168 break; 198 break;
169 199
170 case ASN1_ITYPE_MSTRING: 200 case ASN1_ITYPE_MSTRING:
171 p = *in; 201 p = *in;
172 /* Just read in tag and class */ 202 /* Just read in tag and class */
173 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL, &p, len, -1, 0, 1, ctx); 203 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
174 if(!ret) { 204 &p, len, -1, 0, 1, ctx);
175 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR); 205 if (!ret)
206 {
207 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
208 ERR_R_NESTED_ASN1_ERROR);
176 goto err; 209 goto err;
177 } 210 }
211
178 /* Must be UNIVERSAL class */ 212 /* Must be UNIVERSAL class */
179 if(oclass != V_ASN1_UNIVERSAL) { 213 if (oclass != V_ASN1_UNIVERSAL)
214 {
180 /* If OPTIONAL, assume this is OK */ 215 /* If OPTIONAL, assume this is OK */
181 if(opt) return -1; 216 if (opt) return -1;
182 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MSTRING_NOT_UNIVERSAL); 217 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
218 ASN1_R_MSTRING_NOT_UNIVERSAL);
183 goto err; 219 goto err;
184 } 220 }
185 /* Check tag matches bit map */ 221 /* Check tag matches bit map */
186 if(!(ASN1_tag2bit(otag) & it->utype)) { 222 if (!(ASN1_tag2bit(otag) & it->utype))
223 {
187 /* If OPTIONAL, assume this is OK */ 224 /* If OPTIONAL, assume this is OK */
188 if(opt) return -1; 225 if (opt)
189 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MSTRING_WRONG_TAG); 226 return -1;
227 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
228 ASN1_R_MSTRING_WRONG_TAG);
190 goto err; 229 goto err;
191 } 230 }
192 return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx); 231 return asn1_d2i_ex_primitive(pval, in, len,
232 it, otag, 0, 0, ctx);
193 233
194 case ASN1_ITYPE_EXTERN: 234 case ASN1_ITYPE_EXTERN:
195 /* Use new style d2i */ 235 /* Use new style d2i */
196 ef = it->funcs; 236 ef = it->funcs;
197 return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx); 237 return ef->asn1_ex_d2i(pval, in, len,
238 it, tag, aclass, opt, ctx);
198 239
199 case ASN1_ITYPE_COMPAT: 240 case ASN1_ITYPE_COMPAT:
200 /* we must resort to old style evil hackery */ 241 /* we must resort to old style evil hackery */
201 cf = it->funcs; 242 cf = it->funcs;
202 243
203 /* If OPTIONAL see if it is there */ 244 /* If OPTIONAL see if it is there */
204 if(opt) { 245 if (opt)
246 {
205 int exptag; 247 int exptag;
206 p = *in; 248 p = *in;
207 if(tag == -1) exptag = it->utype; 249 if (tag == -1)
250 exptag = it->utype;
208 else exptag = tag; 251 else exptag = tag;
209 /* Don't care about anything other than presence of expected tag */ 252 /* Don't care about anything other than presence
210 ret = asn1_check_tlen(NULL, NULL, NULL, NULL, NULL, &p, len, exptag, aclass, 1, ctx); 253 * of expected tag */
211 if(!ret) { 254
212 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR); 255 ret = asn1_check_tlen(NULL, NULL, NULL, NULL, NULL,
256 &p, len, exptag, aclass, 1, ctx);
257 if (!ret)
258 {
259 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
260 ERR_R_NESTED_ASN1_ERROR);
213 goto err; 261 goto err;
262 }
263 if (ret == -1)
264 return -1;
214 } 265 }
215 if(ret == -1) return -1; 266
216 }
217 /* This is the old style evil hack IMPLICIT handling: 267 /* This is the old style evil hack IMPLICIT handling:
218 * since the underlying code is expecting a tag and 268 * since the underlying code is expecting a tag and
219 * class other than the one present we change the 269 * class other than the one present we change the
@@ -229,245 +279,332 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, unsigned char **in, long len, const ASN1
229 * buffer. 279 * buffer.
230 */ 280 */
231 281
232 if(tag != -1) { 282 if (tag != -1)
233 p = *in; 283 {
234 imphack = *p; 284 wp = *(unsigned char **)in;
235 *p = (unsigned char)((*p & V_ASN1_CONSTRUCTED) | it->utype); 285 imphack = *wp;
236 } 286 if (p == NULL)
287 {
288 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
289 ERR_R_NESTED_ASN1_ERROR);
290 goto err;
291 }
292 *wp = (unsigned char)((*p & V_ASN1_CONSTRUCTED)
293 | it->utype);
294 }
237 295
238 ptmpval = cf->asn1_d2i(pval, in, len); 296 ptmpval = cf->asn1_d2i(pval, in, len);
239 297
240 if(tag != -1) *p = imphack; 298 if (tag != -1)
299 *wp = imphack;
300
301 if (ptmpval)
302 return 1;
241 303
242 if(ptmpval) return 1;
243 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR); 304 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
244 goto err; 305 goto err;
245 306
246 307
247 case ASN1_ITYPE_CHOICE: 308 case ASN1_ITYPE_CHOICE:
248 if(asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it)) 309 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it))
249 goto auxerr; 310 goto auxerr;
250 311
251 /* Allocate structure */ 312 /* Allocate structure */
252 if(!*pval) { 313 if (!*pval && !ASN1_item_ex_new(pval, it))
253 if(!ASN1_item_ex_new(pval, it)) { 314 {
254 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR); 315 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
255 goto err; 316 ERR_R_NESTED_ASN1_ERROR);
317 goto err;
256 } 318 }
257 }
258 /* CHOICE type, try each possibility in turn */ 319 /* CHOICE type, try each possibility in turn */
259 pchval = NULL; 320 pchval = NULL;
260 p = *in; 321 p = *in;
261 for(i = 0, tt=it->templates; i < it->tcount; i++, tt++) { 322 for (i = 0, tt=it->templates; i < it->tcount; i++, tt++)
323 {
262 pchptr = asn1_get_field_ptr(pval, tt); 324 pchptr = asn1_get_field_ptr(pval, tt);
263 /* We mark field as OPTIONAL so its absence 325 /* We mark field as OPTIONAL so its absence
264 * can be recognised. 326 * can be recognised.
265 */ 327 */
266 ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx); 328 ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx);
267 /* If field not present, try the next one */ 329 /* If field not present, try the next one */
268 if(ret == -1) continue; 330 if (ret == -1)
331 continue;
269 /* If positive return, read OK, break loop */ 332 /* If positive return, read OK, break loop */
270 if(ret > 0) break; 333 if (ret > 0)
334 break;
271 /* Otherwise must be an ASN1 parsing error */ 335 /* Otherwise must be an ASN1 parsing error */
272 errtt = tt; 336 errtt = tt;
273 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR); 337 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
338 ERR_R_NESTED_ASN1_ERROR);
274 goto err; 339 goto err;
275 } 340 }
341
276 /* Did we fall off the end without reading anything? */ 342 /* Did we fall off the end without reading anything? */
277 if(i == it->tcount) { 343 if (i == it->tcount)
344 {
278 /* If OPTIONAL, this is OK */ 345 /* If OPTIONAL, this is OK */
279 if(opt) { 346 if (opt)
347 {
280 /* Free and zero it */ 348 /* Free and zero it */
281 ASN1_item_ex_free(pval, it); 349 ASN1_item_ex_free(pval, it);
282 return -1; 350 return -1;
283 } 351 }
284 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_NO_MATCHING_CHOICE_TYPE); 352 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
353 ASN1_R_NO_MATCHING_CHOICE_TYPE);
285 goto err; 354 goto err;
286 } 355 }
356
287 asn1_set_choice_selector(pval, i, it); 357 asn1_set_choice_selector(pval, i, it);
288 *in = p; 358 *in = p;
289 if(asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it)) 359 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it))
290 goto auxerr; 360 goto auxerr;
291 return 1; 361 return 1;
292 362
363 case ASN1_ITYPE_NDEF_SEQUENCE:
293 case ASN1_ITYPE_SEQUENCE: 364 case ASN1_ITYPE_SEQUENCE:
294 p = *in; 365 p = *in;
295 tmplen = len; 366 tmplen = len;
296 367
297 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */ 368 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
298 if(tag == -1) { 369 if (tag == -1)
370 {
299 tag = V_ASN1_SEQUENCE; 371 tag = V_ASN1_SEQUENCE;
300 aclass = V_ASN1_UNIVERSAL; 372 aclass = V_ASN1_UNIVERSAL;
301 } 373 }
302 /* Get SEQUENCE length and update len, p */ 374 /* Get SEQUENCE length and update len, p */
303 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst, &p, len, tag, aclass, opt, ctx); 375 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
304 if(!ret) { 376 &p, len, tag, aclass, opt, ctx);
305 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR); 377 if (!ret)
378 {
379 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
380 ERR_R_NESTED_ASN1_ERROR);
306 goto err; 381 goto err;
307 } else if(ret == -1) return -1; 382 }
308 if(aux && (aux->flags & ASN1_AFLG_BROKEN)) { 383 else if (ret == -1)
384 return -1;
385 if (aux && (aux->flags & ASN1_AFLG_BROKEN))
386 {
309 len = tmplen - (p - *in); 387 len = tmplen - (p - *in);
310 seq_nolen = 1; 388 seq_nolen = 1;
311 } else seq_nolen = seq_eoc; /* If indefinite we don't do a length check */ 389 }
312 if(!cst) { 390 /* If indefinite we don't do a length check */
313 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_SEQUENCE_NOT_CONSTRUCTED); 391 else seq_nolen = seq_eoc;
392 if (!cst)
393 {
394 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
395 ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
314 goto err; 396 goto err;
315 } 397 }
316 398
317 if(!*pval) { 399 if (!*pval && !ASN1_item_ex_new(pval, it))
318 if(!ASN1_item_ex_new(pval, it)) { 400 {
319 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR); 401 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
320 goto err; 402 ERR_R_NESTED_ASN1_ERROR);
403 goto err;
321 } 404 }
322 } 405
323 if(asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it)) 406 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it))
324 goto auxerr; 407 goto auxerr;
325 408
326 /* Get each field entry */ 409 /* Get each field entry */
327 for(i = 0, tt = it->templates; i < it->tcount; i++, tt++) { 410 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++)
411 {
328 const ASN1_TEMPLATE *seqtt; 412 const ASN1_TEMPLATE *seqtt;
329 ASN1_VALUE **pseqval; 413 ASN1_VALUE **pseqval;
330 seqtt = asn1_do_adb(pval, tt, 1); 414 seqtt = asn1_do_adb(pval, tt, 1);
331 if(!seqtt) goto err; 415 if (!seqtt)
416 goto err;
332 pseqval = asn1_get_field_ptr(pval, seqtt); 417 pseqval = asn1_get_field_ptr(pval, seqtt);
333 /* Have we ran out of data? */ 418 /* Have we ran out of data? */
334 if(!len) break; 419 if (!len)
420 break;
335 q = p; 421 q = p;
336 if(asn1_check_eoc(&p, len)) { 422 if (asn1_check_eoc(&p, len))
337 if(!seq_eoc) { 423 {
338 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_UNEXPECTED_EOC); 424 if (!seq_eoc)
425 {
426 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
427 ASN1_R_UNEXPECTED_EOC);
339 goto err; 428 goto err;
340 } 429 }
341 len -= p - q; 430 len -= p - q;
342 seq_eoc = 0; 431 seq_eoc = 0;
343 q = p; 432 q = p;
344 break; 433 break;
345 } 434 }
346 /* This determines the OPTIONAL flag value. The field cannot 435 /* This determines the OPTIONAL flag value. The field
347 * be omitted if it is the last of a SEQUENCE and there is 436 * cannot be omitted if it is the last of a SEQUENCE
348 * still data to be read. This isn't strictly necessary but 437 * and there is still data to be read. This isn't
349 * it increases efficiency in some cases. 438 * strictly necessary but it increases efficiency in
439 * some cases.
350 */ 440 */
351 if(i == (it->tcount - 1)) isopt = 0; 441 if (i == (it->tcount - 1))
442 isopt = 0;
352 else isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL); 443 else isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
353 /* attempt to read in field, allowing each to be OPTIONAL */ 444 /* attempt to read in field, allowing each to be
354 ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx); 445 * OPTIONAL */
355 if(!ret) { 446
447 ret = asn1_template_ex_d2i(pseqval, &p, len,
448 seqtt, isopt, ctx);
449 if (!ret)
450 {
356 errtt = seqtt; 451 errtt = seqtt;
357 goto err; 452 goto err;
358 } else if(ret == -1) { 453 }
359 /* OPTIONAL component absent. Free and zero the field 454 else if (ret == -1)
455 {
456 /* OPTIONAL component absent.
457 * Free and zero the field.
360 */ 458 */
361 ASN1_template_free(pseqval, seqtt); 459 ASN1_template_free(pseqval, seqtt);
362 continue; 460 continue;
363 } 461 }
364 /* Update length */ 462 /* Update length */
365 len -= p - q; 463 len -= p - q;
366 } 464 }
465
367 /* Check for EOC if expecting one */ 466 /* Check for EOC if expecting one */
368 if(seq_eoc && !asn1_check_eoc(&p, len)) { 467 if (seq_eoc && !asn1_check_eoc(&p, len))
468 {
369 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MISSING_EOC); 469 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MISSING_EOC);
370 goto err; 470 goto err;
371 } 471 }
372 /* Check all data read */ 472 /* Check all data read */
373 if(!seq_nolen && len) { 473 if (!seq_nolen && len)
374 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_SEQUENCE_LENGTH_MISMATCH); 474 {
475 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
476 ASN1_R_SEQUENCE_LENGTH_MISMATCH);
375 goto err; 477 goto err;
376 } 478 }
377 479
378 /* If we get here we've got no more data in the SEQUENCE, 480 /* If we get here we've got no more data in the SEQUENCE,
379 * however we may not have read all fields so check all 481 * however we may not have read all fields so check all
380 * remaining are OPTIONAL and clear any that are. 482 * remaining are OPTIONAL and clear any that are.
381 */ 483 */
382 for(; i < it->tcount; tt++, i++) { 484 for (; i < it->tcount; tt++, i++)
485 {
383 const ASN1_TEMPLATE *seqtt; 486 const ASN1_TEMPLATE *seqtt;
384 seqtt = asn1_do_adb(pval, tt, 1); 487 seqtt = asn1_do_adb(pval, tt, 1);
385 if(!seqtt) goto err; 488 if (!seqtt)
386 if(seqtt->flags & ASN1_TFLG_OPTIONAL) { 489 goto err;
490 if (seqtt->flags & ASN1_TFLG_OPTIONAL)
491 {
387 ASN1_VALUE **pseqval; 492 ASN1_VALUE **pseqval;
388 pseqval = asn1_get_field_ptr(pval, seqtt); 493 pseqval = asn1_get_field_ptr(pval, seqtt);
389 ASN1_template_free(pseqval, seqtt); 494 ASN1_template_free(pseqval, seqtt);
390 } else { 495 }
496 else
497 {
391 errtt = seqtt; 498 errtt = seqtt;
392 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_FIELD_MISSING); 499 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
500 ASN1_R_FIELD_MISSING);
393 goto err; 501 goto err;
502 }
394 } 503 }
395 }
396 /* Save encoding */ 504 /* Save encoding */
397 if(!asn1_enc_save(pval, *in, p - *in, it)) goto auxerr; 505 if (!asn1_enc_save(pval, *in, p - *in, it))
506 goto auxerr;
398 *in = p; 507 *in = p;
399 if(asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it)) 508 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it))
400 goto auxerr; 509 goto auxerr;
401 return 1; 510 return 1;
402 511
403 default: 512 default:
404 return 0; 513 return 0;
405 } 514 }
406 auxerr: 515 auxerr:
407 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_AUX_ERROR); 516 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_AUX_ERROR);
408 err: 517 err:
409 ASN1_item_ex_free(pval, it); 518 ASN1_item_ex_free(pval, it);
410 if(errtt) ERR_add_error_data(4, "Field=", errtt->field_name, ", Type=", it->sname); 519 if (errtt)
411 else ERR_add_error_data(2, "Type=", it->sname); 520 ERR_add_error_data(4, "Field=", errtt->field_name,
521 ", Type=", it->sname);
522 else
523 ERR_add_error_data(2, "Type=", it->sname);
412 return 0; 524 return 0;
413} 525 }
414 526
415/* Templates are handled with two separate functions. One handles any EXPLICIT tag and the other handles the 527/* Templates are handled with two separate functions.
416 * rest. 528 * One handles any EXPLICIT tag and the other handles the rest.
417 */ 529 */
418 530
419static int asn1_template_ex_d2i(ASN1_VALUE **val, unsigned char **in, long inlen, const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx) 531static int asn1_template_ex_d2i(ASN1_VALUE **val,
420{ 532 const unsigned char **in, long inlen,
533 const ASN1_TEMPLATE *tt, char opt,
534 ASN1_TLC *ctx)
535 {
421 int flags, aclass; 536 int flags, aclass;
422 int ret; 537 int ret;
423 long len; 538 long len;
424 unsigned char *p, *q; 539 const unsigned char *p, *q;
425 char exp_eoc; 540 char exp_eoc;
426 if(!val) return 0; 541 if (!val)
542 return 0;
427 flags = tt->flags; 543 flags = tt->flags;
428 aclass = flags & ASN1_TFLG_TAG_CLASS; 544 aclass = flags & ASN1_TFLG_TAG_CLASS;
429 545
430 p = *in; 546 p = *in;
431 547
432 /* Check if EXPLICIT tag expected */ 548 /* Check if EXPLICIT tag expected */
433 if(flags & ASN1_TFLG_EXPTAG) { 549 if (flags & ASN1_TFLG_EXPTAG)
550 {
434 char cst; 551 char cst;
435 /* Need to work out amount of data available to the inner content and where it 552 /* Need to work out amount of data available to the inner
436 * starts: so read in EXPLICIT header to get the info. 553 * content and where it starts: so read in EXPLICIT header to
554 * get the info.
437 */ 555 */
438 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst, &p, inlen, tt->tag, aclass, opt, ctx); 556 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
557 &p, inlen, tt->tag, aclass, opt, ctx);
439 q = p; 558 q = p;
440 if(!ret) { 559 if (!ret)
441 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR); 560 {
561 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
562 ERR_R_NESTED_ASN1_ERROR);
442 return 0; 563 return 0;
443 } else if(ret == -1) return -1; 564 }
444 if(!cst) { 565 else if (ret == -1)
445 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED); 566 return -1;
567 if (!cst)
568 {
569 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
570 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
446 return 0; 571 return 0;
447 } 572 }
448 /* We've found the field so it can't be OPTIONAL now */ 573 /* We've found the field so it can't be OPTIONAL now */
449 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx); 574 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
450 if(!ret) { 575 if (!ret)
451 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR); 576 {
577 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
578 ERR_R_NESTED_ASN1_ERROR);
452 return 0; 579 return 0;
453 } 580 }
454 /* We read the field in OK so update length */ 581 /* We read the field in OK so update length */
455 len -= p - q; 582 len -= p - q;
456 if(exp_eoc) { 583 if (exp_eoc)
584 {
457 /* If NDEF we must have an EOC here */ 585 /* If NDEF we must have an EOC here */
458 if(!asn1_check_eoc(&p, len)) { 586 if (!asn1_check_eoc(&p, len))
459 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I, ASN1_R_MISSING_EOC); 587 {
588 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
589 ASN1_R_MISSING_EOC);
460 goto err; 590 goto err;
591 }
461 } 592 }
462 } else { 593 else
463 /* Otherwise we must hit the EXPLICIT tag end or its an error */ 594 {
464 if(len) { 595 /* Otherwise we must hit the EXPLICIT tag end or its
465 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I, ASN1_R_EXPLICIT_LENGTH_MISMATCH); 596 * an error */
597 if (len)
598 {
599 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
600 ASN1_R_EXPLICIT_LENGTH_MISMATCH);
466 goto err; 601 goto err;
602 }
467 } 603 }
468 } 604 }
469 } else 605 else
470 return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx); 606 return asn1_template_noexp_d2i(val, in, inlen,
607 tt, opt, ctx);
471 608
472 *in = p; 609 *in = p;
473 return 1; 610 return 1;
@@ -476,98 +613,145 @@ static int asn1_template_ex_d2i(ASN1_VALUE **val, unsigned char **in, long inlen
476 ASN1_template_free(val, tt); 613 ASN1_template_free(val, tt);
477 *val = NULL; 614 *val = NULL;
478 return 0; 615 return 0;
479} 616 }
480 617
481static int asn1_template_noexp_d2i(ASN1_VALUE **val, unsigned char **in, long len, const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx) 618static int asn1_template_noexp_d2i(ASN1_VALUE **val,
482{ 619 const unsigned char **in, long len,
620 const ASN1_TEMPLATE *tt, char opt,
621 ASN1_TLC *ctx)
622 {
483 int flags, aclass; 623 int flags, aclass;
484 int ret; 624 int ret;
485 unsigned char *p, *q; 625 const unsigned char *p, *q;
486 if(!val) return 0; 626 if (!val)
627 return 0;
487 flags = tt->flags; 628 flags = tt->flags;
488 aclass = flags & ASN1_TFLG_TAG_CLASS; 629 aclass = flags & ASN1_TFLG_TAG_CLASS;
489 630
490 p = *in; 631 p = *in;
491 q = p; 632 q = p;
492 633
493 if(flags & ASN1_TFLG_SK_MASK) { 634 if (flags & ASN1_TFLG_SK_MASK)
635 {
494 /* SET OF, SEQUENCE OF */ 636 /* SET OF, SEQUENCE OF */
495 int sktag, skaclass; 637 int sktag, skaclass;
496 char sk_eoc; 638 char sk_eoc;
497 /* First work out expected inner tag value */ 639 /* First work out expected inner tag value */
498 if(flags & ASN1_TFLG_IMPTAG) { 640 if (flags & ASN1_TFLG_IMPTAG)
641 {
499 sktag = tt->tag; 642 sktag = tt->tag;
500 skaclass = aclass; 643 skaclass = aclass;
501 } else { 644 }
645 else
646 {
502 skaclass = V_ASN1_UNIVERSAL; 647 skaclass = V_ASN1_UNIVERSAL;
503 if(flags & ASN1_TFLG_SET_OF) sktag = V_ASN1_SET; 648 if (flags & ASN1_TFLG_SET_OF)
504 else sktag = V_ASN1_SEQUENCE; 649 sktag = V_ASN1_SET;
505 } 650 else
651 sktag = V_ASN1_SEQUENCE;
652 }
506 /* Get the tag */ 653 /* Get the tag */
507 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL, &p, len, sktag, skaclass, opt, ctx); 654 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
508 if(!ret) { 655 &p, len, sktag, skaclass, opt, ctx);
509 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR); 656 if (!ret)
657 {
658 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
659 ERR_R_NESTED_ASN1_ERROR);
510 return 0; 660 return 0;
511 } else if(ret == -1) return -1; 661 }
512 if(!*val) *val = (ASN1_VALUE *)sk_new_null(); 662 else if (ret == -1)
513 else { 663 return -1;
664 if (!*val)
665 *val = (ASN1_VALUE *)sk_new_null();
666 else
667 {
514 /* We've got a valid STACK: free up any items present */ 668 /* We've got a valid STACK: free up any items present */
515 STACK *sktmp = (STACK *)*val; 669 STACK *sktmp = (STACK *)*val;
516 ASN1_VALUE *vtmp; 670 ASN1_VALUE *vtmp;
517 while(sk_num(sktmp) > 0) { 671 while(sk_num(sktmp) > 0)
672 {
518 vtmp = (ASN1_VALUE *)sk_pop(sktmp); 673 vtmp = (ASN1_VALUE *)sk_pop(sktmp);
519 ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item)); 674 ASN1_item_ex_free(&vtmp,
675 ASN1_ITEM_ptr(tt->item));
676 }
520 } 677 }
521 }
522 678
523 if(!*val) { 679 if (!*val)
524 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_MALLOC_FAILURE); 680 {
681 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
682 ERR_R_MALLOC_FAILURE);
525 goto err; 683 goto err;
526 } 684 }
685
527 /* Read as many items as we can */ 686 /* Read as many items as we can */
528 while(len > 0) { 687 while(len > 0)
688 {
529 ASN1_VALUE *skfield; 689 ASN1_VALUE *skfield;
530 q = p; 690 q = p;
531 /* See if EOC found */ 691 /* See if EOC found */
532 if(asn1_check_eoc(&p, len)) { 692 if (asn1_check_eoc(&p, len))
533 if(!sk_eoc) { 693 {
534 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I, ASN1_R_UNEXPECTED_EOC); 694 if (!sk_eoc)
695 {
696 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
697 ASN1_R_UNEXPECTED_EOC);
535 goto err; 698 goto err;
536 } 699 }
537 len -= p - q; 700 len -= p - q;
538 sk_eoc = 0; 701 sk_eoc = 0;
539 break; 702 break;
540 } 703 }
541 skfield = NULL; 704 skfield = NULL;
542 if(!ASN1_item_ex_d2i(&skfield, &p, len, ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx)) { 705 if (!ASN1_item_ex_d2i(&skfield, &p, len,
543 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I, ERR_R_NESTED_ASN1_ERROR); 706 ASN1_ITEM_ptr(tt->item),
707 -1, 0, 0, ctx))
708 {
709 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
710 ERR_R_NESTED_ASN1_ERROR);
544 goto err; 711 goto err;
545 } 712 }
546 len -= p - q; 713 len -= p - q;
547 if(!sk_push((STACK *)*val, (char *)skfield)) { 714 if (!sk_push((STACK *)*val, (char *)skfield))
548 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I, ERR_R_MALLOC_FAILURE); 715 {
716 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
717 ERR_R_MALLOC_FAILURE);
549 goto err; 718 goto err;
719 }
550 } 720 }
551 } 721 if (sk_eoc)
552 if(sk_eoc) { 722 {
553 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I, ASN1_R_MISSING_EOC); 723 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC);
554 goto err; 724 goto err;
725 }
555 } 726 }
556 } else if(flags & ASN1_TFLG_IMPTAG) { 727 else if (flags & ASN1_TFLG_IMPTAG)
728 {
557 /* IMPLICIT tagging */ 729 /* IMPLICIT tagging */
558 ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt, ctx); 730 ret = ASN1_item_ex_d2i(val, &p, len,
559 if(!ret) { 731 ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt, ctx);
560 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I, ERR_R_NESTED_ASN1_ERROR); 732 if (!ret)
733 {
734 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
735 ERR_R_NESTED_ASN1_ERROR);
561 goto err; 736 goto err;
562 } else if(ret == -1) return -1; 737 }
563 } else { 738 else if (ret == -1)
739 return -1;
740 }
741 else
742 {
564 /* Nothing special */ 743 /* Nothing special */
565 ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item), -1, 0, opt, ctx); 744 ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
566 if(!ret) { 745 -1, 0, opt, ctx);
567 ASN1err(ASN1_F_ASN1_TEMPLATE_D2I, ERR_R_NESTED_ASN1_ERROR); 746 if (!ret)
747 {
748 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
749 ERR_R_NESTED_ASN1_ERROR);
568 goto err; 750 goto err;
569 } else if(ret == -1) return -1; 751 }
570 } 752 else if (ret == -1)
753 return -1;
754 }
571 755
572 *in = p; 756 *in = p;
573 return 1; 757 return 1;
@@ -576,85 +760,115 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val, unsigned char **in, long le
576 ASN1_template_free(val, tt); 760 ASN1_template_free(val, tt);
577 *val = NULL; 761 *val = NULL;
578 return 0; 762 return 0;
579} 763 }
580 764
581static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, unsigned char **in, long inlen, 765static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
582 const ASN1_ITEM *it, 766 const unsigned char **in, long inlen,
583 int tag, int aclass, char opt, ASN1_TLC *ctx) 767 const ASN1_ITEM *it,
584{ 768 int tag, int aclass, char opt, ASN1_TLC *ctx)
769 {
585 int ret = 0, utype; 770 int ret = 0, utype;
586 long plen; 771 long plen;
587 char cst, inf, free_cont = 0; 772 char cst, inf, free_cont = 0;
588 unsigned char *p; 773 const unsigned char *p;
589 BUF_MEM buf; 774 BUF_MEM buf;
590 unsigned char *cont = NULL; 775 const unsigned char *cont = NULL;
591 long len; 776 long len;
592 if(!pval) { 777 if (!pval)
778 {
593 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL); 779 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
594 return 0; /* Should never happen */ 780 return 0; /* Should never happen */
595 } 781 }
596 782
597 if(it->itype == ASN1_ITYPE_MSTRING) { 783 if (it->itype == ASN1_ITYPE_MSTRING)
784 {
598 utype = tag; 785 utype = tag;
599 tag = -1; 786 tag = -1;
600 } else utype = it->utype; 787 }
788 else
789 utype = it->utype;
601 790
602 if(utype == V_ASN1_ANY) { 791 if (utype == V_ASN1_ANY)
792 {
603 /* If type is ANY need to figure out type from tag */ 793 /* If type is ANY need to figure out type from tag */
604 unsigned char oclass; 794 unsigned char oclass;
605 if(tag >= 0) { 795 if (tag >= 0)
606 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_TAGGED_ANY); 796 {
797 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
798 ASN1_R_ILLEGAL_TAGGED_ANY);
607 return 0; 799 return 0;
608 } 800 }
609 if(opt) { 801 if (opt)
610 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_OPTIONAL_ANY); 802 {
803 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
804 ASN1_R_ILLEGAL_OPTIONAL_ANY);
611 return 0; 805 return 0;
612 } 806 }
613 p = *in; 807 p = *in;
614 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL, &p, inlen, -1, 0, 0, ctx); 808 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
615 if(!ret) { 809 &p, inlen, -1, 0, 0, ctx);
616 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR); 810 if (!ret)
811 {
812 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
813 ERR_R_NESTED_ASN1_ERROR);
617 return 0; 814 return 0;
815 }
816 if (oclass != V_ASN1_UNIVERSAL)
817 utype = V_ASN1_OTHER;
618 } 818 }
619 if(oclass != V_ASN1_UNIVERSAL) utype = V_ASN1_OTHER; 819 if (tag == -1)
620 } 820 {
621 if(tag == -1) {
622 tag = utype; 821 tag = utype;
623 aclass = V_ASN1_UNIVERSAL; 822 aclass = V_ASN1_UNIVERSAL;
624 } 823 }
625 p = *in; 824 p = *in;
626 /* Check header */ 825 /* Check header */
627 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst, &p, inlen, tag, aclass, opt, ctx); 826 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
628 if(!ret) { 827 &p, inlen, tag, aclass, opt, ctx);
828 if (!ret)
829 {
629 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR); 830 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
630 return 0; 831 return 0;
631 } else if(ret == -1) return -1; 832 }
632 833 else if (ret == -1)
633 ret = 0; 834 return -1;
835 ret = 0;
634 /* SEQUENCE, SET and "OTHER" are left in encoded form */ 836 /* SEQUENCE, SET and "OTHER" are left in encoded form */
635 if((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) { 837 if ((utype == V_ASN1_SEQUENCE)
636 /* Clear context cache for type OTHER because the auto clear when 838 || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER))
637 * we have a exact match wont work 839 {
840 /* Clear context cache for type OTHER because the auto clear
841 * when we have a exact match wont work
638 */ 842 */
639 if(utype == V_ASN1_OTHER) { 843 if (utype == V_ASN1_OTHER)
844 {
640 asn1_tlc_clear(ctx); 845 asn1_tlc_clear(ctx);
846 }
641 /* SEQUENCE and SET must be constructed */ 847 /* SEQUENCE and SET must be constructed */
642 } else if(!cst) { 848 else if (!cst)
643 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_TYPE_NOT_CONSTRUCTED); 849 {
850 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
851 ASN1_R_TYPE_NOT_CONSTRUCTED);
644 return 0; 852 return 0;
645 } 853 }
646 854
647 cont = *in; 855 cont = *in;
648 /* If indefinite length constructed find the real end */ 856 /* If indefinite length constructed find the real end */
649 if(inf) { 857 if (inf)
650 if(!asn1_find_end(&p, plen, inf)) goto err; 858 {
859 if (!asn1_find_end(&p, plen, inf))
860 goto err;
651 len = p - cont; 861 len = p - cont;
652 } else { 862 }
863 else
864 {
653 len = p - cont + plen; 865 len = p - cont + plen;
654 p += plen; 866 p += plen;
655 buf.data = NULL; 867 buf.data = NULL;
868 }
656 } 869 }
657 } else if(cst) { 870 else if (cst)
871 {
658 buf.length = 0; 872 buf.length = 0;
659 buf.max = 0; 873 buf.max = 0;
660 buf.data = NULL; 874 buf.data = NULL;
@@ -664,36 +878,46 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, unsigned char **in, long inl
664 * internally irrespective of the type. So instead just check 878 * internally irrespective of the type. So instead just check
665 * for UNIVERSAL class and ignore the tag. 879 * for UNIVERSAL class and ignore the tag.
666 */ 880 */
667 if(!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL)) goto err; 881 if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL))
882 {
883 free_cont = 1;
884 goto err;
885 }
668 len = buf.length; 886 len = buf.length;
669 /* Append a final null to string */ 887 /* Append a final null to string */
670 if(!BUF_MEM_grow_clean(&buf, len + 1)) { 888 if (!BUF_MEM_grow_clean(&buf, len + 1))
671 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_MALLOC_FAILURE); 889 {
890 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
891 ERR_R_MALLOC_FAILURE);
672 return 0; 892 return 0;
673 } 893 }
674 buf.data[len] = 0; 894 buf.data[len] = 0;
675 cont = (unsigned char *)buf.data; 895 cont = (const unsigned char *)buf.data;
676 free_cont = 1; 896 free_cont = 1;
677 } else { 897 }
898 else
899 {
678 cont = p; 900 cont = p;
679 len = plen; 901 len = plen;
680 p += plen; 902 p += plen;
681 } 903 }
682 904
683 /* We now have content length and type: translate into a structure */ 905 /* We now have content length and type: translate into a structure */
684 if(!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it)) goto err; 906 if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
907 goto err;
685 908
686 *in = p; 909 *in = p;
687 ret = 1; 910 ret = 1;
688 err: 911 err:
689 if(free_cont && buf.data) OPENSSL_free(buf.data); 912 if (free_cont && buf.data) OPENSSL_free(buf.data);
690 return ret; 913 return ret;
691} 914 }
692 915
693/* Translate ASN1 content octets into a structure */ 916/* Translate ASN1 content octets into a structure */
694 917
695int asn1_ex_c2i(ASN1_VALUE **pval, unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it) 918int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
696{ 919 int utype, char *free_cont, const ASN1_ITEM *it)
920 {
697 ASN1_VALUE **opval = NULL; 921 ASN1_VALUE **opval = NULL;
698 ASN1_STRING *stmp; 922 ASN1_STRING *stmp;
699 ASN1_TYPE *typ = NULL; 923 ASN1_TYPE *typ = NULL;
@@ -701,43 +925,62 @@ int asn1_ex_c2i(ASN1_VALUE **pval, unsigned char *cont, int len, int utype, char
701 const ASN1_PRIMITIVE_FUNCS *pf; 925 const ASN1_PRIMITIVE_FUNCS *pf;
702 ASN1_INTEGER **tint; 926 ASN1_INTEGER **tint;
703 pf = it->funcs; 927 pf = it->funcs;
704 if(pf && pf->prim_c2i) return pf->prim_c2i(pval, cont, len, utype, free_cont, it); 928
929 if (pf && pf->prim_c2i)
930 return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
705 /* If ANY type clear type and set pointer to internal value */ 931 /* If ANY type clear type and set pointer to internal value */
706 if(it->utype == V_ASN1_ANY) { 932 if (it->utype == V_ASN1_ANY)
707 if(!*pval) { 933 {
934 if (!*pval)
935 {
708 typ = ASN1_TYPE_new(); 936 typ = ASN1_TYPE_new();
937 if (typ == NULL)
938 goto err;
709 *pval = (ASN1_VALUE *)typ; 939 *pval = (ASN1_VALUE *)typ;
710 } else typ = (ASN1_TYPE *)*pval; 940 }
711 if(utype != typ->type) ASN1_TYPE_set(typ, utype, NULL); 941 else
942 typ = (ASN1_TYPE *)*pval;
943
944 if (utype != typ->type)
945 ASN1_TYPE_set(typ, utype, NULL);
712 opval = pval; 946 opval = pval;
713 pval = (ASN1_VALUE **)&typ->value.ptr; 947 pval = &typ->value.asn1_value;
714 } 948 }
715 switch(utype) { 949 switch(utype)
950 {
716 case V_ASN1_OBJECT: 951 case V_ASN1_OBJECT:
717 if(!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len)) goto err; 952 if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
953 goto err;
718 break; 954 break;
719 955
720 case V_ASN1_NULL: 956 case V_ASN1_NULL:
721 if(len) { 957 if (len)
722 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_NULL_IS_WRONG_LENGTH); 958 {
959 ASN1err(ASN1_F_ASN1_EX_C2I,
960 ASN1_R_NULL_IS_WRONG_LENGTH);
723 goto err; 961 goto err;
724 } 962 }
725 *pval = (ASN1_VALUE *)1; 963 *pval = (ASN1_VALUE *)1;
726 break; 964 break;
727 965
728 case V_ASN1_BOOLEAN: 966 case V_ASN1_BOOLEAN:
729 if(len != 1) { 967 if (len != 1)
730 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_BOOLEAN_IS_WRONG_LENGTH); 968 {
969 ASN1err(ASN1_F_ASN1_EX_C2I,
970 ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
731 goto err; 971 goto err;
732 } else { 972 }
973 else
974 {
733 ASN1_BOOLEAN *tbool; 975 ASN1_BOOLEAN *tbool;
734 tbool = (ASN1_BOOLEAN *)pval; 976 tbool = (ASN1_BOOLEAN *)pval;
735 *tbool = *cont; 977 *tbool = *cont;
736 } 978 }
737 break; 979 break;
738 980
739 case V_ASN1_BIT_STRING: 981 case V_ASN1_BIT_STRING:
740 if(!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len)) goto err; 982 if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
983 goto err;
741 break; 984 break;
742 985
743 case V_ASN1_INTEGER: 986 case V_ASN1_INTEGER:
@@ -745,7 +988,8 @@ int asn1_ex_c2i(ASN1_VALUE **pval, unsigned char *cont, int len, int utype, char
745 case V_ASN1_ENUMERATED: 988 case V_ASN1_ENUMERATED:
746 case V_ASN1_NEG_ENUMERATED: 989 case V_ASN1_NEG_ENUMERATED:
747 tint = (ASN1_INTEGER **)pval; 990 tint = (ASN1_INTEGER **)pval;
748 if(!c2i_ASN1_INTEGER(tint, &cont, len)) goto err; 991 if (!c2i_ASN1_INTEGER(tint, &cont, len))
992 goto err;
749 /* Fixup type to match the expected form */ 993 /* Fixup type to match the expected form */
750 (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG); 994 (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
751 break; 995 break;
@@ -769,46 +1013,59 @@ int asn1_ex_c2i(ASN1_VALUE **pval, unsigned char *cont, int len, int utype, char
769 case V_ASN1_SEQUENCE: 1013 case V_ASN1_SEQUENCE:
770 default: 1014 default:
771 /* All based on ASN1_STRING and handled the same */ 1015 /* All based on ASN1_STRING and handled the same */
772 if(!*pval) { 1016 if (!*pval)
1017 {
773 stmp = ASN1_STRING_type_new(utype); 1018 stmp = ASN1_STRING_type_new(utype);
774 if(!stmp) { 1019 if (!stmp)
775 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_MALLOC_FAILURE); 1020 {
1021 ASN1err(ASN1_F_ASN1_EX_C2I,
1022 ERR_R_MALLOC_FAILURE);
776 goto err; 1023 goto err;
777 } 1024 }
778 *pval = (ASN1_VALUE *)stmp; 1025 *pval = (ASN1_VALUE *)stmp;
779 } else { 1026 }
1027 else
1028 {
780 stmp = (ASN1_STRING *)*pval; 1029 stmp = (ASN1_STRING *)*pval;
781 stmp->type = utype; 1030 stmp->type = utype;
782 } 1031 }
783 /* If we've already allocated a buffer use it */ 1032 /* If we've already allocated a buffer use it */
784 if(*free_cont) { 1033 if (*free_cont)
785 if(stmp->data) OPENSSL_free(stmp->data); 1034 {
786 stmp->data = cont; 1035 if (stmp->data)
1036 OPENSSL_free(stmp->data);
1037 stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
787 stmp->length = len; 1038 stmp->length = len;
788 *free_cont = 0; 1039 *free_cont = 0;
789 } else { 1040 }
790 if(!ASN1_STRING_set(stmp, cont, len)) { 1041 else
791 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_MALLOC_FAILURE); 1042 {
1043 if (!ASN1_STRING_set(stmp, cont, len))
1044 {
1045 ASN1err(ASN1_F_ASN1_EX_C2I,
1046 ERR_R_MALLOC_FAILURE);
792 ASN1_STRING_free(stmp); 1047 ASN1_STRING_free(stmp);
793 *pval = NULL; 1048 *pval = NULL;
794 goto err; 1049 goto err;
1050 }
795 } 1051 }
796 }
797 break; 1052 break;
798 } 1053 }
799 /* If ASN1_ANY and NULL type fix up value */ 1054 /* If ASN1_ANY and NULL type fix up value */
800 if(typ && utype==V_ASN1_NULL) typ->value.ptr = NULL; 1055 if (typ && (utype == V_ASN1_NULL))
1056 typ->value.ptr = NULL;
801 1057
802 ret = 1; 1058 ret = 1;
803 err: 1059 err:
804 if(!ret) 1060 if (!ret)
805 { 1061 {
806 ASN1_TYPE_free(typ); 1062 ASN1_TYPE_free(typ);
807 if (opval) 1063 if (opval)
808 *opval = NULL; 1064 *opval = NULL;
809 } 1065 }
810 return ret; 1066 return ret;
811} 1067 }
1068
812 1069
813/* This function finds the end of an ASN1 structure when passed its maximum 1070/* This function finds the end of an ASN1 structure when passed its maximum
814 * length, whether it is indefinite length and a pointer to the content. 1071 * length, whether it is indefinite length and a pointer to the content.
@@ -816,11 +1073,11 @@ int asn1_ex_c2i(ASN1_VALUE **pval, unsigned char *cont, int len, int utype, char
816 * recurse on each indefinite length header. 1073 * recurse on each indefinite length header.
817 */ 1074 */
818 1075
819static int asn1_find_end(unsigned char **in, long len, char inf) 1076static int asn1_find_end(const unsigned char **in, long len, char inf)
820 { 1077 {
821 int expected_eoc; 1078 int expected_eoc;
822 long plen; 1079 long plen;
823 unsigned char *p = *in, *q; 1080 const unsigned char *p = *in, *q;
824 /* If not indefinite length constructed just add length */ 1081 /* If not indefinite length constructed just add length */
825 if (inf == 0) 1082 if (inf == 0)
826 { 1083 {
@@ -830,7 +1087,7 @@ static int asn1_find_end(unsigned char **in, long len, char inf)
830 expected_eoc = 1; 1087 expected_eoc = 1;
831 /* Indefinite length constructed form. Find the end when enough EOCs 1088 /* Indefinite length constructed form. Find the end when enough EOCs
832 * are found. If more indefinite length constructed headers 1089 * are found. If more indefinite length constructed headers
833 * are encountered increment the expected eoc count otherwise justi 1090 * are encountered increment the expected eoc count otherwise just
834 * skip to the end of the data. 1091 * skip to the end of the data.
835 */ 1092 */
836 while (len > 0) 1093 while (len > 0)
@@ -865,38 +1122,55 @@ static int asn1_find_end(unsigned char **in, long len, char inf)
865 *in = p; 1122 *in = p;
866 return 1; 1123 return 1;
867 } 1124 }
868
869/* This function collects the asn1 data from a constructred string 1125/* This function collects the asn1 data from a constructred string
870 * type into a buffer. The values of 'in' and 'len' should refer 1126 * type into a buffer. The values of 'in' and 'len' should refer
871 * to the contents of the constructed type and 'inf' should be set 1127 * to the contents of the constructed type and 'inf' should be set
872 * if it is indefinite length. 1128 * if it is indefinite length.
873 */ 1129 */
874 1130
875static int asn1_collect(BUF_MEM *buf, unsigned char **in, long len, char inf, int tag, int aclass) 1131static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
876{ 1132 char inf, int tag, int aclass)
877 unsigned char *p, *q; 1133 {
1134 const unsigned char *p, *q;
878 long plen; 1135 long plen;
879 char cst, ininf; 1136 char cst, ininf;
880 p = *in; 1137 p = *in;
881 inf &= 1; 1138 inf &= 1;
882 while(len > 0) { 1139 /* If no buffer and not indefinite length constructed just pass over
1140 * the encoded data */
1141 if (!buf && !inf)
1142 {
1143 *in += len;
1144 return 1;
1145 }
1146 while(len > 0)
1147 {
883 q = p; 1148 q = p;
884 /* Check for EOC */ 1149 /* Check for EOC */
885 if(asn1_check_eoc(&p, len)) { 1150 if (asn1_check_eoc(&p, len))
886 /* EOC is illegal outside indefinite length constructed form */ 1151 {
887 if(!inf) { 1152 /* EOC is illegal outside indefinite length
888 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_UNEXPECTED_EOC); 1153 * constructed form */
1154 if (!inf)
1155 {
1156 ASN1err(ASN1_F_ASN1_COLLECT,
1157 ASN1_R_UNEXPECTED_EOC);
889 return 0; 1158 return 0;
890 } 1159 }
891 inf = 0; 1160 inf = 0;
892 break; 1161 break;
893 } 1162 }
894 if(!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p, len, tag, aclass, 0, NULL)) { 1163
1164 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1165 len, tag, aclass, 0, NULL))
1166 {
895 ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR); 1167 ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
896 return 0; 1168 return 0;
897 } 1169 }
1170
898 /* If indefinite length constructed update max length */ 1171 /* If indefinite length constructed update max length */
899 if(cst) { 1172 if (cst)
1173 {
900#ifdef OPENSSL_ALLOW_NESTED_ASN1_STRINGS 1174#ifdef OPENSSL_ALLOW_NESTED_ASN1_STRINGS
901 if (!asn1_collect(buf, &p, plen, ininf, tag, aclass)) 1175 if (!asn1_collect(buf, &p, plen, ininf, tag, aclass))
902 return 0; 1176 return 0;
@@ -904,47 +1178,51 @@ static int asn1_collect(BUF_MEM *buf, unsigned char **in, long len, char inf, in
904 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_NESTED_ASN1_STRING); 1178 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_NESTED_ASN1_STRING);
905 return 0; 1179 return 0;
906#endif 1180#endif
907 } else { 1181 }
908 if(plen && !collect_data(buf, &p, plen)) return 0; 1182 else if (plen && !collect_data(buf, &p, plen))
909 } 1183 return 0;
910 len -= p - q; 1184 len -= p - q;
911 } 1185 }
912 if(inf) { 1186 if (inf)
1187 {
913 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC); 1188 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
914 return 0; 1189 return 0;
915 } 1190 }
916 *in = p; 1191 *in = p;
917 return 1; 1192 return 1;
918} 1193 }
919 1194
920static int collect_data(BUF_MEM *buf, unsigned char **p, long plen) 1195static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
921{ 1196 {
922 int len; 1197 int len;
923 if(buf) { 1198 if (buf)
924 len = buf->length; 1199 {
925 if(!BUF_MEM_grow_clean(buf, len + plen)) { 1200 len = buf->length;
926 ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE); 1201 if (!BUF_MEM_grow_clean(buf, len + plen))
927 return 0; 1202 {
1203 ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE);
1204 return 0;
928 } 1205 }
929 memcpy(buf->data + len, *p, plen); 1206 memcpy(buf->data + len, *p, plen);
930 } 1207 }
931 *p += plen; 1208 *p += plen;
932 return 1; 1209 return 1;
933} 1210 }
934 1211
935/* Check for ASN1 EOC and swallow it if found */ 1212/* Check for ASN1 EOC and swallow it if found */
936 1213
937static int asn1_check_eoc(unsigned char **in, long len) 1214static int asn1_check_eoc(const unsigned char **in, long len)
938{ 1215 {
939 unsigned char *p; 1216 const unsigned char *p;
940 if(len < 2) return 0; 1217 if (len < 2) return 0;
941 p = *in; 1218 p = *in;
942 if(!p[0] && !p[1]) { 1219 if (!p[0] && !p[1])
1220 {
943 *in += 2; 1221 *in += 2;
944 return 1; 1222 return 1;
945 } 1223 }
946 return 0; 1224 return 0;
947} 1225 }
948 1226
949/* Check an ASN1 tag and length: a bit like ASN1_get_object 1227/* Check an ASN1 tag and length: a bit like ASN1_get_object
950 * but it sets the length for indefinite length constructed 1228 * but it sets the length for indefinite length constructed
@@ -953,25 +1231,32 @@ static int asn1_check_eoc(unsigned char **in, long len)
953 * header length just read. 1231 * header length just read.
954 */ 1232 */
955 1233
956static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass, char *inf, char *cst, 1234static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
957 unsigned char **in, long len, int exptag, int expclass, char opt, ASN1_TLC *ctx) 1235 char *inf, char *cst,
958{ 1236 const unsigned char **in, long len,
1237 int exptag, int expclass, char opt,
1238 ASN1_TLC *ctx)
1239 {
959 int i; 1240 int i;
960 int ptag, pclass; 1241 int ptag, pclass;
961 long plen; 1242 long plen;
962 unsigned char *p, *q; 1243 const unsigned char *p, *q;
963 p = *in; 1244 p = *in;
964 q = p; 1245 q = p;
965 1246
966 if(ctx && ctx->valid) { 1247 if (ctx && ctx->valid)
1248 {
967 i = ctx->ret; 1249 i = ctx->ret;
968 plen = ctx->plen; 1250 plen = ctx->plen;
969 pclass = ctx->pclass; 1251 pclass = ctx->pclass;
970 ptag = ctx->ptag; 1252 ptag = ctx->ptag;
971 p += ctx->hdrlen; 1253 p += ctx->hdrlen;
972 } else { 1254 }
1255 else
1256 {
973 i = ASN1_get_object(&p, &plen, &ptag, &pclass, len); 1257 i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
974 if(ctx) { 1258 if (ctx)
1259 {
975 ctx->ret = i; 1260 ctx->ret = i;
976 ctx->plen = plen; 1261 ctx->plen = plen;
977 ctx->pclass = pclass; 1262 ctx->pclass = pclass;
@@ -981,43 +1266,57 @@ static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass, char *i
981 /* If definite length, and no error, length + 1266 /* If definite length, and no error, length +
982 * header can't exceed total amount of data available. 1267 * header can't exceed total amount of data available.
983 */ 1268 */
984 if(!(i & 0x81) && ((plen + ctx->hdrlen) > len)) { 1269 if (!(i & 0x81) && ((plen + ctx->hdrlen) > len))
985 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_TOO_LONG); 1270 {
1271 ASN1err(ASN1_F_ASN1_CHECK_TLEN,
1272 ASN1_R_TOO_LONG);
986 asn1_tlc_clear(ctx); 1273 asn1_tlc_clear(ctx);
987 return 0; 1274 return 0;
1275 }
988 } 1276 }
989 } 1277 }
990 }
991 1278
992 if(i & 0x80) { 1279 if (i & 0x80)
1280 {
993 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER); 1281 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
994 asn1_tlc_clear(ctx); 1282 asn1_tlc_clear(ctx);
995 return 0; 1283 return 0;
996 } 1284 }
997 if(exptag >= 0) { 1285 if (exptag >= 0)
998 if((exptag != ptag) || (expclass != pclass)) { 1286 {
999 /* If type is OPTIONAL, not an error, but indicate missing 1287 if ((exptag != ptag) || (expclass != pclass))
1000 * type. 1288 {
1289 /* If type is OPTIONAL, not an error:
1290 * indicate missing type.
1001 */ 1291 */
1002 if(opt) return -1; 1292 if (opt) return -1;
1003 asn1_tlc_clear(ctx); 1293 asn1_tlc_clear(ctx);
1004 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG); 1294 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
1005 return 0; 1295 return 0;
1006 } 1296 }
1007 /* We have a tag and class match, so assume we are going to do something with it */ 1297 /* We have a tag and class match:
1298 * assume we are going to do something with it */
1008 asn1_tlc_clear(ctx); 1299 asn1_tlc_clear(ctx);
1009 } 1300 }
1301
1302 if (i & 1)
1303 plen = len - (p - q);
1010 1304
1011 if(i & 1) plen = len - (p - q); 1305 if (inf)
1306 *inf = i & 1;
1012 1307
1013 if(inf) *inf = i & 1; 1308 if (cst)
1309 *cst = i & V_ASN1_CONSTRUCTED;
1014 1310
1015 if(cst) *cst = i & V_ASN1_CONSTRUCTED; 1311 if (olen)
1312 *olen = plen;
1016 1313
1017 if(olen) *olen = plen; 1314 if (oclass)
1018 if(oclass) *oclass = pclass; 1315 *oclass = pclass;
1019 if(otag) *otag = ptag; 1316
1317 if (otag)
1318 *otag = ptag;
1020 1319
1021 *in = p; 1320 *in = p;
1022 return 1; 1321 return 1;
1023} 1322 }