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