diff options
author | Andreas Steffen <andreas.steffen@strongswan.org> | 2010-04-04 19:11:18 +0200 |
---|---|---|
committer | Andreas Steffen <andreas.steffen@strongswan.org> | 2010-04-04 19:11:18 +0200 |
commit | cea2857263a7fa62e3a3d8cbd791b3de1f4db2c6 (patch) | |
tree | e717e11e2e9083fc4a3879938828d1c96e79d6d2 /src | |
parent | e3943f5559b50f9c4a0acc93bbbfd9dc33a27276 (diff) | |
download | strongswan-cea2857263a7fa62e3a3d8cbd791b3de1f4db2c6.tar.bz2 strongswan-cea2857263a7fa62e3a3d8cbd791b3de1f4db2c6.tar.xz |
PEM encoding for GMP RSA public and private keys
Diffstat (limited to 'src')
-rw-r--r-- | src/libstrongswan/plugins/gmp/gmp_rsa_private_key.c | 17 | ||||
-rw-r--r-- | src/libstrongswan/plugins/gmp/gmp_rsa_public_key.c | 17 | ||||
-rw-r--r-- | src/libstrongswan/plugins/pem/Makefile.am | 3 | ||||
-rw-r--r-- | src/libstrongswan/plugins/pem/pem_encoder.c | 95 | ||||
-rw-r--r-- | src/libstrongswan/plugins/pem/pem_encoder.h | 33 | ||||
-rw-r--r-- | src/libstrongswan/plugins/pem/pem_plugin.c | 5 |
6 files changed, 167 insertions, 3 deletions
diff --git a/src/libstrongswan/plugins/gmp/gmp_rsa_private_key.c b/src/libstrongswan/plugins/gmp/gmp_rsa_private_key.c index 211ce411e..ac2bad5bf 100644 --- a/src/libstrongswan/plugins/gmp/gmp_rsa_private_key.c +++ b/src/libstrongswan/plugins/gmp/gmp_rsa_private_key.c @@ -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; } diff --git a/src/libstrongswan/plugins/gmp/gmp_rsa_public_key.c b/src/libstrongswan/plugins/gmp/gmp_rsa_public_key.c index d20767177..111533bec 100644 --- a/src/libstrongswan/plugins/gmp/gmp_rsa_public_key.c +++ b/src/libstrongswan/plugins/gmp/gmp_rsa_public_key.c @@ -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; } diff --git a/src/libstrongswan/plugins/pem/Makefile.am b/src/libstrongswan/plugins/pem/Makefile.am index f43423a85..b815b1e0b 100644 --- a/src/libstrongswan/plugins/pem/Makefile.am +++ b/src/libstrongswan/plugins/pem/Makefile.am @@ -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 diff --git a/src/libstrongswan/plugins/pem/pem_encoder.c b/src/libstrongswan/plugins/pem/pem_encoder.c new file mode 100644 index 000000000..65073f50c --- /dev/null +++ b/src/libstrongswan/plugins/pem/pem_encoder.c @@ -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; +} + diff --git a/src/libstrongswan/plugins/pem/pem_encoder.h b/src/libstrongswan/plugins/pem/pem_encoder.h new file mode 100644 index 000000000..a181133b7 --- /dev/null +++ b/src/libstrongswan/plugins/pem/pem_encoder.h @@ -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_ @}*/ + diff --git a/src/libstrongswan/plugins/pem/pem_plugin.c b/src/libstrongswan/plugins/pem/pem_plugin.c index b76987272..810901b7a 100644 --- a/src/libstrongswan/plugins/pem/pem_plugin.c +++ b/src/libstrongswan/plugins/pem/pem_plugin.c @@ -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; } |