2 * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/cryptlib.h"
12 #include <openssl/pkcs12.h>
13 #include "crypto/x509.h" /* for X509_add_cert_new() */
15 DEFINE_STACK_OF(PKCS7
)
16 DEFINE_STACK_OF(PKCS12_SAFEBAG
)
18 /* Simplified PKCS#12 routines */
20 static int parse_pk12(PKCS12
*p12
, const char *pass
, int passlen
,
21 EVP_PKEY
**pkey
, STACK_OF(X509
) *ocerts
);
23 static int parse_bags(const STACK_OF(PKCS12_SAFEBAG
) *bags
, const char *pass
,
24 int passlen
, EVP_PKEY
**pkey
, STACK_OF(X509
) *ocerts
);
26 static int parse_bag(PKCS12_SAFEBAG
*bag
, const char *pass
, int passlen
,
27 EVP_PKEY
**pkey
, STACK_OF(X509
) *ocerts
);
30 * Parse and decrypt a PKCS#12 structure returning user key, user cert and
31 * other (CA) certs. Note either ca should be NULL, *ca should be NULL, or it
32 * should point to a valid STACK structure. pkey and/or cert may be NULL;
33 * if non-NULL the variables they point to can be passed uninitialised.
36 int PKCS12_parse(PKCS12
*p12
, const char *pass
, EVP_PKEY
**pkey
, X509
**cert
,
39 STACK_OF(X509
) *ocerts
= NULL
;
47 /* Check for NULL PKCS12 structure */
50 PKCS12err(PKCS12_F_PKCS12_PARSE
,
51 PKCS12_R_INVALID_NULL_PKCS12_POINTER
);
58 * If password is zero length or NULL then try verifying both cases to
59 * determine which password is correct. The reason for this is that under
60 * PKCS#12 password based encryption no password and a zero length
61 * password are two different things...
64 if (pass
== NULL
|| *pass
== '\0') {
65 if (PKCS12_verify_mac(p12
, NULL
, 0))
67 else if (PKCS12_verify_mac(p12
, "", 0))
70 PKCS12err(PKCS12_F_PKCS12_PARSE
, PKCS12_R_MAC_VERIFY_FAILURE
);
73 } else if (!PKCS12_verify_mac(p12
, pass
, -1)) {
74 PKCS12err(PKCS12_F_PKCS12_PARSE
, PKCS12_R_MAC_VERIFY_FAILURE
);
78 /* If needed, allocate stack for other certificates */
79 if ((cert
!= NULL
|| ca
!= NULL
)
80 && (ocerts
= sk_X509_new_null()) == NULL
) {
81 PKCS12err(PKCS12_F_PKCS12_PARSE
, ERR_R_MALLOC_FAILURE
);
85 if (!parse_pk12(p12
, pass
, -1, pkey
, ocerts
)) {
86 PKCS12err(PKCS12_F_PKCS12_PARSE
, PKCS12_R_PARSE_ERROR
);
90 /* Split the certs in ocerts over *cert and *ca as far as requested */
91 while ((x
= sk_X509_shift(ocerts
)) != NULL
) {
92 if (pkey
!= NULL
&& *pkey
!= NULL
93 && cert
!= NULL
&& *cert
== NULL
) {
97 match
= X509_check_private_key(x
, *pkey
);
106 if (!X509_add_cert_new(ca
, x
, X509_ADD_FLAG_DEFAULT
))
112 sk_X509_free(ocerts
);
119 EVP_PKEY_free(*pkey
);
127 sk_X509_pop_free(ocerts
, X509_free
);
132 /* Parse the outer PKCS#12 structure */
134 /* pkey and/or ocerts may be NULL */
135 static int parse_pk12(PKCS12
*p12
, const char *pass
, int passlen
,
136 EVP_PKEY
**pkey
, STACK_OF(X509
) *ocerts
)
138 STACK_OF(PKCS7
) *asafes
;
139 STACK_OF(PKCS12_SAFEBAG
) *bags
;
143 if ((asafes
= PKCS12_unpack_authsafes(p12
)) == NULL
)
145 for (i
= 0; i
< sk_PKCS7_num(asafes
); i
++) {
146 p7
= sk_PKCS7_value(asafes
, i
);
147 bagnid
= OBJ_obj2nid(p7
->type
);
148 if (bagnid
== NID_pkcs7_data
) {
149 bags
= PKCS12_unpack_p7data(p7
);
150 } else if (bagnid
== NID_pkcs7_encrypted
) {
151 bags
= PKCS12_unpack_p7encdata(p7
, pass
, passlen
);
155 sk_PKCS7_pop_free(asafes
, PKCS7_free
);
158 if (!parse_bags(bags
, pass
, passlen
, pkey
, ocerts
)) {
159 sk_PKCS12_SAFEBAG_pop_free(bags
, PKCS12_SAFEBAG_free
);
160 sk_PKCS7_pop_free(asafes
, PKCS7_free
);
163 sk_PKCS12_SAFEBAG_pop_free(bags
, PKCS12_SAFEBAG_free
);
165 sk_PKCS7_pop_free(asafes
, PKCS7_free
);
169 /* pkey and/or ocerts may be NULL */
170 static int parse_bags(const STACK_OF(PKCS12_SAFEBAG
) *bags
, const char *pass
,
171 int passlen
, EVP_PKEY
**pkey
, STACK_OF(X509
) *ocerts
)
174 for (i
= 0; i
< sk_PKCS12_SAFEBAG_num(bags
); i
++) {
175 if (!parse_bag(sk_PKCS12_SAFEBAG_value(bags
, i
),
176 pass
, passlen
, pkey
, ocerts
))
182 /* pkey and/or ocerts may be NULL */
183 static int parse_bag(PKCS12_SAFEBAG
*bag
, const char *pass
, int passlen
,
184 EVP_PKEY
**pkey
, STACK_OF(X509
) *ocerts
)
186 PKCS8_PRIV_KEY_INFO
*p8
;
188 const ASN1_TYPE
*attrib
;
189 ASN1_BMPSTRING
*fname
= NULL
;
190 ASN1_OCTET_STRING
*lkid
= NULL
;
192 if ((attrib
= PKCS12_SAFEBAG_get0_attr(bag
, NID_friendlyName
)))
193 fname
= attrib
->value
.bmpstring
;
195 if ((attrib
= PKCS12_SAFEBAG_get0_attr(bag
, NID_localKeyID
)))
196 lkid
= attrib
->value
.octet_string
;
198 switch (PKCS12_SAFEBAG_get_nid(bag
)) {
200 if (pkey
== NULL
|| *pkey
!= NULL
)
202 *pkey
= EVP_PKCS82PKEY(PKCS12_SAFEBAG_get0_p8inf(bag
));
207 case NID_pkcs8ShroudedKeyBag
:
208 if (pkey
== NULL
|| *pkey
!= NULL
)
210 if ((p8
= PKCS12_decrypt_skey(bag
, pass
, passlen
)) == NULL
)
212 *pkey
= EVP_PKCS82PKEY(p8
);
213 PKCS8_PRIV_KEY_INFO_free(p8
);
220 || PKCS12_SAFEBAG_get_bag_nid(bag
) != NID_x509Certificate
)
222 if ((x509
= PKCS12_SAFEBAG_get1_cert(bag
)) == NULL
)
224 if (lkid
&& !X509_keyid_set1(x509
, lkid
->data
, lkid
->length
)) {
232 len
= ASN1_STRING_to_UTF8(&data
, fname
);
234 r
= X509_alias_set1(x509
, data
, len
);
243 if (!sk_X509_push(ocerts
, x509
)) {
250 case NID_safeContentsBag
:
251 return parse_bags(PKCS12_SAFEBAG_get0_safes(bag
), pass
, passlen
, pkey
,