Don't store additional encoding for each PKCS#9 attribute

This commit is contained in:
Martin Willi
2012-12-19 10:32:07 +01:00
parent 7f9fedc9bd
commit 60c9b5da8d
+15 -13
View File
@@ -74,7 +74,6 @@ struct attribute_t {
static void attribute_destroy(attribute_t *this) static void attribute_destroy(attribute_t *this)
{ {
free(this->value.ptr); free(this->value.ptr);
free(this->encoding.ptr);
free(this); free(this);
} }
@@ -88,9 +87,6 @@ static attribute_t *attribute_create(int oid, chunk_t value)
INIT(this, INIT(this,
.oid = oid, .oid = oid,
.value = chunk_clone(value), .value = chunk_clone(value),
.encoding = asn1_wrap(ASN1_SEQUENCE, "mm",
asn1_build_known_oid(oid),
asn1_wrap(ASN1_SET, "c", value)),
); );
return this; return this;
@@ -103,26 +99,32 @@ static void build_encoding(private_pkcs9_t *this)
{ {
enumerator_t *enumerator; enumerator_t *enumerator;
attribute_t *attribute; attribute_t *attribute;
u_int len = 0; u_int len = 0, count, i = 0;
chunk_t *chunks;
u_char *pos; u_char *pos;
/* compute the total length of the encoded attributes */ count = this->attributes->get_count(this->attributes);
chunks = malloc(sizeof(chunk_t) * count);
enumerator = this->attributes->create_enumerator(this->attributes); enumerator = this->attributes->create_enumerator(this->attributes);
while (enumerator->enumerate(enumerator, &attribute)) while (enumerator->enumerate(enumerator, &attribute))
{ {
len += attribute->encoding.len; chunks[i] = asn1_wrap(ASN1_SEQUENCE, "mm",
asn1_build_known_oid(attribute->oid),
asn1_wrap(ASN1_SET, "c", attribute->value));
len += chunks[i].len;
i++;
} }
enumerator->destroy(enumerator); enumerator->destroy(enumerator);
/* allocate memory for the attributes and build the encoding */
pos = asn1_build_object(&this->encoding, ASN1_SET, len); pos = asn1_build_object(&this->encoding, ASN1_SET, len);
enumerator = this->attributes->create_enumerator(this->attributes); for (i = 0; i < count; i++)
while (enumerator->enumerate(enumerator, &attribute))
{ {
memcpy(pos, attribute->encoding.ptr, attribute->encoding.len); memcpy(pos, chunks[i].ptr, chunks[i].len);
pos += attribute->encoding.len; pos += chunks[i].len;
free(chunks[i].ptr);
} }
enumerator->destroy(enumerator); free(chunks);
} }
METHOD(pkcs9_t, get_encoding, chunk_t, METHOD(pkcs9_t, get_encoding, chunk_t,