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