Don't output bogus errors in PKCS12_parse
[openssl.git] / crypto / pkcs12 / p12_crt.c
1 /* p12_crt.c */
2 /*
3  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4  * project.
5  */
6 /* ====================================================================
7  * Copyright (c) 1999-2002 The OpenSSL Project.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in
18  *    the documentation and/or other materials provided with the
19  *    distribution.
20  *
21  * 3. All advertising materials mentioning features or use of this
22  *    software must display the following acknowledgment:
23  *    "This product includes software developed by the OpenSSL Project
24  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25  *
26  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27  *    endorse or promote products derived from this software without
28  *    prior written permission. For written permission, please contact
29  *    licensing@OpenSSL.org.
30  *
31  * 5. Products derived from this software may not be called "OpenSSL"
32  *    nor may "OpenSSL" appear in their names without prior written
33  *    permission of the OpenSSL Project.
34  *
35  * 6. Redistributions of any form whatsoever must retain the following
36  *    acknowledgment:
37  *    "This product includes software developed by the OpenSSL Project
38  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51  * OF THE POSSIBILITY OF SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This product includes cryptographic software written by Eric Young
55  * (eay@cryptsoft.com).  This product includes software written by Tim
56  * Hudson (tjh@cryptsoft.com).
57  *
58  */
59
60 #include <stdio.h>
61 #include "internal/cryptlib.h"
62 #include <openssl/pkcs12.h>
63
64 static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags,
65                           PKCS12_SAFEBAG *bag);
66
67 static int copy_bag_attr(PKCS12_SAFEBAG *bag, EVP_PKEY *pkey, int nid)
68 {
69     int idx;
70     X509_ATTRIBUTE *attr;
71     idx = EVP_PKEY_get_attr_by_NID(pkey, nid, -1);
72     if (idx < 0)
73         return 1;
74     attr = EVP_PKEY_get_attr(pkey, idx);
75     if (!X509at_add1_attr(&bag->attrib, attr))
76         return 0;
77     return 1;
78 }
79
80 PKCS12 *PKCS12_create(char *pass, char *name, EVP_PKEY *pkey, X509 *cert,
81                       STACK_OF(X509) *ca, int nid_key, int nid_cert, int iter,
82                       int mac_iter, int keytype)
83 {
84     PKCS12 *p12 = NULL;
85     STACK_OF(PKCS7) *safes = NULL;
86     STACK_OF(PKCS12_SAFEBAG) *bags = NULL;
87     PKCS12_SAFEBAG *bag = NULL;
88     int i;
89     unsigned char keyid[EVP_MAX_MD_SIZE];
90     unsigned int keyidlen = 0;
91
92     /* Set defaults */
93     if (!nid_cert)
94 #ifdef OPENSSL_NO_RC2
95         nid_cert = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
96 #else
97         nid_cert = NID_pbe_WithSHA1And40BitRC2_CBC;
98 #endif
99     if (!nid_key)
100         nid_key = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
101     if (!iter)
102         iter = PKCS12_DEFAULT_ITER;
103     if (!mac_iter)
104         mac_iter = 1;
105
106     if (!pkey && !cert && !ca) {
107         PKCS12err(PKCS12_F_PKCS12_CREATE, PKCS12_R_INVALID_NULL_ARGUMENT);
108         return NULL;
109     }
110
111     if (pkey && cert) {
112         if (!X509_check_private_key(cert, pkey))
113             return NULL;
114         X509_digest(cert, EVP_sha1(), keyid, &keyidlen);
115     }
116
117     if (cert) {
118         bag = PKCS12_add_cert(&bags, cert);
119         if (name && !PKCS12_add_friendlyname(bag, name, -1))
120             goto err;
121         if (keyidlen && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
122             goto err;
123     }
124
125     /* Add all other certificates */
126     for (i = 0; i < sk_X509_num(ca); i++) {
127         if (!PKCS12_add_cert(&bags, sk_X509_value(ca, i)))
128             goto err;
129     }
130
131     if (bags && !PKCS12_add_safe(&safes, bags, nid_cert, iter, pass))
132         goto err;
133
134     sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
135     bags = NULL;
136
137     if (pkey) {
138         bag = PKCS12_add_key(&bags, pkey, keytype, iter, nid_key, pass);
139
140         if (!bag)
141             goto err;
142
143         if (!copy_bag_attr(bag, pkey, NID_ms_csp_name))
144             goto err;
145         if (!copy_bag_attr(bag, pkey, NID_LocalKeySet))
146             goto err;
147
148         if (name && !PKCS12_add_friendlyname(bag, name, -1))
149             goto err;
150         if (keyidlen && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
151             goto err;
152     }
153
154     if (bags && !PKCS12_add_safe(&safes, bags, -1, 0, NULL))
155         goto err;
156
157     sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
158     bags = NULL;
159
160     p12 = PKCS12_add_safes(safes, 0);
161
162     if (!p12)
163         goto err;
164
165     sk_PKCS7_pop_free(safes, PKCS7_free);
166
167     safes = NULL;
168
169     if ((mac_iter != -1) &&
170         !PKCS12_set_mac(p12, pass, -1, NULL, 0, mac_iter, NULL))
171         goto err;
172
173     return p12;
174
175  err:
176     PKCS12_free(p12);
177     sk_PKCS7_pop_free(safes, PKCS7_free);
178     sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
179     return NULL;
180
181 }
182
183 PKCS12_SAFEBAG *PKCS12_add_cert(STACK_OF(PKCS12_SAFEBAG) **pbags, X509 *cert)
184 {
185     PKCS12_SAFEBAG *bag = NULL;
186     char *name;
187     int namelen = -1;
188     unsigned char *keyid;
189     int keyidlen = -1;
190
191     /* Add user certificate */
192     if ((bag = PKCS12_x5092certbag(cert)) == NULL)
193         goto err;
194
195     /*
196      * Use friendlyName and localKeyID in certificate. (if present)
197      */
198
199     name = (char *)X509_alias_get0(cert, &namelen);
200
201     if (name && !PKCS12_add_friendlyname(bag, name, namelen))
202         goto err;
203
204     keyid = X509_keyid_get0(cert, &keyidlen);
205
206     if (keyid && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
207         goto err;
208
209     if (!pkcs12_add_bag(pbags, bag))
210         goto err;
211
212     return bag;
213
214  err:
215     PKCS12_SAFEBAG_free(bag);
216     return NULL;
217
218 }
219
220 PKCS12_SAFEBAG *PKCS12_add_key(STACK_OF(PKCS12_SAFEBAG) **pbags,
221                                EVP_PKEY *key, int key_usage, int iter,
222                                int nid_key, char *pass)
223 {
224
225     PKCS12_SAFEBAG *bag = NULL;
226     PKCS8_PRIV_KEY_INFO *p8 = NULL;
227
228     /* Make a PKCS#8 structure */
229     if ((p8 = EVP_PKEY2PKCS8(key)) == NULL)
230         goto err;
231     if (key_usage && !PKCS8_add_keyusage(p8, key_usage))
232         goto err;
233     if (nid_key != -1) {
234         bag = PKCS12_MAKE_SHKEYBAG(nid_key, pass, -1, NULL, 0, iter, p8);
235         PKCS8_PRIV_KEY_INFO_free(p8);
236     } else
237         bag = PKCS12_MAKE_KEYBAG(p8);
238
239     if (!bag)
240         goto err;
241
242     if (!pkcs12_add_bag(pbags, bag))
243         goto err;
244
245     return bag;
246
247  err:
248     PKCS12_SAFEBAG_free(bag);
249     return NULL;
250
251 }
252
253 int PKCS12_add_safe(STACK_OF(PKCS7) **psafes, STACK_OF(PKCS12_SAFEBAG) *bags,
254                     int nid_safe, int iter, char *pass)
255 {
256     PKCS7 *p7 = NULL;
257     int free_safes = 0;
258
259     if (!*psafes) {
260         *psafes = sk_PKCS7_new_null();
261         if (!*psafes)
262             return 0;
263         free_safes = 1;
264     } else
265         free_safes = 0;
266
267     if (nid_safe == 0)
268 #ifdef OPENSSL_NO_RC2
269         nid_safe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
270 #else
271         nid_safe = NID_pbe_WithSHA1And40BitRC2_CBC;
272 #endif
273
274     if (nid_safe == -1)
275         p7 = PKCS12_pack_p7data(bags);
276     else
277         p7 = PKCS12_pack_p7encdata(nid_safe, pass, -1, NULL, 0, iter, bags);
278     if (!p7)
279         goto err;
280
281     if (!sk_PKCS7_push(*psafes, p7))
282         goto err;
283
284     return 1;
285
286  err:
287     if (free_safes) {
288         sk_PKCS7_free(*psafes);
289         *psafes = NULL;
290     }
291     PKCS7_free(p7);
292     return 0;
293
294 }
295
296 static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags,
297                           PKCS12_SAFEBAG *bag)
298 {
299     int free_bags;
300     if (!pbags)
301         return 1;
302     if (!*pbags) {
303         *pbags = sk_PKCS12_SAFEBAG_new_null();
304         if (!*pbags)
305             return 0;
306         free_bags = 1;
307     } else
308         free_bags = 0;
309
310     if (!sk_PKCS12_SAFEBAG_push(*pbags, bag)) {
311         if (free_bags) {
312             sk_PKCS12_SAFEBAG_free(*pbags);
313             *pbags = NULL;
314         }
315         return 0;
316     }
317
318     return 1;
319
320 }
321
322 PKCS12 *PKCS12_add_safes(STACK_OF(PKCS7) *safes, int nid_p7)
323 {
324     PKCS12 *p12;
325     if (nid_p7 <= 0)
326         nid_p7 = NID_pkcs7_data;
327     p12 = PKCS12_init(nid_p7);
328
329     if (!p12)
330         return NULL;
331
332     if (!PKCS12_pack_authsafes(p12, safes)) {
333         PKCS12_free(p12);
334         return NULL;
335     }
336
337     return p12;
338
339 }