1 /*
2 * Copyright 1999-2024 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
15 static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags,
16 PKCS12_SAFEBAG *bag);
17 static int pkcs12_remove_bag(STACK_OF(PKCS12_SAFEBAG) **pbags,
18 PKCS12_SAFEBAG *bag);
19 static PKCS12_SAFEBAG *pkcs12_add_cert_bag(STACK_OF(PKCS12_SAFEBAG) **pbags,
20 X509 *cert,
21 const char *name,
22 int namelen,
23 unsigned char *keyid,
24 int keyidlen);
25
copy_bag_attr(PKCS12_SAFEBAG * bag,EVP_PKEY * pkey,int nid)26 static int copy_bag_attr(PKCS12_SAFEBAG *bag, EVP_PKEY *pkey, int nid)
27 {
28 int idx = EVP_PKEY_get_attr_by_NID(pkey, nid, -1);
29
30 if (idx < 0)
31 return 1;
32 return X509at_add1_attr(&bag->attrib, EVP_PKEY_get_attr(pkey, idx)) != NULL;
33 }
34
PKCS12_create_ex2(const char * pass,const char * name,EVP_PKEY * pkey,X509 * cert,STACK_OF (X509)* ca,int nid_key,int nid_cert,int iter,int mac_iter,int keytype,OSSL_LIB_CTX * ctx,const char * propq,PKCS12_create_cb * cb,void * cbarg)35 PKCS12 *PKCS12_create_ex2(const char *pass, const char *name, EVP_PKEY *pkey,
36 X509 *cert, STACK_OF(X509) *ca, int nid_key, int nid_cert,
37 int iter, int mac_iter, int keytype,
38 OSSL_LIB_CTX *ctx, const char *propq,
39 PKCS12_create_cb *cb, void *cbarg)
40 {
41 PKCS12 *p12 = NULL;
42 STACK_OF(PKCS7) *safes = NULL;
43 STACK_OF(PKCS12_SAFEBAG) *bags = NULL;
44 PKCS12_SAFEBAG *bag = NULL;
45 int i, cbret;
46 unsigned char keyid[EVP_MAX_MD_SIZE];
47 unsigned int keyidlen = 0;
48 int namelen = -1;
49 unsigned char *pkeyid = NULL;
50 int pkeyidlen = -1;
51
52 /* Set defaults */
53 if (nid_cert == NID_undef)
54 nid_cert = NID_aes_256_cbc;
55 if (nid_key == NID_undef)
56 nid_key = NID_aes_256_cbc;
57 if (!iter)
58 iter = PKCS12_DEFAULT_ITER;
59 if (!mac_iter)
60 mac_iter = PKCS12_DEFAULT_ITER;
61
62 if (pkey == NULL && cert == NULL && ca == NULL) {
63 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_INVALID_NULL_ARGUMENT);
64 return NULL;
65 }
66
67 if (pkey && cert) {
68 if (!X509_check_private_key(cert, pkey))
69 return NULL;
70 if (!X509_digest(cert, EVP_sha1(), keyid, &keyidlen))
71 return NULL;
72 }
73
74 if (cert) {
75 if (name == NULL)
76 name = (char *)X509_alias_get0(cert, &namelen);
77 if (keyidlen > 0) {
78 pkeyid = keyid;
79 pkeyidlen = keyidlen;
80 } else {
81 pkeyid = X509_keyid_get0(cert, &pkeyidlen);
82 }
83
84 bag = pkcs12_add_cert_bag(&bags, cert, name, namelen, pkeyid, pkeyidlen);
85 if (cb != NULL) {
86 cbret = cb(bag, cbarg);
87 if (cbret == -1) {
88 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CALLBACK_FAILED);
89 goto err;
90 } else if (cbret == 0) {
91 pkcs12_remove_bag(&bags, bag);
92 }
93 }
94 }
95
96 /* Add all other certificates */
97 for (i = 0; i < sk_X509_num(ca); i++) {
98 if ((bag = PKCS12_add_cert(&bags, sk_X509_value(ca, i))) == NULL)
99 goto err;
100 if (cb != NULL) {
101 cbret = cb(bag, cbarg);
102 if (cbret == -1) {
103 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CALLBACK_FAILED);
104 goto err;
105 } else if (cbret == 0) {
106 pkcs12_remove_bag(&bags, bag);
107 }
108 }
109 }
110
111 if (bags && !PKCS12_add_safe_ex(&safes, bags, nid_cert, iter, pass,
112 ctx, propq))
113 goto err;
114
115 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
116 bags = NULL;
117
118 if (pkey) {
119 bag = PKCS12_add_key_ex(&bags, pkey, keytype, iter, nid_key, pass,
120 ctx, propq);
121
122 if (!bag)
123 goto err;
124
125 if (!copy_bag_attr(bag, pkey, NID_ms_csp_name))
126 goto err;
127 if (!copy_bag_attr(bag, pkey, NID_LocalKeySet))
128 goto err;
129
130 if (name && !PKCS12_add_friendlyname(bag, name, -1))
131 goto err;
132 if (keyidlen && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
133 goto err;
134 if (cb != NULL) {
135 cbret = cb(bag, cbarg);
136 if (cbret == -1) {
137 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CALLBACK_FAILED);
138 goto err;
139 } else if (cbret == 0) {
140 pkcs12_remove_bag(&bags, bag);
141 }
142 }
143 }
144
145 if (bags && !PKCS12_add_safe(&safes, bags, -1, 0, NULL))
146 goto err;
147
148 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
149 bags = NULL;
150
151 p12 = PKCS12_add_safes_ex(safes, 0, ctx, propq);
152
153 if (p12 == NULL)
154 goto err;
155
156 sk_PKCS7_pop_free(safes, PKCS7_free);
157
158 safes = NULL;
159
160 if ((mac_iter != -1) &&
161 !PKCS12_set_mac(p12, pass, -1, NULL, 0, mac_iter, NULL))
162 goto err;
163
164 return p12;
165
166 err:
167 PKCS12_free(p12);
168 sk_PKCS7_pop_free(safes, PKCS7_free);
169 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
170 return NULL;
171
172 }
173
PKCS12_create_ex(const char * pass,const char * name,EVP_PKEY * pkey,X509 * cert,STACK_OF (X509)* ca,int nid_key,int nid_cert,int iter,int mac_iter,int keytype,OSSL_LIB_CTX * ctx,const char * propq)174 PKCS12 *PKCS12_create_ex(const char *pass, const char *name, EVP_PKEY *pkey, X509 *cert,
175 STACK_OF(X509) *ca, int nid_key, int nid_cert, int iter,
176 int mac_iter, int keytype,
177 OSSL_LIB_CTX *ctx, const char *propq)
178 {
179 return PKCS12_create_ex2(pass, name, pkey, cert, ca, nid_key, nid_cert,
180 iter, mac_iter, keytype, ctx, propq,
181 NULL, NULL);
182 }
183
PKCS12_create(const char * pass,const char * name,EVP_PKEY * pkey,X509 * cert,STACK_OF (X509)* ca,int nid_key,int nid_cert,int iter,int mac_iter,int keytype)184 PKCS12 *PKCS12_create(const char *pass, const char *name, EVP_PKEY *pkey, X509 *cert,
185 STACK_OF(X509) *ca, int nid_key, int nid_cert, int iter,
186 int mac_iter, int keytype)
187 {
188 return PKCS12_create_ex(pass, name, pkey, cert, ca, nid_key, nid_cert,
189 iter, mac_iter, keytype, NULL, NULL);
190 }
191
pkcs12_add_cert_bag(STACK_OF (PKCS12_SAFEBAG)** pbags,X509 * cert,const char * name,int namelen,unsigned char * keyid,int keyidlen)192 static PKCS12_SAFEBAG *pkcs12_add_cert_bag(STACK_OF(PKCS12_SAFEBAG) **pbags,
193 X509 *cert,
194 const char *name,
195 int namelen,
196 unsigned char *keyid,
197 int keyidlen)
198 {
199 PKCS12_SAFEBAG *bag = NULL;
200
201 /* Add user certificate */
202 if ((bag = PKCS12_SAFEBAG_create_cert(cert)) == NULL)
203 goto err;
204
205 if (name != NULL && !PKCS12_add_friendlyname(bag, name, namelen))
206 goto err;
207
208 if (keyid != NULL && !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
216 err:
217 PKCS12_SAFEBAG_free(bag);
218 return NULL;
219 }
220
PKCS12_add_cert(STACK_OF (PKCS12_SAFEBAG)** pbags,X509 * cert)221 PKCS12_SAFEBAG *PKCS12_add_cert(STACK_OF(PKCS12_SAFEBAG) **pbags, X509 *cert)
222 {
223 char *name = NULL;
224 int namelen = -1;
225 unsigned char *keyid = NULL;
226 int keyidlen = -1;
227
228 /*
229 * Use friendlyName and localKeyID in certificate. (if present)
230 */
231 name = (char *)X509_alias_get0(cert, &namelen);
232 keyid = X509_keyid_get0(cert, &keyidlen);
233
234 return pkcs12_add_cert_bag(pbags, cert, name, namelen, keyid, keyidlen);
235 }
236
PKCS12_add_key_ex(STACK_OF (PKCS12_SAFEBAG)** pbags,EVP_PKEY * key,int key_usage,int iter,int nid_key,const char * pass,OSSL_LIB_CTX * ctx,const char * propq)237 PKCS12_SAFEBAG *PKCS12_add_key_ex(STACK_OF(PKCS12_SAFEBAG) **pbags,
238 EVP_PKEY *key, int key_usage, int iter,
239 int nid_key, const char *pass,
240 OSSL_LIB_CTX *ctx, const char *propq)
241 {
242
243 PKCS12_SAFEBAG *bag = NULL;
244 PKCS8_PRIV_KEY_INFO *p8 = NULL;
245
246 /* Make a PKCS#8 structure */
247 if ((p8 = EVP_PKEY2PKCS8(key)) == NULL)
248 goto err;
249 if (key_usage && !PKCS8_add_keyusage(p8, key_usage))
250 goto err;
251 if (nid_key != -1) {
252 /* This call does not take ownership of p8 */
253 bag = PKCS12_SAFEBAG_create_pkcs8_encrypt_ex(nid_key, pass, -1, NULL, 0,
254 iter, p8, ctx, propq);
255 } else {
256 bag = PKCS12_SAFEBAG_create0_p8inf(p8);
257 if (bag != NULL)
258 p8 = NULL; /* bag takes ownership of p8 */
259 }
260 /* This does not need to be in the error path */
261 if (p8 != NULL)
262 PKCS8_PRIV_KEY_INFO_free(p8);
263
264 if (bag == NULL || !pkcs12_add_bag(pbags, bag))
265 goto err;
266
267 return bag;
268
269 err:
270 PKCS12_SAFEBAG_free(bag);
271 return NULL;
272
273 }
274
PKCS12_add_key(STACK_OF (PKCS12_SAFEBAG)** pbags,EVP_PKEY * key,int key_usage,int iter,int nid_key,const char * pass)275 PKCS12_SAFEBAG *PKCS12_add_key(STACK_OF(PKCS12_SAFEBAG) **pbags,
276 EVP_PKEY *key, int key_usage, int iter,
277 int nid_key, const char *pass)
278 {
279 return PKCS12_add_key_ex(pbags, key, key_usage, iter, nid_key, pass,
280 NULL, NULL);
281 }
282
PKCS12_add_secret(STACK_OF (PKCS12_SAFEBAG)** pbags,int nid_type,const unsigned char * value,int len)283 PKCS12_SAFEBAG *PKCS12_add_secret(STACK_OF(PKCS12_SAFEBAG) **pbags,
284 int nid_type, const unsigned char *value, int len)
285 {
286 PKCS12_SAFEBAG *bag = NULL;
287
288 /* Add secret, storing the value as an octet string */
289 if ((bag = PKCS12_SAFEBAG_create_secret(nid_type, V_ASN1_OCTET_STRING, value, len)) == NULL)
290 goto err;
291
292 if (!pkcs12_add_bag(pbags, bag))
293 goto err;
294
295 return bag;
296 err:
297 PKCS12_SAFEBAG_free(bag);
298 return NULL;
299 }
300
PKCS12_add_safe_ex(STACK_OF (PKCS7)** psafes,STACK_OF (PKCS12_SAFEBAG)* bags,int nid_safe,int iter,const char * pass,OSSL_LIB_CTX * ctx,const char * propq)301 int PKCS12_add_safe_ex(STACK_OF(PKCS7) **psafes, STACK_OF(PKCS12_SAFEBAG) *bags,
302 int nid_safe, int iter, const char *pass,
303 OSSL_LIB_CTX *ctx, const char *propq)
304 {
305 PKCS7 *p7 = NULL;
306 int free_safes = 0;
307
308 if (*psafes == NULL) {
309 *psafes = sk_PKCS7_new_null();
310 if (*psafes == NULL)
311 return 0;
312 free_safes = 1;
313 }
314
315 if (nid_safe == 0)
316 #ifdef OPENSSL_NO_RC2
317 nid_safe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
318 #else
319 nid_safe = NID_pbe_WithSHA1And40BitRC2_CBC;
320 #endif
321
322 if (nid_safe == -1)
323 p7 = PKCS12_pack_p7data(bags);
324 else
325 p7 = PKCS12_pack_p7encdata_ex(nid_safe, pass, -1, NULL, 0, iter, bags, ctx, propq);
326 if (p7 == NULL)
327 goto err;
328
329 if (!sk_PKCS7_push(*psafes, p7))
330 goto err;
331
332 return 1;
333
334 err:
335 if (free_safes) {
336 sk_PKCS7_free(*psafes);
337 *psafes = NULL;
338 }
339 PKCS7_free(p7);
340 return 0;
341 }
342
PKCS12_add_safe(STACK_OF (PKCS7)** psafes,STACK_OF (PKCS12_SAFEBAG)* bags,int nid_safe,int iter,const char * pass)343 int PKCS12_add_safe(STACK_OF(PKCS7) **psafes, STACK_OF(PKCS12_SAFEBAG) *bags,
344 int nid_safe, int iter, const char *pass)
345 {
346 return PKCS12_add_safe_ex(psafes, bags, nid_safe, iter, pass, NULL, NULL);
347 }
348
349
pkcs12_remove_bag(STACK_OF (PKCS12_SAFEBAG)** pbags,PKCS12_SAFEBAG * bag)350 static int pkcs12_remove_bag(STACK_OF(PKCS12_SAFEBAG) **pbags,
351 PKCS12_SAFEBAG *bag)
352 {
353 PKCS12_SAFEBAG *tmp;
354
355 if (pbags == NULL || bag == NULL)
356 return 1;
357
358 if ((tmp = sk_PKCS12_SAFEBAG_delete_ptr(*pbags, bag)) == NULL)
359 return 0;
360
361 PKCS12_SAFEBAG_free(tmp);
362 return 1;
363 }
364
pkcs12_add_bag(STACK_OF (PKCS12_SAFEBAG)** pbags,PKCS12_SAFEBAG * bag)365 static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags,
366 PKCS12_SAFEBAG *bag)
367 {
368 int free_bags = 0;
369
370 if (pbags == NULL)
371 return 1;
372 if (*pbags == NULL) {
373 *pbags = sk_PKCS12_SAFEBAG_new_null();
374 if (*pbags == NULL)
375 return 0;
376 free_bags = 1;
377 }
378
379 if (!sk_PKCS12_SAFEBAG_push(*pbags, bag)) {
380 if (free_bags) {
381 sk_PKCS12_SAFEBAG_free(*pbags);
382 *pbags = NULL;
383 }
384 return 0;
385 }
386
387 return 1;
388
389 }
390
PKCS12_add_safes_ex(STACK_OF (PKCS7)* safes,int nid_p7,OSSL_LIB_CTX * ctx,const char * propq)391 PKCS12 *PKCS12_add_safes_ex(STACK_OF(PKCS7) *safes, int nid_p7,
392 OSSL_LIB_CTX *ctx, const char *propq)
393 {
394 PKCS12 *p12;
395
396 if (nid_p7 <= 0)
397 nid_p7 = NID_pkcs7_data;
398 p12 = PKCS12_init_ex(nid_p7, ctx, propq);
399 if (p12 == NULL)
400 return NULL;
401
402 if (!PKCS12_pack_authsafes(p12, safes)) {
403 PKCS12_free(p12);
404 return NULL;
405 }
406
407 return p12;
408
409 }
410
PKCS12_add_safes(STACK_OF (PKCS7)* safes,int nid_p7)411 PKCS12 *PKCS12_add_safes(STACK_OF(PKCS7) *safes, int nid_p7)
412 {
413 return PKCS12_add_safes_ex(safes, nid_p7, NULL, NULL);
414 }
415