2 * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "internal/cryptlib.h"
12 #include <openssl/pkcs12.h>
13 #include "p12_local.h"
15 static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags,
18 static int copy_bag_attr(PKCS12_SAFEBAG *bag, EVP_PKEY *pkey, int nid)
20 int idx = EVP_PKEY_get_attr_by_NID(pkey, nid, -1);
24 return X509at_add1_attr(&bag->attrib, EVP_PKEY_get_attr(pkey, idx)) != NULL;
27 PKCS12 *PKCS12_create_ex(const char *pass, const char *name, EVP_PKEY *pkey,
28 X509 *cert, STACK_OF(X509) *ca, int nid_key, int nid_cert,
29 int iter, int mac_iter, int keytype,
30 OSSL_LIB_CTX *ctx, const char *propq)
33 STACK_OF(PKCS7) *safes = NULL;
34 STACK_OF(PKCS12_SAFEBAG) *bags = NULL;
35 PKCS12_SAFEBAG *bag = NULL;
37 unsigned char keyid[EVP_MAX_MD_SIZE];
38 unsigned int keyidlen = 0;
41 if (nid_cert == NID_undef)
42 nid_cert = NID_aes_256_cbc;
43 if (nid_key == NID_undef)
44 nid_key = NID_aes_256_cbc;
46 iter = PKCS12_DEFAULT_ITER;
48 mac_iter = PKCS12_DEFAULT_ITER;
50 if (pkey == NULL && cert == NULL && ca == NULL) {
51 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_INVALID_NULL_ARGUMENT);
56 if (!X509_check_private_key(cert, pkey))
58 if (!X509_digest(cert, EVP_sha1(), keyid, &keyidlen))
63 bag = PKCS12_add_cert(&bags, cert);
64 if (name && !PKCS12_add_friendlyname(bag, name, -1))
66 if (keyidlen && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
70 /* Add all other certificates */
71 for (i = 0; i < sk_X509_num(ca); i++) {
72 if (!PKCS12_add_cert(&bags, sk_X509_value(ca, i)))
76 if (bags && !PKCS12_add_safe_ex(&safes, bags, nid_cert, iter, pass,
80 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
84 bag = PKCS12_add_key_ex(&bags, pkey, keytype, iter, nid_key, pass,
90 if (!copy_bag_attr(bag, pkey, NID_ms_csp_name))
92 if (!copy_bag_attr(bag, pkey, NID_LocalKeySet))
95 if (name && !PKCS12_add_friendlyname(bag, name, -1))
97 if (keyidlen && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
101 if (bags && !PKCS12_add_safe(&safes, bags, -1, 0, NULL))
104 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
107 p12 = PKCS12_add_safes_ex(safes, 0, ctx, propq);
112 sk_PKCS7_pop_free(safes, PKCS7_free);
116 if ((mac_iter != -1) &&
117 !PKCS12_set_mac(p12, pass, -1, NULL, 0, mac_iter, NULL))
124 sk_PKCS7_pop_free(safes, PKCS7_free);
125 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
130 PKCS12 *PKCS12_create(const char *pass, const char *name, EVP_PKEY *pkey, X509 *cert,
131 STACK_OF(X509) *ca, int nid_key, int nid_cert, int iter,
132 int mac_iter, int keytype)
134 return PKCS12_create_ex(pass, name, pkey, cert, ca, nid_key, nid_cert,
135 iter, mac_iter, keytype, NULL, NULL);
138 PKCS12_SAFEBAG *PKCS12_add_cert(STACK_OF(PKCS12_SAFEBAG) **pbags, X509 *cert)
140 PKCS12_SAFEBAG *bag = NULL;
143 unsigned char *keyid;
146 /* Add user certificate */
147 if ((bag = PKCS12_SAFEBAG_create_cert(cert)) == NULL)
151 * Use friendlyName and localKeyID in certificate. (if present)
154 name = (char *)X509_alias_get0(cert, &namelen);
156 if (name && !PKCS12_add_friendlyname(bag, name, namelen))
159 keyid = X509_keyid_get0(cert, &keyidlen);
161 if (keyid && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
164 if (!pkcs12_add_bag(pbags, bag))
170 PKCS12_SAFEBAG_free(bag);
175 PKCS12_SAFEBAG *PKCS12_add_key_ex(STACK_OF(PKCS12_SAFEBAG) **pbags,
176 EVP_PKEY *key, int key_usage, int iter,
177 int nid_key, const char *pass,
178 OSSL_LIB_CTX *ctx, const char *propq)
181 PKCS12_SAFEBAG *bag = NULL;
182 PKCS8_PRIV_KEY_INFO *p8 = NULL;
184 /* Make a PKCS#8 structure */
185 if ((p8 = EVP_PKEY2PKCS8(key)) == NULL)
187 if (key_usage && !PKCS8_add_keyusage(p8, key_usage))
190 bag = PKCS12_SAFEBAG_create_pkcs8_encrypt_ex(nid_key, pass, -1, NULL, 0,
191 iter, p8, ctx, propq);
192 PKCS8_PRIV_KEY_INFO_free(p8);
194 bag = PKCS12_SAFEBAG_create0_p8inf(p8);
199 if (!pkcs12_add_bag(pbags, bag))
205 PKCS12_SAFEBAG_free(bag);
210 PKCS12_SAFEBAG *PKCS12_add_key(STACK_OF(PKCS12_SAFEBAG) **pbags,
211 EVP_PKEY *key, int key_usage, int iter,
212 int nid_key, const char *pass)
214 return PKCS12_add_key_ex(pbags, key, key_usage, iter, nid_key, pass,
218 PKCS12_SAFEBAG *PKCS12_add_secret(STACK_OF(PKCS12_SAFEBAG) **pbags,
219 int nid_type, const unsigned char *value, int len)
221 PKCS12_SAFEBAG *bag = NULL;
223 /* Add secret, storing the value as an octet string */
224 if ((bag = PKCS12_SAFEBAG_create_secret(nid_type, V_ASN1_OCTET_STRING, value, len)) == NULL)
227 if (!pkcs12_add_bag(pbags, bag))
232 PKCS12_SAFEBAG_free(bag);
236 int PKCS12_add_safe_ex(STACK_OF(PKCS7) **psafes, STACK_OF(PKCS12_SAFEBAG) *bags,
237 int nid_safe, int iter, const char *pass,
238 OSSL_LIB_CTX *ctx, const char *propq)
243 if (*psafes == NULL) {
244 *psafes = sk_PKCS7_new_null();
251 #ifdef OPENSSL_NO_RC2
252 nid_safe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
254 nid_safe = NID_pbe_WithSHA1And40BitRC2_CBC;
258 p7 = PKCS12_pack_p7data(bags);
260 p7 = PKCS12_pack_p7encdata_ex(nid_safe, pass, -1, NULL, 0, iter, bags, ctx, propq);
264 if (!sk_PKCS7_push(*psafes, p7))
271 sk_PKCS7_free(*psafes);
278 int PKCS12_add_safe(STACK_OF(PKCS7) **psafes, STACK_OF(PKCS12_SAFEBAG) *bags,
279 int nid_safe, int iter, const char *pass)
281 return PKCS12_add_safe_ex(psafes, bags, nid_safe, iter, pass, NULL, NULL);
284 static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags,
291 if (*pbags == NULL) {
292 *pbags = sk_PKCS12_SAFEBAG_new_null();
298 if (!sk_PKCS12_SAFEBAG_push(*pbags, bag)) {
300 sk_PKCS12_SAFEBAG_free(*pbags);
310 PKCS12 *PKCS12_add_safes_ex(STACK_OF(PKCS7) *safes, int nid_p7,
311 OSSL_LIB_CTX *ctx, const char *propq)
316 nid_p7 = NID_pkcs7_data;
317 p12 = PKCS12_init_ex(nid_p7, ctx, propq);
321 if (!PKCS12_pack_authsafes(p12, safes)) {
330 PKCS12 *PKCS12_add_safes(STACK_OF(PKCS7) *safes, int nid_p7)
332 return PKCS12_add_safes_ex(safes, nid_p7, NULL, NULL);