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