Support encoding of PKCS#7 "data" containers
authorMartin Willi <martin@revosec.ch>
Tue, 27 Nov 2012 11:21:07 +0000 (12:21 +0100)
committerMartin Willi <martin@revosec.ch>
Wed, 19 Dec 2012 09:32:07 +0000 (10:32 +0100)
src/libstrongswan/plugins/pkcs7/pkcs7_data.c
src/libstrongswan/plugins/pkcs7/pkcs7_data.h
src/libstrongswan/plugins/pkcs7/pkcs7_plugin.c

index 8c4c7ec..5f0ab10 100644 (file)
@@ -16,6 +16,7 @@
 #include "pkcs7_data.h"
 
 #include <asn1/asn1.h>
+#include <asn1/oid.h>
 
 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;
+}
index b0a0578..459d355 100644 (file)
  */
 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_ @}*/
index e4e5764..12932ca 100644 (file)
@@ -15,6 +15,7 @@
 
 #include "pkcs7_plugin.h"
 #include "pkcs7_generic.h"
+#include "pkcs7_data.h"
 
 #include <library.h>
 
@@ -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);