summaryrefslogtreecommitdiff
path: root/trust/pem.c
blob: ce4f5547133c957843613b712a48f8ece32aec8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 * Copyright (C) 2012 Red Hat Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *     * Redistributions of source code must retain the above
 *       copyright notice, this list of conditions and the
 *       following disclaimer.
 *     * Redistributions in binary form must reproduce the
 *       above copyright notice, this list of conditions and
 *       the following disclaimer in the documentation and/or
 *       other materials provided with the distribution.
 *     * The names of contributors to this software may not be
 *       used to endorse or promote products derived from this
 *       software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * Author: Stef Walter <stefw@redhat.com>
 */

#include "config.h"

#include "compat.h"
#include "base64.h"
#include "buffer.h"
#include "debug.h"
#include "pem.h"

#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

#define ARMOR_SUFF          "-----"
#define ARMOR_SUFF_L        5
#define ARMOR_PREF_BEGIN    "-----BEGIN "
#define ARMOR_PREF_BEGIN_L  11
#define ARMOR_PREF_END      "-----END "
#define ARMOR_PREF_END_L    9

enum {
	NONE = 0,
	TRUSTED_CERTIFICATE,
	CERTIFICATE
};

static const char *
pem_find_begin (const char *data,
                size_t n_data,
                char **type)
{
	const char *pref, *suff;

	/* Look for a prefix */
	pref = strnstr ((char *)data, ARMOR_PREF_BEGIN, n_data);
	if (!pref)
		return NULL;

	n_data -= (pref - data) + ARMOR_PREF_BEGIN_L;
	data = pref + ARMOR_PREF_BEGIN_L;

	/* Look for the end of that begin */
	suff = strnstr ((char *)data, ARMOR_SUFF, n_data);
	if (!suff)
		return NULL;

	/* Make sure on the same line */
	if (memchr (pref, '\n', suff - pref))
		return NULL;

	if (type) {
		pref += ARMOR_PREF_BEGIN_L;
		assert (suff > pref);
		*type = strndup (pref, suff - pref);
		return_val_if_fail (*type != NULL, NULL);
	}

	/* The byte after this ---BEGIN--- */
	return suff + ARMOR_SUFF_L;
}

static const char *
pem_find_end (const char *data,
              size_t n_data,
              const char *type)
{
	const char *pref;
	size_t n_type;

	/* Look for a prefix */
	pref = strnstr (data, ARMOR_PREF_END, n_data);
	if (!pref)
		return NULL;

	n_data -= (pref - data) + ARMOR_PREF_END_L;
	data = pref + ARMOR_PREF_END_L;

	/* Next comes the type string */
	n_type = strlen (type);
	if (n_type > n_data || strncmp ((char *)data, type, n_type) != 0)
		return NULL;

	n_data -= n_type;
	data += n_type;

	/* Next comes the suffix */
	if (ARMOR_SUFF_L > n_data || strncmp ((char *)data, ARMOR_SUFF, ARMOR_SUFF_L) != 0)
		return NULL;

	/* The end of the data */
	return pref;
}

static unsigned char *
pem_parse_block (const char *data,
                 size_t n_data,
                 size_t *n_decoded)
{
	const char *x, *hbeg, *hend;
	const char *p, *end;
	unsigned char *decoded;
	size_t length;
	int ret;

	assert (data != NULL);
	assert (n_data != 0);
	assert (n_decoded != NULL);

	p = data;
	end = p + n_data;

	hbeg = hend = NULL;

	/* Try and find a pair of blank lines with only white space between */
	while (hend == NULL) {
		x = memchr (p, '\n', end - p);
		if (!x)
			break;
		++x;
		while (isspace (*x)) {
			/* Found a second line, with only spaces between */
			if (*x == '\n') {
				hbeg = data;
				hend = x;
				break;
			/* Found a space between two lines */
			} else {
				++x;
			}
		}

		/* Try next line */
		p = x;
	}

	/* Headers found? */
	if (hbeg && hend) {
		data = hend;
		n_data = end - data;
	}

	length = (n_data * 3) / 4 + 1;
	decoded = malloc (length);
	return_val_if_fail (decoded != NULL, 0);

	ret = p11_b64_pton (data, n_data, decoded, length);
	if (ret < 0) {
		free (decoded);
		return NULL;
	}

	/* No need to parse headers for our use cases */

	*n_decoded = ret;
	return decoded;
}

unsigned int
p11_pem_parse (const char *data,
               size_t n_data,
               p11_pem_sink sink,
               void *user_data)
{
	const char *beg, *end;
	unsigned int nfound = 0;
	unsigned char *decoded = NULL;
	size_t n_decoded = 0;
	char *type;

	assert (data != NULL);

	while (n_data > 0) {

		/* This returns the first character after the PEM BEGIN header */
		beg = pem_find_begin (data, n_data, &type);
		if (beg == NULL)
			break;

		assert (type != NULL);

		/* This returns the character position before the PEM END header */
		end = pem_find_end (beg, n_data - (beg - data), type);
		if (end == NULL) {
			free (type);
			break;
		}

		if (beg != end) {
			decoded = pem_parse_block (beg, end - beg, &n_decoded);
			if (decoded) {
				if (sink != NULL)
					(sink) (type, decoded, n_decoded, user_data);
				++nfound;
				free (decoded);
			}
		}

		free (type);

		/* Try for another block */
		end += ARMOR_SUFF_L;
		n_data -= (const char *)end - (const char *)data;
		data = end;
	}

	return nfound;
}

bool
p11_pem_write (const unsigned char *contents,
               size_t length,
               const char *type,
               p11_buffer *buf)
{
	size_t estimate;
	size_t prefix;
	char *target;
	int len;

	return_val_if_fail (contents || !length, false);
	return_val_if_fail (type, false);
	return_val_if_fail (buf, false);

	/* Estimate from base64 data. Algorithm from Glib reference */
	estimate = length * 4 / 3 + 7;
	estimate += estimate / 64 + 1;

	p11_buffer_add (buf, ARMOR_PREF_BEGIN, ARMOR_PREF_BEGIN_L);
	p11_buffer_add (buf, type, -1);
	p11_buffer_add (buf, ARMOR_SUFF, ARMOR_SUFF_L);

	prefix = buf->len;
	target = p11_buffer_append (buf, estimate);
	return_val_if_fail (target != NULL, NULL);

	/*
	 * OpenSSL is absolutely certain that it wants its PEM base64
	 * lines to be 64 characters in len.
	 */

	len = p11_b64_ntop (contents, length, target, estimate, 64);

	assert (len > 0);
	assert (len <= estimate);
	buf->len = prefix + len;

	p11_buffer_add (buf, "\n", 1);
	p11_buffer_add (buf, ARMOR_PREF_END, ARMOR_PREF_END_L);
	p11_buffer_add (buf, type, -1);
	p11_buffer_add (buf, ARMOR_SUFF, ARMOR_SUFF_L);
	p11_buffer_add (buf, "\n", 1);

	return p11_buffer_ok (buf);
}