PEM encoding for GMP RSA public and private keys

This commit is contained in:
Andreas Steffen 2010-04-04 19:11:18 +02:00
parent e3943f5559
commit cea2857263
6 changed files with 167 additions and 3 deletions

View File

@ -404,7 +404,13 @@ static bool get_encoding(private_gmp_rsa_private_key_t *this,
key_encoding_type_t type, chunk_t *encoding)
{
chunk_t n, e, d, p, q, exp1, exp2, coeff;
bool success;
bool success, pem = FALSE;
if (type == KEY_PRIV_PEM)
{
pem = TRUE;
type = KEY_PRIV_ASN1_DER;
}
n = gmp_mpz_to_chunk(this->n);
e = gmp_mpz_to_chunk(this->e);
@ -430,6 +436,15 @@ static bool get_encoding(private_gmp_rsa_private_key_t *this,
chunk_clear(&exp2);
chunk_clear(&coeff);
if (pem && success)
{
chunk_t asn1_encoding = *encoding;
success = lib->encoding->encode(lib->encoding, KEY_PRIV_PEM, NULL,
encoding, KEY_PART_RSA_PRIV_ASN1_DER,
asn1_encoding, KEY_PART_END);
chunk_clear(&asn1_encoding);
}
return success;
}

View File

@ -396,7 +396,13 @@ static bool get_encoding(private_gmp_rsa_public_key_t *this,
key_encoding_type_t type, chunk_t *encoding)
{
chunk_t n, e;
bool success;
bool success, pem = FALSE;
if (type == KEY_PUB_PEM)
{
pem = TRUE;
type = KEY_PUB_SPKI_ASN1_DER;
}
n = gmp_mpz_to_chunk(this->n);
e = gmp_mpz_to_chunk(this->e);
@ -406,6 +412,15 @@ static bool get_encoding(private_gmp_rsa_public_key_t *this,
chunk_free(&n);
chunk_free(&e);
if (pem && success)
{
chunk_t asn1_encoding = *encoding;
success = lib->encoding->encode(lib->encoding, KEY_PUB_PEM, NULL,
encoding, KEY_PART_RSA_PUB_ASN1_DER,
asn1_encoding, KEY_PART_END);
chunk_clear(&asn1_encoding);
}
return success;
}

View File

@ -11,6 +11,7 @@ endif
libstrongswan_pem_la_SOURCES = \
pem_plugin.h pem_plugin.c \
pem_builder.c pem_builder.h
pem_builder.c pem_builder.h \
pem_encoder.c pem_encoder.h
libstrongswan_pem_la_LDFLAGS = -module -avoid-version

View File

@ -0,0 +1,95 @@
/*
* Copyright (C) 2010 Andreas Steffen
* 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 "pem_encoder.h"
#define BYTES_PER_LINE 48
/**
* See header.
*/
bool pem_encoder_encode(key_encoding_type_t type, chunk_t *encoding,
va_list args)
{
chunk_t asn1;
char *label;
u_char *pos;
size_t len, written, pem_chars, pem_lines;
switch (type)
{
case KEY_PUB_PEM:
if (!key_encoding_args(args, KEY_PART_RSA_PUB_ASN1_DER,
&asn1, KEY_PART_END))
{
return FALSE;
}
label ="PUBLIC KEY";
break;
case KEY_PRIV_PEM:
if (!key_encoding_args(args, KEY_PART_RSA_PRIV_ASN1_DER,
&asn1, KEY_PART_END))
{
return FALSE;
}
label ="RSA PRIVATE KEY";
break;
default:
return FALSE;
}
/* compute and allocate maximum size of PEM object */
pem_chars = 4*(asn1.len + 2)/3;
pem_lines = (asn1.len + BYTES_PER_LINE - 1) / BYTES_PER_LINE;
*encoding = chunk_alloc(5 + 2*(6 + strlen(label) + 6) + 3 + pem_chars + pem_lines);
pos = encoding->ptr;
len = encoding->len;
/* write PEM header */
written = snprintf(pos, len, "-----BEGIN %s-----\n", label);
pos += written;
len -= written;
/* write PEM body */
while (pem_lines--)
{
chunk_t asn1_line, pem_line;
asn1_line = chunk_create(asn1.ptr, min(asn1.len, BYTES_PER_LINE));
asn1.ptr += asn1_line.len;
asn1.len -= asn1_line.len;
pem_line = chunk_to_base64(asn1_line, pos);
pos += pem_line.len;
len -= pem_line.len;
*pos = '\n';
pos++;
len--;
}
/* write PEM trailer */
written = snprintf(pos, len, "-----END %s-----", label);
pos += written;
len -= written;
/* replace termination null character with newline */
*pos = '\n';
pos++;
len--;
/* compute effective length of PEM object */
encoding->len = pos - encoding->ptr;
return TRUE;
}

View File

@ -0,0 +1,33 @@
/*
* Copyright (C) 2010 Andreas Steffen
* 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 pem_encoder pem_encoder
* @{ @ingroup pem_p
*/
#ifndef PEM_ENCODER_H_
#define PEM_ENCODER_H_
#include <credentials/keys/key_encoding.h>
/**
* Encoding from ASN.1 to PEM format.
*/
bool pem_encoder_encode(key_encoding_type_t type, chunk_t *encoding,
va_list args);
#endif /** PEM_ENCODER_H_ @}*/

View File

@ -16,7 +16,9 @@
#include "pem_plugin.h"
#include <library.h>
#include "pem_builder.h"
#include "pem_encoder.h"
typedef struct private_pem_plugin_t private_pem_plugin_t;
@ -100,6 +102,9 @@ plugin_t *pem_plugin_create()
lib->creds->add_builder(lib->creds, CRED_CERTIFICATE, CERT_PLUTO_CRL,
(builder_function_t)pem_certificate_load);
/* register PEM encoder */
lib->encoding->add_encoder(lib->encoding, pem_encoder_encode);
return &this->public.plugin;
}