]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/pkcs12/p12_crt.c
pkcs12.h header reorganisation.
[thirdparty/openssl.git] / crypto / pkcs12 / p12_crt.c
CommitLineData
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>
54c38b7f 62#include "p12_lcl.h"
8d8c7266 63
0f113f3e
MC
64static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags,
65 PKCS12_SAFEBAG *bag);
9a48b07e 66
8528128b 67static 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 80PKCS12 *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:
e0e920b1
RS
176 PKCS12_free(p12);
177 sk_PKCS7_pop_free(safes, PKCS7_free);
178 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
0f113f3e 179 return NULL;
9a48b07e
DSH
180
181}
182
183PKCS12_SAFEBAG *PKCS12_add_cert(STACK_OF(PKCS12_SAFEBAG) **pbags, X509 *cert)
0f113f3e
MC
184{
185 PKCS12_SAFEBAG *bag = NULL;
186 char *name;
187 int namelen = -1;
188 unsigned char *keyid;
189 int keyidlen = -1;
8d8c7266 190
0f113f3e 191 /* Add user certificate */
762ee38d 192 if ((bag = PKCS12_SAFEBAG_new_cert(cert)) == NULL)
0f113f3e 193 goto err;
9a48b07e 194
0f113f3e
MC
195 /*
196 * Use friendlyName and localKeyID in certificate. (if present)
197 */
9a48b07e 198
0f113f3e 199 name = (char *)X509_alias_get0(cert, &namelen);
9a48b07e 200
0f113f3e
MC
201 if (name && !PKCS12_add_friendlyname(bag, name, namelen))
202 goto err;
9a48b07e 203
0f113f3e 204 keyid = X509_keyid_get0(cert, &keyidlen);
9a48b07e 205
0f113f3e
MC
206 if (keyid && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
207 goto err;
9a48b07e 208
0f113f3e
MC
209 if (!pkcs12_add_bag(pbags, bag))
210 goto err;
9a48b07e 211
0f113f3e 212 return bag;
9a48b07e 213
0f113f3e 214 err:
e0e920b1 215 PKCS12_SAFEBAG_free(bag);
0f113f3e 216 return NULL;
8d8c7266 217
0f113f3e 218}
9a48b07e 219
0f113f3e
MC
220PKCS12_SAFEBAG *PKCS12_add_key(STACK_OF(PKCS12_SAFEBAG) **pbags,
221 EVP_PKEY *key, int key_usage, int iter,
222 int nid_key, char *pass)
223{
9a48b07e 224
0f113f3e
MC
225 PKCS12_SAFEBAG *bag = NULL;
226 PKCS8_PRIV_KEY_INFO *p8 = NULL;
9a48b07e 227
0f113f3e 228 /* Make a PKCS#8 structure */
75ebbd9a 229 if ((p8 = EVP_PKEY2PKCS8(key)) == NULL)
0f113f3e
MC
230 goto err;
231 if (key_usage && !PKCS8_add_keyusage(p8, key_usage))
232 goto err;
233 if (nid_key != -1) {
234 bag = PKCS12_MAKE_SHKEYBAG(nid_key, pass, -1, NULL, 0, iter, p8);
235 PKCS8_PRIV_KEY_INFO_free(p8);
236 } else
237 bag = PKCS12_MAKE_KEYBAG(p8);
9a48b07e 238
0f113f3e
MC
239 if (!bag)
240 goto err;
9a48b07e 241
0f113f3e
MC
242 if (!pkcs12_add_bag(pbags, bag))
243 goto err;
9a48b07e 244
0f113f3e 245 return bag;
9a48b07e 246
0f113f3e 247 err:
e0e920b1 248 PKCS12_SAFEBAG_free(bag);
0f113f3e 249 return NULL;
9a48b07e 250
0f113f3e 251}
8d8c7266 252
9a48b07e 253int PKCS12_add_safe(STACK_OF(PKCS7) **psafes, STACK_OF(PKCS12_SAFEBAG) *bags,
0f113f3e
MC
254 int nid_safe, int iter, char *pass)
255{
256 PKCS7 *p7 = NULL;
257 int free_safes = 0;
258
259 if (!*psafes) {
260 *psafes = sk_PKCS7_new_null();
261 if (!*psafes)
262 return 0;
263 free_safes = 1;
264 } else
265 free_safes = 0;
266
267 if (nid_safe == 0)
558c94ef 268#ifdef OPENSSL_NO_RC2
0f113f3e 269 nid_safe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
558c94ef 270#else
0f113f3e 271 nid_safe = NID_pbe_WithSHA1And40BitRC2_CBC;
558c94ef 272#endif
9a48b07e 273
0f113f3e
MC
274 if (nid_safe == -1)
275 p7 = PKCS12_pack_p7data(bags);
276 else
277 p7 = PKCS12_pack_p7encdata(nid_safe, pass, -1, NULL, 0, iter, bags);
278 if (!p7)
279 goto err;
280
281 if (!sk_PKCS7_push(*psafes, p7))
282 goto err;
283
284 return 1;
285
286 err:
287 if (free_safes) {
288 sk_PKCS7_free(*psafes);
289 *psafes = NULL;
290 }
e0e920b1 291 PKCS7_free(p7);
0f113f3e
MC
292 return 0;
293
294}
295
296static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags,
297 PKCS12_SAFEBAG *bag)
298{
299 int free_bags;
300 if (!pbags)
301 return 1;
302 if (!*pbags) {
303 *pbags = sk_PKCS12_SAFEBAG_new_null();
304 if (!*pbags)
305 return 0;
306 free_bags = 1;
307 } else
308 free_bags = 0;
309
310 if (!sk_PKCS12_SAFEBAG_push(*pbags, bag)) {
311 if (free_bags) {
312 sk_PKCS12_SAFEBAG_free(*pbags);
313 *pbags = NULL;
314 }
315 return 0;
316 }
317
318 return 1;
319
320}
9a48b07e
DSH
321
322PKCS12 *PKCS12_add_safes(STACK_OF(PKCS7) *safes, int nid_p7)
0f113f3e
MC
323{
324 PKCS12 *p12;
325 if (nid_p7 <= 0)
326 nid_p7 = NID_pkcs7_data;
327 p12 = PKCS12_init(nid_p7);
9a48b07e 328
0f113f3e
MC
329 if (!p12)
330 return NULL;
9a48b07e 331
0f113f3e
MC
332 if (!PKCS12_pack_authsafes(p12, safes)) {
333 PKCS12_free(p12);
334 return NULL;
335 }
8d8c7266 336
0f113f3e 337 return p12;
8d8c7266 338
0f113f3e 339}