aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMartin Willi <martin@strongswan.org>2009-08-19 16:10:08 +0200
committerMartin Willi <martin@strongswan.org>2009-08-26 11:23:51 +0200
commite773fe4cab7b9d20ef4d50f49893a1779463cf92 (patch)
tree4f851b8efdd4eba73a7b51ac541d53505a1c3167 /src
parent934d49a4f96e129616e75a946136158e3dfc022c (diff)
downloadstrongswan-e773fe4cab7b9d20ef4d50f49893a1779463cf92.tar.bz2
strongswan-e773fe4cab7b9d20ef4d50f49893a1779463cf92.tar.xz
implemented pkcs1 private/public key encoding and fingerprinting
Diffstat (limited to 'src')
-rw-r--r--src/libstrongswan/plugins/pkcs1/Makefile.am1
-rw-r--r--src/libstrongswan/plugins/pkcs1/pkcs1_encoder.c154
-rw-r--r--src/libstrongswan/plugins/pkcs1/pkcs1_encoder.h32
-rw-r--r--src/libstrongswan/plugins/pkcs1/pkcs1_plugin.c6
4 files changed, 193 insertions, 0 deletions
diff --git a/src/libstrongswan/plugins/pkcs1/Makefile.am b/src/libstrongswan/plugins/pkcs1/Makefile.am
index d50b9184b..88d25a26b 100644
--- a/src/libstrongswan/plugins/pkcs1/Makefile.am
+++ b/src/libstrongswan/plugins/pkcs1/Makefile.am
@@ -6,6 +6,7 @@ AM_CFLAGS = -rdynamic
plugin_LTLIBRARIES = libstrongswan-pkcs1.la
libstrongswan_pkcs1_la_SOURCES = pkcs1_plugin.h pkcs1_plugin.c \
+ pkcs1_encoder.h pkcs1_encoder.c \
pkcs1_builder.h pkcs1_builder.c
libstrongswan_pkcs1_la_LDFLAGS = -module -avoid-version
diff --git a/src/libstrongswan/plugins/pkcs1/pkcs1_encoder.c b/src/libstrongswan/plugins/pkcs1/pkcs1_encoder.c
new file mode 100644
index 000000000..47848bc73
--- /dev/null
+++ b/src/libstrongswan/plugins/pkcs1/pkcs1_encoder.c
@@ -0,0 +1,154 @@
+/*
+ * Copyright (C) 2009 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.
+ */
+
+#include "pkcs1_encoder.h"
+
+#include <debug.h>
+#include <asn1/asn1.h>
+#include <asn1/oid.h>
+
+/**
+ * Build the SHA1 hash of pubkey(info) ASN.1 data
+ */
+static bool hash_pubkey(chunk_t pubkey, chunk_t *hash)
+{
+ hasher_t *hasher;
+
+ hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
+ if (hasher == NULL)
+ {
+ chunk_free(&pubkey);
+ DBG1("SHA1 hash algorithm not supported, fingerprinting failed");
+ return FALSE;
+ }
+ hasher->allocate_hash(hasher, pubkey, hash);
+ hasher->destroy(hasher);
+ chunk_free(&pubkey);
+ return TRUE;
+}
+
+/**
+ * build the fingerprint of the subjectPublicKeyInfo object
+ */
+static bool build_info_sha1(chunk_t *encoding, va_list args)
+{
+ chunk_t n, e, pubkey;
+
+ if (key_encoding_args(args, KEY_PART_RSA_MODULUS, &n,
+ KEY_PART_RSA_PUB_EXP, &e, KEY_PART_END))
+ {
+ pubkey = asn1_wrap(ASN1_SEQUENCE, "cm",
+ asn1_algorithmIdentifier(OID_RSA_ENCRYPTION),
+ asn1_bitstring("m",
+ asn1_wrap(ASN1_SEQUENCE, "mm",
+ asn1_wrap(ASN1_INTEGER, "c", n),
+ asn1_wrap(ASN1_INTEGER, "c", e))));
+ }
+ else
+ {
+ return FALSE;
+ }
+ return hash_pubkey(pubkey, encoding);
+}
+
+/**
+ * build the fingerprint of the subjectPublicKey object
+ */
+static bool build_sha1(chunk_t *encoding, va_list args)
+{
+ chunk_t n, e, pubkey;
+
+ if (key_encoding_args(args, KEY_PART_RSA_MODULUS, &n,
+ KEY_PART_RSA_PUB_EXP, &e, KEY_PART_END))
+ {
+ pubkey = asn1_wrap(ASN1_SEQUENCE, "mm",
+ asn1_wrap(ASN1_INTEGER, "c", n),
+ asn1_wrap(ASN1_INTEGER, "c", e));
+ }
+ else
+ {
+ return FALSE;
+ }
+ return hash_pubkey(pubkey, encoding);
+}
+
+/**
+ * Encode a public key in PKCS#1/ASN.1 DER
+ */
+bool build_pub(chunk_t *encoding, va_list args)
+{
+ chunk_t n, e;
+
+ if (key_encoding_args(args, KEY_PART_RSA_MODULUS, &n,
+ KEY_PART_RSA_PUB_EXP, &e, KEY_PART_END))
+ {
+ *encoding = asn1_wrap(ASN1_SEQUENCE, "mm",
+ asn1_wrap(ASN1_INTEGER, "c", n),
+ asn1_wrap(ASN1_INTEGER, "c", e));
+ return TRUE;
+ }
+ return FALSE;
+}
+
+/**
+ * Encode a private key in PKCS#1/ASN.1 DER
+ */
+bool build_priv(chunk_t *encoding, va_list args)
+{
+ chunk_t n, e, d, p, q, exp1, exp2, coeff;
+
+ if (key_encoding_args(args, KEY_PART_RSA_MODULUS, &n,
+ KEY_PART_RSA_PUB_EXP, &e, KEY_PART_RSA_PRIV_EXP, &d,
+ KEY_PART_RSA_PRIME1, &p, KEY_PART_RSA_PRIME2, &q,
+ KEY_PART_RSA_EXP1, &exp1, KEY_PART_RSA_EXP2, &exp2,
+ KEY_PART_RSA_COEFF, &coeff, KEY_PART_END))
+ {
+ *encoding = asn1_wrap(ASN1_SEQUENCE, "cmmssssss",
+ ASN1_INTEGER_0,
+ asn1_wrap(ASN1_INTEGER, "c", n),
+ asn1_wrap(ASN1_INTEGER, "c", e),
+ asn1_wrap(ASN1_INTEGER, "c", d),
+ asn1_wrap(ASN1_INTEGER, "c", p),
+ asn1_wrap(ASN1_INTEGER, "c", q),
+ asn1_wrap(ASN1_INTEGER, "c", exp1),
+ asn1_wrap(ASN1_INTEGER, "c", exp2),
+ asn1_wrap(ASN1_INTEGER, "c", coeff));
+ return TRUE;
+ }
+ return FALSE;
+}
+
+/**
+ * See header.
+ */
+bool pkcs1_encoder_encode(key_encoding_type_t type, chunk_t *encoding,
+ va_list args)
+{
+ switch (type)
+ {
+ case KEY_ID_PUBKEY_INFO_SHA1:
+ return build_info_sha1(encoding, args);
+ case KEY_ID_PUBKEY_SHA1:
+ return build_sha1(encoding, args);
+ case KEY_PUB_ASN1_DER:
+ return build_pub(encoding, args);
+ case KEY_PRIV_ASN1_DER:
+ return build_priv(encoding, args);
+ default:
+ return FALSE;
+ }
+}
+
+
diff --git a/src/libstrongswan/plugins/pkcs1/pkcs1_encoder.h b/src/libstrongswan/plugins/pkcs1/pkcs1_encoder.h
new file mode 100644
index 000000000..c0bb688ed
--- /dev/null
+++ b/src/libstrongswan/plugins/pkcs1/pkcs1_encoder.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2009 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.
+ */
+
+/**
+ * @defgroup pkcs1_encoder pkcs1_encoder
+ * @{ @ingroup pkcs1
+ */
+
+#ifndef PKCS1_ENCODER_H_
+#define PKCS1_ENCODER_H_
+
+#include <credentials/keys/key_encoding.h>
+
+/**
+ * Encoding function for PKCS#1/ASN.1 fingerprints/key formats.
+ */
+bool pkcs1_encoder_encode(key_encoding_type_t type, chunk_t *encoding,
+ va_list args);
+
+#endif /* PKCS1_ENCODER_ @}*/
diff --git a/src/libstrongswan/plugins/pkcs1/pkcs1_plugin.c b/src/libstrongswan/plugins/pkcs1/pkcs1_plugin.c
index 453a28d7d..5e8cf97d8 100644
--- a/src/libstrongswan/plugins/pkcs1/pkcs1_plugin.c
+++ b/src/libstrongswan/plugins/pkcs1/pkcs1_plugin.c
@@ -17,6 +17,7 @@
#include <library.h>
#include "pkcs1_builder.h"
+#include "pkcs1_encoder.h"
typedef struct private_pkcs1_plugin_t private_pkcs1_plugin_t;
@@ -40,6 +41,9 @@ static void destroy(private_pkcs1_plugin_t *this)
(builder_constructor_t)pkcs1_public_key_builder);
lib->creds->remove_builder(lib->creds,
(builder_constructor_t)pkcs1_private_key_builder);
+
+ lib->encoding->remove_encoder(lib->encoding, pkcs1_encoder_encode);
+
free(this);
}
@@ -59,6 +63,8 @@ plugin_t *plugin_create()
lib->creds->add_builder(lib->creds, CRED_PRIVATE_KEY, KEY_RSA,
(builder_constructor_t)pkcs1_private_key_builder);
+ lib->encoding->add_encoder(lib->encoding, pkcs1_encoder_encode);
+
return &this->public.plugin;
}