Add ECDSA key generation.
authorGuus Sliepen <guus@tinc-vpn.org>
Sun, 3 Jul 2011 20:15:00 +0000 (22:15 +0200)
committerGuus Sliepen <guus@tinc-vpn.org>
Sun, 3 Jul 2011 20:15:00 +0000 (22:15 +0200)
src/Makefile.am
src/openssl/ecdsagen.c [new file with mode: 0644]
src/openssl/ecdsagen.h [new file with mode: 0644]

index a7c56c8..3303374 100644 (file)
@@ -11,7 +11,7 @@ tincd_SOURCES = \
        protocol_key.c protocol_subnet.c route.c subnet.c tincd.c
 
 nodist_tincd_SOURCES = \
-       device.c cipher.c crypto.c ecdh.c digest.c prf.c rsa.c
+       device.c cipher.c crypto.c ecdh.c ecdsagen.c digest.c prf.c rsa.c
 
 tincctl_SOURCES = \
        utils.c getopt.c getopt1.c dropin.c \
@@ -32,7 +32,7 @@ INCLUDES = @INCLUDES@ -I$(top_builddir)
 
 noinst_HEADERS = \
        xalloc.h utils.h getopt.h list.h splay_tree.h dropin.h fake-getaddrinfo.h fake-getnameinfo.h fake-gai-errnos.h ipv6.h ipv4.h ethernet.h \
-       buffer.h cipher.h conf.h connection.h control.h control_common.h crypto.h device.h ecdh.h digest.h edge.h graph.h logger.h meta.h net.h netutl.h node.h prf.h process.h \
+       buffer.h cipher.h conf.h connection.h control.h control_common.h crypto.h device.h digest.h ecdh.h ecdsagen.c edge.h graph.h logger.h meta.h net.h netutl.h node.h prf.h process.h \
        protocol.h route.h rsa.h rsagen.h subnet.h tincctl.h top.h bsd/tunemu.h
 
 LIBS = @LIBS@ @LIBGCRYPT_LIBS@
diff --git a/src/openssl/ecdsagen.c b/src/openssl/ecdsagen.c
new file mode 100644 (file)
index 0000000..93b0df6
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+    ecdsagen.c -- ECDSA key generation and export
+    Copyright (C) 2008 Guus Sliepen <guus@tinc-vpn.org>
+
+    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.
+
+    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.
+
+    You should have received a copy of the GNU General Public License along
+    with this program; if not, write to the Free Software Foundation, Inc.,
+    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "system.h"
+
+#include <openssl/pem.h>
+#include <openssl/err.h>
+#include <openssl/obj_mac.h>
+
+#include "logger.h"
+#include "ecdsagen.h"
+#include "utils.h"
+
+// Generate ECDSA key
+
+bool ecdsa_generate(ecdsa_t *ecdsa) {
+       *ecdsa = EC_KEY_new_by_curve_name(NID_secp521r1);
+
+       if(!EC_KEY_generate_key(*ecdsa)) {
+               logger(LOG_ERR, "Generating EC key failed: %s", ERR_error_string(ERR_get_error(), NULL));
+               abort();
+       }
+
+       EC_KEY_set_asn1_flag(*ecdsa, OPENSSL_EC_NAMED_CURVE);
+       EC_KEY_set_conv_form(*ecdsa, POINT_CONVERSION_COMPRESSED);
+
+       return true;
+}
+
+// Write PEM ECDSA keys
+
+bool ecdsa_write_pem_public_key(ecdsa_t *ecdsa, FILE *fp) {
+       BIO *out = BIO_new(BIO_s_file());
+       BIO_set_fp(out,fp,BIO_NOCLOSE);
+       PEM_write_bio_EC_PUBKEY(out, *ecdsa);
+       BIO_free(out);
+       return true;
+}
+
+bool ecdsa_write_pem_private_key(ecdsa_t *ecdsa, FILE *fp) {
+       BIO *out = BIO_new(BIO_s_file());
+       BIO_set_fp(out,fp,BIO_NOCLOSE);
+       PEM_write_bio_ECPrivateKey(out, *ecdsa, NULL, NULL, 0, NULL, NULL);
+       BIO_free(out);
+       return true;
+}
+
+// Convert ECDSA public key to base64 format
+
+char *ecdsa_get_base64_public_key(ecdsa_t *ecdsa) {
+       unsigned char *pubkey = NULL;
+       int len = i2o_ECPublicKey(*ecdsa, &pubkey);
+
+       char *base64 = malloc(len * 4 / 3 + 5);
+       b64encode(pubkey, base64, len);
+
+       free(pubkey);
+
+       return base64;
+}
diff --git a/src/openssl/ecdsagen.h b/src/openssl/ecdsagen.h
new file mode 100644 (file)
index 0000000..8a40e45
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+    ecdsagen.h -- ECDSA key generation and export
+    Copyright (C) 2011 Guus Sliepen <guus@tinc-vpn.org>
+
+    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.
+
+    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.
+
+    You should have received a copy of the GNU General Public License along
+    with this program; if not, write to the Free Software Foundation, Inc.,
+    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#ifndef __TINC_ECDSAGEN_H__
+#define __TINC_ECDSAGEN_H__
+
+#include "ecdsa.h"
+
+extern bool ecdsa_generate(ecdsa_t *ecdsa);
+extern bool ecdsa_write_pem_public_key(ecdsa_t *ecdsa, FILE *fp);
+extern bool ecdsa_write_pem_private_key(ecdsa_t *ecdsa, FILE *fp);
+extern char *ecdsa_get_base64_public_key(ecdsa_t *ecdsa);
+
+#endif