From e1fcd85fb32ae750cf0845c1335bc79f5b6fc56b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Love=20H=C3=B6rnquist=20=C3=85strand?= Date: Mon, 25 Jun 2007 16:44:24 +0000 Subject: [PATCH] (hx509_pem_write): Add. git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@21301 ec53bebd-3082-4978-b11e-865c3cabbd6b --- lib/hx509/file.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/lib/hx509/file.c b/lib/hx509/file.c index 1152af242..f63a1afaa 100644 --- a/lib/hx509/file.c +++ b/lib/hx509/file.c @@ -134,3 +134,52 @@ _hx509_write_file(const char *fn, const void *data, size_t length) return 0; } + +static void +header(FILE *f, const char *type, const char *str) +{ + fprintf(f, "-----%s %s-----\n", type, str); +} + +int +hx509_pem_write(hx509_context context, const char *type, + char **headers, FILE *f, + const void *data, size_t size) +{ + const char *p = data; + size_t length; + char *line; + int i; + +#define ENCODE_LINE_LENGTH 54 + + header(f, "BEGIN", type); + + for(i = 0; headers && headers[i]; i++) + fprintf(f, "%s\n", headers[i]); + if (i > 0) + fprintf(f, "\n"); + + while (size > 0) { + ssize_t l; + + length = size; + if (length > ENCODE_LINE_LENGTH) + length = ENCODE_LINE_LENGTH; + + l = base64_encode(p, length, &line); + if (l < 0) { + hx509_set_error_string(context, 0, ENOMEM, + "malloc - out of memory"); + return ENOMEM; + } + size -= length; + fprintf(f, "%s\n", line); + p += length; + free(line); + } + + header(f, "END", type); + + return 0; +}