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