]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
sshkey: Add encoder for RSA keys
authorTobias Brunner <tobias@strongswan.org>
Thu, 15 Aug 2013 10:42:09 +0000 (12:42 +0200)
committerTobias Brunner <tobias@strongswan.org>
Fri, 13 Sep 2013 13:23:49 +0000 (15:23 +0200)
src/libstrongswan/credentials/cred_encoding.h
src/libstrongswan/plugins/sshkey/Makefile.am
src/libstrongswan/plugins/sshkey/sshkey_builder.h
src/libstrongswan/plugins/sshkey/sshkey_encoder.c [new file with mode: 0644]
src/libstrongswan/plugins/sshkey/sshkey_encoder.h [new file with mode: 0644]
src/libstrongswan/plugins/sshkey/sshkey_plugin.c

index 41481f376ca84c76c506675e3f497040d3044b74..27a887f27567e82847d6c83a8410859139f807d7 100644 (file)
@@ -87,6 +87,8 @@ enum cred_encoding_type_t {
        PRIVKEY_PGP,
        /** DNSKEY encoding */
        PUBKEY_DNSKEY,
+       /** SSHKEY encoding (Base64) */
+       PUBKEY_SSHKEY,
 
        /** ASN.1 DER encoded certificate */
        CERT_ASN1_DER,
index d2ec631a8dc5b97eec698ed7f73522afe390cf38..22c076f84d43e026de54bda01e2b00880c38a20b 100644 (file)
@@ -12,6 +12,7 @@ endif
 
 libstrongswan_sshkey_la_SOURCES = \
        sshkey_plugin.h sshkey_plugin.c \
-       sshkey_builder.h sshkey_builder.c
+       sshkey_builder.h sshkey_builder.c \
+       sshkey_encoder.h sshkey_encoder.c
 
 libstrongswan_sshkey_la_LDFLAGS = -module -avoid-version
index e4c7a90d0db5399b83833f56eea54593daf0888e..d138c879b8f3917b38f445197f24005cb5c26c55 100644 (file)
@@ -14,7 +14,7 @@
  */
 
 /**
- * @defgroup sshky_public_key sshky_public_key
+ * @defgroup sshkey_public_key sshkey_public_key
  * @{ @ingroup sshkey_p
  */
 
diff --git a/src/libstrongswan/plugins/sshkey/sshkey_encoder.c b/src/libstrongswan/plugins/sshkey/sshkey_encoder.c
new file mode 100644 (file)
index 0000000..8f0cb6b
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2013 Tobias Brunner
+ * 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 "sshkey_encoder.h"
+
+#include <bio/bio_writer.h>
+
+/**
+ * Encode the public key as Base64 encoded SSH key blob
+ */
+static bool build_public_key(chunk_t *encoding, va_list args)
+{
+       bio_writer_t *writer;
+       chunk_t n, e;
+
+       if (cred_encoding_args(args, CRED_PART_RSA_MODULUS, &n,
+                                                  CRED_PART_RSA_PUB_EXP, &e, CRED_PART_END))
+       {
+               writer = bio_writer_create(0);
+               writer->write_data32(writer, chunk_from_str("ssh-rsa"));
+
+               writer->write_data32(writer, e);
+               writer->write_data32(writer, n);
+               *encoding = chunk_to_base64(writer->get_buf(writer), NULL);
+               writer->destroy(writer);
+               return TRUE;
+       }
+       return FALSE;
+}
+
+bool sshkey_encoder_encode(cred_encoding_type_t type, chunk_t *encoding,
+                                                  va_list args)
+{
+       switch (type)
+       {
+               case PUBKEY_SSHKEY:
+                       return build_public_key(encoding, args);
+               default:
+                       return FALSE;
+       }
+}
diff --git a/src/libstrongswan/plugins/sshkey/sshkey_encoder.h b/src/libstrongswan/plugins/sshkey/sshkey_encoder.h
new file mode 100644 (file)
index 0000000..bdd31a6
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2013 Tobias Brunner
+ * 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 sshkey_encoder sshkey_encoder
+ * @{ @ingroup sshkey_p
+ */
+
+#ifndef SSHKEY_ENCODER_H_
+#define SSHKEY_ENCODER_H_
+
+#include <credentials/cred_encoding.h>
+
+/**
+ * Encoding of public keys to RFC 4253 format.
+ */
+bool sshkey_encoder_encode(cred_encoding_type_t type, chunk_t *encoding,
+                                                  va_list args);
+
+#endif /** SSHKEY_ENCODER_H_ @}*/
index fe6252671de5df905e3b2d00b1f0e2a615de1d58..6409feaf1e594c00782cf71df703d7020814393f 100644 (file)
@@ -17,6 +17,7 @@
 
 #include <library.h>
 #include "sshkey_builder.h"
+#include "sshkey_encoder.h"
 
 typedef struct private_sshkey_plugin_t private_sshkey_plugin_t;
 
@@ -51,6 +52,7 @@ METHOD(plugin_t, get_features, int,
 METHOD(plugin_t, destroy, void,
        private_sshkey_plugin_t *this)
 {
+       lib->encoding->remove_encoder(lib->encoding, sshkey_encoder_encode);
        free(this);
 }
 
@@ -70,6 +72,7 @@ plugin_t *sshkey_plugin_create()
                        },
                },
        );
+       lib->encoding->add_encoder(lib->encoding, sshkey_encoder_encode);
 
        return &this->public.plugin;
 }