PEM encoder supports encoding from RSA components directly, allowing gcrypt plugin...
[strongswan.git] / src / libstrongswan / plugins / pem / pem_encoder.c
1 /*
2 * Copyright (C) 2010 Andreas Steffen
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "pem_encoder.h"
17
18 #define BYTES_PER_LINE 48
19
20 /**
21 * See header.
22 */
23 bool pem_encoder_encode(key_encoding_type_t type, chunk_t *encoding,
24 va_list args)
25 {
26 chunk_t asn1;
27 char *label;
28 u_char *pos;
29 size_t len, written, pem_chars, pem_lines;
30 chunk_t n, e, d, p, q, exp1, exp2, coeff, to_free = chunk_empty;
31
32 switch (type)
33 {
34 case KEY_PUB_PEM:
35 label ="PUBLIC KEY";
36 /* direct PKCS#1 PEM encoding */
37 if (key_encoding_args(args, KEY_PART_RSA_PUB_ASN1_DER,
38 &asn1, KEY_PART_END) ||
39 key_encoding_args(args, KEY_PART_ECDSA_PUB_ASN1_DER,
40 &asn1, KEY_PART_END))
41 {
42 break;
43 }
44 /* indirect PEM encoding from components */
45 if (key_encoding_args(args, KEY_PART_RSA_MODULUS, &n,
46 KEY_PART_RSA_PUB_EXP, &e, KEY_PART_END))
47 {
48 if (lib->encoding->encode(lib->encoding, KEY_PUB_SPKI_ASN1_DER,
49 NULL, &asn1, KEY_PART_RSA_MODULUS, n,
50 KEY_PART_RSA_PUB_EXP, e, KEY_PART_END))
51 {
52 to_free = asn1;
53 break;
54 }
55 }
56 return FALSE;
57 case KEY_PRIV_PEM:
58 label ="RSA PRIVATE KEY";
59 /* direct PKCS#1 PEM encoding */
60 if (key_encoding_args(args, KEY_PART_RSA_PRIV_ASN1_DER,
61 &asn1, KEY_PART_END))
62 {
63 break;
64 }
65 /* indirect PEM encoding from components */
66 if (key_encoding_args(args, KEY_PART_RSA_MODULUS, &n,
67 KEY_PART_RSA_PUB_EXP, &e, KEY_PART_RSA_PRIV_EXP, &d,
68 KEY_PART_RSA_PRIME1, &p, KEY_PART_RSA_PRIME2, &q,
69 KEY_PART_RSA_EXP1, &exp1, KEY_PART_RSA_EXP2, &exp2,
70 KEY_PART_RSA_COEFF, &coeff, KEY_PART_END))
71 {
72 if (lib->encoding->encode(lib->encoding, KEY_PRIV_ASN1_DER, NULL,
73 &asn1, KEY_PART_RSA_MODULUS, n,
74 KEY_PART_RSA_PUB_EXP, e, KEY_PART_RSA_PRIV_EXP, d,
75 KEY_PART_RSA_PRIME1, p, KEY_PART_RSA_PRIME2, q,
76 KEY_PART_RSA_EXP1, exp1, KEY_PART_RSA_EXP2, exp2,
77 KEY_PART_RSA_COEFF, coeff, KEY_PART_END))
78 {
79 to_free = asn1;
80 break;
81 }
82 }
83 if (key_encoding_args(args, KEY_PART_ECDSA_PRIV_ASN1_DER,
84 &asn1, KEY_PART_END))
85 {
86 label ="EC PRIVATE KEY";
87 break;
88 }
89 return FALSE;
90 default:
91 return FALSE;
92 }
93
94 /* compute and allocate maximum size of PEM object */
95 pem_chars = 4*(asn1.len + 2)/3;
96 pem_lines = (asn1.len + BYTES_PER_LINE - 1) / BYTES_PER_LINE;
97 *encoding = chunk_alloc(5 + 2*(6 + strlen(label) + 6) + 3 + pem_chars + pem_lines);
98 pos = encoding->ptr;
99 len = encoding->len;
100
101 /* write PEM header */
102 written = snprintf(pos, len, "-----BEGIN %s-----\n", label);
103 pos += written;
104 len -= written;
105
106 /* write PEM body */
107 while (pem_lines--)
108 {
109 chunk_t asn1_line, pem_line;
110
111 asn1_line = chunk_create(asn1.ptr, min(asn1.len, BYTES_PER_LINE));
112 asn1.ptr += asn1_line.len;
113 asn1.len -= asn1_line.len;
114 pem_line = chunk_to_base64(asn1_line, pos);
115 pos += pem_line.len;
116 len -= pem_line.len;
117 *pos = '\n';
118 pos++;
119 len--;
120 }
121
122 chunk_clear(&to_free);
123
124 /* write PEM trailer */
125 written = snprintf(pos, len, "-----END %s-----", label);
126 pos += written;
127 len -= written;
128
129 /* replace termination null character with newline */
130 *pos = '\n';
131 pos++;
132 len--;
133
134 /* compute effective length of PEM object */
135 encoding->len = pos - encoding->ptr;
136 return TRUE;
137 }
138