From 3c2986bf0a2b39ac66ff4053307838dc297b478f Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Tue, 27 Nov 2012 12:21:07 +0100 Subject: [PATCH] Support encoding of PKCS#7 "data" containers --- src/libstrongswan/plugins/pkcs7/pkcs7_data.c | 58 +++++++++++++++++-- src/libstrongswan/plugins/pkcs7/pkcs7_data.h | 10 ++++ .../plugins/pkcs7/pkcs7_plugin.c | 3 + 3 files changed, 66 insertions(+), 5 deletions(-) diff --git a/src/libstrongswan/plugins/pkcs7/pkcs7_data.c b/src/libstrongswan/plugins/pkcs7/pkcs7_data.c index 8c4c7ec75..5f0ab10f4 100644 --- a/src/libstrongswan/plugins/pkcs7/pkcs7_data.c +++ b/src/libstrongswan/plugins/pkcs7/pkcs7_data.c @@ -16,6 +16,7 @@ #include "pkcs7_data.h" #include +#include typedef struct private_pkcs7_data_t private_pkcs7_data_t; @@ -35,7 +36,7 @@ struct private_pkcs7_data_t { chunk_t content; /** - * Encoded PKCS#7 signed-data + * Encoded PKCS#7 data */ chunk_t encoding; }; @@ -82,9 +83,9 @@ METHOD(container_t, destroy, void, } /** - * See header. + * Create an empty container */ -pkcs7_t *pkcs7_data_load(chunk_t encoding, chunk_t content) +static private_pkcs7_data_t* create_empty() { private_pkcs7_data_t *this; @@ -98,9 +99,56 @@ pkcs7_t *pkcs7_data_load(chunk_t encoding, chunk_t content) .destroy = _destroy, }, }, - .encoding = chunk_clone(encoding), - .content = chunk_clone(content), ); + return this; +} + +/** + * See header. + */ +pkcs7_t *pkcs7_data_load(chunk_t encoding, chunk_t content) +{ + private_pkcs7_data_t *this = create_empty(); + + this->encoding = chunk_clone(encoding); + this->content = chunk_clone(content); + return &this->public; } + +/** + * See header. + */ +pkcs7_t *pkcs7_data_gen(container_type_t type, va_list args) +{ + private_pkcs7_data_t *this; + chunk_t blob = chunk_empty; + + while (TRUE) + { + switch (va_arg(args, builder_part_t)) + { + case BUILD_BLOB: + blob = va_arg(args, chunk_t); + continue; + case BUILD_END: + break; + default: + return NULL; + } + break; + } + + if (blob.len) + { + this = create_empty(); + + this->content = asn1_wrap(ASN1_OCTET_STRING, "c", blob); + this->encoding = asn1_wrap(ASN1_SEQUENCE, "mm", + asn1_build_known_oid(OID_PKCS7_DATA), + asn1_wrap(ASN1_CONTEXT_C_0, "c", this->content)); + return &this->public; + } + return NULL; +} diff --git a/src/libstrongswan/plugins/pkcs7/pkcs7_data.h b/src/libstrongswan/plugins/pkcs7/pkcs7_data.h index b0a05780f..459d355ca 100644 --- a/src/libstrongswan/plugins/pkcs7/pkcs7_data.h +++ b/src/libstrongswan/plugins/pkcs7/pkcs7_data.h @@ -33,4 +33,14 @@ */ pkcs7_t *pkcs7_data_load(chunk_t encoding, chunk_t content); +/** + * Generate a PKCS#7 data container. + * + * The only accepted builder argument is BUILDER_BLOB. + * + * @param type container type, must be CONTAINER_PKCS7_DATA + * @param args builder_t arguments to use. + */ +pkcs7_t *pkcs7_data_gen(container_type_t type, va_list args); + #endif /** PKCS7_DATA_H_ @}*/ diff --git a/src/libstrongswan/plugins/pkcs7/pkcs7_plugin.c b/src/libstrongswan/plugins/pkcs7/pkcs7_plugin.c index e4e5764cf..12932ca82 100644 --- a/src/libstrongswan/plugins/pkcs7/pkcs7_plugin.c +++ b/src/libstrongswan/plugins/pkcs7/pkcs7_plugin.c @@ -15,6 +15,7 @@ #include "pkcs7_plugin.h" #include "pkcs7_generic.h" +#include "pkcs7_data.h" #include @@ -43,6 +44,8 @@ METHOD(plugin_t, get_features, int, static plugin_feature_t f[] = { PLUGIN_REGISTER(CONTAINER_DECODE, pkcs7_generic_load, TRUE), PLUGIN_PROVIDE(CONTAINER_DECODE, CONTAINER_PKCS7), + PLUGIN_REGISTER(CONTAINER_ENCODE, pkcs7_data_gen, TRUE), + PLUGIN_PROVIDE(CONTAINER_ENCODE, CONTAINER_PKCS7_DATA), }; *features = f; return countof(f);