1/*
2 * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10#include <stdio.h>
11#include "internal/cryptlib.h"
12#include <openssl/pkcs12.h>
13
14/* Simplified PKCS#12 routines */
15
16static int parse_pk12(PKCS12 *p12, const char *pass, int passlen,
17 EVP_PKEY **pkey, STACK_OF(X509) *ocerts);
18
19static int parse_bags(const STACK_OF(PKCS12_SAFEBAG) *bags, const char *pass,
20 int passlen, EVP_PKEY **pkey, STACK_OF(X509) *ocerts);
21
22static int parse_bag(PKCS12_SAFEBAG *bag, const char *pass, int passlen,
23 EVP_PKEY **pkey, STACK_OF(X509) *ocerts);
24
25/*
26 * Parse and decrypt a PKCS#12 structure returning user key, user cert and
27 * other (CA) certs. Note either ca should be NULL, *ca should be NULL, or it
28 * should point to a valid STACK structure. pkey and cert can be passed
29 * uninitialised.
30 */
31
32int PKCS12_parse(PKCS12 *p12, const char *pass, EVP_PKEY **pkey, X509 **cert,
33 STACK_OF(X509) **ca)
34{
35 STACK_OF(X509) *ocerts = NULL;
36 X509 *x = NULL;
37
38 if (pkey)
39 *pkey = NULL;
40 if (cert)
41 *cert = NULL;
42
43 /* Check for NULL PKCS12 structure */
44
45 if (p12 == NULL) {
46 PKCS12err(PKCS12_F_PKCS12_PARSE,
47 PKCS12_R_INVALID_NULL_PKCS12_POINTER);
48 return 0;
49 }
50
51 /* Check the mac */
52
53 /*
54 * If password is zero length or NULL then try verifying both cases to
55 * determine which password is correct. The reason for this is that under
56 * PKCS#12 password based encryption no password and a zero length
57 * password are two different things...
58 */
59
60 if (pass == NULL || *pass == '\0') {
61 if (PKCS12_verify_mac(p12, NULL, 0))
62 pass = NULL;
63 else if (PKCS12_verify_mac(p12, "", 0))
64 pass = "";
65 else {
66 PKCS12err(PKCS12_F_PKCS12_PARSE, PKCS12_R_MAC_VERIFY_FAILURE);
67 goto err;
68 }
69 } else if (!PKCS12_verify_mac(p12, pass, -1)) {
70 PKCS12err(PKCS12_F_PKCS12_PARSE, PKCS12_R_MAC_VERIFY_FAILURE);
71 goto err;
72 }
73
74 /* Allocate stack for other certificates */
75 ocerts = sk_X509_new_null();
76
77 if (!ocerts) {
78 PKCS12err(PKCS12_F_PKCS12_PARSE, ERR_R_MALLOC_FAILURE);
79 goto err;
80 }
81
82 if (!parse_pk12(p12, pass, -1, pkey, ocerts)) {
83 PKCS12err(PKCS12_F_PKCS12_PARSE, PKCS12_R_PARSE_ERROR);
84 goto err;
85 }
86
87 while ((x = sk_X509_pop(ocerts))) {
88 if (pkey != NULL && *pkey != NULL
89 && cert != NULL && *cert == NULL) {
90 ERR_set_mark();
91 if (X509_check_private_key(x, *pkey)) {
92 *cert = x;
93 x = NULL;
94 }
95 ERR_pop_to_mark();
96 }
97
98 if (ca && x) {
99 if (*ca == NULL)
100 *ca = sk_X509_new_null();
101 if (*ca == NULL)
102 goto err;
103 if (!sk_X509_push(*ca, x))
104 goto err;
105 x = NULL;
106 }
107 X509_free(x);
108 }
109
110 sk_X509_pop_free(ocerts, X509_free);
111
112 return 1;
113
114 err:
115
116 if (pkey) {
117 EVP_PKEY_free(*pkey);
118 *pkey = NULL;
119 }
120 if (cert) {
121 X509_free(*cert);
122 *cert = NULL;
123 }
124 X509_free(x);
125 sk_X509_pop_free(ocerts, X509_free);
126 return 0;
127
128}
129
130/* Parse the outer PKCS#12 structure */
131
132static int parse_pk12(PKCS12 *p12, const char *pass, int passlen,
133 EVP_PKEY **pkey, STACK_OF(X509) *ocerts)
134{
135 STACK_OF(PKCS7) *asafes;
136 STACK_OF(PKCS12_SAFEBAG) *bags;
137 int i, bagnid;
138 PKCS7 *p7;
139
140 if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
141 return 0;
142 for (i = 0; i < sk_PKCS7_num(asafes); i++) {
143 p7 = sk_PKCS7_value(asafes, i);
144 bagnid = OBJ_obj2nid(p7->type);
145 if (bagnid == NID_pkcs7_data) {
146 bags = PKCS12_unpack_p7data(p7);
147 } else if (bagnid == NID_pkcs7_encrypted) {
148 bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
149 } else
150 continue;
151 if (!bags) {
152 sk_PKCS7_pop_free(asafes, PKCS7_free);
153 return 0;
154 }
155 if (!parse_bags(bags, pass, passlen, pkey, ocerts)) {
156 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
157 sk_PKCS7_pop_free(asafes, PKCS7_free);
158 return 0;
159 }
160 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
161 }
162 sk_PKCS7_pop_free(asafes, PKCS7_free);
163 return 1;
164}
165
166static int parse_bags(const STACK_OF(PKCS12_SAFEBAG) *bags, const char *pass,
167 int passlen, EVP_PKEY **pkey, STACK_OF(X509) *ocerts)
168{
169 int i;
170 for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
171 if (!parse_bag(sk_PKCS12_SAFEBAG_value(bags, i),
172 pass, passlen, pkey, ocerts))
173 return 0;
174 }
175 return 1;
176}
177
178static int parse_bag(PKCS12_SAFEBAG *bag, const char *pass, int passlen,
179 EVP_PKEY **pkey, STACK_OF(X509) *ocerts)
180{
181 PKCS8_PRIV_KEY_INFO *p8;
182 X509 *x509;
183 const ASN1_TYPE *attrib;
184 ASN1_BMPSTRING *fname = NULL;
185 ASN1_OCTET_STRING *lkid = NULL;
186
187 if ((attrib = PKCS12_SAFEBAG_get0_attr(bag, NID_friendlyName)))
188 fname = attrib->value.bmpstring;
189
190 if ((attrib = PKCS12_SAFEBAG_get0_attr(bag, NID_localKeyID)))
191 lkid = attrib->value.octet_string;
192
193 switch (PKCS12_SAFEBAG_get_nid(bag)) {
194 case NID_keyBag:
195 if (pkey == NULL || *pkey != NULL)
196 return 1;
197 *pkey = EVP_PKCS82PKEY(PKCS12_SAFEBAG_get0_p8inf(bag));
198 if (*pkey == NULL)
199 return 0;
200 break;
201
202 case NID_pkcs8ShroudedKeyBag:
203 if (pkey == NULL || *pkey != NULL)
204 return 1;
205 if ((p8 = PKCS12_decrypt_skey(bag, pass, passlen)) == NULL)
206 return 0;
207 *pkey = EVP_PKCS82PKEY(p8);
208 PKCS8_PRIV_KEY_INFO_free(p8);
209 if (!(*pkey))
210 return 0;
211 break;
212
213 case NID_certBag:
214 if (PKCS12_SAFEBAG_get_bag_nid(bag) != NID_x509Certificate)
215 return 1;
216 if ((x509 = PKCS12_SAFEBAG_get1_cert(bag)) == NULL)
217 return 0;
218 if (lkid && !X509_keyid_set1(x509, lkid->data, lkid->length)) {
219 X509_free(x509);
220 return 0;
221 }
222 if (fname) {
223 int len, r;
224 unsigned char *data;
225 len = ASN1_STRING_to_UTF8(&data, fname);
226 if (len >= 0) {
227 r = X509_alias_set1(x509, data, len);
228 OPENSSL_free(data);
229 if (!r) {
230 X509_free(x509);
231 return 0;
232 }
233 }
234 }
235
236 if (!sk_X509_push(ocerts, x509)) {
237 X509_free(x509);
238 return 0;
239 }
240
241 break;
242
243 case NID_safeContentsBag:
244 return parse_bags(PKCS12_SAFEBAG_get0_safes(bag), pass, passlen, pkey,
245 ocerts);
246
247 default:
248 return 1;
249 }
250 return 1;
251}
252