diff --git a/src/libstrongswan/crypto/pkcs9.c b/src/libstrongswan/crypto/pkcs9.c index af34cfa07..63a615238 100644 --- a/src/libstrongswan/crypto/pkcs9.c +++ b/src/libstrongswan/crypto/pkcs9.c @@ -182,21 +182,22 @@ static void attribute_destroy(attribute_t *this) */ static attribute_t *attribute_create(int oid, chunk_t value) { - attribute_t *this = malloc_thing(attribute_t); + attribute_t *this; + + INIT(this, + .destroy = attribute_destroy, + .oid = oid, + .value = chunk_clone(value), + .encoding = asn1_wrap(ASN1_SEQUENCE, "cm", + asn1_attributeIdentifier(oid), + asn1_simple_object(ASN1_SET, value)), + ); - this->oid = oid; - this->value = chunk_clone(value); - this->encoding = asn1_wrap(ASN1_SEQUENCE, "cm", - asn1_attributeIdentifier(oid), - asn1_simple_object(ASN1_SET, value)); - this->destroy = (void (*) (attribute_t*))attribute_destroy; return this; } -/** - * Implements pkcs9_t.build_encoding - */ -static void build_encoding(private_pkcs9_t *this) +METHOD(pkcs9_t, build_encoding, void, + private_pkcs9_t *this) { enumerator_t *enumerator; attribute_t *attribute; @@ -235,10 +236,8 @@ static void build_encoding(private_pkcs9_t *this) } } -/** - * Implements pkcs9_t.get_encoding - */ -static chunk_t get_encoding(private_pkcs9_t *this) +METHOD(pkcs9_t, get_encoding, chunk_t, + private_pkcs9_t *this) { if (this->encoding.ptr == NULL) { @@ -247,10 +246,8 @@ static chunk_t get_encoding(private_pkcs9_t *this) return this->encoding; } -/** - * Implements pkcs9_t.get_attribute - */ -static chunk_t get_attribute(private_pkcs9_t *this, int oid) +METHOD(pkcs9_t, get_attribute, chunk_t, + private_pkcs9_t *this, int oid) { enumerator_t *enumerator; chunk_t value = chunk_empty; @@ -269,20 +266,16 @@ static chunk_t get_attribute(private_pkcs9_t *this, int oid) return value; } -/** - * Implements pkcs9_t.set_attribute - */ -static void set_attribute(private_pkcs9_t *this, int oid, chunk_t value) +METHOD(pkcs9_t, set_attribute, void, + private_pkcs9_t *this, int oid, chunk_t value) { attribute_t *attribute = attribute_create(oid, value); this->attributes->insert_last(this->attributes, (void*)attribute); } -/** - * Implements pkcs9_t.get_messageDigest - */ -static chunk_t get_messageDigest(private_pkcs9_t *this) +METHOD(pkcs9_t, get_messageDigest, chunk_t, + private_pkcs9_t *this) { const int oid = OID_PKCS9_MESSAGE_DIGEST; chunk_t value = get_attribute(this, oid); @@ -299,10 +292,8 @@ static chunk_t get_messageDigest(private_pkcs9_t *this) return chunk_clone(value); } -/** - * Implements pkcs9_t.set_attribute - */ -static void set_messageDigest(private_pkcs9_t *this, chunk_t value) +METHOD(pkcs9_t, set_messageDigest, void, + private_pkcs9_t *this, chunk_t value) { const int oid = OID_PKCS9_MESSAGE_DIGEST; chunk_t messageDigest = asn1_simple_object(asn1_attributeType(oid), value); @@ -311,10 +302,8 @@ static void set_messageDigest(private_pkcs9_t *this, chunk_t value) free(messageDigest.ptr); } -/** - * Implements pkcs9_t.destroy - */ -static void destroy(private_pkcs9_t *this) +METHOD(pkcs9_t, destroy, void, + private_pkcs9_t *this) { this->attributes->destroy_offset(this->attributes, offsetof(attribute_t, destroy)); free(this->encoding.ptr); @@ -326,20 +315,20 @@ static void destroy(private_pkcs9_t *this) */ static private_pkcs9_t *pkcs9_create_empty(void) { - private_pkcs9_t *this = malloc_thing(private_pkcs9_t); + private_pkcs9_t *this; - /* initialize */ - this->encoding = chunk_empty; - this->attributes = linked_list_create(); - - /*public functions */ - this->public.build_encoding = (void (*) (pkcs9_t*))build_encoding; - this->public.get_encoding = (chunk_t (*) (pkcs9_t*))get_encoding; - this->public.get_attribute = (chunk_t (*) (pkcs9_t*,int))get_attribute; - this->public.set_attribute = (void (*) (pkcs9_t*,int,chunk_t))set_attribute; - this->public.get_messageDigest = (chunk_t (*) (pkcs9_t*))get_messageDigest; - this->public.set_messageDigest = (void (*) (pkcs9_t*,chunk_t))set_messageDigest; - this->public.destroy = (void (*) (pkcs9_t*))destroy; + INIT(this, + .public = { + .build_encoding = _build_encoding, + .get_encoding = _get_encoding, + .get_attribute = _get_attribute, + .set_attribute = _set_attribute, + .get_messageDigest = _get_messageDigest, + .set_messageDigest = _set_messageDigest, + .destroy = _destroy, + }, + .attributes = linked_list_create(), + ); return this; }