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("$Heimdal: ks_p12.c 21146 2007-06-18 21:37:25Z lha $"
43 typedef int (*collector_func
)(hx509_context
,
44 struct hx509_collector
*,
46 const PKCS12_Attributes
*);
49 const heim_oid
* (*oid
)(void);
54 parse_pkcs12_type(hx509_context
, struct hx509_collector
*, const heim_oid
*,
55 const void *, size_t, const PKCS12_Attributes
*);
58 static const PKCS12_Attribute
*
59 find_attribute(const PKCS12_Attributes
*attrs
, const heim_oid
*oid
)
64 for (i
= 0; i
< attrs
->len
; i
++)
65 if (der_heim_oid_cmp(oid
, &attrs
->val
[i
].attrId
) == 0)
66 return &attrs
->val
[i
];
71 keyBag_parser(hx509_context context
,
72 struct hx509_collector
*c
,
73 const void *data
, size_t length
,
74 const PKCS12_Attributes
*attrs
)
76 const PKCS12_Attribute
*attr
;
77 PKCS8PrivateKeyInfo ki
;
78 const heim_octet_string
*os
= NULL
;
81 attr
= find_attribute(attrs
, oid_id_pkcs_9_at_localKeyId());
83 os
= &attr
->attrValues
;
85 ret
= decode_PKCS8PrivateKeyInfo(data
, length
, &ki
, NULL
);
89 _hx509_collector_private_key_add(context
,
91 &ki
.privateKeyAlgorithm
,
95 free_PKCS8PrivateKeyInfo(&ki
);
100 ShroudedKeyBag_parser(hx509_context context
,
101 struct hx509_collector
*c
,
102 const void *data
, size_t length
,
103 const PKCS12_Attributes
*attrs
)
105 PKCS8EncryptedPrivateKeyInfo pk
;
106 heim_octet_string content
;
109 memset(&pk
, 0, sizeof(pk
));
111 ret
= decode_PKCS8EncryptedPrivateKeyInfo(data
, length
, &pk
, NULL
);
115 ret
= _hx509_pbe_decrypt(context
,
116 _hx509_collector_get_lock(c
),
117 &pk
.encryptionAlgorithm
,
120 free_PKCS8EncryptedPrivateKeyInfo(&pk
);
124 ret
= keyBag_parser(context
, c
, content
.data
, content
.length
, attrs
);
125 der_free_octet_string(&content
);
130 certBag_parser(hx509_context context
,
131 struct hx509_collector
*c
,
132 const void *data
, size_t length
,
133 const PKCS12_Attributes
*attrs
)
135 heim_octet_string os
;
140 ret
= decode_PKCS12_CertBag(data
, length
, &cb
, NULL
);
144 if (der_heim_oid_cmp(oid_id_pkcs_9_at_certTypes_x509(), &cb
.certType
)) {
145 free_PKCS12_CertBag(&cb
);
149 ret
= decode_PKCS12_OctetString(cb
.certValue
.data
,
153 free_PKCS12_CertBag(&cb
);
157 ret
= hx509_cert_init_data(context
, os
.data
, os
.length
, &cert
);
158 der_free_octet_string(&os
);
162 ret
= _hx509_collector_certs_add(context
, c
, cert
);
164 hx509_cert_free(cert
);
169 const PKCS12_Attribute
*attr
;
170 const heim_oid
* (*oids
[])(void) = {
171 oid_id_pkcs_9_at_localKeyId
, oid_id_pkcs_9_at_friendlyName
175 for (i
= 0; i
< sizeof(oids
)/sizeof(oids
[0]); i
++) {
176 const heim_oid
*oid
= (*(oids
[i
]))();
177 attr
= find_attribute(attrs
, oid
);
179 _hx509_set_cert_attribute(context
, cert
, oid
,
184 hx509_cert_free(cert
);
190 parse_safe_content(hx509_context context
,
191 struct hx509_collector
*c
,
192 const unsigned char *p
, size_t len
)
194 PKCS12_SafeContents sc
;
197 memset(&sc
, 0, sizeof(sc
));
199 ret
= decode_PKCS12_SafeContents(p
, len
, &sc
, NULL
);
203 for (i
= 0; i
< sc
.len
; i
++)
204 parse_pkcs12_type(context
,
207 sc
.val
[i
].bagValue
.data
,
208 sc
.val
[i
].bagValue
.length
,
209 sc
.val
[i
].bagAttributes
);
211 free_PKCS12_SafeContents(&sc
);
216 safeContent_parser(hx509_context context
,
217 struct hx509_collector
*c
,
218 const void *data
, size_t length
,
219 const PKCS12_Attributes
*attrs
)
221 heim_octet_string os
;
224 ret
= decode_PKCS12_OctetString(data
, length
, &os
, NULL
);
227 ret
= parse_safe_content(context
, c
, os
.data
, os
.length
);
228 der_free_octet_string(&os
);
233 encryptedData_parser(hx509_context context
,
234 struct hx509_collector
*c
,
235 const void *data
, size_t length
,
236 const PKCS12_Attributes
*attrs
)
238 heim_octet_string content
;
239 heim_oid contentType
;
242 memset(&contentType
, 0, sizeof(contentType
));
244 ret
= hx509_cms_decrypt_encrypted(context
,
245 _hx509_collector_get_lock(c
),
252 if (der_heim_oid_cmp(&contentType
, oid_id_pkcs7_data()) == 0)
253 ret
= parse_safe_content(context
, c
, content
.data
, content
.length
);
255 der_free_octet_string(&content
);
256 der_free_oid(&contentType
);
261 envelopedData_parser(hx509_context context
,
262 struct hx509_collector
*c
,
263 const void *data
, size_t length
,
264 const PKCS12_Attributes
*attrs
)
266 heim_octet_string content
;
267 heim_oid contentType
;
271 memset(&contentType
, 0, sizeof(contentType
));
273 lock
= _hx509_collector_get_lock(c
);
275 ret
= hx509_cms_unenvelope(context
,
276 _hx509_lock_unlock_certs(lock
),
283 hx509_set_error_string(context
, HX509_ERROR_APPEND
, ret
,
284 "PKCS12 failed to unenvelope");
288 if (der_heim_oid_cmp(&contentType
, oid_id_pkcs7_data()) == 0)
289 ret
= parse_safe_content(context
, c
, content
.data
, content
.length
);
291 der_free_octet_string(&content
);
292 der_free_oid(&contentType
);
298 struct type bagtypes
[] = {
299 { oid_id_pkcs12_keyBag
, keyBag_parser
},
300 { oid_id_pkcs12_pkcs8ShroudedKeyBag
, ShroudedKeyBag_parser
},
301 { oid_id_pkcs12_certBag
, certBag_parser
},
302 { oid_id_pkcs7_data
, safeContent_parser
},
303 { oid_id_pkcs7_encryptedData
, encryptedData_parser
},
304 { oid_id_pkcs7_envelopedData
, envelopedData_parser
}
308 parse_pkcs12_type(hx509_context context
,
309 struct hx509_collector
*c
,
311 const void *data
, size_t length
,
312 const PKCS12_Attributes
*attrs
)
316 for (i
= 0; i
< sizeof(bagtypes
)/sizeof(bagtypes
[0]); i
++)
317 if (der_heim_oid_cmp((*bagtypes
[i
].oid
)(), oid
) == 0)
318 (*bagtypes
[i
].func
)(context
, c
, data
, length
, attrs
);
322 p12_init(hx509_context context
,
323 hx509_certs certs
, void **data
, int flags
,
324 const char *residue
, hx509_lock lock
)
326 struct ks_pkcs12
*p12
;
330 PKCS12_AuthenticatedSafe as
;
332 struct hx509_collector
*c
;
337 lock
= _hx509_empty_lock
;
339 ret
= _hx509_collector_alloc(context
, lock
, &c
);
343 p12
= calloc(1, sizeof(*p12
));
346 hx509_set_error_string(context
, 0, ret
, "out of memory");
350 p12
->fn
= strdup(residue
);
351 if (p12
->fn
== NULL
) {
353 hx509_set_error_string(context
, 0, ret
, "out of memory");
357 if (flags
& HX509_CERTS_CREATE
) {
358 ret
= hx509_certs_init(context
, "MEMORY:ks-file-create",
359 0, lock
, &p12
->certs
);
365 ret
= _hx509_map_file(residue
, &buf
, &len
, NULL
);
367 hx509_clear_error_string(context
);
371 ret
= decode_PKCS12_PFX(buf
, len
, &pfx
, NULL
);
372 _hx509_unmap_file(buf
, len
);
374 hx509_set_error_string(context
, 0, ret
,
375 "Failed to decode the PFX in %s", residue
);
379 if (der_heim_oid_cmp(&pfx
.authSafe
.contentType
, oid_id_pkcs7_data()) != 0) {
380 free_PKCS12_PFX(&pfx
);
382 hx509_set_error_string(context
, 0, ret
,
383 "PKCS PFX isn't a pkcs7-data container");
387 if (pfx
.authSafe
.content
== NULL
) {
388 free_PKCS12_PFX(&pfx
);
390 hx509_set_error_string(context
, 0, ret
,
391 "PKCS PFX missing data");
396 heim_octet_string asdata
;
398 ret
= decode_PKCS12_OctetString(pfx
.authSafe
.content
->data
,
399 pfx
.authSafe
.content
->length
,
402 free_PKCS12_PFX(&pfx
);
404 hx509_clear_error_string(context
);
407 ret
= decode_PKCS12_AuthenticatedSafe(asdata
.data
,
411 der_free_octet_string(&asdata
);
413 hx509_clear_error_string(context
);
418 for (i
= 0; i
< as
.len
; i
++)
419 parse_pkcs12_type(context
,
421 &as
.val
[i
].contentType
,
422 as
.val
[i
].content
->data
,
423 as
.val
[i
].content
->length
,
426 free_PKCS12_AuthenticatedSafe(&as
);
428 ret
= _hx509_collector_collect_certs(context
, c
, &p12
->certs
);
433 _hx509_collector_free(c
);
439 hx509_certs_free(&p12
->certs
);
447 addBag(hx509_context context
,
448 PKCS12_AuthenticatedSafe
*as
,
456 ptr
= realloc(as
->val
, sizeof(as
->val
[0]) * (as
->len
+ 1));
458 hx509_set_error_string(context
, 0, ENOMEM
, "out of memory");
463 ret
= der_copy_oid(oid
, &as
->val
[as
->len
].contentType
);
465 hx509_set_error_string(context
, 0, ret
, "out of memory");
469 as
->val
[as
->len
].content
= calloc(1, sizeof(*as
->val
[0].content
));
470 if (as
->val
[as
->len
].content
== NULL
) {
471 der_free_oid(&as
->val
[as
->len
].contentType
);
472 hx509_set_error_string(context
, 0, ENOMEM
, "malloc out of memory");
476 as
->val
[as
->len
].content
->data
= data
;
477 as
->val
[as
->len
].content
->length
= length
;
485 store_func(hx509_context context
, void *ctx
, hx509_cert c
)
487 PKCS12_AuthenticatedSafe
*as
= ctx
;
488 PKCS12_OctetString os
;
493 memset(&os
, 0, sizeof(os
));
494 memset(&cb
, 0, sizeof(cb
));
499 ret
= hx509_cert_binary(context
, c
, &os
);
503 ASN1_MALLOC_ENCODE(PKCS12_OctetString
,
504 cb
.certValue
.data
,cb
.certValue
.length
,
509 ret
= der_copy_oid(oid_id_pkcs_9_at_certTypes_x509(), &cb
.certType
);
511 free_PKCS12_CertBag(&cb
);
514 ASN1_MALLOC_ENCODE(PKCS12_CertBag
, os
.data
, os
.length
,
516 free_PKCS12_CertBag(&cb
);
520 ret
= addBag(context
, as
, oid_id_pkcs12_certBag(), os
.data
, os
.length
);
522 if (_hx509_cert_private_key_exportable(c
)) {
523 hx509_private_key key
= _hx509_cert_private_key(c
);
524 PKCS8PrivateKeyInfo pki
;
526 memset(&pki
, 0, sizeof(pki
));
528 ret
= der_parse_hex_heim_integer("00", &pki
.version
);
531 ret
= _hx509_private_key_oid(context
, key
,
532 &pki
.privateKeyAlgorithm
.algorithm
);
534 free_PKCS8PrivateKeyInfo(&pki
);
537 ret
= _hx509_private_key_export(context
,
538 _hx509_cert_private_key(c
),
541 free_PKCS8PrivateKeyInfo(&pki
);
544 /* set attribute, oid_id_pkcs_9_at_localKeyId() */
546 ASN1_MALLOC_ENCODE(PKCS8PrivateKeyInfo
, os
.data
, os
.length
,
548 free_PKCS8PrivateKeyInfo(&pki
);
552 ret
= addBag(context
, as
, oid_id_pkcs12_keyBag(), os
.data
, os
.length
);
562 p12_store(hx509_context context
,
563 hx509_certs certs
, void *data
, int flags
, hx509_lock lock
)
565 struct ks_pkcs12
*p12
= data
;
567 PKCS12_AuthenticatedSafe as
;
568 PKCS12_OctetString asdata
;
572 memset(&as
, 0, sizeof(as
));
573 memset(&pfx
, 0, sizeof(pfx
));
575 ret
= hx509_certs_iter(context
, p12
->certs
, store_func
, &as
);
579 ASN1_MALLOC_ENCODE(PKCS12_AuthenticatedSafe
, asdata
.data
, asdata
.length
,
581 free_PKCS12_AuthenticatedSafe(&as
);
585 ret
= der_parse_hex_heim_integer("03", &pfx
.version
);
591 pfx
.authSafe
.content
= calloc(1, sizeof(*pfx
.authSafe
.content
));
593 ASN1_MALLOC_ENCODE(PKCS12_OctetString
,
594 pfx
.authSafe
.content
->data
,
595 pfx
.authSafe
.content
->length
,
596 &asdata
, &size
, ret
);
601 ret
= der_copy_oid(oid_id_pkcs7_data(), &pfx
.authSafe
.contentType
);
605 ASN1_MALLOC_ENCODE(PKCS12_PFX
, asdata
.data
, asdata
.length
,
611 const struct _hx509_password
*pw
;
613 pw
= _hx509_lock_get_passwords(lock
);
615 pfx
.macData
= calloc(1, sizeof(*pfx
.macData
));
616 if (pfx
.macData
== NULL
) {
618 hx509_set_error_string(context
, 0, ret
, "malloc out of memory");
621 if (pfx
.macData
== NULL
) {
626 ret
= calculate_hash(&aspath
, pw
, pfx
.macData
);
629 rk_dumpdata(p12
->fn
, asdata
.data
, asdata
.length
);
633 free_PKCS12_AuthenticatedSafe(&as
);
634 free_PKCS12_PFX(&pfx
);
641 p12_free(hx509_certs certs
, void *data
)
643 struct ks_pkcs12
*p12
= data
;
644 hx509_certs_free(&p12
->certs
);
651 p12_add(hx509_context context
, hx509_certs certs
, void *data
, hx509_cert c
)
653 struct ks_pkcs12
*p12
= data
;
654 return hx509_certs_add(context
, p12
->certs
, c
);
658 p12_iter_start(hx509_context context
,
663 struct ks_pkcs12
*p12
= data
;
664 return hx509_certs_start_seq(context
, p12
->certs
, cursor
);
668 p12_iter(hx509_context context
,
674 struct ks_pkcs12
*p12
= data
;
675 return hx509_certs_next_cert(context
, p12
->certs
, cursor
, cert
);
679 p12_iter_end(hx509_context context
,
684 struct ks_pkcs12
*p12
= data
;
685 return hx509_certs_end_seq(context
, p12
->certs
, cursor
);
688 static struct hx509_keyset_ops keyset_pkcs12
= {
702 _hx509_ks_pkcs12_register(hx509_context context
)
704 _hx509_ks_register(context
, &keyset_pkcs12
);