2 * Copyright (c) 2005 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 typedef enum { USE_PEM, USE_DER } outformat;
49 parse_certificate(hx509_context context, const char *fn,
50 struct hx509_collector *c,
51 const hx509_pem_header *headers,
52 const void *data, size_t len,
53 const AlgorithmIdentifier *ai)
58 ret = hx509_cert_init_data(context, data, len, &cert);
62 ret = _hx509_collector_certs_add(context, c, cert);
63 hx509_cert_free(cert);
68 try_decrypt(hx509_context context,
69 struct hx509_collector *collector,
70 const AlgorithmIdentifier *alg,
78 heim_octet_string clear;
83 keylen = EVP_CIPHER_key_length(c);
87 hx509_clear_error_string(context);
91 ret = EVP_BytesToKey(c, EVP_md5(), ivdata,
92 password, passwordlen,
95 hx509_set_error_string(context, 0, HX509_CRYPTO_INTERNAL_ERROR,
96 "Failed to do string2key for private key");
97 return HX509_CRYPTO_INTERNAL_ERROR;
100 clear.data = malloc(len);
101 if (clear.data == NULL) {
102 hx509_set_error_string(context, 0, ENOMEM,
103 "Out of memory to decrypt for private key");
112 ctx = EVP_CIPHER_CTX_new();
114 hx509_set_error_string(context, 0, ENOMEM,
115 "Out of memory to decrypt for private key");
119 EVP_CipherInit_ex(ctx, c, NULL, key, ivdata, 0);
120 EVP_Cipher(ctx, clear.data, cipher, len);
121 EVP_CIPHER_CTX_free(ctx);
124 ret = _hx509_collector_private_key_add(context,
131 memset(clear.data, 0, clear.length);
134 memset(key, 0, keylen);
140 parse_pkcs8_private_key(hx509_context context, const char *fn,
141 struct hx509_collector *c,
142 const hx509_pem_header *headers,
143 const void *data, size_t length,
144 const AlgorithmIdentifier *ai)
146 PKCS8PrivateKeyInfo ki;
147 heim_octet_string keydata;
151 ret = decode_PKCS8PrivateKeyInfo(data, length, &ki, NULL);
155 keydata.data = rk_UNCONST(data);
156 keydata.length = length;
158 ret = _hx509_collector_private_key_add(context,
160 &ki.privateKeyAlgorithm,
164 free_PKCS8PrivateKeyInfo(&ki);
169 parse_pem_private_key(hx509_context context, const char *fn,
170 struct hx509_collector *c,
171 const hx509_pem_header *headers,
172 const void *data, size_t len,
173 const AlgorithmIdentifier *ai)
178 enc = hx509_pem_find_header(headers, "Proc-Type");
184 const EVP_CIPHER *cipher;
185 const struct _hx509_password *pw;
190 lock = _hx509_collector_get_lock(c);
192 hx509_set_error_string(context, 0, HX509_ALG_NOT_SUPP,
193 "Failed to get password for "
194 "password protected file %s", fn);
195 return HX509_ALG_NOT_SUPP;
198 if (strcmp(enc, "4,ENCRYPTED") != 0) {
199 hx509_set_error_string(context, 0, HX509_PARSING_KEY_FAILED,
200 "Private key encrypted in unknown method %s "
203 hx509_clear_error_string(context);
204 return HX509_PARSING_KEY_FAILED;
207 dek = hx509_pem_find_header(headers, "DEK-Info");
209 hx509_set_error_string(context, 0, HX509_PARSING_KEY_FAILED,
210 "Encrypted private key missing DEK-Info");
211 return HX509_PARSING_KEY_FAILED;
216 hx509_clear_error_string(context);
220 iv = strchr(type, ',');
223 hx509_set_error_string(context, 0, HX509_PARSING_KEY_FAILED,
225 return HX509_PARSING_KEY_FAILED;
231 ivdata = malloc(size);
232 if (ivdata == NULL) {
233 hx509_clear_error_string(context);
238 cipher = EVP_get_cipherbyname(type);
239 if (cipher == NULL) {
241 hx509_set_error_string(context, 0, HX509_ALG_NOT_SUPP,
242 "Private key encrypted with "
243 "unsupported cipher: %s",
246 return HX509_ALG_NOT_SUPP;
249 #define PKCS5_SALT_LEN 8
251 ssize = hex_decode(iv, ivdata, size);
256 if (ssize < 0 || ssize < PKCS5_SALT_LEN || ssize < EVP_CIPHER_iv_length(cipher)) {
258 hx509_set_error_string(context, 0, HX509_PARSING_KEY_FAILED,
259 "Salt have wrong length in "
261 return HX509_PARSING_KEY_FAILED;
264 pw = _hx509_lock_get_passwords(lock);
266 const void *password;
269 for (i = 0; i < pw->len; i++) {
270 password = pw->val[i];
271 passwordlen = strlen(password);
273 ret = try_decrypt(context, c, ai, cipher, ivdata,
274 password, passwordlen, data, len);
285 memset(&prompt, 0, sizeof(prompt));
287 prompt.prompt = "Password for keyfile: ";
288 prompt.type = HX509_PROMPT_TYPE_PASSWORD;
289 prompt.reply.data = password;
290 prompt.reply.length = sizeof(password);
292 ret = hx509_lock_prompt(lock, &prompt);
294 ret = try_decrypt(context, c, ai, cipher, ivdata, password,
295 strlen(password), data, len);
296 /* XXX add password to lock password collection ? */
297 memset(password, 0, sizeof(password));
302 heim_octet_string keydata;
304 keydata.data = rk_UNCONST(data);
305 keydata.length = len;
307 ret = _hx509_collector_private_key_add(context, c, ai, NULL,
317 int (*func)(hx509_context, const char *, struct hx509_collector *,
318 const hx509_pem_header *, const void *, size_t,
319 const AlgorithmIdentifier *);
320 const AlgorithmIdentifier *(*ai)(void);
322 { "CERTIFICATE", parse_certificate, NULL },
323 { "PRIVATE KEY", parse_pkcs8_private_key, NULL },
324 { "RSA PRIVATE KEY", parse_pem_private_key, hx509_signature_rsa },
325 { "EC PRIVATE KEY", parse_pem_private_key, hx509_signature_ecPublicKey }
331 struct hx509_collector *c;
335 pem_func(hx509_context context, const char *type,
336 const hx509_pem_header *header,
337 const void *data, size_t len, void *ctx)
339 struct pem_ctx *pem_ctx = (struct pem_ctx*)ctx;
343 for (j = 0; j < sizeof(formats)/sizeof(formats[0]); j++) {
344 const char *q = formats[j].name;
345 if (strcasecmp(type, q) == 0) {
346 const AlgorithmIdentifier *ai = NULL;
347 if (formats[j].ai != NULL)
348 ai = (*formats[j].ai)();
350 ret = (*formats[j].func)(context, NULL, pem_ctx->c,
351 header, data, len, ai);
352 if (ret && (pem_ctx->flags & HX509_CERTS_UNPROTECT_ALL)) {
353 hx509_set_error_string(context, HX509_ERROR_APPEND, ret,
354 "Failed parseing PEM format %s", type);
360 if (j == sizeof(formats)/sizeof(formats[0])) {
361 ret = HX509_UNSUPPORTED_OPERATION;
362 hx509_set_error_string(context, 0, ret,
363 "Found no matching PEM format for %s", type);
374 file_init_common(hx509_context context,
375 hx509_certs certs, void **data, int flags,
376 const char *residue, hx509_lock lock, outformat format)
379 struct ks_file *ksf = NULL;
380 hx509_private_key *keys = NULL;
382 struct pem_ctx pem_ctx;
384 pem_ctx.flags = flags;
390 lock = _hx509_empty_lock;
392 ksf = calloc(1, sizeof(*ksf));
394 hx509_clear_error_string(context);
397 ksf->format = format;
399 ksf->fn = strdup(residue);
400 if (ksf->fn == NULL) {
401 hx509_clear_error_string(context);
407 * XXX this is broken, the function should parse the file before
411 if (flags & HX509_CERTS_CREATE) {
412 ret = hx509_certs_init(context, "MEMORY:ks-file-create",
413 0, lock, &ksf->certs);
420 ret = _hx509_collector_alloc(context, lock, &pem_ctx.c);
424 for (p = ksf->fn; p != NULL; p = pnext) {
427 pnext = strchr(p, ',');
432 if ((f = fopen(p, "r")) == NULL) {
434 hx509_set_error_string(context, 0, ret,
435 "Failed to open PEM file \"%s\": %s",
441 ret = hx509_pem_read(context, f, pem_func, &pem_ctx);
443 if (ret != 0 && ret != HX509_PARSING_KEY_FAILED)
445 else if (ret == HX509_PARSING_KEY_FAILED) {
450 ret = rk_undumpdata(p, &ptr, &length);
452 hx509_clear_error_string(context);
456 for (i = 0; i < sizeof(formats)/sizeof(formats[0]); i++) {
457 const AlgorithmIdentifier *ai = NULL;
458 if (formats[i].ai != NULL)
459 ai = (*formats[i].ai)();
461 ret = (*formats[i].func)(context, p, pem_ctx.c, NULL, ptr, length, ai);
467 hx509_clear_error_string(context);
473 ret = _hx509_collector_collect_certs(context, pem_ctx.c, &ksf->certs);
477 ret = _hx509_collector_collect_private_keys(context, pem_ctx.c, &keys);
481 for (i = 0; keys[i]; i++)
482 _hx509_certs_keys_add(context, ksf->certs, keys[i]);
483 _hx509_certs_keys_free(context, keys);
495 _hx509_collector_free(pem_ctx.c);
501 file_init_pem(hx509_context context,
502 hx509_certs certs, void **data, int flags,
503 const char *residue, hx509_lock lock)
505 return file_init_common(context, certs, data, flags, residue, lock, USE_PEM);
509 file_init_der(hx509_context context,
510 hx509_certs certs, void **data, int flags,
511 const char *residue, hx509_lock lock)
513 return file_init_common(context, certs, data, flags, residue, lock, USE_DER);
517 file_free(hx509_certs certs, void *data)
519 struct ks_file *ksf = data;
520 hx509_certs_free(&ksf->certs);
532 store_func(hx509_context context, void *ctx, hx509_cert c)
534 struct store_ctx *sc = ctx;
535 heim_octet_string data;
538 ret = hx509_cert_binary(context, c, &data);
542 switch (sc->format) {
544 fwrite(data.data, data.length, 1, sc->f);
548 hx509_pem_write(context, "CERTIFICATE", NULL, sc->f,
549 data.data, data.length);
551 if (_hx509_cert_private_key_exportable(c)) {
552 hx509_private_key key = _hx509_cert_private_key(c);
553 ret = _hx509_private_key_export(context, key,
554 HX509_KEY_FORMAT_DER, &data);
557 hx509_pem_write(context, _hx509_private_pem_name(key), NULL, sc->f,
558 data.data, data.length);
568 file_store(hx509_context context,
569 hx509_certs certs, void *data, int flags, hx509_lock lock)
571 struct ks_file *ksf = data;
575 sc.f = fopen(ksf->fn, "w");
577 hx509_set_error_string(context, 0, ENOENT,
578 "Failed to open file %s for writing");
581 rk_cloexec_file(sc.f);
582 sc.format = ksf->format;
584 ret = hx509_certs_iter_f(context, ksf->certs, store_func, &sc);
590 file_add(hx509_context context, hx509_certs certs, void *data, hx509_cert c)
592 struct ks_file *ksf = data;
593 return hx509_certs_add(context, ksf->certs, c);
597 file_iter_start(hx509_context context,
598 hx509_certs certs, void *data, void **cursor)
600 struct ks_file *ksf = data;
601 return hx509_certs_start_seq(context, ksf->certs, cursor);
605 file_iter(hx509_context context,
606 hx509_certs certs, void *data, void *iter, hx509_cert *cert)
608 struct ks_file *ksf = data;
609 return hx509_certs_next_cert(context, ksf->certs, iter, cert);
613 file_iter_end(hx509_context context,
618 struct ks_file *ksf = data;
619 return hx509_certs_end_seq(context, ksf->certs, cursor);
623 file_getkeys(hx509_context context,
626 hx509_private_key **keys)
628 struct ks_file *ksf = data;
629 return _hx509_certs_keys_get(context, ksf->certs, keys);
633 file_addkey(hx509_context context,
636 hx509_private_key key)
638 struct ks_file *ksf = data;
639 return _hx509_certs_keys_add(context, ksf->certs, key);
642 static struct hx509_keyset_ops keyset_file = {
658 static struct hx509_keyset_ops keyset_pemfile = {
674 static struct hx509_keyset_ops keyset_derfile = {
692 _hx509_ks_file_register(hx509_context context)
694 _hx509_ks_register(context, &keyset_file);
695 _hx509_ks_register(context, &keyset_pemfile);
696 _hx509_ks_register(context, &keyset_derfile);