2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
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).
63 /* Simplified PKCS#12 routines */
66 static int parse_pk12( PKCS12 *p12, unsigned char *pass, int passlen, EVP_PKEY **pkey, X509 **cert, STACK **ca);
67 static int parse_bags( STACK *bags, unsigned char *pass, int passlen, EVP_PKEY **pkey, X509 **cert, STACK **ca, ASN1_OCTET_STRING **keyid, char *keymatch);
68 static int parse_bag( PKCS12_SAFEBAG *bag, unsigned char *pass, int passlen, EVP_PKEY **pkey, X509 **cert, STACK **ca, ASN1_OCTET_STRING **keyid, char *keymatch);
70 static int parse_pk12();
71 static int parse_bags();
72 static int parse_bag();
75 /* Parse and decrypt a PKCS#12 structure returning user key, user cert
76 * and other (CA) certs. Note either ca should be NULL, *ca should be NULL,
77 * or it should point to a valid STACK structure. pkey and cert can be
81 int PKCS12_parse (p12, pass, pkey, cert, ca)
89 /* Check for NULL PKCS12 structure */
92 PKCS12err(PKCS12_F_PKCS12_PARSE,PKCS12_R_INVALID_NULL_PKCS12_POINTER);
96 /* Allocate stack for ca certificates if needed */
97 if ((ca != NULL) && (*ca == NULL)) {
98 if (!(*ca = sk_new(NULL))) {
99 PKCS12err(PKCS12_F_PKCS12_PARSE,ERR_R_MALLOC_FAILURE);
104 if(pkey) *pkey = NULL;
105 if(cert) *cert = NULL;
109 if (!PKCS12_verify_mac (p12, pass, -1)) {
110 PKCS12err(PKCS12_F_PKCS12_PARSE,PKCS12_R_MAC_VERIFY_FAILURE);
114 if (!parse_pk12 (p12, pass, -1, pkey, cert, ca)) {
115 PKCS12err(PKCS12_F_PKCS12_PARSE,PKCS12_R_PARSE_ERROR);
123 if (pkey && *pkey) EVP_PKEY_free (*pkey);
124 if (cert && *cert) X509_free (*cert);
125 if (ca) sk_pop_free (*ca, X509_free);
130 /* Parse the outer PKCS#12 structure */
132 static int parse_pk12 (p12, pass, passlen, pkey, cert, ca)
140 STACK *asafes, *bags;
143 ASN1_OCTET_STRING *keyid = NULL;
145 if (!( asafes = M_PKCS12_unpack_authsafes (p12))) return 0;
146 for (i = 0; i < sk_num (asafes); i++) {
147 p7 = (PKCS7 *) sk_value (asafes, i);
148 bagnid = OBJ_obj2nid (p7->type);
149 if (bagnid == NID_pkcs7_data) {
150 bags = M_PKCS12_unpack_p7data (p7);
151 } else if (bagnid == NID_pkcs7_encrypted) {
152 bags = M_PKCS12_unpack_p7encdata (p7, pass, passlen);
155 sk_pop_free (asafes, PKCS7_free);
158 if (!parse_bags (bags, pass, passlen, pkey, cert, ca,
159 &keyid, &keymatch)) {
160 sk_pop_free (bags, PKCS12_SAFEBAG_free);
161 sk_pop_free (asafes, PKCS7_free);
164 sk_pop_free (bags, PKCS12_SAFEBAG_free);
166 sk_pop_free (asafes, PKCS7_free);
167 if (keyid) ASN1_OCTET_STRING_free (keyid);
172 static int parse_bags (bags, pass, passlen, pkey, cert, ca, keyid, keymatch)
179 ASN1_OCTET_STRING **keyid;
183 for (i = 0; i < sk_num (bags); i++) {
184 if (!parse_bag ((PKCS12_SAFEBAG *)sk_value (bags, i),
185 pass, passlen, pkey, cert, ca, keyid,
191 #define MATCH_KEY 0x1
192 #define MATCH_CERT 0x2
193 #define MATCH_ALL 0x3
195 static int parse_bag (bag, pass, passlen, pkey, cert, ca, keyid, keymatch)
202 ASN1_OCTET_STRING **keyid;
205 PKCS8_PRIV_KEY_INFO *p8;
207 ASN1_OCTET_STRING *lkey = NULL;
211 if ((attrib = PKCS12_get_attr (bag, NID_localKeyID)))
212 lkey = attrib->value.octet_string;
214 /* Check for any local key id matching (if needed) */
215 if (lkey && ((*keymatch & MATCH_ALL) != MATCH_ALL)) {
217 if (ASN1_OCTET_STRING_cmp (*keyid, lkey)) lkey = NULL;
219 if (!(*keyid = ASN1_OCTET_STRING_dup (lkey))) {
220 PKCS12err(PKCS12_F_PARSE_BAGS,ERR_R_MALLOC_FAILURE);
226 switch (M_PKCS12_bag_type(bag))
229 if (!lkey || !pkey) return 1;
230 if (!(*pkey = EVP_PKCS82PKEY (bag->value.keybag))) return 0;
231 *keymatch |= MATCH_KEY;
234 case NID_pkcs8ShroudedKeyBag:
235 if (!lkey || !pkey) return 1;
236 if (!(p8 = M_PKCS12_decrypt_skey (bag, pass, passlen)))
238 *pkey = EVP_PKCS82PKEY (p8);
239 PKCS8_PRIV_KEY_INFO_free (p8);
240 if (!(*pkey)) return 0;
241 *keymatch |= MATCH_KEY;
245 if (M_PKCS12_cert_bag_type(bag) != NID_x509Certificate )
247 if (!(x509 = M_PKCS12_certbag2x509(bag))) return 0;
249 *keymatch |= MATCH_CERT;
250 if (cert) *cert = x509;
251 } else if (ca) sk_push (*ca, (char *)x509);
254 case NID_safeContentsBag:
255 return parse_bags(bag->value.safes, pass, passlen,
256 pkey, cert, ca, keyid, keymatch);