]>
Commit | Line | Data |
---|---|---|
0f113f3e MC |
1 | /* |
2 | * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL | |
9a48b07e | 3 | * project. |
8d8c7266 DSH |
4 | */ |
5 | /* ==================================================================== | |
9a48b07e | 6 | * Copyright (c) 1999-2002 The OpenSSL Project. All rights reserved. |
8d8c7266 DSH |
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 | |
0f113f3e | 13 | * notice, this list of conditions and the following disclaimer. |
8d8c7266 DSH |
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> | |
b39fc560 | 60 | #include "internal/cryptlib.h" |
ec577822 | 61 | #include <openssl/pkcs12.h> |
8d8c7266 | 62 | |
0f113f3e MC |
63 | static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags, |
64 | PKCS12_SAFEBAG *bag); | |
9a48b07e | 65 | |
8528128b | 66 | static int copy_bag_attr(PKCS12_SAFEBAG *bag, EVP_PKEY *pkey, int nid) |
0f113f3e MC |
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 | } | |
8528128b | 78 | |
6b691a5c | 79 | PKCS12 *PKCS12_create(char *pass, char *name, EVP_PKEY *pkey, X509 *cert, |
0f113f3e MC |
80 | STACK_OF(X509) *ca, int nid_key, int nid_cert, int iter, |
81 | int mac_iter, int keytype) | |
8d8c7266 | 82 | { |
0f113f3e MC |
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) | |
558c94ef | 93 | #ifdef OPENSSL_NO_RC2 |
0f113f3e | 94 | nid_cert = NID_pbe_WithSHA1And3_Key_TripleDES_CBC; |
558c94ef | 95 | #else |
0f113f3e | 96 | nid_cert = NID_pbe_WithSHA1And40BitRC2_CBC; |
558c94ef | 97 | #endif |
0f113f3e MC |
98 | if (!nid_key) |
99 | nid_key = NID_pbe_WithSHA1And3_Key_TripleDES_CBC; | |
100 | if (!iter) | |
101 | iter = PKCS12_DEFAULT_ITER; | |
102 | if (!mac_iter) | |
103 | mac_iter = 1; | |
104 | ||
105 | if (!pkey && !cert && !ca) { | |
106 | PKCS12err(PKCS12_F_PKCS12_CREATE, PKCS12_R_INVALID_NULL_ARGUMENT); | |
107 | return NULL; | |
108 | } | |
109 | ||
110 | if (pkey && cert) { | |
111 | if (!X509_check_private_key(cert, pkey)) | |
112 | return NULL; | |
113 | X509_digest(cert, EVP_sha1(), keyid, &keyidlen); | |
114 | } | |
115 | ||
116 | if (cert) { | |
117 | bag = PKCS12_add_cert(&bags, cert); | |
118 | if (name && !PKCS12_add_friendlyname(bag, name, -1)) | |
119 | goto err; | |
120 | if (keyidlen && !PKCS12_add_localkeyid(bag, keyid, keyidlen)) | |
121 | goto err; | |
122 | } | |
123 | ||
124 | /* Add all other certificates */ | |
125 | for (i = 0; i < sk_X509_num(ca); i++) { | |
126 | if (!PKCS12_add_cert(&bags, sk_X509_value(ca, i))) | |
127 | goto err; | |
128 | } | |
129 | ||
130 | if (bags && !PKCS12_add_safe(&safes, bags, nid_cert, iter, pass)) | |
131 | goto err; | |
132 | ||
133 | sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free); | |
134 | bags = NULL; | |
135 | ||
136 | if (pkey) { | |
137 | bag = PKCS12_add_key(&bags, pkey, keytype, iter, nid_key, pass); | |
138 | ||
139 | if (!bag) | |
140 | goto err; | |
141 | ||
142 | if (!copy_bag_attr(bag, pkey, NID_ms_csp_name)) | |
143 | goto err; | |
144 | if (!copy_bag_attr(bag, pkey, NID_LocalKeySet)) | |
145 | goto err; | |
146 | ||
147 | if (name && !PKCS12_add_friendlyname(bag, name, -1)) | |
148 | goto err; | |
149 | if (keyidlen && !PKCS12_add_localkeyid(bag, keyid, keyidlen)) | |
150 | goto err; | |
151 | } | |
152 | ||
153 | if (bags && !PKCS12_add_safe(&safes, bags, -1, 0, NULL)) | |
154 | goto err; | |
155 | ||
156 | sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free); | |
157 | bags = NULL; | |
158 | ||
159 | p12 = PKCS12_add_safes(safes, 0); | |
160 | ||
161 | if (!p12) | |
162 | goto err; | |
163 | ||
164 | sk_PKCS7_pop_free(safes, PKCS7_free); | |
165 | ||
166 | safes = NULL; | |
167 | ||
168 | if ((mac_iter != -1) && | |
169 | !PKCS12_set_mac(p12, pass, -1, NULL, 0, mac_iter, NULL)) | |
170 | goto err; | |
171 | ||
172 | return p12; | |
173 | ||
174 | err: | |
e0e920b1 RS |
175 | PKCS12_free(p12); |
176 | sk_PKCS7_pop_free(safes, PKCS7_free); | |
177 | sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free); | |
0f113f3e | 178 | return NULL; |
9a48b07e DSH |
179 | |
180 | } | |
181 | ||
182 | PKCS12_SAFEBAG *PKCS12_add_cert(STACK_OF(PKCS12_SAFEBAG) **pbags, X509 *cert) | |
0f113f3e MC |
183 | { |
184 | PKCS12_SAFEBAG *bag = NULL; | |
185 | char *name; | |
186 | int namelen = -1; | |
187 | unsigned char *keyid; | |
188 | int keyidlen = -1; | |
8d8c7266 | 189 | |
0f113f3e | 190 | /* Add user certificate */ |
75ebbd9a | 191 | if ((bag = PKCS12_x5092certbag(cert)) == NULL) |
0f113f3e | 192 | goto err; |
9a48b07e | 193 | |
0f113f3e MC |
194 | /* |
195 | * Use friendlyName and localKeyID in certificate. (if present) | |
196 | */ | |
9a48b07e | 197 | |
0f113f3e | 198 | name = (char *)X509_alias_get0(cert, &namelen); |
9a48b07e | 199 | |
0f113f3e MC |
200 | if (name && !PKCS12_add_friendlyname(bag, name, namelen)) |
201 | goto err; | |
9a48b07e | 202 | |
0f113f3e | 203 | keyid = X509_keyid_get0(cert, &keyidlen); |
9a48b07e | 204 | |
0f113f3e MC |
205 | if (keyid && !PKCS12_add_localkeyid(bag, keyid, keyidlen)) |
206 | goto err; | |
9a48b07e | 207 | |
0f113f3e MC |
208 | if (!pkcs12_add_bag(pbags, bag)) |
209 | goto err; | |
9a48b07e | 210 | |
0f113f3e | 211 | return bag; |
9a48b07e | 212 | |
0f113f3e | 213 | err: |
e0e920b1 | 214 | PKCS12_SAFEBAG_free(bag); |
0f113f3e | 215 | return NULL; |
8d8c7266 | 216 | |
0f113f3e | 217 | } |
9a48b07e | 218 | |
0f113f3e MC |
219 | PKCS12_SAFEBAG *PKCS12_add_key(STACK_OF(PKCS12_SAFEBAG) **pbags, |
220 | EVP_PKEY *key, int key_usage, int iter, | |
221 | int nid_key, char *pass) | |
222 | { | |
9a48b07e | 223 | |
0f113f3e MC |
224 | PKCS12_SAFEBAG *bag = NULL; |
225 | PKCS8_PRIV_KEY_INFO *p8 = NULL; | |
9a48b07e | 226 | |
0f113f3e | 227 | /* Make a PKCS#8 structure */ |
75ebbd9a | 228 | if ((p8 = EVP_PKEY2PKCS8(key)) == NULL) |
0f113f3e MC |
229 | goto err; |
230 | if (key_usage && !PKCS8_add_keyusage(p8, key_usage)) | |
231 | goto err; | |
232 | if (nid_key != -1) { | |
233 | bag = PKCS12_MAKE_SHKEYBAG(nid_key, pass, -1, NULL, 0, iter, p8); | |
234 | PKCS8_PRIV_KEY_INFO_free(p8); | |
235 | } else | |
236 | bag = PKCS12_MAKE_KEYBAG(p8); | |
9a48b07e | 237 | |
0f113f3e MC |
238 | if (!bag) |
239 | goto err; | |
9a48b07e | 240 | |
0f113f3e MC |
241 | if (!pkcs12_add_bag(pbags, bag)) |
242 | goto err; | |
9a48b07e | 243 | |
0f113f3e | 244 | return bag; |
9a48b07e | 245 | |
0f113f3e | 246 | err: |
e0e920b1 | 247 | PKCS12_SAFEBAG_free(bag); |
0f113f3e | 248 | return NULL; |
9a48b07e | 249 | |
0f113f3e | 250 | } |
8d8c7266 | 251 | |
9a48b07e | 252 | int PKCS12_add_safe(STACK_OF(PKCS7) **psafes, STACK_OF(PKCS12_SAFEBAG) *bags, |
0f113f3e MC |
253 | int nid_safe, int iter, char *pass) |
254 | { | |
255 | PKCS7 *p7 = NULL; | |
256 | int free_safes = 0; | |
257 | ||
258 | if (!*psafes) { | |
259 | *psafes = sk_PKCS7_new_null(); | |
260 | if (!*psafes) | |
261 | return 0; | |
262 | free_safes = 1; | |
263 | } else | |
264 | free_safes = 0; | |
265 | ||
266 | if (nid_safe == 0) | |
558c94ef | 267 | #ifdef OPENSSL_NO_RC2 |
0f113f3e | 268 | nid_safe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC; |
558c94ef | 269 | #else |
0f113f3e | 270 | nid_safe = NID_pbe_WithSHA1And40BitRC2_CBC; |
558c94ef | 271 | #endif |
9a48b07e | 272 | |
0f113f3e MC |
273 | if (nid_safe == -1) |
274 | p7 = PKCS12_pack_p7data(bags); | |
275 | else | |
276 | p7 = PKCS12_pack_p7encdata(nid_safe, pass, -1, NULL, 0, iter, bags); | |
277 | if (!p7) | |
278 | goto err; | |
279 | ||
280 | if (!sk_PKCS7_push(*psafes, p7)) | |
281 | goto err; | |
282 | ||
283 | return 1; | |
284 | ||
285 | err: | |
286 | if (free_safes) { | |
287 | sk_PKCS7_free(*psafes); | |
288 | *psafes = NULL; | |
289 | } | |
e0e920b1 | 290 | PKCS7_free(p7); |
0f113f3e MC |
291 | return 0; |
292 | ||
293 | } | |
294 | ||
295 | static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags, | |
296 | PKCS12_SAFEBAG *bag) | |
297 | { | |
298 | int free_bags; | |
299 | if (!pbags) | |
300 | return 1; | |
301 | if (!*pbags) { | |
302 | *pbags = sk_PKCS12_SAFEBAG_new_null(); | |
303 | if (!*pbags) | |
304 | return 0; | |
305 | free_bags = 1; | |
306 | } else | |
307 | free_bags = 0; | |
308 | ||
309 | if (!sk_PKCS12_SAFEBAG_push(*pbags, bag)) { | |
310 | if (free_bags) { | |
311 | sk_PKCS12_SAFEBAG_free(*pbags); | |
312 | *pbags = NULL; | |
313 | } | |
314 | return 0; | |
315 | } | |
316 | ||
317 | return 1; | |
318 | ||
319 | } | |
9a48b07e DSH |
320 | |
321 | PKCS12 *PKCS12_add_safes(STACK_OF(PKCS7) *safes, int nid_p7) | |
0f113f3e MC |
322 | { |
323 | PKCS12 *p12; | |
324 | if (nid_p7 <= 0) | |
325 | nid_p7 = NID_pkcs7_data; | |
326 | p12 = PKCS12_init(nid_p7); | |
9a48b07e | 327 | |
0f113f3e MC |
328 | if (!p12) |
329 | return NULL; | |
9a48b07e | 330 | |
0f113f3e MC |
331 | if (!PKCS12_pack_authsafes(p12, safes)) { |
332 | PKCS12_free(p12); | |
333 | return NULL; | |
334 | } | |
8d8c7266 | 335 | |
0f113f3e | 336 | return p12; |
8d8c7266 | 337 | |
0f113f3e | 338 | } |