generic public key factory moved
authorTobias Brunner <tobias@strongswan.org>
Thu, 15 May 2008 12:33:00 +0000 (12:33 -0000)
committerTobias Brunner <tobias@strongswan.org>
Thu, 15 May 2008 12:33:00 +0000 (12:33 -0000)
src/libstrongswan/plugins/gmp/Makefile.am
src/libstrongswan/plugins/gmp/gmp_plugin.c
src/libstrongswan/plugins/gmp/gmp_public_key.c [deleted file]
src/libstrongswan/plugins/gmp/gmp_public_key.h [deleted file]
src/libstrongswan/plugins/pubkey/Makefile.am
src/libstrongswan/plugins/pubkey/pubkey_plugin.c
src/libstrongswan/plugins/pubkey/pubkey_public_key.c [new file with mode: 0644]
src/libstrongswan/plugins/pubkey/pubkey_public_key.h [new file with mode: 0644]

index 5d715d0..f073b5d 100644 (file)
@@ -8,8 +8,8 @@ plugin_LTLIBRARIES = libstrongswan-gmp.la
 libstrongswan_gmp_la_SOURCES = gmp_plugin.h gmp_plugin.c \
   gmp_diffie_hellman.c gmp_diffie_hellman.h \
   gmp_rsa_private_key.c gmp_rsa_private_key.h \
-  gmp_rsa_public_key.c gmp_rsa_public_key.h \
-  gmp_public_key.c gmp_public_key.h
+  gmp_rsa_public_key.c gmp_rsa_public_key.h
+
 libstrongswan_gmp_la_LDFLAGS = -module
 libstrongswan_gmp_la_LIBADD = -lgmp
 
index 3cbd2f1..9f637e6 100644 (file)
@@ -19,7 +19,6 @@
 
 #include <library.h>
 #include "gmp_diffie_hellman.h"
-#include "gmp_public_key.h"
 #include "gmp_rsa_private_key.h"
 #include "gmp_rsa_public_key.h"
 
@@ -82,8 +81,6 @@ plugin_t *plugin_create()
                                                (builder_constructor_t)gmp_rsa_private_key_builder);
        lib->creds->add_builder(lib->creds, CRED_PUBLIC_KEY, KEY_RSA,
                                                (builder_constructor_t)gmp_rsa_public_key_builder);
-       lib->creds->add_builder(lib->creds, CRED_PUBLIC_KEY, KEY_ANY,
-                                               (builder_constructor_t)gmp_public_key_builder);
        
        return &this->public.plugin;
 }
diff --git a/src/libstrongswan/plugins/gmp/gmp_public_key.c b/src/libstrongswan/plugins/gmp/gmp_public_key.c
deleted file mode 100644 (file)
index 4105f58..0000000
+++ /dev/null
@@ -1,160 +0,0 @@
-/*
- * Copyright (C) 2008 Martin Willi
- * Copyright (C) 2000-2008 Andreas Steffen
- * Hochschule fuer Technik Rapperswil
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation; either version 2 of the License, or (at your
- * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * for more details.
- *
- * $Id$
- */
-
-#include "gmp_public_key.h"
-
-#include <debug.h>
-#include <asn1/oid.h>
-#include <asn1/asn1.h>
-#include <asn1/asn1_parser.h>
-
-/**
- * ASN.1 definition of a subjectPublicKeyInfo structure
- */
-static const asn1Object_t pkinfoObjects[] = {
-       { 0, "subjectPublicKeyInfo",ASN1_SEQUENCE,              ASN1_NONE       }, /* 0 */
-       { 1,   "algorithm",                     ASN1_EOC,                       ASN1_RAW        }, /* 1 */
-       { 1,   "subjectPublicKey",      ASN1_BIT_STRING,        ASN1_BODY       }, /* 2 */
-       { 0, "exit",                            ASN1_EOC,                       ASN1_EXIT       }
-};
-#define PKINFO_SUBJECT_PUBLIC_KEY_ALGORITHM    1
-#define PKINFO_SUBJECT_PUBLIC_KEY                      2
-
-
-/**
- * Load a public key from an ASN1 encoded blob
- */
-static public_key_t *load(chunk_t blob)
-{
-       asn1_parser_t *parser;
-       chunk_t object;
-       int objectID;
-       public_key_t *key = NULL;
-       key_type_t type = KEY_ANY;
-
-       parser = asn1_parser_create(pkinfoObjects, blob);
-       
-       while (parser->iterate(parser, &objectID, &object))
-       {
-               switch (objectID)
-               {
-                       case PKINFO_SUBJECT_PUBLIC_KEY_ALGORITHM:
-                       {
-                               int oid = asn1_parse_algorithmIdentifier(object,
-                                                                               parser->get_level(parser)+1, NULL);
-                               
-                               if (oid == OID_RSA_ENCRYPTION)
-                               {
-                                       type = KEY_RSA;
-                               }
-                               else
-                               {
-                                       /* key type not supported */
-                                       goto end;
-                               }
-                               break;
-                       }
-                       case PKINFO_SUBJECT_PUBLIC_KEY:
-                               if (object.len > 0 && *object.ptr == 0x00)
-                               {
-                                       /* skip initial bit string octet defining 0 unused bits */
-                                       object = chunk_skip(object, 1);
-                               }
-                               key = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, type,
-                                                                                BUILD_BLOB_ASN1_DER, chunk_clone(object),
-                                                                                BUILD_END);
-                               break;
-               }
-       } 
-       
-end:
-       parser->destroy(parser);
-       free(blob.ptr);
-       return key; 
-}
-
-typedef struct private_builder_t private_builder_t;
-/**
- * Builder implementation for key loading
- */
-struct private_builder_t {
-       /** implements the builder interface */
-       builder_t public;
-       /** loaded public key */
-       public_key_t *key;
-};
-
-/**
- * Implementation of builder_t.build
- */
-static public_key_t *build(private_builder_t *this)
-{
-       public_key_t *key = this->key;
-       
-       free(this);
-       return key;
-}
-
-/**
- * Implementation of builder_t.add
- */
-static void add(private_builder_t *this, builder_part_t part, ...)
-{
-       va_list args;
-       
-       if (this->key)
-       {
-               DBG1("ignoring surplus build part %N", builder_part_names, part);
-               return;
-       }
-       switch (part)
-       {
-               case BUILD_BLOB_ASN1_DER:
-               {
-                       va_start(args, part);
-                       this->key = load(va_arg(args, chunk_t));
-                       va_end(args);
-                       break;
-               }
-               default:
-                       DBG1("ignoring unsupported build part %N", builder_part_names, part);
-                       break;
-       }
-}
-
-/**
- * Builder construction function
- */
-builder_t *gmp_public_key_builder(key_type_t type)
-{
-       private_builder_t *this;
-       
-       if (type != KEY_ANY)
-       {
-               return NULL;
-       }
-       
-       this = malloc_thing(private_builder_t);
-       
-       this->key = NULL;
-       this->public.add = (void(*)(builder_t *this, builder_part_t part, ...))add;
-       this->public.build = (void*(*)(builder_t *this))build;
-       
-       return &this->public;
-}
-
diff --git a/src/libstrongswan/plugins/gmp/gmp_public_key.h b/src/libstrongswan/plugins/gmp/gmp_public_key.h
deleted file mode 100644 (file)
index 2dfa166..0000000
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * Copyright (C) 2008 Martin Willi
- * Hochschule fuer Technik Rapperswil
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation; either version 2 of the License, or (at your
- * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * for more details.
- *
- * $Id$
- */
-
-/**
- * @defgroup gmp_public_key gmp_public_key
- * @{ @ingroup gmp_p
- */
-
-#ifndef GMP_PUBLIC_KEY_H_
-#define GMP_PUBLIC_KEY_H_
-
-#include <credentials/keys/public_key.h>
-
-/**
- * Create the builder for a generic public key.
- *
- * @param type         type of the key, must be KEY_ANY
- * @return                     builder instance
- */
-builder_t *gmp_public_key_builder(key_type_t type);
-
-#endif /*GMP_RSA_PUBLIC_KEY_H_ @}*/
index 3d67f35..3b51261 100644 (file)
@@ -6,6 +6,8 @@ AM_CFLAGS = -rdynamic
 plugin_LTLIBRARIES = libstrongswan-pubkey.la
 
 libstrongswan_pubkey_la_SOURCES = pubkey_plugin.h pubkey_plugin.c \
-  pubkey_cert.h pubkey_cert.c
+  pubkey_cert.h pubkey_cert.c\
+  pubkey_public_key.h pubkey_public_key.c
+
 libstrongswan_pubkey_la_LDFLAGS = -module
 
index 77d2c86..37d0dcd 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <library.h>
 #include "pubkey_cert.h"
+#include "pubkey_public_key.h"
 
 typedef struct private_pubkey_plugin_t private_pubkey_plugin_t;
 
@@ -54,6 +55,8 @@ plugin_t *plugin_create()
 
        lib->creds->add_builder(lib->creds, CRED_CERTIFICATE, CERT_TRUSTED_PUBKEY,
                                                        (builder_constructor_t)pubkey_cert_builder);
+       lib->creds->add_builder(lib->creds, CRED_PUBLIC_KEY, KEY_ANY,
+                                                       (builder_constructor_t)pubkey_public_key_builder);
 
        return &this->public.plugin;
 }
diff --git a/src/libstrongswan/plugins/pubkey/pubkey_public_key.c b/src/libstrongswan/plugins/pubkey/pubkey_public_key.c
new file mode 100644 (file)
index 0000000..c4805aa
--- /dev/null
@@ -0,0 +1,160 @@
+/*
+ * Copyright (C) 2008 Martin Willi
+ * Copyright (C) 2000-2008 Andreas Steffen
+ * Hochschule fuer Technik Rapperswil
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ *
+ * $Id$
+ */
+
+#include "pubkey_public_key.h"
+
+#include <debug.h>
+#include <asn1/oid.h>
+#include <asn1/asn1.h>
+#include <asn1/asn1_parser.h>
+
+/**
+ * ASN.1 definition of a subjectPublicKeyInfo structure
+ */
+static const asn1Object_t pkinfoObjects[] = {
+       { 0, "subjectPublicKeyInfo",ASN1_SEQUENCE,              ASN1_NONE       }, /* 0 */
+       { 1,   "algorithm",                     ASN1_EOC,                       ASN1_RAW        }, /* 1 */
+       { 1,   "subjectPublicKey",      ASN1_BIT_STRING,        ASN1_BODY       }, /* 2 */
+       { 0, "exit",                            ASN1_EOC,                       ASN1_EXIT       }
+};
+#define PKINFO_SUBJECT_PUBLIC_KEY_ALGORITHM    1
+#define PKINFO_SUBJECT_PUBLIC_KEY                      2
+
+
+/**
+ * Load a public key from an ASN1 encoded blob
+ */
+static public_key_t *load(chunk_t blob)
+{
+       asn1_parser_t *parser;
+       chunk_t object;
+       int objectID;
+       public_key_t *key = NULL;
+       key_type_t type = KEY_ANY;
+
+       parser = asn1_parser_create(pkinfoObjects, blob);
+       
+       while (parser->iterate(parser, &objectID, &object))
+       {
+               switch (objectID)
+               {
+                       case PKINFO_SUBJECT_PUBLIC_KEY_ALGORITHM:
+                       {
+                               int oid = asn1_parse_algorithmIdentifier(object,
+                                                                               parser->get_level(parser)+1, NULL);
+                               
+                               if (oid == OID_RSA_ENCRYPTION)
+                               {
+                                       type = KEY_RSA;
+                               }
+                               else
+                               {
+                                       /* key type not supported */
+                                       goto end;
+                               }
+                               break;
+                       }
+                       case PKINFO_SUBJECT_PUBLIC_KEY:
+                               if (object.len > 0 && *object.ptr == 0x00)
+                               {
+                                       /* skip initial bit string octet defining 0 unused bits */
+                                       object = chunk_skip(object, 1);
+                               }
+                               key = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, type,
+                                                                                BUILD_BLOB_ASN1_DER, chunk_clone(object),
+                                                                                BUILD_END);
+                               break;
+               }
+       } 
+       
+end:
+       parser->destroy(parser);
+       free(blob.ptr);
+       return key; 
+}
+
+typedef struct private_builder_t private_builder_t;
+/**
+ * Builder implementation for key loading
+ */
+struct private_builder_t {
+       /** implements the builder interface */
+       builder_t public;
+       /** loaded public key */
+       public_key_t *key;
+};
+
+/**
+ * Implementation of builder_t.build
+ */
+static public_key_t *build(private_builder_t *this)
+{
+       public_key_t *key = this->key;
+       
+       free(this);
+       return key;
+}
+
+/**
+ * Implementation of builder_t.add
+ */
+static void add(private_builder_t *this, builder_part_t part, ...)
+{
+       va_list args;
+       
+       if (this->key)
+       {
+               DBG1("ignoring surplus build part %N", builder_part_names, part);
+               return;
+       }
+       switch (part)
+       {
+               case BUILD_BLOB_ASN1_DER:
+               {
+                       va_start(args, part);
+                       this->key = load(va_arg(args, chunk_t));
+                       va_end(args);
+                       break;
+               }
+               default:
+                       DBG1("ignoring unsupported build part %N", builder_part_names, part);
+                       break;
+       }
+}
+
+/**
+ * Builder construction function
+ */
+builder_t *pubkey_public_key_builder(key_type_t type)
+{
+       private_builder_t *this;
+       
+       if (type != KEY_ANY)
+       {
+               return NULL;
+       }
+       
+       this = malloc_thing(private_builder_t);
+       
+       this->key = NULL;
+       this->public.add = (void(*)(builder_t *this, builder_part_t part, ...))add;
+       this->public.build = (void*(*)(builder_t *this))build;
+       
+       return &this->public;
+}
+
diff --git a/src/libstrongswan/plugins/pubkey/pubkey_public_key.h b/src/libstrongswan/plugins/pubkey/pubkey_public_key.h
new file mode 100644 (file)
index 0000000..73fcd19
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2008 Martin Willi
+ * Hochschule fuer Technik Rapperswil
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ *
+ * $Id$
+ */
+
+/**
+ * @defgroup pubkey_public_key pubkey_public_key
+ * @{ @ingroup pubkey_p
+ */
+
+#ifndef PUBKEY_PUBLIC_KEY_H_
+#define PUBKEY_PUBLIC_KEY_H_
+
+#include <credentials/keys/public_key.h>
+
+/**
+ * Create the builder for a generic public key.
+ *
+ * @param type         type of the key, must be KEY_ANY
+ * @return                     builder instance
+ */
+builder_t *pubkey_public_key_builder(key_type_t type);
+
+#endif /*PUBKEY_RSA_PUBLIC_KEY_H_ @}*/