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