3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
63 #include <openssl/pem.h>
64 #include <openssl/err.h>
65 #include <openssl/pkcs12.h>
67 /* PKCS#12 password change routine */
69 static int newpass_p12(PKCS12 *p12, const char *oldpass, const char *newpass);
70 static int newpass_bags(STACK_OF(PKCS12_SAFEBAG) *bags, const char *oldpass,
72 static int newpass_bag(PKCS12_SAFEBAG *bag, const char *oldpass,
74 static int alg_get(X509_ALGOR *alg, int *pnid, int *piter, int *psaltlen);
77 * Change the password on a PKCS#12 structure.
80 int PKCS12_newpass(PKCS12 *p12, const char *oldpass, const char *newpass)
82 /* Check for NULL PKCS12 structure */
85 PKCS12err(PKCS12_F_PKCS12_NEWPASS,
86 PKCS12_R_INVALID_NULL_PKCS12_POINTER);
92 if (!PKCS12_verify_mac(p12, oldpass, -1)) {
93 PKCS12err(PKCS12_F_PKCS12_NEWPASS, PKCS12_R_MAC_VERIFY_FAILURE);
97 if (!newpass_p12(p12, oldpass, newpass)) {
98 PKCS12err(PKCS12_F_PKCS12_NEWPASS, PKCS12_R_PARSE_ERROR);
105 /* Parse the outer PKCS#12 structure */
107 static int newpass_p12(PKCS12 *p12, const char *oldpass, const char *newpass)
109 STACK_OF(PKCS7) *asafes = NULL, *newsafes = NULL;
110 STACK_OF(PKCS12_SAFEBAG) *bags = NULL;
111 int i, bagnid, pbe_nid = 0, pbe_iter = 0, pbe_saltlen = 0;
113 ASN1_OCTET_STRING *p12_data_tmp = NULL;
114 unsigned char mac[EVP_MAX_MD_SIZE];
118 if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
120 if ((newsafes = sk_PKCS7_new_null()) == NULL)
122 for (i = 0; i < sk_PKCS7_num(asafes); i++) {
123 p7 = sk_PKCS7_value(asafes, i);
124 bagnid = OBJ_obj2nid(p7->type);
125 if (bagnid == NID_pkcs7_data) {
126 bags = PKCS12_unpack_p7data(p7);
127 } else if (bagnid == NID_pkcs7_encrypted) {
128 bags = PKCS12_unpack_p7encdata(p7, oldpass, -1);
129 if (!alg_get(p7->d.encrypted->enc_data->algorithm,
130 &pbe_nid, &pbe_iter, &pbe_saltlen))
137 if (!newpass_bags(bags, oldpass, newpass))
139 /* Repack bag in same form with new password */
140 if (bagnid == NID_pkcs7_data)
141 p7new = PKCS12_pack_p7data(bags);
143 p7new = PKCS12_pack_p7encdata(pbe_nid, newpass, -1, NULL,
144 pbe_saltlen, pbe_iter, bags);
145 if (!p7new || !sk_PKCS7_push(newsafes, p7new))
147 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
151 /* Repack safe: save old safe in case of error */
153 p12_data_tmp = p12->authsafes->d.data;
154 if ((p12->authsafes->d.data = ASN1_OCTET_STRING_new()) == NULL)
156 if (!PKCS12_pack_authsafes(p12, newsafes))
158 if (!PKCS12_gen_mac(p12, newpass, -1, mac, &maclen))
160 if (!ASN1_OCTET_STRING_set(p12->mac->dinfo->digest, mac, maclen))
166 /* Restore old safe if necessary */
168 ASN1_OCTET_STRING_free(p12_data_tmp);
169 } else if (p12_data_tmp != NULL) {
170 ASN1_OCTET_STRING_free(p12->authsafes->d.data);
171 p12->authsafes->d.data = p12_data_tmp;
173 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
174 sk_PKCS7_pop_free(asafes, PKCS7_free);
175 sk_PKCS7_pop_free(newsafes, PKCS7_free);
179 static int newpass_bags(STACK_OF(PKCS12_SAFEBAG) *bags, const char *oldpass,
183 for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
184 if (!newpass_bag(sk_PKCS12_SAFEBAG_value(bags, i), oldpass, newpass))
190 /* Change password of safebag: only needs handle shrouded keybags */
192 static int newpass_bag(PKCS12_SAFEBAG *bag, const char *oldpass,
195 PKCS8_PRIV_KEY_INFO *p8;
197 int p8_nid, p8_saltlen, p8_iter;
199 if (M_PKCS12_bag_type(bag) != NID_pkcs8ShroudedKeyBag)
202 if (!(p8 = PKCS8_decrypt(bag->value.shkeybag, oldpass, -1)))
204 if (!alg_get(bag->value.shkeybag->algor, &p8_nid, &p8_iter, &p8_saltlen))
206 p8new = PKCS8_encrypt(p8_nid, NULL, newpass, -1, NULL, p8_saltlen,
208 PKCS8_PRIV_KEY_INFO_free(p8);
211 X509_SIG_free(bag->value.shkeybag);
212 bag->value.shkeybag = p8new;
216 static int alg_get(X509_ALGOR *alg, int *pnid, int *piter, int *psaltlen)
219 const unsigned char *p;
221 p = alg->parameter->value.sequence->data;
222 pbe = d2i_PBEPARAM(NULL, &p, alg->parameter->value.sequence->length);
225 *pnid = OBJ_obj2nid(alg->algorithm);
226 *piter = ASN1_INTEGER_get(pbe->iter);
227 *psaltlen = pbe->salt->length;