sshkey: Add encoder for RSA keys

This commit is contained in:
Tobias Brunner
2013-09-13 15:23:49 +02:00
parent 3b939e20a9
commit f40e9f4d16
6 changed files with 93 additions and 2 deletions
@@ -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,
+2 -1
View 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
@@ -14,7 +14,7 @@
*/
/**
* @defgroup sshky_public_key sshky_public_key
* @defgroup sshkey_public_key sshkey_public_key
* @{ @ingroup sshkey_p
*/
@@ -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;
}
}
@@ -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_ @}*/
@@ -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;
}