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
41 typedef int (*collector_func
)(hx509_context
,
42 struct hx509_collector
*,
44 const PKCS12_Attributes
*);
52 parse_pkcs12_type(hx509_context
, struct hx509_collector
*, const heim_oid
*,
53 const void *, size_t, const PKCS12_Attributes
*);
56 static const PKCS12_Attribute
*
57 find_attribute(const PKCS12_Attributes
*attrs
, const heim_oid
*oid
)
62 for (i
= 0; i
< attrs
->len
; i
++)
63 if (der_heim_oid_cmp(oid
, &attrs
->val
[i
].attrId
) == 0)
64 return &attrs
->val
[i
];
69 keyBag_parser(hx509_context context
,
70 struct hx509_collector
*c
,
71 const void *data
, size_t length
,
72 const PKCS12_Attributes
*attrs
)
74 const PKCS12_Attribute
*attr
;
75 PKCS8PrivateKeyInfo ki
;
76 const heim_octet_string
*os
= NULL
;
79 attr
= find_attribute(attrs
, &asn1_oid_id_pkcs_9_at_localKeyId
);
81 os
= &attr
->attrValues
;
83 ret
= decode_PKCS8PrivateKeyInfo(data
, length
, &ki
, NULL
);
87 _hx509_collector_private_key_add(context
,
89 &ki
.privateKeyAlgorithm
,
93 free_PKCS8PrivateKeyInfo(&ki
);
98 ShroudedKeyBag_parser(hx509_context context
,
99 struct hx509_collector
*c
,
100 const void *data
, size_t length
,
101 const PKCS12_Attributes
*attrs
)
103 PKCS8EncryptedPrivateKeyInfo pk
;
104 heim_octet_string content
;
107 memset(&pk
, 0, sizeof(pk
));
109 ret
= decode_PKCS8EncryptedPrivateKeyInfo(data
, length
, &pk
, NULL
);
113 ret
= _hx509_pbe_decrypt(context
,
114 _hx509_collector_get_lock(c
),
115 &pk
.encryptionAlgorithm
,
118 free_PKCS8EncryptedPrivateKeyInfo(&pk
);
122 ret
= keyBag_parser(context
, c
, content
.data
, content
.length
, attrs
);
123 der_free_octet_string(&content
);
128 certBag_parser(hx509_context context
,
129 struct hx509_collector
*c
,
130 const void *data
, size_t length
,
131 const PKCS12_Attributes
*attrs
)
133 heim_octet_string os
;
138 ret
= decode_PKCS12_CertBag(data
, length
, &cb
, NULL
);
142 if (der_heim_oid_cmp(&asn1_oid_id_pkcs_9_at_certTypes_x509
, &cb
.certType
)) {
143 free_PKCS12_CertBag(&cb
);
147 ret
= decode_PKCS12_OctetString(cb
.certValue
.data
,
151 free_PKCS12_CertBag(&cb
);
155 ret
= hx509_cert_init_data(context
, os
.data
, os
.length
, &cert
);
156 der_free_octet_string(&os
);
160 ret
= _hx509_collector_certs_add(context
, c
, cert
);
162 hx509_cert_free(cert
);
167 const PKCS12_Attribute
*attr
;
168 const heim_oid
*oids
[] = {
169 &asn1_oid_id_pkcs_9_at_localKeyId
, &asn1_oid_id_pkcs_9_at_friendlyName
173 for (i
= 0; i
< sizeof(oids
)/sizeof(oids
[0]); i
++) {
174 const heim_oid
*oid
= oids
[i
];
175 attr
= find_attribute(attrs
, oid
);
177 _hx509_set_cert_attribute(context
, cert
, oid
,
182 hx509_cert_free(cert
);
188 parse_safe_content(hx509_context context
,
189 struct hx509_collector
*c
,
190 const unsigned char *p
, size_t len
)
192 PKCS12_SafeContents sc
;
195 memset(&sc
, 0, sizeof(sc
));
197 ret
= decode_PKCS12_SafeContents(p
, len
, &sc
, NULL
);
201 for (i
= 0; i
< sc
.len
; i
++)
202 parse_pkcs12_type(context
,
205 sc
.val
[i
].bagValue
.data
,
206 sc
.val
[i
].bagValue
.length
,
207 sc
.val
[i
].bagAttributes
);
209 free_PKCS12_SafeContents(&sc
);
214 safeContent_parser(hx509_context context
,
215 struct hx509_collector
*c
,
216 const void *data
, size_t length
,
217 const PKCS12_Attributes
*attrs
)
219 heim_octet_string os
;
222 ret
= decode_PKCS12_OctetString(data
, length
, &os
, NULL
);
225 ret
= parse_safe_content(context
, c
, os
.data
, os
.length
);
226 der_free_octet_string(&os
);
231 encryptedData_parser(hx509_context context
,
232 struct hx509_collector
*c
,
233 const void *data
, size_t length
,
234 const PKCS12_Attributes
*attrs
)
236 heim_octet_string content
;
237 heim_oid contentType
;
240 memset(&contentType
, 0, sizeof(contentType
));
242 ret
= hx509_cms_decrypt_encrypted(context
,
243 _hx509_collector_get_lock(c
),
250 if (der_heim_oid_cmp(&contentType
, &asn1_oid_id_pkcs7_data
) == 0)
251 ret
= parse_safe_content(context
, c
, content
.data
, content
.length
);
253 der_free_octet_string(&content
);
254 der_free_oid(&contentType
);
259 envelopedData_parser(hx509_context context
,
260 struct hx509_collector
*c
,
261 const void *data
, size_t length
,
262 const PKCS12_Attributes
*attrs
)
264 heim_octet_string content
;
265 heim_oid contentType
;
269 memset(&contentType
, 0, sizeof(contentType
));
271 lock
= _hx509_collector_get_lock(c
);
273 ret
= hx509_cms_unenvelope(context
,
274 _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
, &asn1_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 { &asn1_oid_id_pkcs12_keyBag
, keyBag_parser
},
299 { &asn1_oid_id_pkcs12_pkcs8ShroudedKeyBag
, ShroudedKeyBag_parser
},
300 { &asn1_oid_id_pkcs12_certBag
, certBag_parser
},
301 { &asn1_oid_id_pkcs7_data
, safeContent_parser
},
302 { &asn1_oid_id_pkcs7_encryptedData
, encryptedData_parser
},
303 { &asn1_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
= rk_undumpdata(residue
, &buf
, &len
);
366 hx509_clear_error_string(context
);
370 ret
= decode_PKCS12_PFX(buf
, len
, &pfx
, NULL
);
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
, &asn1_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(&asn1_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
, &asn1_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, asn1_oid_id_pkcs_9_at_localKeyId */
545 ASN1_MALLOC_ENCODE(PKCS8PrivateKeyInfo
, os
.data
, os
.length
,
547 free_PKCS8PrivateKeyInfo(&pki
);
551 ret
= addBag(context
, as
, &asn1_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_f(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(&asn1_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
);