openssl update: 1.0.1h
[tomato/tomato-dir865l.git] / release / src / router / openssl / crypto / pkcs12 / p12_crt.c
blob35e8a4a8d4ba226d705a006c2c226984e005ef45
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.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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.
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/)"
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.
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.
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/)"
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 * ====================================================================
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).
59 #include <stdio.h>
60 #include "cryptlib.h"
61 #include <openssl/pkcs12.h>
64 static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags, PKCS12_SAFEBAG *bag);
66 static int copy_bag_attr(PKCS12_SAFEBAG *bag, EVP_PKEY *pkey, int nid)
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;
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)
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;
91 /* Set defaults */
92 if (!nid_cert)
94 #ifdef OPENSSL_FIPS
95 if (FIPS_mode())
96 nid_cert = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
97 else
98 #endif
99 #ifdef OPENSSL_NO_RC2
100 nid_cert = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
101 #else
102 nid_cert = NID_pbe_WithSHA1And40BitRC2_CBC;
103 #endif
105 if (!nid_key)
106 nid_key = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
107 if (!iter)
108 iter = PKCS12_DEFAULT_ITER;
109 if (!mac_iter)
110 mac_iter = 1;
112 if(!pkey && !cert && !ca)
114 PKCS12err(PKCS12_F_PKCS12_CREATE,PKCS12_R_INVALID_NULL_ARGUMENT);
115 return NULL;
118 if (pkey && cert)
120 if(!X509_check_private_key(cert, pkey))
121 return NULL;
122 X509_digest(cert, EVP_sha1(), keyid, &keyidlen);
125 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;
134 /* Add all other certificates */
135 for(i = 0; i < sk_X509_num(ca); i++)
137 if (!PKCS12_add_cert(&bags, sk_X509_value(ca, i)))
138 goto err;
141 if (bags && !PKCS12_add_safe(&safes, bags, nid_cert, iter, pass))
142 goto err;
144 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
145 bags = NULL;
147 if (pkey)
149 bag = PKCS12_add_key(&bags, pkey, keytype, iter, nid_key, pass);
151 if (!bag)
152 goto err;
154 if (!copy_bag_attr(bag, pkey, NID_ms_csp_name))
155 goto err;
156 if (!copy_bag_attr(bag, pkey, NID_LocalKeySet))
157 goto err;
159 if(name && !PKCS12_add_friendlyname(bag, name, -1))
160 goto err;
161 if(keyidlen && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
162 goto err;
165 if (bags && !PKCS12_add_safe(&safes, bags, -1, 0, NULL))
166 goto err;
168 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
169 bags = NULL;
171 p12 = PKCS12_add_safes(safes, 0);
173 if (!p12)
174 goto err;
176 sk_PKCS7_pop_free(safes, PKCS7_free);
178 safes = NULL;
180 if ((mac_iter != -1) &&
181 !PKCS12_set_mac(p12, pass, -1, NULL, 0, mac_iter, NULL))
182 goto err;
184 return p12;
186 err:
188 if (p12)
189 PKCS12_free(p12);
190 if (safes)
191 sk_PKCS7_pop_free(safes, PKCS7_free);
192 if (bags)
193 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
194 return NULL;
198 PKCS12_SAFEBAG *PKCS12_add_cert(STACK_OF(PKCS12_SAFEBAG) **pbags, X509 *cert)
200 PKCS12_SAFEBAG *bag = NULL;
201 char *name;
202 int namelen = -1;
203 unsigned char *keyid;
204 int keyidlen = -1;
206 /* Add user certificate */
207 if(!(bag = PKCS12_x5092certbag(cert)))
208 goto err;
210 /* Use friendlyName and localKeyID in certificate.
211 * (if present)
214 name = (char *)X509_alias_get0(cert, &namelen);
216 if(name && !PKCS12_add_friendlyname(bag, name, namelen))
217 goto err;
219 keyid = X509_keyid_get0(cert, &keyidlen);
221 if(keyid && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
222 goto err;
224 if (!pkcs12_add_bag(pbags, bag))
225 goto err;
227 return bag;
229 err:
231 if (bag)
232 PKCS12_SAFEBAG_free(bag);
234 return NULL;
238 PKCS12_SAFEBAG *PKCS12_add_key(STACK_OF(PKCS12_SAFEBAG) **pbags, EVP_PKEY *key,
239 int key_usage, int iter,
240 int nid_key, char *pass)
243 PKCS12_SAFEBAG *bag = NULL;
244 PKCS8_PRIV_KEY_INFO *p8 = NULL;
246 /* Make a PKCS#8 structure */
247 if(!(p8 = EVP_PKEY2PKCS8(key)))
248 goto err;
249 if(key_usage && !PKCS8_add_keyusage(p8, key_usage))
250 goto err;
251 if (nid_key != -1)
253 bag = PKCS12_MAKE_SHKEYBAG(nid_key, pass, -1, NULL, 0, iter, p8);
254 PKCS8_PRIV_KEY_INFO_free(p8);
256 else
257 bag = PKCS12_MAKE_KEYBAG(p8);
259 if(!bag)
260 goto err;
262 if (!pkcs12_add_bag(pbags, bag))
263 goto err;
265 return bag;
267 err:
269 if (bag)
270 PKCS12_SAFEBAG_free(bag);
272 return NULL;
276 int PKCS12_add_safe(STACK_OF(PKCS7) **psafes, STACK_OF(PKCS12_SAFEBAG) *bags,
277 int nid_safe, int iter, char *pass)
279 PKCS7 *p7 = NULL;
280 int free_safes = 0;
282 if (!*psafes)
284 *psafes = sk_PKCS7_new_null();
285 if (!*psafes)
286 return 0;
287 free_safes = 1;
289 else
290 free_safes = 0;
292 if (nid_safe == 0)
293 #ifdef OPENSSL_NO_RC2
294 nid_safe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
295 #else
296 nid_safe = NID_pbe_WithSHA1And40BitRC2_CBC;
297 #endif
299 if (nid_safe == -1)
300 p7 = PKCS12_pack_p7data(bags);
301 else
302 p7 = PKCS12_pack_p7encdata(nid_safe, pass, -1, NULL, 0,
303 iter, bags);
304 if (!p7)
305 goto err;
307 if (!sk_PKCS7_push(*psafes, p7))
308 goto err;
310 return 1;
312 err:
313 if (free_safes)
315 sk_PKCS7_free(*psafes);
316 *psafes = NULL;
319 if (p7)
320 PKCS7_free(p7);
322 return 0;
326 static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags, PKCS12_SAFEBAG *bag)
328 int free_bags;
329 if (!pbags)
330 return 1;
331 if (!*pbags)
333 *pbags = sk_PKCS12_SAFEBAG_new_null();
334 if (!*pbags)
335 return 0;
336 free_bags = 1;
338 else
339 free_bags = 0;
341 if (!sk_PKCS12_SAFEBAG_push(*pbags, bag))
343 if (free_bags)
345 sk_PKCS12_SAFEBAG_free(*pbags);
346 *pbags = NULL;
348 return 0;
351 return 1;
356 PKCS12 *PKCS12_add_safes(STACK_OF(PKCS7) *safes, int nid_p7)
358 PKCS12 *p12;
359 if (nid_p7 <= 0)
360 nid_p7 = NID_pkcs7_data;
361 p12 = PKCS12_init(nid_p7);
363 if (!p12)
364 return NULL;
366 if(!PKCS12_pack_authsafes(p12, safes))
368 PKCS12_free(p12);
369 return NULL;
372 return p12;