| 1 |
/* Copyright (C) 1995-1998 Eric Young ([email protected]) |
| 2 |
* All rights reserved. |
| 3 |
* |
| 4 |
* This package is an SSL implementation written |
| 5 |
* by Eric Young ([email protected]). |
| 6 |
* The implementation was written so as to conform with Netscapes SSL. |
| 7 |
* |
| 8 |
* This library is free for commercial and non-commercial use as long as |
| 9 |
* the following conditions are aheared to. The following conditions |
| 10 |
* apply to all code found in this distribution, be it the RC4, RSA, |
| 11 |
* lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 |
* included with this distribution is covered by the same copyright terms |
| 13 |
* except that the holder is Tim Hudson ([email protected]). |
| 14 |
* |
| 15 |
* Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 |
* the code are not to be removed. |
| 17 |
* If this package is used in a product, Eric Young should be given attribution |
| 18 |
* as the author of the parts of the library used. |
| 19 |
* This can be in the form of a textual message at program startup or |
| 20 |
* in documentation (online or textual) provided with the package. |
| 21 |
* |
| 22 |
* Redistribution and use in source and binary forms, with or without |
| 23 |
* modification, are permitted provided that the following conditions |
| 24 |
* are met: |
| 25 |
* 1. Redistributions of source code must retain the copyright |
| 26 |
* notice, this list of conditions and the following disclaimer. |
| 27 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 28 |
* notice, this list of conditions and the following disclaimer in the |
| 29 |
* documentation and/or other materials provided with the distribution. |
| 30 |
* 3. All advertising materials mentioning features or use of this software |
| 31 |
* must display the following acknowledgement: |
| 32 |
* "This product includes cryptographic software written by |
| 33 |
* Eric Young ([email protected])" |
| 34 |
* The word 'cryptographic' can be left out if the rouines from the library |
| 35 |
* being used are not cryptographic related :-). |
| 36 |
* 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 |
* the apps directory (application code) you must include an acknowledgement: |
| 38 |
* "This product includes software written by Tim Hudson ([email protected])" |
| 39 |
* |
| 40 |
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 |
* SUCH DAMAGE. |
| 51 |
* |
| 52 |
* The licence and distribution terms for any publically available version or |
| 53 |
* derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 |
* copied and put under another distribution licence |
| 55 |
* [including the GNU Public Licence.] */ |
| 56 |
|
| 57 |
#include <openssl/asn1.h> |
| 58 |
|
| 59 |
#include <string.h> |
| 60 |
|
| 61 |
#include <openssl/asn1t.h> |
| 62 |
#include <openssl/buf.h> |
| 63 |
#include <openssl/err.h> |
| 64 |
#include <openssl/mem.h> |
| 65 |
|
| 66 |
#include "../internal.h" |
| 67 |
|
| 68 |
static int asn1_check_eoc(const unsigned char **in, long len); |
| 69 |
static int asn1_find_end(const unsigned char **in, long len, char inf); |
| 70 |
|
| 71 |
static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len, |
| 72 |
char inf, int tag, int aclass, int depth); |
| 73 |
|
| 74 |
static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen); |
| 75 |
|
| 76 |
static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass, |
| 77 |
char *inf, char *cst, |
| 78 |
const unsigned char **in, long len, |
| 79 |
int exptag, int expclass, char opt, ASN1_TLC *ctx); |
| 80 |
|
| 81 |
static int asn1_template_ex_d2i(ASN1_VALUE **pval, |
| 82 |
const unsigned char **in, long len, |
| 83 |
const ASN1_TEMPLATE *tt, char opt, |
| 84 |
ASN1_TLC *ctx); |
| 85 |
static int asn1_template_noexp_d2i(ASN1_VALUE **val, |
| 86 |
const unsigned char **in, long len, |
| 87 |
const ASN1_TEMPLATE *tt, char opt, |
| 88 |
ASN1_TLC *ctx); |
| 89 |
static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, |
| 90 |
const unsigned char **in, long len, |
| 91 |
const ASN1_ITEM *it, |
| 92 |
int tag, int aclass, char opt, |
| 93 |
ASN1_TLC *ctx); |
| 94 |
|
| 95 |
/* Table to convert tags to bit values, used for MSTRING type */ |
| 96 |
static const unsigned long tag2bit[32] = { |
| 97 |
0, 0, 0, B_ASN1_BIT_STRING, /* tags 0 - 3 */ |
| 98 |
B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN, /* tags 4- 7 */ |
| 99 |
B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, /* tags |
| 100 |
* 8-11 */ |
| 101 |
B_ASN1_UTF8STRING, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, /* tags |
| 102 |
* 12-15 |
| 103 |
*/ |
| 104 |
B_ASN1_SEQUENCE, 0, B_ASN1_NUMERICSTRING, B_ASN1_PRINTABLESTRING, /* tags |
| 105 |
* 16-19 |
| 106 |
*/ |
| 107 |
B_ASN1_T61STRING, B_ASN1_VIDEOTEXSTRING, B_ASN1_IA5STRING, /* tags 20-22 */ |
| 108 |
B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME, /* tags 23-24 */ |
| 109 |
B_ASN1_GRAPHICSTRING, B_ASN1_ISO64STRING, B_ASN1_GENERALSTRING, /* tags |
| 110 |
* 25-27 */ |
| 111 |
B_ASN1_UNIVERSALSTRING, B_ASN1_UNKNOWN, B_ASN1_BMPSTRING, B_ASN1_UNKNOWN, /* tags |
| 112 |
* 28-31 |
| 113 |
*/ |
| 114 |
}; |
| 115 |
|
| 116 |
unsigned long ASN1_tag2bit(int tag) |
| 117 |
{ |
| 118 |
if ((tag < 0) || (tag > 30)) |
| 119 |
return 0; |
| 120 |
return tag2bit[tag]; |
| 121 |
} |
| 122 |
|
| 123 |
/* Macro to initialize and invalidate the cache */ |
| 124 |
|
| 125 |
#define asn1_tlc_clear(c) if (c) (c)->valid = 0 |
| 126 |
/* Version to avoid compiler warning about 'c' always non-NULL */ |
| 127 |
#define asn1_tlc_clear_nc(c) (c)->valid = 0 |
| 128 |
|
| 129 |
/* |
| 130 |
* Decode an ASN1 item, this currently behaves just like a standard 'd2i' |
| 131 |
* function. 'in' points to a buffer to read the data from, in future we |
| 132 |
* will have more advanced versions that can input data a piece at a time and |
| 133 |
* this will simply be a special case. |
| 134 |
*/ |
| 135 |
|
| 136 |
ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval, |
| 137 |
const unsigned char **in, long len, |
| 138 |
const ASN1_ITEM *it) |
| 139 |
{ |
| 140 |
ASN1_TLC c; |
| 141 |
ASN1_VALUE *ptmpval = NULL; |
| 142 |
if (!pval) |
| 143 |
pval = &ptmpval; |
| 144 |
asn1_tlc_clear_nc(&c); |
| 145 |
if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0) |
| 146 |
return *pval; |
| 147 |
return NULL; |
| 148 |
} |
| 149 |
|
| 150 |
int ASN1_template_d2i(ASN1_VALUE **pval, |
| 151 |
const unsigned char **in, long len, |
| 152 |
const ASN1_TEMPLATE *tt) |
| 153 |
{ |
| 154 |
ASN1_TLC c; |
| 155 |
asn1_tlc_clear_nc(&c); |
| 156 |
return asn1_template_ex_d2i(pval, in, len, tt, 0, &c); |
| 157 |
} |
| 158 |
|
| 159 |
/* |
| 160 |
* Decode an item, taking care of IMPLICIT tagging, if any. If 'opt' set and |
| 161 |
* tag mismatch return -1 to handle OPTIONAL |
| 162 |
*/ |
| 163 |
|
| 164 |
int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, |
| 165 |
const ASN1_ITEM *it, |
| 166 |
int tag, int aclass, char opt, ASN1_TLC *ctx) |
| 167 |
{ |
| 168 |
const ASN1_TEMPLATE *tt, *errtt = NULL; |
| 169 |
const ASN1_COMPAT_FUNCS *cf; |
| 170 |
const ASN1_EXTERN_FUNCS *ef; |
| 171 |
const ASN1_AUX *aux = it->funcs; |
| 172 |
ASN1_aux_cb *asn1_cb; |
| 173 |
const unsigned char *p = NULL, *q; |
| 174 |
unsigned char *wp = NULL; /* BIG FAT WARNING! BREAKS CONST WHERE USED */ |
| 175 |
unsigned char imphack = 0, oclass; |
| 176 |
char seq_eoc, seq_nolen, cst, isopt; |
| 177 |
long tmplen; |
| 178 |
int i; |
| 179 |
int otag; |
| 180 |
int ret = 0; |
| 181 |
ASN1_VALUE **pchptr, *ptmpval; |
| 182 |
int combine = aclass & ASN1_TFLG_COMBINE; |
| 183 |
if (!pval) |
| 184 |
return 0; |
| 185 |
if (aux && aux->asn1_cb) |
| 186 |
asn1_cb = aux->asn1_cb; |
| 187 |
else |
| 188 |
asn1_cb = 0; |
| 189 |
|
| 190 |
switch (it->itype) { |
| 191 |
case ASN1_ITYPE_PRIMITIVE: |
| 192 |
if (it->templates) { |
| 193 |
/* |
| 194 |
* tagging or OPTIONAL is currently illegal on an item template |
| 195 |
* because the flags can't get passed down. In practice this |
| 196 |
* isn't a problem: we include the relevant flags from the item |
| 197 |
* template in the template itself. |
| 198 |
*/ |
| 199 |
if ((tag != -1) || opt) { |
| 200 |
OPENSSL_PUT_ERROR(ASN1, |
| 201 |
ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE); |
| 202 |
goto err; |
| 203 |
} |
| 204 |
return asn1_template_ex_d2i(pval, in, len, |
| 205 |
it->templates, opt, ctx); |
| 206 |
} |
| 207 |
return asn1_d2i_ex_primitive(pval, in, len, it, |
| 208 |
tag, aclass, opt, ctx); |
| 209 |
break; |
| 210 |
|
| 211 |
case ASN1_ITYPE_MSTRING: |
| 212 |
p = *in; |
| 213 |
/* Just read in tag and class */ |
| 214 |
ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL, |
| 215 |
&p, len, -1, 0, 1, ctx); |
| 216 |
if (!ret) { |
| 217 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 218 |
goto err; |
| 219 |
} |
| 220 |
|
| 221 |
/* Must be UNIVERSAL class */ |
| 222 |
if (oclass != V_ASN1_UNIVERSAL) { |
| 223 |
/* If OPTIONAL, assume this is OK */ |
| 224 |
if (opt) |
| 225 |
return -1; |
| 226 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_MSTRING_NOT_UNIVERSAL); |
| 227 |
goto err; |
| 228 |
} |
| 229 |
/* Check tag matches bit map */ |
| 230 |
if (!(ASN1_tag2bit(otag) & it->utype)) { |
| 231 |
/* If OPTIONAL, assume this is OK */ |
| 232 |
if (opt) |
| 233 |
return -1; |
| 234 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_MSTRING_WRONG_TAG); |
| 235 |
goto err; |
| 236 |
} |
| 237 |
return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx); |
| 238 |
|
| 239 |
case ASN1_ITYPE_EXTERN: |
| 240 |
/* Use new style d2i */ |
| 241 |
ef = it->funcs; |
| 242 |
return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx); |
| 243 |
|
| 244 |
case ASN1_ITYPE_COMPAT: |
| 245 |
/* we must resort to old style evil hackery */ |
| 246 |
cf = it->funcs; |
| 247 |
|
| 248 |
/* If OPTIONAL see if it is there */ |
| 249 |
if (opt) { |
| 250 |
int exptag; |
| 251 |
p = *in; |
| 252 |
if (tag == -1) |
| 253 |
exptag = it->utype; |
| 254 |
else |
| 255 |
exptag = tag; |
| 256 |
/* |
| 257 |
* Don't care about anything other than presence of expected tag |
| 258 |
*/ |
| 259 |
|
| 260 |
ret = asn1_check_tlen(NULL, NULL, NULL, NULL, NULL, |
| 261 |
&p, len, exptag, aclass, 1, ctx); |
| 262 |
if (!ret) { |
| 263 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 264 |
goto err; |
| 265 |
} |
| 266 |
if (ret == -1) |
| 267 |
return -1; |
| 268 |
} |
| 269 |
|
| 270 |
/* |
| 271 |
* This is the old style evil hack IMPLICIT handling: since the |
| 272 |
* underlying code is expecting a tag and class other than the one |
| 273 |
* present we change the buffer temporarily then change it back |
| 274 |
* afterwards. This doesn't and never did work for tags > 30. Yes |
| 275 |
* this is *horrible* but it is only needed for old style d2i which |
| 276 |
* will hopefully not be around for much longer. FIXME: should copy |
| 277 |
* the buffer then modify it so the input buffer can be const: we |
| 278 |
* should *always* copy because the old style d2i might modify the |
| 279 |
* buffer. |
| 280 |
*/ |
| 281 |
|
| 282 |
if (tag != -1) { |
| 283 |
wp = *(unsigned char **)in; |
| 284 |
imphack = *wp; |
| 285 |
if (p == NULL) { |
| 286 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 287 |
goto err; |
| 288 |
} |
| 289 |
*wp = (unsigned char)((*p & V_ASN1_CONSTRUCTED) |
| 290 |
| it->utype); |
| 291 |
} |
| 292 |
|
| 293 |
ptmpval = cf->asn1_d2i(pval, in, len); |
| 294 |
|
| 295 |
if (tag != -1) |
| 296 |
*wp = imphack; |
| 297 |
|
| 298 |
if (ptmpval) |
| 299 |
return 1; |
| 300 |
|
| 301 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 302 |
goto err; |
| 303 |
|
| 304 |
case ASN1_ITYPE_CHOICE: |
| 305 |
if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL)) |
| 306 |
goto auxerr; |
| 307 |
|
| 308 |
if (*pval) { |
| 309 |
/* Free up and zero CHOICE value if initialised */ |
| 310 |
i = asn1_get_choice_selector(pval, it); |
| 311 |
if ((i >= 0) && (i < it->tcount)) { |
| 312 |
tt = it->templates + i; |
| 313 |
pchptr = asn1_get_field_ptr(pval, tt); |
| 314 |
ASN1_template_free(pchptr, tt); |
| 315 |
asn1_set_choice_selector(pval, -1, it); |
| 316 |
} |
| 317 |
} else if (!ASN1_item_ex_new(pval, it)) { |
| 318 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 319 |
goto err; |
| 320 |
} |
| 321 |
/* CHOICE type, try each possibility in turn */ |
| 322 |
p = *in; |
| 323 |
for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) { |
| 324 |
pchptr = asn1_get_field_ptr(pval, tt); |
| 325 |
/* |
| 326 |
* We mark field as OPTIONAL so its absence can be recognised. |
| 327 |
*/ |
| 328 |
ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx); |
| 329 |
/* If field not present, try the next one */ |
| 330 |
if (ret == -1) |
| 331 |
continue; |
| 332 |
/* If positive return, read OK, break loop */ |
| 333 |
if (ret > 0) |
| 334 |
break; |
| 335 |
/* Otherwise must be an ASN1 parsing error */ |
| 336 |
errtt = tt; |
| 337 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 338 |
goto err; |
| 339 |
} |
| 340 |
|
| 341 |
/* Did we fall off the end without reading anything? */ |
| 342 |
if (i == it->tcount) { |
| 343 |
/* If OPTIONAL, this is OK */ |
| 344 |
if (opt) { |
| 345 |
/* Free and zero it */ |
| 346 |
ASN1_item_ex_free(pval, it); |
| 347 |
return -1; |
| 348 |
} |
| 349 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NO_MATCHING_CHOICE_TYPE); |
| 350 |
goto err; |
| 351 |
} |
| 352 |
|
| 353 |
asn1_set_choice_selector(pval, i, it); |
| 354 |
if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL)) |
| 355 |
goto auxerr; |
| 356 |
*in = p; |
| 357 |
return 1; |
| 358 |
|
| 359 |
case ASN1_ITYPE_NDEF_SEQUENCE: |
| 360 |
case ASN1_ITYPE_SEQUENCE: |
| 361 |
p = *in; |
| 362 |
tmplen = len; |
| 363 |
|
| 364 |
/* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */ |
| 365 |
if (tag == -1) { |
| 366 |
tag = V_ASN1_SEQUENCE; |
| 367 |
aclass = V_ASN1_UNIVERSAL; |
| 368 |
} |
| 369 |
/* Get SEQUENCE length and update len, p */ |
| 370 |
ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst, |
| 371 |
&p, len, tag, aclass, opt, ctx); |
| 372 |
if (!ret) { |
| 373 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 374 |
goto err; |
| 375 |
} else if (ret == -1) |
| 376 |
return -1; |
| 377 |
if (aux && (aux->flags & ASN1_AFLG_BROKEN)) { |
| 378 |
len = tmplen - (p - *in); |
| 379 |
seq_nolen = 1; |
| 380 |
} |
| 381 |
/* If indefinite we don't do a length check */ |
| 382 |
else |
| 383 |
seq_nolen = seq_eoc; |
| 384 |
if (!cst) { |
| 385 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_SEQUENCE_NOT_CONSTRUCTED); |
| 386 |
goto err; |
| 387 |
} |
| 388 |
|
| 389 |
if (!*pval && !ASN1_item_ex_new(pval, it)) { |
| 390 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 391 |
goto err; |
| 392 |
} |
| 393 |
|
| 394 |
if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL)) |
| 395 |
goto auxerr; |
| 396 |
|
| 397 |
/* Free up and zero any ADB found */ |
| 398 |
for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) { |
| 399 |
if (tt->flags & ASN1_TFLG_ADB_MASK) { |
| 400 |
const ASN1_TEMPLATE *seqtt; |
| 401 |
ASN1_VALUE **pseqval; |
| 402 |
seqtt = asn1_do_adb(pval, tt, 1); |
| 403 |
pseqval = asn1_get_field_ptr(pval, seqtt); |
| 404 |
ASN1_template_free(pseqval, seqtt); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
/* Get each field entry */ |
| 409 |
for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) { |
| 410 |
const ASN1_TEMPLATE *seqtt; |
| 411 |
ASN1_VALUE **pseqval; |
| 412 |
seqtt = asn1_do_adb(pval, tt, 1); |
| 413 |
if (!seqtt) |
| 414 |
goto err; |
| 415 |
pseqval = asn1_get_field_ptr(pval, seqtt); |
| 416 |
/* Have we ran out of data? */ |
| 417 |
if (!len) |
| 418 |
break; |
| 419 |
q = p; |
| 420 |
if (asn1_check_eoc(&p, len)) { |
| 421 |
if (!seq_eoc) { |
| 422 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNEXPECTED_EOC); |
| 423 |
goto err; |
| 424 |
} |
| 425 |
len -= p - q; |
| 426 |
seq_eoc = 0; |
| 427 |
q = p; |
| 428 |
break; |
| 429 |
} |
| 430 |
/* |
| 431 |
* This determines the OPTIONAL flag value. The field cannot be |
| 432 |
* omitted if it is the last of a SEQUENCE and there is still |
| 433 |
* data to be read. This isn't strictly necessary but it |
| 434 |
* increases efficiency in some cases. |
| 435 |
*/ |
| 436 |
if (i == (it->tcount - 1)) |
| 437 |
isopt = 0; |
| 438 |
else |
| 439 |
isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL); |
| 440 |
/* |
| 441 |
* attempt to read in field, allowing each to be OPTIONAL |
| 442 |
*/ |
| 443 |
|
| 444 |
ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx); |
| 445 |
if (!ret) { |
| 446 |
errtt = seqtt; |
| 447 |
goto err; |
| 448 |
} else if (ret == -1) { |
| 449 |
/* |
| 450 |
* OPTIONAL component absent. Free and zero the field. |
| 451 |
*/ |
| 452 |
ASN1_template_free(pseqval, seqtt); |
| 453 |
continue; |
| 454 |
} |
| 455 |
/* Update length */ |
| 456 |
len -= p - q; |
| 457 |
} |
| 458 |
|
| 459 |
/* Check for EOC if expecting one */ |
| 460 |
if (seq_eoc && !asn1_check_eoc(&p, len)) { |
| 461 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_MISSING_EOC); |
| 462 |
goto err; |
| 463 |
} |
| 464 |
/* Check all data read */ |
| 465 |
if (!seq_nolen && len) { |
| 466 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_SEQUENCE_LENGTH_MISMATCH); |
| 467 |
goto err; |
| 468 |
} |
| 469 |
|
| 470 |
/* |
| 471 |
* If we get here we've got no more data in the SEQUENCE, however we |
| 472 |
* may not have read all fields so check all remaining are OPTIONAL |
| 473 |
* and clear any that are. |
| 474 |
*/ |
| 475 |
for (; i < it->tcount; tt++, i++) { |
| 476 |
const ASN1_TEMPLATE *seqtt; |
| 477 |
seqtt = asn1_do_adb(pval, tt, 1); |
| 478 |
if (!seqtt) |
| 479 |
goto err; |
| 480 |
if (seqtt->flags & ASN1_TFLG_OPTIONAL) { |
| 481 |
ASN1_VALUE **pseqval; |
| 482 |
pseqval = asn1_get_field_ptr(pval, seqtt); |
| 483 |
ASN1_template_free(pseqval, seqtt); |
| 484 |
} else { |
| 485 |
errtt = seqtt; |
| 486 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_FIELD_MISSING); |
| 487 |
goto err; |
| 488 |
} |
| 489 |
} |
| 490 |
/* Save encoding */ |
| 491 |
if (!asn1_enc_save(pval, *in, p - *in, it)) |
| 492 |
goto auxerr; |
| 493 |
if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL)) |
| 494 |
goto auxerr; |
| 495 |
*in = p; |
| 496 |
return 1; |
| 497 |
|
| 498 |
default: |
| 499 |
return 0; |
| 500 |
} |
| 501 |
auxerr: |
| 502 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_AUX_ERROR); |
| 503 |
err: |
| 504 |
if (combine == 0) |
| 505 |
ASN1_item_ex_free(pval, it); |
| 506 |
if (errtt) |
| 507 |
ERR_add_error_data(4, "Field=", errtt->field_name, |
| 508 |
", Type=", it->sname); |
| 509 |
else |
| 510 |
ERR_add_error_data(2, "Type=", it->sname); |
| 511 |
return 0; |
| 512 |
} |
| 513 |
|
| 514 |
/* |
| 515 |
* Templates are handled with two separate functions. One handles any |
| 516 |
* EXPLICIT tag and the other handles the rest. |
| 517 |
*/ |
| 518 |
|
| 519 |
static int asn1_template_ex_d2i(ASN1_VALUE **val, |
| 520 |
const unsigned char **in, long inlen, |
| 521 |
const ASN1_TEMPLATE *tt, char opt, |
| 522 |
ASN1_TLC *ctx) |
| 523 |
{ |
| 524 |
int flags, aclass; |
| 525 |
int ret; |
| 526 |
long len; |
| 527 |
const unsigned char *p, *q; |
| 528 |
char exp_eoc; |
| 529 |
if (!val) |
| 530 |
return 0; |
| 531 |
flags = tt->flags; |
| 532 |
aclass = flags & ASN1_TFLG_TAG_CLASS; |
| 533 |
|
| 534 |
p = *in; |
| 535 |
|
| 536 |
/* Check if EXPLICIT tag expected */ |
| 537 |
if (flags & ASN1_TFLG_EXPTAG) { |
| 538 |
char cst; |
| 539 |
/* |
| 540 |
* Need to work out amount of data available to the inner content and |
| 541 |
* where it starts: so read in EXPLICIT header to get the info. |
| 542 |
*/ |
| 543 |
ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst, |
| 544 |
&p, inlen, tt->tag, aclass, opt, ctx); |
| 545 |
q = p; |
| 546 |
if (!ret) { |
| 547 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 548 |
return 0; |
| 549 |
} else if (ret == -1) |
| 550 |
return -1; |
| 551 |
if (!cst) { |
| 552 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED); |
| 553 |
return 0; |
| 554 |
} |
| 555 |
/* We've found the field so it can't be OPTIONAL now */ |
| 556 |
ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx); |
| 557 |
if (!ret) { |
| 558 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 559 |
return 0; |
| 560 |
} |
| 561 |
/* We read the field in OK so update length */ |
| 562 |
len -= p - q; |
| 563 |
if (exp_eoc) { |
| 564 |
/* If NDEF we must have an EOC here */ |
| 565 |
if (!asn1_check_eoc(&p, len)) { |
| 566 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_MISSING_EOC); |
| 567 |
goto err; |
| 568 |
} |
| 569 |
} else { |
| 570 |
/* |
| 571 |
* Otherwise we must hit the EXPLICIT tag end or its an error |
| 572 |
*/ |
| 573 |
if (len) { |
| 574 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_EXPLICIT_LENGTH_MISMATCH); |
| 575 |
goto err; |
| 576 |
} |
| 577 |
} |
| 578 |
} else |
| 579 |
return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx); |
| 580 |
|
| 581 |
*in = p; |
| 582 |
return 1; |
| 583 |
|
| 584 |
err: |
| 585 |
ASN1_template_free(val, tt); |
| 586 |
return 0; |
| 587 |
} |
| 588 |
|
| 589 |
static int asn1_template_noexp_d2i(ASN1_VALUE **val, |
| 590 |
const unsigned char **in, long len, |
| 591 |
const ASN1_TEMPLATE *tt, char opt, |
| 592 |
ASN1_TLC *ctx) |
| 593 |
{ |
| 594 |
int flags, aclass; |
| 595 |
int ret; |
| 596 |
const unsigned char *p; |
| 597 |
if (!val) |
| 598 |
return 0; |
| 599 |
flags = tt->flags; |
| 600 |
aclass = flags & ASN1_TFLG_TAG_CLASS; |
| 601 |
|
| 602 |
p = *in; |
| 603 |
|
| 604 |
if (flags & ASN1_TFLG_SK_MASK) { |
| 605 |
/* SET OF, SEQUENCE OF */ |
| 606 |
int sktag, skaclass; |
| 607 |
char sk_eoc; |
| 608 |
/* First work out expected inner tag value */ |
| 609 |
if (flags & ASN1_TFLG_IMPTAG) { |
| 610 |
sktag = tt->tag; |
| 611 |
skaclass = aclass; |
| 612 |
} else { |
| 613 |
skaclass = V_ASN1_UNIVERSAL; |
| 614 |
if (flags & ASN1_TFLG_SET_OF) |
| 615 |
sktag = V_ASN1_SET; |
| 616 |
else |
| 617 |
sktag = V_ASN1_SEQUENCE; |
| 618 |
} |
| 619 |
/* Get the tag */ |
| 620 |
ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL, |
| 621 |
&p, len, sktag, skaclass, opt, ctx); |
| 622 |
if (!ret) { |
| 623 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 624 |
return 0; |
| 625 |
} else if (ret == -1) |
| 626 |
return -1; |
| 627 |
if (!*val) |
| 628 |
*val = (ASN1_VALUE *)sk_new_null(); |
| 629 |
else { |
| 630 |
/* |
| 631 |
* We've got a valid STACK: free up any items present |
| 632 |
*/ |
| 633 |
STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val; |
| 634 |
ASN1_VALUE *vtmp; |
| 635 |
while (sk_ASN1_VALUE_num(sktmp) > 0) { |
| 636 |
vtmp = sk_ASN1_VALUE_pop(sktmp); |
| 637 |
ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item)); |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
if (!*val) { |
| 642 |
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE); |
| 643 |
goto err; |
| 644 |
} |
| 645 |
|
| 646 |
/* Read as many items as we can */ |
| 647 |
while (len > 0) { |
| 648 |
ASN1_VALUE *skfield; |
| 649 |
const unsigned char *q = p; |
| 650 |
/* See if EOC found */ |
| 651 |
if (asn1_check_eoc(&p, len)) { |
| 652 |
if (!sk_eoc) { |
| 653 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNEXPECTED_EOC); |
| 654 |
goto err; |
| 655 |
} |
| 656 |
len -= p - q; |
| 657 |
sk_eoc = 0; |
| 658 |
break; |
| 659 |
} |
| 660 |
skfield = NULL; |
| 661 |
if (!ASN1_item_ex_d2i(&skfield, &p, len, |
| 662 |
ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx)) { |
| 663 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 664 |
goto err; |
| 665 |
} |
| 666 |
len -= p - q; |
| 667 |
if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) { |
| 668 |
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE); |
| 669 |
goto err; |
| 670 |
} |
| 671 |
} |
| 672 |
if (sk_eoc) { |
| 673 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_MISSING_EOC); |
| 674 |
goto err; |
| 675 |
} |
| 676 |
} else if (flags & ASN1_TFLG_IMPTAG) { |
| 677 |
/* IMPLICIT tagging */ |
| 678 |
ret = ASN1_item_ex_d2i(val, &p, len, |
| 679 |
ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt, |
| 680 |
ctx); |
| 681 |
if (!ret) { |
| 682 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 683 |
goto err; |
| 684 |
} else if (ret == -1) |
| 685 |
return -1; |
| 686 |
} else { |
| 687 |
/* Nothing special */ |
| 688 |
ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item), |
| 689 |
-1, tt->flags & ASN1_TFLG_COMBINE, opt, ctx); |
| 690 |
if (!ret) { |
| 691 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 692 |
goto err; |
| 693 |
} else if (ret == -1) |
| 694 |
return -1; |
| 695 |
} |
| 696 |
|
| 697 |
*in = p; |
| 698 |
return 1; |
| 699 |
|
| 700 |
err: |
| 701 |
ASN1_template_free(val, tt); |
| 702 |
return 0; |
| 703 |
} |
| 704 |
|
| 705 |
static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, |
| 706 |
const unsigned char **in, long inlen, |
| 707 |
const ASN1_ITEM *it, |
| 708 |
int tag, int aclass, char opt, ASN1_TLC *ctx) |
| 709 |
{ |
| 710 |
int ret = 0, utype; |
| 711 |
long plen; |
| 712 |
char cst, inf, free_cont = 0; |
| 713 |
const unsigned char *p; |
| 714 |
BUF_MEM buf = {0, NULL, 0 }; |
| 715 |
const unsigned char *cont = NULL; |
| 716 |
long len; |
| 717 |
if (!pval) { |
| 718 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_NULL); |
| 719 |
return 0; /* Should never happen */ |
| 720 |
} |
| 721 |
|
| 722 |
if (it->itype == ASN1_ITYPE_MSTRING) { |
| 723 |
utype = tag; |
| 724 |
tag = -1; |
| 725 |
} else |
| 726 |
utype = it->utype; |
| 727 |
|
| 728 |
if (utype == V_ASN1_ANY) { |
| 729 |
/* If type is ANY need to figure out type from tag */ |
| 730 |
unsigned char oclass; |
| 731 |
if (tag >= 0) { |
| 732 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_TAGGED_ANY); |
| 733 |
return 0; |
| 734 |
} |
| 735 |
if (opt) { |
| 736 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_OPTIONAL_ANY); |
| 737 |
return 0; |
| 738 |
} |
| 739 |
p = *in; |
| 740 |
ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL, |
| 741 |
&p, inlen, -1, 0, 0, ctx); |
| 742 |
if (!ret) { |
| 743 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 744 |
return 0; |
| 745 |
} |
| 746 |
if (oclass != V_ASN1_UNIVERSAL) |
| 747 |
utype = V_ASN1_OTHER; |
| 748 |
} |
| 749 |
if (tag == -1) { |
| 750 |
tag = utype; |
| 751 |
aclass = V_ASN1_UNIVERSAL; |
| 752 |
} |
| 753 |
p = *in; |
| 754 |
/* Check header */ |
| 755 |
ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst, |
| 756 |
&p, inlen, tag, aclass, opt, ctx); |
| 757 |
if (!ret) { |
| 758 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 759 |
return 0; |
| 760 |
} else if (ret == -1) |
| 761 |
return -1; |
| 762 |
ret = 0; |
| 763 |
/* SEQUENCE, SET and "OTHER" are left in encoded form */ |
| 764 |
if ((utype == V_ASN1_SEQUENCE) |
| 765 |
|| (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) { |
| 766 |
/* |
| 767 |
* Clear context cache for type OTHER because the auto clear when we |
| 768 |
* have a exact match wont work |
| 769 |
*/ |
| 770 |
if (utype == V_ASN1_OTHER) { |
| 771 |
asn1_tlc_clear(ctx); |
| 772 |
} |
| 773 |
/* SEQUENCE and SET must be constructed */ |
| 774 |
else if (!cst) { |
| 775 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_TYPE_NOT_CONSTRUCTED); |
| 776 |
return 0; |
| 777 |
} |
| 778 |
|
| 779 |
cont = *in; |
| 780 |
/* If indefinite length constructed find the real end */ |
| 781 |
if (inf) { |
| 782 |
if (!asn1_find_end(&p, plen, inf)) |
| 783 |
goto err; |
| 784 |
len = p - cont; |
| 785 |
} else { |
| 786 |
len = p - cont + plen; |
| 787 |
p += plen; |
| 788 |
} |
| 789 |
} else if (cst) { |
| 790 |
if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN |
| 791 |
|| utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER |
| 792 |
|| utype == V_ASN1_ENUMERATED) { |
| 793 |
/* These types only have primitive encodings. */ |
| 794 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_TYPE_NOT_PRIMITIVE); |
| 795 |
return 0; |
| 796 |
} |
| 797 |
|
| 798 |
/* Free any returned 'buf' content */ |
| 799 |
free_cont = 1; |
| 800 |
/* |
| 801 |
* Should really check the internal tags are correct but some things |
| 802 |
* may get this wrong. The relevant specs say that constructed string |
| 803 |
* types should be OCTET STRINGs internally irrespective of the type. |
| 804 |
* So instead just check for UNIVERSAL class and ignore the tag. |
| 805 |
*/ |
| 806 |
if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) { |
| 807 |
goto err; |
| 808 |
} |
| 809 |
len = buf.length; |
| 810 |
/* Append a final null to string */ |
| 811 |
if (!BUF_MEM_grow_clean(&buf, len + 1)) { |
| 812 |
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE); |
| 813 |
goto err; |
| 814 |
} |
| 815 |
buf.data[len] = 0; |
| 816 |
cont = (const unsigned char *)buf.data; |
| 817 |
} else { |
| 818 |
cont = p; |
| 819 |
len = plen; |
| 820 |
p += plen; |
| 821 |
} |
| 822 |
|
| 823 |
/* We now have content length and type: translate into a structure */ |
| 824 |
/* asn1_ex_c2i may reuse allocated buffer, and so sets free_cont to 0 */ |
| 825 |
if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it)) |
| 826 |
goto err; |
| 827 |
|
| 828 |
*in = p; |
| 829 |
ret = 1; |
| 830 |
err: |
| 831 |
if (free_cont && buf.data) |
| 832 |
OPENSSL_free(buf.data); |
| 833 |
return ret; |
| 834 |
} |
| 835 |
|
| 836 |
/* Translate ASN1 content octets into a structure */ |
| 837 |
|
| 838 |
int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, |
| 839 |
int utype, char *free_cont, const ASN1_ITEM *it) |
| 840 |
{ |
| 841 |
ASN1_VALUE **opval = NULL; |
| 842 |
ASN1_STRING *stmp; |
| 843 |
ASN1_TYPE *typ = NULL; |
| 844 |
int ret = 0; |
| 845 |
const ASN1_PRIMITIVE_FUNCS *pf; |
| 846 |
ASN1_INTEGER **tint; |
| 847 |
pf = it->funcs; |
| 848 |
|
| 849 |
if (pf && pf->prim_c2i) |
| 850 |
return pf->prim_c2i(pval, cont, len, utype, free_cont, it); |
| 851 |
/* If ANY type clear type and set pointer to internal value */ |
| 852 |
if (it->utype == V_ASN1_ANY) { |
| 853 |
if (!*pval) { |
| 854 |
typ = ASN1_TYPE_new(); |
| 855 |
if (typ == NULL) |
| 856 |
goto err; |
| 857 |
*pval = (ASN1_VALUE *)typ; |
| 858 |
} else |
| 859 |
typ = (ASN1_TYPE *)*pval; |
| 860 |
|
| 861 |
if (utype != typ->type) |
| 862 |
ASN1_TYPE_set(typ, utype, NULL); |
| 863 |
opval = pval; |
| 864 |
pval = &typ->value.asn1_value; |
| 865 |
} |
| 866 |
switch (utype) { |
| 867 |
case V_ASN1_OBJECT: |
| 868 |
if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len)) |
| 869 |
goto err; |
| 870 |
break; |
| 871 |
|
| 872 |
case V_ASN1_NULL: |
| 873 |
if (len) { |
| 874 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NULL_IS_WRONG_LENGTH); |
| 875 |
goto err; |
| 876 |
} |
| 877 |
*pval = (ASN1_VALUE *)1; |
| 878 |
break; |
| 879 |
|
| 880 |
case V_ASN1_BOOLEAN: |
| 881 |
if (len != 1) { |
| 882 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH); |
| 883 |
goto err; |
| 884 |
} else { |
| 885 |
ASN1_BOOLEAN *tbool; |
| 886 |
tbool = (ASN1_BOOLEAN *)pval; |
| 887 |
*tbool = *cont; |
| 888 |
} |
| 889 |
break; |
| 890 |
|
| 891 |
case V_ASN1_BIT_STRING: |
| 892 |
if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len)) |
| 893 |
goto err; |
| 894 |
break; |
| 895 |
|
| 896 |
case V_ASN1_INTEGER: |
| 897 |
case V_ASN1_ENUMERATED: |
| 898 |
tint = (ASN1_INTEGER **)pval; |
| 899 |
if (!c2i_ASN1_INTEGER(tint, &cont, len)) |
| 900 |
goto err; |
| 901 |
/* Fixup type to match the expected form */ |
| 902 |
(*tint)->type = utype | ((*tint)->type & V_ASN1_NEG); |
| 903 |
break; |
| 904 |
|
| 905 |
case V_ASN1_OCTET_STRING: |
| 906 |
case V_ASN1_NUMERICSTRING: |
| 907 |
case V_ASN1_PRINTABLESTRING: |
| 908 |
case V_ASN1_T61STRING: |
| 909 |
case V_ASN1_VIDEOTEXSTRING: |
| 910 |
case V_ASN1_IA5STRING: |
| 911 |
case V_ASN1_UTCTIME: |
| 912 |
case V_ASN1_GENERALIZEDTIME: |
| 913 |
case V_ASN1_GRAPHICSTRING: |
| 914 |
case V_ASN1_VISIBLESTRING: |
| 915 |
case V_ASN1_GENERALSTRING: |
| 916 |
case V_ASN1_UNIVERSALSTRING: |
| 917 |
case V_ASN1_BMPSTRING: |
| 918 |
case V_ASN1_UTF8STRING: |
| 919 |
case V_ASN1_OTHER: |
| 920 |
case V_ASN1_SET: |
| 921 |
case V_ASN1_SEQUENCE: |
| 922 |
default: |
| 923 |
if (utype == V_ASN1_BMPSTRING && (len & 1)) { |
| 924 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BMPSTRING_IS_WRONG_LENGTH); |
| 925 |
goto err; |
| 926 |
} |
| 927 |
if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) { |
| 928 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH); |
| 929 |
goto err; |
| 930 |
} |
| 931 |
/* All based on ASN1_STRING and handled the same */ |
| 932 |
if (!*pval) { |
| 933 |
stmp = ASN1_STRING_type_new(utype); |
| 934 |
if (!stmp) { |
| 935 |
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE); |
| 936 |
goto err; |
| 937 |
} |
| 938 |
*pval = (ASN1_VALUE *)stmp; |
| 939 |
} else { |
| 940 |
stmp = (ASN1_STRING *)*pval; |
| 941 |
stmp->type = utype; |
| 942 |
} |
| 943 |
/* If we've already allocated a buffer use it */ |
| 944 |
if (*free_cont) { |
| 945 |
if (stmp->data) |
| 946 |
OPENSSL_free(stmp->data); |
| 947 |
stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */ |
| 948 |
stmp->length = len; |
| 949 |
*free_cont = 0; |
| 950 |
} else { |
| 951 |
if (!ASN1_STRING_set(stmp, cont, len)) { |
| 952 |
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE); |
| 953 |
ASN1_STRING_free(stmp); |
| 954 |
*pval = NULL; |
| 955 |
goto err; |
| 956 |
} |
| 957 |
} |
| 958 |
break; |
| 959 |
} |
| 960 |
/* If ASN1_ANY and NULL type fix up value */ |
| 961 |
if (typ && (utype == V_ASN1_NULL)) |
| 962 |
typ->value.ptr = NULL; |
| 963 |
|
| 964 |
ret = 1; |
| 965 |
err: |
| 966 |
if (!ret) { |
| 967 |
ASN1_TYPE_free(typ); |
| 968 |
if (opval) |
| 969 |
*opval = NULL; |
| 970 |
} |
| 971 |
return ret; |
| 972 |
} |
| 973 |
|
| 974 |
/* |
| 975 |
* This function finds the end of an ASN1 structure when passed its maximum |
| 976 |
* length, whether it is indefinite length and a pointer to the content. This |
| 977 |
* is more efficient than calling asn1_collect because it does not recurse on |
| 978 |
* each indefinite length header. |
| 979 |
*/ |
| 980 |
|
| 981 |
static int asn1_find_end(const unsigned char **in, long len, char inf) |
| 982 |
{ |
| 983 |
int expected_eoc; |
| 984 |
long plen; |
| 985 |
const unsigned char *p = *in, *q; |
| 986 |
/* If not indefinite length constructed just add length */ |
| 987 |
if (inf == 0) { |
| 988 |
*in += len; |
| 989 |
return 1; |
| 990 |
} |
| 991 |
expected_eoc = 1; |
| 992 |
/* |
| 993 |
* Indefinite length constructed form. Find the end when enough EOCs are |
| 994 |
* found. If more indefinite length constructed headers are encountered |
| 995 |
* increment the expected eoc count otherwise just skip to the end of the |
| 996 |
* data. |
| 997 |
*/ |
| 998 |
while (len > 0) { |
| 999 |
if (asn1_check_eoc(&p, len)) { |
| 1000 |
expected_eoc--; |
| 1001 |
if (expected_eoc == 0) |
| 1002 |
break; |
| 1003 |
len -= 2; |
| 1004 |
continue; |
| 1005 |
} |
| 1006 |
q = p; |
| 1007 |
/* Just read in a header: only care about the length */ |
| 1008 |
if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len, |
| 1009 |
-1, 0, 0, NULL)) { |
| 1010 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 1011 |
return 0; |
| 1012 |
} |
| 1013 |
if (inf) |
| 1014 |
expected_eoc++; |
| 1015 |
else |
| 1016 |
p += plen; |
| 1017 |
len -= p - q; |
| 1018 |
} |
| 1019 |
if (expected_eoc) { |
| 1020 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_MISSING_EOC); |
| 1021 |
return 0; |
| 1022 |
} |
| 1023 |
*in = p; |
| 1024 |
return 1; |
| 1025 |
} |
| 1026 |
|
| 1027 |
/* |
| 1028 |
* This function collects the asn1 data from a constructred string type into |
| 1029 |
* a buffer. The values of 'in' and 'len' should refer to the contents of the |
| 1030 |
* constructed type and 'inf' should be set if it is indefinite length. |
| 1031 |
*/ |
| 1032 |
|
| 1033 |
#ifndef ASN1_MAX_STRING_NEST |
| 1034 |
/* |
| 1035 |
* This determines how many levels of recursion are permitted in ASN1 string |
| 1036 |
* types. If it is not limited stack overflows can occur. If set to zero no |
| 1037 |
* recursion is allowed at all. Although zero should be adequate examples |
| 1038 |
* exist that require a value of 1. So 5 should be more than enough. |
| 1039 |
*/ |
| 1040 |
# define ASN1_MAX_STRING_NEST 5 |
| 1041 |
#endif |
| 1042 |
|
| 1043 |
static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len, |
| 1044 |
char inf, int tag, int aclass, int depth) |
| 1045 |
{ |
| 1046 |
const unsigned char *p, *q; |
| 1047 |
long plen; |
| 1048 |
char cst, ininf; |
| 1049 |
p = *in; |
| 1050 |
inf &= 1; |
| 1051 |
/* |
| 1052 |
* If no buffer and not indefinite length constructed just pass over the |
| 1053 |
* encoded data |
| 1054 |
*/ |
| 1055 |
if (!buf && !inf) { |
| 1056 |
*in += len; |
| 1057 |
return 1; |
| 1058 |
} |
| 1059 |
while (len > 0) { |
| 1060 |
q = p; |
| 1061 |
/* Check for EOC */ |
| 1062 |
if (asn1_check_eoc(&p, len)) { |
| 1063 |
/* |
| 1064 |
* EOC is illegal outside indefinite length constructed form |
| 1065 |
*/ |
| 1066 |
if (!inf) { |
| 1067 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNEXPECTED_EOC); |
| 1068 |
return 0; |
| 1069 |
} |
| 1070 |
inf = 0; |
| 1071 |
break; |
| 1072 |
} |
| 1073 |
|
| 1074 |
if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p, |
| 1075 |
len, tag, aclass, 0, NULL)) { |
| 1076 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 1077 |
return 0; |
| 1078 |
} |
| 1079 |
|
| 1080 |
/* If indefinite length constructed update max length */ |
| 1081 |
if (cst) { |
| 1082 |
if (depth >= ASN1_MAX_STRING_NEST) { |
| 1083 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_STRING); |
| 1084 |
return 0; |
| 1085 |
} |
| 1086 |
if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1)) |
| 1087 |
return 0; |
| 1088 |
} else if (plen && !collect_data(buf, &p, plen)) |
| 1089 |
return 0; |
| 1090 |
len -= p - q; |
| 1091 |
} |
| 1092 |
if (inf) { |
| 1093 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_MISSING_EOC); |
| 1094 |
return 0; |
| 1095 |
} |
| 1096 |
*in = p; |
| 1097 |
return 1; |
| 1098 |
} |
| 1099 |
|
| 1100 |
static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen) |
| 1101 |
{ |
| 1102 |
int len; |
| 1103 |
if (buf) { |
| 1104 |
len = buf->length; |
| 1105 |
if (!BUF_MEM_grow_clean(buf, len + plen)) { |
| 1106 |
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE); |
| 1107 |
return 0; |
| 1108 |
} |
| 1109 |
memcpy(buf->data + len, *p, plen); |
| 1110 |
} |
| 1111 |
*p += plen; |
| 1112 |
return 1; |
| 1113 |
} |
| 1114 |
|
| 1115 |
/* Check for ASN1 EOC and swallow it if found */ |
| 1116 |
|
| 1117 |
static int asn1_check_eoc(const unsigned char **in, long len) |
| 1118 |
{ |
| 1119 |
const unsigned char *p; |
| 1120 |
if (len < 2) |
| 1121 |
return 0; |
| 1122 |
p = *in; |
| 1123 |
if (!p[0] && !p[1]) { |
| 1124 |
*in += 2; |
| 1125 |
return 1; |
| 1126 |
} |
| 1127 |
return 0; |
| 1128 |
} |
| 1129 |
|
| 1130 |
/* |
| 1131 |
* Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the |
| 1132 |
* length for indefinite length constructed form, we don't know the exact |
| 1133 |
* length but we can set an upper bound to the amount of data available minus |
| 1134 |
* the header length just read. |
| 1135 |
*/ |
| 1136 |
|
| 1137 |
static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass, |
| 1138 |
char *inf, char *cst, |
| 1139 |
const unsigned char **in, long len, |
| 1140 |
int exptag, int expclass, char opt, ASN1_TLC *ctx) |
| 1141 |
{ |
| 1142 |
int i; |
| 1143 |
int ptag, pclass; |
| 1144 |
long plen; |
| 1145 |
const unsigned char *p, *q; |
| 1146 |
p = *in; |
| 1147 |
q = p; |
| 1148 |
|
| 1149 |
if (ctx && ctx->valid) { |
| 1150 |
i = ctx->ret; |
| 1151 |
plen = ctx->plen; |
| 1152 |
pclass = ctx->pclass; |
| 1153 |
ptag = ctx->ptag; |
| 1154 |
p += ctx->hdrlen; |
| 1155 |
} else { |
| 1156 |
i = ASN1_get_object(&p, &plen, &ptag, &pclass, len); |
| 1157 |
if (ctx) { |
| 1158 |
ctx->ret = i; |
| 1159 |
ctx->plen = plen; |
| 1160 |
ctx->pclass = pclass; |
| 1161 |
ctx->ptag = ptag; |
| 1162 |
ctx->hdrlen = p - q; |
| 1163 |
ctx->valid = 1; |
| 1164 |
/* |
| 1165 |
* If definite length, and no error, length + header can't exceed |
| 1166 |
* total amount of data available. |
| 1167 |
*/ |
| 1168 |
if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) { |
| 1169 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG); |
| 1170 |
asn1_tlc_clear(ctx); |
| 1171 |
return 0; |
| 1172 |
} |
| 1173 |
} |
| 1174 |
} |
| 1175 |
|
| 1176 |
if (i & 0x80) { |
| 1177 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_OBJECT_HEADER); |
| 1178 |
asn1_tlc_clear(ctx); |
| 1179 |
return 0; |
| 1180 |
} |
| 1181 |
if (exptag >= 0) { |
| 1182 |
if ((exptag != ptag) || (expclass != pclass)) { |
| 1183 |
/* |
| 1184 |
* If type is OPTIONAL, not an error: indicate missing type. |
| 1185 |
*/ |
| 1186 |
if (opt) |
| 1187 |
return -1; |
| 1188 |
asn1_tlc_clear(ctx); |
| 1189 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_TAG); |
| 1190 |
return 0; |
| 1191 |
} |
| 1192 |
/* |
| 1193 |
* We have a tag and class match: assume we are going to do something |
| 1194 |
* with it |
| 1195 |
*/ |
| 1196 |
asn1_tlc_clear(ctx); |
| 1197 |
} |
| 1198 |
|
| 1199 |
if (i & 1) |
| 1200 |
plen = len - (p - q); |
| 1201 |
|
| 1202 |
if (inf) |
| 1203 |
*inf = i & 1; |
| 1204 |
|
| 1205 |
if (cst) |
| 1206 |
*cst = i & V_ASN1_CONSTRUCTED; |
| 1207 |
|
| 1208 |
if (olen) |
| 1209 |
*olen = plen; |
| 1210 |
|
| 1211 |
if (oclass) |
| 1212 |
*oclass = pclass; |
| 1213 |
|
| 1214 |
if (otag) |
| 1215 |
*otag = ptag; |
| 1216 |
|
| 1217 |
*in = p; |
| 1218 |
return 1; |
| 1219 |
} |