2 * Copyright (c) 2004 - 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
35 RCSID("$Id: ks_p12.c 21146 2007-06-18 21:37:25Z lha $");
42 typedef int (*collector_func)(hx509_context,
43 struct hx509_collector *,
45 const PKCS12_Attributes *);
48 const heim_oid * (*oid)(void);
53 parse_pkcs12_type(hx509_context, struct hx509_collector *, const heim_oid *,
54 const void *, size_t, const PKCS12_Attributes *);
57 static const PKCS12_Attribute *
58 find_attribute(const PKCS12_Attributes *attrs, const heim_oid *oid)
63 for (i = 0; i < attrs->len; i++)
64 if (der_heim_oid_cmp(oid, &attrs->val[i].attrId) == 0)
65 return &attrs->val[i];
70 keyBag_parser(hx509_context context,
71 struct hx509_collector *c,
72 const void *data, size_t length,
73 const PKCS12_Attributes *attrs)
75 const PKCS12_Attribute *attr;
76 PKCS8PrivateKeyInfo ki;
77 const heim_octet_string *os = NULL;
80 attr = find_attribute(attrs, oid_id_pkcs_9_at_localKeyId());
82 os = &attr->attrValues;
84 ret = decode_PKCS8PrivateKeyInfo(data, length, &ki, NULL);
88 _hx509_collector_private_key_add(context,
90 &ki.privateKeyAlgorithm,
94 free_PKCS8PrivateKeyInfo(&ki);
99 ShroudedKeyBag_parser(hx509_context context,
100 struct hx509_collector *c,
101 const void *data, size_t length,
102 const PKCS12_Attributes *attrs)
104 PKCS8EncryptedPrivateKeyInfo pk;
105 heim_octet_string content;
108 memset(&pk, 0, sizeof(pk));
110 ret = decode_PKCS8EncryptedPrivateKeyInfo(data, length, &pk, NULL);
114 ret = _hx509_pbe_decrypt(context,
115 _hx509_collector_get_lock(c),
116 &pk.encryptionAlgorithm,
119 free_PKCS8EncryptedPrivateKeyInfo(&pk);
123 ret = keyBag_parser(context, c, content.data, content.length, attrs);
124 der_free_octet_string(&content);
129 certBag_parser(hx509_context context,
130 struct hx509_collector *c,
131 const void *data, size_t length,
132 const PKCS12_Attributes *attrs)
134 heim_octet_string os;
139 ret = decode_PKCS12_CertBag(data, length, &cb, NULL);
143 if (der_heim_oid_cmp(oid_id_pkcs_9_at_certTypes_x509(), &cb.certType)) {
144 free_PKCS12_CertBag(&cb);
148 ret = decode_PKCS12_OctetString(cb.certValue.data,
152 free_PKCS12_CertBag(&cb);
156 ret = hx509_cert_init_data(context, os.data, os.length, &cert);
157 der_free_octet_string(&os);
161 ret = _hx509_collector_certs_add(context, c, cert);
163 hx509_cert_free(cert);
168 const PKCS12_Attribute *attr;
169 const heim_oid * (*oids[])(void) = {
170 oid_id_pkcs_9_at_localKeyId, oid_id_pkcs_9_at_friendlyName
174 for (i = 0; i < sizeof(oids)/sizeof(oids[0]); i++) {
175 const heim_oid *oid = (*(oids[i]))();
176 attr = find_attribute(attrs, oid);
178 _hx509_set_cert_attribute(context, cert, oid,
183 hx509_cert_free(cert);
189 parse_safe_content(hx509_context context,
190 struct hx509_collector *c,
191 const unsigned char *p, size_t len)
193 PKCS12_SafeContents sc;
196 memset(&sc, 0, sizeof(sc));
198 ret = decode_PKCS12_SafeContents(p, len, &sc, NULL);
202 for (i = 0; i < sc.len ; i++)
203 parse_pkcs12_type(context,
206 sc.val[i].bagValue.data,
207 sc.val[i].bagValue.length,
208 sc.val[i].bagAttributes);
210 free_PKCS12_SafeContents(&sc);
215 safeContent_parser(hx509_context context,
216 struct hx509_collector *c,
217 const void *data, size_t length,
218 const PKCS12_Attributes *attrs)
220 heim_octet_string os;
223 ret = decode_PKCS12_OctetString(data, length, &os, NULL);
226 ret = parse_safe_content(context, c, os.data, os.length);
227 der_free_octet_string(&os);
232 encryptedData_parser(hx509_context context,
233 struct hx509_collector *c,
234 const void *data, size_t length,
235 const PKCS12_Attributes *attrs)
237 heim_octet_string content;
238 heim_oid contentType;
241 memset(&contentType, 0, sizeof(contentType));
243 ret = hx509_cms_decrypt_encrypted(context,
244 _hx509_collector_get_lock(c),
251 if (der_heim_oid_cmp(&contentType, oid_id_pkcs7_data()) == 0)
252 ret = parse_safe_content(context, c, content.data, content.length);
254 der_free_octet_string(&content);
255 der_free_oid(&contentType);
260 envelopedData_parser(hx509_context context,
261 struct hx509_collector *c,
262 const void *data, size_t length,
263 const PKCS12_Attributes *attrs)
265 heim_octet_string content;
266 heim_oid contentType;
270 memset(&contentType, 0, sizeof(contentType));
272 lock = _hx509_collector_get_lock(c);
274 ret = hx509_cms_unenvelope(context,
275 _hx509_lock_unlock_certs(lock),
282 hx509_set_error_string(context, HX509_ERROR_APPEND, ret,
283 "PKCS12 failed to unenvelope");
287 if (der_heim_oid_cmp(&contentType, oid_id_pkcs7_data()) == 0)
288 ret = parse_safe_content(context, c, content.data, content.length);
290 der_free_octet_string(&content);
291 der_free_oid(&contentType);
297 struct type bagtypes[] = {
298 { oid_id_pkcs12_keyBag, keyBag_parser },
299 { oid_id_pkcs12_pkcs8ShroudedKeyBag, ShroudedKeyBag_parser },
300 { oid_id_pkcs12_certBag, certBag_parser },
301 { oid_id_pkcs7_data, safeContent_parser },
302 { oid_id_pkcs7_encryptedData, encryptedData_parser },
303 { oid_id_pkcs7_envelopedData, envelopedData_parser }
307 parse_pkcs12_type(hx509_context context,
308 struct hx509_collector *c,
310 const void *data, size_t length,
311 const PKCS12_Attributes *attrs)
315 for (i = 0; i < sizeof(bagtypes)/sizeof(bagtypes[0]); i++)
316 if (der_heim_oid_cmp((*bagtypes[i].oid)(), oid) == 0)
317 (*bagtypes[i].func)(context, c, data, length, attrs);
321 p12_init(hx509_context context,
322 hx509_certs certs, void **data, int flags,
323 const char *residue, hx509_lock lock)
325 struct ks_pkcs12 *p12;
329 PKCS12_AuthenticatedSafe as;
331 struct hx509_collector *c;
336 lock = _hx509_empty_lock;
338 ret = _hx509_collector_alloc(context, lock, &c);
342 p12 = calloc(1, sizeof(*p12));
345 hx509_set_error_string(context, 0, ret, "out of memory");
349 p12->fn = strdup(residue);
350 if (p12->fn == NULL) {
352 hx509_set_error_string(context, 0, ret, "out of memory");
356 if (flags & HX509_CERTS_CREATE) {
357 ret = hx509_certs_init(context, "MEMORY:ks-file-create",
358 0, lock, &p12->certs);
364 ret = _hx509_map_file(residue, &buf, &len, NULL);
366 hx509_clear_error_string(context);
370 ret = decode_PKCS12_PFX(buf, len, &pfx, NULL);
371 _hx509_unmap_file(buf, len);
373 hx509_set_error_string(context, 0, ret,
374 "Failed to decode the PFX in %s", residue);
378 if (der_heim_oid_cmp(&pfx.authSafe.contentType, oid_id_pkcs7_data()) != 0) {
379 free_PKCS12_PFX(&pfx);
381 hx509_set_error_string(context, 0, ret,
382 "PKCS PFX isn't a pkcs7-data container");
386 if (pfx.authSafe.content == NULL) {
387 free_PKCS12_PFX(&pfx);
389 hx509_set_error_string(context, 0, ret,
390 "PKCS PFX missing data");
395 heim_octet_string asdata;
397 ret = decode_PKCS12_OctetString(pfx.authSafe.content->data,
398 pfx.authSafe.content->length,
401 free_PKCS12_PFX(&pfx);
403 hx509_clear_error_string(context);
406 ret = decode_PKCS12_AuthenticatedSafe(asdata.data,
410 der_free_octet_string(&asdata);
412 hx509_clear_error_string(context);
417 for (i = 0; i < as.len; i++)
418 parse_pkcs12_type(context,
420 &as.val[i].contentType,
421 as.val[i].content->data,
422 as.val[i].content->length,
425 free_PKCS12_AuthenticatedSafe(&as);
427 ret = _hx509_collector_collect_certs(context, c, &p12->certs);
432 _hx509_collector_free(c);
438 hx509_certs_free(&p12->certs);
446 addBag(hx509_context context,
447 PKCS12_AuthenticatedSafe *as,
455 ptr = realloc(as->val, sizeof(as->val[0]) * (as->len + 1));
457 hx509_set_error_string(context, 0, ENOMEM, "out of memory");
462 ret = der_copy_oid(oid, &as->val[as->len].contentType);
464 hx509_set_error_string(context, 0, ret, "out of memory");
468 as->val[as->len].content = calloc(1, sizeof(*as->val[0].content));
469 if (as->val[as->len].content == NULL) {
470 der_free_oid(&as->val[as->len].contentType);
471 hx509_set_error_string(context, 0, ENOMEM, "malloc out of memory");
475 as->val[as->len].content->data = data;
476 as->val[as->len].content->length = length;
484 store_func(hx509_context context, void *ctx, hx509_cert c)
486 PKCS12_AuthenticatedSafe *as = ctx;
487 PKCS12_OctetString os;
492 memset(&os, 0, sizeof(os));
493 memset(&cb, 0, sizeof(cb));
498 ret = hx509_cert_binary(context, c, &os);
502 ASN1_MALLOC_ENCODE(PKCS12_OctetString,
503 cb.certValue.data,cb.certValue.length,
508 ret = der_copy_oid(oid_id_pkcs_9_at_certTypes_x509(), &cb.certType);
510 free_PKCS12_CertBag(&cb);
513 ASN1_MALLOC_ENCODE(PKCS12_CertBag, os.data, os.length,
515 free_PKCS12_CertBag(&cb);
519 ret = addBag(context, as, oid_id_pkcs12_certBag(), os.data, os.length);
521 if (_hx509_cert_private_key_exportable(c)) {
522 hx509_private_key key = _hx509_cert_private_key(c);
523 PKCS8PrivateKeyInfo pki;
525 memset(&pki, 0, sizeof(pki));
527 ret = der_parse_hex_heim_integer("00", &pki.version);
530 ret = _hx509_private_key_oid(context, key,
531 &pki.privateKeyAlgorithm.algorithm);
533 free_PKCS8PrivateKeyInfo(&pki);
536 ret = _hx509_private_key_export(context,
537 _hx509_cert_private_key(c),
540 free_PKCS8PrivateKeyInfo(&pki);
543 /* set attribute, oid_id_pkcs_9_at_localKeyId() */
545 ASN1_MALLOC_ENCODE(PKCS8PrivateKeyInfo, os.data, os.length,
547 free_PKCS8PrivateKeyInfo(&pki);
551 ret = addBag(context, as, oid_id_pkcs12_keyBag(), os.data, os.length);
561 p12_store(hx509_context context,
562 hx509_certs certs, void *data, int flags, hx509_lock lock)
564 struct ks_pkcs12 *p12 = data;
566 PKCS12_AuthenticatedSafe as;
567 PKCS12_OctetString asdata;
571 memset(&as, 0, sizeof(as));
572 memset(&pfx, 0, sizeof(pfx));
574 ret = hx509_certs_iter(context, p12->certs, store_func, &as);
578 ASN1_MALLOC_ENCODE(PKCS12_AuthenticatedSafe, asdata.data, asdata.length,
580 free_PKCS12_AuthenticatedSafe(&as);
584 ret = der_parse_hex_heim_integer("03", &pfx.version);
590 pfx.authSafe.content = calloc(1, sizeof(*pfx.authSafe.content));
592 ASN1_MALLOC_ENCODE(PKCS12_OctetString,
593 pfx.authSafe.content->data,
594 pfx.authSafe.content->length,
595 &asdata, &size, ret);
600 ret = der_copy_oid(oid_id_pkcs7_data(), &pfx.authSafe.contentType);
604 ASN1_MALLOC_ENCODE(PKCS12_PFX, asdata.data, asdata.length,
610 const struct _hx509_password *pw;
612 pw = _hx509_lock_get_passwords(lock);
614 pfx.macData = calloc(1, sizeof(*pfx.macData));
615 if (pfx.macData == NULL) {
617 hx509_set_error_string(context, 0, ret, "malloc out of memory");
620 if (pfx.macData == NULL) {
625 ret = calculate_hash(&aspath, pw, pfx.macData);
628 rk_dumpdata(p12->fn, asdata.data, asdata.length);
632 free_PKCS12_AuthenticatedSafe(&as);
633 free_PKCS12_PFX(&pfx);
640 p12_free(hx509_certs certs, void *data)
642 struct ks_pkcs12 *p12 = data;
643 hx509_certs_free(&p12->certs);
650 p12_add(hx509_context context, hx509_certs certs, void *data, hx509_cert c)
652 struct ks_pkcs12 *p12 = data;
653 return hx509_certs_add(context, p12->certs, c);
657 p12_iter_start(hx509_context context,
662 struct ks_pkcs12 *p12 = data;
663 return hx509_certs_start_seq(context, p12->certs, cursor);
667 p12_iter(hx509_context context,
673 struct ks_pkcs12 *p12 = data;
674 return hx509_certs_next_cert(context, p12->certs, cursor, cert);
678 p12_iter_end(hx509_context context,
683 struct ks_pkcs12 *p12 = data;
684 return hx509_certs_end_seq(context, p12->certs, cursor);
687 static struct hx509_keyset_ops keyset_pkcs12 = {
701 _hx509_ks_pkcs12_register(hx509_context context)
703 _hx509_ks_register(context, &keyset_pkcs12);