diff options
author | Tobias Brunner <tobias@strongswan.org> | 2013-08-15 12:42:09 +0200 |
---|---|---|
committer | Tobias Brunner <tobias@strongswan.org> | 2013-09-13 15:23:49 +0200 |
commit | f40e9f4d161d79a5e4c93c4b0423c6535b9553f9 (patch) | |
tree | de138620c88f1473b0bc493fe95ad502f7168310 | |
parent | 3b939e20a96984584a941da452513220ef0c38af (diff) | |
download | strongswan-f40e9f4d161d79a5e4c93c4b0423c6535b9553f9.tar.bz2 strongswan-f40e9f4d161d79a5e4c93c4b0423c6535b9553f9.tar.xz |
sshkey: Add encoder for RSA keys
-rw-r--r-- | src/libstrongswan/credentials/cred_encoding.h | 2 | ||||
-rw-r--r-- | src/libstrongswan/plugins/sshkey/Makefile.am | 3 | ||||
-rw-r--r-- | src/libstrongswan/plugins/sshkey/sshkey_builder.h | 2 | ||||
-rw-r--r-- | src/libstrongswan/plugins/sshkey/sshkey_encoder.c | 53 | ||||
-rw-r--r-- | src/libstrongswan/plugins/sshkey/sshkey_encoder.h | 32 | ||||
-rw-r--r-- | src/libstrongswan/plugins/sshkey/sshkey_plugin.c | 3 |
6 files changed, 93 insertions, 2 deletions
diff --git a/src/libstrongswan/credentials/cred_encoding.h b/src/libstrongswan/credentials/cred_encoding.h index 41481f376..27a887f27 100644 --- a/src/libstrongswan/credentials/cred_encoding.h +++ b/src/libstrongswan/credentials/cred_encoding.h @@ -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, diff --git a/src/libstrongswan/plugins/sshkey/Makefile.am b/src/libstrongswan/plugins/sshkey/Makefile.am index d2ec631a8..22c076f84 100644 --- a/src/libstrongswan/plugins/sshkey/Makefile.am +++ b/src/libstrongswan/plugins/sshkey/Makefile.am @@ -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 diff --git a/src/libstrongswan/plugins/sshkey/sshkey_builder.h b/src/libstrongswan/plugins/sshkey/sshkey_builder.h index e4c7a90d0..d138c879b 100644 --- a/src/libstrongswan/plugins/sshkey/sshkey_builder.h +++ b/src/libstrongswan/plugins/sshkey/sshkey_builder.h @@ -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 index 000000000..8f0cb6b63 --- /dev/null +++ b/src/libstrongswan/plugins/sshkey/sshkey_encoder.c @@ -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 index 000000000..bdd31a6c8 --- /dev/null +++ b/src/libstrongswan/plugins/sshkey/sshkey_encoder.h @@ -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_ @}*/ diff --git a/src/libstrongswan/plugins/sshkey/sshkey_plugin.c b/src/libstrongswan/plugins/sshkey/sshkey_plugin.c index fe6252671..6409feaf1 100644 --- a/src/libstrongswan/plugins/sshkey/sshkey_plugin.c +++ b/src/libstrongswan/plugins/sshkey/sshkey_plugin.c @@ -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; } |