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