Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/pkcs12/p12_npas.c

raw
1 /*
2 * Copyright 1999-2025 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 <stdlib.h>
12 #include <string.h>
13 #include <openssl/pem.h>
14 #include <openssl/err.h>
15 #include <openssl/pkcs12.h>
16 #include "p12_local.h"
17
18 /* PKCS#12 password change routine */
19
20 static int newpass_p12(PKCS12 *p12, const char *oldpass, const char *newpass);
21 static int newpass_bags(STACK_OF(PKCS12_SAFEBAG) *bags, const char *oldpass,
22 const char *newpass,
23 OSSL_LIB_CTX *libctx, const char *propq);
24 static int newpass_bag(PKCS12_SAFEBAG *bag, const char *oldpass,
25 const char *newpass,
26 OSSL_LIB_CTX *libctx, const char *propq);
27 static int alg_get(const X509_ALGOR *alg, int *pnid, int *piter,
28 int *psaltlen, int *cipherid);
29
30 /*
31 * Change the password on a PKCS#12 structure.
32 */
33
34 int PKCS12_newpass(PKCS12 *p12, const char *oldpass, const char *newpass)
35 {
36 /* Check for NULL PKCS12 structure */
37
38 if (p12 == NULL) {
39 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_INVALID_NULL_PKCS12_POINTER);
40 return 0;
41 }
42
43 /* Check the mac */
44 if (p12->mac != NULL) {
45 if (!PKCS12_verify_mac(p12, oldpass, -1)) {
46 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_VERIFY_FAILURE);
47 return 0;
48 }
49 }
50 if (!newpass_p12(p12, oldpass, newpass)) {
51 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_PARSE_ERROR);
52 return 0;
53 }
54
55 return 1;
56 }
57
58 /* Parse the outer PKCS#12 structure */
59
60 static int newpass_p12(PKCS12 *p12, const char *oldpass, const char *newpass)
61 {
62 STACK_OF(PKCS7) *asafes = NULL, *newsafes = NULL;
63 STACK_OF(PKCS12_SAFEBAG) *bags = NULL;
64 int i, bagnid, pbe_nid = 0, pbe_iter = 0, pbe_saltlen = 0, cipherid = NID_undef;
65 PKCS7 *p7;
66 PKCS7 *p7new = NULL;
67 ASN1_OCTET_STRING *p12_data_tmp = NULL, *macoct = NULL;
68 unsigned char mac[EVP_MAX_MD_SIZE];
69 unsigned int maclen;
70 int rv = 0;
71
72 if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
73 goto err;
74 if ((newsafes = sk_PKCS7_new_null()) == NULL)
75 goto err;
76 for (i = 0; i < sk_PKCS7_num(asafes); i++) {
77 p7 = sk_PKCS7_value(asafes, i);
78
79 bagnid = OBJ_obj2nid(p7->type);
80 if (bagnid == NID_pkcs7_data) {
81 bags = PKCS12_unpack_p7data(p7);
82 } else if (bagnid == NID_pkcs7_encrypted) {
83 bags = PKCS12_unpack_p7encdata(p7, oldpass, -1);
84 if (p7->d.encrypted == NULL
85 || !alg_get(p7->d.encrypted->enc_data->algorithm,
86 &pbe_nid, &pbe_iter, &pbe_saltlen, &cipherid))
87 goto err;
88 } else {
89 continue;
90 }
91 if (bags == NULL)
92 goto err;
93 if (!newpass_bags(bags, oldpass, newpass,
94 p7->ctx.libctx, p7->ctx.propq))
95 goto err;
96 /* Repack bag in same form with new password */
97 if (bagnid == NID_pkcs7_data)
98 p7new = PKCS12_pack_p7data(bags);
99 else
100 p7new = PKCS12_pack_p7encdata_ex(pbe_nid, newpass, -1, NULL,
101 pbe_saltlen, pbe_iter, bags,
102 p7->ctx.libctx, p7->ctx.propq);
103 if (p7new == NULL || !sk_PKCS7_push(newsafes, p7new)) {
104 PKCS7_free(p7new);
105 goto err;
106 }
107 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
108 bags = NULL;
109 }
110
111 /* Repack safe: save old safe in case of error */
112
113 p12_data_tmp = p12->authsafes->d.data;
114 if ((p12->authsafes->d.data = ASN1_OCTET_STRING_new()) == NULL)
115 goto err;
116 if (!PKCS12_pack_authsafes(p12, newsafes))
117 goto err;
118
119 if (p12->mac != NULL) {
120 if (!PKCS12_gen_mac(p12, newpass, -1, mac, &maclen))
121 goto err;
122 X509_SIG_getm(p12->mac->dinfo, NULL, &macoct);
123 if (!ASN1_OCTET_STRING_set(macoct, mac, maclen))
124 goto err;
125 }
126
127 rv = 1;
128
129 err:
130 /* Restore old safe if necessary */
131 if (rv == 1) {
132 ASN1_OCTET_STRING_free(p12_data_tmp);
133 } else if (p12_data_tmp != NULL) {
134 ASN1_OCTET_STRING_free(p12->authsafes->d.data);
135 p12->authsafes->d.data = p12_data_tmp;
136 }
137 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
138 sk_PKCS7_pop_free(asafes, PKCS7_free);
139 sk_PKCS7_pop_free(newsafes, PKCS7_free);
140 return rv;
141 }
142
143 static int newpass_bags(STACK_OF(PKCS12_SAFEBAG) *bags, const char *oldpass,
144 const char *newpass,
145 OSSL_LIB_CTX *libctx, const char *propq)
146 {
147 int i;
148 for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
149 if (!newpass_bag(sk_PKCS12_SAFEBAG_value(bags, i), oldpass, newpass,
150 libctx, propq))
151 return 0;
152 }
153 return 1;
154 }
155
156 /* Change password of safebag: only needs handle shrouded keybags */
157
158 static int newpass_bag(PKCS12_SAFEBAG *bag, const char *oldpass,
159 const char *newpass,
160 OSSL_LIB_CTX *libctx, const char *propq)
161 {
162 EVP_CIPHER *cipher = NULL;
163 PKCS8_PRIV_KEY_INFO *p8;
164 X509_SIG *p8new;
165 int p8_nid, p8_saltlen, p8_iter, cipherid = 0;
166 const X509_ALGOR *shalg;
167
168 if (PKCS12_SAFEBAG_get_nid(bag) != NID_pkcs8ShroudedKeyBag)
169 return 1;
170
171 if ((p8 = PKCS8_decrypt_ex(bag->value.shkeybag, oldpass, -1,
172 libctx, propq))
173 == NULL)
174 return 0;
175 X509_SIG_get0(bag->value.shkeybag, &shalg, NULL);
176 if (!alg_get(shalg, &p8_nid, &p8_iter, &p8_saltlen, &cipherid)) {
177 PKCS8_PRIV_KEY_INFO_free(p8);
178 return 0;
179 }
180 if (cipherid != NID_undef) {
181 cipher = EVP_CIPHER_fetch(libctx, OBJ_nid2sn(cipherid), propq);
182 if (cipher == NULL) {
183 PKCS8_PRIV_KEY_INFO_free(p8);
184 return 0;
185 }
186 }
187 p8new = PKCS8_encrypt_ex(p8_nid, cipher, newpass, -1, NULL, p8_saltlen,
188 p8_iter, p8, libctx, propq);
189 PKCS8_PRIV_KEY_INFO_free(p8);
190 EVP_CIPHER_free(cipher);
191 if (p8new == NULL)
192 return 0;
193 X509_SIG_free(bag->value.shkeybag);
194 bag->value.shkeybag = p8new;
195 return 1;
196 }
197
198 static int alg_get(const X509_ALGOR *alg, int *pnid, int *piter,
199 int *psaltlen, int *cipherid)
200 {
201 int ret = 0, pbenid, aparamtype;
202 int encnid, prfnid;
203 const ASN1_OBJECT *aoid;
204 const void *aparam;
205 PBEPARAM *pbe = NULL;
206 PBE2PARAM *pbe2 = NULL;
207 PBKDF2PARAM *kdf = NULL;
208
209 X509_ALGOR_get0(&aoid, &aparamtype, &aparam, alg);
210 pbenid = OBJ_obj2nid(aoid);
211
212 switch (pbenid) {
213 case NID_pbes2:
214 if (aparamtype == V_ASN1_SEQUENCE)
215 pbe2 = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBE2PARAM));
216 if (pbe2 == NULL)
217 goto done;
218
219 X509_ALGOR_get0(NULL, &aparamtype, &aparam, pbe2->keyfunc);
220 X509_ALGOR_get0(&aoid, NULL, NULL, pbe2->encryption);
221 encnid = OBJ_obj2nid(aoid);
222
223 if (aparamtype == V_ASN1_SEQUENCE)
224 kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBKDF2PARAM));
225 if (kdf == NULL)
226 goto done;
227
228 /* Only OCTET_STRING is supported */
229 if (kdf->salt->type != V_ASN1_OCTET_STRING)
230 goto done;
231
232 if (kdf->prf == NULL) {
233 prfnid = NID_hmacWithSHA1;
234 } else {
235 X509_ALGOR_get0(&aoid, NULL, NULL, kdf->prf);
236 prfnid = OBJ_obj2nid(aoid);
237 }
238 *psaltlen = kdf->salt->value.octet_string->length;
239 *piter = ASN1_INTEGER_get(kdf->iter);
240 *pnid = prfnid;
241 *cipherid = encnid;
242 break;
243 default:
244 pbe = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBEPARAM), alg->parameter);
245 if (pbe == NULL)
246 goto done;
247 *pnid = OBJ_obj2nid(alg->algorithm);
248 *piter = ASN1_INTEGER_get(pbe->iter);
249 *psaltlen = pbe->salt->length;
250 *cipherid = NID_undef;
251 ret = 1;
252 break;
253 }
254 ret = 1;
255 done:
256 if (kdf != NULL)
257 PBKDF2PARAM_free(kdf);
258 if (pbe2 != NULL)
259 PBE2PARAM_free(pbe2);
260 if (pbe != NULL)
261 PBEPARAM_free(pbe);
262 return ret;
263 }