2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
5 /* ====================================================================
6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
60 #include "internal/cryptlib.h"
61 #include <openssl/pkcs12.h>
63 /* Simplified PKCS#12 routines */
65 static int parse_pk12(PKCS12
*p12
, const char *pass
, int passlen
,
66 EVP_PKEY
**pkey
, STACK_OF(X509
) *ocerts
);
68 static int parse_bags(STACK_OF(PKCS12_SAFEBAG
) *bags
, const char *pass
,
69 int passlen
, EVP_PKEY
**pkey
, STACK_OF(X509
) *ocerts
);
71 static int parse_bag(PKCS12_SAFEBAG
*bag
, const char *pass
, int passlen
,
72 EVP_PKEY
**pkey
, STACK_OF(X509
) *ocerts
);
75 * Parse and decrypt a PKCS#12 structure returning user key, user cert and
76 * other (CA) certs. Note either ca should be NULL, *ca should be NULL, or it
77 * should point to a valid STACK structure. pkey and cert can be passed
81 int PKCS12_parse(PKCS12
*p12
, const char *pass
, EVP_PKEY
**pkey
, X509
**cert
,
84 STACK_OF(X509
) *ocerts
= NULL
;
86 /* Check for NULL PKCS12 structure */
89 PKCS12err(PKCS12_F_PKCS12_PARSE
,
90 PKCS12_R_INVALID_NULL_PKCS12_POINTER
);
102 * If password is zero length or NULL then try verifying both cases to
103 * determine which password is correct. The reason for this is that under
104 * PKCS#12 password based encryption no password and a zero length
105 * password are two different things...
108 if (!pass
|| !*pass
) {
109 if (PKCS12_verify_mac(p12
, NULL
, 0))
111 else if (PKCS12_verify_mac(p12
, "", 0))
114 PKCS12err(PKCS12_F_PKCS12_PARSE
, PKCS12_R_MAC_VERIFY_FAILURE
);
117 } else if (!PKCS12_verify_mac(p12
, pass
, -1)) {
118 PKCS12err(PKCS12_F_PKCS12_PARSE
, PKCS12_R_MAC_VERIFY_FAILURE
);
122 /* Allocate stack for other certificates */
123 ocerts
= sk_X509_new_null();
126 PKCS12err(PKCS12_F_PKCS12_PARSE
, ERR_R_MALLOC_FAILURE
);
130 if (!parse_pk12(p12
, pass
, -1, pkey
, ocerts
)) {
131 PKCS12err(PKCS12_F_PKCS12_PARSE
, PKCS12_R_PARSE_ERROR
);
135 while ((x
= sk_X509_pop(ocerts
))) {
136 if (pkey
&& *pkey
&& cert
&& !*cert
) {
138 if (X509_check_private_key(x
, *pkey
)) {
147 *ca
= sk_X509_new_null();
150 if (!sk_X509_push(*ca
, x
))
157 sk_X509_pop_free(ocerts
, X509_free
);
164 EVP_PKEY_free(*pkey
);
168 sk_X509_pop_free(ocerts
, X509_free
);
173 /* Parse the outer PKCS#12 structure */
175 static int parse_pk12(PKCS12
*p12
, const char *pass
, int passlen
,
176 EVP_PKEY
**pkey
, STACK_OF(X509
) *ocerts
)
178 STACK_OF(PKCS7
) *asafes
;
179 STACK_OF(PKCS12_SAFEBAG
) *bags
;
183 if ((asafes
= PKCS12_unpack_authsafes(p12
)) == NULL
)
185 for (i
= 0; i
< sk_PKCS7_num(asafes
); i
++) {
186 p7
= sk_PKCS7_value(asafes
, i
);
187 bagnid
= OBJ_obj2nid(p7
->type
);
188 if (bagnid
== NID_pkcs7_data
) {
189 bags
= PKCS12_unpack_p7data(p7
);
190 } else if (bagnid
== NID_pkcs7_encrypted
) {
191 bags
= PKCS12_unpack_p7encdata(p7
, pass
, passlen
);
195 sk_PKCS7_pop_free(asafes
, PKCS7_free
);
198 if (!parse_bags(bags
, pass
, passlen
, pkey
, ocerts
)) {
199 sk_PKCS12_SAFEBAG_pop_free(bags
, PKCS12_SAFEBAG_free
);
200 sk_PKCS7_pop_free(asafes
, PKCS7_free
);
203 sk_PKCS12_SAFEBAG_pop_free(bags
, PKCS12_SAFEBAG_free
);
205 sk_PKCS7_pop_free(asafes
, PKCS7_free
);
209 static int parse_bags(STACK_OF(PKCS12_SAFEBAG
) *bags
, const char *pass
,
210 int passlen
, EVP_PKEY
**pkey
, STACK_OF(X509
) *ocerts
)
213 for (i
= 0; i
< sk_PKCS12_SAFEBAG_num(bags
); i
++) {
214 if (!parse_bag(sk_PKCS12_SAFEBAG_value(bags
, i
),
215 pass
, passlen
, pkey
, ocerts
))
221 static int parse_bag(PKCS12_SAFEBAG
*bag
, const char *pass
, int passlen
,
222 EVP_PKEY
**pkey
, STACK_OF(X509
) *ocerts
)
224 PKCS8_PRIV_KEY_INFO
*p8
;
227 ASN1_BMPSTRING
*fname
= NULL
;
228 ASN1_OCTET_STRING
*lkid
= NULL
;
230 if ((attrib
= PKCS12_get_attr(bag
, NID_friendlyName
)))
231 fname
= attrib
->value
.bmpstring
;
233 if ((attrib
= PKCS12_get_attr(bag
, NID_localKeyID
)))
234 lkid
= attrib
->value
.octet_string
;
236 switch (M_PKCS12_bag_type(bag
)) {
240 if ((*pkey
= EVP_PKCS82PKEY(bag
->value
.keybag
)) == NULL
)
244 case NID_pkcs8ShroudedKeyBag
:
247 if ((p8
= PKCS12_decrypt_skey(bag
, pass
, passlen
)) == NULL
)
249 *pkey
= EVP_PKCS82PKEY(p8
);
250 PKCS8_PRIV_KEY_INFO_free(p8
);
256 if (M_PKCS12_cert_bag_type(bag
) != NID_x509Certificate
)
258 if ((x509
= PKCS12_certbag2x509(bag
)) == NULL
)
260 if (lkid
&& !X509_keyid_set1(x509
, lkid
->data
, lkid
->length
)) {
267 len
= ASN1_STRING_to_UTF8(&data
, fname
);
269 r
= X509_alias_set1(x509
, data
, len
);
278 if (!sk_X509_push(ocerts
, x509
)) {
285 case NID_safeContentsBag
:
286 return parse_bags(bag
->value
.safes
, pass
, passlen
, pkey
, ocerts
);