sl@0
|
1 |
/* p12_kiss.c */
|
sl@0
|
2 |
/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
|
sl@0
|
3 |
* project 1999.
|
sl@0
|
4 |
*/
|
sl@0
|
5 |
/* ====================================================================
|
sl@0
|
6 |
* Copyright (c) 1999 The OpenSSL Project. All rights reserved.
|
sl@0
|
7 |
*
|
sl@0
|
8 |
* Redistribution and use in source and binary forms, with or without
|
sl@0
|
9 |
* modification, are permitted provided that the following conditions
|
sl@0
|
10 |
* are met:
|
sl@0
|
11 |
*
|
sl@0
|
12 |
* 1. Redistributions of source code must retain the above copyright
|
sl@0
|
13 |
* notice, this list of conditions and the following disclaimer.
|
sl@0
|
14 |
*
|
sl@0
|
15 |
* 2. Redistributions in binary form must reproduce the above copyright
|
sl@0
|
16 |
* notice, this list of conditions and the following disclaimer in
|
sl@0
|
17 |
* the documentation and/or other materials provided with the
|
sl@0
|
18 |
* distribution.
|
sl@0
|
19 |
*
|
sl@0
|
20 |
* 3. All advertising materials mentioning features or use of this
|
sl@0
|
21 |
* software must display the following acknowledgment:
|
sl@0
|
22 |
* "This product includes software developed by the OpenSSL Project
|
sl@0
|
23 |
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
|
sl@0
|
24 |
*
|
sl@0
|
25 |
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
|
sl@0
|
26 |
* endorse or promote products derived from this software without
|
sl@0
|
27 |
* prior written permission. For written permission, please contact
|
sl@0
|
28 |
* licensing@OpenSSL.org.
|
sl@0
|
29 |
*
|
sl@0
|
30 |
* 5. Products derived from this software may not be called "OpenSSL"
|
sl@0
|
31 |
* nor may "OpenSSL" appear in their names without prior written
|
sl@0
|
32 |
* permission of the OpenSSL Project.
|
sl@0
|
33 |
*
|
sl@0
|
34 |
* 6. Redistributions of any form whatsoever must retain the following
|
sl@0
|
35 |
* acknowledgment:
|
sl@0
|
36 |
* "This product includes software developed by the OpenSSL Project
|
sl@0
|
37 |
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
|
sl@0
|
38 |
*
|
sl@0
|
39 |
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
|
sl@0
|
40 |
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
sl@0
|
41 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
sl@0
|
42 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
|
sl@0
|
43 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
sl@0
|
44 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
sl@0
|
45 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
sl@0
|
46 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
sl@0
|
47 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
sl@0
|
48 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
sl@0
|
49 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
sl@0
|
50 |
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
sl@0
|
51 |
* ====================================================================
|
sl@0
|
52 |
*
|
sl@0
|
53 |
* This product includes cryptographic software written by Eric Young
|
sl@0
|
54 |
* (eay@cryptsoft.com). This product includes software written by Tim
|
sl@0
|
55 |
* Hudson (tjh@cryptsoft.com).
|
sl@0
|
56 |
*
|
sl@0
|
57 |
*/
|
sl@0
|
58 |
|
sl@0
|
59 |
#include <stdio.h>
|
sl@0
|
60 |
#include "cryptlib.h"
|
sl@0
|
61 |
#include <openssl/pkcs12.h>
|
sl@0
|
62 |
|
sl@0
|
63 |
/* Simplified PKCS#12 routines */
|
sl@0
|
64 |
|
sl@0
|
65 |
static int parse_pk12( PKCS12 *p12, const char *pass, int passlen,
|
sl@0
|
66 |
EVP_PKEY **pkey, X509 **cert, STACK_OF(X509) **ca);
|
sl@0
|
67 |
|
sl@0
|
68 |
static int parse_bags( STACK_OF(PKCS12_SAFEBAG) *bags, const char *pass,
|
sl@0
|
69 |
int passlen, EVP_PKEY **pkey, X509 **cert,
|
sl@0
|
70 |
STACK_OF(X509) **ca, ASN1_OCTET_STRING **keyid,
|
sl@0
|
71 |
char *keymatch);
|
sl@0
|
72 |
|
sl@0
|
73 |
static int parse_bag( PKCS12_SAFEBAG *bag, const char *pass, int passlen,
|
sl@0
|
74 |
EVP_PKEY **pkey, X509 **cert, STACK_OF(X509) **ca,
|
sl@0
|
75 |
ASN1_OCTET_STRING **keyid, char *keymatch);
|
sl@0
|
76 |
|
sl@0
|
77 |
/* Parse and decrypt a PKCS#12 structure returning user key, user cert
|
sl@0
|
78 |
* and other (CA) certs. Note either ca should be NULL, *ca should be NULL,
|
sl@0
|
79 |
* or it should point to a valid STACK structure. pkey and cert can be
|
sl@0
|
80 |
* passed unitialised.
|
sl@0
|
81 |
*/
|
sl@0
|
82 |
|
sl@0
|
83 |
EXPORT_C int PKCS12_parse(PKCS12 *p12, const char *pass, EVP_PKEY **pkey, X509 **cert,
|
sl@0
|
84 |
STACK_OF(X509) **ca)
|
sl@0
|
85 |
{
|
sl@0
|
86 |
|
sl@0
|
87 |
/* Check for NULL PKCS12 structure */
|
sl@0
|
88 |
|
sl@0
|
89 |
if(!p12) {
|
sl@0
|
90 |
PKCS12err(PKCS12_F_PKCS12_PARSE,PKCS12_R_INVALID_NULL_PKCS12_POINTER);
|
sl@0
|
91 |
return 0;
|
sl@0
|
92 |
}
|
sl@0
|
93 |
|
sl@0
|
94 |
/* Allocate stack for ca certificates if needed */
|
sl@0
|
95 |
if ((ca != NULL) && (*ca == NULL)) {
|
sl@0
|
96 |
if (!(*ca = sk_X509_new_null())) {
|
sl@0
|
97 |
PKCS12err(PKCS12_F_PKCS12_PARSE,ERR_R_MALLOC_FAILURE);
|
sl@0
|
98 |
return 0;
|
sl@0
|
99 |
}
|
sl@0
|
100 |
}
|
sl@0
|
101 |
|
sl@0
|
102 |
if(pkey) *pkey = NULL;
|
sl@0
|
103 |
if(cert) *cert = NULL;
|
sl@0
|
104 |
|
sl@0
|
105 |
/* Check the mac */
|
sl@0
|
106 |
|
sl@0
|
107 |
/* If password is zero length or NULL then try verifying both cases
|
sl@0
|
108 |
* to determine which password is correct. The reason for this is that
|
sl@0
|
109 |
* under PKCS#12 password based encryption no password and a zero length
|
sl@0
|
110 |
* password are two different things...
|
sl@0
|
111 |
*/
|
sl@0
|
112 |
|
sl@0
|
113 |
if(!pass || !*pass) {
|
sl@0
|
114 |
if(PKCS12_verify_mac(p12, NULL, 0)) pass = NULL;
|
sl@0
|
115 |
else if(PKCS12_verify_mac(p12, "", 0)) pass = "";
|
sl@0
|
116 |
else {
|
sl@0
|
117 |
PKCS12err(PKCS12_F_PKCS12_PARSE,PKCS12_R_MAC_VERIFY_FAILURE);
|
sl@0
|
118 |
goto err;
|
sl@0
|
119 |
}
|
sl@0
|
120 |
} else if (!PKCS12_verify_mac(p12, pass, -1)) {
|
sl@0
|
121 |
PKCS12err(PKCS12_F_PKCS12_PARSE,PKCS12_R_MAC_VERIFY_FAILURE);
|
sl@0
|
122 |
goto err;
|
sl@0
|
123 |
}
|
sl@0
|
124 |
|
sl@0
|
125 |
if (!parse_pk12 (p12, pass, -1, pkey, cert, ca))
|
sl@0
|
126 |
{
|
sl@0
|
127 |
PKCS12err(PKCS12_F_PKCS12_PARSE,PKCS12_R_PARSE_ERROR);
|
sl@0
|
128 |
goto err;
|
sl@0
|
129 |
}
|
sl@0
|
130 |
|
sl@0
|
131 |
return 1;
|
sl@0
|
132 |
|
sl@0
|
133 |
err:
|
sl@0
|
134 |
|
sl@0
|
135 |
if (pkey && *pkey) EVP_PKEY_free(*pkey);
|
sl@0
|
136 |
if (cert && *cert) X509_free(*cert);
|
sl@0
|
137 |
if (ca) sk_X509_pop_free(*ca, X509_free);
|
sl@0
|
138 |
return 0;
|
sl@0
|
139 |
|
sl@0
|
140 |
}
|
sl@0
|
141 |
|
sl@0
|
142 |
/* Parse the outer PKCS#12 structure */
|
sl@0
|
143 |
|
sl@0
|
144 |
static int parse_pk12(PKCS12 *p12, const char *pass, int passlen,
|
sl@0
|
145 |
EVP_PKEY **pkey, X509 **cert, STACK_OF(X509) **ca)
|
sl@0
|
146 |
{
|
sl@0
|
147 |
STACK_OF(PKCS7) *asafes;
|
sl@0
|
148 |
STACK_OF(PKCS12_SAFEBAG) *bags;
|
sl@0
|
149 |
int i, bagnid;
|
sl@0
|
150 |
PKCS7 *p7;
|
sl@0
|
151 |
ASN1_OCTET_STRING *keyid = NULL;
|
sl@0
|
152 |
|
sl@0
|
153 |
char keymatch = 0;
|
sl@0
|
154 |
if (!(asafes = PKCS12_unpack_authsafes (p12))) return 0;
|
sl@0
|
155 |
for (i = 0; i < sk_PKCS7_num (asafes); i++) {
|
sl@0
|
156 |
p7 = sk_PKCS7_value (asafes, i);
|
sl@0
|
157 |
bagnid = OBJ_obj2nid (p7->type);
|
sl@0
|
158 |
if (bagnid == NID_pkcs7_data) {
|
sl@0
|
159 |
bags = PKCS12_unpack_p7data(p7);
|
sl@0
|
160 |
} else if (bagnid == NID_pkcs7_encrypted) {
|
sl@0
|
161 |
bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
|
sl@0
|
162 |
} else continue;
|
sl@0
|
163 |
if (!bags) {
|
sl@0
|
164 |
sk_PKCS7_pop_free(asafes, PKCS7_free);
|
sl@0
|
165 |
return 0;
|
sl@0
|
166 |
}
|
sl@0
|
167 |
if (!parse_bags(bags, pass, passlen, pkey, cert, ca,
|
sl@0
|
168 |
&keyid, &keymatch)) {
|
sl@0
|
169 |
sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
|
sl@0
|
170 |
sk_PKCS7_pop_free(asafes, PKCS7_free);
|
sl@0
|
171 |
return 0;
|
sl@0
|
172 |
}
|
sl@0
|
173 |
sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
|
sl@0
|
174 |
}
|
sl@0
|
175 |
sk_PKCS7_pop_free(asafes, PKCS7_free);
|
sl@0
|
176 |
if (keyid) M_ASN1_OCTET_STRING_free(keyid);
|
sl@0
|
177 |
return 1;
|
sl@0
|
178 |
}
|
sl@0
|
179 |
|
sl@0
|
180 |
|
sl@0
|
181 |
static int parse_bags(STACK_OF(PKCS12_SAFEBAG) *bags, const char *pass,
|
sl@0
|
182 |
int passlen, EVP_PKEY **pkey, X509 **cert,
|
sl@0
|
183 |
STACK_OF(X509) **ca, ASN1_OCTET_STRING **keyid,
|
sl@0
|
184 |
char *keymatch)
|
sl@0
|
185 |
{
|
sl@0
|
186 |
int i;
|
sl@0
|
187 |
for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
|
sl@0
|
188 |
if (!parse_bag(sk_PKCS12_SAFEBAG_value (bags, i),
|
sl@0
|
189 |
pass, passlen, pkey, cert, ca, keyid,
|
sl@0
|
190 |
keymatch)) return 0;
|
sl@0
|
191 |
}
|
sl@0
|
192 |
return 1;
|
sl@0
|
193 |
}
|
sl@0
|
194 |
|
sl@0
|
195 |
#define MATCH_KEY 0x1
|
sl@0
|
196 |
#define MATCH_CERT 0x2
|
sl@0
|
197 |
#define MATCH_ALL 0x3
|
sl@0
|
198 |
|
sl@0
|
199 |
static int parse_bag(PKCS12_SAFEBAG *bag, const char *pass, int passlen,
|
sl@0
|
200 |
EVP_PKEY **pkey, X509 **cert, STACK_OF(X509) **ca,
|
sl@0
|
201 |
ASN1_OCTET_STRING **keyid,
|
sl@0
|
202 |
char *keymatch)
|
sl@0
|
203 |
{
|
sl@0
|
204 |
PKCS8_PRIV_KEY_INFO *p8;
|
sl@0
|
205 |
X509 *x509;
|
sl@0
|
206 |
ASN1_OCTET_STRING *lkey = NULL, *ckid = NULL;
|
sl@0
|
207 |
ASN1_TYPE *attrib;
|
sl@0
|
208 |
ASN1_BMPSTRING *fname = NULL;
|
sl@0
|
209 |
|
sl@0
|
210 |
if ((attrib = PKCS12_get_attr (bag, NID_friendlyName)))
|
sl@0
|
211 |
fname = attrib->value.bmpstring;
|
sl@0
|
212 |
|
sl@0
|
213 |
if ((attrib = PKCS12_get_attr (bag, NID_localKeyID))) {
|
sl@0
|
214 |
lkey = attrib->value.octet_string;
|
sl@0
|
215 |
ckid = lkey;
|
sl@0
|
216 |
}
|
sl@0
|
217 |
|
sl@0
|
218 |
/* Check for any local key id matching (if needed) */
|
sl@0
|
219 |
if (lkey && ((*keymatch & MATCH_ALL) != MATCH_ALL)) {
|
sl@0
|
220 |
if (*keyid) {
|
sl@0
|
221 |
if (M_ASN1_OCTET_STRING_cmp(*keyid, lkey)) lkey = NULL;
|
sl@0
|
222 |
} else {
|
sl@0
|
223 |
if (!(*keyid = M_ASN1_OCTET_STRING_dup(lkey))) {
|
sl@0
|
224 |
PKCS12err(PKCS12_F_PARSE_BAG,ERR_R_MALLOC_FAILURE);
|
sl@0
|
225 |
return 0;
|
sl@0
|
226 |
}
|
sl@0
|
227 |
}
|
sl@0
|
228 |
}
|
sl@0
|
229 |
|
sl@0
|
230 |
switch (M_PKCS12_bag_type(bag))
|
sl@0
|
231 |
{
|
sl@0
|
232 |
case NID_keyBag:
|
sl@0
|
233 |
if (!lkey || !pkey) return 1;
|
sl@0
|
234 |
if (!(*pkey = EVP_PKCS82PKEY(bag->value.keybag))) return 0;
|
sl@0
|
235 |
*keymatch |= MATCH_KEY;
|
sl@0
|
236 |
break;
|
sl@0
|
237 |
|
sl@0
|
238 |
case NID_pkcs8ShroudedKeyBag:
|
sl@0
|
239 |
if (!lkey || !pkey) return 1;
|
sl@0
|
240 |
if (!(p8 = PKCS12_decrypt_skey(bag, pass, passlen)))
|
sl@0
|
241 |
return 0;
|
sl@0
|
242 |
*pkey = EVP_PKCS82PKEY(p8);
|
sl@0
|
243 |
PKCS8_PRIV_KEY_INFO_free(p8);
|
sl@0
|
244 |
if (!(*pkey)) return 0;
|
sl@0
|
245 |
*keymatch |= MATCH_KEY;
|
sl@0
|
246 |
break;
|
sl@0
|
247 |
|
sl@0
|
248 |
case NID_certBag:
|
sl@0
|
249 |
if (M_PKCS12_cert_bag_type(bag) != NID_x509Certificate )
|
sl@0
|
250 |
return 1;
|
sl@0
|
251 |
if (!(x509 = PKCS12_certbag2x509(bag))) return 0;
|
sl@0
|
252 |
if(ckid)
|
sl@0
|
253 |
{
|
sl@0
|
254 |
if (!X509_keyid_set1(x509, ckid->data, ckid->length))
|
sl@0
|
255 |
{
|
sl@0
|
256 |
X509_free(x509);
|
sl@0
|
257 |
return 0;
|
sl@0
|
258 |
}
|
sl@0
|
259 |
}
|
sl@0
|
260 |
if(fname) {
|
sl@0
|
261 |
int len, r;
|
sl@0
|
262 |
unsigned char *data;
|
sl@0
|
263 |
len = ASN1_STRING_to_UTF8(&data, fname);
|
sl@0
|
264 |
if(len > 0) {
|
sl@0
|
265 |
r = X509_alias_set1(x509, data, len);
|
sl@0
|
266 |
OPENSSL_free(data);
|
sl@0
|
267 |
if (!r)
|
sl@0
|
268 |
{
|
sl@0
|
269 |
X509_free(x509);
|
sl@0
|
270 |
return 0;
|
sl@0
|
271 |
}
|
sl@0
|
272 |
}
|
sl@0
|
273 |
}
|
sl@0
|
274 |
|
sl@0
|
275 |
|
sl@0
|
276 |
if (lkey) {
|
sl@0
|
277 |
*keymatch |= MATCH_CERT;
|
sl@0
|
278 |
if (cert) *cert = x509;
|
sl@0
|
279 |
else X509_free(x509);
|
sl@0
|
280 |
} else {
|
sl@0
|
281 |
if(ca) sk_X509_push (*ca, x509);
|
sl@0
|
282 |
else X509_free(x509);
|
sl@0
|
283 |
}
|
sl@0
|
284 |
break;
|
sl@0
|
285 |
|
sl@0
|
286 |
case NID_safeContentsBag:
|
sl@0
|
287 |
return parse_bags(bag->value.safes, pass, passlen,
|
sl@0
|
288 |
pkey, cert, ca, keyid, keymatch);
|
sl@0
|
289 |
break;
|
sl@0
|
290 |
|
sl@0
|
291 |
default:
|
sl@0
|
292 |
return 1;
|
sl@0
|
293 |
break;
|
sl@0
|
294 |
}
|
sl@0
|
295 |
return 1;
|
sl@0
|
296 |
}
|
sl@0
|
297 |
|