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: * Support for ENcoding ASN.1 data based on BER/DER (Basic/Distinguished andre@0: * Encoding Rules). andre@0: */ andre@0: andre@0: #include "secasn1.h" andre@0: andre@0: typedef enum { andre@0: beforeHeader, andre@0: duringContents, andre@0: duringGroup, andre@0: duringSequence, andre@0: afterContents, andre@0: afterImplicit, andre@0: afterInline, andre@0: afterPointer, andre@0: afterChoice, andre@0: notInUse andre@0: } sec_asn1e_parse_place; andre@0: andre@0: typedef enum { andre@0: allDone, andre@0: encodeError, andre@0: keepGoing, andre@0: needBytes andre@0: } sec_asn1e_parse_status; andre@0: andre@0: typedef enum { andre@0: hdr_normal = 0, /* encode header normally */ andre@0: hdr_any = 1, /* header already encoded in content */ andre@0: hdr_decoder = 2, /* template only used by decoder. skip it. */ andre@0: hdr_optional = 3, /* optional component, to be omitted */ andre@0: hdr_placeholder = 4 /* place holder for from_buf content */ andre@0: } sec_asn1e_hdr_encoding; andre@0: andre@0: typedef struct sec_asn1e_state_struct { andre@0: SEC_ASN1EncoderContext *top; andre@0: const SEC_ASN1Template *theTemplate; andre@0: void *src; andre@0: andre@0: struct sec_asn1e_state_struct *parent; /* aka prev */ andre@0: struct sec_asn1e_state_struct *child; /* aka next */ andre@0: andre@0: sec_asn1e_parse_place place; /* where we are in encoding process */ andre@0: andre@0: /* andre@0: * XXX explain the next fields as clearly as possible... andre@0: */ andre@0: unsigned char tag_modifiers; andre@0: unsigned char tag_number; andre@0: unsigned long underlying_kind; andre@0: andre@0: int depth; andre@0: andre@0: PRBool isExplicit, /* we are handling an isExplicit header */ andre@0: indefinite, /* need end-of-contents */ andre@0: is_string, /* encoding a simple string or an ANY */ andre@0: may_stream, /* when streaming, do indefinite encoding */ andre@0: optional, /* omit field if it has no contents */ andre@0: disallowStreaming; /* disallow streaming in all sub-templates */ andre@0: } sec_asn1e_state; andre@0: andre@0: /* andre@0: * An "outsider" will have an opaque pointer to this, created by calling andre@0: * SEC_ASN1EncoderStart(). It will be passed back in to all subsequent andre@0: * calls to SEC_ASN1EncoderUpdate() and related routines, and when done andre@0: * it is passed to SEC_ASN1EncoderFinish(). andre@0: */ andre@0: struct sec_EncoderContext_struct { andre@0: PLArenaPool *our_pool; /* for our internal allocs */ andre@0: andre@0: sec_asn1e_state *current; andre@0: sec_asn1e_parse_status status; andre@0: andre@0: PRBool streaming; andre@0: PRBool from_buf; andre@0: andre@0: SEC_ASN1NotifyProc notify_proc; /* call before/after handling field */ andre@0: void *notify_arg; /* argument to notify_proc */ andre@0: PRBool during_notify; /* true during call to notify_proc */ andre@0: andre@0: SEC_ASN1WriteProc output_proc; /* pass encoded bytes to this */ andre@0: void *output_arg; /* argument to that function */ andre@0: }; andre@0: andre@0: andre@0: static sec_asn1e_state * andre@0: sec_asn1e_push_state (SEC_ASN1EncoderContext *cx, andre@0: const SEC_ASN1Template *theTemplate, andre@0: const void *src, PRBool new_depth) andre@0: { andre@0: sec_asn1e_state *state, *new_state; andre@0: andre@0: state = cx->current; andre@0: andre@0: new_state = (sec_asn1e_state*)PORT_ArenaZAlloc (cx->our_pool, andre@0: sizeof(*new_state)); andre@0: if (new_state == NULL) { andre@0: cx->status = encodeError; andre@0: return NULL; andre@0: } andre@0: andre@0: new_state->top = cx; andre@0: new_state->parent = state; andre@0: new_state->theTemplate = theTemplate; andre@0: new_state->place = notInUse; andre@0: if (src != NULL) andre@0: new_state->src = (char *)src + theTemplate->offset; andre@0: andre@0: if (state != NULL) { andre@0: new_state->depth = state->depth; andre@0: if (new_depth) andre@0: new_state->depth++; andre@0: state->child = new_state; andre@0: } andre@0: andre@0: cx->current = new_state; andre@0: return new_state; andre@0: } andre@0: andre@0: andre@0: static void andre@0: sec_asn1e_scrub_state (sec_asn1e_state *state) andre@0: { andre@0: /* andre@0: * Some default "scrubbing". andre@0: * XXX right set of initializations? andre@0: */ andre@0: state->place = beforeHeader; andre@0: state->indefinite = PR_FALSE; andre@0: } andre@0: andre@0: andre@0: static void andre@0: sec_asn1e_notify_before (SEC_ASN1EncoderContext *cx, void *src, int depth) andre@0: { andre@0: if (cx->notify_proc == NULL) andre@0: return; andre@0: andre@0: cx->during_notify = PR_TRUE; andre@0: (* cx->notify_proc) (cx->notify_arg, PR_TRUE, src, depth); andre@0: cx->during_notify = PR_FALSE; andre@0: } andre@0: andre@0: andre@0: static void andre@0: sec_asn1e_notify_after (SEC_ASN1EncoderContext *cx, void *src, int depth) andre@0: { andre@0: if (cx->notify_proc == NULL) andre@0: return; andre@0: andre@0: cx->during_notify = PR_TRUE; andre@0: (* cx->notify_proc) (cx->notify_arg, PR_FALSE, src, depth); andre@0: cx->during_notify = PR_FALSE; andre@0: } andre@0: andre@0: andre@0: static sec_asn1e_state * andre@0: sec_asn1e_init_state_based_on_template (sec_asn1e_state *state) andre@0: { andre@0: PRBool isExplicit, is_string, may_stream, optional, universal; andre@0: PRBool disallowStreaming; andre@0: unsigned char tag_modifiers; andre@0: unsigned long encode_kind, under_kind; andre@0: unsigned long tag_number; andre@0: PRBool isInline = PR_FALSE; andre@0: andre@0: andre@0: encode_kind = state->theTemplate->kind; andre@0: andre@0: universal = ((encode_kind & SEC_ASN1_CLASS_MASK) == SEC_ASN1_UNIVERSAL) andre@0: ? PR_TRUE : PR_FALSE; andre@0: andre@0: isExplicit = (encode_kind & SEC_ASN1_EXPLICIT) ? PR_TRUE : PR_FALSE; andre@0: encode_kind &= ~SEC_ASN1_EXPLICIT; andre@0: andre@0: optional = (encode_kind & SEC_ASN1_OPTIONAL) ? PR_TRUE : PR_FALSE; andre@0: encode_kind &= ~SEC_ASN1_OPTIONAL; andre@0: andre@0: PORT_Assert (!(isExplicit && universal)); /* bad templates */ andre@0: andre@0: may_stream = (encode_kind & SEC_ASN1_MAY_STREAM) ? PR_TRUE : PR_FALSE; andre@0: encode_kind &= ~SEC_ASN1_MAY_STREAM; andre@0: andre@0: disallowStreaming = (encode_kind & SEC_ASN1_NO_STREAM) ? PR_TRUE : PR_FALSE; andre@0: encode_kind &= ~SEC_ASN1_NO_STREAM; andre@0: andre@0: /* Just clear this to get it out of the way; we do not need it here */ andre@0: encode_kind &= ~SEC_ASN1_DYNAMIC; andre@0: andre@0: if( encode_kind & SEC_ASN1_CHOICE ) { andre@0: under_kind = SEC_ASN1_CHOICE; andre@0: } else if ((encode_kind & (SEC_ASN1_POINTER | SEC_ASN1_INLINE)) || andre@0: (!universal && !isExplicit)) { andre@0: const SEC_ASN1Template *subt; andre@0: void *src = NULL; andre@0: andre@0: PORT_Assert ((encode_kind & (SEC_ASN1_ANY | SEC_ASN1_SKIP)) == 0); andre@0: andre@0: sec_asn1e_scrub_state (state); andre@0: andre@0: if (encode_kind & SEC_ASN1_POINTER) { andre@0: src = *(void **)state->src; andre@0: state->place = afterPointer; andre@0: andre@0: if (src == NULL) { andre@0: /* andre@0: * If this is optional, but NULL, then the field does andre@0: * not need to be encoded. In this case we are done; andre@0: * we do not want to push a subtemplate. andre@0: */ andre@0: if (optional) andre@0: return state; andre@0: andre@0: /* andre@0: * XXX this is an error; need to figure out andre@0: * how to handle this andre@0: */ andre@0: } andre@0: } else { andre@0: src = state->src; andre@0: if (encode_kind & SEC_ASN1_INLINE) { andre@0: /* check that there are no extraneous bits */ andre@0: /* PORT_Assert (encode_kind == SEC_ASN1_INLINE && !optional); */ andre@0: state->place = afterInline; andre@0: isInline = PR_TRUE; andre@0: } else { andre@0: /* andre@0: * Save the tag modifiers and tag number here before moving andre@0: * on to the next state in case this is a member of a andre@0: * SEQUENCE OF andre@0: */ andre@0: state->tag_modifiers = (unsigned char) andre@0: (encode_kind & (SEC_ASN1_TAG_MASK & ~SEC_ASN1_TAGNUM_MASK)); andre@0: state->tag_number = (unsigned char) andre@0: (encode_kind & SEC_ASN1_TAGNUM_MASK); andre@0: andre@0: state->place = afterImplicit; andre@0: state->optional = optional; andre@0: } andre@0: } andre@0: andre@0: subt = SEC_ASN1GetSubtemplate (state->theTemplate, state->src, PR_TRUE); andre@0: if (isInline && optional) { andre@0: /* we only handle a very limited set of optional inline cases at andre@0: this time */ andre@0: if (PR_FALSE != SEC_ASN1IsTemplateSimple(subt)) { andre@0: /* we now know that the target is a SECItem*, so we can check andre@0: if the source contains one */ andre@0: SECItem* target = (SECItem*)state->src; andre@0: if (!target || !target->data || !target->len) { andre@0: /* no valid data to encode subtemplate */ andre@0: return state; andre@0: } andre@0: } else { andre@0: PORT_Assert(0); /* complex templates are not handled as andre@0: inline optional */ andre@0: } andre@0: } andre@0: state = sec_asn1e_push_state (state->top, subt, src, PR_FALSE); andre@0: if (state == NULL) andre@0: return state; andre@0: andre@0: if (universal) { andre@0: /* andre@0: * This is a POINTER or INLINE; just init based on that andre@0: * and we are done. andre@0: */ andre@0: return sec_asn1e_init_state_based_on_template (state); andre@0: } andre@0: andre@0: /* andre@0: * This is an implicit, non-universal (meaning, application-private andre@0: * or context-specific) field. This results in a "magic" tag but andre@0: * encoding based on the underlying type. We pushed a new state andre@0: * that is based on the subtemplate (the underlying type), but andre@0: * now we will sort of alias it to give it some of our properties andre@0: * (tag, optional status, etc.). andre@0: * andre@0: * NB: ALL the following flags in the subtemplate are disallowed andre@0: * and/or ignored: EXPLICIT, OPTIONAL, INNER, INLINE, POINTER. andre@0: */ andre@0: andre@0: under_kind = state->theTemplate->kind; andre@0: if ((under_kind & SEC_ASN1_MAY_STREAM) && !disallowStreaming) { andre@0: may_stream = PR_TRUE; andre@0: } andre@0: under_kind &= ~(SEC_ASN1_MAY_STREAM | SEC_ASN1_DYNAMIC); andre@0: } else { andre@0: under_kind = encode_kind; andre@0: } andre@0: andre@0: /* andre@0: * Sanity check that there are no unwanted bits marked in under_kind. andre@0: * These bits were either removed above (after we recorded them) or andre@0: * they simply should not be found (signalling a bad/broken template). andre@0: * XXX is this the right set of bits to test here? (i.e. need to add andre@0: * or remove any?) andre@0: */ andre@0: #define UNEXPECTED_FLAGS \ andre@0: (SEC_ASN1_EXPLICIT | SEC_ASN1_OPTIONAL | SEC_ASN1_SKIP | SEC_ASN1_INNER | \ andre@0: SEC_ASN1_DYNAMIC | SEC_ASN1_MAY_STREAM | SEC_ASN1_INLINE | SEC_ASN1_POINTER) andre@0: andre@0: PORT_Assert ((under_kind & UNEXPECTED_FLAGS) == 0); andre@0: under_kind &= ~UNEXPECTED_FLAGS; andre@0: #undef UNEXPECTED_FLAGS andre@0: andre@0: if (encode_kind & SEC_ASN1_ANY) { andre@0: PORT_Assert (encode_kind == under_kind); andre@0: tag_modifiers = 0; andre@0: tag_number = 0; andre@0: is_string = PR_TRUE; andre@0: } else { andre@0: tag_modifiers = (unsigned char) andre@0: (encode_kind & (SEC_ASN1_TAG_MASK & ~SEC_ASN1_TAGNUM_MASK)); andre@0: /* andre@0: * XXX This assumes only single-octet identifiers. To handle andre@0: * the HIGH TAG form we would need to do some more work, especially andre@0: * in how to specify them in the template, because right now we andre@0: * do not provide a way to specify more *tag* bits in encode_kind. andre@0: */ andre@0: tag_number = encode_kind & SEC_ASN1_TAGNUM_MASK; andre@0: andre@0: is_string = PR_FALSE; andre@0: switch (under_kind & SEC_ASN1_TAGNUM_MASK) { andre@0: case SEC_ASN1_SET: andre@0: /* andre@0: * XXX A plain old SET (as opposed to a SET OF) is not implemented. andre@0: * If it ever is, remove this assert... andre@0: */ andre@0: PORT_Assert ((under_kind & SEC_ASN1_GROUP) != 0); andre@0: /* fallthru */ andre@0: case SEC_ASN1_SEQUENCE: andre@0: tag_modifiers |= SEC_ASN1_CONSTRUCTED; andre@0: break; andre@0: case SEC_ASN1_BIT_STRING: andre@0: case SEC_ASN1_BMP_STRING: andre@0: case SEC_ASN1_GENERALIZED_TIME: andre@0: case SEC_ASN1_IA5_STRING: andre@0: case SEC_ASN1_OCTET_STRING: andre@0: case SEC_ASN1_PRINTABLE_STRING: andre@0: case SEC_ASN1_T61_STRING: andre@0: case SEC_ASN1_UNIVERSAL_STRING: andre@0: case SEC_ASN1_UTC_TIME: andre@0: case SEC_ASN1_UTF8_STRING: andre@0: case SEC_ASN1_VISIBLE_STRING: andre@0: /* andre@0: * We do not yet know if we will be constructing the string, andre@0: * so we have to wait to do this final tag modification. andre@0: */ andre@0: is_string = PR_TRUE; andre@0: break; andre@0: } andre@0: } andre@0: andre@0: state->tag_modifiers = tag_modifiers; andre@0: state->tag_number = (unsigned char)tag_number; andre@0: state->underlying_kind = under_kind; andre@0: state->isExplicit = isExplicit; andre@0: state->may_stream = may_stream; andre@0: state->is_string = is_string; andre@0: state->optional = optional; andre@0: state->disallowStreaming = disallowStreaming; andre@0: andre@0: sec_asn1e_scrub_state (state); andre@0: andre@0: return state; andre@0: } andre@0: andre@0: andre@0: static void andre@0: sec_asn1e_write_part (sec_asn1e_state *state, andre@0: const char *buf, unsigned long len, andre@0: SEC_ASN1EncodingPart part) andre@0: { andre@0: SEC_ASN1EncoderContext *cx; andre@0: andre@0: cx = state->top; andre@0: (* cx->output_proc) (cx->output_arg, buf, len, state->depth, part); andre@0: } andre@0: andre@0: andre@0: /* andre@0: * XXX This assumes only single-octet identifiers. To handle andre@0: * the HIGH TAG form we would need to modify this interface and andre@0: * teach it to properly encode the special form. andre@0: */ andre@0: static void andre@0: sec_asn1e_write_identifier_bytes (sec_asn1e_state *state, unsigned char value) andre@0: { andre@0: char byte; andre@0: andre@0: byte = (char) value; andre@0: sec_asn1e_write_part (state, &byte, 1, SEC_ASN1_Identifier); andre@0: } andre@0: andre@0: int andre@0: SEC_ASN1EncodeLength(unsigned char *buf,int value) { andre@0: int lenlen; andre@0: andre@0: lenlen = SEC_ASN1LengthLength (value); andre@0: if (lenlen == 1) { andre@0: buf[0] = value; andre@0: } else { andre@0: int i; andre@0: andre@0: i = lenlen - 1; andre@0: buf[0] = 0x80 | i; andre@0: while (i) { andre@0: buf[i--] = value; andre@0: value >>= 8; andre@0: } andre@0: PORT_Assert (value == 0); andre@0: } andre@0: return lenlen; andre@0: } andre@0: andre@0: static void andre@0: sec_asn1e_write_length_bytes (sec_asn1e_state *state, unsigned long value, andre@0: PRBool indefinite) andre@0: { andre@0: int lenlen; andre@0: unsigned char buf[sizeof(unsigned long) + 1]; andre@0: andre@0: if (indefinite) { andre@0: PORT_Assert (value == 0); andre@0: buf[0] = 0x80; andre@0: lenlen = 1; andre@0: } else { andre@0: lenlen = SEC_ASN1EncodeLength(buf,value); andre@0: } andre@0: andre@0: sec_asn1e_write_part (state, (char *) buf, lenlen, SEC_ASN1_Length); andre@0: } andre@0: andre@0: andre@0: static void andre@0: sec_asn1e_write_contents_bytes (sec_asn1e_state *state, andre@0: const char *buf, unsigned long len) andre@0: { andre@0: sec_asn1e_write_part (state, buf, len, SEC_ASN1_Contents); andre@0: } andre@0: andre@0: andre@0: static void andre@0: sec_asn1e_write_end_of_contents_bytes (sec_asn1e_state *state) andre@0: { andre@0: const char eoc[2] = {0, 0}; andre@0: andre@0: sec_asn1e_write_part (state, eoc, 2, SEC_ASN1_EndOfContents); andre@0: } andre@0: andre@0: static int andre@0: sec_asn1e_which_choice andre@0: ( andre@0: void *src, andre@0: const SEC_ASN1Template *theTemplate andre@0: ) andre@0: { andre@0: int rv; andre@0: unsigned int which = *(unsigned int *)src; andre@0: andre@0: for( rv = 1, theTemplate++; theTemplate->kind != 0; rv++, theTemplate++ ) { andre@0: if( which == theTemplate->size ) { andre@0: return rv; andre@0: } andre@0: } andre@0: andre@0: return 0; andre@0: } andre@0: andre@0: static unsigned long andre@0: sec_asn1e_contents_length (const SEC_ASN1Template *theTemplate, void *src, andre@0: PRBool disallowStreaming, PRBool insideIndefinite, andre@0: sec_asn1e_hdr_encoding *pHdrException) andre@0: { andre@0: unsigned long encode_kind, underlying_kind; andre@0: PRBool isExplicit, optional, universal, may_stream; andre@0: unsigned long len; andre@0: andre@0: /* andre@0: * This function currently calculates the length in all cases andre@0: * except the following: when writing out the contents of a andre@0: * template that belongs to a state where it was a sub-template andre@0: * with the SEC_ASN1_MAY_STREAM bit set and it's parent had the andre@0: * optional bit set. The information that the parent is optional andre@0: * and that we should return the length of 0 when that length is andre@0: * present since that means the optional field is no longer present. andre@0: * So we add the disallowStreaming flag which is passed in when andre@0: * writing the contents, but for all recursive calls to andre@0: * sec_asn1e_contents_length, we pass PR_FALSE, because this andre@0: * function correctly calculates the length for children templates andre@0: * from that point on. Confused yet? At least you didn't have andre@0: * to figure it out. ;) -javi andre@0: */ andre@0: encode_kind = theTemplate->kind; andre@0: andre@0: universal = ((encode_kind & SEC_ASN1_CLASS_MASK) == SEC_ASN1_UNIVERSAL) andre@0: ? PR_TRUE : PR_FALSE; andre@0: andre@0: isExplicit = (encode_kind & SEC_ASN1_EXPLICIT) ? PR_TRUE : PR_FALSE; andre@0: encode_kind &= ~SEC_ASN1_EXPLICIT; andre@0: andre@0: optional = (encode_kind & SEC_ASN1_OPTIONAL) ? PR_TRUE : PR_FALSE; andre@0: encode_kind &= ~SEC_ASN1_OPTIONAL; andre@0: andre@0: PORT_Assert (!(isExplicit && universal)); /* bad templates */ andre@0: andre@0: may_stream = (encode_kind & SEC_ASN1_MAY_STREAM) ? PR_TRUE : PR_FALSE; andre@0: encode_kind &= ~SEC_ASN1_MAY_STREAM; andre@0: andre@0: /* Just clear this to get it out of the way; we do not need it here */ andre@0: encode_kind &= ~SEC_ASN1_DYNAMIC; andre@0: andre@0: if (encode_kind & SEC_ASN1_NO_STREAM) { andre@0: disallowStreaming = PR_TRUE; andre@0: } andre@0: encode_kind &= ~SEC_ASN1_NO_STREAM; andre@0: andre@0: if (encode_kind & SEC_ASN1_CHOICE) { andre@0: void *src2; andre@0: int indx = sec_asn1e_which_choice(src, theTemplate); andre@0: if (0 == indx) { andre@0: /* XXX set an error? "choice not found" */ andre@0: /* state->top->status = encodeError; */ andre@0: return 0; andre@0: } andre@0: andre@0: src2 = (void *) andre@0: ((char *)src - theTemplate->offset + theTemplate[indx].offset); andre@0: andre@0: return sec_asn1e_contents_length(&theTemplate[indx], src2, andre@0: disallowStreaming, insideIndefinite, andre@0: pHdrException); andre@0: } andre@0: andre@0: if ((encode_kind & (SEC_ASN1_POINTER | SEC_ASN1_INLINE)) || !universal) { andre@0: /* XXX any bits we want to disallow (PORT_Assert against) here? */ andre@0: theTemplate = SEC_ASN1GetSubtemplate (theTemplate, src, PR_TRUE); andre@0: if (encode_kind & SEC_ASN1_POINTER) { andre@0: src = *(void **)src; andre@0: if (src == NULL) { andre@0: *pHdrException = optional ? hdr_optional : hdr_normal; andre@0: return 0; andre@0: } andre@0: } else if (encode_kind & SEC_ASN1_INLINE) { andre@0: /* check that there are no extraneous bits */ andre@0: if (optional) { andre@0: if (PR_FALSE != SEC_ASN1IsTemplateSimple(theTemplate)) { andre@0: /* we now know that the target is a SECItem*, so we can check andre@0: if the source contains one */ andre@0: SECItem* target = (SECItem*)src; andre@0: if (!target || !target->data || !target->len) { andre@0: /* no valid data to encode subtemplate */ andre@0: *pHdrException = hdr_optional; andre@0: return 0; andre@0: } andre@0: } else { andre@0: PORT_Assert(0); /* complex templates not handled as inline andre@0: optional */ andre@0: } andre@0: } andre@0: } andre@0: andre@0: src = (char *)src + theTemplate->offset; andre@0: andre@0: /* recurse to find the length of the subtemplate */ andre@0: len = sec_asn1e_contents_length (theTemplate, src, disallowStreaming, andre@0: insideIndefinite, pHdrException); andre@0: if (len == 0 && optional) { andre@0: *pHdrException = hdr_optional; andre@0: } else if (isExplicit) { andre@0: if (*pHdrException == hdr_any) { andre@0: /* *we* do not want to add in a header, andre@0: ** but our caller still does. andre@0: */ andre@0: *pHdrException = hdr_normal; andre@0: } else if (*pHdrException == hdr_normal) { andre@0: /* if the inner content exists, our length is andre@0: * len(identifier) + len(length) + len(innercontent) andre@0: * XXX we currently assume len(identifier) == 1; andre@0: * to support a high-tag-number this would need to be smarter. andre@0: */ andre@0: len += 1 + SEC_ASN1LengthLength (len); andre@0: } andre@0: } andre@0: return len; andre@0: } andre@0: underlying_kind = encode_kind; andre@0: andre@0: /* This is only used in decoding; it plays no part in encoding. */ andre@0: if (underlying_kind & SEC_ASN1_SAVE) { andre@0: /* check that there are no extraneous bits */ andre@0: PORT_Assert (underlying_kind == SEC_ASN1_SAVE); andre@0: *pHdrException = hdr_decoder; andre@0: return 0; andre@0: } andre@0: andre@0: #define UNEXPECTED_FLAGS \ andre@0: (SEC_ASN1_EXPLICIT | SEC_ASN1_OPTIONAL | SEC_ASN1_INLINE | SEC_ASN1_POINTER |\ andre@0: SEC_ASN1_DYNAMIC | SEC_ASN1_MAY_STREAM | SEC_ASN1_SAVE | SEC_ASN1_SKIP) andre@0: andre@0: /* Having any of these bits is not expected here... */ andre@0: PORT_Assert ((underlying_kind & UNEXPECTED_FLAGS) == 0); andre@0: underlying_kind &= ~UNEXPECTED_FLAGS; andre@0: #undef UNEXPECTED_FLAGS andre@0: andre@0: if (underlying_kind & SEC_ASN1_CHOICE) { andre@0: void *src2; andre@0: int indx = sec_asn1e_which_choice(src, theTemplate); andre@0: if (0 == indx) { andre@0: /* XXX set an error? "choice not found" */ andre@0: /* state->top->status = encodeError; */ andre@0: return 0; andre@0: } andre@0: andre@0: src2 = (void *) andre@0: ((char *)src - theTemplate->offset + theTemplate[indx].offset); andre@0: len = sec_asn1e_contents_length(&theTemplate[indx], src2, andre@0: disallowStreaming, insideIndefinite, andre@0: pHdrException); andre@0: } else { andre@0: switch (underlying_kind) { andre@0: case SEC_ASN1_SEQUENCE_OF: andre@0: case SEC_ASN1_SET_OF: andre@0: { andre@0: const SEC_ASN1Template *tmpt; andre@0: void *sub_src; andre@0: unsigned long sub_len; andre@0: void **group; andre@0: andre@0: len = 0; andre@0: andre@0: group = *(void ***)src; andre@0: if (group == NULL) andre@0: break; andre@0: andre@0: tmpt = SEC_ASN1GetSubtemplate (theTemplate, src, PR_TRUE); andre@0: andre@0: for (; *group != NULL; group++) { andre@0: sub_src = (char *)(*group) + tmpt->offset; andre@0: sub_len = sec_asn1e_contents_length (tmpt, sub_src, andre@0: disallowStreaming, andre@0: insideIndefinite, andre@0: pHdrException); andre@0: len += sub_len; andre@0: /* andre@0: * XXX The 1 below is the presumed length of the identifier; andre@0: * to support a high-tag-number this would need to be smarter. andre@0: */ andre@0: if (*pHdrException == hdr_normal) andre@0: len += 1 + SEC_ASN1LengthLength (sub_len); andre@0: } andre@0: } andre@0: break; andre@0: andre@0: case SEC_ASN1_SEQUENCE: andre@0: case SEC_ASN1_SET: andre@0: { andre@0: const SEC_ASN1Template *tmpt; andre@0: void *sub_src; andre@0: unsigned long sub_len; andre@0: andre@0: len = 0; andre@0: for (tmpt = theTemplate + 1; tmpt->kind; tmpt++) { andre@0: sub_src = (char *)src + tmpt->offset; andre@0: sub_len = sec_asn1e_contents_length (tmpt, sub_src, andre@0: disallowStreaming, andre@0: insideIndefinite, andre@0: pHdrException); andre@0: len += sub_len; andre@0: /* andre@0: * XXX The 1 below is the presumed length of the identifier; andre@0: * to support a high-tag-number this would need to be smarter. andre@0: */ andre@0: if (*pHdrException == hdr_normal) andre@0: len += 1 + SEC_ASN1LengthLength (sub_len); andre@0: } andre@0: } andre@0: break; andre@0: andre@0: case SEC_ASN1_BIT_STRING: andre@0: /* convert bit length to byte */ andre@0: len = (((SECItem *)src)->len + 7) >> 3; andre@0: /* bit string contents involve an extra octet */ andre@0: if (len) andre@0: len++; andre@0: break; andre@0: andre@0: case SEC_ASN1_INTEGER: andre@0: /* ASN.1 INTEGERs are signed. andre@0: * If the source is an unsigned integer, the encoder will need andre@0: * to handle the conversion here. andre@0: */ andre@0: { andre@0: unsigned char *buf = ((SECItem *)src)->data; andre@0: SECItemType integerType = ((SECItem *)src)->type; andre@0: len = ((SECItem *)src)->len; andre@0: while (len > 0) { andre@0: if (*buf != 0) { andre@0: if (*buf & 0x80 && integerType == siUnsignedInteger) { andre@0: len++; /* leading zero needed to make number signed */ andre@0: } andre@0: break; /* reached beginning of number */ andre@0: } andre@0: if (len == 1) { andre@0: break; /* the number 0 */ andre@0: } andre@0: if (buf[1] & 0x80) { andre@0: break; /* leading zero already present */ andre@0: } andre@0: /* extraneous leading zero, keep going */ andre@0: buf++; andre@0: len--; andre@0: } andre@0: } andre@0: break; andre@0: andre@0: default: andre@0: len = ((SECItem *)src)->len; andre@0: break; andre@0: } /* end switch */ andre@0: andre@0: #ifndef WHAT_PROBLEM_DOES_THIS_SOLVE andre@0: /* if we're streaming, we may have a secitem w/len 0 as placeholder */ andre@0: if (!len && insideIndefinite && may_stream && !disallowStreaming) { andre@0: len = 1; andre@0: } andre@0: #endif andre@0: } /* end else */ andre@0: andre@0: if (len == 0 && optional) andre@0: *pHdrException = hdr_optional; andre@0: else if (underlying_kind == SEC_ASN1_ANY) andre@0: *pHdrException = hdr_any; andre@0: else andre@0: *pHdrException = hdr_normal; andre@0: andre@0: return len; andre@0: } andre@0: andre@0: andre@0: static void andre@0: sec_asn1e_write_header (sec_asn1e_state *state) andre@0: { andre@0: unsigned long contents_length; andre@0: unsigned char tag_number, tag_modifiers; andre@0: sec_asn1e_hdr_encoding hdrException = hdr_normal; andre@0: PRBool indefinite = PR_FALSE; andre@0: andre@0: PORT_Assert (state->place == beforeHeader); andre@0: andre@0: tag_number = state->tag_number; andre@0: tag_modifiers = state->tag_modifiers; andre@0: andre@0: if (state->underlying_kind == SEC_ASN1_ANY) { andre@0: state->place = duringContents; andre@0: return; andre@0: } andre@0: andre@0: if (state->underlying_kind & SEC_ASN1_CHOICE) { andre@0: int indx = sec_asn1e_which_choice(state->src, state->theTemplate); andre@0: if( 0 == indx ) { andre@0: /* XXX set an error? "choice not found" */ andre@0: state->top->status = encodeError; andre@0: return; andre@0: } andre@0: state->place = afterChoice; andre@0: state = sec_asn1e_push_state(state->top, &state->theTemplate[indx], andre@0: (char *)state->src - state->theTemplate->offset, andre@0: PR_TRUE); andre@0: if (state) { andre@0: /* andre@0: * Do the "before" field notification. andre@0: */ andre@0: sec_asn1e_notify_before (state->top, state->src, state->depth); andre@0: state = sec_asn1e_init_state_based_on_template (state); andre@0: } andre@0: return; andre@0: } andre@0: andre@0: /* The !isString test below is apparently intended to ensure that all andre@0: ** constructed types receive indefinite length encoding. andre@0: */ andre@0: indefinite = (PRBool) andre@0: (state->top->streaming && state->may_stream && andre@0: (state->top->from_buf || !state->is_string)); andre@0: andre@0: /* andre@0: * If we are doing a definite-length encoding, first we have to andre@0: * walk the data structure to calculate the entire contents length. andre@0: * If we are doing an indefinite-length encoding, we still need to andre@0: * know if the contents is: andre@0: * optional and to be omitted, or andre@0: * an ANY (header is pre-encoded), or andre@0: * a SAVE or some other kind of template used only by the decoder. andre@0: * So, we call this function either way. andre@0: */ andre@0: contents_length = sec_asn1e_contents_length (state->theTemplate, andre@0: state->src, andre@0: state->disallowStreaming, andre@0: indefinite, andre@0: &hdrException); andre@0: /* andre@0: * We might be told explicitly not to put out a header. andre@0: * But it can also be the case, via a pushed subtemplate, that andre@0: * sec_asn1e_contents_length could not know that this field is andre@0: * really optional. So check for that explicitly, too. andre@0: */ andre@0: if (hdrException != hdr_normal || andre@0: (contents_length == 0 && state->optional)) { andre@0: state->place = afterContents; andre@0: if (state->top->streaming && andre@0: state->may_stream && andre@0: state->top->from_buf) { andre@0: /* we did not find an optional indefinite string, so we andre@0: * don't encode it. However, if TakeFromBuf is on, we stop andre@0: * here anyway to give our caller a chance to intercept at the andre@0: * same point where we would stop if the field were present. andre@0: */ andre@0: state->top->status = needBytes; andre@0: } andre@0: return; andre@0: } andre@0: andre@0: if (indefinite) { andre@0: /* andre@0: * We need to put out an indefinite-length encoding. andre@0: * The only universal types that can be constructed are SETs, andre@0: * SEQUENCEs, and strings; so check that it is one of those, andre@0: * or that it is not universal (e.g. context-specific). andre@0: */ andre@0: state->indefinite = PR_TRUE; andre@0: PORT_Assert ((tag_number == SEC_ASN1_SET) andre@0: || (tag_number == SEC_ASN1_SEQUENCE) andre@0: || ((tag_modifiers & SEC_ASN1_CLASS_MASK) != 0) andre@0: || state->is_string); andre@0: tag_modifiers |= SEC_ASN1_CONSTRUCTED; andre@0: contents_length = 0; andre@0: } andre@0: andre@0: sec_asn1e_write_identifier_bytes (state, andre@0: (unsigned char)(tag_number | tag_modifiers)); andre@0: sec_asn1e_write_length_bytes (state, contents_length, state->indefinite); andre@0: andre@0: if (contents_length == 0 && !state->indefinite) { andre@0: /* andre@0: * If no real contents to encode, then we are done with this field. andre@0: */ andre@0: state->place = afterContents; andre@0: return; andre@0: } andre@0: andre@0: /* andre@0: * An EXPLICIT is nothing but an outer header, which we have already andre@0: * written. Now we need to do the inner header and contents. andre@0: */ andre@0: if (state->isExplicit) { andre@0: const SEC_ASN1Template *subt = andre@0: SEC_ASN1GetSubtemplate(state->theTemplate, state->src, PR_TRUE); andre@0: state->place = afterContents; andre@0: state = sec_asn1e_push_state (state->top, subt, state->src, PR_TRUE); andre@0: if (state != NULL) andre@0: state = sec_asn1e_init_state_based_on_template (state); andre@0: return; andre@0: } andre@0: andre@0: switch (state->underlying_kind) { andre@0: case SEC_ASN1_SET_OF: andre@0: case SEC_ASN1_SEQUENCE_OF: andre@0: /* andre@0: * We need to push a child to handle each member. andre@0: */ andre@0: { andre@0: void **group; andre@0: const SEC_ASN1Template *subt; andre@0: andre@0: group = *(void ***)state->src; andre@0: if (group == NULL || *group == NULL) { andre@0: /* andre@0: * Group is empty; we are done. andre@0: */ andre@0: state->place = afterContents; andre@0: return; andre@0: } andre@0: state->place = duringGroup; andre@0: subt = SEC_ASN1GetSubtemplate (state->theTemplate, state->src, andre@0: PR_TRUE); andre@0: state = sec_asn1e_push_state (state->top, subt, *group, PR_TRUE); andre@0: if (state != NULL) andre@0: state = sec_asn1e_init_state_based_on_template (state); andre@0: } andre@0: break; andre@0: andre@0: case SEC_ASN1_SEQUENCE: andre@0: case SEC_ASN1_SET: andre@0: /* andre@0: * We need to push a child to handle the individual fields. andre@0: */ andre@0: state->place = duringSequence; andre@0: state = sec_asn1e_push_state (state->top, state->theTemplate + 1, andre@0: state->src, PR_TRUE); andre@0: if (state != NULL) { andre@0: /* andre@0: * Do the "before" field notification. andre@0: */ andre@0: sec_asn1e_notify_before (state->top, state->src, state->depth); andre@0: state = sec_asn1e_init_state_based_on_template (state); andre@0: } andre@0: break; andre@0: andre@0: default: andre@0: /* andre@0: * I think we do not need to do anything else. andre@0: * XXX Correct? andre@0: */ andre@0: state->place = duringContents; andre@0: break; andre@0: } andre@0: } andre@0: andre@0: andre@0: static void andre@0: sec_asn1e_write_contents_from_buf (sec_asn1e_state *state, andre@0: const char *buf, unsigned long len) andre@0: { andre@0: PORT_Assert (state->place == duringContents); andre@0: PORT_Assert (state->top->from_buf); andre@0: PORT_Assert (state->may_stream && !state->disallowStreaming); andre@0: andre@0: /* andre@0: * Probably they just turned on "take from buf", but have not andre@0: * yet given us any bytes. If there is nothing in the buffer andre@0: * then we have nothing to do but return and wait. andre@0: */ andre@0: if (buf == NULL || len == 0) { andre@0: state->top->status = needBytes; andre@0: return; andre@0: } andre@0: /* andre@0: * We are streaming, reading from a passed-in buffer. andre@0: * This means we are encoding a simple string or an ANY. andre@0: * For the former, we need to put out a substring, with its andre@0: * own identifier and length. For an ANY, we just write it andre@0: * out as is (our caller is required to ensure that it andre@0: * is a properly encoded entity). andre@0: */ andre@0: PORT_Assert (state->is_string); /* includes ANY */ andre@0: if (state->underlying_kind != SEC_ASN1_ANY) { andre@0: unsigned char identifier; andre@0: andre@0: /* andre@0: * Create the identifier based on underlying_kind. We cannot andre@0: * use tag_number and tag_modifiers because this can be an andre@0: * implicitly encoded field. In that case, the underlying andre@0: * substrings *are* encoded with their real tag. andre@0: */ andre@0: identifier = (unsigned char) andre@0: (state->underlying_kind & SEC_ASN1_TAG_MASK); andre@0: /* andre@0: * The underlying kind should just be a simple string; there andre@0: * should be no bits like CONTEXT_SPECIFIC or CONSTRUCTED set. andre@0: */ andre@0: PORT_Assert ((identifier & SEC_ASN1_TAGNUM_MASK) == identifier); andre@0: /* andre@0: * Write out the tag and length for the substring. andre@0: */ andre@0: sec_asn1e_write_identifier_bytes (state, identifier); andre@0: if (state->underlying_kind == SEC_ASN1_BIT_STRING) { andre@0: char byte; andre@0: /* andre@0: * Assume we have a length in bytes but we need to output andre@0: * a proper bit string. This interface only works for bit andre@0: * strings that are full multiples of 8. If support for andre@0: * real, variable length bit strings is needed then the andre@0: * caller will have to know to pass in a bit length instead andre@0: * of a byte length and then this code will have to andre@0: * perform the encoding necessary (length written is length andre@0: * in bytes plus 1, and the first octet of string is the andre@0: * number of bits remaining between the end of the bit andre@0: * string and the next byte boundary). andre@0: */ andre@0: sec_asn1e_write_length_bytes (state, len + 1, PR_FALSE); andre@0: byte = 0; andre@0: sec_asn1e_write_contents_bytes (state, &byte, 1); andre@0: } else { andre@0: sec_asn1e_write_length_bytes (state, len, PR_FALSE); andre@0: } andre@0: } andre@0: sec_asn1e_write_contents_bytes (state, buf, len); andre@0: state->top->status = needBytes; andre@0: } andre@0: andre@0: static void andre@0: sec_asn1e_write_contents (sec_asn1e_state *state) andre@0: { andre@0: unsigned long len = 0; andre@0: andre@0: PORT_Assert (state->place == duringContents); andre@0: andre@0: switch (state->underlying_kind) { andre@0: case SEC_ASN1_SET: andre@0: case SEC_ASN1_SEQUENCE: andre@0: PORT_Assert (0); andre@0: break; andre@0: andre@0: case SEC_ASN1_BIT_STRING: andre@0: { andre@0: SECItem *item; andre@0: char rem; andre@0: andre@0: item = (SECItem *)state->src; andre@0: len = (item->len + 7) >> 3; andre@0: rem = (unsigned char)((len << 3) - item->len); /* remaining bits */ andre@0: sec_asn1e_write_contents_bytes (state, &rem, 1); andre@0: sec_asn1e_write_contents_bytes (state, (char *) item->data, len); andre@0: } andre@0: break; andre@0: andre@0: case SEC_ASN1_BMP_STRING: andre@0: /* The number of bytes must be divisable by 2 */ andre@0: if ((((SECItem *)state->src)->len) % 2) { andre@0: SEC_ASN1EncoderContext *cx; andre@0: andre@0: cx = state->top; andre@0: cx->status = encodeError; andre@0: break; andre@0: } andre@0: /* otherwise, fall through to write the content */ andre@0: goto process_string; andre@0: andre@0: case SEC_ASN1_UNIVERSAL_STRING: andre@0: /* The number of bytes must be divisable by 4 */ andre@0: if ((((SECItem *)state->src)->len) % 4) { andre@0: SEC_ASN1EncoderContext *cx; andre@0: andre@0: cx = state->top; andre@0: cx->status = encodeError; andre@0: break; andre@0: } andre@0: /* otherwise, fall through to write the content */ andre@0: goto process_string; andre@0: andre@0: case SEC_ASN1_INTEGER: andre@0: /* ASN.1 INTEGERs are signed. If the source is an unsigned andre@0: * integer, the encoder will need to handle the conversion here. andre@0: */ andre@0: { andre@0: unsigned int blen; andre@0: unsigned char *buf; andre@0: SECItemType integerType; andre@0: blen = ((SECItem *)state->src)->len; andre@0: buf = ((SECItem *)state->src)->data; andre@0: integerType = ((SECItem *)state->src)->type; andre@0: while (blen > 0) { andre@0: if (*buf & 0x80 && integerType == siUnsignedInteger) { andre@0: char zero = 0; /* write a leading 0 */ andre@0: sec_asn1e_write_contents_bytes(state, &zero, 1); andre@0: /* and then the remaining buffer */ andre@0: sec_asn1e_write_contents_bytes(state, andre@0: (char *)buf, blen); andre@0: break; andre@0: } andre@0: /* Check three possibilities: andre@0: * 1. No leading zeros, msb of MSB is not 1; andre@0: * 2. The number is zero itself; andre@0: * 3. Encoding a signed integer with a leading zero, andre@0: * keep the zero so that the number is positive. andre@0: */ andre@0: if (*buf != 0 || andre@0: blen == 1 || andre@0: (buf[1] & 0x80 && integerType != siUnsignedInteger) ) andre@0: { andre@0: sec_asn1e_write_contents_bytes(state, andre@0: (char *)buf, blen); andre@0: break; andre@0: } andre@0: /* byte is 0, continue */ andre@0: buf++; andre@0: blen--; andre@0: } andre@0: } andre@0: /* done with this content */ andre@0: break; andre@0: andre@0: process_string: andre@0: default: andre@0: { andre@0: SECItem *item; andre@0: andre@0: item = (SECItem *)state->src; andre@0: sec_asn1e_write_contents_bytes (state, (char *) item->data, andre@0: item->len); andre@0: } andre@0: break; andre@0: } andre@0: state->place = afterContents; andre@0: } andre@0: andre@0: /* andre@0: * We are doing a SET OF or SEQUENCE OF, and have just finished an item. andre@0: */ andre@0: static void andre@0: sec_asn1e_next_in_group (sec_asn1e_state *state) andre@0: { andre@0: sec_asn1e_state *child; andre@0: void **group; andre@0: void *member; andre@0: andre@0: PORT_Assert (state->place == duringGroup); andre@0: PORT_Assert (state->child != NULL); andre@0: andre@0: child = state->child; andre@0: andre@0: group = *(void ***)state->src; andre@0: andre@0: /* andre@0: * Find placement of current item. andre@0: */ andre@0: member = (char *)(state->child->src) - child->theTemplate->offset; andre@0: while (*group != member) andre@0: group++; andre@0: andre@0: /* andre@0: * Move forward to next item. andre@0: */ andre@0: group++; andre@0: if (*group == NULL) { andre@0: /* andre@0: * That was our last one; we are done now. andre@0: */ andre@0: child->place = notInUse; andre@0: state->place = afterContents; andre@0: return; andre@0: } andre@0: child->src = (char *)(*group) + child->theTemplate->offset; andre@0: andre@0: /* andre@0: * Re-"push" child. andre@0: */ andre@0: sec_asn1e_scrub_state (child); andre@0: state->top->current = child; andre@0: } andre@0: andre@0: andre@0: /* andre@0: * We are moving along through a sequence; move forward by one, andre@0: * (detecting end-of-sequence when it happens). andre@0: */ andre@0: static void andre@0: sec_asn1e_next_in_sequence (sec_asn1e_state *state) andre@0: { andre@0: sec_asn1e_state *child; andre@0: andre@0: PORT_Assert (state->place == duringSequence); andre@0: PORT_Assert (state->child != NULL); andre@0: andre@0: child = state->child; andre@0: andre@0: /* andre@0: * Do the "after" field notification. andre@0: */ andre@0: sec_asn1e_notify_after (state->top, child->src, child->depth); andre@0: andre@0: /* andre@0: * Move forward. andre@0: */ andre@0: child->theTemplate++; andre@0: if (child->theTemplate->kind == 0) { andre@0: /* andre@0: * We are done with this sequence. andre@0: */ andre@0: child->place = notInUse; andre@0: state->place = afterContents; andre@0: return; andre@0: } andre@0: andre@0: /* andre@0: * Reset state and push. andre@0: */ andre@0: andre@0: child->src = (char *)state->src + child->theTemplate->offset; andre@0: andre@0: /* andre@0: * Do the "before" field notification. andre@0: */ andre@0: sec_asn1e_notify_before (state->top, child->src, child->depth); andre@0: andre@0: state->top->current = child; andre@0: (void) sec_asn1e_init_state_based_on_template (child); andre@0: } andre@0: andre@0: andre@0: static void andre@0: sec_asn1e_after_contents (sec_asn1e_state *state) andre@0: { andre@0: PORT_Assert (state->place == afterContents); andre@0: andre@0: if (state->indefinite) andre@0: sec_asn1e_write_end_of_contents_bytes (state); andre@0: andre@0: /* andre@0: * Just make my parent be the current state. It will then clean andre@0: * up after me and free me (or reuse me). andre@0: */ andre@0: state->top->current = state->parent; andre@0: } andre@0: andre@0: andre@0: /* andre@0: * This function is called whether or not we are streaming; if we andre@0: * *are* streaming, our caller can also instruct us to take bytes andre@0: * from the passed-in buffer (at buf, for length len, which is likely andre@0: * bytes but could even mean bits if the current field is a bit string). andre@0: * If we have been so instructed, we will gobble up bytes from there andre@0: * (rather than from our src structure) and output them, and then andre@0: * we will just return, expecting to be called again -- either with andre@0: * more bytes or after our caller has instructed us that we are done andre@0: * (for now) with the buffer. andre@0: */ andre@0: SECStatus andre@0: SEC_ASN1EncoderUpdate (SEC_ASN1EncoderContext *cx, andre@0: const char *buf, unsigned long len) andre@0: { andre@0: sec_asn1e_state *state; andre@0: andre@0: if (cx->status == needBytes) { andre@0: cx->status = keepGoing; andre@0: } andre@0: andre@0: while (cx->status == keepGoing) { andre@0: state = cx->current; andre@0: switch (state->place) { andre@0: case beforeHeader: andre@0: sec_asn1e_write_header (state); andre@0: break; andre@0: case duringContents: andre@0: if (cx->from_buf) andre@0: sec_asn1e_write_contents_from_buf (state, buf, len); andre@0: else andre@0: sec_asn1e_write_contents (state); andre@0: break; andre@0: case duringGroup: andre@0: sec_asn1e_next_in_group (state); andre@0: break; andre@0: case duringSequence: andre@0: sec_asn1e_next_in_sequence (state); andre@0: break; andre@0: case afterContents: andre@0: sec_asn1e_after_contents (state); andre@0: break; andre@0: case afterImplicit: andre@0: case afterInline: andre@0: case afterPointer: andre@0: case afterChoice: andre@0: /* andre@0: * These states are more documentation than anything. andre@0: * They just need to force a pop. andre@0: */ andre@0: PORT_Assert (!state->indefinite); andre@0: state->place = afterContents; andre@0: break; andre@0: case notInUse: andre@0: default: andre@0: /* This is not an error, but rather a plain old BUG! */ andre@0: PORT_Assert (0); andre@0: cx->status = encodeError; andre@0: break; andre@0: } andre@0: andre@0: if (cx->status == encodeError) andre@0: break; andre@0: andre@0: /* It might have changed, so we have to update our local copy. */ andre@0: state = cx->current; andre@0: andre@0: /* If it is NULL, we have popped all the way to the top. */ andre@0: if (state == NULL) { andre@0: cx->status = allDone; andre@0: break; andre@0: } andre@0: } andre@0: andre@0: if (cx->status == encodeError) { andre@0: return SECFailure; andre@0: } andre@0: andre@0: return SECSuccess; andre@0: } andre@0: andre@0: andre@0: void andre@0: SEC_ASN1EncoderFinish (SEC_ASN1EncoderContext *cx) andre@0: { andre@0: /* andre@0: * XXX anything else that needs to be finished? andre@0: */ andre@0: andre@0: PORT_FreeArena (cx->our_pool, PR_FALSE); andre@0: } andre@0: andre@0: andre@0: SEC_ASN1EncoderContext * andre@0: SEC_ASN1EncoderStart (const void *src, const SEC_ASN1Template *theTemplate, andre@0: SEC_ASN1WriteProc output_proc, void *output_arg) andre@0: { andre@0: PLArenaPool *our_pool; andre@0: SEC_ASN1EncoderContext *cx; andre@0: andre@0: our_pool = PORT_NewArena (SEC_ASN1_DEFAULT_ARENA_SIZE); andre@0: if (our_pool == NULL) andre@0: return NULL; andre@0: andre@0: cx = (SEC_ASN1EncoderContext*)PORT_ArenaZAlloc (our_pool, sizeof(*cx)); andre@0: if (cx == NULL) { andre@0: PORT_FreeArena (our_pool, PR_FALSE); andre@0: return NULL; andre@0: } andre@0: andre@0: cx->our_pool = our_pool; andre@0: cx->output_proc = output_proc; andre@0: cx->output_arg = output_arg; andre@0: andre@0: cx->status = keepGoing; andre@0: andre@0: if (sec_asn1e_push_state(cx, theTemplate, src, PR_FALSE) == NULL andre@0: || sec_asn1e_init_state_based_on_template (cx->current) == NULL) { andre@0: /* andre@0: * Trouble initializing (probably due to failed allocations) andre@0: * requires that we just give up. andre@0: */ andre@0: PORT_FreeArena (our_pool, PR_FALSE); andre@0: return NULL; andre@0: } andre@0: andre@0: return cx; andre@0: } andre@0: andre@0: andre@0: /* andre@0: * XXX Do we need a FilterProc, too? andre@0: */ andre@0: andre@0: andre@0: void andre@0: SEC_ASN1EncoderSetNotifyProc (SEC_ASN1EncoderContext *cx, andre@0: SEC_ASN1NotifyProc fn, void *arg) andre@0: { andre@0: cx->notify_proc = fn; andre@0: cx->notify_arg = arg; andre@0: } andre@0: andre@0: andre@0: void andre@0: SEC_ASN1EncoderClearNotifyProc (SEC_ASN1EncoderContext *cx) andre@0: { andre@0: cx->notify_proc = NULL; andre@0: cx->notify_arg = NULL; /* not necessary; just being clean */ andre@0: } andre@0: andre@0: void andre@0: SEC_ASN1EncoderAbort(SEC_ASN1EncoderContext *cx, int error) andre@0: { andre@0: PORT_Assert(cx); andre@0: PORT_SetError(error); andre@0: cx->status = encodeError; andre@0: } andre@0: andre@0: void andre@0: SEC_ASN1EncoderSetStreaming (SEC_ASN1EncoderContext *cx) andre@0: { andre@0: /* XXX is there a way to check that we are "between" fields here? */ andre@0: andre@0: cx->streaming = PR_TRUE; andre@0: } andre@0: andre@0: andre@0: void andre@0: SEC_ASN1EncoderClearStreaming (SEC_ASN1EncoderContext *cx) andre@0: { andre@0: /* XXX is there a way to check that we are "between" fields here? */ andre@0: andre@0: cx->streaming = PR_FALSE; andre@0: } andre@0: andre@0: andre@0: void andre@0: SEC_ASN1EncoderSetTakeFromBuf (SEC_ASN1EncoderContext *cx) andre@0: { andre@0: /* andre@0: * XXX is there a way to check that we are "between" fields here? this andre@0: * needs to include a check for being in between groups of items in andre@0: * a SET_OF or SEQUENCE_OF. andre@0: */ andre@0: PORT_Assert (cx->streaming); andre@0: andre@0: cx->from_buf = PR_TRUE; andre@0: } andre@0: andre@0: andre@0: void andre@0: SEC_ASN1EncoderClearTakeFromBuf (SEC_ASN1EncoderContext *cx) andre@0: { andre@0: /* we should actually be taking from buf *now* */ andre@0: PORT_Assert (cx->from_buf); andre@0: if (! cx->from_buf) /* if not, just do nothing */ andre@0: return; andre@0: andre@0: cx->from_buf = PR_FALSE; andre@0: andre@0: if (cx->status == needBytes) { andre@0: cx->status = keepGoing; andre@0: cx->current->place = afterContents; andre@0: } andre@0: } andre@0: andre@0: andre@0: SECStatus andre@0: SEC_ASN1Encode (const void *src, const SEC_ASN1Template *theTemplate, andre@0: SEC_ASN1WriteProc output_proc, void *output_arg) andre@0: { andre@0: SEC_ASN1EncoderContext *ecx; andre@0: SECStatus rv; andre@0: andre@0: ecx = SEC_ASN1EncoderStart (src, theTemplate, output_proc, output_arg); andre@0: if (ecx == NULL) andre@0: return SECFailure; andre@0: andre@0: rv = SEC_ASN1EncoderUpdate (ecx, NULL, 0); andre@0: andre@0: SEC_ASN1EncoderFinish (ecx); andre@0: return rv; andre@0: } andre@0: andre@0: andre@0: /* andre@0: * XXX depth and data_kind are unused; is there a PC way to silence warnings? andre@0: * (I mean "politically correct", not anything to do with intel/win platform) andre@0: */ andre@0: static void andre@0: sec_asn1e_encode_item_count (void *arg, const char *buf, unsigned long len, andre@0: int depth, SEC_ASN1EncodingPart data_kind) andre@0: { andre@0: unsigned long *count; andre@0: andre@0: count = (unsigned long*)arg; andre@0: PORT_Assert (count != NULL); andre@0: andre@0: *count += len; andre@0: } andre@0: andre@0: andre@0: /* XXX depth and data_kind are unused; is there a PC way to silence warnings? */ andre@0: static void andre@0: sec_asn1e_encode_item_store (void *arg, const char *buf, unsigned long len, andre@0: int depth, SEC_ASN1EncodingPart data_kind) andre@0: { andre@0: SECItem *dest; andre@0: andre@0: dest = (SECItem*)arg; andre@0: PORT_Assert (dest != NULL); andre@0: andre@0: PORT_Memcpy (dest->data + dest->len, buf, len); andre@0: dest->len += len; andre@0: } andre@0: andre@0: andre@0: /* andre@0: * Allocate an entire SECItem, or just the data part of it, to hold andre@0: * "len" bytes of stuff. Allocate from the given pool, if specified, andre@0: * otherwise just do a vanilla PORT_Alloc. andre@0: * andre@0: * XXX This seems like a reasonable general-purpose function (for SECITEM_)? andre@0: */ andre@0: static SECItem * andre@0: sec_asn1e_allocate_item (PLArenaPool *poolp, SECItem *dest, unsigned long len) andre@0: { andre@0: if (poolp != NULL) { andre@0: void *release; andre@0: andre@0: release = PORT_ArenaMark (poolp); andre@0: if (dest == NULL) andre@0: dest = (SECItem*)PORT_ArenaAlloc (poolp, sizeof(SECItem)); andre@0: if (dest != NULL) { andre@0: dest->data = (unsigned char*)PORT_ArenaAlloc (poolp, len); andre@0: if (dest->data == NULL) { andre@0: dest = NULL; andre@0: } andre@0: } andre@0: if (dest == NULL) { andre@0: /* one or both allocations failed; release everything */ andre@0: PORT_ArenaRelease (poolp, release); andre@0: } else { andre@0: /* everything okay; unmark the arena */ andre@0: PORT_ArenaUnmark (poolp, release); andre@0: } andre@0: } else { andre@0: SECItem *indest; andre@0: andre@0: indest = dest; andre@0: if (dest == NULL) andre@0: dest = (SECItem*)PORT_Alloc (sizeof(SECItem)); andre@0: if (dest != NULL) { andre@0: dest->type = siBuffer; andre@0: dest->data = (unsigned char*)PORT_Alloc (len); andre@0: if (dest->data == NULL) { andre@0: if (indest == NULL) andre@0: PORT_Free (dest); andre@0: dest = NULL; andre@0: } andre@0: } andre@0: } andre@0: andre@0: return dest; andre@0: } andre@0: andre@0: andre@0: SECItem * andre@0: SEC_ASN1EncodeItem (PLArenaPool *poolp, SECItem *dest, const void *src, andre@0: const SEC_ASN1Template *theTemplate) andre@0: { andre@0: unsigned long encoding_length; andre@0: SECStatus rv; andre@0: andre@0: PORT_Assert (dest == NULL || dest->data == NULL); andre@0: andre@0: encoding_length = 0; andre@0: rv = SEC_ASN1Encode (src, theTemplate, andre@0: sec_asn1e_encode_item_count, &encoding_length); andre@0: if (rv != SECSuccess) andre@0: return NULL; andre@0: andre@0: dest = sec_asn1e_allocate_item (poolp, dest, encoding_length); andre@0: if (dest == NULL) andre@0: return NULL; andre@0: andre@0: /* XXX necessary? This really just checks for a bug in the allocate fn */ andre@0: PORT_Assert (dest->data != NULL); andre@0: if (dest->data == NULL) andre@0: return NULL; andre@0: andre@0: dest->len = 0; andre@0: (void) SEC_ASN1Encode (src, theTemplate, sec_asn1e_encode_item_store, dest); andre@0: andre@0: PORT_Assert (encoding_length == dest->len); andre@0: return dest; andre@0: } andre@0: andre@0: andre@0: static SECItem * andre@0: sec_asn1e_integer(PLArenaPool *poolp, SECItem *dest, unsigned long value, andre@0: PRBool is_unsigned) andre@0: { andre@0: unsigned long copy; andre@0: unsigned char sign; andre@0: int len = 0; andre@0: andre@0: /* andre@0: * Determine the length of the encoded value (minimum of 1). andre@0: */ andre@0: copy = value; andre@0: do { andre@0: len++; andre@0: sign = (unsigned char)(copy & 0x80); andre@0: copy >>= 8; andre@0: } while (copy); andre@0: andre@0: /* andre@0: * If 'value' is non-negative, and the high bit of the last andre@0: * byte we counted was set, we need to add one to the length so andre@0: * we put a high-order zero byte in the encoding. andre@0: */ andre@0: if (sign && (is_unsigned || (long)value >= 0)) andre@0: len++; andre@0: andre@0: /* andre@0: * Allocate the item (if necessary) and the data pointer within. andre@0: */ andre@0: dest = sec_asn1e_allocate_item (poolp, dest, len); andre@0: if (dest == NULL) andre@0: return NULL; andre@0: andre@0: /* andre@0: * Store the value, byte by byte, in the item. andre@0: */ andre@0: dest->len = len; andre@0: while (len) { andre@0: dest->data[--len] = (unsigned char)value; andre@0: value >>= 8; andre@0: } andre@0: PORT_Assert (value == 0); andre@0: andre@0: return dest; andre@0: } andre@0: andre@0: andre@0: SECItem * andre@0: SEC_ASN1EncodeInteger(PLArenaPool *poolp, SECItem *dest, long value) andre@0: { andre@0: return sec_asn1e_integer (poolp, dest, (unsigned long) value, PR_FALSE); andre@0: } andre@0: andre@0: andre@0: SECItem * andre@0: SEC_ASN1EncodeUnsignedInteger(PLArenaPool *poolp, andre@0: SECItem *dest, unsigned long value) andre@0: { andre@0: return sec_asn1e_integer (poolp, dest, value, PR_TRUE); andre@0: }