| OLD | NEW |
| (Empty) |
| 1 /* This Source Code Form is subject to the terms of the Mozilla Public | |
| 2 * License, v. 2.0. If a copy of the MPL was not distributed with this | |
| 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | |
| 4 | |
| 5 /* | |
| 6 * Support for DEcoding ASN.1 data based on BER/DER (Basic/Distinguished | |
| 7 * Encoding Rules). | |
| 8 */ | |
| 9 | |
| 10 /* #define DEBUG_ASN1D_STATES 1 */ | |
| 11 | |
| 12 #ifdef DEBUG_ASN1D_STATES | |
| 13 #include <stdio.h> | |
| 14 #define PR_Assert sec_asn1d_Assert | |
| 15 #endif | |
| 16 | |
| 17 #include <limits.h> | |
| 18 | |
| 19 #include "secasn1.h" | |
| 20 #include "secerr.h" | |
| 21 | |
| 22 typedef enum { | |
| 23 beforeIdentifier, | |
| 24 duringIdentifier, | |
| 25 afterIdentifier, | |
| 26 beforeLength, | |
| 27 duringLength, | |
| 28 afterLength, | |
| 29 beforeBitString, | |
| 30 duringBitString, | |
| 31 duringConstructedString, | |
| 32 duringGroup, | |
| 33 duringLeaf, | |
| 34 duringSaveEncoding, | |
| 35 duringSequence, | |
| 36 afterConstructedString, | |
| 37 afterGroup, | |
| 38 afterExplicit, | |
| 39 afterImplicit, | |
| 40 afterInline, | |
| 41 afterPointer, | |
| 42 afterSaveEncoding, | |
| 43 beforeEndOfContents, | |
| 44 duringEndOfContents, | |
| 45 afterEndOfContents, | |
| 46 beforeChoice, | |
| 47 duringChoice, | |
| 48 afterChoice, | |
| 49 notInUse | |
| 50 } sec_asn1d_parse_place; | |
| 51 | |
| 52 #ifdef DEBUG_ASN1D_STATES | |
| 53 static const char * const place_names[] = { | |
| 54 "beforeIdentifier", | |
| 55 "duringIdentifier", | |
| 56 "afterIdentifier", | |
| 57 "beforeLength", | |
| 58 "duringLength", | |
| 59 "afterLength", | |
| 60 "beforeBitString", | |
| 61 "duringBitString", | |
| 62 "duringConstructedString", | |
| 63 "duringGroup", | |
| 64 "duringLeaf", | |
| 65 "duringSaveEncoding", | |
| 66 "duringSequence", | |
| 67 "afterConstructedString", | |
| 68 "afterGroup", | |
| 69 "afterExplicit", | |
| 70 "afterImplicit", | |
| 71 "afterInline", | |
| 72 "afterPointer", | |
| 73 "afterSaveEncoding", | |
| 74 "beforeEndOfContents", | |
| 75 "duringEndOfContents", | |
| 76 "afterEndOfContents", | |
| 77 "beforeChoice", | |
| 78 "duringChoice", | |
| 79 "afterChoice", | |
| 80 "notInUse" | |
| 81 }; | |
| 82 | |
| 83 static const char * const class_names[] = { | |
| 84 "UNIVERSAL", | |
| 85 "APPLICATION", | |
| 86 "CONTEXT_SPECIFIC", | |
| 87 "PRIVATE" | |
| 88 }; | |
| 89 | |
| 90 static const char * const method_names[] = { "PRIMITIVE", "CONSTRUCTED" }; | |
| 91 | |
| 92 static const char * const type_names[] = { | |
| 93 "END_OF_CONTENTS", | |
| 94 "BOOLEAN", | |
| 95 "INTEGER", | |
| 96 "BIT_STRING", | |
| 97 "OCTET_STRING", | |
| 98 "NULL", | |
| 99 "OBJECT_ID", | |
| 100 "OBJECT_DESCRIPTOR", | |
| 101 "(type 08)", | |
| 102 "REAL", | |
| 103 "ENUMERATED", | |
| 104 "EMBEDDED", | |
| 105 "UTF8_STRING", | |
| 106 "(type 0d)", | |
| 107 "(type 0e)", | |
| 108 "(type 0f)", | |
| 109 "SEQUENCE", | |
| 110 "SET", | |
| 111 "NUMERIC_STRING", | |
| 112 "PRINTABLE_STRING", | |
| 113 "T61_STRING", | |
| 114 "VIDEOTEXT_STRING", | |
| 115 "IA5_STRING", | |
| 116 "UTC_TIME", | |
| 117 "GENERALIZED_TIME", | |
| 118 "GRAPHIC_STRING", | |
| 119 "VISIBLE_STRING", | |
| 120 "GENERAL_STRING", | |
| 121 "UNIVERSAL_STRING", | |
| 122 "(type 1d)", | |
| 123 "BMP_STRING", | |
| 124 "HIGH_TAG_VALUE" | |
| 125 }; | |
| 126 | |
| 127 static const char * const flag_names[] = { /* flags, right to left */ | |
| 128 "OPTIONAL", | |
| 129 "EXPLICIT", | |
| 130 "ANY", | |
| 131 "INLINE", | |
| 132 "POINTER", | |
| 133 "GROUP", | |
| 134 "DYNAMIC", | |
| 135 "SKIP", | |
| 136 "INNER", | |
| 137 "SAVE", | |
| 138 "", /* decoder ignores "MAY_STREAM", */ | |
| 139 "SKIP_REST", | |
| 140 "CHOICE", | |
| 141 "NO_STREAM", | |
| 142 "DEBUG_BREAK", | |
| 143 "unknown 08", | |
| 144 "unknown 10", | |
| 145 "unknown 20", | |
| 146 "unknown 40", | |
| 147 "unknown 80" | |
| 148 }; | |
| 149 | |
| 150 static int /* bool */ | |
| 151 formatKind(unsigned long kind, char * buf) | |
| 152 { | |
| 153 int i; | |
| 154 unsigned long k = kind & SEC_ASN1_TAGNUM_MASK; | |
| 155 unsigned long notag = kind & (SEC_ASN1_CHOICE | SEC_ASN1_POINTER | | |
| 156 SEC_ASN1_INLINE | SEC_ASN1_ANY | SEC_ASN1_SAVE); | |
| 157 | |
| 158 buf[0] = 0; | |
| 159 if ((kind & SEC_ASN1_CLASS_MASK) != SEC_ASN1_UNIVERSAL) { | |
| 160 sprintf(buf, " %s", class_names[(kind & SEC_ASN1_CLASS_MASK) >> 6] ); | |
| 161 buf += strlen(buf); | |
| 162 } | |
| 163 if (kind & SEC_ASN1_METHOD_MASK) { | |
| 164 sprintf(buf, " %s", method_names[1]); | |
| 165 buf += strlen(buf); | |
| 166 } | |
| 167 if ((kind & SEC_ASN1_CLASS_MASK) == SEC_ASN1_UNIVERSAL) { | |
| 168 if (k || !notag) { | |
| 169 sprintf(buf, " %s", type_names[k] ); | |
| 170 if ((k == SEC_ASN1_SET || k == SEC_ASN1_SEQUENCE) && | |
| 171 (kind & SEC_ASN1_GROUP)) { | |
| 172 buf += strlen(buf); | |
| 173 sprintf(buf, "_OF"); | |
| 174 } | |
| 175 } | |
| 176 } else { | |
| 177 sprintf(buf, " [%d]", k); | |
| 178 } | |
| 179 buf += strlen(buf); | |
| 180 | |
| 181 for (k = kind >> 8, i = 0; k; k >>= 1, ++i) { | |
| 182 if (k & 1) { | |
| 183 sprintf(buf, " %s", flag_names[i]); | |
| 184 buf += strlen(buf); | |
| 185 } | |
| 186 } | |
| 187 return notag != 0; | |
| 188 } | |
| 189 | |
| 190 #endif /* DEBUG_ASN1D_STATES */ | |
| 191 | |
| 192 typedef enum { | |
| 193 allDone, | |
| 194 decodeError, | |
| 195 keepGoing, | |
| 196 needBytes | |
| 197 } sec_asn1d_parse_status; | |
| 198 | |
| 199 struct subitem { | |
| 200 const void *data; | |
| 201 unsigned long len; /* only used for substrings */ | |
| 202 struct subitem *next; | |
| 203 }; | |
| 204 | |
| 205 typedef struct sec_asn1d_state_struct { | |
| 206 SEC_ASN1DecoderContext *top; | |
| 207 const SEC_ASN1Template *theTemplate; | |
| 208 void *dest; | |
| 209 | |
| 210 void *our_mark; /* free on completion */ | |
| 211 | |
| 212 struct sec_asn1d_state_struct *parent; /* aka prev */ | |
| 213 struct sec_asn1d_state_struct *child; /* aka next */ | |
| 214 | |
| 215 sec_asn1d_parse_place place; | |
| 216 | |
| 217 /* | |
| 218 * XXX explain the next fields as clearly as possible... | |
| 219 */ | |
| 220 unsigned char found_tag_modifiers; | |
| 221 unsigned char expect_tag_modifiers; | |
| 222 unsigned long check_tag_mask; | |
| 223 unsigned long found_tag_number; | |
| 224 unsigned long expect_tag_number; | |
| 225 unsigned long underlying_kind; | |
| 226 | |
| 227 unsigned long contents_length; | |
| 228 unsigned long pending; | |
| 229 unsigned long consumed; | |
| 230 | |
| 231 int depth; | |
| 232 | |
| 233 /* | |
| 234 * Bit strings have their length adjusted -- the first octet of the | |
| 235 * contents contains a value between 0 and 7 which says how many bits | |
| 236 * at the end of the octets are not actually part of the bit string; | |
| 237 * when parsing bit strings we put that value here because we need it | |
| 238 * later, for adjustment of the length (when the whole string is done). | |
| 239 */ | |
| 240 unsigned int bit_string_unused_bits; | |
| 241 | |
| 242 /* | |
| 243 * The following are used for indefinite-length constructed strings. | |
| 244 */ | |
| 245 struct subitem *subitems_head; | |
| 246 struct subitem *subitems_tail; | |
| 247 | |
| 248 PRPackedBool | |
| 249 allocate, /* when true, need to allocate the destination */ | |
| 250 endofcontents, /* this state ended up parsing end-of-contents octets */ | |
| 251 explicit, /* we are handling an explicit header */ | |
| 252 indefinite, /* the current item has indefinite-length encoding */ | |
| 253 missing, /* an optional field that was not present */ | |
| 254 optional, /* the template says this field may be omitted */ | |
| 255 substring; /* this is a substring of a constructed string */ | |
| 256 | |
| 257 } sec_asn1d_state; | |
| 258 | |
| 259 #define IS_HIGH_TAG_NUMBER(n) ((n) == SEC_ASN1_HIGH_TAG_NUMBER) | |
| 260 #define LAST_TAG_NUMBER_BYTE(b) (((b) & 0x80) == 0) | |
| 261 #define TAG_NUMBER_BITS 7 | |
| 262 #define TAG_NUMBER_MASK 0x7f | |
| 263 | |
| 264 #define LENGTH_IS_SHORT_FORM(b) (((b) & 0x80) == 0) | |
| 265 #define LONG_FORM_LENGTH(b) ((b) & 0x7f) | |
| 266 | |
| 267 #define HIGH_BITS(field,cnt) ((field) >> ((sizeof(field) * 8) - (cnt))) | |
| 268 | |
| 269 | |
| 270 /* | |
| 271 * An "outsider" will have an opaque pointer to this, created by calling | |
| 272 * SEC_ASN1DecoderStart(). It will be passed back in to all subsequent | |
| 273 * calls to SEC_ASN1DecoderUpdate(), and when done it is passed to | |
| 274 * SEC_ASN1DecoderFinish(). | |
| 275 */ | |
| 276 struct sec_DecoderContext_struct { | |
| 277 PLArenaPool *our_pool; /* for our internal allocs */ | |
| 278 PLArenaPool *their_pool; /* for destination structure allocs */ | |
| 279 #ifdef SEC_ASN1D_FREE_ON_ERROR /* | |
| 280 * XXX see comment below (by same | |
| 281 * ifdef) that explains why this | |
| 282 * does not work (need more smarts | |
| 283 * in order to free back to mark) | |
| 284 */ | |
| 285 /* | |
| 286 * XXX how to make their_mark work in the case where they do NOT | |
| 287 * give us a pool pointer? | |
| 288 */ | |
| 289 void *their_mark; /* free on error */ | |
| 290 #endif | |
| 291 | |
| 292 sec_asn1d_state *current; | |
| 293 sec_asn1d_parse_status status; | |
| 294 | |
| 295 SEC_ASN1NotifyProc notify_proc; /* call before/after handling field */ | |
| 296 void *notify_arg; /* argument to notify_proc */ | |
| 297 PRBool during_notify; /* true during call to notify_proc */ | |
| 298 | |
| 299 SEC_ASN1WriteProc filter_proc; /* pass field bytes to this */ | |
| 300 void *filter_arg; /* argument to that function */ | |
| 301 PRBool filter_only; /* do not allocate/store fields */ | |
| 302 }; | |
| 303 | |
| 304 | |
| 305 /* | |
| 306 * XXX this is a fairly generic function that may belong elsewhere | |
| 307 */ | |
| 308 static void * | |
| 309 sec_asn1d_alloc (PLArenaPool *poolp, unsigned long len) | |
| 310 { | |
| 311 void *thing; | |
| 312 | |
| 313 if (poolp != NULL) { | |
| 314 /* | |
| 315 * Allocate from the pool. | |
| 316 */ | |
| 317 thing = PORT_ArenaAlloc (poolp, len); | |
| 318 } else { | |
| 319 /* | |
| 320 * Allocate generically. | |
| 321 */ | |
| 322 thing = PORT_Alloc (len); | |
| 323 } | |
| 324 | |
| 325 return thing; | |
| 326 } | |
| 327 | |
| 328 | |
| 329 /* | |
| 330 * XXX this is a fairly generic function that may belong elsewhere | |
| 331 */ | |
| 332 static void * | |
| 333 sec_asn1d_zalloc (PLArenaPool *poolp, unsigned long len) | |
| 334 { | |
| 335 void *thing; | |
| 336 | |
| 337 thing = sec_asn1d_alloc (poolp, len); | |
| 338 if (thing != NULL) | |
| 339 PORT_Memset (thing, 0, len); | |
| 340 return thing; | |
| 341 } | |
| 342 | |
| 343 | |
| 344 static sec_asn1d_state * | |
| 345 sec_asn1d_push_state (SEC_ASN1DecoderContext *cx, | |
| 346 const SEC_ASN1Template *theTemplate, | |
| 347 void *dest, PRBool new_depth) | |
| 348 { | |
| 349 sec_asn1d_state *state, *new_state; | |
| 350 | |
| 351 state = cx->current; | |
| 352 | |
| 353 PORT_Assert (state == NULL || state->child == NULL); | |
| 354 | |
| 355 if (state != NULL) { | |
| 356 PORT_Assert (state->our_mark == NULL); | |
| 357 state->our_mark = PORT_ArenaMark (cx->our_pool); | |
| 358 } | |
| 359 | |
| 360 new_state = (sec_asn1d_state*)sec_asn1d_zalloc (cx->our_pool, | |
| 361 sizeof(*new_state)); | |
| 362 if (new_state == NULL) { | |
| 363 goto loser; | |
| 364 } | |
| 365 | |
| 366 new_state->top = cx; | |
| 367 new_state->parent = state; | |
| 368 new_state->theTemplate = theTemplate; | |
| 369 new_state->place = notInUse; | |
| 370 if (dest != NULL) | |
| 371 new_state->dest = (char *)dest + theTemplate->offset; | |
| 372 | |
| 373 if (state != NULL) { | |
| 374 new_state->depth = state->depth; | |
| 375 if (new_depth) { | |
| 376 if (++new_state->depth > SEC_ASN1D_MAX_DEPTH) { | |
| 377 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 378 goto loser; | |
| 379 } | |
| 380 } | |
| 381 state->child = new_state; | |
| 382 } | |
| 383 | |
| 384 cx->current = new_state; | |
| 385 return new_state; | |
| 386 | |
| 387 loser: | |
| 388 cx->status = decodeError; | |
| 389 if (state != NULL) { | |
| 390 PORT_ArenaRelease(cx->our_pool, state->our_mark); | |
| 391 state->our_mark = NULL; | |
| 392 } | |
| 393 return NULL; | |
| 394 } | |
| 395 | |
| 396 | |
| 397 static void | |
| 398 sec_asn1d_scrub_state (sec_asn1d_state *state) | |
| 399 { | |
| 400 /* | |
| 401 * Some default "scrubbing". | |
| 402 * XXX right set of initializations? | |
| 403 */ | |
| 404 state->place = beforeIdentifier; | |
| 405 state->endofcontents = PR_FALSE; | |
| 406 state->indefinite = PR_FALSE; | |
| 407 state->missing = PR_FALSE; | |
| 408 PORT_Assert (state->consumed == 0); | |
| 409 } | |
| 410 | |
| 411 | |
| 412 static void | |
| 413 sec_asn1d_notify_before (SEC_ASN1DecoderContext *cx, void *dest, int depth) | |
| 414 { | |
| 415 if (cx->notify_proc == NULL) | |
| 416 return; | |
| 417 | |
| 418 cx->during_notify = PR_TRUE; | |
| 419 (* cx->notify_proc) (cx->notify_arg, PR_TRUE, dest, depth); | |
| 420 cx->during_notify = PR_FALSE; | |
| 421 } | |
| 422 | |
| 423 | |
| 424 static void | |
| 425 sec_asn1d_notify_after (SEC_ASN1DecoderContext *cx, void *dest, int depth) | |
| 426 { | |
| 427 if (cx->notify_proc == NULL) | |
| 428 return; | |
| 429 | |
| 430 cx->during_notify = PR_TRUE; | |
| 431 (* cx->notify_proc) (cx->notify_arg, PR_FALSE, dest, depth); | |
| 432 cx->during_notify = PR_FALSE; | |
| 433 } | |
| 434 | |
| 435 | |
| 436 static sec_asn1d_state * | |
| 437 sec_asn1d_init_state_based_on_template (sec_asn1d_state *state) | |
| 438 { | |
| 439 PRBool explicit, optional, universal; | |
| 440 unsigned char expect_tag_modifiers; | |
| 441 unsigned long encode_kind, under_kind; | |
| 442 unsigned long check_tag_mask, expect_tag_number; | |
| 443 | |
| 444 | |
| 445 /* XXX Check that both of these tests are really needed/appropriate. */ | |
| 446 if (state == NULL || state->top->status == decodeError) | |
| 447 return state; | |
| 448 | |
| 449 encode_kind = state->theTemplate->kind; | |
| 450 | |
| 451 if (encode_kind & SEC_ASN1_SAVE) { | |
| 452 /* | |
| 453 * This is a "magic" field that saves away all bytes, allowing | |
| 454 * the immediately following field to still be decoded from this | |
| 455 * same spot -- sort of a fork. | |
| 456 */ | |
| 457 /* check that there are no extraneous bits */ | |
| 458 PORT_Assert (encode_kind == SEC_ASN1_SAVE); | |
| 459 if (state->top->filter_only) { | |
| 460 /* | |
| 461 * If we are not storing, then we do not do the SAVE field | |
| 462 * at all. Just move ahead to the "real" field instead, | |
| 463 * doing the appropriate notify calls before and after. | |
| 464 */ | |
| 465 sec_asn1d_notify_after (state->top, state->dest, state->depth); | |
| 466 /* | |
| 467 * Since we are not storing, allow for our current dest value | |
| 468 * to be NULL. (This might not actually occur, but right now I | |
| 469 * cannot convince myself one way or the other.) If it is NULL, | |
| 470 * assume that our parent dest can help us out. | |
| 471 */ | |
| 472 if (state->dest == NULL) | |
| 473 state->dest = state->parent->dest; | |
| 474 else | |
| 475 state->dest = (char *)state->dest - state->theTemplate->offset; | |
| 476 state->theTemplate++; | |
| 477 if (state->dest != NULL) | |
| 478 state->dest = (char *)state->dest + state->theTemplate->offset; | |
| 479 sec_asn1d_notify_before (state->top, state->dest, state->depth); | |
| 480 encode_kind = state->theTemplate->kind; | |
| 481 PORT_Assert ((encode_kind & SEC_ASN1_SAVE) == 0); | |
| 482 } else { | |
| 483 sec_asn1d_scrub_state (state); | |
| 484 state->place = duringSaveEncoding; | |
| 485 state = sec_asn1d_push_state (state->top, SEC_AnyTemplate, | |
| 486 state->dest, PR_FALSE); | |
| 487 if (state != NULL) | |
| 488 state = sec_asn1d_init_state_based_on_template (state); | |
| 489 return state; | |
| 490 } | |
| 491 } | |
| 492 | |
| 493 | |
| 494 universal = ((encode_kind & SEC_ASN1_CLASS_MASK) == SEC_ASN1_UNIVERSAL) | |
| 495 ? PR_TRUE : PR_FALSE; | |
| 496 | |
| 497 explicit = (encode_kind & SEC_ASN1_EXPLICIT) ? PR_TRUE : PR_FALSE; | |
| 498 encode_kind &= ~SEC_ASN1_EXPLICIT; | |
| 499 | |
| 500 optional = (encode_kind & SEC_ASN1_OPTIONAL) ? PR_TRUE : PR_FALSE; | |
| 501 encode_kind &= ~SEC_ASN1_OPTIONAL; | |
| 502 | |
| 503 PORT_Assert (!(explicit && universal)); /* bad templates */ | |
| 504 | |
| 505 encode_kind &= ~SEC_ASN1_DYNAMIC; | |
| 506 encode_kind &= ~SEC_ASN1_MAY_STREAM; | |
| 507 | |
| 508 if (encode_kind & SEC_ASN1_CHOICE) { | |
| 509 #if 0 /* XXX remove? */ | |
| 510 sec_asn1d_state *child = sec_asn1d_push_state(state->top, state->theTempla
te, state->dest, PR_FALSE); | |
| 511 if ((sec_asn1d_state *)NULL == child) { | |
| 512 return (sec_asn1d_state *)NULL; | |
| 513 } | |
| 514 | |
| 515 child->allocate = state->allocate; | |
| 516 child->place = beforeChoice; | |
| 517 return child; | |
| 518 #else | |
| 519 state->place = beforeChoice; | |
| 520 return state; | |
| 521 #endif | |
| 522 } | |
| 523 | |
| 524 if ((encode_kind & (SEC_ASN1_POINTER | SEC_ASN1_INLINE)) || (!universal | |
| 525 && !explicit)) { | |
| 526 const SEC_ASN1Template *subt; | |
| 527 void *dest; | |
| 528 PRBool child_allocate; | |
| 529 | |
| 530 PORT_Assert ((encode_kind & (SEC_ASN1_ANY | SEC_ASN1_SKIP)) == 0); | |
| 531 | |
| 532 sec_asn1d_scrub_state (state); | |
| 533 child_allocate = PR_FALSE; | |
| 534 | |
| 535 if (encode_kind & SEC_ASN1_POINTER) { | |
| 536 /* | |
| 537 * A POINTER means we need to allocate the destination for | |
| 538 * this field. But, since it may also be an optional field, | |
| 539 * we defer the allocation until later; we just record that | |
| 540 * it needs to be done. | |
| 541 * | |
| 542 * There are two possible scenarios here -- one is just a | |
| 543 * plain POINTER (kind of like INLINE, except with allocation) | |
| 544 * and the other is an implicitly-tagged POINTER. We don't | |
| 545 * need to do anything special here for the two cases, but | |
| 546 * since the template definition can be tricky, we do check | |
| 547 * that there are no extraneous bits set in encode_kind. | |
| 548 * | |
| 549 * XXX The same conditions which assert should set an error. | |
| 550 */ | |
| 551 if (universal) { | |
| 552 /* | |
| 553 * "universal" means this entry is a standalone POINTER; | |
| 554 * there should be no other bits set in encode_kind. | |
| 555 */ | |
| 556 PORT_Assert (encode_kind == SEC_ASN1_POINTER); | |
| 557 } else { | |
| 558 /* | |
| 559 * If we get here we have an implicitly-tagged field | |
| 560 * that needs to be put into a POINTER. The subtemplate | |
| 561 * will determine how to decode the field, but encode_kind | |
| 562 * describes the (implicit) tag we are looking for. | |
| 563 * The non-tag bits of encode_kind will be ignored by | |
| 564 * the code below; none of them should be set, however, | |
| 565 * except for the POINTER bit itself -- so check that. | |
| 566 */ | |
| 567 PORT_Assert ((encode_kind & ~SEC_ASN1_TAG_MASK) | |
| 568 == SEC_ASN1_POINTER); | |
| 569 } | |
| 570 if (!state->top->filter_only) | |
| 571 child_allocate = PR_TRUE; | |
| 572 dest = NULL; | |
| 573 state->place = afterPointer; | |
| 574 } else { | |
| 575 dest = state->dest; | |
| 576 if (encode_kind & SEC_ASN1_INLINE) { | |
| 577 /* check that there are no extraneous bits */ | |
| 578 PORT_Assert (encode_kind == SEC_ASN1_INLINE && !optional); | |
| 579 state->place = afterInline; | |
| 580 } else { | |
| 581 state->place = afterImplicit; | |
| 582 } | |
| 583 } | |
| 584 | |
| 585 state->optional = optional; | |
| 586 subt = SEC_ASN1GetSubtemplate (state->theTemplate, state->dest, PR_FALSE
); | |
| 587 state = sec_asn1d_push_state (state->top, subt, dest, PR_FALSE); | |
| 588 if (state == NULL) | |
| 589 return NULL; | |
| 590 | |
| 591 state->allocate = child_allocate; | |
| 592 | |
| 593 if (universal) { | |
| 594 state = sec_asn1d_init_state_based_on_template (state); | |
| 595 if (state != NULL) { | |
| 596 /* | |
| 597 * If this field is optional, we need to record that on | |
| 598 * the pushed child so it won't fail if the field isn't | |
| 599 * found. I can't think of a way that this new state | |
| 600 * could already have optional set (which we would wipe | |
| 601 * out below if our local optional is not set) -- but | |
| 602 * just to be sure, assert that it isn't set. | |
| 603 */ | |
| 604 PORT_Assert (!state->optional); | |
| 605 state->optional = optional; | |
| 606 } | |
| 607 return state; | |
| 608 } | |
| 609 | |
| 610 under_kind = state->theTemplate->kind; | |
| 611 under_kind &= ~SEC_ASN1_MAY_STREAM; | |
| 612 } else if (explicit) { | |
| 613 /* | |
| 614 * For explicit, we only need to match the encoding tag next, | |
| 615 * then we will push another state to handle the entire inner | |
| 616 * part. In this case, there is no underlying kind which plays | |
| 617 * any part in the determination of the outer, explicit tag. | |
| 618 * So we just set under_kind to 0, which is not a valid tag, | |
| 619 * and the rest of the tag matching stuff should be okay. | |
| 620 */ | |
| 621 under_kind = 0; | |
| 622 } else { | |
| 623 /* | |
| 624 * Nothing special; the underlying kind and the given encoding | |
| 625 * information are the same. | |
| 626 */ | |
| 627 under_kind = encode_kind; | |
| 628 } | |
| 629 | |
| 630 /* XXX is this the right set of bits to test here? */ | |
| 631 PORT_Assert ((under_kind & (SEC_ASN1_EXPLICIT | SEC_ASN1_OPTIONAL | |
| 632 | SEC_ASN1_MAY_STREAM | |
| 633 | SEC_ASN1_INLINE | SEC_ASN1_POINTER)) == 0); | |
| 634 | |
| 635 if (encode_kind & (SEC_ASN1_ANY | SEC_ASN1_SKIP)) { | |
| 636 PORT_Assert (encode_kind == under_kind); | |
| 637 if (encode_kind & SEC_ASN1_SKIP) { | |
| 638 PORT_Assert (!optional); | |
| 639 PORT_Assert (encode_kind == SEC_ASN1_SKIP); | |
| 640 state->dest = NULL; | |
| 641 } | |
| 642 check_tag_mask = 0; | |
| 643 expect_tag_modifiers = 0; | |
| 644 expect_tag_number = 0; | |
| 645 } else { | |
| 646 check_tag_mask = SEC_ASN1_TAG_MASK; | |
| 647 expect_tag_modifiers = (unsigned char)encode_kind & SEC_ASN1_TAG_MASK | |
| 648 & ~SEC_ASN1_TAGNUM_MASK; | |
| 649 /* | |
| 650 * XXX This assumes only single-octet identifiers. To handle | |
| 651 * the HIGH TAG form we would need to do some more work, especially | |
| 652 * in how to specify them in the template, because right now we | |
| 653 * do not provide a way to specify more *tag* bits in encode_kind. | |
| 654 */ | |
| 655 expect_tag_number = encode_kind & SEC_ASN1_TAGNUM_MASK; | |
| 656 | |
| 657 switch (under_kind & SEC_ASN1_TAGNUM_MASK) { | |
| 658 case SEC_ASN1_SET: | |
| 659 /* | |
| 660 * XXX A plain old SET (as opposed to a SET OF) is not implemented. | |
| 661 * If it ever is, remove this assert... | |
| 662 */ | |
| 663 PORT_Assert ((under_kind & SEC_ASN1_GROUP) != 0); | |
| 664 /* fallthru */ | |
| 665 case SEC_ASN1_SEQUENCE: | |
| 666 expect_tag_modifiers |= SEC_ASN1_CONSTRUCTED; | |
| 667 break; | |
| 668 case SEC_ASN1_BIT_STRING: | |
| 669 case SEC_ASN1_BMP_STRING: | |
| 670 case SEC_ASN1_GENERALIZED_TIME: | |
| 671 case SEC_ASN1_IA5_STRING: | |
| 672 case SEC_ASN1_OCTET_STRING: | |
| 673 case SEC_ASN1_PRINTABLE_STRING: | |
| 674 case SEC_ASN1_T61_STRING: | |
| 675 case SEC_ASN1_UNIVERSAL_STRING: | |
| 676 case SEC_ASN1_UTC_TIME: | |
| 677 case SEC_ASN1_UTF8_STRING: | |
| 678 case SEC_ASN1_VISIBLE_STRING: | |
| 679 check_tag_mask &= ~SEC_ASN1_CONSTRUCTED; | |
| 680 break; | |
| 681 } | |
| 682 } | |
| 683 | |
| 684 state->check_tag_mask = check_tag_mask; | |
| 685 state->expect_tag_modifiers = expect_tag_modifiers; | |
| 686 state->expect_tag_number = expect_tag_number; | |
| 687 state->underlying_kind = under_kind; | |
| 688 state->explicit = explicit; | |
| 689 state->optional = optional; | |
| 690 | |
| 691 sec_asn1d_scrub_state (state); | |
| 692 | |
| 693 return state; | |
| 694 } | |
| 695 | |
| 696 static sec_asn1d_state * | |
| 697 sec_asn1d_get_enclosing_construct(sec_asn1d_state *state) | |
| 698 { | |
| 699 for (state = state->parent; state; state = state->parent) { | |
| 700 sec_asn1d_parse_place place = state->place; | |
| 701 if (place != afterImplicit && | |
| 702 place != afterPointer && | |
| 703 place != afterInline && | |
| 704 place != afterSaveEncoding && | |
| 705 place != duringSaveEncoding && | |
| 706 place != duringChoice) { | |
| 707 | |
| 708 /* we've walked up the stack to a state that represents | |
| 709 ** the enclosing construct. | |
| 710 */ | |
| 711 break; | |
| 712 } | |
| 713 } | |
| 714 return state; | |
| 715 } | |
| 716 | |
| 717 static PRBool | |
| 718 sec_asn1d_parent_allows_EOC(sec_asn1d_state *state) | |
| 719 { | |
| 720 /* get state of enclosing construct. */ | |
| 721 state = sec_asn1d_get_enclosing_construct(state); | |
| 722 if (state) { | |
| 723 sec_asn1d_parse_place place = state->place; | |
| 724 /* Is it one of the types that permits an unexpected EOC? */ | |
| 725 int eoc_permitted = | |
| 726 (place == duringGroup || | |
| 727 place == duringConstructedString || | |
| 728 state->child->optional); | |
| 729 return (state->indefinite && eoc_permitted) ? PR_TRUE : PR_FALSE; | |
| 730 } | |
| 731 return PR_FALSE; | |
| 732 } | |
| 733 | |
| 734 static unsigned long | |
| 735 sec_asn1d_parse_identifier (sec_asn1d_state *state, | |
| 736 const char *buf, unsigned long len) | |
| 737 { | |
| 738 unsigned char byte; | |
| 739 unsigned char tag_number; | |
| 740 | |
| 741 PORT_Assert (state->place == beforeIdentifier); | |
| 742 | |
| 743 if (len == 0) { | |
| 744 state->top->status = needBytes; | |
| 745 return 0; | |
| 746 } | |
| 747 | |
| 748 byte = (unsigned char) *buf; | |
| 749 #ifdef DEBUG_ASN1D_STATES | |
| 750 { | |
| 751 char kindBuf[256]; | |
| 752 formatKind(byte, kindBuf); | |
| 753 printf("Found tag %02x %s\n", byte, kindBuf); | |
| 754 } | |
| 755 #endif | |
| 756 tag_number = byte & SEC_ASN1_TAGNUM_MASK; | |
| 757 | |
| 758 if (IS_HIGH_TAG_NUMBER (tag_number)) { | |
| 759 state->place = duringIdentifier; | |
| 760 state->found_tag_number = 0; | |
| 761 /* | |
| 762 * Actually, we have no idea how many bytes are pending, but we | |
| 763 * do know that it is at least 1. That is all we know; we have | |
| 764 * to look at each byte to know if there is another, etc. | |
| 765 */ | |
| 766 state->pending = 1; | |
| 767 } else { | |
| 768 if (byte == 0 && sec_asn1d_parent_allows_EOC(state)) { | |
| 769 /* | |
| 770 * Our parent has indefinite-length encoding, and the | |
| 771 * entire tag found is 0, so it seems that we have hit the | |
| 772 * end-of-contents octets. To handle this, we just change | |
| 773 * our state to that which expects to get the bytes of the | |
| 774 * end-of-contents octets and let that code re-read this byte | |
| 775 * so that our categorization of field types is correct. | |
| 776 * After that, our parent will then deal with everything else. | |
| 777 */ | |
| 778 state->place = duringEndOfContents; | |
| 779 state->pending = 2; | |
| 780 state->found_tag_number = 0; | |
| 781 state->found_tag_modifiers = 0; | |
| 782 /* | |
| 783 * We might be an optional field that is, as we now find out, | |
| 784 * missing. Give our parent a clue that this happened. | |
| 785 */ | |
| 786 if (state->optional) | |
| 787 state->missing = PR_TRUE; | |
| 788 return 0; | |
| 789 } | |
| 790 state->place = afterIdentifier; | |
| 791 state->found_tag_number = tag_number; | |
| 792 } | |
| 793 state->found_tag_modifiers = byte & ~SEC_ASN1_TAGNUM_MASK; | |
| 794 | |
| 795 return 1; | |
| 796 } | |
| 797 | |
| 798 | |
| 799 static unsigned long | |
| 800 sec_asn1d_parse_more_identifier (sec_asn1d_state *state, | |
| 801 const char *buf, unsigned long len) | |
| 802 { | |
| 803 unsigned char byte; | |
| 804 int count; | |
| 805 | |
| 806 PORT_Assert (state->pending == 1); | |
| 807 PORT_Assert (state->place == duringIdentifier); | |
| 808 | |
| 809 if (len == 0) { | |
| 810 state->top->status = needBytes; | |
| 811 return 0; | |
| 812 } | |
| 813 | |
| 814 count = 0; | |
| 815 | |
| 816 while (len && state->pending) { | |
| 817 if (HIGH_BITS (state->found_tag_number, TAG_NUMBER_BITS) != 0) { | |
| 818 /* | |
| 819 * The given high tag number overflows our container; | |
| 820 * just give up. This is not likely to *ever* happen. | |
| 821 */ | |
| 822 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 823 state->top->status = decodeError; | |
| 824 return 0; | |
| 825 } | |
| 826 | |
| 827 state->found_tag_number <<= TAG_NUMBER_BITS; | |
| 828 | |
| 829 byte = (unsigned char) buf[count++]; | |
| 830 state->found_tag_number |= (byte & TAG_NUMBER_MASK); | |
| 831 | |
| 832 len--; | |
| 833 if (LAST_TAG_NUMBER_BYTE (byte)) | |
| 834 state->pending = 0; | |
| 835 } | |
| 836 | |
| 837 if (state->pending == 0) | |
| 838 state->place = afterIdentifier; | |
| 839 | |
| 840 return count; | |
| 841 } | |
| 842 | |
| 843 | |
| 844 static void | |
| 845 sec_asn1d_confirm_identifier (sec_asn1d_state *state) | |
| 846 { | |
| 847 PRBool match; | |
| 848 | |
| 849 PORT_Assert (state->place == afterIdentifier); | |
| 850 | |
| 851 match = (PRBool)(((state->found_tag_modifiers & state->check_tag_mask) | |
| 852 == state->expect_tag_modifiers) | |
| 853 && ((state->found_tag_number & state->check_tag_mask) | |
| 854 == state->expect_tag_number)); | |
| 855 if (match) { | |
| 856 state->place = beforeLength; | |
| 857 } else { | |
| 858 if (state->optional) { | |
| 859 state->missing = PR_TRUE; | |
| 860 state->place = afterEndOfContents; | |
| 861 } else { | |
| 862 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 863 state->top->status = decodeError; | |
| 864 } | |
| 865 } | |
| 866 } | |
| 867 | |
| 868 | |
| 869 static unsigned long | |
| 870 sec_asn1d_parse_length (sec_asn1d_state *state, | |
| 871 const char *buf, unsigned long len) | |
| 872 { | |
| 873 unsigned char byte; | |
| 874 | |
| 875 PORT_Assert (state->place == beforeLength); | |
| 876 | |
| 877 if (len == 0) { | |
| 878 state->top->status = needBytes; | |
| 879 return 0; | |
| 880 } | |
| 881 | |
| 882 /* | |
| 883 * The default/likely outcome. It may get adjusted below. | |
| 884 */ | |
| 885 state->place = afterLength; | |
| 886 | |
| 887 byte = (unsigned char) *buf; | |
| 888 | |
| 889 if (LENGTH_IS_SHORT_FORM (byte)) { | |
| 890 state->contents_length = byte; | |
| 891 } else { | |
| 892 state->contents_length = 0; | |
| 893 state->pending = LONG_FORM_LENGTH (byte); | |
| 894 if (state->pending == 0) { | |
| 895 state->indefinite = PR_TRUE; | |
| 896 } else { | |
| 897 state->place = duringLength; | |
| 898 } | |
| 899 } | |
| 900 | |
| 901 /* If we're parsing an ANY, SKIP, or SAVE template, and | |
| 902 ** the object being saved is definite length encoded and constructed, | |
| 903 ** there's no point in decoding that construct's members. | |
| 904 ** So, just forget it's constructed and treat it as primitive. | |
| 905 ** (SAVE appears as an ANY at this point) | |
| 906 */ | |
| 907 if (!state->indefinite && | |
| 908 (state->underlying_kind & (SEC_ASN1_ANY | SEC_ASN1_SKIP))) { | |
| 909 state->found_tag_modifiers &= ~SEC_ASN1_CONSTRUCTED; | |
| 910 } | |
| 911 | |
| 912 return 1; | |
| 913 } | |
| 914 | |
| 915 | |
| 916 static unsigned long | |
| 917 sec_asn1d_parse_more_length (sec_asn1d_state *state, | |
| 918 const char *buf, unsigned long len) | |
| 919 { | |
| 920 int count; | |
| 921 | |
| 922 PORT_Assert (state->pending > 0); | |
| 923 PORT_Assert (state->place == duringLength); | |
| 924 | |
| 925 if (len == 0) { | |
| 926 state->top->status = needBytes; | |
| 927 return 0; | |
| 928 } | |
| 929 | |
| 930 count = 0; | |
| 931 | |
| 932 while (len && state->pending) { | |
| 933 if (HIGH_BITS (state->contents_length, 9) != 0) { | |
| 934 /* | |
| 935 * The given full content length overflows our container; | |
| 936 * just give up. | |
| 937 */ | |
| 938 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 939 state->top->status = decodeError; | |
| 940 return 0; | |
| 941 } | |
| 942 | |
| 943 state->contents_length <<= 8; | |
| 944 state->contents_length |= (unsigned char) buf[count++]; | |
| 945 | |
| 946 len--; | |
| 947 state->pending--; | |
| 948 } | |
| 949 | |
| 950 if (state->pending == 0) | |
| 951 state->place = afterLength; | |
| 952 | |
| 953 return count; | |
| 954 } | |
| 955 | |
| 956 /* | |
| 957 * Helper function for sec_asn1d_prepare_for_contents. | |
| 958 * Checks that a value representing a number of bytes consumed can be | |
| 959 * subtracted from a remaining length. If so, returns PR_TRUE. | |
| 960 * Otherwise, sets the error SEC_ERROR_BAD_DER, indicates that there was a | |
| 961 * decoding error in the given SEC_ASN1DecoderContext, and returns PR_FALSE. | |
| 962 */ | |
| 963 static PRBool | |
| 964 sec_asn1d_check_and_subtract_length (unsigned long *remaining, | |
| 965 unsigned long consumed, | |
| 966 SEC_ASN1DecoderContext *cx) | |
| 967 { | |
| 968 PORT_Assert(remaining); | |
| 969 PORT_Assert(cx); | |
| 970 if (!remaining || !cx) { | |
| 971 PORT_SetError (SEC_ERROR_INVALID_ARGS); | |
| 972 cx->status = decodeError; | |
| 973 return PR_FALSE; | |
| 974 } | |
| 975 if (*remaining < consumed) { | |
| 976 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 977 cx->status = decodeError; | |
| 978 return PR_FALSE; | |
| 979 } | |
| 980 *remaining -= consumed; | |
| 981 return PR_TRUE; | |
| 982 } | |
| 983 | |
| 984 static void | |
| 985 sec_asn1d_prepare_for_contents (sec_asn1d_state *state) | |
| 986 { | |
| 987 SECItem *item; | |
| 988 PLArenaPool *poolp; | |
| 989 unsigned long alloc_len; | |
| 990 sec_asn1d_state *parent; | |
| 991 | |
| 992 #ifdef DEBUG_ASN1D_STATES | |
| 993 { | |
| 994 printf("Found Length %d %s\n", state->contents_length, | |
| 995 state->indefinite ? "indefinite" : ""); | |
| 996 } | |
| 997 #endif | |
| 998 | |
| 999 /** | |
| 1000 * The maximum length for a child element should be constrained to the | |
| 1001 * length remaining in the first definite length element in the ancestor | |
| 1002 * stack. If there is no definite length element in the ancestor stack, | |
| 1003 * there's nothing to constrain the length of the child, so there's no | |
| 1004 * further processing necessary. | |
| 1005 * | |
| 1006 * It's necessary to walk the ancestor stack, because it's possible to have | |
| 1007 * definite length children that are part of an indefinite length element, | |
| 1008 * which is itself part of an indefinite length element, and which is | |
| 1009 * ultimately part of a definite length element. A simple example of this | |
| 1010 * would be the handling of constructed OCTET STRINGs in BER encoding. | |
| 1011 * | |
| 1012 * This algorithm finds the first definite length element in the ancestor | |
| 1013 * stack, if any, and if so, ensures that the length of the child element | |
| 1014 * is consistent with the number of bytes remaining in the constraining | |
| 1015 * ancestor element (that is, after accounting for any other sibling | |
| 1016 * elements that may have been read). | |
| 1017 * | |
| 1018 * It's slightly complicated by the need to account both for integer | |
| 1019 * underflow and overflow, as well as ensure that for indefinite length | |
| 1020 * encodings, there's also enough space for the End-of-Contents (EOC) | |
| 1021 * octets (Tag = 0x00, Length = 0x00, or two bytes). | |
| 1022 */ | |
| 1023 | |
| 1024 /* Determine the maximum length available for this element by finding the | |
| 1025 * first definite length ancestor, if any. */ | |
| 1026 parent = sec_asn1d_get_enclosing_construct(state); | |
| 1027 while (parent && parent->indefinite) { | |
| 1028 parent = sec_asn1d_get_enclosing_construct(parent); | |
| 1029 } | |
| 1030 /* If parent is null, state is either the outermost state / at the top of | |
| 1031 * the stack, or the outermost state uses indefinite length encoding. In | |
| 1032 * these cases, there's nothing external to constrain this element, so | |
| 1033 * there's nothing to check. */ | |
| 1034 if (parent) { | |
| 1035 unsigned long remaining = parent->pending; | |
| 1036 parent = state; | |
| 1037 do { | |
| 1038 if (!sec_asn1d_check_and_subtract_length( | |
| 1039 &remaining, parent->consumed, state->top) || | |
| 1040 /* If parent->indefinite is true, parent->contents_length is | |
| 1041 * zero and this is a no-op. */ | |
| 1042 !sec_asn1d_check_and_subtract_length( | |
| 1043 &remaining, parent->contents_length, state->top) || | |
| 1044 /* If parent->indefinite is true, then ensure there is enough | |
| 1045 * space for an EOC tag of 2 bytes. */ | |
| 1046 (parent->indefinite && !sec_asn1d_check_and_subtract_length( | |
| 1047 &remaining, 2, state->top))) { | |
| 1048 /* This element is larger than its enclosing element, which is | |
| 1049 * invalid. */ | |
| 1050 return; | |
| 1051 } | |
| 1052 } while ((parent = sec_asn1d_get_enclosing_construct(parent)) && | |
| 1053 parent->indefinite); | |
| 1054 } | |
| 1055 | |
| 1056 /* | |
| 1057 * XXX I cannot decide if this allocation should exclude the case | |
| 1058 * where state->endofcontents is true -- figure it out! | |
| 1059 */ | |
| 1060 if (state->allocate) { | |
| 1061 void *dest; | |
| 1062 | |
| 1063 PORT_Assert (state->dest == NULL); | |
| 1064 /* | |
| 1065 * We are handling a POINTER or a member of a GROUP, and need to | |
| 1066 * allocate for the data structure. | |
| 1067 */ | |
| 1068 dest = sec_asn1d_zalloc (state->top->their_pool, | |
| 1069 state->theTemplate->size); | |
| 1070 if (dest == NULL) { | |
| 1071 state->top->status = decodeError; | |
| 1072 return; | |
| 1073 } | |
| 1074 state->dest = (char *)dest + state->theTemplate->offset; | |
| 1075 | |
| 1076 /* | |
| 1077 * For a member of a GROUP, our parent will later put the | |
| 1078 * pointer wherever it belongs. But for a POINTER, we need | |
| 1079 * to record the destination now, in case notify or filter | |
| 1080 * procs need access to it -- they cannot find it otherwise, | |
| 1081 * until it is too late (for one-pass processing). | |
| 1082 */ | |
| 1083 if (state->parent->place == afterPointer) { | |
| 1084 void **placep; | |
| 1085 | |
| 1086 placep = state->parent->dest; | |
| 1087 *placep = dest; | |
| 1088 } | |
| 1089 } | |
| 1090 | |
| 1091 /* | |
| 1092 * Remember, length may be indefinite here! In that case, | |
| 1093 * both contents_length and pending will be zero. | |
| 1094 */ | |
| 1095 state->pending = state->contents_length; | |
| 1096 | |
| 1097 /* | |
| 1098 * An EXPLICIT is nothing but an outer header, which we have | |
| 1099 * already parsed and accepted. Now we need to do the inner | |
| 1100 * header and its contents. | |
| 1101 */ | |
| 1102 if (state->explicit) { | |
| 1103 state->place = afterExplicit; | |
| 1104 state = sec_asn1d_push_state (state->top, | |
| 1105 SEC_ASN1GetSubtemplate(state->theTemplate, | |
| 1106 state->dest, | |
| 1107 PR_FALSE), | |
| 1108 state->dest, PR_TRUE); | |
| 1109 if (state != NULL) | |
| 1110 state = sec_asn1d_init_state_based_on_template (state); | |
| 1111 return; | |
| 1112 } | |
| 1113 | |
| 1114 /* | |
| 1115 * For GROUP (SET OF, SEQUENCE OF), even if we know the length here | |
| 1116 * we cannot tell how many items we will end up with ... so push a | |
| 1117 * state that can keep track of "children" (the individual members | |
| 1118 * of the group; we will allocate as we go and put them all together | |
| 1119 * at the end. | |
| 1120 */ | |
| 1121 if (state->underlying_kind & SEC_ASN1_GROUP) { | |
| 1122 /* XXX If this assertion holds (should be able to confirm it via | |
| 1123 * inspection, too) then move this code into the switch statement | |
| 1124 * below under cases SET_OF and SEQUENCE_OF; it will be cleaner. | |
| 1125 */ | |
| 1126 PORT_Assert (state->underlying_kind == SEC_ASN1_SET_OF | |
| 1127 || state->underlying_kind == SEC_ASN1_SEQUENCE_OF | |
| 1128 || state->underlying_kind == (SEC_ASN1_SEQUENCE_OF|SEC_ASN1_DYNAMIC) | |
| 1129 || state->underlying_kind == (SEC_ASN1_SEQUENCE_OF|SEC_ASN1_DYNAMIC) | |
| 1130 ); | |
| 1131 if (state->contents_length != 0 || state->indefinite) { | |
| 1132 const SEC_ASN1Template *subt; | |
| 1133 | |
| 1134 state->place = duringGroup; | |
| 1135 subt = SEC_ASN1GetSubtemplate (state->theTemplate, state->dest, | |
| 1136 PR_FALSE); | |
| 1137 state = sec_asn1d_push_state (state->top, subt, NULL, PR_TRUE); | |
| 1138 if (state != NULL) { | |
| 1139 if (!state->top->filter_only) | |
| 1140 state->allocate = PR_TRUE; /* XXX propogate this? */ | |
| 1141 /* | |
| 1142 * Do the "before" field notification for next in group. | |
| 1143 */ | |
| 1144 sec_asn1d_notify_before (state->top, state->dest, state->depth); | |
| 1145 state = sec_asn1d_init_state_based_on_template (state); | |
| 1146 } | |
| 1147 } else { | |
| 1148 /* | |
| 1149 * A group of zero; we are done. | |
| 1150 * Set state to afterGroup and let that code plant the NULL. | |
| 1151 */ | |
| 1152 state->place = afterGroup; | |
| 1153 } | |
| 1154 return; | |
| 1155 } | |
| 1156 | |
| 1157 switch (state->underlying_kind) { | |
| 1158 case SEC_ASN1_SEQUENCE: | |
| 1159 /* | |
| 1160 * We need to push a child to handle the individual fields. | |
| 1161 */ | |
| 1162 state->place = duringSequence; | |
| 1163 state = sec_asn1d_push_state (state->top, state->theTemplate + 1, | |
| 1164 state->dest, PR_TRUE); | |
| 1165 if (state != NULL) { | |
| 1166 /* | |
| 1167 * Do the "before" field notification. | |
| 1168 */ | |
| 1169 sec_asn1d_notify_before (state->top, state->dest, state->depth); | |
| 1170 state = sec_asn1d_init_state_based_on_template (state); | |
| 1171 } | |
| 1172 break; | |
| 1173 | |
| 1174 case SEC_ASN1_SET: /* XXX SET is not really implemented */ | |
| 1175 /* | |
| 1176 * XXX A plain SET requires special handling; scanning of a | |
| 1177 * template to see where a field should go (because by definition, | |
| 1178 * they are not in any particular order, and you have to look at | |
| 1179 * each tag to disambiguate what the field is). We may never | |
| 1180 * implement this because in practice, it seems to be unused. | |
| 1181 */ | |
| 1182 PORT_Assert(0); | |
| 1183 PORT_SetError (SEC_ERROR_BAD_DER); /* XXX */ | |
| 1184 state->top->status = decodeError; | |
| 1185 break; | |
| 1186 | |
| 1187 case SEC_ASN1_NULL: | |
| 1188 /* | |
| 1189 * The NULL type, by definition, is "nothing", content length of zero. | |
| 1190 * An indefinite-length encoding is not alloweed. | |
| 1191 */ | |
| 1192 if (state->contents_length || state->indefinite) { | |
| 1193 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 1194 state->top->status = decodeError; | |
| 1195 break; | |
| 1196 } | |
| 1197 if (state->dest != NULL) { | |
| 1198 item = (SECItem *)(state->dest); | |
| 1199 item->data = NULL; | |
| 1200 item->len = 0; | |
| 1201 } | |
| 1202 state->place = afterEndOfContents; | |
| 1203 break; | |
| 1204 | |
| 1205 case SEC_ASN1_BMP_STRING: | |
| 1206 /* Error if length is not divisable by 2 */ | |
| 1207 if (state->contents_length % 2) { | |
| 1208 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 1209 state->top->status = decodeError; | |
| 1210 break; | |
| 1211 } | |
| 1212 /* otherwise, handle as other string types */ | |
| 1213 goto regular_string_type; | |
| 1214 | |
| 1215 case SEC_ASN1_UNIVERSAL_STRING: | |
| 1216 /* Error if length is not divisable by 4 */ | |
| 1217 if (state->contents_length % 4) { | |
| 1218 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 1219 state->top->status = decodeError; | |
| 1220 break; | |
| 1221 } | |
| 1222 /* otherwise, handle as other string types */ | |
| 1223 goto regular_string_type; | |
| 1224 | |
| 1225 case SEC_ASN1_SKIP: | |
| 1226 case SEC_ASN1_ANY: | |
| 1227 case SEC_ASN1_ANY_CONTENTS: | |
| 1228 /* | |
| 1229 * These are not (necessarily) strings, but they need nearly | |
| 1230 * identical handling (especially when we need to deal with | |
| 1231 * constructed sub-pieces), so we pretend they are. | |
| 1232 */ | |
| 1233 /* fallthru */ | |
| 1234 regular_string_type: | |
| 1235 case SEC_ASN1_BIT_STRING: | |
| 1236 case SEC_ASN1_IA5_STRING: | |
| 1237 case SEC_ASN1_OCTET_STRING: | |
| 1238 case SEC_ASN1_PRINTABLE_STRING: | |
| 1239 case SEC_ASN1_T61_STRING: | |
| 1240 case SEC_ASN1_UTC_TIME: | |
| 1241 case SEC_ASN1_UTF8_STRING: | |
| 1242 case SEC_ASN1_VISIBLE_STRING: | |
| 1243 /* | |
| 1244 * We are allocating for a primitive or a constructed string. | |
| 1245 * If it is a constructed string, it may also be indefinite-length. | |
| 1246 * If it is primitive, the length can (legally) be zero. | |
| 1247 * Our first order of business is to allocate the memory for | |
| 1248 * the string, if we can (if we know the length). | |
| 1249 */ | |
| 1250 item = (SECItem *)(state->dest); | |
| 1251 | |
| 1252 /* | |
| 1253 * If the item is a definite-length constructed string, then | |
| 1254 * the contents_length is actually larger than what we need | |
| 1255 * (because it also counts each intermediate header which we | |
| 1256 * will be throwing away as we go), but it is a perfectly good | |
| 1257 * upper bound that we just allocate anyway, and then concat | |
| 1258 * as we go; we end up wasting a few extra bytes but save a | |
| 1259 * whole other copy. | |
| 1260 */ | |
| 1261 alloc_len = state->contents_length; | |
| 1262 poolp = NULL; /* quiet compiler warnings about unused... */ | |
| 1263 | |
| 1264 if (item == NULL || state->top->filter_only) { | |
| 1265 if (item != NULL) { | |
| 1266 item->data = NULL; | |
| 1267 item->len = 0; | |
| 1268 } | |
| 1269 alloc_len = 0; | |
| 1270 } else if (state->substring) { | |
| 1271 /* | |
| 1272 * If we are a substring of a constructed string, then we may | |
| 1273 * not have to allocate anything (because our parent, the | |
| 1274 * actual constructed string, did it for us). If we are a | |
| 1275 * substring and we *do* have to allocate, that means our | |
| 1276 * parent is an indefinite-length, so we allocate from our pool; | |
| 1277 * later our parent will copy our string into the aggregated | |
| 1278 * whole and free our pool allocation. | |
| 1279 */ | |
| 1280 if (item->data == NULL) { | |
| 1281 PORT_Assert (item->len == 0); | |
| 1282 poolp = state->top->our_pool; | |
| 1283 } else { | |
| 1284 alloc_len = 0; | |
| 1285 } | |
| 1286 } else { | |
| 1287 item->len = 0; | |
| 1288 item->data = NULL; | |
| 1289 poolp = state->top->their_pool; | |
| 1290 } | |
| 1291 | |
| 1292 if (alloc_len || ((! state->indefinite) | |
| 1293 && (state->subitems_head != NULL))) { | |
| 1294 struct subitem *subitem; | |
| 1295 int len; | |
| 1296 | |
| 1297 PORT_Assert (item); | |
| 1298 if (!item) { | |
| 1299 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 1300 state->top->status = decodeError; | |
| 1301 return; | |
| 1302 } | |
| 1303 PORT_Assert (item->len == 0 && item->data == NULL); | |
| 1304 /* | |
| 1305 * Check for and handle an ANY which has stashed aside the | |
| 1306 * header (identifier and length) bytes for us to include | |
| 1307 * in the saved contents. | |
| 1308 */ | |
| 1309 if (state->subitems_head != NULL) { | |
| 1310 PORT_Assert (state->underlying_kind == SEC_ASN1_ANY); | |
| 1311 for (subitem = state->subitems_head; | |
| 1312 subitem != NULL; subitem = subitem->next) | |
| 1313 alloc_len += subitem->len; | |
| 1314 } | |
| 1315 | |
| 1316 item->data = (unsigned char*)sec_asn1d_zalloc (poolp, alloc_len); | |
| 1317 if (item->data == NULL) { | |
| 1318 state->top->status = decodeError; | |
| 1319 break; | |
| 1320 } | |
| 1321 | |
| 1322 len = 0; | |
| 1323 for (subitem = state->subitems_head; | |
| 1324 subitem != NULL; subitem = subitem->next) { | |
| 1325 PORT_Memcpy (item->data + len, subitem->data, subitem->len); | |
| 1326 len += subitem->len; | |
| 1327 } | |
| 1328 item->len = len; | |
| 1329 | |
| 1330 /* | |
| 1331 * Because we use arenas and have a mark set, we later free | |
| 1332 * everything we have allocated, so this does *not* present | |
| 1333 * a memory leak (it is just temporarily left dangling). | |
| 1334 */ | |
| 1335 state->subitems_head = state->subitems_tail = NULL; | |
| 1336 } | |
| 1337 | |
| 1338 if (state->contents_length == 0 && (! state->indefinite)) { | |
| 1339 /* | |
| 1340 * A zero-length simple or constructed string; we are done. | |
| 1341 */ | |
| 1342 state->place = afterEndOfContents; | |
| 1343 } else if (state->found_tag_modifiers & SEC_ASN1_CONSTRUCTED) { | |
| 1344 const SEC_ASN1Template *sub; | |
| 1345 | |
| 1346 switch (state->underlying_kind) { | |
| 1347 case SEC_ASN1_ANY: | |
| 1348 case SEC_ASN1_ANY_CONTENTS: | |
| 1349 sub = SEC_AnyTemplate; | |
| 1350 break; | |
| 1351 case SEC_ASN1_BIT_STRING: | |
| 1352 sub = SEC_BitStringTemplate; | |
| 1353 break; | |
| 1354 case SEC_ASN1_BMP_STRING: | |
| 1355 sub = SEC_BMPStringTemplate; | |
| 1356 break; | |
| 1357 case SEC_ASN1_GENERALIZED_TIME: | |
| 1358 sub = SEC_GeneralizedTimeTemplate; | |
| 1359 break; | |
| 1360 case SEC_ASN1_IA5_STRING: | |
| 1361 sub = SEC_IA5StringTemplate; | |
| 1362 break; | |
| 1363 case SEC_ASN1_OCTET_STRING: | |
| 1364 sub = SEC_OctetStringTemplate; | |
| 1365 break; | |
| 1366 case SEC_ASN1_PRINTABLE_STRING: | |
| 1367 sub = SEC_PrintableStringTemplate; | |
| 1368 break; | |
| 1369 case SEC_ASN1_T61_STRING: | |
| 1370 sub = SEC_T61StringTemplate; | |
| 1371 break; | |
| 1372 case SEC_ASN1_UNIVERSAL_STRING: | |
| 1373 sub = SEC_UniversalStringTemplate; | |
| 1374 break; | |
| 1375 case SEC_ASN1_UTC_TIME: | |
| 1376 sub = SEC_UTCTimeTemplate; | |
| 1377 break; | |
| 1378 case SEC_ASN1_UTF8_STRING: | |
| 1379 sub = SEC_UTF8StringTemplate; | |
| 1380 break; | |
| 1381 case SEC_ASN1_VISIBLE_STRING: | |
| 1382 sub = SEC_VisibleStringTemplate; | |
| 1383 break; | |
| 1384 case SEC_ASN1_SKIP: | |
| 1385 sub = SEC_SkipTemplate; | |
| 1386 break; | |
| 1387 default: /* redundant given outer switch cases, but */ | |
| 1388 PORT_Assert(0); /* the compiler does not seem to know that, */ | |
| 1389 sub = NULL; /* so just do enough to quiet it. */ | |
| 1390 break; | |
| 1391 } | |
| 1392 | |
| 1393 state->place = duringConstructedString; | |
| 1394 state = sec_asn1d_push_state (state->top, sub, item, PR_TRUE); | |
| 1395 if (state != NULL) { | |
| 1396 state->substring = PR_TRUE; /* XXX propogate? */ | |
| 1397 state = sec_asn1d_init_state_based_on_template (state); | |
| 1398 } | |
| 1399 } else if (state->indefinite) { | |
| 1400 /* | |
| 1401 * An indefinite-length string *must* be constructed! | |
| 1402 */ | |
| 1403 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 1404 state->top->status = decodeError; | |
| 1405 } else { | |
| 1406 /* | |
| 1407 * A non-zero-length simple string. | |
| 1408 */ | |
| 1409 if (state->underlying_kind == SEC_ASN1_BIT_STRING) | |
| 1410 state->place = beforeBitString; | |
| 1411 else | |
| 1412 state->place = duringLeaf; | |
| 1413 } | |
| 1414 break; | |
| 1415 | |
| 1416 default: | |
| 1417 /* | |
| 1418 * We are allocating for a simple leaf item. | |
| 1419 */ | |
| 1420 if (state->contents_length) { | |
| 1421 if (state->dest != NULL) { | |
| 1422 item = (SECItem *)(state->dest); | |
| 1423 item->len = 0; | |
| 1424 if (state->top->filter_only) { | |
| 1425 item->data = NULL; | |
| 1426 } else { | |
| 1427 item->data = (unsigned char*) | |
| 1428 sec_asn1d_zalloc (state->top->their_pool, | |
| 1429 state->contents_length); | |
| 1430 if (item->data == NULL) { | |
| 1431 state->top->status = decodeError; | |
| 1432 return; | |
| 1433 } | |
| 1434 } | |
| 1435 } | |
| 1436 state->place = duringLeaf; | |
| 1437 } else { | |
| 1438 /* | |
| 1439 * An indefinite-length or zero-length item is not allowed. | |
| 1440 * (All legal cases of such were handled above.) | |
| 1441 */ | |
| 1442 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 1443 state->top->status = decodeError; | |
| 1444 } | |
| 1445 } | |
| 1446 } | |
| 1447 | |
| 1448 | |
| 1449 static void | |
| 1450 sec_asn1d_free_child (sec_asn1d_state *state, PRBool error) | |
| 1451 { | |
| 1452 if (state->child != NULL) { | |
| 1453 PORT_Assert (error || state->child->consumed == 0); | |
| 1454 PORT_Assert (state->our_mark != NULL); | |
| 1455 PORT_ArenaZRelease (state->top->our_pool, state->our_mark); | |
| 1456 if (error && state->top->their_pool == NULL) { | |
| 1457 /* | |
| 1458 * XXX We need to free anything allocated. | |
| 1459 * At this point, we failed in the middle of decoding. But we | |
| 1460 * can't free the data we previously allocated with PR_Malloc | |
| 1461 * unless we keep track of every pointer. So instead we have a | |
| 1462 * memory leak when decoding fails half-way, unless an arena is | |
| 1463 * used. See bug 95311 . | |
| 1464 */ | |
| 1465 } | |
| 1466 state->child = NULL; | |
| 1467 state->our_mark = NULL; | |
| 1468 } else { | |
| 1469 /* | |
| 1470 * It is important that we do not leave a mark unreleased/unmarked. | |
| 1471 * But I do not think we should ever have one set in this case, only | |
| 1472 * if we had a child (handled above). So check for that. If this | |
| 1473 * assertion should ever get hit, then we probably need to add code | |
| 1474 * here to release back to our_mark (and then set our_mark to NULL). | |
| 1475 */ | |
| 1476 PORT_Assert (state->our_mark == NULL); | |
| 1477 } | |
| 1478 state->place = beforeEndOfContents; | |
| 1479 } | |
| 1480 | |
| 1481 /* We have just saved an entire encoded ASN.1 object (type) for a SAVE | |
| 1482 ** template, and now in the next template, we are going to decode that | |
| 1483 ** saved data by calling SEC_ASN1DecoderUpdate recursively. | |
| 1484 ** If that recursive call fails with needBytes, it is a fatal error, | |
| 1485 ** because the encoded object should have been complete. | |
| 1486 ** If that recursive call fails with decodeError, it will have already | |
| 1487 ** cleaned up the state stack, so we must bail out quickly. | |
| 1488 ** | |
| 1489 ** These checks of the status returned by the recursive call are now | |
| 1490 ** done in the caller of this function, immediately after it returns. | |
| 1491 */ | |
| 1492 static void | |
| 1493 sec_asn1d_reuse_encoding (sec_asn1d_state *state) | |
| 1494 { | |
| 1495 sec_asn1d_state *child; | |
| 1496 unsigned long consumed; | |
| 1497 SECItem *item; | |
| 1498 void *dest; | |
| 1499 | |
| 1500 | |
| 1501 child = state->child; | |
| 1502 PORT_Assert (child != NULL); | |
| 1503 | |
| 1504 consumed = child->consumed; | |
| 1505 child->consumed = 0; | |
| 1506 | |
| 1507 item = (SECItem *)(state->dest); | |
| 1508 PORT_Assert (item != NULL); | |
| 1509 | |
| 1510 PORT_Assert (item->len == consumed); | |
| 1511 | |
| 1512 /* | |
| 1513 * Free any grandchild. | |
| 1514 */ | |
| 1515 sec_asn1d_free_child (child, PR_FALSE); | |
| 1516 | |
| 1517 /* | |
| 1518 * Notify after the SAVE field. | |
| 1519 */ | |
| 1520 sec_asn1d_notify_after (state->top, state->dest, state->depth); | |
| 1521 | |
| 1522 /* | |
| 1523 * Adjust to get new dest and move forward. | |
| 1524 */ | |
| 1525 dest = (char *)state->dest - state->theTemplate->offset; | |
| 1526 state->theTemplate++; | |
| 1527 child->dest = (char *)dest + state->theTemplate->offset; | |
| 1528 child->theTemplate = state->theTemplate; | |
| 1529 | |
| 1530 /* | |
| 1531 * Notify before the "real" field. | |
| 1532 */ | |
| 1533 PORT_Assert (state->depth == child->depth); | |
| 1534 sec_asn1d_notify_before (state->top, child->dest, child->depth); | |
| 1535 | |
| 1536 /* | |
| 1537 * This will tell DecoderUpdate to return when it is done. | |
| 1538 */ | |
| 1539 state->place = afterSaveEncoding; | |
| 1540 | |
| 1541 /* | |
| 1542 * We already have a child; "push" it by making it current. | |
| 1543 */ | |
| 1544 state->top->current = child; | |
| 1545 | |
| 1546 /* | |
| 1547 * And initialize it so it is ready to parse. | |
| 1548 */ | |
| 1549 (void) sec_asn1d_init_state_based_on_template(child); | |
| 1550 | |
| 1551 /* | |
| 1552 * Now parse that out of our data. | |
| 1553 */ | |
| 1554 if (SEC_ASN1DecoderUpdate (state->top, | |
| 1555 (char *) item->data, item->len) != SECSuccess) | |
| 1556 return; | |
| 1557 if (state->top->status == needBytes) { | |
| 1558 return; | |
| 1559 } | |
| 1560 | |
| 1561 PORT_Assert (state->top->current == state); | |
| 1562 PORT_Assert (state->child == child); | |
| 1563 | |
| 1564 /* | |
| 1565 * That should have consumed what we consumed before. | |
| 1566 */ | |
| 1567 PORT_Assert (consumed == child->consumed); | |
| 1568 child->consumed = 0; | |
| 1569 | |
| 1570 /* | |
| 1571 * Done. | |
| 1572 */ | |
| 1573 state->consumed += consumed; | |
| 1574 child->place = notInUse; | |
| 1575 state->place = afterEndOfContents; | |
| 1576 } | |
| 1577 | |
| 1578 | |
| 1579 static unsigned long | |
| 1580 sec_asn1d_parse_leaf (sec_asn1d_state *state, | |
| 1581 const char *buf, unsigned long len) | |
| 1582 { | |
| 1583 SECItem *item; | |
| 1584 unsigned long bufLen; | |
| 1585 | |
| 1586 if (len == 0) { | |
| 1587 state->top->status = needBytes; | |
| 1588 return 0; | |
| 1589 } | |
| 1590 | |
| 1591 if (state->pending < len) | |
| 1592 len = state->pending; | |
| 1593 | |
| 1594 bufLen = len; | |
| 1595 | |
| 1596 item = (SECItem *)(state->dest); | |
| 1597 if (item != NULL && item->data != NULL) { | |
| 1598 unsigned long offset; | |
| 1599 /* Strip leading zeroes when target is unsigned integer */ | |
| 1600 if (state->underlying_kind == SEC_ASN1_INTEGER && /* INTEGER */ | |
| 1601 item->len == 0 && /* MSB */ | |
| 1602 item->type == siUnsignedInteger) /* unsigned */ | |
| 1603 { | |
| 1604 while (len > 1 && buf[0] == 0) { /* leading 0 */ | |
| 1605 buf++; | |
| 1606 len--; | |
| 1607 } | |
| 1608 } | |
| 1609 offset = item->len; | |
| 1610 if (state->underlying_kind == SEC_ASN1_BIT_STRING) { | |
| 1611 // The previous bit string must have no unused bits. | |
| 1612 if (item->len & 0x7) { | |
| 1613 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 1614 state->top->status = decodeError; | |
| 1615 return 0; | |
| 1616 } | |
| 1617 // If this is a bit string, the length is bits, not bytes. | |
| 1618 offset = item->len >> 3; | |
| 1619 } | |
| 1620 if (state->underlying_kind == SEC_ASN1_BIT_STRING) { | |
| 1621 unsigned long len_in_bits; | |
| 1622 // Protect against overflow during the bytes-to-bits conversion. | |
| 1623 if (len >= (ULONG_MAX >> 3) + 1) { | |
| 1624 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 1625 state->top->status = decodeError; | |
| 1626 return 0; | |
| 1627 } | |
| 1628 len_in_bits = (len << 3) - state->bit_string_unused_bits; | |
| 1629 // Protect against overflow when computing the total length in bits. | |
| 1630 if (UINT_MAX - item->len < len_in_bits) { | |
| 1631 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 1632 state->top->status = decodeError; | |
| 1633 return 0; | |
| 1634 } | |
| 1635 item->len += len_in_bits; | |
| 1636 } else { | |
| 1637 if (UINT_MAX - item->len < len) { | |
| 1638 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 1639 state->top->status = decodeError; | |
| 1640 return 0; | |
| 1641 } | |
| 1642 item->len += len; | |
| 1643 } | |
| 1644 PORT_Memcpy (item->data + offset, buf, len); | |
| 1645 } | |
| 1646 state->pending -= bufLen; | |
| 1647 if (state->pending == 0) | |
| 1648 state->place = beforeEndOfContents; | |
| 1649 | |
| 1650 return bufLen; | |
| 1651 } | |
| 1652 | |
| 1653 | |
| 1654 static unsigned long | |
| 1655 sec_asn1d_parse_bit_string (sec_asn1d_state *state, | |
| 1656 const char *buf, unsigned long len) | |
| 1657 { | |
| 1658 unsigned char byte; | |
| 1659 | |
| 1660 /*PORT_Assert (state->pending > 0); */ | |
| 1661 PORT_Assert (state->place == beforeBitString); | |
| 1662 | |
| 1663 if (state->pending == 0) { | |
| 1664 if (state->dest != NULL) { | |
| 1665 SECItem *item = (SECItem *)(state->dest); | |
| 1666 item->data = NULL; | |
| 1667 item->len = 0; | |
| 1668 state->place = beforeEndOfContents; | |
| 1669 return 0; | |
| 1670 } | |
| 1671 } | |
| 1672 | |
| 1673 if (len == 0) { | |
| 1674 state->top->status = needBytes; | |
| 1675 return 0; | |
| 1676 } | |
| 1677 | |
| 1678 byte = (unsigned char) *buf; | |
| 1679 if (byte > 7) { | |
| 1680 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 1681 state->top->status = decodeError; | |
| 1682 return 0; | |
| 1683 } | |
| 1684 | |
| 1685 state->bit_string_unused_bits = byte; | |
| 1686 state->place = duringBitString; | |
| 1687 state->pending -= 1; | |
| 1688 | |
| 1689 return 1; | |
| 1690 } | |
| 1691 | |
| 1692 | |
| 1693 static unsigned long | |
| 1694 sec_asn1d_parse_more_bit_string (sec_asn1d_state *state, | |
| 1695 const char *buf, unsigned long len) | |
| 1696 { | |
| 1697 PORT_Assert (state->place == duringBitString); | |
| 1698 if (state->pending == 0) { | |
| 1699 /* An empty bit string with some unused bits is invalid. */ | |
| 1700 if (state->bit_string_unused_bits) { | |
| 1701 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 1702 state->top->status = decodeError; | |
| 1703 } else { | |
| 1704 /* An empty bit string with no unused bits is OK. */ | |
| 1705 state->place = beforeEndOfContents; | |
| 1706 } | |
| 1707 return 0; | |
| 1708 } | |
| 1709 | |
| 1710 len = sec_asn1d_parse_leaf (state, buf, len); | |
| 1711 return len; | |
| 1712 } | |
| 1713 | |
| 1714 | |
| 1715 /* | |
| 1716 * XXX All callers should be looking at return value to detect | |
| 1717 * out-of-memory errors (and stop!). | |
| 1718 */ | |
| 1719 static struct subitem * | |
| 1720 sec_asn1d_add_to_subitems (sec_asn1d_state *state, | |
| 1721 const void *data, unsigned long len, | |
| 1722 PRBool copy_data) | |
| 1723 { | |
| 1724 struct subitem *thing; | |
| 1725 | |
| 1726 thing = (struct subitem*)sec_asn1d_zalloc (state->top->our_pool, | |
| 1727 sizeof (struct subitem)); | |
| 1728 if (thing == NULL) { | |
| 1729 state->top->status = decodeError; | |
| 1730 return NULL; | |
| 1731 } | |
| 1732 | |
| 1733 if (copy_data) { | |
| 1734 void *copy; | |
| 1735 copy = sec_asn1d_alloc (state->top->our_pool, len); | |
| 1736 if (copy == NULL) { | |
| 1737 state->top->status = decodeError; | |
| 1738 if (!state->top->our_pool) | |
| 1739 PORT_Free(thing); | |
| 1740 return NULL; | |
| 1741 } | |
| 1742 PORT_Memcpy (copy, data, len); | |
| 1743 thing->data = copy; | |
| 1744 } else { | |
| 1745 thing->data = data; | |
| 1746 } | |
| 1747 thing->len = len; | |
| 1748 thing->next = NULL; | |
| 1749 | |
| 1750 if (state->subitems_head == NULL) { | |
| 1751 PORT_Assert (state->subitems_tail == NULL); | |
| 1752 state->subitems_head = state->subitems_tail = thing; | |
| 1753 } else { | |
| 1754 state->subitems_tail->next = thing; | |
| 1755 state->subitems_tail = thing; | |
| 1756 } | |
| 1757 | |
| 1758 return thing; | |
| 1759 } | |
| 1760 | |
| 1761 | |
| 1762 static void | |
| 1763 sec_asn1d_record_any_header (sec_asn1d_state *state, | |
| 1764 const char *buf, | |
| 1765 unsigned long len) | |
| 1766 { | |
| 1767 SECItem *item; | |
| 1768 | |
| 1769 item = (SECItem *)(state->dest); | |
| 1770 if (item != NULL && item->data != NULL) { | |
| 1771 PORT_Assert (state->substring); | |
| 1772 PORT_Memcpy (item->data + item->len, buf, len); | |
| 1773 item->len += len; | |
| 1774 } else { | |
| 1775 sec_asn1d_add_to_subitems (state, buf, len, PR_TRUE); | |
| 1776 } | |
| 1777 } | |
| 1778 | |
| 1779 | |
| 1780 /* | |
| 1781 * We are moving along through the substrings of a constructed string, | |
| 1782 * and have just finished parsing one -- we need to save our child data | |
| 1783 * (if the child was not already writing directly into the destination) | |
| 1784 * and then move forward by one. | |
| 1785 * | |
| 1786 * We also have to detect when we are done: | |
| 1787 * - a definite-length encoding stops when our pending value hits 0 | |
| 1788 * - an indefinite-length encoding stops when our child is empty | |
| 1789 * (which means it was the end-of-contents octets) | |
| 1790 */ | |
| 1791 static void | |
| 1792 sec_asn1d_next_substring (sec_asn1d_state *state) | |
| 1793 { | |
| 1794 sec_asn1d_state *child; | |
| 1795 SECItem *item; | |
| 1796 unsigned long child_consumed; | |
| 1797 PRBool done; | |
| 1798 | |
| 1799 PORT_Assert (state->place == duringConstructedString); | |
| 1800 PORT_Assert (state->child != NULL); | |
| 1801 | |
| 1802 child = state->child; | |
| 1803 | |
| 1804 child_consumed = child->consumed; | |
| 1805 child->consumed = 0; | |
| 1806 state->consumed += child_consumed; | |
| 1807 | |
| 1808 done = PR_FALSE; | |
| 1809 | |
| 1810 if (state->pending) { | |
| 1811 PORT_Assert (!state->indefinite); | |
| 1812 if (child_consumed > state->pending) { | |
| 1813 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 1814 state->top->status = decodeError; | |
| 1815 return; | |
| 1816 } | |
| 1817 | |
| 1818 state->pending -= child_consumed; | |
| 1819 if (state->pending == 0) | |
| 1820 done = PR_TRUE; | |
| 1821 } else { | |
| 1822 PRBool preallocatedString; | |
| 1823 sec_asn1d_state *temp_state; | |
| 1824 PORT_Assert (state->indefinite); | |
| 1825 | |
| 1826 item = (SECItem *)(child->dest); | |
| 1827 | |
| 1828 /** | |
| 1829 * At this point, there's three states at play: | |
| 1830 * child: The element that was just parsed | |
| 1831 * state: The currently processed element | |
| 1832 * 'parent' (aka state->parent): The enclosing construct | |
| 1833 * of state, or NULL if this is the top-most element. | |
| 1834 * | |
| 1835 * This state handles both substrings of a constructed string AND | |
| 1836 * child elements of items whose template type was that of | |
| 1837 * SEC_ASN1_ANY, SEC_ASN1_SAVE, SEC_ASN1_ANY_CONTENTS, SEC_ASN1_SKIP | |
| 1838 * template, as described in sec_asn1d_prepare_for_contents. For | |
| 1839 * brevity, these will be referred to as 'string' and 'any' types. | |
| 1840 * | |
| 1841 * This leads to the following possibilities: | |
| 1842 * 1: This element is an indefinite length string, part of a | |
| 1843 * definite length string. | |
| 1844 * 2: This element is an indefinite length string, part of an | |
| 1845 * indefinite length string. | |
| 1846 * 3: This element is an indefinite length any, part of a | |
| 1847 * definite length any. | |
| 1848 * 4: This element is an indefinite length any, part of an | |
| 1849 * indefinite length any. | |
| 1850 * 5: This element is an indefinite length any and does not | |
| 1851 * meet any of the above criteria. Note that this would include | |
| 1852 * an indefinite length string type matching an indefinite | |
| 1853 * length any template. | |
| 1854 * | |
| 1855 * In Cases #1 and #3, the definite length 'parent' element will | |
| 1856 * have allocated state->dest based on the parent elements definite | |
| 1857 * size. During the processing of 'child', sec_asn1d_parse_leaf will | |
| 1858 * have copied the (string, any) data directly into the offset of | |
| 1859 * dest, as appropriate, so there's no need for this class to still | |
| 1860 * store the child - it's already been processed. | |
| 1861 * | |
| 1862 * In Cases #2 and #4, dest will be set to the parent element's dest, | |
| 1863 * but dest->data will not have been allocated yet, due to the | |
| 1864 * indefinite length encoding. In this situation, it's necessary to | |
| 1865 * hold onto child (and all other children) until the EOC, at which | |
| 1866 * point, it becomes possible to compute 'state's overall length. Once | |
| 1867 * 'state' has a computed length, this can then be fed to 'parent' (via | |
| 1868 * this state), and then 'parent' can similarly compute the length of | |
| 1869 * all of its children up to the EOC, which will ultimately transit to | |
| 1870 * sec_asn1d_concat_substrings, determine the overall size needed, | |
| 1871 * allocate, and copy the contents (of all of parent's children, which | |
| 1872 * would include 'state', just as 'state' will have copied all of its | |
| 1873 * children via sec_asn1d_concat_substrings) | |
| 1874 * | |
| 1875 * The final case, Case #5, will manifest in that item->data and | |
| 1876 * item->len will be NULL/0, respectively, since this element was | |
| 1877 * indefinite-length encoded. In that case, both the tag and length will | |
| 1878 * already exist in state's subitems, via sec_asn1d_record_any_header, | |
| 1879 * and so the contents (aka 'child') should be added to that list of | |
| 1880 * items to concatenate in sec_asn1d_concat_substrings once the EOC | |
| 1881 * is encountered. | |
| 1882 * | |
| 1883 * To distinguish #2/#4 from #1/#3, it's sufficient to walk the ancestor | |
| 1884 * tree. If the current type is a string type, then the enclosing | |
| 1885 * construct will be that same type (#1/#2). If the current type is an | |
| 1886 * any type, then the enclosing construct is either an any type (#3/#4) | |
| 1887 * or some other type (#5). Since this is BER, this nesting relationship | |
| 1888 * between 'state' and 'parent' may go through several levels of | |
| 1889 * constructed encoding, so continue walking the ancestor chain until a | |
| 1890 * clear determination can be made. | |
| 1891 * | |
| 1892 * The variable preallocatedString is used to indicate Case #1/#3, | |
| 1893 * indicating an in-place copy has already occurred, and Cases #2, #4, | |
| 1894 * and #5 all have the same behaviour of adding a new substring. | |
| 1895 */ | |
| 1896 preallocatedString = PR_FALSE; | |
| 1897 temp_state = state; | |
| 1898 while (temp_state && item == temp_state->dest && temp_state->indefinite)
{ | |
| 1899 sec_asn1d_state *parent = sec_asn1d_get_enclosing_construct(temp_sta
te); | |
| 1900 if (!parent || parent->underlying_kind != temp_state->underlying_kin
d) { | |
| 1901 /* Case #5 - Either this is a top-level construct or it is part | |
| 1902 * of some other element (e.g. a SEQUENCE), in which case, a | |
| 1903 * new item should be allocated. */ | |
| 1904 break; | |
| 1905 } | |
| 1906 if (!parent->indefinite) { | |
| 1907 /* Cases #1 / #3 - A definite length ancestor exists, for which | |
| 1908 * this is a substring that has already copied into dest. */ | |
| 1909 preallocatedString = PR_TRUE; | |
| 1910 break; | |
| 1911 } | |
| 1912 if (!parent->substring) { | |
| 1913 /* Cases #2 / #4 - If the parent is not a substring, but is | |
| 1914 * indefinite, then there's nothing further up that may have | |
| 1915 * preallocated dest, thus child will not have already | |
| 1916 * been copied in place, therefore it's necessary to save child | |
| 1917 * as a subitem. */ | |
| 1918 break; | |
| 1919 } | |
| 1920 temp_state = parent; | |
| 1921 } | |
| 1922 if (item != NULL && item->data != NULL && !preallocatedString) { | |
| 1923 /* | |
| 1924 * Save the string away for later concatenation. | |
| 1925 */ | |
| 1926 PORT_Assert (item->data != NULL); | |
| 1927 sec_asn1d_add_to_subitems (state, item->data, item->len, PR_FALSE); | |
| 1928 /* | |
| 1929 * Clear the child item for the next round. | |
| 1930 */ | |
| 1931 item->data = NULL; | |
| 1932 item->len = 0; | |
| 1933 } | |
| 1934 | |
| 1935 /* | |
| 1936 * If our child was just our end-of-contents octets, we are done. | |
| 1937 */ | |
| 1938 if (child->endofcontents) | |
| 1939 done = PR_TRUE; | |
| 1940 } | |
| 1941 | |
| 1942 /* | |
| 1943 * Stop or do the next one. | |
| 1944 */ | |
| 1945 if (done) { | |
| 1946 child->place = notInUse; | |
| 1947 state->place = afterConstructedString; | |
| 1948 } else { | |
| 1949 sec_asn1d_scrub_state (child); | |
| 1950 state->top->current = child; | |
| 1951 } | |
| 1952 } | |
| 1953 | |
| 1954 | |
| 1955 /* | |
| 1956 * We are doing a SET OF or SEQUENCE OF, and have just finished an item. | |
| 1957 */ | |
| 1958 static void | |
| 1959 sec_asn1d_next_in_group (sec_asn1d_state *state) | |
| 1960 { | |
| 1961 sec_asn1d_state *child; | |
| 1962 unsigned long child_consumed; | |
| 1963 | |
| 1964 PORT_Assert (state->place == duringGroup); | |
| 1965 PORT_Assert (state->child != NULL); | |
| 1966 | |
| 1967 child = state->child; | |
| 1968 | |
| 1969 child_consumed = child->consumed; | |
| 1970 child->consumed = 0; | |
| 1971 state->consumed += child_consumed; | |
| 1972 | |
| 1973 /* | |
| 1974 * If our child was just our end-of-contents octets, we are done. | |
| 1975 */ | |
| 1976 if (child->endofcontents) { | |
| 1977 /* XXX I removed the PORT_Assert (child->dest == NULL) because there | |
| 1978 * was a bug in that a template that was a sequence of which also had | |
| 1979 * a child of a sequence of, in an indefinite group was not working | |
| 1980 * properly. This fix seems to work, (added the if statement below), | |
| 1981 * and nothing appears broken, but I am putting this note here just | |
| 1982 * in case. */ | |
| 1983 /* | |
| 1984 * XXX No matter how many times I read that comment, | |
| 1985 * I cannot figure out what case he was fixing. I believe what he | |
| 1986 * did was deliberate, so I am loathe to touch it. I need to | |
| 1987 * understand how it could ever be that child->dest != NULL but | |
| 1988 * child->endofcontents is true, and why it is important to check | |
| 1989 * that state->subitems_head is NULL. This really needs to be | |
| 1990 * figured out, as I am not sure if the following code should be | |
| 1991 * compensating for "offset", as is done a little farther below | |
| 1992 * in the more normal case. | |
| 1993 */ | |
| 1994 PORT_Assert (state->indefinite); | |
| 1995 PORT_Assert (state->pending == 0); | |
| 1996 if(child->dest && !state->subitems_head) { | |
| 1997 sec_asn1d_add_to_subitems (state, child->dest, 0, PR_FALSE); | |
| 1998 child->dest = NULL; | |
| 1999 } | |
| 2000 | |
| 2001 child->place = notInUse; | |
| 2002 state->place = afterGroup; | |
| 2003 return; | |
| 2004 } | |
| 2005 | |
| 2006 /* | |
| 2007 * Do the "after" field notification for next in group. | |
| 2008 */ | |
| 2009 sec_asn1d_notify_after (state->top, child->dest, child->depth); | |
| 2010 | |
| 2011 /* | |
| 2012 * Save it away (unless we are not storing). | |
| 2013 */ | |
| 2014 if (child->dest != NULL) { | |
| 2015 void *dest; | |
| 2016 | |
| 2017 dest = child->dest; | |
| 2018 dest = (char *)dest - child->theTemplate->offset; | |
| 2019 sec_asn1d_add_to_subitems (state, dest, 0, PR_FALSE); | |
| 2020 child->dest = NULL; | |
| 2021 } | |
| 2022 | |
| 2023 /* | |
| 2024 * Account for those bytes; see if we are done. | |
| 2025 */ | |
| 2026 if (state->pending) { | |
| 2027 PORT_Assert (!state->indefinite); | |
| 2028 if (child_consumed > state->pending) { | |
| 2029 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 2030 state->top->status = decodeError; | |
| 2031 return; | |
| 2032 } | |
| 2033 | |
| 2034 state->pending -= child_consumed; | |
| 2035 if (state->pending == 0) { | |
| 2036 child->place = notInUse; | |
| 2037 state->place = afterGroup; | |
| 2038 return; | |
| 2039 } | |
| 2040 } | |
| 2041 | |
| 2042 /* | |
| 2043 * Do the "before" field notification for next item in group. | |
| 2044 */ | |
| 2045 sec_asn1d_notify_before (state->top, child->dest, child->depth); | |
| 2046 | |
| 2047 /* | |
| 2048 * Now we do the next one. | |
| 2049 */ | |
| 2050 sec_asn1d_scrub_state (child); | |
| 2051 | |
| 2052 /* Initialize child state from the template */ | |
| 2053 sec_asn1d_init_state_based_on_template(child); | |
| 2054 | |
| 2055 state->top->current = child; | |
| 2056 } | |
| 2057 | |
| 2058 | |
| 2059 /* | |
| 2060 * We are moving along through a sequence; move forward by one, | |
| 2061 * (detecting end-of-sequence when it happens). | |
| 2062 * XXX The handling of "missing" is ugly. Fix it. | |
| 2063 */ | |
| 2064 static void | |
| 2065 sec_asn1d_next_in_sequence (sec_asn1d_state *state) | |
| 2066 { | |
| 2067 sec_asn1d_state *child; | |
| 2068 unsigned long child_consumed; | |
| 2069 PRBool child_missing; | |
| 2070 | |
| 2071 PORT_Assert (state->place == duringSequence); | |
| 2072 PORT_Assert (state->child != NULL); | |
| 2073 | |
| 2074 child = state->child; | |
| 2075 | |
| 2076 /* | |
| 2077 * Do the "after" field notification. | |
| 2078 */ | |
| 2079 sec_asn1d_notify_after (state->top, child->dest, child->depth); | |
| 2080 | |
| 2081 child_missing = (PRBool) child->missing; | |
| 2082 child_consumed = child->consumed; | |
| 2083 child->consumed = 0; | |
| 2084 | |
| 2085 /* | |
| 2086 * Take care of accounting. | |
| 2087 */ | |
| 2088 if (child_missing) { | |
| 2089 PORT_Assert (child->optional); | |
| 2090 } else { | |
| 2091 state->consumed += child_consumed; | |
| 2092 /* | |
| 2093 * Free any grandchild. | |
| 2094 */ | |
| 2095 sec_asn1d_free_child (child, PR_FALSE); | |
| 2096 if (state->pending) { | |
| 2097 PORT_Assert (!state->indefinite); | |
| 2098 if (child_consumed > state->pending) { | |
| 2099 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 2100 state->top->status = decodeError; | |
| 2101 return; | |
| 2102 } | |
| 2103 state->pending -= child_consumed; | |
| 2104 if (state->pending == 0) { | |
| 2105 child->theTemplate++; | |
| 2106 while (child->theTemplate->kind != 0) { | |
| 2107 if ((child->theTemplate->kind & SEC_ASN1_OPTIONAL) == 0) { | |
| 2108 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 2109 state->top->status = decodeError; | |
| 2110 return; | |
| 2111 } | |
| 2112 child->theTemplate++; | |
| 2113 } | |
| 2114 child->place = notInUse; | |
| 2115 state->place = afterEndOfContents; | |
| 2116 return; | |
| 2117 } | |
| 2118 } | |
| 2119 } | |
| 2120 | |
| 2121 /* | |
| 2122 * Move forward. | |
| 2123 */ | |
| 2124 child->theTemplate++; | |
| 2125 if (child->theTemplate->kind == 0) { | |
| 2126 /* | |
| 2127 * We are done with this sequence. | |
| 2128 */ | |
| 2129 child->place = notInUse; | |
| 2130 if (state->pending) { | |
| 2131 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 2132 state->top->status = decodeError; | |
| 2133 } else if (child_missing) { | |
| 2134 /* | |
| 2135 * We got to the end, but have a child that started parsing | |
| 2136 * and ended up "missing". The only legitimate reason for | |
| 2137 * this is that we had one or more optional fields at the | |
| 2138 * end of our sequence, and we were encoded indefinite-length, | |
| 2139 * so when we went looking for those optional fields we | |
| 2140 * found our end-of-contents octets instead. | |
| 2141 * (Yes, this is ugly; dunno a better way to handle it.) | |
| 2142 * So, first confirm the situation, and then mark that we | |
| 2143 * are done. | |
| 2144 */ | |
| 2145 if (state->indefinite && child->endofcontents) { | |
| 2146 PORT_Assert (child_consumed == 2); | |
| 2147 if (child_consumed != 2) { | |
| 2148 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 2149 state->top->status = decodeError; | |
| 2150 } else { | |
| 2151 state->consumed += child_consumed; | |
| 2152 state->place = afterEndOfContents; | |
| 2153 } | |
| 2154 } else { | |
| 2155 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 2156 state->top->status = decodeError; | |
| 2157 } | |
| 2158 } else { | |
| 2159 /* | |
| 2160 * We have to finish out, maybe reading end-of-contents octets; | |
| 2161 * let the normal logic do the right thing. | |
| 2162 */ | |
| 2163 state->place = beforeEndOfContents; | |
| 2164 } | |
| 2165 } else { | |
| 2166 unsigned char child_found_tag_modifiers = 0; | |
| 2167 unsigned long child_found_tag_number = 0; | |
| 2168 | |
| 2169 /* | |
| 2170 * Reset state and push. | |
| 2171 */ | |
| 2172 if (state->dest != NULL) | |
| 2173 child->dest = (char *)state->dest + child->theTemplate->offset; | |
| 2174 | |
| 2175 /* | |
| 2176 * Do the "before" field notification. | |
| 2177 */ | |
| 2178 sec_asn1d_notify_before (state->top, child->dest, child->depth); | |
| 2179 | |
| 2180 if (child_missing) { /* if previous child was missing, copy the tag data
we already have */ | |
| 2181 child_found_tag_modifiers = child->found_tag_modifiers; | |
| 2182 child_found_tag_number = child->found_tag_number; | |
| 2183 } | |
| 2184 state->top->current = child; | |
| 2185 child = sec_asn1d_init_state_based_on_template (child); | |
| 2186 if (child_missing && child) { | |
| 2187 child->place = afterIdentifier; | |
| 2188 child->found_tag_modifiers = child_found_tag_modifiers; | |
| 2189 child->found_tag_number = child_found_tag_number; | |
| 2190 child->consumed = child_consumed; | |
| 2191 if (child->underlying_kind == SEC_ASN1_ANY | |
| 2192 && !child->top->filter_only) { | |
| 2193 /* | |
| 2194 * If the new field is an ANY, and we are storing, then | |
| 2195 * we need to save the tag out. We would have done this | |
| 2196 * already in the normal case, but since we were looking | |
| 2197 * for an optional field, and we did not find it, we only | |
| 2198 * now realize we need to save the tag. | |
| 2199 */ | |
| 2200 unsigned char identifier; | |
| 2201 | |
| 2202 /* | |
| 2203 * Check that we did not end up with a high tag; for that | |
| 2204 * we need to re-encode the tag into multiple bytes in order | |
| 2205 * to store it back to look like what we parsed originally. | |
| 2206 * In practice this does not happen, but for completeness | |
| 2207 * sake it should probably be made to work at some point. | |
| 2208 */ | |
| 2209 PORT_Assert (child_found_tag_number < SEC_ASN1_HIGH_TAG_NUMBER); | |
| 2210 identifier = (unsigned char)(child_found_tag_modifiers | child_f
ound_tag_number); | |
| 2211 sec_asn1d_record_any_header (child, (char *) &identifier, 1); | |
| 2212 } | |
| 2213 } | |
| 2214 } | |
| 2215 } | |
| 2216 | |
| 2217 | |
| 2218 static void | |
| 2219 sec_asn1d_concat_substrings (sec_asn1d_state *state) | |
| 2220 { | |
| 2221 PORT_Assert (state->place == afterConstructedString); | |
| 2222 | |
| 2223 if (state->subitems_head != NULL) { | |
| 2224 struct subitem *substring; | |
| 2225 unsigned long alloc_len, item_len; | |
| 2226 unsigned char *where; | |
| 2227 SECItem *item; | |
| 2228 PRBool is_bit_string; | |
| 2229 | |
| 2230 item_len = 0; | |
| 2231 is_bit_string = (state->underlying_kind == SEC_ASN1_BIT_STRING) | |
| 2232 ? PR_TRUE : PR_FALSE; | |
| 2233 | |
| 2234 substring = state->subitems_head; | |
| 2235 while (substring != NULL) { | |
| 2236 /* | |
| 2237 * All bit-string substrings except the last one should be | |
| 2238 * a clean multiple of 8 bits. | |
| 2239 */ | |
| 2240 if (is_bit_string && (substring->next != NULL) | |
| 2241 && (substring->len & 0x7)) { | |
| 2242 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 2243 state->top->status = decodeError; | |
| 2244 return; | |
| 2245 } | |
| 2246 item_len += substring->len; | |
| 2247 substring = substring->next; | |
| 2248 } | |
| 2249 | |
| 2250 if (is_bit_string) { | |
| 2251 alloc_len = ((item_len + 7) >> 3); | |
| 2252 } else { | |
| 2253 /* | |
| 2254 * Add 2 for the end-of-contents octets of an indefinite-length | |
| 2255 * ANY that is *not* also an INNER. Because we zero-allocate | |
| 2256 * below, all we need to do is increase the length here. | |
| 2257 */ | |
| 2258 if (state->underlying_kind == SEC_ASN1_ANY && state->indefinite) | |
| 2259 item_len += 2; | |
| 2260 alloc_len = item_len; | |
| 2261 } | |
| 2262 | |
| 2263 item = (SECItem *)(state->dest); | |
| 2264 PORT_Assert (item != NULL); | |
| 2265 PORT_Assert (item->data == NULL); | |
| 2266 item->data = (unsigned char*)sec_asn1d_zalloc (state->top->their_pool, | |
| 2267 alloc_len); | |
| 2268 if (item->data == NULL) { | |
| 2269 state->top->status = decodeError; | |
| 2270 return; | |
| 2271 } | |
| 2272 item->len = item_len; | |
| 2273 | |
| 2274 where = item->data; | |
| 2275 substring = state->subitems_head; | |
| 2276 while (substring != NULL) { | |
| 2277 if (is_bit_string) | |
| 2278 item_len = (substring->len + 7) >> 3; | |
| 2279 else | |
| 2280 item_len = substring->len; | |
| 2281 PORT_Memcpy (where, substring->data, item_len); | |
| 2282 where += item_len; | |
| 2283 substring = substring->next; | |
| 2284 } | |
| 2285 | |
| 2286 /* | |
| 2287 * Because we use arenas and have a mark set, we later free | |
| 2288 * everything we have allocated, so this does *not* present | |
| 2289 * a memory leak (it is just temporarily left dangling). | |
| 2290 */ | |
| 2291 state->subitems_head = state->subitems_tail = NULL; | |
| 2292 } | |
| 2293 | |
| 2294 state->place = afterEndOfContents; | |
| 2295 } | |
| 2296 | |
| 2297 | |
| 2298 static void | |
| 2299 sec_asn1d_concat_group (sec_asn1d_state *state) | |
| 2300 { | |
| 2301 const void ***placep; | |
| 2302 | |
| 2303 PORT_Assert (state->place == afterGroup); | |
| 2304 | |
| 2305 placep = (const void***)state->dest; | |
| 2306 PORT_Assert(state->subitems_head == NULL || placep != NULL); | |
| 2307 if (placep != NULL) { | |
| 2308 struct subitem *item; | |
| 2309 const void **group; | |
| 2310 int count; | |
| 2311 | |
| 2312 count = 0; | |
| 2313 item = state->subitems_head; | |
| 2314 while (item != NULL) { | |
| 2315 PORT_Assert (item->next != NULL || item == state->subitems_tail); | |
| 2316 count++; | |
| 2317 item = item->next; | |
| 2318 } | |
| 2319 | |
| 2320 group = (const void**)sec_asn1d_zalloc (state->top->their_pool, | |
| 2321 (count + 1) * (sizeof(void *))); | |
| 2322 if (group == NULL) { | |
| 2323 state->top->status = decodeError; | |
| 2324 return; | |
| 2325 } | |
| 2326 | |
| 2327 *placep = group; | |
| 2328 | |
| 2329 item = state->subitems_head; | |
| 2330 while (item != NULL) { | |
| 2331 *group++ = item->data; | |
| 2332 item = item->next; | |
| 2333 } | |
| 2334 *group = NULL; | |
| 2335 | |
| 2336 /* | |
| 2337 * Because we use arenas and have a mark set, we later free | |
| 2338 * everything we have allocated, so this does *not* present | |
| 2339 * a memory leak (it is just temporarily left dangling). | |
| 2340 */ | |
| 2341 state->subitems_head = state->subitems_tail = NULL; | |
| 2342 } | |
| 2343 | |
| 2344 state->place = afterEndOfContents; | |
| 2345 } | |
| 2346 | |
| 2347 | |
| 2348 /* | |
| 2349 * For those states that push a child to handle a subtemplate, | |
| 2350 * "absorb" that child (transfer necessary information). | |
| 2351 */ | |
| 2352 static void | |
| 2353 sec_asn1d_absorb_child (sec_asn1d_state *state) | |
| 2354 { | |
| 2355 /* | |
| 2356 * There is absolutely supposed to be a child there. | |
| 2357 */ | |
| 2358 PORT_Assert (state->child != NULL); | |
| 2359 | |
| 2360 /* | |
| 2361 * Inherit the missing status of our child, and do the ugly | |
| 2362 * backing-up if necessary. | |
| 2363 */ | |
| 2364 state->missing = state->child->missing; | |
| 2365 if (state->missing) { | |
| 2366 state->found_tag_number = state->child->found_tag_number; | |
| 2367 state->found_tag_modifiers = state->child->found_tag_modifiers; | |
| 2368 state->endofcontents = state->child->endofcontents; | |
| 2369 } | |
| 2370 | |
| 2371 /* | |
| 2372 * Add in number of bytes consumed by child. | |
| 2373 * (Only EXPLICIT should have already consumed bytes itself.) | |
| 2374 */ | |
| 2375 PORT_Assert (state->place == afterExplicit || state->consumed == 0); | |
| 2376 state->consumed += state->child->consumed; | |
| 2377 | |
| 2378 /* | |
| 2379 * Subtract from bytes pending; this only applies to a definite-length | |
| 2380 * EXPLICIT field. | |
| 2381 */ | |
| 2382 if (state->pending) { | |
| 2383 PORT_Assert (!state->indefinite); | |
| 2384 PORT_Assert (state->place == afterExplicit); | |
| 2385 | |
| 2386 /* | |
| 2387 * If we had a definite-length explicit, then what the child | |
| 2388 * consumed should be what was left pending. | |
| 2389 */ | |
| 2390 if (state->pending != state->child->consumed) { | |
| 2391 if (state->pending < state->child->consumed) { | |
| 2392 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 2393 state->top->status = decodeError; | |
| 2394 return; | |
| 2395 } | |
| 2396 /* | |
| 2397 * Okay, this is a hack. It *should* be an error whether | |
| 2398 * pending is too big or too small, but it turns out that | |
| 2399 * we had a bug in our *old* DER encoder that ended up | |
| 2400 * counting an explicit header twice in the case where | |
| 2401 * the underlying type was an ANY. So, because we cannot | |
| 2402 * prevent receiving these (our own certificate server can | |
| 2403 * send them to us), we need to be lenient and accept them. | |
| 2404 * To do so, we need to pretend as if we read all of the | |
| 2405 * bytes that the header said we would find, even though | |
| 2406 * we actually came up short. | |
| 2407 */ | |
| 2408 state->consumed += (state->pending - state->child->consumed); | |
| 2409 } | |
| 2410 state->pending = 0; | |
| 2411 } | |
| 2412 | |
| 2413 /* | |
| 2414 * Indicate that we are done with child. | |
| 2415 */ | |
| 2416 state->child->consumed = 0; | |
| 2417 | |
| 2418 /* | |
| 2419 * And move on to final state. | |
| 2420 * (Technically everybody could move to afterEndOfContents except | |
| 2421 * for an indefinite-length EXPLICIT; for simplicity though we assert | |
| 2422 * that but let the end-of-contents code do the real determination.) | |
| 2423 */ | |
| 2424 PORT_Assert (state->place == afterExplicit || (! state->indefinite)); | |
| 2425 state->place = beforeEndOfContents; | |
| 2426 } | |
| 2427 | |
| 2428 | |
| 2429 static void | |
| 2430 sec_asn1d_prepare_for_end_of_contents (sec_asn1d_state *state) | |
| 2431 { | |
| 2432 PORT_Assert (state->place == beforeEndOfContents); | |
| 2433 | |
| 2434 if (state->indefinite) { | |
| 2435 state->place = duringEndOfContents; | |
| 2436 state->pending = 2; | |
| 2437 } else { | |
| 2438 state->place = afterEndOfContents; | |
| 2439 } | |
| 2440 } | |
| 2441 | |
| 2442 | |
| 2443 static unsigned long | |
| 2444 sec_asn1d_parse_end_of_contents (sec_asn1d_state *state, | |
| 2445 const char *buf, unsigned long len) | |
| 2446 { | |
| 2447 unsigned int i; | |
| 2448 | |
| 2449 PORT_Assert (state->pending <= 2); | |
| 2450 PORT_Assert (state->place == duringEndOfContents); | |
| 2451 | |
| 2452 if (len == 0) { | |
| 2453 state->top->status = needBytes; | |
| 2454 return 0; | |
| 2455 } | |
| 2456 | |
| 2457 if (state->pending < len) | |
| 2458 len = state->pending; | |
| 2459 | |
| 2460 for (i = 0; i < len; i++) { | |
| 2461 if (buf[i] != 0) { | |
| 2462 /* | |
| 2463 * We expect to find only zeros; if not, just give up. | |
| 2464 */ | |
| 2465 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 2466 state->top->status = decodeError; | |
| 2467 return 0; | |
| 2468 } | |
| 2469 } | |
| 2470 | |
| 2471 state->pending -= len; | |
| 2472 | |
| 2473 if (state->pending == 0) { | |
| 2474 state->place = afterEndOfContents; | |
| 2475 state->endofcontents = PR_TRUE; | |
| 2476 } | |
| 2477 | |
| 2478 return len; | |
| 2479 } | |
| 2480 | |
| 2481 | |
| 2482 static void | |
| 2483 sec_asn1d_pop_state (sec_asn1d_state *state) | |
| 2484 { | |
| 2485 #if 0 /* XXX I think this should always be handled explicitly by parent? */ | |
| 2486 /* | |
| 2487 * Account for our child. | |
| 2488 */ | |
| 2489 if (state->child != NULL) { | |
| 2490 state->consumed += state->child->consumed; | |
| 2491 if (state->pending) { | |
| 2492 PORT_Assert (!state->indefinite); | |
| 2493 if (state->child->consumed > state->pending) { | |
| 2494 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 2495 state->top->status = decodeError; | |
| 2496 } else { | |
| 2497 state->pending -= state->child->consumed; | |
| 2498 } | |
| 2499 } | |
| 2500 state->child->consumed = 0; | |
| 2501 } | |
| 2502 #endif /* XXX */ | |
| 2503 | |
| 2504 /* | |
| 2505 * Free our child. | |
| 2506 */ | |
| 2507 sec_asn1d_free_child (state, PR_FALSE); | |
| 2508 | |
| 2509 /* | |
| 2510 * Just make my parent be the current state. It will then clean | |
| 2511 * up after me and free me (or reuse me). | |
| 2512 */ | |
| 2513 state->top->current = state->parent; | |
| 2514 } | |
| 2515 | |
| 2516 static sec_asn1d_state * | |
| 2517 sec_asn1d_before_choice (sec_asn1d_state *state) | |
| 2518 { | |
| 2519 sec_asn1d_state *child; | |
| 2520 | |
| 2521 if (state->allocate) { | |
| 2522 void *dest; | |
| 2523 | |
| 2524 dest = sec_asn1d_zalloc(state->top->their_pool, state->theTemplate->size
); | |
| 2525 if ((void *)NULL == dest) { | |
| 2526 state->top->status = decodeError; | |
| 2527 return (sec_asn1d_state *)NULL; | |
| 2528 } | |
| 2529 | |
| 2530 state->dest = (char *)dest + state->theTemplate->offset; | |
| 2531 } | |
| 2532 | |
| 2533 child = sec_asn1d_push_state(state->top, state->theTemplate + 1, | |
| 2534 (char *)state->dest - state->theTemplate->offse
t, | |
| 2535 PR_FALSE); | |
| 2536 if ((sec_asn1d_state *)NULL == child) { | |
| 2537 return (sec_asn1d_state *)NULL; | |
| 2538 } | |
| 2539 | |
| 2540 sec_asn1d_scrub_state(child); | |
| 2541 child = sec_asn1d_init_state_based_on_template(child); | |
| 2542 if ((sec_asn1d_state *)NULL == child) { | |
| 2543 return (sec_asn1d_state *)NULL; | |
| 2544 } | |
| 2545 | |
| 2546 child->optional = PR_TRUE; | |
| 2547 | |
| 2548 state->place = duringChoice; | |
| 2549 | |
| 2550 return child; | |
| 2551 } | |
| 2552 | |
| 2553 static sec_asn1d_state * | |
| 2554 sec_asn1d_during_choice (sec_asn1d_state *state) | |
| 2555 { | |
| 2556 sec_asn1d_state *child = state->child; | |
| 2557 | |
| 2558 PORT_Assert((sec_asn1d_state *)NULL != child); | |
| 2559 | |
| 2560 if (child->missing) { | |
| 2561 unsigned char child_found_tag_modifiers = 0; | |
| 2562 unsigned long child_found_tag_number = 0; | |
| 2563 void * dest; | |
| 2564 | |
| 2565 state->consumed += child->consumed; | |
| 2566 | |
| 2567 if (child->endofcontents) { | |
| 2568 /* This choice is probably the first item in a GROUP | |
| 2569 ** (e.g. SET_OF) that was indefinite-length encoded. | |
| 2570 ** We're actually at the end of that GROUP. | |
| 2571 ** We look up the stack to be sure that we find | |
| 2572 ** a state with indefinite length encoding before we | |
| 2573 ** find a state (like a SEQUENCE) that is definite. | |
| 2574 */ | |
| 2575 child->place = notInUse; | |
| 2576 state->place = afterChoice; | |
| 2577 state->endofcontents = PR_TRUE; /* propagate this up */ | |
| 2578 if (sec_asn1d_parent_allows_EOC(state)) | |
| 2579 return state; | |
| 2580 PORT_SetError(SEC_ERROR_BAD_DER); | |
| 2581 state->top->status = decodeError; | |
| 2582 return NULL; | |
| 2583 } | |
| 2584 | |
| 2585 dest = (char *)child->dest - child->theTemplate->offset; | |
| 2586 child->theTemplate++; | |
| 2587 | |
| 2588 if (0 == child->theTemplate->kind) { | |
| 2589 /* Ran out of choices */ | |
| 2590 PORT_SetError(SEC_ERROR_BAD_DER); | |
| 2591 state->top->status = decodeError; | |
| 2592 return (sec_asn1d_state *)NULL; | |
| 2593 } | |
| 2594 child->dest = (char *)dest + child->theTemplate->offset; | |
| 2595 | |
| 2596 /* cargo'd from next_in_sequence innards */ | |
| 2597 if (state->pending) { | |
| 2598 PORT_Assert(!state->indefinite); | |
| 2599 if (child->consumed > state->pending) { | |
| 2600 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 2601 state->top->status = decodeError; | |
| 2602 return NULL; | |
| 2603 } | |
| 2604 state->pending -= child->consumed; | |
| 2605 if (0 == state->pending) { | |
| 2606 /* XXX uh.. not sure if I should have stopped this | |
| 2607 * from happening before. */ | |
| 2608 PORT_Assert(0); | |
| 2609 PORT_SetError(SEC_ERROR_BAD_DER); | |
| 2610 state->top->status = decodeError; | |
| 2611 return (sec_asn1d_state *)NULL; | |
| 2612 } | |
| 2613 } | |
| 2614 | |
| 2615 child->consumed = 0; | |
| 2616 sec_asn1d_scrub_state(child); | |
| 2617 | |
| 2618 /* move it on top again */ | |
| 2619 state->top->current = child; | |
| 2620 | |
| 2621 child_found_tag_modifiers = child->found_tag_modifiers; | |
| 2622 child_found_tag_number = child->found_tag_number; | |
| 2623 | |
| 2624 child = sec_asn1d_init_state_based_on_template(child); | |
| 2625 if ((sec_asn1d_state *)NULL == child) { | |
| 2626 return (sec_asn1d_state *)NULL; | |
| 2627 } | |
| 2628 | |
| 2629 /* copy our findings to the new top */ | |
| 2630 child->found_tag_modifiers = child_found_tag_modifiers; | |
| 2631 child->found_tag_number = child_found_tag_number; | |
| 2632 | |
| 2633 child->optional = PR_TRUE; | |
| 2634 child->place = afterIdentifier; | |
| 2635 | |
| 2636 return child; | |
| 2637 } | |
| 2638 if ((void *)NULL != state->dest) { | |
| 2639 /* Store the enum */ | |
| 2640 int *which = (int *)state->dest; | |
| 2641 *which = (int)child->theTemplate->size; | |
| 2642 } | |
| 2643 | |
| 2644 child->place = notInUse; | |
| 2645 | |
| 2646 state->place = afterChoice; | |
| 2647 return state; | |
| 2648 } | |
| 2649 | |
| 2650 static void | |
| 2651 sec_asn1d_after_choice (sec_asn1d_state *state) | |
| 2652 { | |
| 2653 state->consumed += state->child->consumed; | |
| 2654 state->child->consumed = 0; | |
| 2655 state->place = afterEndOfContents; | |
| 2656 sec_asn1d_pop_state(state); | |
| 2657 } | |
| 2658 | |
| 2659 unsigned long | |
| 2660 sec_asn1d_uinteger(SECItem *src) | |
| 2661 { | |
| 2662 unsigned long value; | |
| 2663 int len; | |
| 2664 | |
| 2665 if (src->len > 5 || (src->len > 4 && src->data[0] == 0)) | |
| 2666 return 0; | |
| 2667 | |
| 2668 value = 0; | |
| 2669 len = src->len; | |
| 2670 while (len) { | |
| 2671 value <<= 8; | |
| 2672 value |= src->data[--len]; | |
| 2673 } | |
| 2674 return value; | |
| 2675 } | |
| 2676 | |
| 2677 SECStatus | |
| 2678 SEC_ASN1DecodeInteger(SECItem *src, unsigned long *value) | |
| 2679 { | |
| 2680 unsigned long v; | |
| 2681 unsigned int i; | |
| 2682 | |
| 2683 if (src == NULL) { | |
| 2684 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
| 2685 return SECFailure; | |
| 2686 } | |
| 2687 | |
| 2688 if (src->len > sizeof(unsigned long)) { | |
| 2689 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
| 2690 return SECFailure; | |
| 2691 } | |
| 2692 | |
| 2693 if (src->data == NULL) { | |
| 2694 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
| 2695 return SECFailure; | |
| 2696 } | |
| 2697 | |
| 2698 if (src->data[0] & 0x80) | |
| 2699 v = -1; /* signed and negative - start with all 1's */ | |
| 2700 else | |
| 2701 v = 0; | |
| 2702 | |
| 2703 for (i= 0; i < src->len; i++) { | |
| 2704 /* shift in next byte */ | |
| 2705 v <<= 8; | |
| 2706 v |= src->data[i]; | |
| 2707 } | |
| 2708 *value = v; | |
| 2709 return SECSuccess; | |
| 2710 } | |
| 2711 | |
| 2712 #ifdef DEBUG_ASN1D_STATES | |
| 2713 static void | |
| 2714 dump_states(SEC_ASN1DecoderContext *cx) | |
| 2715 { | |
| 2716 sec_asn1d_state *state; | |
| 2717 char kindBuf[256]; | |
| 2718 | |
| 2719 for (state = cx->current; state->parent; state = state->parent) { | |
| 2720 ; | |
| 2721 } | |
| 2722 | |
| 2723 for (; state; state = state->child) { | |
| 2724 int i; | |
| 2725 for (i = 0; i < state->depth; i++) { | |
| 2726 printf(" "); | |
| 2727 } | |
| 2728 | |
| 2729 i = formatKind(state->theTemplate->kind, kindBuf); | |
| 2730 printf("%s: tmpl %08x, kind%s", | |
| 2731 (state == cx->current) ? "STATE" : "State", | |
| 2732 state->theTemplate, | |
| 2733 kindBuf); | |
| 2734 printf(" %s", (state->place >= 0 && state->place <= notInUse) | |
| 2735 ? place_names[ state->place ] | |
| 2736 : "(undefined)"); | |
| 2737 if (!i) | |
| 2738 printf(", expect 0x%02x", | |
| 2739 state->expect_tag_number | state->expect_tag_modifiers); | |
| 2740 | |
| 2741 printf("%s%s%s %d\n", | |
| 2742 state->indefinite ? ", indef" : "", | |
| 2743 state->missing ? ", miss" : "", | |
| 2744 state->endofcontents ? ", EOC" : "", | |
| 2745 state->pending | |
| 2746 ); | |
| 2747 } | |
| 2748 | |
| 2749 return; | |
| 2750 } | |
| 2751 #endif /* DEBUG_ASN1D_STATES */ | |
| 2752 | |
| 2753 SECStatus | |
| 2754 SEC_ASN1DecoderUpdate (SEC_ASN1DecoderContext *cx, | |
| 2755 const char *buf, unsigned long len) | |
| 2756 { | |
| 2757 sec_asn1d_state *state = NULL; | |
| 2758 unsigned long consumed; | |
| 2759 SEC_ASN1EncodingPart what; | |
| 2760 sec_asn1d_state *stateEnd = cx->current; | |
| 2761 | |
| 2762 if (cx->status == needBytes) | |
| 2763 cx->status = keepGoing; | |
| 2764 | |
| 2765 while (cx->status == keepGoing) { | |
| 2766 state = cx->current; | |
| 2767 what = SEC_ASN1_Contents; | |
| 2768 consumed = 0; | |
| 2769 #ifdef DEBUG_ASN1D_STATES | |
| 2770 printf("\nPLACE = %s, next byte = 0x%02x, %08x[%d]\n", | |
| 2771 (state->place >= 0 && state->place <= notInUse) ? | |
| 2772 place_names[ state->place ] : "(undefined)", | |
| 2773 (unsigned int)((unsigned char *)buf)[ consumed ], | |
| 2774 buf, consumed); | |
| 2775 dump_states(cx); | |
| 2776 #endif /* DEBUG_ASN1D_STATES */ | |
| 2777 switch (state->place) { | |
| 2778 case beforeIdentifier: | |
| 2779 consumed = sec_asn1d_parse_identifier (state, buf, len); | |
| 2780 what = SEC_ASN1_Identifier; | |
| 2781 break; | |
| 2782 case duringIdentifier: | |
| 2783 consumed = sec_asn1d_parse_more_identifier (state, buf, len); | |
| 2784 what = SEC_ASN1_Identifier; | |
| 2785 break; | |
| 2786 case afterIdentifier: | |
| 2787 sec_asn1d_confirm_identifier (state); | |
| 2788 break; | |
| 2789 case beforeLength: | |
| 2790 consumed = sec_asn1d_parse_length (state, buf, len); | |
| 2791 what = SEC_ASN1_Length; | |
| 2792 break; | |
| 2793 case duringLength: | |
| 2794 consumed = sec_asn1d_parse_more_length (state, buf, len); | |
| 2795 what = SEC_ASN1_Length; | |
| 2796 break; | |
| 2797 case afterLength: | |
| 2798 sec_asn1d_prepare_for_contents (state); | |
| 2799 break; | |
| 2800 case beforeBitString: | |
| 2801 consumed = sec_asn1d_parse_bit_string (state, buf, len); | |
| 2802 break; | |
| 2803 case duringBitString: | |
| 2804 consumed = sec_asn1d_parse_more_bit_string (state, buf, len); | |
| 2805 break; | |
| 2806 case duringConstructedString: | |
| 2807 sec_asn1d_next_substring (state); | |
| 2808 break; | |
| 2809 case duringGroup: | |
| 2810 sec_asn1d_next_in_group (state); | |
| 2811 break; | |
| 2812 case duringLeaf: | |
| 2813 consumed = sec_asn1d_parse_leaf (state, buf, len); | |
| 2814 break; | |
| 2815 case duringSaveEncoding: | |
| 2816 sec_asn1d_reuse_encoding (state); | |
| 2817 if (cx->status == decodeError) { | |
| 2818 /* recursive call has already popped all states from stack. | |
| 2819 ** Bail out quickly. | |
| 2820 */ | |
| 2821 return SECFailure; | |
| 2822 } | |
| 2823 if (cx->status == needBytes) { | |
| 2824 /* recursive call wanted more data. Fatal. Clean up below. */ | |
| 2825 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 2826 cx->status = decodeError; | |
| 2827 } | |
| 2828 break; | |
| 2829 case duringSequence: | |
| 2830 sec_asn1d_next_in_sequence (state); | |
| 2831 break; | |
| 2832 case afterConstructedString: | |
| 2833 sec_asn1d_concat_substrings (state); | |
| 2834 break; | |
| 2835 case afterExplicit: | |
| 2836 case afterImplicit: | |
| 2837 case afterInline: | |
| 2838 case afterPointer: | |
| 2839 sec_asn1d_absorb_child (state); | |
| 2840 break; | |
| 2841 case afterGroup: | |
| 2842 sec_asn1d_concat_group (state); | |
| 2843 break; | |
| 2844 case afterSaveEncoding: | |
| 2845 /* SEC_ASN1DecoderUpdate has called itself recursively to | |
| 2846 ** decode SAVEd encoded data, and now is done decoding that. | |
| 2847 ** Return to the calling copy of SEC_ASN1DecoderUpdate. | |
| 2848 */ | |
| 2849 return SECSuccess; | |
| 2850 case beforeEndOfContents: | |
| 2851 sec_asn1d_prepare_for_end_of_contents (state); | |
| 2852 break; | |
| 2853 case duringEndOfContents: | |
| 2854 consumed = sec_asn1d_parse_end_of_contents (state, buf, len); | |
| 2855 what = SEC_ASN1_EndOfContents; | |
| 2856 break; | |
| 2857 case afterEndOfContents: | |
| 2858 sec_asn1d_pop_state (state); | |
| 2859 break; | |
| 2860 case beforeChoice: | |
| 2861 state = sec_asn1d_before_choice(state); | |
| 2862 break; | |
| 2863 case duringChoice: | |
| 2864 state = sec_asn1d_during_choice(state); | |
| 2865 break; | |
| 2866 case afterChoice: | |
| 2867 sec_asn1d_after_choice(state); | |
| 2868 break; | |
| 2869 case notInUse: | |
| 2870 default: | |
| 2871 /* This is not an error, but rather a plain old BUG! */ | |
| 2872 PORT_Assert (0); | |
| 2873 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 2874 cx->status = decodeError; | |
| 2875 break; | |
| 2876 } | |
| 2877 | |
| 2878 if (cx->status == decodeError) | |
| 2879 break; | |
| 2880 | |
| 2881 /* We should not consume more than we have. */ | |
| 2882 PORT_Assert (consumed <= len); | |
| 2883 if (consumed > len) { | |
| 2884 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 2885 cx->status = decodeError; | |
| 2886 break; | |
| 2887 } | |
| 2888 | |
| 2889 /* It might have changed, so we have to update our local copy. */ | |
| 2890 state = cx->current; | |
| 2891 | |
| 2892 /* If it is NULL, we have popped all the way to the top. */ | |
| 2893 if (state == NULL) { | |
| 2894 PORT_Assert (consumed == 0); | |
| 2895 #if 0 /* XXX I want this here, but it seems that we have situations (like | |
| 2896 * downloading a pkcs7 cert chain from some issuers) that give us a | |
| 2897 * length which is greater than the entire encoding. So, we cannot | |
| 2898 * have this be an error. | |
| 2899 */ | |
| 2900 if (len > 0) { | |
| 2901 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 2902 cx->status = decodeError; | |
| 2903 } else | |
| 2904 #endif | |
| 2905 cx->status = allDone; | |
| 2906 break; | |
| 2907 } | |
| 2908 else if (state->theTemplate->kind == SEC_ASN1_SKIP_REST) { | |
| 2909 cx->status = allDone; | |
| 2910 break; | |
| 2911 } | |
| 2912 | |
| 2913 if (consumed == 0) | |
| 2914 continue; | |
| 2915 | |
| 2916 /* | |
| 2917 * The following check is specifically looking for an ANY | |
| 2918 * that is *not* also an INNER, because we need to save aside | |
| 2919 * all bytes in that case -- the contents parts will get | |
| 2920 * handled like all other contents, and the end-of-contents | |
| 2921 * bytes are added by the concat code, but the outer header | |
| 2922 * bytes need to get saved too, so we do them explicitly here. | |
| 2923 */ | |
| 2924 if (state->underlying_kind == SEC_ASN1_ANY | |
| 2925 && !cx->filter_only && (what == SEC_ASN1_Identifier | |
| 2926 || what == SEC_ASN1_Length)) { | |
| 2927 sec_asn1d_record_any_header (state, buf, consumed); | |
| 2928 } | |
| 2929 | |
| 2930 /* | |
| 2931 * We had some number of good, accepted bytes. If the caller | |
| 2932 * has registered to see them, pass them along. | |
| 2933 */ | |
| 2934 if (state->top->filter_proc != NULL) { | |
| 2935 int depth; | |
| 2936 | |
| 2937 depth = state->depth; | |
| 2938 if (what == SEC_ASN1_EndOfContents && !state->indefinite) { | |
| 2939 PORT_Assert (state->parent != NULL | |
| 2940 && state->parent->indefinite); | |
| 2941 depth--; | |
| 2942 PORT_Assert (depth == state->parent->depth); | |
| 2943 } | |
| 2944 (* state->top->filter_proc) (state->top->filter_arg, | |
| 2945 buf, consumed, depth, what); | |
| 2946 } | |
| 2947 | |
| 2948 state->consumed += consumed; | |
| 2949 buf += consumed; | |
| 2950 len -= consumed; | |
| 2951 } | |
| 2952 | |
| 2953 if (cx->status == decodeError) { | |
| 2954 while (state != NULL && stateEnd->parent!=state) { | |
| 2955 sec_asn1d_free_child (state, PR_TRUE); | |
| 2956 state = state->parent; | |
| 2957 } | |
| 2958 #ifdef SEC_ASN1D_FREE_ON_ERROR /* | |
| 2959 * XXX This does not work because we can | |
| 2960 * end up leaving behind dangling pointers | |
| 2961 * to stuff that was allocated. In order | |
| 2962 * to make this really work (which would | |
| 2963 * be a good thing, I think), we need to | |
| 2964 * keep track of every place/pointer that | |
| 2965 * was allocated and make sure to NULL it | |
| 2966 * out before we then free back to the mark. | |
| 2967 */ | |
| 2968 if (cx->their_pool != NULL) { | |
| 2969 PORT_Assert (cx->their_mark != NULL); | |
| 2970 PORT_ArenaRelease (cx->their_pool, cx->their_mark); | |
| 2971 cx->their_mark = NULL; | |
| 2972 } | |
| 2973 #endif | |
| 2974 return SECFailure; | |
| 2975 } | |
| 2976 | |
| 2977 #if 0 /* XXX This is what I want, but cannot have because it seems we | |
| 2978 * have situations (like when downloading a pkcs7 cert chain from | |
| 2979 * some issuers) that give us a total length which is greater than | |
| 2980 * the entire encoding. So, we have to allow allDone to have a | |
| 2981 * remaining length greater than zero. I wanted to catch internal | |
| 2982 * bugs with this, noticing when we do not have the right length. | |
| 2983 * Oh well. | |
| 2984 */ | |
| 2985 PORT_Assert (len == 0 | |
| 2986 && (cx->status == needBytes || cx->status == allDone)); | |
| 2987 #else | |
| 2988 PORT_Assert ((len == 0 && cx->status == needBytes) | |
| 2989 || cx->status == allDone); | |
| 2990 #endif | |
| 2991 return SECSuccess; | |
| 2992 } | |
| 2993 | |
| 2994 | |
| 2995 SECStatus | |
| 2996 SEC_ASN1DecoderFinish (SEC_ASN1DecoderContext *cx) | |
| 2997 { | |
| 2998 SECStatus rv; | |
| 2999 | |
| 3000 if (cx->status == needBytes) { | |
| 3001 PORT_SetError (SEC_ERROR_BAD_DER); | |
| 3002 rv = SECFailure; | |
| 3003 } else { | |
| 3004 rv = SECSuccess; | |
| 3005 } | |
| 3006 | |
| 3007 /* | |
| 3008 * XXX anything else that needs to be finished? | |
| 3009 */ | |
| 3010 | |
| 3011 PORT_FreeArena (cx->our_pool, PR_TRUE); | |
| 3012 | |
| 3013 return rv; | |
| 3014 } | |
| 3015 | |
| 3016 | |
| 3017 SEC_ASN1DecoderContext * | |
| 3018 SEC_ASN1DecoderStart (PLArenaPool *their_pool, void *dest, | |
| 3019 const SEC_ASN1Template *theTemplate) | |
| 3020 { | |
| 3021 PLArenaPool *our_pool; | |
| 3022 SEC_ASN1DecoderContext *cx; | |
| 3023 | |
| 3024 our_pool = PORT_NewArena (SEC_ASN1_DEFAULT_ARENA_SIZE); | |
| 3025 if (our_pool == NULL) | |
| 3026 return NULL; | |
| 3027 | |
| 3028 cx = (SEC_ASN1DecoderContext*)PORT_ArenaZAlloc (our_pool, sizeof(*cx)); | |
| 3029 if (cx == NULL) { | |
| 3030 PORT_FreeArena (our_pool, PR_FALSE); | |
| 3031 return NULL; | |
| 3032 } | |
| 3033 | |
| 3034 cx->our_pool = our_pool; | |
| 3035 if (their_pool != NULL) { | |
| 3036 cx->their_pool = their_pool; | |
| 3037 #ifdef SEC_ASN1D_FREE_ON_ERROR | |
| 3038 cx->their_mark = PORT_ArenaMark (their_pool); | |
| 3039 #endif | |
| 3040 } | |
| 3041 | |
| 3042 cx->status = needBytes; | |
| 3043 | |
| 3044 if (sec_asn1d_push_state(cx, theTemplate, dest, PR_FALSE) == NULL | |
| 3045 || sec_asn1d_init_state_based_on_template (cx->current) == NULL) { | |
| 3046 /* | |
| 3047 * Trouble initializing (probably due to failed allocations) | |
| 3048 * requires that we just give up. | |
| 3049 */ | |
| 3050 PORT_FreeArena (our_pool, PR_FALSE); | |
| 3051 return NULL; | |
| 3052 } | |
| 3053 | |
| 3054 return cx; | |
| 3055 } | |
| 3056 | |
| 3057 | |
| 3058 void | |
| 3059 SEC_ASN1DecoderSetFilterProc (SEC_ASN1DecoderContext *cx, | |
| 3060 SEC_ASN1WriteProc fn, void *arg, | |
| 3061 PRBool only) | |
| 3062 { | |
| 3063 /* check that we are "between" fields here */ | |
| 3064 PORT_Assert (cx->during_notify); | |
| 3065 | |
| 3066 cx->filter_proc = fn; | |
| 3067 cx->filter_arg = arg; | |
| 3068 cx->filter_only = only; | |
| 3069 } | |
| 3070 | |
| 3071 | |
| 3072 void | |
| 3073 SEC_ASN1DecoderClearFilterProc (SEC_ASN1DecoderContext *cx) | |
| 3074 { | |
| 3075 /* check that we are "between" fields here */ | |
| 3076 PORT_Assert (cx->during_notify); | |
| 3077 | |
| 3078 cx->filter_proc = NULL; | |
| 3079 cx->filter_arg = NULL; | |
| 3080 cx->filter_only = PR_FALSE; | |
| 3081 } | |
| 3082 | |
| 3083 | |
| 3084 void | |
| 3085 SEC_ASN1DecoderSetNotifyProc (SEC_ASN1DecoderContext *cx, | |
| 3086 SEC_ASN1NotifyProc fn, void *arg) | |
| 3087 { | |
| 3088 cx->notify_proc = fn; | |
| 3089 cx->notify_arg = arg; | |
| 3090 } | |
| 3091 | |
| 3092 | |
| 3093 void | |
| 3094 SEC_ASN1DecoderClearNotifyProc (SEC_ASN1DecoderContext *cx) | |
| 3095 { | |
| 3096 cx->notify_proc = NULL; | |
| 3097 cx->notify_arg = NULL; /* not necessary; just being clean */ | |
| 3098 } | |
| 3099 | |
| 3100 void | |
| 3101 SEC_ASN1DecoderAbort(SEC_ASN1DecoderContext *cx, int error) | |
| 3102 { | |
| 3103 PORT_Assert(cx); | |
| 3104 PORT_SetError(error); | |
| 3105 cx->status = decodeError; | |
| 3106 } | |
| 3107 | |
| 3108 | |
| 3109 SECStatus | |
| 3110 SEC_ASN1Decode (PLArenaPool *poolp, void *dest, | |
| 3111 const SEC_ASN1Template *theTemplate, | |
| 3112 const char *buf, long len) | |
| 3113 { | |
| 3114 SEC_ASN1DecoderContext *dcx; | |
| 3115 SECStatus urv, frv; | |
| 3116 | |
| 3117 dcx = SEC_ASN1DecoderStart (poolp, dest, theTemplate); | |
| 3118 if (dcx == NULL) | |
| 3119 return SECFailure; | |
| 3120 | |
| 3121 urv = SEC_ASN1DecoderUpdate (dcx, buf, len); | |
| 3122 frv = SEC_ASN1DecoderFinish (dcx); | |
| 3123 | |
| 3124 if (urv != SECSuccess) | |
| 3125 return urv; | |
| 3126 | |
| 3127 return frv; | |
| 3128 } | |
| 3129 | |
| 3130 | |
| 3131 SECStatus | |
| 3132 SEC_ASN1DecodeItem (PLArenaPool *poolp, void *dest, | |
| 3133 const SEC_ASN1Template *theTemplate, | |
| 3134 const SECItem *src) | |
| 3135 { | |
| 3136 return SEC_ASN1Decode (poolp, dest, theTemplate, | |
| 3137 (const char *)src->data, src->len); | |
| 3138 } | |
| 3139 | |
| 3140 #ifdef DEBUG_ASN1D_STATES | |
| 3141 void sec_asn1d_Assert(const char *s, const char *file, PRIntn ln) | |
| 3142 { | |
| 3143 printf("Assertion failed, \"%s\", file %s, line %d\n", s, file, ln); | |
| 3144 fflush(stdout); | |
| 3145 } | |
| 3146 #endif | |
| 3147 | |
| 3148 /* | |
| 3149 * Generic templates for individual/simple items and pointers to | |
| 3150 * and sets of same. | |
| 3151 * | |
| 3152 * If you need to add a new one, please note the following: | |
| 3153 * - For each new basic type you should add *four* templates: | |
| 3154 * one plain, one PointerTo, one SequenceOf and one SetOf. | |
| 3155 * - If the new type can be constructed (meaning, it is a | |
| 3156 * *string* type according to BER/DER rules), then you should | |
| 3157 * or-in SEC_ASN1_MAY_STREAM to the type in the basic template. | |
| 3158 * See the definition of the OctetString template for an example. | |
| 3159 * - It may not be obvious, but these are in *alphabetical* | |
| 3160 * order based on the SEC_ASN1_XXX name; so put new ones in | |
| 3161 * the appropriate place. | |
| 3162 */ | |
| 3163 | |
| 3164 const SEC_ASN1Template SEC_SequenceOfAnyTemplate[] = { | |
| 3165 { SEC_ASN1_SEQUENCE_OF, 0, SEC_AnyTemplate } | |
| 3166 }; | |
| 3167 | |
| 3168 #if 0 | |
| 3169 | |
| 3170 const SEC_ASN1Template SEC_PointerToBitStringTemplate[] = { | |
| 3171 { SEC_ASN1_POINTER, 0, SEC_BitStringTemplate } | |
| 3172 }; | |
| 3173 | |
| 3174 const SEC_ASN1Template SEC_SequenceOfBitStringTemplate[] = { | |
| 3175 { SEC_ASN1_SEQUENCE_OF, 0, SEC_BitStringTemplate } | |
| 3176 }; | |
| 3177 | |
| 3178 const SEC_ASN1Template SEC_SetOfBitStringTemplate[] = { | |
| 3179 { SEC_ASN1_SET_OF, 0, SEC_BitStringTemplate } | |
| 3180 }; | |
| 3181 | |
| 3182 const SEC_ASN1Template SEC_PointerToBMPStringTemplate[] = { | |
| 3183 { SEC_ASN1_POINTER, 0, SEC_BMPStringTemplate } | |
| 3184 }; | |
| 3185 | |
| 3186 const SEC_ASN1Template SEC_SequenceOfBMPStringTemplate[] = { | |
| 3187 { SEC_ASN1_SEQUENCE_OF, 0, SEC_BMPStringTemplate } | |
| 3188 }; | |
| 3189 | |
| 3190 const SEC_ASN1Template SEC_SetOfBMPStringTemplate[] = { | |
| 3191 { SEC_ASN1_SET_OF, 0, SEC_BMPStringTemplate } | |
| 3192 }; | |
| 3193 | |
| 3194 const SEC_ASN1Template SEC_PointerToBooleanTemplate[] = { | |
| 3195 { SEC_ASN1_POINTER, 0, SEC_BooleanTemplate } | |
| 3196 }; | |
| 3197 | |
| 3198 const SEC_ASN1Template SEC_SequenceOfBooleanTemplate[] = { | |
| 3199 { SEC_ASN1_SEQUENCE_OF, 0, SEC_BooleanTemplate } | |
| 3200 }; | |
| 3201 | |
| 3202 const SEC_ASN1Template SEC_SetOfBooleanTemplate[] = { | |
| 3203 { SEC_ASN1_SET_OF, 0, SEC_BooleanTemplate } | |
| 3204 }; | |
| 3205 | |
| 3206 #endif | |
| 3207 | |
| 3208 const SEC_ASN1Template SEC_EnumeratedTemplate[] = { | |
| 3209 { SEC_ASN1_ENUMERATED, 0, NULL, sizeof(SECItem) } | |
| 3210 }; | |
| 3211 | |
| 3212 const SEC_ASN1Template SEC_PointerToEnumeratedTemplate[] = { | |
| 3213 { SEC_ASN1_POINTER, 0, SEC_EnumeratedTemplate } | |
| 3214 }; | |
| 3215 | |
| 3216 #if 0 | |
| 3217 | |
| 3218 const SEC_ASN1Template SEC_SequenceOfEnumeratedTemplate[] = { | |
| 3219 { SEC_ASN1_SEQUENCE_OF, 0, SEC_EnumeratedTemplate } | |
| 3220 }; | |
| 3221 | |
| 3222 #endif | |
| 3223 | |
| 3224 const SEC_ASN1Template SEC_SetOfEnumeratedTemplate[] = { | |
| 3225 { SEC_ASN1_SET_OF, 0, SEC_EnumeratedTemplate } | |
| 3226 }; | |
| 3227 | |
| 3228 const SEC_ASN1Template SEC_PointerToGeneralizedTimeTemplate[] = { | |
| 3229 { SEC_ASN1_POINTER, 0, SEC_GeneralizedTimeTemplate } | |
| 3230 }; | |
| 3231 | |
| 3232 #if 0 | |
| 3233 | |
| 3234 const SEC_ASN1Template SEC_SequenceOfGeneralizedTimeTemplate[] = { | |
| 3235 { SEC_ASN1_SEQUENCE_OF, 0, SEC_GeneralizedTimeTemplate } | |
| 3236 }; | |
| 3237 | |
| 3238 const SEC_ASN1Template SEC_SetOfGeneralizedTimeTemplate[] = { | |
| 3239 { SEC_ASN1_SET_OF, 0, SEC_GeneralizedTimeTemplate } | |
| 3240 }; | |
| 3241 | |
| 3242 const SEC_ASN1Template SEC_PointerToIA5StringTemplate[] = { | |
| 3243 { SEC_ASN1_POINTER, 0, SEC_IA5StringTemplate } | |
| 3244 }; | |
| 3245 | |
| 3246 const SEC_ASN1Template SEC_SequenceOfIA5StringTemplate[] = { | |
| 3247 { SEC_ASN1_SEQUENCE_OF, 0, SEC_IA5StringTemplate } | |
| 3248 }; | |
| 3249 | |
| 3250 const SEC_ASN1Template SEC_SetOfIA5StringTemplate[] = { | |
| 3251 { SEC_ASN1_SET_OF, 0, SEC_IA5StringTemplate } | |
| 3252 }; | |
| 3253 | |
| 3254 const SEC_ASN1Template SEC_PointerToIntegerTemplate[] = { | |
| 3255 { SEC_ASN1_POINTER, 0, SEC_IntegerTemplate } | |
| 3256 }; | |
| 3257 | |
| 3258 const SEC_ASN1Template SEC_SequenceOfIntegerTemplate[] = { | |
| 3259 { SEC_ASN1_SEQUENCE_OF, 0, SEC_IntegerTemplate } | |
| 3260 }; | |
| 3261 | |
| 3262 const SEC_ASN1Template SEC_SetOfIntegerTemplate[] = { | |
| 3263 { SEC_ASN1_SET_OF, 0, SEC_IntegerTemplate } | |
| 3264 }; | |
| 3265 | |
| 3266 const SEC_ASN1Template SEC_PointerToNullTemplate[] = { | |
| 3267 { SEC_ASN1_POINTER, 0, SEC_NullTemplate } | |
| 3268 }; | |
| 3269 | |
| 3270 const SEC_ASN1Template SEC_SequenceOfNullTemplate[] = { | |
| 3271 { SEC_ASN1_SEQUENCE_OF, 0, SEC_NullTemplate } | |
| 3272 }; | |
| 3273 | |
| 3274 const SEC_ASN1Template SEC_SetOfNullTemplate[] = { | |
| 3275 { SEC_ASN1_SET_OF, 0, SEC_NullTemplate } | |
| 3276 }; | |
| 3277 | |
| 3278 const SEC_ASN1Template SEC_PointerToObjectIDTemplate[] = { | |
| 3279 { SEC_ASN1_POINTER, 0, SEC_ObjectIDTemplate } | |
| 3280 }; | |
| 3281 | |
| 3282 #endif | |
| 3283 | |
| 3284 const SEC_ASN1Template SEC_SequenceOfObjectIDTemplate[] = { | |
| 3285 { SEC_ASN1_SEQUENCE_OF, 0, SEC_ObjectIDTemplate } | |
| 3286 }; | |
| 3287 | |
| 3288 #if 0 | |
| 3289 | |
| 3290 const SEC_ASN1Template SEC_SetOfObjectIDTemplate[] = { | |
| 3291 { SEC_ASN1_SET_OF, 0, SEC_ObjectIDTemplate } | |
| 3292 }; | |
| 3293 | |
| 3294 const SEC_ASN1Template SEC_SequenceOfOctetStringTemplate[] = { | |
| 3295 { SEC_ASN1_SEQUENCE_OF, 0, SEC_OctetStringTemplate } | |
| 3296 }; | |
| 3297 | |
| 3298 const SEC_ASN1Template SEC_SetOfOctetStringTemplate[] = { | |
| 3299 { SEC_ASN1_SET_OF, 0, SEC_OctetStringTemplate } | |
| 3300 }; | |
| 3301 | |
| 3302 #endif | |
| 3303 | |
| 3304 const SEC_ASN1Template SEC_PrintableStringTemplate[] = { | |
| 3305 { SEC_ASN1_PRINTABLE_STRING | SEC_ASN1_MAY_STREAM, 0, NULL, sizeof(SECItem)} | |
| 3306 }; | |
| 3307 | |
| 3308 #if 0 | |
| 3309 | |
| 3310 const SEC_ASN1Template SEC_PointerToPrintableStringTemplate[] = { | |
| 3311 { SEC_ASN1_POINTER, 0, SEC_PrintableStringTemplate } | |
| 3312 }; | |
| 3313 | |
| 3314 const SEC_ASN1Template SEC_SequenceOfPrintableStringTemplate[] = { | |
| 3315 { SEC_ASN1_SEQUENCE_OF, 0, SEC_PrintableStringTemplate } | |
| 3316 }; | |
| 3317 | |
| 3318 const SEC_ASN1Template SEC_SetOfPrintableStringTemplate[] = { | |
| 3319 { SEC_ASN1_SET_OF, 0, SEC_PrintableStringTemplate } | |
| 3320 }; | |
| 3321 | |
| 3322 #endif | |
| 3323 | |
| 3324 const SEC_ASN1Template SEC_T61StringTemplate[] = { | |
| 3325 { SEC_ASN1_T61_STRING | SEC_ASN1_MAY_STREAM, 0, NULL, sizeof(SECItem) } | |
| 3326 }; | |
| 3327 | |
| 3328 #if 0 | |
| 3329 | |
| 3330 const SEC_ASN1Template SEC_PointerToT61StringTemplate[] = { | |
| 3331 { SEC_ASN1_POINTER, 0, SEC_T61StringTemplate } | |
| 3332 }; | |
| 3333 | |
| 3334 const SEC_ASN1Template SEC_SequenceOfT61StringTemplate[] = { | |
| 3335 { SEC_ASN1_SEQUENCE_OF, 0, SEC_T61StringTemplate } | |
| 3336 }; | |
| 3337 | |
| 3338 const SEC_ASN1Template SEC_SetOfT61StringTemplate[] = { | |
| 3339 { SEC_ASN1_SET_OF, 0, SEC_T61StringTemplate } | |
| 3340 }; | |
| 3341 | |
| 3342 #endif | |
| 3343 | |
| 3344 const SEC_ASN1Template SEC_UniversalStringTemplate[] = { | |
| 3345 { SEC_ASN1_UNIVERSAL_STRING | SEC_ASN1_MAY_STREAM, 0, NULL, sizeof(SECItem)} | |
| 3346 }; | |
| 3347 | |
| 3348 #if 0 | |
| 3349 | |
| 3350 const SEC_ASN1Template SEC_PointerToUniversalStringTemplate[] = { | |
| 3351 { SEC_ASN1_POINTER, 0, SEC_UniversalStringTemplate } | |
| 3352 }; | |
| 3353 | |
| 3354 const SEC_ASN1Template SEC_SequenceOfUniversalStringTemplate[] = { | |
| 3355 { SEC_ASN1_SEQUENCE_OF, 0, SEC_UniversalStringTemplate } | |
| 3356 }; | |
| 3357 | |
| 3358 const SEC_ASN1Template SEC_SetOfUniversalStringTemplate[] = { | |
| 3359 { SEC_ASN1_SET_OF, 0, SEC_UniversalStringTemplate } | |
| 3360 }; | |
| 3361 | |
| 3362 const SEC_ASN1Template SEC_PointerToUTCTimeTemplate[] = { | |
| 3363 { SEC_ASN1_POINTER, 0, SEC_UTCTimeTemplate } | |
| 3364 }; | |
| 3365 | |
| 3366 const SEC_ASN1Template SEC_SequenceOfUTCTimeTemplate[] = { | |
| 3367 { SEC_ASN1_SEQUENCE_OF, 0, SEC_UTCTimeTemplate } | |
| 3368 }; | |
| 3369 | |
| 3370 const SEC_ASN1Template SEC_SetOfUTCTimeTemplate[] = { | |
| 3371 { SEC_ASN1_SET_OF, 0, SEC_UTCTimeTemplate } | |
| 3372 }; | |
| 3373 | |
| 3374 const SEC_ASN1Template SEC_PointerToUTF8StringTemplate[] = { | |
| 3375 { SEC_ASN1_POINTER, 0, SEC_UTF8StringTemplate } | |
| 3376 }; | |
| 3377 | |
| 3378 const SEC_ASN1Template SEC_SequenceOfUTF8StringTemplate[] = { | |
| 3379 { SEC_ASN1_SEQUENCE_OF, 0, SEC_UTF8StringTemplate } | |
| 3380 }; | |
| 3381 | |
| 3382 const SEC_ASN1Template SEC_SetOfUTF8StringTemplate[] = { | |
| 3383 { SEC_ASN1_SET_OF, 0, SEC_UTF8StringTemplate } | |
| 3384 }; | |
| 3385 | |
| 3386 #endif | |
| 3387 | |
| 3388 const SEC_ASN1Template SEC_VisibleStringTemplate[] = { | |
| 3389 { SEC_ASN1_VISIBLE_STRING | SEC_ASN1_MAY_STREAM, 0, NULL, sizeof(SECItem) } | |
| 3390 }; | |
| 3391 | |
| 3392 #if 0 | |
| 3393 | |
| 3394 const SEC_ASN1Template SEC_PointerToVisibleStringTemplate[] = { | |
| 3395 { SEC_ASN1_POINTER, 0, SEC_VisibleStringTemplate } | |
| 3396 }; | |
| 3397 | |
| 3398 const SEC_ASN1Template SEC_SequenceOfVisibleStringTemplate[] = { | |
| 3399 { SEC_ASN1_SEQUENCE_OF, 0, SEC_VisibleStringTemplate } | |
| 3400 }; | |
| 3401 | |
| 3402 const SEC_ASN1Template SEC_SetOfVisibleStringTemplate[] = { | |
| 3403 { SEC_ASN1_SET_OF, 0, SEC_VisibleStringTemplate } | |
| 3404 }; | |
| 3405 | |
| 3406 #endif | |
| 3407 | |
| 3408 /* | |
| 3409 * Template for skipping a subitem. | |
| 3410 * | |
| 3411 * Note that it only makes sense to use this for decoding (when you want | |
| 3412 * to decode something where you are only interested in one or two of | |
| 3413 * the fields); you cannot encode a SKIP! | |
| 3414 */ | |
| 3415 const SEC_ASN1Template SEC_SkipTemplate[] = { | |
| 3416 { SEC_ASN1_SKIP } | |
| 3417 }; | |
| 3418 | |
| 3419 | |
| 3420 /* These functions simply return the address of the above-declared templates. | |
| 3421 ** This is necessary for Windows DLLs. Sigh. | |
| 3422 */ | |
| 3423 SEC_ASN1_CHOOSER_IMPLEMENT(SEC_EnumeratedTemplate) | |
| 3424 SEC_ASN1_CHOOSER_IMPLEMENT(SEC_PointerToEnumeratedTemplate) | |
| 3425 SEC_ASN1_CHOOSER_IMPLEMENT(SEC_SequenceOfAnyTemplate) | |
| 3426 SEC_ASN1_CHOOSER_IMPLEMENT(SEC_SequenceOfObjectIDTemplate) | |
| 3427 SEC_ASN1_CHOOSER_IMPLEMENT(SEC_SkipTemplate) | |
| 3428 SEC_ASN1_CHOOSER_IMPLEMENT(SEC_UniversalStringTemplate) | |
| 3429 SEC_ASN1_CHOOSER_IMPLEMENT(SEC_PrintableStringTemplate) | |
| 3430 SEC_ASN1_CHOOSER_IMPLEMENT(SEC_T61StringTemplate) | |
| 3431 SEC_ASN1_CHOOSER_IMPLEMENT(SEC_PointerToGeneralizedTimeTemplate) | |
| 3432 | |
| OLD | NEW |