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