aboutsummaryrefslogtreecommitdiffstats
path: root/src/libstrongswan/plugins/pem
diff options
context:
space:
mode:
Diffstat (limited to 'src/libstrongswan/plugins/pem')
-rw-r--r--src/libstrongswan/plugins/pem/Makefile.am3
-rw-r--r--src/libstrongswan/plugins/pem/pem_encoder.c95
-rw-r--r--src/libstrongswan/plugins/pem/pem_encoder.h33
-rw-r--r--src/libstrongswan/plugins/pem/pem_plugin.c5
4 files changed, 135 insertions, 1 deletions
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;
}