9d9a25d0990c7f1c4b977476758cc86ecd73a79f
[openssl.git] / crypto / pkcs12 / p12_crt.c
1 /* p12_crt.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999-2002 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
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
18  *    distribution.
19  *
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/)"
24  *
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.
29  *
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.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
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  * ====================================================================
52  *
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).
56  *
57  */
58
59 #include <stdio.h>
60 #include "cryptlib.h"
61 #include <openssl/pkcs12.h>
62
63
64 static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags, PKCS12_SAFEBAG *bag);
65
66 static int copy_bag_attr(PKCS12_SAFEBAG *bag, EVP_PKEY *pkey, int nid)
67         {
68         int idx;
69         X509_ATTRIBUTE *attr;
70         idx = EVP_PKEY_get_attr_by_NID(pkey, nid, -1);
71         if (idx < 0)
72                 return 1;
73         attr = EVP_PKEY_get_attr(pkey, idx);
74         if (!X509at_add1_attr(&bag->attrib, attr))
75                 return 0;
76         return 1;
77         }
78
79 PKCS12 *PKCS12_create(char *pass, char *name, EVP_PKEY *pkey, X509 *cert,
80              STACK_OF(X509) *ca, int nid_key, int nid_cert, int iter, int mac_iter,
81              int keytype)
82 {
83         PKCS12 *p12 = NULL;
84         STACK_OF(PKCS7) *safes = NULL;
85         STACK_OF(PKCS12_SAFEBAG) *bags = NULL;
86         PKCS12_SAFEBAG *bag = NULL;
87         int i;
88         unsigned char keyid[EVP_MAX_MD_SIZE];
89         unsigned int keyidlen = 0;
90
91         /* Set defaults */
92         if (!nid_cert)
93 #ifdef OPENSSL_FIPS
94                 if (FIPS_mode())
95                         nid_cert = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
96                 else
97 #endif
98                 nid_cert = NID_pbe_WithSHA1And40BitRC2_CBC;
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                 {
108                 PKCS12err(PKCS12_F_PKCS12_CREATE,PKCS12_R_INVALID_NULL_ARGUMENT);
109                 return NULL;
110                 }
111
112         if (pkey && cert)
113                 {
114                 if(!X509_check_private_key(cert, pkey))
115                         return NULL;
116                 X509_digest(cert, EVP_sha1(), keyid, &keyidlen);
117                 }
118
119         if (cert)
120                 {
121                 bag = PKCS12_add_cert(&bags, cert);
122                 if(name && !PKCS12_add_friendlyname(bag, name, -1))
123                         goto err;
124                 if(keyidlen && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
125                         goto err;
126                 }
127
128         /* Add all other certificates */
129         for(i = 0; i < sk_X509_num(ca); i++)
130                 {
131                 if (!PKCS12_add_cert(&bags, sk_X509_value(ca, i)))
132                         goto err;
133                 }
134
135         if (bags && !PKCS12_add_safe(&safes, bags, nid_cert, iter, pass))
136                         goto err;
137
138         sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
139         bags = NULL;
140
141         if (pkey)
142                 {
143                 bag = PKCS12_add_key(&bags, pkey, keytype, iter, nid_key, pass);
144
145                 if (!bag)
146                         goto err;
147
148                 if (!copy_bag_attr(bag, pkey, NID_ms_csp_name))
149                         goto err;
150                 if (!copy_bag_attr(bag, pkey, NID_LocalKeySet))
151                         goto err;
152
153                 if(name && !PKCS12_add_friendlyname(bag, name, -1))
154                         goto err;
155                 if(keyidlen && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
156                         goto err;
157                 }
158
159         if (bags && !PKCS12_add_safe(&safes, bags, -1, 0, NULL))
160                         goto err;
161
162         sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
163         bags = NULL;
164
165         p12 = PKCS12_add_safes(safes, 0);
166
167         if (!p12)
168                 goto err;
169
170         sk_PKCS7_pop_free(safes, PKCS7_free);
171
172         safes = NULL;
173
174         if ((mac_iter != -1) &&
175                 !PKCS12_set_mac(p12, pass, -1, NULL, 0, mac_iter, NULL))
176             goto err;
177
178         return p12;
179
180         err:
181
182         if (p12)
183                 PKCS12_free(p12);
184         if (safes)
185                 sk_PKCS7_pop_free(safes, PKCS7_free);
186         if (bags)
187                 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
188         return NULL;
189
190 }
191
192 PKCS12_SAFEBAG *PKCS12_add_cert(STACK_OF(PKCS12_SAFEBAG) **pbags, X509 *cert)
193         {
194         PKCS12_SAFEBAG *bag = NULL;
195         char *name;
196         int namelen = -1;
197         unsigned char *keyid;
198         int keyidlen = -1;
199
200         /* Add user certificate */
201         if(!(bag = PKCS12_x5092certbag(cert)))
202                 goto err;
203
204         /* Use friendlyName and localKeyID in certificate.
205          * (if present)
206          */
207
208         name = (char *)X509_alias_get0(cert, &namelen);
209
210         if(name && !PKCS12_add_friendlyname(bag, name, namelen))
211                 goto err;
212
213         keyid = X509_keyid_get0(cert, &keyidlen);
214
215         if(keyid && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
216                 goto err;
217
218         if (!pkcs12_add_bag(pbags, bag))
219                 goto err;
220
221         return bag;
222
223         err:
224
225         if (bag)
226                 PKCS12_SAFEBAG_free(bag);
227
228         return NULL;
229
230         }
231
232 PKCS12_SAFEBAG *PKCS12_add_key(STACK_OF(PKCS12_SAFEBAG) **pbags, EVP_PKEY *key,
233                                                 int key_usage, int iter,
234                                                 int nid_key, char *pass)
235         {
236
237         PKCS12_SAFEBAG *bag = NULL;
238         PKCS8_PRIV_KEY_INFO *p8 = NULL;
239
240         /* Make a PKCS#8 structure */
241         if(!(p8 = EVP_PKEY2PKCS8(key)))
242                 goto err;
243         if(key_usage && !PKCS8_add_keyusage(p8, key_usage))
244                 goto err;
245         if (nid_key != -1)
246                 {
247                 bag = PKCS12_MAKE_SHKEYBAG(nid_key, pass, -1, NULL, 0, iter, p8);
248                 PKCS8_PRIV_KEY_INFO_free(p8);
249                 }
250         else
251                 bag = PKCS12_MAKE_KEYBAG(p8);
252
253         if(!bag)
254                 goto err;
255
256         if (!pkcs12_add_bag(pbags, bag))
257                 goto err;
258
259         return bag;
260
261         err:
262
263         if (bag)
264                 PKCS12_SAFEBAG_free(bag);
265
266         return NULL;
267
268         }
269
270 int PKCS12_add_safe(STACK_OF(PKCS7) **psafes, STACK_OF(PKCS12_SAFEBAG) *bags,
271                                                 int nid_safe, int iter, char *pass)
272         {
273         PKCS7 *p7 = NULL;
274         int free_safes = 0;
275
276         if (!*psafes)
277                 {
278                 *psafes = sk_PKCS7_new_null();
279                 if (!*psafes)
280                         return 0;
281                 free_safes = 1;
282                 }
283         else
284                 free_safes = 0;
285
286         if (nid_safe == 0)
287                 nid_safe = NID_pbe_WithSHA1And40BitRC2_CBC;
288
289         if (nid_safe == -1)
290                 p7 = PKCS12_pack_p7data(bags);
291         else
292                 p7 = PKCS12_pack_p7encdata(nid_safe, pass, -1, NULL, 0,
293                                           iter, bags);
294         if (!p7)
295                 goto err;
296
297         if (!sk_PKCS7_push(*psafes, p7))
298                 goto err;
299
300         return 1;
301
302         err:
303         if (free_safes)
304                 {
305                 sk_PKCS7_free(*psafes);
306                 *psafes = NULL;
307                 }
308
309         if (p7)
310                 PKCS7_free(p7);
311
312         return 0;
313
314         }
315
316 static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags, PKCS12_SAFEBAG *bag)
317         {
318         int free_bags;
319         if (!pbags)
320                 return 1;
321         if (!*pbags)
322                 {
323                 *pbags = sk_PKCS12_SAFEBAG_new_null();
324                 if (!*pbags)
325                         return 0;
326                 free_bags = 1;
327                 }
328         else 
329                 free_bags = 0;
330
331         if (!sk_PKCS12_SAFEBAG_push(*pbags, bag))
332                 {
333                 if (free_bags)
334                         {
335                         sk_PKCS12_SAFEBAG_free(*pbags);
336                         *pbags = NULL;
337                         }
338                 return 0;
339                 }
340
341         return 1;
342
343         }
344                 
345
346 PKCS12 *PKCS12_add_safes(STACK_OF(PKCS7) *safes, int nid_p7)
347         {
348         PKCS12 *p12;
349         if (nid_p7 <= 0)
350                 nid_p7 = NID_pkcs7_data;
351         p12 = PKCS12_init(nid_p7);
352
353         if (!p12)
354                 return NULL;
355
356         if(!PKCS12_pack_authsafes(p12, safes))
357                 {
358                 PKCS12_free(p12);
359                 return NULL;
360                 }
361
362         return p12;
363
364         }