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)
22 idx = EVP_PKEY_get_attr_by_NID(pkey, nid, -1);
25 attr = EVP_PKEY_get_attr(pkey, idx);
26 if (!X509at_add1_attr(&bag->attrib, attr))
31 PKCS12 *PKCS12_create_ex(const char *pass, const char *name, EVP_PKEY *pkey,
32 X509 *cert, STACK_OF(X509) *ca, int nid_key, int nid_cert,
33 int iter, int mac_iter, int keytype,
34 OSSL_LIB_CTX *ctx, const char *propq)
37 STACK_OF(PKCS7) *safes = NULL;
38 STACK_OF(PKCS12_SAFEBAG) *bags = NULL;
39 PKCS12_SAFEBAG *bag = NULL;
41 unsigned char keyid[EVP_MAX_MD_SIZE];
42 unsigned int keyidlen = 0;
45 if (nid_cert == NID_undef)
46 nid_cert = NID_aes_256_cbc;
47 if (nid_key == NID_undef)
48 nid_key = NID_aes_256_cbc;
50 iter = PKCS12_DEFAULT_ITER;
52 mac_iter = PKCS12_DEFAULT_ITER;
54 if (pkey == NULL && cert == NULL && ca == NULL) {
55 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_INVALID_NULL_ARGUMENT);
60 if (!X509_check_private_key(cert, pkey))
62 if (!X509_digest(cert, EVP_sha1(), keyid, &keyidlen))
67 bag = PKCS12_add_cert(&bags, cert);
68 if (name && !PKCS12_add_friendlyname(bag, name, -1))
70 if (keyidlen && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
74 /* Add all other certificates */
75 for (i = 0; i < sk_X509_num(ca); i++) {
76 if (!PKCS12_add_cert(&bags, sk_X509_value(ca, i)))
80 if (bags && !PKCS12_add_safe_ex(&safes, bags, nid_cert, iter, pass,
84 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
88 bag = PKCS12_add_key_ex(&bags, pkey, keytype, iter, nid_key, pass,
94 if (!copy_bag_attr(bag, pkey, NID_ms_csp_name))
96 if (!copy_bag_attr(bag, pkey, NID_LocalKeySet))
99 if (name && !PKCS12_add_friendlyname(bag, name, -1))
101 if (keyidlen && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
105 if (bags && !PKCS12_add_safe(&safes, bags, -1, 0, NULL))
108 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
111 p12 = PKCS12_add_safes_ex(safes, 0, ctx, propq);
116 sk_PKCS7_pop_free(safes, PKCS7_free);
120 if ((mac_iter != -1) &&
121 !PKCS12_set_mac(p12, pass, -1, NULL, 0, mac_iter, NULL))
128 sk_PKCS7_pop_free(safes, PKCS7_free);
129 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
134 PKCS12 *PKCS12_create(const char *pass, const char *name, EVP_PKEY *pkey, X509 *cert,
135 STACK_OF(X509) *ca, int nid_key, int nid_cert, int iter,
136 int mac_iter, int keytype)
138 return PKCS12_create_ex(pass, name, pkey, cert, ca, nid_key, nid_cert,
139 iter, mac_iter, keytype, NULL, NULL);
142 PKCS12_SAFEBAG *PKCS12_add_cert(STACK_OF(PKCS12_SAFEBAG) **pbags, X509 *cert)
144 PKCS12_SAFEBAG *bag = NULL;
147 unsigned char *keyid;
150 /* Add user certificate */
151 if ((bag = PKCS12_SAFEBAG_create_cert(cert)) == NULL)
155 * Use friendlyName and localKeyID in certificate. (if present)
158 name = (char *)X509_alias_get0(cert, &namelen);
160 if (name && !PKCS12_add_friendlyname(bag, name, namelen))
163 keyid = X509_keyid_get0(cert, &keyidlen);
165 if (keyid && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
168 if (!pkcs12_add_bag(pbags, bag))
174 PKCS12_SAFEBAG_free(bag);
179 PKCS12_SAFEBAG *PKCS12_add_key_ex(STACK_OF(PKCS12_SAFEBAG) **pbags,
180 EVP_PKEY *key, int key_usage, int iter,
181 int nid_key, const char *pass,
182 OSSL_LIB_CTX *ctx, const char *propq)
185 PKCS12_SAFEBAG *bag = NULL;
186 PKCS8_PRIV_KEY_INFO *p8 = NULL;
188 /* Make a PKCS#8 structure */
189 if ((p8 = EVP_PKEY2PKCS8(key)) == NULL)
191 if (key_usage && !PKCS8_add_keyusage(p8, key_usage))
194 bag = PKCS12_SAFEBAG_create_pkcs8_encrypt_ex(nid_key, pass, -1, NULL, 0,
195 iter, p8, ctx, propq);
196 PKCS8_PRIV_KEY_INFO_free(p8);
198 bag = PKCS12_SAFEBAG_create0_p8inf(p8);
203 if (!pkcs12_add_bag(pbags, bag))
209 PKCS12_SAFEBAG_free(bag);
214 PKCS12_SAFEBAG *PKCS12_add_key(STACK_OF(PKCS12_SAFEBAG) **pbags,
215 EVP_PKEY *key, int key_usage, int iter,
216 int nid_key, const char *pass)
218 return PKCS12_add_key_ex(pbags, key, key_usage, iter, nid_key, pass,
222 PKCS12_SAFEBAG *PKCS12_add_secret(STACK_OF(PKCS12_SAFEBAG) **pbags,
223 int nid_type, const unsigned char *value, int len)
225 PKCS12_SAFEBAG *bag = NULL;
227 /* Add secret, storing the value as an octet string */
228 if ((bag = PKCS12_SAFEBAG_create_secret(nid_type, V_ASN1_OCTET_STRING, value, len)) == NULL)
231 if (!pkcs12_add_bag(pbags, bag))
236 PKCS12_SAFEBAG_free(bag);
240 int PKCS12_add_safe_ex(STACK_OF(PKCS7) **psafes, STACK_OF(PKCS12_SAFEBAG) *bags,
241 int nid_safe, int iter, const char *pass,
242 OSSL_LIB_CTX *ctx, const char *propq)
247 if (*psafes == NULL) {
248 *psafes = sk_PKCS7_new_null();
255 #ifdef OPENSSL_NO_RC2
256 nid_safe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
258 nid_safe = NID_pbe_WithSHA1And40BitRC2_CBC;
262 p7 = PKCS12_pack_p7data(bags);
264 p7 = PKCS12_pack_p7encdata_ex(nid_safe, pass, -1, NULL, 0, iter, bags, ctx, propq);
268 if (!sk_PKCS7_push(*psafes, p7))
275 sk_PKCS7_free(*psafes);
282 int PKCS12_add_safe(STACK_OF(PKCS7) **psafes, STACK_OF(PKCS12_SAFEBAG) *bags,
283 int nid_safe, int iter, const char *pass)
285 return PKCS12_add_safe_ex(psafes, bags, nid_safe, iter, pass, NULL, NULL);
288 static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags,
295 if (*pbags == NULL) {
296 *pbags = sk_PKCS12_SAFEBAG_new_null();
302 if (!sk_PKCS12_SAFEBAG_push(*pbags, bag)) {
304 sk_PKCS12_SAFEBAG_free(*pbags);
314 PKCS12 *PKCS12_add_safes_ex(STACK_OF(PKCS7) *safes, int nid_p7,
315 OSSL_LIB_CTX *ctx, const char *propq)
320 nid_p7 = NID_pkcs7_data;
321 p12 = PKCS12_init_ex(nid_p7, ctx, propq);
325 if (!PKCS12_pack_authsafes(p12, safes)) {
334 PKCS12 *PKCS12_add_safes(STACK_OF(PKCS7) *safes, int nid_p7)
336 return PKCS12_add_safes_ex(safes, nid_p7, NULL, NULL);