]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/pkcs12/p12_npas.c
Remove /* foo.c */ comments
[thirdparty/openssl.git] / crypto / pkcs12 / p12_npas.c
1 /*
2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
3 * 1999.
4 */
5 /* ====================================================================
6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <openssl/pem.h>
63 #include <openssl/err.h>
64 #include <openssl/pkcs12.h>
65
66 /* PKCS#12 password change routine */
67
68 static int newpass_p12(PKCS12 *p12, char *oldpass, char *newpass);
69 static int newpass_bags(STACK_OF(PKCS12_SAFEBAG) *bags, char *oldpass,
70 char *newpass);
71 static int newpass_bag(PKCS12_SAFEBAG *bag, char *oldpass, char *newpass);
72 static int alg_get(X509_ALGOR *alg, int *pnid, int *piter, int *psaltlen);
73
74 /*
75 * Change the password on a PKCS#12 structure.
76 */
77
78 int PKCS12_newpass(PKCS12 *p12, char *oldpass, char *newpass)
79 {
80 /* Check for NULL PKCS12 structure */
81
82 if (!p12) {
83 PKCS12err(PKCS12_F_PKCS12_NEWPASS,
84 PKCS12_R_INVALID_NULL_PKCS12_POINTER);
85 return 0;
86 }
87
88 /* Check the mac */
89
90 if (!PKCS12_verify_mac(p12, oldpass, -1)) {
91 PKCS12err(PKCS12_F_PKCS12_NEWPASS, PKCS12_R_MAC_VERIFY_FAILURE);
92 return 0;
93 }
94
95 if (!newpass_p12(p12, oldpass, newpass)) {
96 PKCS12err(PKCS12_F_PKCS12_NEWPASS, PKCS12_R_PARSE_ERROR);
97 return 0;
98 }
99
100 return 1;
101 }
102
103 /* Parse the outer PKCS#12 structure */
104
105 static int newpass_p12(PKCS12 *p12, char *oldpass, char *newpass)
106 {
107 STACK_OF(PKCS7) *asafes, *newsafes;
108 STACK_OF(PKCS12_SAFEBAG) *bags;
109 int i, bagnid, pbe_nid = 0, pbe_iter = 0, pbe_saltlen = 0;
110 PKCS7 *p7, *p7new;
111 ASN1_OCTET_STRING *p12_data_tmp = NULL, *macnew = NULL;
112 unsigned char mac[EVP_MAX_MD_SIZE];
113 unsigned int maclen;
114
115 if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
116 return 0;
117 if ((newsafes = sk_PKCS7_new_null()) == NULL)
118 return 0;
119 for (i = 0; i < sk_PKCS7_num(asafes); i++) {
120 p7 = sk_PKCS7_value(asafes, i);
121 bagnid = OBJ_obj2nid(p7->type);
122 if (bagnid == NID_pkcs7_data) {
123 bags = PKCS12_unpack_p7data(p7);
124 } else if (bagnid == NID_pkcs7_encrypted) {
125 bags = PKCS12_unpack_p7encdata(p7, oldpass, -1);
126 if (!alg_get(p7->d.encrypted->enc_data->algorithm,
127 &pbe_nid, &pbe_iter, &pbe_saltlen)) {
128 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
129 bags = NULL;
130 }
131 } else
132 continue;
133 if (!bags) {
134 sk_PKCS7_pop_free(asafes, PKCS7_free);
135 return 0;
136 }
137 if (!newpass_bags(bags, oldpass, newpass)) {
138 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
139 sk_PKCS7_pop_free(asafes, PKCS7_free);
140 return 0;
141 }
142 /* Repack bag in same form with new password */
143 if (bagnid == NID_pkcs7_data)
144 p7new = PKCS12_pack_p7data(bags);
145 else
146 p7new = PKCS12_pack_p7encdata(pbe_nid, newpass, -1, NULL,
147 pbe_saltlen, pbe_iter, bags);
148 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
149 if (!p7new) {
150 sk_PKCS7_pop_free(asafes, PKCS7_free);
151 return 0;
152 }
153 sk_PKCS7_push(newsafes, p7new);
154 }
155 sk_PKCS7_pop_free(asafes, PKCS7_free);
156
157 /* Repack safe: save old safe in case of error */
158
159 p12_data_tmp = p12->authsafes->d.data;
160 if ((p12->authsafes->d.data = ASN1_OCTET_STRING_new()) == NULL)
161 goto saferr;
162 if (!PKCS12_pack_authsafes(p12, newsafes))
163 goto saferr;
164
165 if (!PKCS12_gen_mac(p12, newpass, -1, mac, &maclen))
166 goto saferr;
167 if ((macnew = ASN1_OCTET_STRING_new()) == NULL)
168 goto saferr;
169 if (!ASN1_OCTET_STRING_set(macnew, mac, maclen))
170 goto saferr;
171 ASN1_OCTET_STRING_free(p12->mac->dinfo->digest);
172 p12->mac->dinfo->digest = macnew;
173 ASN1_OCTET_STRING_free(p12_data_tmp);
174
175 return 1;
176
177 saferr:
178 /* Restore old safe */
179 ASN1_OCTET_STRING_free(p12->authsafes->d.data);
180 ASN1_OCTET_STRING_free(macnew);
181 p12->authsafes->d.data = p12_data_tmp;
182 return 0;
183
184 }
185
186 static int newpass_bags(STACK_OF(PKCS12_SAFEBAG) *bags, char *oldpass,
187 char *newpass)
188 {
189 int i;
190 for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
191 if (!newpass_bag(sk_PKCS12_SAFEBAG_value(bags, i), oldpass, newpass))
192 return 0;
193 }
194 return 1;
195 }
196
197 /* Change password of safebag: only needs handle shrouded keybags */
198
199 static int newpass_bag(PKCS12_SAFEBAG *bag, char *oldpass, char *newpass)
200 {
201 PKCS8_PRIV_KEY_INFO *p8;
202 X509_SIG *p8new;
203 int p8_nid, p8_saltlen, p8_iter;
204
205 if (M_PKCS12_bag_type(bag) != NID_pkcs8ShroudedKeyBag)
206 return 1;
207
208 if ((p8 = PKCS8_decrypt(bag->value.shkeybag, oldpass, -1)) == NULL)
209 return 0;
210 if (!alg_get(bag->value.shkeybag->algor, &p8_nid, &p8_iter, &p8_saltlen))
211 return 0;
212 if ((p8new = PKCS8_encrypt(p8_nid, NULL, newpass, -1, NULL, p8_saltlen,
213 p8_iter, p8)) == NULL)
214 return 0;
215 X509_SIG_free(bag->value.shkeybag);
216 bag->value.shkeybag = p8new;
217 return 1;
218 }
219
220 static int alg_get(X509_ALGOR *alg, int *pnid, int *piter, int *psaltlen)
221 {
222 PBEPARAM *pbe;
223 pbe = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBEPARAM), alg->parameter);
224 if (!pbe)
225 return 0;
226 *pnid = OBJ_obj2nid(alg->algorithm);
227 *piter = ASN1_INTEGER_get(pbe->iter);
228 *psaltlen = pbe->salt->length;
229 PBEPARAM_free(pbe);
230 return 1;
231 }