1/*
2 * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10#include <stdio.h>
11#include "internal/cryptlib.h"
12#include <openssl/pkcs12.h>
13#include "p12_local.h"
14
15static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags,
16 PKCS12_SAFEBAG *bag);
17
18static int copy_bag_attr(PKCS12_SAFEBAG *bag, EVP_PKEY *pkey, int nid)
19{
20 int idx;
21 X509_ATTRIBUTE *attr;
22 idx = EVP_PKEY_get_attr_by_NID(pkey, nid, -1);
23 if (idx < 0)
24 return 1;
25 attr = EVP_PKEY_get_attr(pkey, idx);
26 if (!X509at_add1_attr(&bag->attrib, attr))
27 return 0;
28 return 1;
29}
30
31PKCS12 *PKCS12_create(const char *pass, const char *name, EVP_PKEY *pkey, X509 *cert,
32 STACK_OF(X509) *ca, int nid_key, int nid_cert, int iter,
33 int mac_iter, int keytype)
34{
35 PKCS12 *p12 = NULL;
36 STACK_OF(PKCS7) *safes = NULL;
37 STACK_OF(PKCS12_SAFEBAG) *bags = NULL;
38 PKCS12_SAFEBAG *bag = NULL;
39 int i;
40 unsigned char keyid[EVP_MAX_MD_SIZE];
41 unsigned int keyidlen = 0;
42
43 /* Set defaults */
44 if (!nid_cert)
45#ifdef OPENSSL_NO_RC2
46 nid_cert = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
47#else
48 nid_cert = NID_pbe_WithSHA1And40BitRC2_CBC;
49#endif
50 if (!nid_key)
51 nid_key = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
52 if (!iter)
53 iter = PKCS12_DEFAULT_ITER;
54 if (!mac_iter)
55 mac_iter = 1;
56
57 if (pkey == NULL && cert == NULL && ca == NULL) {
58 PKCS12err(PKCS12_F_PKCS12_CREATE, PKCS12_R_INVALID_NULL_ARGUMENT);
59 return NULL;
60 }
61
62 if (pkey && cert) {
63 if (!X509_check_private_key(cert, pkey))
64 return NULL;
65 X509_digest(cert, EVP_sha1(), keyid, &keyidlen);
66 }
67
68 if (cert) {
69 bag = PKCS12_add_cert(&bags, cert);
70 if (name && !PKCS12_add_friendlyname(bag, name, -1))
71 goto err;
72 if (keyidlen && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
73 goto err;
74 }
75
76 /* Add all other certificates */
77 for (i = 0; i < sk_X509_num(ca); i++) {
78 if (!PKCS12_add_cert(&bags, sk_X509_value(ca, i)))
79 goto err;
80 }
81
82 if (bags && !PKCS12_add_safe(&safes, bags, nid_cert, iter, pass))
83 goto err;
84
85 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
86 bags = NULL;
87
88 if (pkey) {
89 bag = PKCS12_add_key(&bags, pkey, keytype, iter, nid_key, pass);
90
91 if (!bag)
92 goto err;
93
94 if (!copy_bag_attr(bag, pkey, NID_ms_csp_name))
95 goto err;
96 if (!copy_bag_attr(bag, pkey, NID_LocalKeySet))
97 goto err;
98
99 if (name && !PKCS12_add_friendlyname(bag, name, -1))
100 goto err;
101 if (keyidlen && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
102 goto err;
103 }
104
105 if (bags && !PKCS12_add_safe(&safes, bags, -1, 0, NULL))
106 goto err;
107
108 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
109 bags = NULL;
110
111 p12 = PKCS12_add_safes(safes, 0);
112
113 if (p12 == NULL)
114 goto err;
115
116 sk_PKCS7_pop_free(safes, PKCS7_free);
117
118 safes = NULL;
119
120 if ((mac_iter != -1) &&
121 !PKCS12_set_mac(p12, pass, -1, NULL, 0, mac_iter, NULL))
122 goto err;
123
124 return p12;
125
126 err:
127 PKCS12_free(p12);
128 sk_PKCS7_pop_free(safes, PKCS7_free);
129 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
130 return NULL;
131
132}
133
134PKCS12_SAFEBAG *PKCS12_add_cert(STACK_OF(PKCS12_SAFEBAG) **pbags, X509 *cert)
135{
136 PKCS12_SAFEBAG *bag = NULL;
137 char *name;
138 int namelen = -1;
139 unsigned char *keyid;
140 int keyidlen = -1;
141
142 /* Add user certificate */
143 if ((bag = PKCS12_SAFEBAG_create_cert(cert)) == NULL)
144 goto err;
145
146 /*
147 * Use friendlyName and localKeyID in certificate. (if present)
148 */
149
150 name = (char *)X509_alias_get0(cert, &namelen);
151
152 if (name && !PKCS12_add_friendlyname(bag, name, namelen))
153 goto err;
154
155 keyid = X509_keyid_get0(cert, &keyidlen);
156
157 if (keyid && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
158 goto err;
159
160 if (!pkcs12_add_bag(pbags, bag))
161 goto err;
162
163 return bag;
164
165 err:
166 PKCS12_SAFEBAG_free(bag);
167 return NULL;
168
169}
170
171PKCS12_SAFEBAG *PKCS12_add_key(STACK_OF(PKCS12_SAFEBAG) **pbags,
172 EVP_PKEY *key, int key_usage, int iter,
173 int nid_key, const char *pass)
174{
175
176 PKCS12_SAFEBAG *bag = NULL;
177 PKCS8_PRIV_KEY_INFO *p8 = NULL;
178
179 /* Make a PKCS#8 structure */
180 if ((p8 = EVP_PKEY2PKCS8(key)) == NULL)
181 goto err;
182 if (key_usage && !PKCS8_add_keyusage(p8, key_usage))
183 goto err;
184 if (nid_key != -1) {
185 bag = PKCS12_SAFEBAG_create_pkcs8_encrypt(nid_key, pass, -1, NULL, 0,
186 iter, p8);
187 PKCS8_PRIV_KEY_INFO_free(p8);
188 } else
189 bag = PKCS12_SAFEBAG_create0_p8inf(p8);
190
191 if (!bag)
192 goto err;
193
194 if (!pkcs12_add_bag(pbags, bag))
195 goto err;
196
197 return bag;
198
199 err:
200 PKCS12_SAFEBAG_free(bag);
201 return NULL;
202
203}
204
205int PKCS12_add_safe(STACK_OF(PKCS7) **psafes, STACK_OF(PKCS12_SAFEBAG) *bags,
206 int nid_safe, int iter, const char *pass)
207{
208 PKCS7 *p7 = NULL;
209 int free_safes = 0;
210
211 if (*psafes == NULL) {
212 *psafes = sk_PKCS7_new_null();
213 if (*psafes == NULL)
214 return 0;
215 free_safes = 1;
216 }
217
218 if (nid_safe == 0)
219#ifdef OPENSSL_NO_RC2
220 nid_safe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
221#else
222 nid_safe = NID_pbe_WithSHA1And40BitRC2_CBC;
223#endif
224
225 if (nid_safe == -1)
226 p7 = PKCS12_pack_p7data(bags);
227 else
228 p7 = PKCS12_pack_p7encdata(nid_safe, pass, -1, NULL, 0, iter, bags);
229 if (p7 == NULL)
230 goto err;
231
232 if (!sk_PKCS7_push(*psafes, p7))
233 goto err;
234
235 return 1;
236
237 err:
238 if (free_safes) {
239 sk_PKCS7_free(*psafes);
240 *psafes = NULL;
241 }
242 PKCS7_free(p7);
243 return 0;
244
245}
246
247static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags,
248 PKCS12_SAFEBAG *bag)
249{
250 int free_bags = 0;
251
252 if (pbags == NULL)
253 return 1;
254 if (*pbags == NULL) {
255 *pbags = sk_PKCS12_SAFEBAG_new_null();
256 if (*pbags == NULL)
257 return 0;
258 free_bags = 1;
259 }
260
261 if (!sk_PKCS12_SAFEBAG_push(*pbags, bag)) {
262 if (free_bags) {
263 sk_PKCS12_SAFEBAG_free(*pbags);
264 *pbags = NULL;
265 }
266 return 0;
267 }
268
269 return 1;
270
271}
272
273PKCS12 *PKCS12_add_safes(STACK_OF(PKCS7) *safes, int nid_p7)
274{
275 PKCS12 *p12;
276
277 if (nid_p7 <= 0)
278 nid_p7 = NID_pkcs7_data;
279 p12 = PKCS12_init(nid_p7);
280 if (p12 == NULL)
281 return NULL;
282
283 if (!PKCS12_pack_authsafes(p12, safes)) {
284 PKCS12_free(p12);
285 return NULL;
286 }
287
288 return p12;
289
290}
291