andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: /* andre@0: Optimized ASN.1 DER decoder andre@0: andre@0: */ andre@0: andre@0: #include "secerr.h" andre@0: #include "secasn1.h" /* for SEC_ASN1GetSubtemplate */ andre@0: #include "secitem.h" andre@0: andre@0: /* andre@0: * simple definite-length ASN.1 decoder andre@0: */ andre@0: andre@0: static unsigned char* definite_length_decoder(const unsigned char *buf, andre@0: const unsigned int length, andre@0: unsigned int *data_length, andre@0: PRBool includeTag) andre@0: { andre@0: unsigned char tag; andre@0: unsigned int used_length= 0; andre@0: unsigned int data_len; andre@0: andre@0: if (used_length >= length) andre@0: { andre@0: return NULL; andre@0: } andre@0: tag = buf[used_length++]; andre@0: andre@0: /* blow out when we come to the end */ andre@0: if (tag == 0) andre@0: { andre@0: return NULL; andre@0: } andre@0: andre@0: if (used_length >= length) andre@0: { andre@0: return NULL; andre@0: } andre@0: data_len = buf[used_length++]; andre@0: andre@0: if (data_len&0x80) andre@0: { andre@0: int len_count = data_len & 0x7f; andre@0: andre@0: data_len = 0; andre@0: andre@0: while (len_count-- > 0) andre@0: { andre@0: if (used_length >= length) andre@0: { andre@0: return NULL; andre@0: } andre@0: data_len = (data_len << 8) | buf[used_length++]; andre@0: } andre@0: } andre@0: andre@0: if (data_len > (length-used_length) ) andre@0: { andre@0: return NULL; andre@0: } andre@0: if (includeTag) data_len += used_length; andre@0: andre@0: *data_length = data_len; andre@0: return ((unsigned char*)buf + (includeTag ? 0 : used_length)); andre@0: } andre@0: andre@0: static SECStatus GetItem(SECItem* src, SECItem* dest, PRBool includeTag) andre@0: { andre@0: if ( (!src) || (!dest) || (!src->data && src->len) ) andre@0: { andre@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); andre@0: return SECFailure; andre@0: } andre@0: andre@0: if (!src->len) andre@0: { andre@0: /* reaching the end of the buffer is not an error */ andre@0: dest->data = NULL; andre@0: dest->len = 0; andre@0: return SECSuccess; andre@0: } andre@0: andre@0: dest->data = definite_length_decoder(src->data, src->len, &dest->len, andre@0: includeTag); andre@0: if (dest->data == NULL) andre@0: { andre@0: PORT_SetError(SEC_ERROR_BAD_DER); andre@0: return SECFailure; andre@0: } andre@0: src->len -= (dest->data - src->data) + dest->len; andre@0: src->data = dest->data + dest->len; andre@0: return SECSuccess; andre@0: } andre@0: andre@0: /* check if the actual component's type matches the type in the template */ andre@0: andre@0: static SECStatus MatchComponentType(const SEC_ASN1Template* templateEntry, andre@0: SECItem* item, PRBool* match, void* dest) andre@0: { andre@0: unsigned long kind = 0; andre@0: unsigned char tag = 0; andre@0: andre@0: if ( (!item) || (!item->data && item->len) || (!templateEntry) || (!match) ) andre@0: { andre@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); andre@0: return SECFailure; andre@0: } andre@0: andre@0: if (!item->len) andre@0: { andre@0: *match = PR_FALSE; andre@0: return SECSuccess; andre@0: } andre@0: andre@0: kind = templateEntry->kind; andre@0: tag = *(unsigned char*) item->data; andre@0: andre@0: if ( ( (kind & SEC_ASN1_INLINE) || andre@0: (kind & SEC_ASN1_POINTER) ) && andre@0: (0 == (kind & SEC_ASN1_TAG_MASK) ) ) andre@0: { andre@0: /* These cases are special because the template's "kind" does not andre@0: give us the information for the ASN.1 tag of the next item. It can andre@0: only be figured out from the subtemplate. */ andre@0: if (!(kind & SEC_ASN1_OPTIONAL)) andre@0: { andre@0: /* This is a required component. If there is a type mismatch, andre@0: the decoding of the subtemplate will fail, so assume this andre@0: is a match at the parent level and let it fail later. This andre@0: avoids a redundant check in matching cases */ andre@0: *match = PR_TRUE; andre@0: return SECSuccess; andre@0: } andre@0: else andre@0: { andre@0: /* optional component. This is the hard case. Now we need to andre@0: look at the subtemplate to get the expected kind */ andre@0: const SEC_ASN1Template* subTemplate = andre@0: SEC_ASN1GetSubtemplate (templateEntry, dest, PR_FALSE); andre@0: if (!subTemplate) andre@0: { andre@0: PORT_SetError(SEC_ERROR_BAD_TEMPLATE); andre@0: return SECFailure; andre@0: } andre@0: if ( (subTemplate->kind & SEC_ASN1_INLINE) || andre@0: (subTemplate->kind & SEC_ASN1_POINTER) ) andre@0: { andre@0: /* disallow nesting SEC_ASN1_POINTER and SEC_ASN1_INLINE, andre@0: otherwise you may get a false positive due to the recursion andre@0: optimization above that always matches the type if the andre@0: component is required . Nesting these should never be andre@0: required, so that no one should miss this ability */ andre@0: PORT_SetError(SEC_ERROR_BAD_TEMPLATE); andre@0: return SECFailure; andre@0: } andre@0: return MatchComponentType(subTemplate, item, match, andre@0: (void*)((char*)dest + templateEntry->offset)); andre@0: } andre@0: } andre@0: andre@0: if (kind & SEC_ASN1_CHOICE) andre@0: { andre@0: /* we need to check the component's tag against each choice's tag */ andre@0: /* XXX it would be nice to save the index of the choice here so that andre@0: DecodeChoice wouldn't have to do this again. However, due to the andre@0: recursivity of MatchComponentType, we don't know if we are in a andre@0: required or optional component, so we can't write anywhere in andre@0: the destination within this function */ andre@0: unsigned choiceIndex = 1; andre@0: const SEC_ASN1Template* choiceEntry; andre@0: while ( (choiceEntry = &templateEntry[choiceIndex++]) && (choiceEntry->kind)) andre@0: { andre@0: if ( (SECSuccess == MatchComponentType(choiceEntry, item, match, andre@0: (void*)((char*)dest + choiceEntry->offset))) && andre@0: (PR_TRUE == *match) ) andre@0: { andre@0: return SECSuccess; andre@0: } andre@0: } andre@0: /* no match, caller must decide if this is BAD DER, or not. */ andre@0: *match = PR_FALSE; andre@0: return SECSuccess; andre@0: } andre@0: andre@0: if (kind & SEC_ASN1_ANY) andre@0: { andre@0: /* SEC_ASN1_ANY always matches */ andre@0: *match = PR_TRUE; andre@0: return SECSuccess; andre@0: } andre@0: andre@0: if ( (0 == ((unsigned char)kind & SEC_ASN1_TAGNUM_MASK)) && andre@0: (!(kind & SEC_ASN1_EXPLICIT)) && andre@0: ( ( (kind & SEC_ASN1_SAVE) || andre@0: (kind & SEC_ASN1_SKIP) ) && andre@0: (!(kind & SEC_ASN1_OPTIONAL)) andre@0: ) andre@0: ) andre@0: { andre@0: /* when saving or skipping a required component, a type is not andre@0: required in the template. This is for legacy support of andre@0: SEC_ASN1_SAVE and SEC_ASN1_SKIP only. XXX I would like to andre@0: deprecate these usages and always require a type, as this andre@0: disables type checking, and effectively forbids us from andre@0: transparently ignoring optional components we aren't aware of */ andre@0: *match = PR_TRUE; andre@0: return SECSuccess; andre@0: } andre@0: andre@0: /* first, do a class check */ andre@0: if ( (tag & SEC_ASN1_CLASS_MASK) != andre@0: (((unsigned char)kind) & SEC_ASN1_CLASS_MASK) ) andre@0: { andre@0: #ifdef DEBUG andre@0: /* this is only to help debugging of the decoder in case of problems */ andre@0: unsigned char tagclass = tag & SEC_ASN1_CLASS_MASK; andre@0: unsigned char expectedclass = (unsigned char)kind & SEC_ASN1_CLASS_MASK; andre@0: tagclass = tagclass; andre@0: expectedclass = expectedclass; andre@0: #endif andre@0: *match = PR_FALSE; andre@0: return SECSuccess; andre@0: } andre@0: andre@0: /* now do a tag check */ andre@0: if ( ((unsigned char)kind & SEC_ASN1_TAGNUM_MASK) != andre@0: (tag & SEC_ASN1_TAGNUM_MASK)) andre@0: { andre@0: *match = PR_FALSE; andre@0: return SECSuccess; andre@0: } andre@0: andre@0: /* now, do a method check. This depends on the class */ andre@0: switch (tag & SEC_ASN1_CLASS_MASK) andre@0: { andre@0: case SEC_ASN1_UNIVERSAL: andre@0: /* For types of the SEC_ASN1_UNIVERSAL class, we know which must be andre@0: primitive or constructed based on the tag */ andre@0: switch (tag & SEC_ASN1_TAGNUM_MASK) andre@0: { andre@0: case SEC_ASN1_SEQUENCE: andre@0: case SEC_ASN1_SET: andre@0: case SEC_ASN1_EMBEDDED_PDV: andre@0: /* this component must be a constructed type */ andre@0: /* XXX add any new universal constructed type here */ andre@0: if (tag & SEC_ASN1_CONSTRUCTED) andre@0: { andre@0: *match = PR_TRUE; andre@0: return SECSuccess; andre@0: } andre@0: break; andre@0: andre@0: default: andre@0: /* this component must be a primitive type */ andre@0: if (! (tag & SEC_ASN1_CONSTRUCTED)) andre@0: { andre@0: *match = PR_TRUE; andre@0: return SECSuccess; andre@0: } andre@0: break; andre@0: } andre@0: break; andre@0: andre@0: default: andre@0: /* for all other classes, we check the method based on the template */ andre@0: if ( (unsigned char)(kind & SEC_ASN1_METHOD_MASK) == andre@0: (tag & SEC_ASN1_METHOD_MASK) ) andre@0: { andre@0: *match = PR_TRUE; andre@0: return SECSuccess; andre@0: } andre@0: /* method does not match between template and component */ andre@0: break; andre@0: } andre@0: andre@0: *match = PR_FALSE; andre@0: return SECSuccess; andre@0: } andre@0: andre@0: #ifdef DEBUG andre@0: andre@0: static SECStatus CheckSequenceTemplate(const SEC_ASN1Template* sequenceTemplate) andre@0: { andre@0: SECStatus rv = SECSuccess; andre@0: const SEC_ASN1Template* sequenceEntry = NULL; andre@0: unsigned long seqIndex = 0; andre@0: unsigned long lastEntryIndex = 0; andre@0: unsigned long ambiguityIndex = 0; andre@0: PRBool foundAmbiguity = PR_FALSE; andre@0: andre@0: do andre@0: { andre@0: sequenceEntry = &sequenceTemplate[seqIndex++]; andre@0: if (sequenceEntry->kind) andre@0: { andre@0: /* ensure that we don't have an optional component of SEC_ASN1_ANY andre@0: in the middle of the sequence, since we could not handle it */ andre@0: /* XXX this function needs to dig into the subtemplates to find andre@0: the next tag */ andre@0: if ( (PR_FALSE == foundAmbiguity) && andre@0: (sequenceEntry->kind & SEC_ASN1_OPTIONAL) && andre@0: (sequenceEntry->kind & SEC_ASN1_ANY) ) andre@0: { andre@0: foundAmbiguity = PR_TRUE; andre@0: ambiguityIndex = seqIndex - 1; andre@0: } andre@0: } andre@0: } while (sequenceEntry->kind); andre@0: andre@0: lastEntryIndex = seqIndex - 2; andre@0: andre@0: if (PR_FALSE != foundAmbiguity) andre@0: { andre@0: if (ambiguityIndex < lastEntryIndex) andre@0: { andre@0: /* ambiguity can only be tolerated on the last entry */ andre@0: PORT_SetError(SEC_ERROR_BAD_TEMPLATE); andre@0: rv = SECFailure; andre@0: } andre@0: } andre@0: andre@0: /* XXX also enforce ASN.1 requirement that tags be andre@0: distinct for consecutive optional components */ andre@0: andre@0: return rv; andre@0: } andre@0: andre@0: #endif andre@0: andre@0: static SECStatus DecodeItem(void* dest, andre@0: const SEC_ASN1Template* templateEntry, andre@0: SECItem* src, PLArenaPool* arena, PRBool checkTag); andre@0: andre@0: static SECStatus DecodeSequence(void* dest, andre@0: const SEC_ASN1Template* templateEntry, andre@0: SECItem* src, PLArenaPool* arena) andre@0: { andre@0: SECStatus rv = SECSuccess; andre@0: SECItem source; andre@0: SECItem sequence; andre@0: const SEC_ASN1Template* sequenceTemplate = &(templateEntry[1]); andre@0: const SEC_ASN1Template* sequenceEntry = NULL; andre@0: unsigned long seqindex = 0; andre@0: andre@0: #ifdef DEBUG andre@0: /* for a sequence, we need to validate the template. */ andre@0: rv = CheckSequenceTemplate(sequenceTemplate); andre@0: #endif andre@0: andre@0: source = *src; andre@0: andre@0: /* get the sequence */ andre@0: if (SECSuccess == rv) andre@0: { andre@0: rv = GetItem(&source, &sequence, PR_FALSE); andre@0: } andre@0: andre@0: /* process it */ andre@0: if (SECSuccess == rv) andre@0: do andre@0: { andre@0: sequenceEntry = &sequenceTemplate[seqindex++]; andre@0: if ( (sequenceEntry && sequenceEntry->kind) && andre@0: (sequenceEntry->kind != SEC_ASN1_SKIP_REST) ) andre@0: { andre@0: rv = DecodeItem(dest, sequenceEntry, &sequence, arena, PR_TRUE); andre@0: } andre@0: } while ( (SECSuccess == rv) && andre@0: (sequenceEntry->kind && andre@0: sequenceEntry->kind != SEC_ASN1_SKIP_REST) ); andre@0: /* we should have consumed all the bytes in the sequence by now andre@0: unless the caller doesn't care about the rest of the sequence */ andre@0: if (SECSuccess == rv && sequence.len && andre@0: sequenceEntry && sequenceEntry->kind != SEC_ASN1_SKIP_REST) andre@0: { andre@0: /* it isn't 100% clear whether this is a bad DER or a bad template. andre@0: The problem is that logically, they don't match - there is extra andre@0: data in the DER that the template doesn't know about */ andre@0: PORT_SetError(SEC_ERROR_BAD_DER); andre@0: rv = SECFailure; andre@0: } andre@0: andre@0: return rv; andre@0: } andre@0: andre@0: static SECStatus DecodeInline(void* dest, andre@0: const SEC_ASN1Template* templateEntry, andre@0: SECItem* src, PLArenaPool* arena, PRBool checkTag) andre@0: { andre@0: const SEC_ASN1Template* inlineTemplate = andre@0: SEC_ASN1GetSubtemplate (templateEntry, dest, PR_FALSE); andre@0: return DecodeItem((void*)((char*)dest + templateEntry->offset), andre@0: inlineTemplate, src, arena, checkTag); andre@0: } andre@0: andre@0: static SECStatus DecodePointer(void* dest, andre@0: const SEC_ASN1Template* templateEntry, andre@0: SECItem* src, PLArenaPool* arena, PRBool checkTag) andre@0: { andre@0: const SEC_ASN1Template* ptrTemplate = andre@0: SEC_ASN1GetSubtemplate (templateEntry, dest, PR_FALSE); andre@0: void* subdata = PORT_ArenaZAlloc(arena, ptrTemplate->size); andre@0: *(void**)((char*)dest + templateEntry->offset) = subdata; andre@0: if (subdata) andre@0: { andre@0: return DecodeItem(subdata, ptrTemplate, src, arena, checkTag); andre@0: } andre@0: else andre@0: { andre@0: PORT_SetError(SEC_ERROR_NO_MEMORY); andre@0: return SECFailure; andre@0: } andre@0: } andre@0: andre@0: static SECStatus DecodeImplicit(void* dest, andre@0: const SEC_ASN1Template* templateEntry, andre@0: SECItem* src, PLArenaPool* arena) andre@0: { andre@0: if (templateEntry->kind & SEC_ASN1_POINTER) andre@0: { andre@0: return DecodePointer((void*)((char*)dest ), andre@0: templateEntry, src, arena, PR_FALSE); andre@0: } andre@0: else andre@0: { andre@0: return DecodeInline((void*)((char*)dest ), andre@0: templateEntry, src, arena, PR_FALSE); andre@0: } andre@0: } andre@0: andre@0: static SECStatus DecodeChoice(void* dest, andre@0: const SEC_ASN1Template* templateEntry, andre@0: SECItem* src, PLArenaPool* arena) andre@0: { andre@0: SECStatus rv = SECSuccess; andre@0: SECItem choice; andre@0: const SEC_ASN1Template* choiceTemplate = &(templateEntry[1]); andre@0: const SEC_ASN1Template* choiceEntry = NULL; andre@0: unsigned long choiceindex = 0; andre@0: andre@0: /* XXX for a choice component, we should validate the template to make andre@0: sure the tags are distinct, in debug builds. This hasn't been andre@0: implemented yet */ andre@0: /* rv = CheckChoiceTemplate(sequenceTemplate); */ andre@0: andre@0: /* process it */ andre@0: do andre@0: { andre@0: choice = *src; andre@0: choiceEntry = &choiceTemplate[choiceindex++]; andre@0: if (choiceEntry->kind) andre@0: { andre@0: rv = DecodeItem(dest, choiceEntry, &choice, arena, PR_TRUE); andre@0: } andre@0: } while ( (SECFailure == rv) && (choiceEntry->kind)); andre@0: andre@0: if (SECFailure == rv) andre@0: { andre@0: /* the component didn't match any of the choices */ andre@0: PORT_SetError(SEC_ERROR_BAD_DER); andre@0: } andre@0: else andre@0: { andre@0: /* set the type in the union here */ andre@0: int *which = (int *)((char *)dest + templateEntry->offset); andre@0: *which = (int)choiceEntry->size; andre@0: } andre@0: andre@0: /* we should have consumed all the bytes by now */ andre@0: /* fail if we have not */ andre@0: if (SECSuccess == rv && choice.len) andre@0: { andre@0: /* there is extra data that isn't listed in the template */ andre@0: PORT_SetError(SEC_ERROR_BAD_DER); andre@0: rv = SECFailure; andre@0: } andre@0: return rv; andre@0: } andre@0: andre@0: static SECStatus DecodeGroup(void* dest, andre@0: const SEC_ASN1Template* templateEntry, andre@0: SECItem* src, PLArenaPool* arena) andre@0: { andre@0: SECStatus rv = SECSuccess; andre@0: SECItem source; andre@0: SECItem group; andre@0: PRUint32 totalEntries = 0; andre@0: PRUint32 entryIndex = 0; andre@0: void** entries = NULL; andre@0: andre@0: const SEC_ASN1Template* subTemplate = andre@0: SEC_ASN1GetSubtemplate (templateEntry, dest, PR_FALSE); andre@0: andre@0: source = *src; andre@0: andre@0: /* get the group */ andre@0: if (SECSuccess == rv) andre@0: { andre@0: rv = GetItem(&source, &group, PR_FALSE); andre@0: } andre@0: andre@0: /* XXX we should check the subtemplate in debug builds */ andre@0: if (SECSuccess == rv) andre@0: { andre@0: /* first, count the number of entries. Benchmarking showed that this andre@0: counting pass is more efficient than trying to allocate entries as andre@0: we read the DER, even if allocating many entries at a time andre@0: */ andre@0: SECItem counter = group; andre@0: do andre@0: { andre@0: SECItem anitem; andre@0: rv = GetItem(&counter, &anitem, PR_TRUE); andre@0: if (SECSuccess == rv && (anitem.len) ) andre@0: { andre@0: totalEntries++; andre@0: } andre@0: } while ( (SECSuccess == rv) && (counter.len) ); andre@0: andre@0: if (SECSuccess == rv) andre@0: { andre@0: /* allocate room for pointer array and entries */ andre@0: /* we want to allocate the array even if there is 0 entry */ andre@0: entries = (void**)PORT_ArenaZAlloc(arena, sizeof(void*)* andre@0: (totalEntries + 1 ) + /* the extra one is for NULL termination */ andre@0: subTemplate->size*totalEntries); andre@0: andre@0: if (entries) andre@0: { andre@0: entries[totalEntries] = NULL; /* terminate the array */ andre@0: } andre@0: else andre@0: { andre@0: PORT_SetError(SEC_ERROR_NO_MEMORY); andre@0: rv = SECFailure; andre@0: } andre@0: if (SECSuccess == rv) andre@0: { andre@0: void* entriesData = (unsigned char*)entries + (unsigned long)(sizeof(void*)*(totalEntries + 1 )); andre@0: /* and fix the pointers in the array */ andre@0: PRUint32 entriesIndex = 0; andre@0: for (entriesIndex = 0;entriesIndexsize*entriesIndex); andre@0: } andre@0: } andre@0: } andre@0: } andre@0: andre@0: if (SECSuccess == rv && totalEntries) andre@0: do andre@0: { andre@0: if (!(entryIndexoffset), &entries, sizeof(void**)); andre@0: andre@0: return rv; andre@0: } andre@0: andre@0: static SECStatus DecodeExplicit(void* dest, andre@0: const SEC_ASN1Template* templateEntry, andre@0: SECItem* src, PLArenaPool* arena) andre@0: { andre@0: SECStatus rv = SECSuccess; andre@0: SECItem subItem; andre@0: SECItem constructed = *src; andre@0: andre@0: rv = GetItem(&constructed, &subItem, PR_FALSE); andre@0: andre@0: if (SECSuccess == rv) andre@0: { andre@0: if (templateEntry->kind & SEC_ASN1_POINTER) andre@0: { andre@0: rv = DecodePointer(dest, templateEntry, &subItem, arena, PR_TRUE); andre@0: } andre@0: else andre@0: { andre@0: rv = DecodeInline(dest, templateEntry, &subItem, arena, PR_TRUE); andre@0: } andre@0: } andre@0: andre@0: return rv; andre@0: } andre@0: andre@0: /* new decoder implementation. This is a recursive function */ andre@0: andre@0: static SECStatus DecodeItem(void* dest, andre@0: const SEC_ASN1Template* templateEntry, andre@0: SECItem* src, PLArenaPool* arena, PRBool checkTag) andre@0: { andre@0: SECStatus rv = SECSuccess; andre@0: SECItem temp; andre@0: SECItem mark; andre@0: PRBool pop = PR_FALSE; andre@0: PRBool decode = PR_TRUE; andre@0: PRBool save = PR_FALSE; andre@0: unsigned long kind; andre@0: PRBool match = PR_TRUE; andre@0: PRBool optional = PR_FALSE; andre@0: andre@0: PR_ASSERT(src && dest && templateEntry && arena); andre@0: #if 0 andre@0: if (!src || !dest || !templateEntry || !arena) andre@0: { andre@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); andre@0: rv = SECFailure; andre@0: } andre@0: #endif andre@0: andre@0: if (SECSuccess == rv) andre@0: { andre@0: /* do the template validation */ andre@0: kind = templateEntry->kind; andre@0: optional = (0 != (kind & SEC_ASN1_OPTIONAL)); andre@0: if (!kind) andre@0: { andre@0: PORT_SetError(SEC_ERROR_BAD_TEMPLATE); andre@0: rv = SECFailure; andre@0: } andre@0: } andre@0: andre@0: if (SECSuccess == rv) andre@0: { andre@0: #ifdef DEBUG andre@0: if (kind & SEC_ASN1_DEBUG_BREAK) andre@0: { andre@0: /* when debugging the decoder or a template that fails to andre@0: decode, put SEC_ASN1_DEBUG in the component that gives you andre@0: trouble. The decoder will then get to this block and assert. andre@0: If you want to debug the rest of the code, you can set a andre@0: breakpoint and set dontassert to PR_TRUE, which will let andre@0: you skip over the assert and continue the debugging session andre@0: past it. */ andre@0: PRBool dontassert = PR_FALSE; andre@0: PR_ASSERT(dontassert); /* set bkpoint here & set dontassert*/ andre@0: } andre@0: #endif andre@0: andre@0: if ((kind & SEC_ASN1_SKIP) || andre@0: (kind & SEC_ASN1_SAVE)) andre@0: { andre@0: /* if skipping or saving this component, don't decode it */ andre@0: decode = PR_FALSE; andre@0: } andre@0: andre@0: if (kind & (SEC_ASN1_SAVE | SEC_ASN1_OPTIONAL)) andre@0: { andre@0: /* if saving this component, or if it is optional, we may not want to andre@0: move past it, so save the position in case we have to rewind */ andre@0: mark = *src; andre@0: if (kind & SEC_ASN1_SAVE) andre@0: { andre@0: save = PR_TRUE; andre@0: if (0 == (kind & SEC_ASN1_SKIP)) andre@0: { andre@0: /* we will for sure have to rewind when saving this andre@0: component and not skipping it. This is true for all andre@0: legacy uses of SEC_ASN1_SAVE where the following entry andre@0: in the template would causes the same component to be andre@0: processed again */ andre@0: pop = PR_TRUE; andre@0: } andre@0: } andre@0: } andre@0: andre@0: rv = GetItem(src, &temp, PR_TRUE); andre@0: } andre@0: andre@0: if (SECSuccess == rv) andre@0: { andre@0: /* now check if the component matches what we expect in the template */ andre@0: andre@0: if (PR_TRUE == checkTag) andre@0: andre@0: { andre@0: rv = MatchComponentType(templateEntry, &temp, &match, dest); andre@0: } andre@0: andre@0: if ( (SECSuccess == rv) && (PR_TRUE != match) ) andre@0: { andre@0: if (kind & SEC_ASN1_OPTIONAL) andre@0: { andre@0: andre@0: /* the optional component is missing. This is not fatal. */ andre@0: /* Rewind, don't decode, and don't save */ andre@0: pop = PR_TRUE; andre@0: decode = PR_FALSE; andre@0: save = PR_FALSE; andre@0: } andre@0: else andre@0: { andre@0: /* a required component is missing. abort */ andre@0: PORT_SetError(SEC_ERROR_BAD_DER); andre@0: rv = SECFailure; andre@0: } andre@0: } andre@0: } andre@0: andre@0: if ((SECSuccess == rv) && (PR_TRUE == decode)) andre@0: { andre@0: /* the order of processing here is is the tricky part */ andre@0: /* we start with our special cases */ andre@0: /* first, check the component class */ andre@0: if (kind & SEC_ASN1_INLINE) andre@0: { andre@0: /* decode inline template */ andre@0: rv = DecodeInline(dest, templateEntry, &temp , arena, PR_TRUE); andre@0: } andre@0: andre@0: else andre@0: if (kind & SEC_ASN1_EXPLICIT) andre@0: { andre@0: rv = DecodeExplicit(dest, templateEntry, &temp, arena); andre@0: } andre@0: else andre@0: if ( (SEC_ASN1_UNIVERSAL != (kind & SEC_ASN1_CLASS_MASK)) && andre@0: andre@0: (!(kind & SEC_ASN1_EXPLICIT))) andre@0: { andre@0: andre@0: /* decode implicitly tagged components */ andre@0: rv = DecodeImplicit(dest, templateEntry, &temp , arena); andre@0: } andre@0: else andre@0: if (kind & SEC_ASN1_POINTER) andre@0: { andre@0: rv = DecodePointer(dest, templateEntry, &temp, arena, PR_TRUE); andre@0: } andre@0: else andre@0: if (kind & SEC_ASN1_CHOICE) andre@0: { andre@0: rv = DecodeChoice(dest, templateEntry, &temp, arena); andre@0: } andre@0: else andre@0: if (kind & SEC_ASN1_ANY) andre@0: { andre@0: /* catch-all ANY type, don't decode */ andre@0: save = PR_TRUE; andre@0: if (kind & SEC_ASN1_INNER) andre@0: { andre@0: /* skip the tag and length */ andre@0: SECItem newtemp = temp; andre@0: rv = GetItem(&newtemp, &temp, PR_FALSE); andre@0: } andre@0: } andre@0: else andre@0: if (kind & SEC_ASN1_GROUP) andre@0: { andre@0: if ( (SEC_ASN1_SEQUENCE == (kind & SEC_ASN1_TAGNUM_MASK)) || andre@0: (SEC_ASN1_SET == (kind & SEC_ASN1_TAGNUM_MASK)) ) andre@0: { andre@0: rv = DecodeGroup(dest, templateEntry, &temp , arena); andre@0: } andre@0: else andre@0: { andre@0: /* a group can only be a SET OF or SEQUENCE OF */ andre@0: PORT_SetError(SEC_ERROR_BAD_TEMPLATE); andre@0: rv = SECFailure; andre@0: } andre@0: } andre@0: else andre@0: if (SEC_ASN1_SEQUENCE == (kind & SEC_ASN1_TAGNUM_MASK)) andre@0: { andre@0: /* plain SEQUENCE */ andre@0: rv = DecodeSequence(dest, templateEntry, &temp , arena); andre@0: } andre@0: else andre@0: { andre@0: /* handle all other types as "save" */ andre@0: /* we should only get here for primitive universal types */ andre@0: SECItem newtemp = temp; andre@0: rv = GetItem(&newtemp, &temp, PR_FALSE); andre@0: save = PR_TRUE; andre@0: if ((SECSuccess == rv) && andre@0: SEC_ASN1_UNIVERSAL == (kind & SEC_ASN1_CLASS_MASK)) andre@0: { andre@0: unsigned long tagnum = kind & SEC_ASN1_TAGNUM_MASK; andre@0: if ( temp.len == 0 && (tagnum == SEC_ASN1_BOOLEAN || andre@0: tagnum == SEC_ASN1_INTEGER || andre@0: tagnum == SEC_ASN1_BIT_STRING || andre@0: tagnum == SEC_ASN1_OBJECT_ID || andre@0: tagnum == SEC_ASN1_ENUMERATED || andre@0: tagnum == SEC_ASN1_UTC_TIME || andre@0: tagnum == SEC_ASN1_GENERALIZED_TIME) ) andre@0: { andre@0: /* these types MUST have at least one content octet */ andre@0: PORT_SetError(SEC_ERROR_BAD_DER); andre@0: rv = SECFailure; andre@0: } andre@0: else andre@0: switch (tagnum) andre@0: { andre@0: /* special cases of primitive types */ andre@0: case SEC_ASN1_INTEGER: andre@0: { andre@0: /* remove leading zeroes if the caller requested andre@0: siUnsignedInteger andre@0: This is to allow RSA key operations to work */ andre@0: SECItem* destItem = (SECItem*) ((char*)dest + andre@0: templateEntry->offset); andre@0: if (destItem && (siUnsignedInteger == destItem->type)) andre@0: { andre@0: while (temp.len > 1 && temp.data[0] == 0) andre@0: { /* leading 0 */ andre@0: temp.data++; andre@0: temp.len--; andre@0: } andre@0: } andre@0: break; andre@0: } andre@0: andre@0: case SEC_ASN1_BIT_STRING: andre@0: { andre@0: /* change the length in the SECItem to be the number andre@0: of bits */ andre@0: temp.len = (temp.len-1)*8 - (temp.data[0] & 0x7); andre@0: temp.data++; andre@0: break; andre@0: } andre@0: andre@0: default: andre@0: { andre@0: break; andre@0: } andre@0: } andre@0: } andre@0: } andre@0: } andre@0: andre@0: if ((SECSuccess == rv) && (PR_TRUE == save)) andre@0: { andre@0: SECItem* destItem = (SECItem*) ((char*)dest + templateEntry->offset); andre@0: if (destItem) andre@0: { andre@0: /* we leave the type alone in the destination SECItem. andre@0: If part of the destination was allocated by the decoder, in andre@0: cases of POINTER, SET OF and SEQUENCE OF, then type is set to andre@0: siBuffer due to the use of PORT_ArenaZAlloc*/ andre@0: destItem->data = temp.len ? temp.data : NULL; andre@0: destItem->len = temp.len; andre@0: } andre@0: else andre@0: { andre@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); andre@0: rv = SECFailure; andre@0: } andre@0: } andre@0: andre@0: if (PR_TRUE == pop) andre@0: { andre@0: /* we don't want to move ahead, so restore the position */ andre@0: *src = mark; andre@0: } andre@0: return rv; andre@0: } andre@0: andre@0: /* the function below is the public one */ andre@0: andre@0: SECStatus SEC_QuickDERDecodeItem(PLArenaPool* arena, void* dest, andre@0: const SEC_ASN1Template* templateEntry, andre@0: const SECItem* src) andre@0: { andre@0: SECStatus rv = SECSuccess; andre@0: SECItem newsrc; andre@0: andre@0: if (!arena || !templateEntry || !src) andre@0: { andre@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); andre@0: rv = SECFailure; andre@0: } andre@0: andre@0: if (SECSuccess == rv) andre@0: { andre@0: newsrc = *src; andre@0: rv = DecodeItem(dest, templateEntry, &newsrc, arena, PR_TRUE); andre@0: if (SECSuccess == rv && newsrc.len) andre@0: { andre@0: rv = SECFailure; andre@0: PORT_SetError(SEC_ERROR_EXTRA_INPUT); andre@0: } andre@0: } andre@0: andre@0: return rv; andre@0: } andre@0: