/src/strongswan/src/libstrongswan/plugins/pem/pem_builder.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2013-2026 Tobias Brunner |
3 | | * Copyright (C) 2009 Martin Willi |
4 | | * Copyright (C) 2001-2008 Andreas Steffen |
5 | | * |
6 | | * Copyright (C) secunet Security Networks AG |
7 | | * |
8 | | * This program is free software; you can redistribute it and/or modify it |
9 | | * under the terms of the GNU General Public License as published by the |
10 | | * Free Software Foundation; either version 2 of the License, or (at your |
11 | | * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. |
12 | | * |
13 | | * This program is distributed in the hope that it will be useful, but |
14 | | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
15 | | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
16 | | * for more details. |
17 | | */ |
18 | | |
19 | | #include "pem_builder.h" |
20 | | #include "pem_bundle.h" |
21 | | |
22 | | #include <stdio.h> |
23 | | #include <stdlib.h> |
24 | | #include <unistd.h> |
25 | | #include <errno.h> |
26 | | #include <string.h> |
27 | | #include <stddef.h> |
28 | | #include <fcntl.h> |
29 | | #include <sys/types.h> |
30 | | #include <sys/stat.h> |
31 | | |
32 | | #include <utils/debug.h> |
33 | | #include <library.h> |
34 | | #include <utils/lexparser.h> |
35 | | #include <asn1/asn1.h> |
36 | | #include <crypto/hashers/hasher.h> |
37 | | #include <crypto/crypters/crypter.h> |
38 | | #include <credentials/certificates/x509.h> |
39 | | |
40 | 0 | #define PKCS5_SALT_LEN 8 /* bytes */ |
41 | | |
42 | | /** |
43 | | * Parser state |
44 | | */ |
45 | | typedef enum { |
46 | | PEM_PRE = 0, |
47 | | PEM_MSG = 1, |
48 | | PEM_HEADER = 2, |
49 | | PEM_BODY = 3, |
50 | | PEM_POST = 4, |
51 | | PEM_ABORT = 5 |
52 | | } state_t; |
53 | | |
54 | | /** |
55 | | * check the presence of a pattern in a character string, skip if found |
56 | | */ |
57 | | static bool present(char* pattern, chunk_t* ch) |
58 | 7.25M | { |
59 | 7.25M | u_int len = strlen(pattern); |
60 | | |
61 | 7.25M | if (ch->len >= len && strneq(ch->ptr, pattern, len)) |
62 | 125k | { |
63 | 125k | *ch = chunk_skip(*ch, len); |
64 | 125k | return TRUE; |
65 | 125k | } |
66 | 7.13M | return FALSE; |
67 | 7.25M | } |
68 | | |
69 | | /** |
70 | | * find a boundary of the form -----tag name----- |
71 | | */ |
72 | | static bool find_boundary(char* tag, chunk_t *line) |
73 | 1.42M | { |
74 | 1.42M | chunk_t name = chunk_empty; |
75 | | |
76 | 1.42M | if (!present("-----", line) || |
77 | 88.2k | !present(tag, line) || |
78 | 28.9k | !line->len || *line->ptr != ' ') |
79 | 1.40M | { |
80 | 1.40M | return FALSE; |
81 | 1.40M | } |
82 | 23.1k | *line = chunk_skip(*line, 1); |
83 | | |
84 | | /* extract name */ |
85 | 23.1k | name.ptr = line->ptr; |
86 | 5.75M | while (line->len > 0) |
87 | 5.74M | { |
88 | 5.74M | if (present("-----", line)) |
89 | 8.05k | { |
90 | 8.05k | DBG2(DBG_ASN, " -----%s %.*s-----", tag, (int)name.len, name.ptr); |
91 | 8.05k | return TRUE; |
92 | 8.05k | } |
93 | 5.73M | line->ptr++; line->len--; name.len++; |
94 | 5.73M | } |
95 | 15.0k | return FALSE; |
96 | 23.1k | } |
97 | | |
98 | | /* |
99 | | * decrypts a passphrase protected encrypted data block |
100 | | */ |
101 | | static status_t pem_decrypt(chunk_t *blob, encryption_algorithm_t alg, |
102 | | size_t key_size, chunk_t iv, chunk_t passphrase) |
103 | 0 | { |
104 | 0 | hasher_t *hasher; |
105 | 0 | crypter_t *crypter; |
106 | 0 | chunk_t salt = { iv.ptr, PKCS5_SALT_LEN }; |
107 | 0 | chunk_t hash; |
108 | 0 | chunk_t decrypted; |
109 | 0 | chunk_t key = {alloca(key_size), key_size}; |
110 | 0 | uint8_t padding, *last_padding_pos, *first_padding_pos; |
111 | 0 | size_t block_size; |
112 | |
|
113 | 0 | if (!key_size) |
114 | 0 | { |
115 | 0 | return FAILED; |
116 | 0 | } |
117 | | |
118 | | /* build key from passphrase and IV */ |
119 | 0 | hasher = lib->crypto->create_hasher(lib->crypto, HASH_MD5); |
120 | 0 | if (hasher == NULL) |
121 | 0 | { |
122 | 0 | DBG1(DBG_ASN, " MD5 hash algorithm not available"); |
123 | 0 | return NOT_SUPPORTED; |
124 | 0 | } |
125 | 0 | hash.len = hasher->get_hash_size(hasher); |
126 | 0 | hash.ptr = alloca(hash.len); |
127 | 0 | if (!hasher->get_hash(hasher, passphrase, NULL) || |
128 | 0 | !hasher->get_hash(hasher, salt, hash.ptr)) |
129 | 0 | { |
130 | 0 | hasher->destroy(hasher); |
131 | 0 | return FAILED; |
132 | 0 | } |
133 | 0 | memcpy(key.ptr, hash.ptr, hash.len); |
134 | |
|
135 | 0 | if (key.len > hash.len) |
136 | 0 | { |
137 | 0 | if (!hasher->get_hash(hasher, hash, NULL) || |
138 | 0 | !hasher->get_hash(hasher, passphrase, NULL) || |
139 | 0 | !hasher->get_hash(hasher, salt, hash.ptr)) |
140 | 0 | { |
141 | 0 | hasher->destroy(hasher); |
142 | 0 | return FAILED; |
143 | 0 | } |
144 | 0 | memcpy(key.ptr + hash.len, hash.ptr, key.len - hash.len); |
145 | 0 | } |
146 | 0 | hasher->destroy(hasher); |
147 | | |
148 | | /* decrypt blob */ |
149 | 0 | crypter = lib->crypto->create_crypter(lib->crypto, alg, key_size); |
150 | 0 | if (crypter == NULL) |
151 | 0 | { |
152 | 0 | DBG1(DBG_ASN, " %N encryption algorithm not available", |
153 | 0 | encryption_algorithm_names, alg); |
154 | 0 | memwipe(key.ptr, key.len); |
155 | 0 | return NOT_SUPPORTED; |
156 | 0 | } |
157 | | |
158 | 0 | block_size = crypter->get_block_size(crypter); |
159 | 0 | if (iv.len != crypter->get_iv_size(crypter) || blob->len % block_size) |
160 | 0 | { |
161 | 0 | DBG1(DBG_ASN, " data size is not multiple of block size"); |
162 | 0 | crypter->destroy(crypter); |
163 | 0 | memwipe(key.ptr, key.len); |
164 | 0 | return PARSE_ERROR; |
165 | 0 | } |
166 | 0 | if (!crypter->set_key(crypter, key) || |
167 | 0 | !crypter->decrypt(crypter, *blob, iv, &decrypted)) |
168 | 0 | { |
169 | 0 | crypter->destroy(crypter); |
170 | 0 | memwipe(key.ptr, key.len); |
171 | 0 | return FAILED; |
172 | 0 | } |
173 | 0 | crypter->destroy(crypter); |
174 | 0 | memcpy(blob->ptr, decrypted.ptr, blob->len); |
175 | 0 | chunk_clear(&decrypted); |
176 | 0 | memwipe(key.ptr, key.len); |
177 | | |
178 | | /* determine amount of padding */ |
179 | 0 | last_padding_pos = blob->ptr + blob->len - 1; |
180 | 0 | padding = *last_padding_pos; |
181 | 0 | if (!padding || padding > block_size) |
182 | 0 | { |
183 | 0 | DBG1(DBG_ASN, " invalid passphrase"); |
184 | 0 | return INVALID_ARG; |
185 | 0 | } |
186 | 0 | first_padding_pos = last_padding_pos - padding; |
187 | | /* check the padding pattern */ |
188 | 0 | while (--last_padding_pos > first_padding_pos) |
189 | 0 | { |
190 | 0 | if (*last_padding_pos != padding) |
191 | 0 | { |
192 | 0 | DBG1(DBG_ASN, " invalid passphrase"); |
193 | 0 | return INVALID_ARG; |
194 | 0 | } |
195 | 0 | } |
196 | | /* remove padding */ |
197 | 0 | blob->len -= padding; |
198 | 0 | return SUCCESS; |
199 | 0 | } |
200 | | |
201 | | /** |
202 | | * Converts a PEM encoded file into its binary form (RFC 1421, RFC 934) |
203 | | */ |
204 | | static status_t pem_to_bin(chunk_t *blob, bool *pgp) |
205 | 11.7k | { |
206 | 11.7k | encryption_algorithm_t alg = ENCR_UNDEFINED; |
207 | 11.7k | size_t key_size = 0; |
208 | 11.7k | bool encrypted = FALSE; |
209 | 11.7k | state_t state = PEM_PRE; |
210 | 11.7k | chunk_t src = *blob; |
211 | 11.7k | chunk_t dst = *blob; |
212 | 11.7k | chunk_t line = chunk_empty; |
213 | 11.7k | chunk_t iv = chunk_empty; |
214 | 11.7k | u_char iv_buf[HASH_SIZE_MD5]; |
215 | 11.7k | status_t status = NOT_FOUND; |
216 | 11.7k | enumerator_t *enumerator; |
217 | 11.7k | shared_key_t *shared; |
218 | | |
219 | 11.7k | dst.len = 0; |
220 | 11.7k | iv.ptr = iv_buf; |
221 | 11.7k | iv.len = 0; |
222 | | |
223 | 1.43M | while (fetchline(&src, &line)) |
224 | 1.42M | { |
225 | 1.42M | if (state == PEM_PRE) |
226 | 557k | { |
227 | 557k | if (find_boundary("BEGIN", &line)) |
228 | 7.40k | { |
229 | 7.40k | state = PEM_MSG; |
230 | 7.40k | } |
231 | 557k | continue; |
232 | 557k | } |
233 | 869k | else |
234 | 869k | { |
235 | 869k | if (find_boundary("END", &line)) |
236 | 658 | { |
237 | 658 | state = PEM_POST; |
238 | 658 | break; |
239 | 658 | } |
240 | 868k | if (state == PEM_MSG) |
241 | 6.78k | { |
242 | 6.78k | state = PEM_HEADER; |
243 | 6.78k | if (memchr(line.ptr, ':', line.len) == NULL) |
244 | 2.01k | { |
245 | 2.01k | state = PEM_BODY; |
246 | 2.01k | } |
247 | 6.78k | } |
248 | 868k | if (state == PEM_HEADER) |
249 | 183k | { |
250 | 183k | err_t ugh = NULL; |
251 | 183k | chunk_t name = chunk_empty; |
252 | 183k | chunk_t value = chunk_empty; |
253 | | |
254 | | /* an empty line separates HEADER and BODY */ |
255 | 183k | if (line.len == 0) |
256 | 279 | { |
257 | 279 | state = PEM_BODY; |
258 | 279 | continue; |
259 | 279 | } |
260 | | |
261 | | /* we are looking for a parameter: value pair */ |
262 | 183k | DBG2(DBG_ASN, " %.*s", (int)line.len, line.ptr); |
263 | 183k | ugh = extract_parameter_value(&name, &value, &line); |
264 | 183k | if (ugh != NULL) |
265 | 75.0k | { |
266 | 75.0k | continue; |
267 | 75.0k | } |
268 | 108k | if (match("Proc-Type", &name) && value.len && *value.ptr == '4') |
269 | 6.33k | { |
270 | 6.33k | encrypted = TRUE; |
271 | 6.33k | } |
272 | 101k | else if (match("DEK-Info", &name)) |
273 | 21.4k | { |
274 | 21.4k | chunk_t dek; |
275 | | |
276 | 21.4k | if (!extract_token(&dek, ',', &value)) |
277 | 7.14k | { |
278 | 7.14k | dek = value; |
279 | 7.14k | } |
280 | 21.4k | if (match("DES-EDE3-CBC", &dek)) |
281 | 4.60k | { |
282 | 4.60k | alg = ENCR_3DES; |
283 | 4.60k | key_size = 24; |
284 | 4.60k | } |
285 | 16.7k | else if (match("AES-128-CBC", &dek)) |
286 | 4.29k | { |
287 | 4.29k | alg = ENCR_AES_CBC; |
288 | 4.29k | key_size = 16; |
289 | 4.29k | } |
290 | 12.5k | else if (match("AES-192-CBC", &dek)) |
291 | 3.47k | { |
292 | 3.47k | alg = ENCR_AES_CBC; |
293 | 3.47k | key_size = 24; |
294 | 3.47k | } |
295 | 9.03k | else if (match("AES-256-CBC", &dek)) |
296 | 8.51k | { |
297 | 8.51k | alg = ENCR_AES_CBC; |
298 | 8.51k | key_size = 32; |
299 | 8.51k | } |
300 | 514 | else |
301 | 514 | { |
302 | 514 | DBG1(DBG_ASN, " encryption algorithm '%.*s'" |
303 | 514 | " not supported", (int)dek.len, dek.ptr); |
304 | 514 | return NOT_SUPPORTED; |
305 | 514 | } |
306 | 20.8k | if (!eat_whitespace(&value) || value.len > 2*sizeof(iv_buf)) |
307 | 387 | { |
308 | 387 | return PARSE_ERROR; |
309 | 387 | } |
310 | 20.5k | iv = chunk_from_hex(value, iv_buf); |
311 | 20.5k | } |
312 | 108k | } |
313 | 685k | else /* state is PEM_BODY */ |
314 | 685k | { |
315 | 685k | chunk_t data; |
316 | | |
317 | | /* remove any trailing whitespace */ |
318 | 685k | if (!extract_token(&data ,' ', &line)) |
319 | 543k | { |
320 | 543k | data = line; |
321 | 543k | } |
322 | | |
323 | | /* check for PGP armor checksum */ |
324 | 685k | if (data.len && *data.ptr == '=') |
325 | 5.55k | { |
326 | 5.55k | *pgp = TRUE; |
327 | 5.55k | data.ptr++; |
328 | 5.55k | data.len--; |
329 | 5.55k | DBG2(DBG_ASN, " armor checksum: %.*s", (int)data.len, |
330 | 5.55k | data.ptr); |
331 | 5.55k | continue; |
332 | 5.55k | } |
333 | | |
334 | 679k | if (blob->len - dst.len < data.len / 4 * 3) |
335 | 0 | { |
336 | 0 | state = PEM_ABORT; |
337 | 0 | } |
338 | 679k | data = chunk_from_base64(data, dst.ptr); |
339 | | |
340 | 679k | dst.ptr += data.len; |
341 | 679k | dst.len += data.len; |
342 | 679k | } |
343 | 868k | } |
344 | 1.42M | } |
345 | | /* set length to size of binary blob */ |
346 | 10.8k | blob->len = dst.len; |
347 | | |
348 | 10.8k | if (state != PEM_POST || !blob->len) |
349 | 10.2k | { |
350 | 10.2k | DBG1(DBG_LIB, " file coded in unknown format, discarded"); |
351 | 10.2k | return PARSE_ERROR; |
352 | 10.2k | } |
353 | 631 | if (!encrypted) |
354 | 589 | { |
355 | 589 | return SUCCESS; |
356 | 589 | } |
357 | 42 | if (alg == ENCR_UNDEFINED || !key_size) |
358 | 19 | { |
359 | 19 | DBG1(DBG_ASN, " encrypted PEM file has no valid DEK-Info"); |
360 | 19 | return PARSE_ERROR; |
361 | 19 | } |
362 | | |
363 | 23 | enumerator = lib->credmgr->create_shared_enumerator(lib->credmgr, |
364 | 23 | SHARED_PRIVATE_KEY_PASS, NULL, NULL); |
365 | 23 | while (enumerator->enumerate(enumerator, &shared, NULL, NULL)) |
366 | 0 | { |
367 | 0 | chunk_t passphrase, chunk; |
368 | |
|
369 | 0 | passphrase = shared->get_key(shared); |
370 | 0 | chunk = chunk_clone(*blob); |
371 | 0 | status = pem_decrypt(&chunk, alg, key_size, iv, passphrase); |
372 | 0 | if (status == SUCCESS) |
373 | 0 | { |
374 | 0 | memcpy(blob->ptr, chunk.ptr, chunk.len); |
375 | 0 | blob->len = chunk.len; |
376 | 0 | } |
377 | 0 | chunk_clear(&chunk); |
378 | 0 | if (status != INVALID_ARG) |
379 | 0 | { /* try again only if passphrase invalid */ |
380 | 0 | break; |
381 | 0 | } |
382 | 0 | } |
383 | 23 | enumerator->destroy(enumerator); |
384 | 23 | return status; |
385 | 42 | } |
386 | | |
387 | | /** |
388 | | * Check if a blob looks like an ASN1 SEQUENCE or SET with BER indefinite length |
389 | | */ |
390 | | static bool is_ber_indefinite_length(chunk_t blob) |
391 | 25.6k | { |
392 | 25.6k | if (blob.len >= 4) |
393 | 25.0k | { |
394 | 25.0k | switch (blob.ptr[0]) |
395 | 25.0k | { |
396 | 15.5k | case ASN1_SEQUENCE: |
397 | 15.8k | case ASN1_SET: |
398 | | /* BER indefinite length uses 0x80, and is terminated with |
399 | | * end-of-content using 0x00,0x00 */ |
400 | 15.8k | return blob.ptr[1] == 0x80 && |
401 | 777 | blob.ptr[blob.len - 2] == 0 && |
402 | 702 | blob.ptr[blob.len - 1] == 0; |
403 | 9.19k | default: |
404 | 9.19k | break; |
405 | 25.0k | } |
406 | 25.0k | } |
407 | 9.79k | return FALSE; |
408 | 25.6k | } |
409 | | |
410 | | /** |
411 | | * load the credential from a blob |
412 | | */ |
413 | | static void *load_from_blob(chunk_t blob, credential_type_t type, int subtype, |
414 | | identification_t *subject, x509_flag_t flags) |
415 | 25.6k | { |
416 | 25.6k | void *cred = NULL; |
417 | 25.6k | bool pgp = FALSE; |
418 | | |
419 | 25.6k | blob = chunk_clone(blob); |
420 | 25.6k | if (!is_ber_indefinite_length(blob) && !is_asn1(blob)) |
421 | 11.7k | { |
422 | 11.7k | if (pem_to_bin(&blob, &pgp) != SUCCESS) |
423 | 11.1k | { |
424 | 11.1k | chunk_clear(&blob); |
425 | 11.1k | return NULL; |
426 | 11.1k | } |
427 | 589 | if (pgp && type == CRED_PRIVATE_KEY) |
428 | 0 | { |
429 | | /* PGP encoded keys are parsed with a KEY_ANY key type, as it |
430 | | * can contain any type of key. However, ipsec.secrets uses |
431 | | * RSA for PGP keys, which is actually wrong. */ |
432 | 0 | subtype = KEY_ANY; |
433 | 0 | } |
434 | 589 | } |
435 | | /* if CERT_ANY is given, ASN1 encoded blob is handled as X509 */ |
436 | 14.5k | if (type == CRED_CERTIFICATE && subtype == CERT_ANY) |
437 | 0 | { |
438 | 0 | subtype = pgp ? CERT_GPG : CERT_X509; |
439 | 0 | } |
440 | 14.5k | if (type == CRED_CERTIFICATE && subtype == CERT_TRUSTED_PUBKEY && subject) |
441 | 0 | { |
442 | 0 | cred = lib->creds->create(lib->creds, type, subtype, |
443 | 0 | BUILD_BLOB_ASN1_DER, blob, BUILD_SUBJECT, subject, |
444 | 0 | BUILD_END); |
445 | 0 | } |
446 | 14.5k | else |
447 | 14.5k | { |
448 | 14.5k | cred = lib->creds->create(lib->creds, type, subtype, |
449 | 14.5k | pgp ? BUILD_BLOB_PGP : BUILD_BLOB_ASN1_DER, blob, |
450 | 14.5k | flags ? BUILD_X509_FLAG : BUILD_END, |
451 | 14.5k | flags, BUILD_END); |
452 | 14.5k | } |
453 | 14.5k | chunk_clear(&blob); |
454 | 14.5k | return cred; |
455 | 25.6k | } |
456 | | |
457 | | /** |
458 | | * load the credential from a file |
459 | | */ |
460 | | static void *load_from_file(char *file, credential_type_t type, int subtype, |
461 | | identification_t *subject, x509_flag_t flags) |
462 | 0 | { |
463 | 0 | void *cred; |
464 | 0 | chunk_t *chunk; |
465 | |
|
466 | 0 | chunk = chunk_map(file, FALSE); |
467 | 0 | if (!chunk) |
468 | 0 | { |
469 | 0 | DBG1(DBG_LIB, " opening '%s' failed: %s", file, strerror(errno)); |
470 | 0 | return NULL; |
471 | 0 | } |
472 | 0 | cred = load_from_blob(*chunk, type, subtype, subject, flags); |
473 | 0 | chunk_unmap_clear(chunk); |
474 | 0 | return cred; |
475 | 0 | } |
476 | | |
477 | | /** |
478 | | * Load all kind of PEM encoded credentials. |
479 | | */ |
480 | | static void *pem_load(credential_type_t type, int subtype, va_list args) |
481 | 93.4k | { |
482 | 93.4k | char *file = NULL; |
483 | 93.4k | chunk_t pem = chunk_empty; |
484 | 93.4k | identification_t *subject = NULL; |
485 | 93.4k | int flags = 0; |
486 | | |
487 | 93.4k | while (TRUE) |
488 | 119k | { |
489 | 119k | switch (va_arg(args, builder_part_t)) |
490 | 119k | { |
491 | 0 | case BUILD_FROM_FILE: |
492 | 0 | file = va_arg(args, char*); |
493 | 0 | continue; |
494 | 25.6k | case BUILD_BLOB: |
495 | 25.6k | case BUILD_BLOB_PEM: |
496 | 25.6k | pem = va_arg(args, chunk_t); |
497 | 25.6k | continue; |
498 | 0 | case BUILD_SUBJECT: |
499 | 0 | subject = va_arg(args, identification_t*); |
500 | 0 | continue; |
501 | 0 | case BUILD_X509_FLAG: |
502 | 0 | flags = va_arg(args, int); |
503 | 0 | continue; |
504 | 25.6k | case BUILD_END: |
505 | 25.6k | break; |
506 | 67.7k | default: |
507 | 67.7k | return NULL; |
508 | 119k | } |
509 | 25.6k | break; |
510 | 119k | } |
511 | | |
512 | 25.6k | if (pem.len) |
513 | 25.6k | { |
514 | 25.6k | return load_from_blob(pem, type, subtype, subject, flags); |
515 | 25.6k | } |
516 | 0 | if (file) |
517 | 0 | { |
518 | 0 | return load_from_file(file, type, subtype, subject, flags); |
519 | 0 | } |
520 | 0 | return NULL; |
521 | 0 | } |
522 | | |
523 | | /** |
524 | | * Private key PEM loader. |
525 | | */ |
526 | | private_key_t *pem_private_key_load(key_type_t type, va_list args) |
527 | 2 | { |
528 | 2 | return pem_load(CRED_PRIVATE_KEY, type, args); |
529 | 2 | } |
530 | | |
531 | | /** |
532 | | * Public key PEM loader. |
533 | | */ |
534 | | public_key_t *pem_public_key_load(key_type_t type, va_list args) |
535 | 1.13k | { |
536 | 1.13k | return pem_load(CRED_PUBLIC_KEY, type, args); |
537 | 1.13k | } |
538 | | |
539 | | /** |
540 | | * Certificate PEM loader. |
541 | | */ |
542 | | certificate_t *pem_certificate_load(certificate_type_t type, va_list args) |
543 | 92.2k | { |
544 | 92.2k | return pem_load(CRED_CERTIFICATE, type, args); |
545 | 92.2k | } |
546 | | |
547 | | /** |
548 | | * Container PEM loader. |
549 | | */ |
550 | | container_t *pem_container_load(container_type_t type, va_list args) |
551 | 0 | { |
552 | 0 | return pem_load(CRED_CONTAINER, type, args); |
553 | 0 | } |
554 | | |
555 | | /** |
556 | | * Load a PEM certificate bundle container. |
557 | | */ |
558 | | container_t *pem_container_load_bundle(container_type_t type, va_list args) |
559 | 0 | { |
560 | 0 | char *file = NULL; |
561 | 0 | chunk_t pem = chunk_empty, *mapped = NULL, src, dst, line, data, der; |
562 | 0 | pem_bundle_t *bundle; |
563 | 0 | certificate_t *cert; |
564 | 0 | state_t state = PEM_PRE; |
565 | 0 | bool found = FALSE; |
566 | |
|
567 | 0 | while (TRUE) |
568 | 0 | { |
569 | 0 | switch (va_arg(args, builder_part_t)) |
570 | 0 | { |
571 | 0 | case BUILD_FROM_FILE: |
572 | 0 | file = va_arg(args, char*); |
573 | 0 | continue; |
574 | 0 | case BUILD_BLOB: |
575 | 0 | case BUILD_BLOB_PEM: |
576 | 0 | pem = va_arg(args, chunk_t); |
577 | 0 | continue; |
578 | 0 | case BUILD_END: |
579 | 0 | break; |
580 | 0 | default: |
581 | 0 | return NULL; |
582 | 0 | } |
583 | 0 | break; |
584 | 0 | } |
585 | | |
586 | 0 | if (!pem.len && file) |
587 | 0 | { |
588 | 0 | mapped = chunk_map(file, FALSE); |
589 | 0 | if (!mapped) |
590 | 0 | { |
591 | 0 | DBG1(DBG_LIB, " opening '%s' failed: %s", file, strerror(errno)); |
592 | 0 | return NULL; |
593 | 0 | } |
594 | 0 | pem = *mapped; |
595 | 0 | } |
596 | | |
597 | 0 | if (is_ber_indefinite_length(pem) || is_asn1(pem)) |
598 | 0 | { |
599 | 0 | if (mapped) |
600 | 0 | { |
601 | 0 | chunk_unmap(mapped); |
602 | 0 | } |
603 | 0 | return NULL; |
604 | 0 | } |
605 | | |
606 | 0 | pem = chunk_clone(pem); |
607 | 0 | if (mapped) |
608 | 0 | { |
609 | 0 | chunk_unmap(mapped); |
610 | 0 | } |
611 | |
|
612 | 0 | bundle = pem_bundle_create(); |
613 | 0 | src = pem; |
614 | 0 | while (fetchline(&src, &line)) |
615 | 0 | { |
616 | 0 | if (state != PEM_BODY) |
617 | 0 | { |
618 | 0 | if (!present("-----BEGIN CERTIFICATE-----", &line)) |
619 | 0 | { |
620 | 0 | continue; |
621 | 0 | } |
622 | 0 | state = PEM_BODY; |
623 | | /* decode every certificate inline at the start of the buffer */ |
624 | 0 | der = dst = pem; |
625 | 0 | der.len = 0; |
626 | 0 | } |
627 | 0 | else |
628 | 0 | { |
629 | 0 | if (present("-----END CERTIFICATE-----", &line)) |
630 | 0 | { |
631 | 0 | cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, |
632 | 0 | CERT_X509, BUILD_BLOB_ASN1_DER, der, |
633 | 0 | BUILD_END); |
634 | 0 | if (cert) |
635 | 0 | { |
636 | 0 | bundle->add_cert(bundle, cert); |
637 | 0 | found = TRUE; |
638 | 0 | } |
639 | 0 | state = PEM_PRE; |
640 | 0 | continue; |
641 | 0 | } |
642 | | |
643 | 0 | if (!extract_token(&data, ' ', &line)) |
644 | 0 | { |
645 | 0 | data = line; |
646 | 0 | } |
647 | 0 | if (dst.len < data.len / 4 * 3) |
648 | 0 | { |
649 | 0 | state = PEM_PRE; |
650 | 0 | continue; |
651 | 0 | } |
652 | 0 | data = chunk_from_base64(data, dst.ptr); |
653 | 0 | dst.ptr += data.len; |
654 | 0 | dst.len -= data.len; |
655 | 0 | der.len += data.len; |
656 | 0 | } |
657 | 0 | } |
658 | |
|
659 | 0 | chunk_free(&pem); |
660 | |
|
661 | 0 | if (!found) |
662 | 0 | { |
663 | 0 | bundle->pem.container.destroy(&bundle->pem.container); |
664 | 0 | return NULL; |
665 | 0 | } |
666 | 0 | return &bundle->pem.container; |
667 | 0 | } |