summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/pkcs12/p12_crt.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/pkcs12/p12_crt.c')
-rw-r--r--src/lib/libcrypto/pkcs12/p12_crt.c367
1 files changed, 0 insertions, 367 deletions
diff --git a/src/lib/libcrypto/pkcs12/p12_crt.c b/src/lib/libcrypto/pkcs12/p12_crt.c
deleted file mode 100644
index e863de52ce..0000000000
--- a/src/lib/libcrypto/pkcs12/p12_crt.c
+++ /dev/null
@@ -1,367 +0,0 @@
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#ifdef OPENSSL_FIPS
63#include <openssl/fips.h>
64#endif
65
66
67
68static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags, 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, int mac_iter,
85 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 {
98#ifdef OPENSSL_FIPS
99 if (FIPS_mode())
100 nid_cert = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
101 else
102#endif
103 nid_cert = NID_pbe_WithSHA1And40BitRC2_CBC;
104 }
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;
111
112 if(!pkey && !cert && !ca)
113 {
114 PKCS12err(PKCS12_F_PKCS12_CREATE,PKCS12_R_INVALID_NULL_ARGUMENT);
115 return NULL;
116 }
117
118 if (pkey && cert)
119 {
120 if(!X509_check_private_key(cert, pkey))
121 return NULL;
122 X509_digest(cert, EVP_sha1(), keyid, &keyidlen);
123 }
124
125 if (cert)
126 {
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 {
137 if (!PKCS12_add_cert(&bags, sk_X509_value(ca, i)))
138 goto err;
139 }
140
141 if (bags && !PKCS12_add_safe(&safes, bags, nid_cert, iter, pass))
142 goto err;
143
144 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
145 bags = NULL;
146
147 if (pkey)
148 {
149 bag = PKCS12_add_key(&bags, pkey, keytype, iter, nid_key, pass);
150
151 if (!bag)
152 goto err;
153
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;
158
159 if(name && !PKCS12_add_friendlyname(bag, name, -1))
160 goto err;
161 if(keyidlen && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
162 goto err;
163 }
164
165 if (bags && !PKCS12_add_safe(&safes, bags, -1, 0, NULL))
166 goto err;
167
168 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
169 bags = NULL;
170
171 p12 = PKCS12_add_safes(safes, 0);
172
173 sk_PKCS7_pop_free(safes, PKCS7_free);
174
175 safes = NULL;
176
177 if ((mac_iter != -1) &&
178 !PKCS12_set_mac(p12, pass, -1, NULL, 0, mac_iter, NULL))
179 goto err;
180
181 return p12;
182
183 err:
184
185 if (p12)
186 PKCS12_free(p12);
187 if (safes)
188 sk_PKCS7_pop_free(safes, PKCS7_free);
189 if (bags)
190 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
191 return NULL;
192
193}
194
195PKCS12_SAFEBAG *PKCS12_add_cert(STACK_OF(PKCS12_SAFEBAG) **pbags, X509 *cert)
196 {
197 PKCS12_SAFEBAG *bag = NULL;
198 char *name;
199 int namelen = -1;
200 unsigned char *keyid;
201 int keyidlen = -1;
202
203 /* Add user certificate */
204 if(!(bag = PKCS12_x5092certbag(cert)))
205 goto err;
206
207 /* Use friendlyName and localKeyID in certificate.
208 * (if present)
209 */
210
211 name = (char *)X509_alias_get0(cert, &namelen);
212
213 if(name && !PKCS12_add_friendlyname(bag, name, namelen))
214 goto err;
215
216 keyid = X509_keyid_get0(cert, &keyidlen);
217
218 if(keyid && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
219 goto err;
220
221 if (!pkcs12_add_bag(pbags, bag))
222 goto err;
223
224 return bag;
225
226 err:
227
228 if (bag)
229 PKCS12_SAFEBAG_free(bag);
230
231 return NULL;
232
233 }
234
235PKCS12_SAFEBAG *PKCS12_add_key(STACK_OF(PKCS12_SAFEBAG) **pbags, EVP_PKEY *key,
236 int key_usage, int iter,
237 int nid_key, char *pass)
238 {
239
240 PKCS12_SAFEBAG *bag = NULL;
241 PKCS8_PRIV_KEY_INFO *p8 = NULL;
242
243 /* Make a PKCS#8 structure */
244 if(!(p8 = EVP_PKEY2PKCS8(key)))
245 goto err;
246 if(key_usage && !PKCS8_add_keyusage(p8, key_usage))
247 goto err;
248 if (nid_key != -1)
249 {
250 bag = PKCS12_MAKE_SHKEYBAG(nid_key, pass, -1, NULL, 0, iter, p8);
251 PKCS8_PRIV_KEY_INFO_free(p8);
252 }
253 else
254 bag = PKCS12_MAKE_KEYBAG(p8);
255
256 if(!bag)
257 goto err;
258
259 if (!pkcs12_add_bag(pbags, bag))
260 goto err;
261
262 return bag;
263
264 err:
265
266 if (bag)
267 PKCS12_SAFEBAG_free(bag);
268
269 return NULL;
270
271 }
272
273int PKCS12_add_safe(STACK_OF(PKCS7) **psafes, STACK_OF(PKCS12_SAFEBAG) *bags,
274 int nid_safe, int iter, char *pass)
275 {
276 PKCS7 *p7 = NULL;
277 int free_safes = 0;
278
279 if (!*psafes)
280 {
281 *psafes = sk_PKCS7_new_null();
282 if (!*psafes)
283 return 0;
284 free_safes = 1;
285 }
286 else
287 free_safes = 0;
288
289 if (nid_safe == 0)
290 nid_safe = NID_pbe_WithSHA1And40BitRC2_CBC;
291
292 if (nid_safe == -1)
293 p7 = PKCS12_pack_p7data(bags);
294 else
295 p7 = PKCS12_pack_p7encdata(nid_safe, pass, -1, NULL, 0,
296 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 {
308 sk_PKCS7_free(*psafes);
309 *psafes = NULL;
310 }
311
312 if (p7)
313 PKCS7_free(p7);
314
315 return 0;
316
317 }
318
319static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags, PKCS12_SAFEBAG *bag)
320 {
321 int free_bags;
322 if (!pbags)
323 return 1;
324 if (!*pbags)
325 {
326 *pbags = sk_PKCS12_SAFEBAG_new_null();
327 if (!*pbags)
328 return 0;
329 free_bags = 1;
330 }
331 else
332 free_bags = 0;
333
334 if (!sk_PKCS12_SAFEBAG_push(*pbags, bag))
335 {
336 if (free_bags)
337 {
338 sk_PKCS12_SAFEBAG_free(*pbags);
339 *pbags = NULL;
340 }
341 return 0;
342 }
343
344 return 1;
345
346 }
347
348
349PKCS12 *PKCS12_add_safes(STACK_OF(PKCS7) *safes, int nid_p7)
350 {
351 PKCS12 *p12;
352 if (nid_p7 <= 0)
353 nid_p7 = NID_pkcs7_data;
354 p12 = PKCS12_init(nid_p7);
355
356 if (!p12)
357 return NULL;
358
359 if(!PKCS12_pack_authsafes(p12, safes))
360 {
361 PKCS12_free(p12);
362 return NULL;
363 }
364
365 return p12;
366
367 }