added keyid2sql helper script
authorAndreas Steffen <andreas.steffen@strongswan.org>
Tue, 29 Jul 2008 19:46:39 +0000 (19:46 -0000)
committerAndreas Steffen <andreas.steffen@strongswan.org>
Tue, 29 Jul 2008 19:46:39 +0000 (19:46 -0000)
scripts/Makefile.am
scripts/keyid2sql.c [new file with mode: 0644]

index 845409c..baf69ed 100644 (file)
@@ -1,10 +1,12 @@
 INCLUDES = -I$(top_srcdir)/src/libstrongswan
 AM_CFLAGS =-DIPSEC_PLUGINDIR=\"${plugindir}\"
 
-noinst_PROGRAMS = bin2array bin2sql id2sql key2keyid
+noinst_PROGRAMS = bin2array bin2sql id2sql key2keyid keyid2sql
 bin2array_SOURCES = bin2array.c
 bin2sql_SOURCES = bin2sql.c
 id2sql_SOURCES = id2sql.c
 key2keyid_SOURCES = key2keyid.c
+keyid2sql_SOURCES = keyid2sql.c
 id2sql_LDADD = $(top_builddir)/src/libstrongswan/libstrongswan.la
 key2keyid_LDADD = $(top_builddir)/src/libstrongswan/libstrongswan.la
+keyid2sql_LDADD = $(top_builddir)/src/libstrongswan/libstrongswan.la
diff --git a/scripts/keyid2sql.c b/scripts/keyid2sql.c
new file mode 100644 (file)
index 0000000..f7d447a
--- /dev/null
@@ -0,0 +1,76 @@
+
+#include <stdio.h>
+#include <library.h>
+#include <debug.h>
+
+/**
+ * print the keyids of a private or public key in sql format
+ */
+int main(int argc, char *argv[])
+{
+       public_key_t *public;
+       private_key_t *private;
+       identification_t *keyid;
+       chunk_t chunk;
+       char buf[8096];
+       int read, n;
+       
+       library_init(NULL);
+       lib->plugins->load(lib->plugins, IPSEC_PLUGINDIR, "gmp pubkey sha1");
+       atexit(library_deinit);
+
+       read = fread(buf, 1, sizeof(buf), stdin);
+       if (read <= 0)
+       {
+               fprintf(stderr, "reading key failed.\n");
+               return -1;
+       }
+       
+       chunk = chunk_create(buf, read);
+       
+       private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, KEY_RSA,
+                                                                BUILD_BLOB_ASN1_DER, chunk_clone(chunk),
+                                                                BUILD_END);
+       if (private)
+       {
+               keyid = private->get_id(private, ID_PUBKEY_INFO_SHA1);
+               chunk = keyid->get_encoding(keyid);
+
+               printf("%d, X'", ID_PUBKEY_INFO_SHA1);
+               for (n = 0; n < chunk.len; n++)
+               {
+                       printf("%.2x", chunk.ptr[n]);
+               }
+               printf("'\n");
+               private->destroy(private);
+               return 0;
+       }
+       
+       public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_ANY,
+                                                               BUILD_BLOB_ASN1_DER, chunk_clone(chunk),
+                                                               BUILD_END);
+       if (!public)
+       {
+               public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_RSA,
+                                                                       BUILD_BLOB_ASN1_DER, chunk_clone(chunk),
+                                                                       BUILD_END);
+       }
+       if (public)
+       {
+               keyid = public->get_id(public, ID_PUBKEY_INFO_SHA1);
+               chunk = keyid->get_encoding(keyid);
+
+               printf("%d, X'", ID_PUBKEY_INFO_SHA1);
+               for (n = 0; n < chunk.len; n++)
+               {
+                       printf("%.2x", chunk.ptr[n]);
+               }
+               printf("'\n");
+               public->destroy(public);
+               return 0;
+       }
+       
+       fprintf(stderr, "unable to parse input key.\n");
+       return -1;
+}
+