security/nss/lib/util/quickder.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 /*
michael@0 6 Optimized ASN.1 DER decoder
michael@0 7
michael@0 8 */
michael@0 9
michael@0 10 #include "secerr.h"
michael@0 11 #include "secasn1.h" /* for SEC_ASN1GetSubtemplate */
michael@0 12 #include "secitem.h"
michael@0 13
michael@0 14 /*
michael@0 15 * simple definite-length ASN.1 decoder
michael@0 16 */
michael@0 17
michael@0 18 static unsigned char* definite_length_decoder(const unsigned char *buf,
michael@0 19 const unsigned int length,
michael@0 20 unsigned int *data_length,
michael@0 21 PRBool includeTag)
michael@0 22 {
michael@0 23 unsigned char tag;
michael@0 24 unsigned int used_length= 0;
michael@0 25 unsigned int data_len;
michael@0 26
michael@0 27 if (used_length >= length)
michael@0 28 {
michael@0 29 return NULL;
michael@0 30 }
michael@0 31 tag = buf[used_length++];
michael@0 32
michael@0 33 /* blow out when we come to the end */
michael@0 34 if (tag == 0)
michael@0 35 {
michael@0 36 return NULL;
michael@0 37 }
michael@0 38
michael@0 39 if (used_length >= length)
michael@0 40 {
michael@0 41 return NULL;
michael@0 42 }
michael@0 43 data_len = buf[used_length++];
michael@0 44
michael@0 45 if (data_len&0x80)
michael@0 46 {
michael@0 47 int len_count = data_len & 0x7f;
michael@0 48
michael@0 49 data_len = 0;
michael@0 50
michael@0 51 while (len_count-- > 0)
michael@0 52 {
michael@0 53 if (used_length >= length)
michael@0 54 {
michael@0 55 return NULL;
michael@0 56 }
michael@0 57 data_len = (data_len << 8) | buf[used_length++];
michael@0 58 }
michael@0 59 }
michael@0 60
michael@0 61 if (data_len > (length-used_length) )
michael@0 62 {
michael@0 63 return NULL;
michael@0 64 }
michael@0 65 if (includeTag) data_len += used_length;
michael@0 66
michael@0 67 *data_length = data_len;
michael@0 68 return ((unsigned char*)buf + (includeTag ? 0 : used_length));
michael@0 69 }
michael@0 70
michael@0 71 static SECStatus GetItem(SECItem* src, SECItem* dest, PRBool includeTag)
michael@0 72 {
michael@0 73 if ( (!src) || (!dest) || (!src->data && src->len) )
michael@0 74 {
michael@0 75 PORT_SetError(SEC_ERROR_INVALID_ARGS);
michael@0 76 return SECFailure;
michael@0 77 }
michael@0 78
michael@0 79 if (!src->len)
michael@0 80 {
michael@0 81 /* reaching the end of the buffer is not an error */
michael@0 82 dest->data = NULL;
michael@0 83 dest->len = 0;
michael@0 84 return SECSuccess;
michael@0 85 }
michael@0 86
michael@0 87 dest->data = definite_length_decoder(src->data, src->len, &dest->len,
michael@0 88 includeTag);
michael@0 89 if (dest->data == NULL)
michael@0 90 {
michael@0 91 PORT_SetError(SEC_ERROR_BAD_DER);
michael@0 92 return SECFailure;
michael@0 93 }
michael@0 94 src->len -= (dest->data - src->data) + dest->len;
michael@0 95 src->data = dest->data + dest->len;
michael@0 96 return SECSuccess;
michael@0 97 }
michael@0 98
michael@0 99 /* check if the actual component's type matches the type in the template */
michael@0 100
michael@0 101 static SECStatus MatchComponentType(const SEC_ASN1Template* templateEntry,
michael@0 102 SECItem* item, PRBool* match, void* dest)
michael@0 103 {
michael@0 104 unsigned long kind = 0;
michael@0 105 unsigned char tag = 0;
michael@0 106
michael@0 107 if ( (!item) || (!item->data && item->len) || (!templateEntry) || (!match) )
michael@0 108 {
michael@0 109 PORT_SetError(SEC_ERROR_INVALID_ARGS);
michael@0 110 return SECFailure;
michael@0 111 }
michael@0 112
michael@0 113 if (!item->len)
michael@0 114 {
michael@0 115 *match = PR_FALSE;
michael@0 116 return SECSuccess;
michael@0 117 }
michael@0 118
michael@0 119 kind = templateEntry->kind;
michael@0 120 tag = *(unsigned char*) item->data;
michael@0 121
michael@0 122 if ( ( (kind & SEC_ASN1_INLINE) ||
michael@0 123 (kind & SEC_ASN1_POINTER) ) &&
michael@0 124 (0 == (kind & SEC_ASN1_TAG_MASK) ) )
michael@0 125 {
michael@0 126 /* These cases are special because the template's "kind" does not
michael@0 127 give us the information for the ASN.1 tag of the next item. It can
michael@0 128 only be figured out from the subtemplate. */
michael@0 129 if (!(kind & SEC_ASN1_OPTIONAL))
michael@0 130 {
michael@0 131 /* This is a required component. If there is a type mismatch,
michael@0 132 the decoding of the subtemplate will fail, so assume this
michael@0 133 is a match at the parent level and let it fail later. This
michael@0 134 avoids a redundant check in matching cases */
michael@0 135 *match = PR_TRUE;
michael@0 136 return SECSuccess;
michael@0 137 }
michael@0 138 else
michael@0 139 {
michael@0 140 /* optional component. This is the hard case. Now we need to
michael@0 141 look at the subtemplate to get the expected kind */
michael@0 142 const SEC_ASN1Template* subTemplate =
michael@0 143 SEC_ASN1GetSubtemplate (templateEntry, dest, PR_FALSE);
michael@0 144 if (!subTemplate)
michael@0 145 {
michael@0 146 PORT_SetError(SEC_ERROR_BAD_TEMPLATE);
michael@0 147 return SECFailure;
michael@0 148 }
michael@0 149 if ( (subTemplate->kind & SEC_ASN1_INLINE) ||
michael@0 150 (subTemplate->kind & SEC_ASN1_POINTER) )
michael@0 151 {
michael@0 152 /* disallow nesting SEC_ASN1_POINTER and SEC_ASN1_INLINE,
michael@0 153 otherwise you may get a false positive due to the recursion
michael@0 154 optimization above that always matches the type if the
michael@0 155 component is required . Nesting these should never be
michael@0 156 required, so that no one should miss this ability */
michael@0 157 PORT_SetError(SEC_ERROR_BAD_TEMPLATE);
michael@0 158 return SECFailure;
michael@0 159 }
michael@0 160 return MatchComponentType(subTemplate, item, match,
michael@0 161 (void*)((char*)dest + templateEntry->offset));
michael@0 162 }
michael@0 163 }
michael@0 164
michael@0 165 if (kind & SEC_ASN1_CHOICE)
michael@0 166 {
michael@0 167 /* we need to check the component's tag against each choice's tag */
michael@0 168 /* XXX it would be nice to save the index of the choice here so that
michael@0 169 DecodeChoice wouldn't have to do this again. However, due to the
michael@0 170 recursivity of MatchComponentType, we don't know if we are in a
michael@0 171 required or optional component, so we can't write anywhere in
michael@0 172 the destination within this function */
michael@0 173 unsigned choiceIndex = 1;
michael@0 174 const SEC_ASN1Template* choiceEntry;
michael@0 175 while ( (choiceEntry = &templateEntry[choiceIndex++]) && (choiceEntry->kind))
michael@0 176 {
michael@0 177 if ( (SECSuccess == MatchComponentType(choiceEntry, item, match,
michael@0 178 (void*)((char*)dest + choiceEntry->offset))) &&
michael@0 179 (PR_TRUE == *match) )
michael@0 180 {
michael@0 181 return SECSuccess;
michael@0 182 }
michael@0 183 }
michael@0 184 /* no match, caller must decide if this is BAD DER, or not. */
michael@0 185 *match = PR_FALSE;
michael@0 186 return SECSuccess;
michael@0 187 }
michael@0 188
michael@0 189 if (kind & SEC_ASN1_ANY)
michael@0 190 {
michael@0 191 /* SEC_ASN1_ANY always matches */
michael@0 192 *match = PR_TRUE;
michael@0 193 return SECSuccess;
michael@0 194 }
michael@0 195
michael@0 196 if ( (0 == ((unsigned char)kind & SEC_ASN1_TAGNUM_MASK)) &&
michael@0 197 (!(kind & SEC_ASN1_EXPLICIT)) &&
michael@0 198 ( ( (kind & SEC_ASN1_SAVE) ||
michael@0 199 (kind & SEC_ASN1_SKIP) ) &&
michael@0 200 (!(kind & SEC_ASN1_OPTIONAL))
michael@0 201 )
michael@0 202 )
michael@0 203 {
michael@0 204 /* when saving or skipping a required component, a type is not
michael@0 205 required in the template. This is for legacy support of
michael@0 206 SEC_ASN1_SAVE and SEC_ASN1_SKIP only. XXX I would like to
michael@0 207 deprecate these usages and always require a type, as this
michael@0 208 disables type checking, and effectively forbids us from
michael@0 209 transparently ignoring optional components we aren't aware of */
michael@0 210 *match = PR_TRUE;
michael@0 211 return SECSuccess;
michael@0 212 }
michael@0 213
michael@0 214 /* first, do a class check */
michael@0 215 if ( (tag & SEC_ASN1_CLASS_MASK) !=
michael@0 216 (((unsigned char)kind) & SEC_ASN1_CLASS_MASK) )
michael@0 217 {
michael@0 218 #ifdef DEBUG
michael@0 219 /* this is only to help debugging of the decoder in case of problems */
michael@0 220 unsigned char tagclass = tag & SEC_ASN1_CLASS_MASK;
michael@0 221 unsigned char expectedclass = (unsigned char)kind & SEC_ASN1_CLASS_MASK;
michael@0 222 tagclass = tagclass;
michael@0 223 expectedclass = expectedclass;
michael@0 224 #endif
michael@0 225 *match = PR_FALSE;
michael@0 226 return SECSuccess;
michael@0 227 }
michael@0 228
michael@0 229 /* now do a tag check */
michael@0 230 if ( ((unsigned char)kind & SEC_ASN1_TAGNUM_MASK) !=
michael@0 231 (tag & SEC_ASN1_TAGNUM_MASK))
michael@0 232 {
michael@0 233 *match = PR_FALSE;
michael@0 234 return SECSuccess;
michael@0 235 }
michael@0 236
michael@0 237 /* now, do a method check. This depends on the class */
michael@0 238 switch (tag & SEC_ASN1_CLASS_MASK)
michael@0 239 {
michael@0 240 case SEC_ASN1_UNIVERSAL:
michael@0 241 /* For types of the SEC_ASN1_UNIVERSAL class, we know which must be
michael@0 242 primitive or constructed based on the tag */
michael@0 243 switch (tag & SEC_ASN1_TAGNUM_MASK)
michael@0 244 {
michael@0 245 case SEC_ASN1_SEQUENCE:
michael@0 246 case SEC_ASN1_SET:
michael@0 247 case SEC_ASN1_EMBEDDED_PDV:
michael@0 248 /* this component must be a constructed type */
michael@0 249 /* XXX add any new universal constructed type here */
michael@0 250 if (tag & SEC_ASN1_CONSTRUCTED)
michael@0 251 {
michael@0 252 *match = PR_TRUE;
michael@0 253 return SECSuccess;
michael@0 254 }
michael@0 255 break;
michael@0 256
michael@0 257 default:
michael@0 258 /* this component must be a primitive type */
michael@0 259 if (! (tag & SEC_ASN1_CONSTRUCTED))
michael@0 260 {
michael@0 261 *match = PR_TRUE;
michael@0 262 return SECSuccess;
michael@0 263 }
michael@0 264 break;
michael@0 265 }
michael@0 266 break;
michael@0 267
michael@0 268 default:
michael@0 269 /* for all other classes, we check the method based on the template */
michael@0 270 if ( (unsigned char)(kind & SEC_ASN1_METHOD_MASK) ==
michael@0 271 (tag & SEC_ASN1_METHOD_MASK) )
michael@0 272 {
michael@0 273 *match = PR_TRUE;
michael@0 274 return SECSuccess;
michael@0 275 }
michael@0 276 /* method does not match between template and component */
michael@0 277 break;
michael@0 278 }
michael@0 279
michael@0 280 *match = PR_FALSE;
michael@0 281 return SECSuccess;
michael@0 282 }
michael@0 283
michael@0 284 #ifdef DEBUG
michael@0 285
michael@0 286 static SECStatus CheckSequenceTemplate(const SEC_ASN1Template* sequenceTemplate)
michael@0 287 {
michael@0 288 SECStatus rv = SECSuccess;
michael@0 289 const SEC_ASN1Template* sequenceEntry = NULL;
michael@0 290 unsigned long seqIndex = 0;
michael@0 291 unsigned long lastEntryIndex = 0;
michael@0 292 unsigned long ambiguityIndex = 0;
michael@0 293 PRBool foundAmbiguity = PR_FALSE;
michael@0 294
michael@0 295 do
michael@0 296 {
michael@0 297 sequenceEntry = &sequenceTemplate[seqIndex++];
michael@0 298 if (sequenceEntry->kind)
michael@0 299 {
michael@0 300 /* ensure that we don't have an optional component of SEC_ASN1_ANY
michael@0 301 in the middle of the sequence, since we could not handle it */
michael@0 302 /* XXX this function needs to dig into the subtemplates to find
michael@0 303 the next tag */
michael@0 304 if ( (PR_FALSE == foundAmbiguity) &&
michael@0 305 (sequenceEntry->kind & SEC_ASN1_OPTIONAL) &&
michael@0 306 (sequenceEntry->kind & SEC_ASN1_ANY) )
michael@0 307 {
michael@0 308 foundAmbiguity = PR_TRUE;
michael@0 309 ambiguityIndex = seqIndex - 1;
michael@0 310 }
michael@0 311 }
michael@0 312 } while (sequenceEntry->kind);
michael@0 313
michael@0 314 lastEntryIndex = seqIndex - 2;
michael@0 315
michael@0 316 if (PR_FALSE != foundAmbiguity)
michael@0 317 {
michael@0 318 if (ambiguityIndex < lastEntryIndex)
michael@0 319 {
michael@0 320 /* ambiguity can only be tolerated on the last entry */
michael@0 321 PORT_SetError(SEC_ERROR_BAD_TEMPLATE);
michael@0 322 rv = SECFailure;
michael@0 323 }
michael@0 324 }
michael@0 325
michael@0 326 /* XXX also enforce ASN.1 requirement that tags be
michael@0 327 distinct for consecutive optional components */
michael@0 328
michael@0 329 return rv;
michael@0 330 }
michael@0 331
michael@0 332 #endif
michael@0 333
michael@0 334 static SECStatus DecodeItem(void* dest,
michael@0 335 const SEC_ASN1Template* templateEntry,
michael@0 336 SECItem* src, PLArenaPool* arena, PRBool checkTag);
michael@0 337
michael@0 338 static SECStatus DecodeSequence(void* dest,
michael@0 339 const SEC_ASN1Template* templateEntry,
michael@0 340 SECItem* src, PLArenaPool* arena)
michael@0 341 {
michael@0 342 SECStatus rv = SECSuccess;
michael@0 343 SECItem source;
michael@0 344 SECItem sequence;
michael@0 345 const SEC_ASN1Template* sequenceTemplate = &(templateEntry[1]);
michael@0 346 const SEC_ASN1Template* sequenceEntry = NULL;
michael@0 347 unsigned long seqindex = 0;
michael@0 348
michael@0 349 #ifdef DEBUG
michael@0 350 /* for a sequence, we need to validate the template. */
michael@0 351 rv = CheckSequenceTemplate(sequenceTemplate);
michael@0 352 #endif
michael@0 353
michael@0 354 source = *src;
michael@0 355
michael@0 356 /* get the sequence */
michael@0 357 if (SECSuccess == rv)
michael@0 358 {
michael@0 359 rv = GetItem(&source, &sequence, PR_FALSE);
michael@0 360 }
michael@0 361
michael@0 362 /* process it */
michael@0 363 if (SECSuccess == rv)
michael@0 364 do
michael@0 365 {
michael@0 366 sequenceEntry = &sequenceTemplate[seqindex++];
michael@0 367 if ( (sequenceEntry && sequenceEntry->kind) &&
michael@0 368 (sequenceEntry->kind != SEC_ASN1_SKIP_REST) )
michael@0 369 {
michael@0 370 rv = DecodeItem(dest, sequenceEntry, &sequence, arena, PR_TRUE);
michael@0 371 }
michael@0 372 } while ( (SECSuccess == rv) &&
michael@0 373 (sequenceEntry->kind &&
michael@0 374 sequenceEntry->kind != SEC_ASN1_SKIP_REST) );
michael@0 375 /* we should have consumed all the bytes in the sequence by now
michael@0 376 unless the caller doesn't care about the rest of the sequence */
michael@0 377 if (SECSuccess == rv && sequence.len &&
michael@0 378 sequenceEntry && sequenceEntry->kind != SEC_ASN1_SKIP_REST)
michael@0 379 {
michael@0 380 /* it isn't 100% clear whether this is a bad DER or a bad template.
michael@0 381 The problem is that logically, they don't match - there is extra
michael@0 382 data in the DER that the template doesn't know about */
michael@0 383 PORT_SetError(SEC_ERROR_BAD_DER);
michael@0 384 rv = SECFailure;
michael@0 385 }
michael@0 386
michael@0 387 return rv;
michael@0 388 }
michael@0 389
michael@0 390 static SECStatus DecodeInline(void* dest,
michael@0 391 const SEC_ASN1Template* templateEntry,
michael@0 392 SECItem* src, PLArenaPool* arena, PRBool checkTag)
michael@0 393 {
michael@0 394 const SEC_ASN1Template* inlineTemplate =
michael@0 395 SEC_ASN1GetSubtemplate (templateEntry, dest, PR_FALSE);
michael@0 396 return DecodeItem((void*)((char*)dest + templateEntry->offset),
michael@0 397 inlineTemplate, src, arena, checkTag);
michael@0 398 }
michael@0 399
michael@0 400 static SECStatus DecodePointer(void* dest,
michael@0 401 const SEC_ASN1Template* templateEntry,
michael@0 402 SECItem* src, PLArenaPool* arena, PRBool checkTag)
michael@0 403 {
michael@0 404 const SEC_ASN1Template* ptrTemplate =
michael@0 405 SEC_ASN1GetSubtemplate (templateEntry, dest, PR_FALSE);
michael@0 406 void* subdata = PORT_ArenaZAlloc(arena, ptrTemplate->size);
michael@0 407 *(void**)((char*)dest + templateEntry->offset) = subdata;
michael@0 408 if (subdata)
michael@0 409 {
michael@0 410 return DecodeItem(subdata, ptrTemplate, src, arena, checkTag);
michael@0 411 }
michael@0 412 else
michael@0 413 {
michael@0 414 PORT_SetError(SEC_ERROR_NO_MEMORY);
michael@0 415 return SECFailure;
michael@0 416 }
michael@0 417 }
michael@0 418
michael@0 419 static SECStatus DecodeImplicit(void* dest,
michael@0 420 const SEC_ASN1Template* templateEntry,
michael@0 421 SECItem* src, PLArenaPool* arena)
michael@0 422 {
michael@0 423 if (templateEntry->kind & SEC_ASN1_POINTER)
michael@0 424 {
michael@0 425 return DecodePointer((void*)((char*)dest ),
michael@0 426 templateEntry, src, arena, PR_FALSE);
michael@0 427 }
michael@0 428 else
michael@0 429 {
michael@0 430 return DecodeInline((void*)((char*)dest ),
michael@0 431 templateEntry, src, arena, PR_FALSE);
michael@0 432 }
michael@0 433 }
michael@0 434
michael@0 435 static SECStatus DecodeChoice(void* dest,
michael@0 436 const SEC_ASN1Template* templateEntry,
michael@0 437 SECItem* src, PLArenaPool* arena)
michael@0 438 {
michael@0 439 SECStatus rv = SECSuccess;
michael@0 440 SECItem choice;
michael@0 441 const SEC_ASN1Template* choiceTemplate = &(templateEntry[1]);
michael@0 442 const SEC_ASN1Template* choiceEntry = NULL;
michael@0 443 unsigned long choiceindex = 0;
michael@0 444
michael@0 445 /* XXX for a choice component, we should validate the template to make
michael@0 446 sure the tags are distinct, in debug builds. This hasn't been
michael@0 447 implemented yet */
michael@0 448 /* rv = CheckChoiceTemplate(sequenceTemplate); */
michael@0 449
michael@0 450 /* process it */
michael@0 451 do
michael@0 452 {
michael@0 453 choice = *src;
michael@0 454 choiceEntry = &choiceTemplate[choiceindex++];
michael@0 455 if (choiceEntry->kind)
michael@0 456 {
michael@0 457 rv = DecodeItem(dest, choiceEntry, &choice, arena, PR_TRUE);
michael@0 458 }
michael@0 459 } while ( (SECFailure == rv) && (choiceEntry->kind));
michael@0 460
michael@0 461 if (SECFailure == rv)
michael@0 462 {
michael@0 463 /* the component didn't match any of the choices */
michael@0 464 PORT_SetError(SEC_ERROR_BAD_DER);
michael@0 465 }
michael@0 466 else
michael@0 467 {
michael@0 468 /* set the type in the union here */
michael@0 469 int *which = (int *)((char *)dest + templateEntry->offset);
michael@0 470 *which = (int)choiceEntry->size;
michael@0 471 }
michael@0 472
michael@0 473 /* we should have consumed all the bytes by now */
michael@0 474 /* fail if we have not */
michael@0 475 if (SECSuccess == rv && choice.len)
michael@0 476 {
michael@0 477 /* there is extra data that isn't listed in the template */
michael@0 478 PORT_SetError(SEC_ERROR_BAD_DER);
michael@0 479 rv = SECFailure;
michael@0 480 }
michael@0 481 return rv;
michael@0 482 }
michael@0 483
michael@0 484 static SECStatus DecodeGroup(void* dest,
michael@0 485 const SEC_ASN1Template* templateEntry,
michael@0 486 SECItem* src, PLArenaPool* arena)
michael@0 487 {
michael@0 488 SECStatus rv = SECSuccess;
michael@0 489 SECItem source;
michael@0 490 SECItem group;
michael@0 491 PRUint32 totalEntries = 0;
michael@0 492 PRUint32 entryIndex = 0;
michael@0 493 void** entries = NULL;
michael@0 494
michael@0 495 const SEC_ASN1Template* subTemplate =
michael@0 496 SEC_ASN1GetSubtemplate (templateEntry, dest, PR_FALSE);
michael@0 497
michael@0 498 source = *src;
michael@0 499
michael@0 500 /* get the group */
michael@0 501 if (SECSuccess == rv)
michael@0 502 {
michael@0 503 rv = GetItem(&source, &group, PR_FALSE);
michael@0 504 }
michael@0 505
michael@0 506 /* XXX we should check the subtemplate in debug builds */
michael@0 507 if (SECSuccess == rv)
michael@0 508 {
michael@0 509 /* first, count the number of entries. Benchmarking showed that this
michael@0 510 counting pass is more efficient than trying to allocate entries as
michael@0 511 we read the DER, even if allocating many entries at a time
michael@0 512 */
michael@0 513 SECItem counter = group;
michael@0 514 do
michael@0 515 {
michael@0 516 SECItem anitem;
michael@0 517 rv = GetItem(&counter, &anitem, PR_TRUE);
michael@0 518 if (SECSuccess == rv && (anitem.len) )
michael@0 519 {
michael@0 520 totalEntries++;
michael@0 521 }
michael@0 522 } while ( (SECSuccess == rv) && (counter.len) );
michael@0 523
michael@0 524 if (SECSuccess == rv)
michael@0 525 {
michael@0 526 /* allocate room for pointer array and entries */
michael@0 527 /* we want to allocate the array even if there is 0 entry */
michael@0 528 entries = (void**)PORT_ArenaZAlloc(arena, sizeof(void*)*
michael@0 529 (totalEntries + 1 ) + /* the extra one is for NULL termination */
michael@0 530 subTemplate->size*totalEntries);
michael@0 531
michael@0 532 if (entries)
michael@0 533 {
michael@0 534 entries[totalEntries] = NULL; /* terminate the array */
michael@0 535 }
michael@0 536 else
michael@0 537 {
michael@0 538 PORT_SetError(SEC_ERROR_NO_MEMORY);
michael@0 539 rv = SECFailure;
michael@0 540 }
michael@0 541 if (SECSuccess == rv)
michael@0 542 {
michael@0 543 void* entriesData = (unsigned char*)entries + (unsigned long)(sizeof(void*)*(totalEntries + 1 ));
michael@0 544 /* and fix the pointers in the array */
michael@0 545 PRUint32 entriesIndex = 0;
michael@0 546 for (entriesIndex = 0;entriesIndex<totalEntries;entriesIndex++)
michael@0 547 {
michael@0 548 entries[entriesIndex] =
michael@0 549 (char*)entriesData + (subTemplate->size*entriesIndex);
michael@0 550 }
michael@0 551 }
michael@0 552 }
michael@0 553 }
michael@0 554
michael@0 555 if (SECSuccess == rv && totalEntries)
michael@0 556 do
michael@0 557 {
michael@0 558 if (!(entryIndex<totalEntries))
michael@0 559 {
michael@0 560 rv = SECFailure;
michael@0 561 break;
michael@0 562 }
michael@0 563 rv = DecodeItem(entries[entryIndex++], subTemplate, &group, arena, PR_TRUE);
michael@0 564 } while ( (SECSuccess == rv) && (group.len) );
michael@0 565 /* we should be at the end of the set by now */
michael@0 566 /* save the entries where requested */
michael@0 567 memcpy(((char*)dest + templateEntry->offset), &entries, sizeof(void**));
michael@0 568
michael@0 569 return rv;
michael@0 570 }
michael@0 571
michael@0 572 static SECStatus DecodeExplicit(void* dest,
michael@0 573 const SEC_ASN1Template* templateEntry,
michael@0 574 SECItem* src, PLArenaPool* arena)
michael@0 575 {
michael@0 576 SECStatus rv = SECSuccess;
michael@0 577 SECItem subItem;
michael@0 578 SECItem constructed = *src;
michael@0 579
michael@0 580 rv = GetItem(&constructed, &subItem, PR_FALSE);
michael@0 581
michael@0 582 if (SECSuccess == rv)
michael@0 583 {
michael@0 584 if (templateEntry->kind & SEC_ASN1_POINTER)
michael@0 585 {
michael@0 586 rv = DecodePointer(dest, templateEntry, &subItem, arena, PR_TRUE);
michael@0 587 }
michael@0 588 else
michael@0 589 {
michael@0 590 rv = DecodeInline(dest, templateEntry, &subItem, arena, PR_TRUE);
michael@0 591 }
michael@0 592 }
michael@0 593
michael@0 594 return rv;
michael@0 595 }
michael@0 596
michael@0 597 /* new decoder implementation. This is a recursive function */
michael@0 598
michael@0 599 static SECStatus DecodeItem(void* dest,
michael@0 600 const SEC_ASN1Template* templateEntry,
michael@0 601 SECItem* src, PLArenaPool* arena, PRBool checkTag)
michael@0 602 {
michael@0 603 SECStatus rv = SECSuccess;
michael@0 604 SECItem temp;
michael@0 605 SECItem mark;
michael@0 606 PRBool pop = PR_FALSE;
michael@0 607 PRBool decode = PR_TRUE;
michael@0 608 PRBool save = PR_FALSE;
michael@0 609 unsigned long kind;
michael@0 610 PRBool match = PR_TRUE;
michael@0 611 PRBool optional = PR_FALSE;
michael@0 612
michael@0 613 PR_ASSERT(src && dest && templateEntry && arena);
michael@0 614 #if 0
michael@0 615 if (!src || !dest || !templateEntry || !arena)
michael@0 616 {
michael@0 617 PORT_SetError(SEC_ERROR_INVALID_ARGS);
michael@0 618 rv = SECFailure;
michael@0 619 }
michael@0 620 #endif
michael@0 621
michael@0 622 if (SECSuccess == rv)
michael@0 623 {
michael@0 624 /* do the template validation */
michael@0 625 kind = templateEntry->kind;
michael@0 626 optional = (0 != (kind & SEC_ASN1_OPTIONAL));
michael@0 627 if (!kind)
michael@0 628 {
michael@0 629 PORT_SetError(SEC_ERROR_BAD_TEMPLATE);
michael@0 630 rv = SECFailure;
michael@0 631 }
michael@0 632 }
michael@0 633
michael@0 634 if (SECSuccess == rv)
michael@0 635 {
michael@0 636 #ifdef DEBUG
michael@0 637 if (kind & SEC_ASN1_DEBUG_BREAK)
michael@0 638 {
michael@0 639 /* when debugging the decoder or a template that fails to
michael@0 640 decode, put SEC_ASN1_DEBUG in the component that gives you
michael@0 641 trouble. The decoder will then get to this block and assert.
michael@0 642 If you want to debug the rest of the code, you can set a
michael@0 643 breakpoint and set dontassert to PR_TRUE, which will let
michael@0 644 you skip over the assert and continue the debugging session
michael@0 645 past it. */
michael@0 646 PRBool dontassert = PR_FALSE;
michael@0 647 PR_ASSERT(dontassert); /* set bkpoint here & set dontassert*/
michael@0 648 }
michael@0 649 #endif
michael@0 650
michael@0 651 if ((kind & SEC_ASN1_SKIP) ||
michael@0 652 (kind & SEC_ASN1_SAVE))
michael@0 653 {
michael@0 654 /* if skipping or saving this component, don't decode it */
michael@0 655 decode = PR_FALSE;
michael@0 656 }
michael@0 657
michael@0 658 if (kind & (SEC_ASN1_SAVE | SEC_ASN1_OPTIONAL))
michael@0 659 {
michael@0 660 /* if saving this component, or if it is optional, we may not want to
michael@0 661 move past it, so save the position in case we have to rewind */
michael@0 662 mark = *src;
michael@0 663 if (kind & SEC_ASN1_SAVE)
michael@0 664 {
michael@0 665 save = PR_TRUE;
michael@0 666 if (0 == (kind & SEC_ASN1_SKIP))
michael@0 667 {
michael@0 668 /* we will for sure have to rewind when saving this
michael@0 669 component and not skipping it. This is true for all
michael@0 670 legacy uses of SEC_ASN1_SAVE where the following entry
michael@0 671 in the template would causes the same component to be
michael@0 672 processed again */
michael@0 673 pop = PR_TRUE;
michael@0 674 }
michael@0 675 }
michael@0 676 }
michael@0 677
michael@0 678 rv = GetItem(src, &temp, PR_TRUE);
michael@0 679 }
michael@0 680
michael@0 681 if (SECSuccess == rv)
michael@0 682 {
michael@0 683 /* now check if the component matches what we expect in the template */
michael@0 684
michael@0 685 if (PR_TRUE == checkTag)
michael@0 686
michael@0 687 {
michael@0 688 rv = MatchComponentType(templateEntry, &temp, &match, dest);
michael@0 689 }
michael@0 690
michael@0 691 if ( (SECSuccess == rv) && (PR_TRUE != match) )
michael@0 692 {
michael@0 693 if (kind & SEC_ASN1_OPTIONAL)
michael@0 694 {
michael@0 695
michael@0 696 /* the optional component is missing. This is not fatal. */
michael@0 697 /* Rewind, don't decode, and don't save */
michael@0 698 pop = PR_TRUE;
michael@0 699 decode = PR_FALSE;
michael@0 700 save = PR_FALSE;
michael@0 701 }
michael@0 702 else
michael@0 703 {
michael@0 704 /* a required component is missing. abort */
michael@0 705 PORT_SetError(SEC_ERROR_BAD_DER);
michael@0 706 rv = SECFailure;
michael@0 707 }
michael@0 708 }
michael@0 709 }
michael@0 710
michael@0 711 if ((SECSuccess == rv) && (PR_TRUE == decode))
michael@0 712 {
michael@0 713 /* the order of processing here is is the tricky part */
michael@0 714 /* we start with our special cases */
michael@0 715 /* first, check the component class */
michael@0 716 if (kind & SEC_ASN1_INLINE)
michael@0 717 {
michael@0 718 /* decode inline template */
michael@0 719 rv = DecodeInline(dest, templateEntry, &temp , arena, PR_TRUE);
michael@0 720 }
michael@0 721
michael@0 722 else
michael@0 723 if (kind & SEC_ASN1_EXPLICIT)
michael@0 724 {
michael@0 725 rv = DecodeExplicit(dest, templateEntry, &temp, arena);
michael@0 726 }
michael@0 727 else
michael@0 728 if ( (SEC_ASN1_UNIVERSAL != (kind & SEC_ASN1_CLASS_MASK)) &&
michael@0 729
michael@0 730 (!(kind & SEC_ASN1_EXPLICIT)))
michael@0 731 {
michael@0 732
michael@0 733 /* decode implicitly tagged components */
michael@0 734 rv = DecodeImplicit(dest, templateEntry, &temp , arena);
michael@0 735 }
michael@0 736 else
michael@0 737 if (kind & SEC_ASN1_POINTER)
michael@0 738 {
michael@0 739 rv = DecodePointer(dest, templateEntry, &temp, arena, PR_TRUE);
michael@0 740 }
michael@0 741 else
michael@0 742 if (kind & SEC_ASN1_CHOICE)
michael@0 743 {
michael@0 744 rv = DecodeChoice(dest, templateEntry, &temp, arena);
michael@0 745 }
michael@0 746 else
michael@0 747 if (kind & SEC_ASN1_ANY)
michael@0 748 {
michael@0 749 /* catch-all ANY type, don't decode */
michael@0 750 save = PR_TRUE;
michael@0 751 if (kind & SEC_ASN1_INNER)
michael@0 752 {
michael@0 753 /* skip the tag and length */
michael@0 754 SECItem newtemp = temp;
michael@0 755 rv = GetItem(&newtemp, &temp, PR_FALSE);
michael@0 756 }
michael@0 757 }
michael@0 758 else
michael@0 759 if (kind & SEC_ASN1_GROUP)
michael@0 760 {
michael@0 761 if ( (SEC_ASN1_SEQUENCE == (kind & SEC_ASN1_TAGNUM_MASK)) ||
michael@0 762 (SEC_ASN1_SET == (kind & SEC_ASN1_TAGNUM_MASK)) )
michael@0 763 {
michael@0 764 rv = DecodeGroup(dest, templateEntry, &temp , arena);
michael@0 765 }
michael@0 766 else
michael@0 767 {
michael@0 768 /* a group can only be a SET OF or SEQUENCE OF */
michael@0 769 PORT_SetError(SEC_ERROR_BAD_TEMPLATE);
michael@0 770 rv = SECFailure;
michael@0 771 }
michael@0 772 }
michael@0 773 else
michael@0 774 if (SEC_ASN1_SEQUENCE == (kind & SEC_ASN1_TAGNUM_MASK))
michael@0 775 {
michael@0 776 /* plain SEQUENCE */
michael@0 777 rv = DecodeSequence(dest, templateEntry, &temp , arena);
michael@0 778 }
michael@0 779 else
michael@0 780 {
michael@0 781 /* handle all other types as "save" */
michael@0 782 /* we should only get here for primitive universal types */
michael@0 783 SECItem newtemp = temp;
michael@0 784 rv = GetItem(&newtemp, &temp, PR_FALSE);
michael@0 785 save = PR_TRUE;
michael@0 786 if ((SECSuccess == rv) &&
michael@0 787 SEC_ASN1_UNIVERSAL == (kind & SEC_ASN1_CLASS_MASK))
michael@0 788 {
michael@0 789 unsigned long tagnum = kind & SEC_ASN1_TAGNUM_MASK;
michael@0 790 if ( temp.len == 0 && (tagnum == SEC_ASN1_BOOLEAN ||
michael@0 791 tagnum == SEC_ASN1_INTEGER ||
michael@0 792 tagnum == SEC_ASN1_BIT_STRING ||
michael@0 793 tagnum == SEC_ASN1_OBJECT_ID ||
michael@0 794 tagnum == SEC_ASN1_ENUMERATED ||
michael@0 795 tagnum == SEC_ASN1_UTC_TIME ||
michael@0 796 tagnum == SEC_ASN1_GENERALIZED_TIME) )
michael@0 797 {
michael@0 798 /* these types MUST have at least one content octet */
michael@0 799 PORT_SetError(SEC_ERROR_BAD_DER);
michael@0 800 rv = SECFailure;
michael@0 801 }
michael@0 802 else
michael@0 803 switch (tagnum)
michael@0 804 {
michael@0 805 /* special cases of primitive types */
michael@0 806 case SEC_ASN1_INTEGER:
michael@0 807 {
michael@0 808 /* remove leading zeroes if the caller requested
michael@0 809 siUnsignedInteger
michael@0 810 This is to allow RSA key operations to work */
michael@0 811 SECItem* destItem = (SECItem*) ((char*)dest +
michael@0 812 templateEntry->offset);
michael@0 813 if (destItem && (siUnsignedInteger == destItem->type))
michael@0 814 {
michael@0 815 while (temp.len > 1 && temp.data[0] == 0)
michael@0 816 { /* leading 0 */
michael@0 817 temp.data++;
michael@0 818 temp.len--;
michael@0 819 }
michael@0 820 }
michael@0 821 break;
michael@0 822 }
michael@0 823
michael@0 824 case SEC_ASN1_BIT_STRING:
michael@0 825 {
michael@0 826 /* change the length in the SECItem to be the number
michael@0 827 of bits */
michael@0 828 temp.len = (temp.len-1)*8 - (temp.data[0] & 0x7);
michael@0 829 temp.data++;
michael@0 830 break;
michael@0 831 }
michael@0 832
michael@0 833 default:
michael@0 834 {
michael@0 835 break;
michael@0 836 }
michael@0 837 }
michael@0 838 }
michael@0 839 }
michael@0 840 }
michael@0 841
michael@0 842 if ((SECSuccess == rv) && (PR_TRUE == save))
michael@0 843 {
michael@0 844 SECItem* destItem = (SECItem*) ((char*)dest + templateEntry->offset);
michael@0 845 if (destItem)
michael@0 846 {
michael@0 847 /* we leave the type alone in the destination SECItem.
michael@0 848 If part of the destination was allocated by the decoder, in
michael@0 849 cases of POINTER, SET OF and SEQUENCE OF, then type is set to
michael@0 850 siBuffer due to the use of PORT_ArenaZAlloc*/
michael@0 851 destItem->data = temp.len ? temp.data : NULL;
michael@0 852 destItem->len = temp.len;
michael@0 853 }
michael@0 854 else
michael@0 855 {
michael@0 856 PORT_SetError(SEC_ERROR_INVALID_ARGS);
michael@0 857 rv = SECFailure;
michael@0 858 }
michael@0 859 }
michael@0 860
michael@0 861 if (PR_TRUE == pop)
michael@0 862 {
michael@0 863 /* we don't want to move ahead, so restore the position */
michael@0 864 *src = mark;
michael@0 865 }
michael@0 866 return rv;
michael@0 867 }
michael@0 868
michael@0 869 /* the function below is the public one */
michael@0 870
michael@0 871 SECStatus SEC_QuickDERDecodeItem(PLArenaPool* arena, void* dest,
michael@0 872 const SEC_ASN1Template* templateEntry,
michael@0 873 const SECItem* src)
michael@0 874 {
michael@0 875 SECStatus rv = SECSuccess;
michael@0 876 SECItem newsrc;
michael@0 877
michael@0 878 if (!arena || !templateEntry || !src)
michael@0 879 {
michael@0 880 PORT_SetError(SEC_ERROR_INVALID_ARGS);
michael@0 881 rv = SECFailure;
michael@0 882 }
michael@0 883
michael@0 884 if (SECSuccess == rv)
michael@0 885 {
michael@0 886 newsrc = *src;
michael@0 887 rv = DecodeItem(dest, templateEntry, &newsrc, arena, PR_TRUE);
michael@0 888 if (SECSuccess == rv && newsrc.len)
michael@0 889 {
michael@0 890 rv = SECFailure;
michael@0 891 PORT_SetError(SEC_ERROR_EXTRA_INPUT);
michael@0 892 }
michael@0 893 }
michael@0 894
michael@0 895 return rv;
michael@0 896 }
michael@0 897

mercurial