mirror of
https://github.com/strongswan/strongswan.git
synced 2025-10-09 00:00:53 -04:00
PKCS#9 coding style cleanups
This commit is contained in:
parent
f0c02e27c4
commit
c1005c120c
@ -25,6 +25,7 @@
|
|||||||
#include "pkcs9.h"
|
#include "pkcs9.h"
|
||||||
|
|
||||||
typedef struct private_pkcs9_t private_pkcs9_t;
|
typedef struct private_pkcs9_t private_pkcs9_t;
|
||||||
|
typedef struct attribute_t attribute_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Private data of a pkcs9_t attribute list.
|
* Private data of a pkcs9_t attribute list.
|
||||||
@ -46,12 +47,11 @@ struct private_pkcs9_t {
|
|||||||
linked_list_t *attributes;
|
linked_list_t *attributes;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct attribute_t attribute_t;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Definition of an attribute_t object.
|
* Definition of an attribute_t object.
|
||||||
*/
|
*/
|
||||||
struct attribute_t {
|
struct attribute_t {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Object Identifier (OID)
|
* Object Identifier (OID)
|
||||||
*/
|
*/
|
||||||
@ -66,54 +66,32 @@ struct attribute_t {
|
|||||||
* ASN.1 encoding
|
* ASN.1 encoding
|
||||||
*/
|
*/
|
||||||
chunk_t encoding;
|
chunk_t encoding;
|
||||||
|
|
||||||
/**
|
|
||||||
* Destroys the attribute.
|
|
||||||
*/
|
|
||||||
void (*destroy) (attribute_t *this);
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* return the ASN.1 encoding of a PKCS#9 attribute
|
* return the ASN.1 encoding of a PKCS#9 attribute
|
||||||
*/
|
*/
|
||||||
static asn1_t asn1_attributeType(int oid)
|
static asn1_t get_attribute_type(int oid)
|
||||||
{
|
{
|
||||||
asn1_t type;
|
|
||||||
|
|
||||||
switch (oid)
|
switch (oid)
|
||||||
{
|
{
|
||||||
case OID_PKCS9_CONTENT_TYPE:
|
case OID_PKCS9_CONTENT_TYPE:
|
||||||
type = ASN1_OID;
|
return ASN1_OID;
|
||||||
break;
|
|
||||||
case OID_PKCS9_SIGNING_TIME:
|
case OID_PKCS9_SIGNING_TIME:
|
||||||
type = ASN1_UTCTIME;
|
return ASN1_UTCTIME;
|
||||||
break;
|
|
||||||
case OID_PKCS9_MESSAGE_DIGEST:
|
|
||||||
type = ASN1_OCTET_STRING;
|
|
||||||
break;
|
|
||||||
case OID_PKI_MESSAGE_TYPE:
|
case OID_PKI_MESSAGE_TYPE:
|
||||||
type = ASN1_PRINTABLESTRING;
|
|
||||||
break;
|
|
||||||
case OID_PKI_STATUS:
|
case OID_PKI_STATUS:
|
||||||
type = ASN1_PRINTABLESTRING;
|
|
||||||
break;
|
|
||||||
case OID_PKI_FAIL_INFO:
|
case OID_PKI_FAIL_INFO:
|
||||||
type = ASN1_PRINTABLESTRING;
|
return ASN1_PRINTABLESTRING;
|
||||||
break;
|
|
||||||
case OID_PKI_SENDER_NONCE:
|
case OID_PKI_SENDER_NONCE:
|
||||||
type = ASN1_OCTET_STRING;
|
|
||||||
break;
|
|
||||||
case OID_PKI_RECIPIENT_NONCE:
|
case OID_PKI_RECIPIENT_NONCE:
|
||||||
type = ASN1_OCTET_STRING;
|
case OID_PKCS9_MESSAGE_DIGEST:
|
||||||
break;
|
return ASN1_OCTET_STRING;
|
||||||
case OID_PKI_TRANS_ID:
|
case OID_PKI_TRANS_ID:
|
||||||
type = ASN1_PRINTABLESTRING;
|
return ASN1_PRINTABLESTRING;
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
type = ASN1_EOC;
|
return ASN1_EOC;
|
||||||
}
|
}
|
||||||
return type;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -134,12 +112,11 @@ static attribute_t *attribute_create(int oid, chunk_t value)
|
|||||||
attribute_t *this;
|
attribute_t *this;
|
||||||
|
|
||||||
INIT(this,
|
INIT(this,
|
||||||
.destroy = attribute_destroy,
|
|
||||||
.oid = oid,
|
.oid = oid,
|
||||||
.value = chunk_clone(value),
|
.value = chunk_clone(value),
|
||||||
.encoding = asn1_wrap(ASN1_SEQUENCE, "mm",
|
.encoding = asn1_wrap(ASN1_SEQUENCE, "mm",
|
||||||
asn1_build_known_oid(oid),
|
asn1_build_known_oid(oid),
|
||||||
asn1_simple_object(ASN1_SET, value)),
|
asn1_wrap(ASN1_SET, "c", value)),
|
||||||
);
|
);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
@ -152,40 +129,27 @@ static void build_encoding(private_pkcs9_t *this)
|
|||||||
{
|
{
|
||||||
enumerator_t *enumerator;
|
enumerator_t *enumerator;
|
||||||
attribute_t *attribute;
|
attribute_t *attribute;
|
||||||
u_int attributes_len = 0;
|
u_int len = 0;
|
||||||
|
u_char *pos;
|
||||||
if (this->encoding.ptr)
|
|
||||||
{
|
|
||||||
chunk_free(&this->encoding);
|
|
||||||
}
|
|
||||||
if (this->attributes->get_count(this->attributes) == 0)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* compute the total length of the encoded attributes */
|
/* compute the total length of the encoded attributes */
|
||||||
enumerator = this->attributes->create_enumerator(this->attributes);
|
enumerator = this->attributes->create_enumerator(this->attributes);
|
||||||
|
while (enumerator->enumerate(enumerator, &attribute))
|
||||||
while (enumerator->enumerate(enumerator, (void**)&attribute))
|
|
||||||
{
|
{
|
||||||
attributes_len += attribute->encoding.len;
|
len += attribute->encoding.len;
|
||||||
}
|
}
|
||||||
enumerator->destroy(enumerator);
|
enumerator->destroy(enumerator);
|
||||||
|
|
||||||
/* allocate memory for the attributes and build the encoding */
|
/* allocate memory for the attributes and build the encoding */
|
||||||
{
|
pos = asn1_build_object(&this->encoding, ASN1_SET, len);
|
||||||
u_char *pos = asn1_build_object(&this->encoding, ASN1_SET, attributes_len);
|
|
||||||
|
|
||||||
enumerator = this->attributes->create_enumerator(this->attributes);
|
enumerator = this->attributes->create_enumerator(this->attributes);
|
||||||
|
while (enumerator->enumerate(enumerator, &attribute))
|
||||||
while (enumerator->enumerate(enumerator, (void**)&attribute))
|
|
||||||
{
|
{
|
||||||
memcpy(pos, attribute->encoding.ptr, attribute->encoding.len);
|
memcpy(pos, attribute->encoding.ptr, attribute->encoding.len);
|
||||||
pos += attribute->encoding.len;
|
pos += attribute->encoding.len;
|
||||||
}
|
}
|
||||||
enumerator->destroy(enumerator);
|
enumerator->destroy(enumerator);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
METHOD(pkcs9_t, get_encoding, chunk_t,
|
METHOD(pkcs9_t, get_encoding, chunk_t,
|
||||||
private_pkcs9_t *this)
|
private_pkcs9_t *this)
|
||||||
@ -205,7 +169,7 @@ METHOD(pkcs9_t, get_attribute, chunk_t,
|
|||||||
attribute_t *attribute;
|
attribute_t *attribute;
|
||||||
|
|
||||||
enumerator = this->attributes->create_enumerator(this->attributes);
|
enumerator = this->attributes->create_enumerator(this->attributes);
|
||||||
while (enumerator->enumerate(enumerator, (void**)&attribute))
|
while (enumerator->enumerate(enumerator, &attribute))
|
||||||
{
|
{
|
||||||
if (attribute->oid == oid)
|
if (attribute->oid == oid)
|
||||||
{
|
{
|
||||||
@ -214,14 +178,12 @@ METHOD(pkcs9_t, get_attribute, chunk_t,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
enumerator->destroy(enumerator);
|
enumerator->destroy(enumerator);
|
||||||
if (value.ptr &&
|
if (value.len && asn1_unwrap(&value, &value) != ASN1_INVALID)
|
||||||
!asn1_parse_simple_object(&value, asn1_attributeType(oid), 0,
|
|
||||||
oid_names[oid].name))
|
|
||||||
{
|
{
|
||||||
return chunk_empty;
|
|
||||||
}
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
return chunk_empty;
|
||||||
|
}
|
||||||
|
|
||||||
METHOD(pkcs9_t, set_attribute_raw, void,
|
METHOD(pkcs9_t, set_attribute_raw, void,
|
||||||
private_pkcs9_t *this, int oid, chunk_t value)
|
private_pkcs9_t *this, int oid, chunk_t value)
|
||||||
@ -235,7 +197,7 @@ METHOD(pkcs9_t, set_attribute_raw, void,
|
|||||||
METHOD(pkcs9_t, set_attribute, void,
|
METHOD(pkcs9_t, set_attribute, void,
|
||||||
private_pkcs9_t *this, int oid, chunk_t value)
|
private_pkcs9_t *this, int oid, chunk_t value)
|
||||||
{
|
{
|
||||||
chunk_t attr = asn1_simple_object(asn1_attributeType(oid), value);
|
chunk_t attr = asn1_simple_object(get_attribute_type(oid), value);
|
||||||
|
|
||||||
set_attribute_raw(this, oid, attr);
|
set_attribute_raw(this, oid, attr);
|
||||||
}
|
}
|
||||||
@ -243,15 +205,16 @@ METHOD(pkcs9_t, set_attribute, void,
|
|||||||
METHOD(pkcs9_t, destroy, void,
|
METHOD(pkcs9_t, destroy, void,
|
||||||
private_pkcs9_t *this)
|
private_pkcs9_t *this)
|
||||||
{
|
{
|
||||||
this->attributes->destroy_offset(this->attributes, offsetof(attribute_t, destroy));
|
this->attributes->destroy_function(this->attributes,
|
||||||
|
(void*)attribute_destroy);
|
||||||
free(this->encoding.ptr);
|
free(this->encoding.ptr);
|
||||||
free(this);
|
free(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* Generic private constructor
|
* Described in header.
|
||||||
*/
|
*/
|
||||||
static private_pkcs9_t *pkcs9_create_empty(void)
|
pkcs9_t *pkcs9_create(void)
|
||||||
{
|
{
|
||||||
private_pkcs9_t *this;
|
private_pkcs9_t *this;
|
||||||
|
|
||||||
@ -266,16 +229,6 @@ static private_pkcs9_t *pkcs9_create_empty(void)
|
|||||||
.attributes = linked_list_create(),
|
.attributes = linked_list_create(),
|
||||||
);
|
);
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Described in header.
|
|
||||||
*/
|
|
||||||
pkcs9_t *pkcs9_create(void)
|
|
||||||
{
|
|
||||||
private_pkcs9_t *this = pkcs9_create_empty();
|
|
||||||
|
|
||||||
return &this->public;
|
return &this->public;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -317,50 +270,28 @@ static bool parse_attributes(chunk_t chunk, int level0, private_pkcs9_t* this)
|
|||||||
oid = asn1_known_oid(object);
|
oid = asn1_known_oid(object);
|
||||||
break;
|
break;
|
||||||
case ATTRIBUTE_OBJ_VALUE:
|
case ATTRIBUTE_OBJ_VALUE:
|
||||||
if (oid == OID_UNKNOWN)
|
if (oid != OID_UNKNOWN)
|
||||||
{
|
{
|
||||||
break;
|
|
||||||
}
|
|
||||||
/* add the attribute to a linked list */
|
|
||||||
{
|
|
||||||
attribute_t *attribute = attribute_create(oid, object);
|
|
||||||
|
|
||||||
this->attributes->insert_last(this->attributes,
|
this->attributes->insert_last(this->attributes,
|
||||||
(void*)attribute);
|
attribute_create(oid, object));
|
||||||
}
|
|
||||||
/* parse known attributes */
|
|
||||||
{
|
|
||||||
asn1_t type = asn1_attributeType(oid);
|
|
||||||
|
|
||||||
if (type != ASN1_EOC)
|
|
||||||
{
|
|
||||||
if (!asn1_parse_simple_object(&object, type,
|
|
||||||
parser->get_level(parser)+1,
|
|
||||||
oid_names[oid].name))
|
|
||||||
{
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
success = parser->success(parser);
|
success = parser->success(parser);
|
||||||
|
|
||||||
end:
|
|
||||||
parser->destroy(parser);
|
parser->destroy(parser);
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Described in header.
|
* Described in header.
|
||||||
*/
|
*/
|
||||||
pkcs9_t *pkcs9_create_from_chunk(chunk_t chunk, u_int level)
|
pkcs9_t *pkcs9_create_from_chunk(chunk_t chunk, u_int level)
|
||||||
{
|
{
|
||||||
private_pkcs9_t *this = pkcs9_create_empty();
|
private_pkcs9_t *this = (private_pkcs9_t*)pkcs9_create();
|
||||||
|
|
||||||
this->encoding = chunk_clone(chunk);
|
this->encoding = chunk_clone(chunk);
|
||||||
|
|
||||||
if (!parse_attributes(chunk, level, this))
|
if (!parse_attributes(chunk, level, this))
|
||||||
{
|
{
|
||||||
destroy(this);
|
destroy(this);
|
||||||
|
@ -27,19 +27,19 @@ typedef struct pkcs9_t pkcs9_t;
|
|||||||
#include <library.h>
|
#include <library.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* PKCS#9 attributes.
|
* PKCS#9 attribute lists.
|
||||||
*/
|
*/
|
||||||
struct pkcs9_t {
|
struct pkcs9_t {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets ASN.1 encoding of PKCS#9 attribute list
|
* Gets ASN.1 encoding of PKCS#9 attribute list.
|
||||||
*
|
*
|
||||||
* @return ASN.1 encoded PKCSI#9 list
|
* @return ASN.1 encoded PKCSI#9 list
|
||||||
*/
|
*/
|
||||||
chunk_t (*get_encoding) (pkcs9_t *this);
|
chunk_t (*get_encoding) (pkcs9_t *this);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a PKCS#9 attribute
|
* Gets a PKCS#9 attribute from the list.
|
||||||
*
|
*
|
||||||
* @param oid OID of the attribute
|
* @param oid OID of the attribute
|
||||||
* @return value of the attribute (internal data)
|
* @return value of the attribute (internal data)
|
||||||
@ -47,7 +47,7 @@ struct pkcs9_t {
|
|||||||
chunk_t (*get_attribute) (pkcs9_t *this, int oid);
|
chunk_t (*get_attribute) (pkcs9_t *this, int oid);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a PKCS#9 attribute
|
* Adds a PKCS#9 attribute.
|
||||||
*
|
*
|
||||||
* @param oid OID of the attribute
|
* @param oid OID of the attribute
|
||||||
* @param value value of the attribute (gets cloned)
|
* @param value value of the attribute (gets cloned)
|
||||||
@ -55,7 +55,7 @@ struct pkcs9_t {
|
|||||||
void (*set_attribute) (pkcs9_t *this, int oid, chunk_t value);
|
void (*set_attribute) (pkcs9_t *this, int oid, chunk_t value);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a ASN.1 encoded PKCS#9 attribute
|
* Adds a ASN.1 encoded PKCS#9 attribute.
|
||||||
*
|
*
|
||||||
* @param oid OID of the attribute
|
* @param oid OID of the attribute
|
||||||
* @param value ASN.1 encoded value of the attribute (gets adopted)
|
* @param value ASN.1 encoded value of the attribute (gets adopted)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user