summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortb <>2024-01-25 10:44:39 +0000
committertb <>2024-01-25 10:44:39 +0000
commit9061bc33eff0f42a09003e414462268a933b6546 (patch)
tree4a0f888e114455b35722611e69b32cfcc8008c8d
parent194233cb74b81e0be44fef744914b91369d9912f (diff)
downloadopenbsd-9061bc33eff0f42a09003e414462268a933b6546.tar.gz
openbsd-9061bc33eff0f42a09003e414462268a933b6546.tar.bz2
openbsd-9061bc33eff0f42a09003e414462268a933b6546.zip
Rework newpass_p12() a bit more
Split the bottom half that repacks the authsafes into a helper function. This simplifies the curly exit path and makes it clearer what is being done. PKCS12_pack_authsafes() is a very inconvenient API and there are some extra dances needed due to it. ok jsing
-rw-r--r--src/lib/libcrypto/pkcs12/p12_npas.c83
1 files changed, 49 insertions, 34 deletions
diff --git a/src/lib/libcrypto/pkcs12/p12_npas.c b/src/lib/libcrypto/pkcs12/p12_npas.c
index 557457b0f3..b0858b6f2b 100644
--- a/src/lib/libcrypto/pkcs12/p12_npas.c
+++ b/src/lib/libcrypto/pkcs12/p12_npas.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: p12_npas.c,v 1.20 2024/01/25 09:40:09 tb Exp $ */ 1/* $OpenBSD: p12_npas.c,v 1.21 2024/01/25 10:44:39 tb Exp $ */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
@@ -165,13 +165,54 @@ pkcs7_repack_encdata(PKCS7 *pkcs7, STACK_OF(PKCS7) *newsafes, const char *oldpas
165} 165}
166 166
167static int 167static int
168newpass_p12(PKCS12 *p12, const char *oldpass, const char *newpass) 168pkcs12_repack_safe(PKCS12 *pkcs12, STACK_OF(PKCS7) *newsafes,
169 const char *newpass)
169{ 170{
170 STACK_OF(PKCS7) *asafes = NULL, *newsafes = NULL; 171 ASN1_OCTET_STRING *old_data;
171 ASN1_OCTET_STRING *p12_data_tmp = NULL, *macnew = NULL; 172 ASN1_OCTET_STRING *new_mac = NULL;
172 unsigned char mac[EVP_MAX_MD_SIZE]; 173 unsigned char mac[EVP_MAX_MD_SIZE];
173 unsigned int maclen; 174 unsigned int maclen;
175 int ret = 0;
176
177 if ((old_data = pkcs12->authsafes->d.data) == NULL)
178 goto err;
179 if ((pkcs12->authsafes->d.data = ASN1_OCTET_STRING_new()) == NULL)
180 goto err;
181 if (!PKCS12_pack_authsafes(pkcs12, newsafes))
182 goto err;
183 if (!PKCS12_gen_mac(pkcs12, newpass, -1, mac, &maclen))
184 goto err;
185 if ((new_mac = ASN1_OCTET_STRING_new()) == NULL)
186 goto err;
187 if (!ASN1_OCTET_STRING_set(new_mac, mac, maclen))
188 goto err;
189
190 ASN1_OCTET_STRING_free(pkcs12->mac->dinfo->digest);
191 pkcs12->mac->dinfo->digest = new_mac;
192 new_mac = NULL;
193
194 ASN1_OCTET_STRING_free(old_data);
195 old_data = NULL;
196
197 ret = 1;
198
199 err:
200 if (old_data != NULL) {
201 ASN1_OCTET_STRING_free(pkcs12->authsafes->d.data);
202 pkcs12->authsafes->d.data = old_data;
203 }
204 explicit_bzero(mac, sizeof(mac));
205 ASN1_OCTET_STRING_free(new_mac);
206
207 return ret;
208}
209
210static int
211newpass_p12(PKCS12 *p12, const char *oldpass, const char *newpass)
212{
213 STACK_OF(PKCS7) *asafes = NULL, *newsafes = NULL;
174 int i; 214 int i;
215 int ret = 0;
175 216
176 if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL) 217 if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
177 goto err; 218 goto err;
@@ -192,43 +233,17 @@ newpass_p12(PKCS12 *p12, const char *oldpass, const char *newpass)
192 break; 233 break;
193 } 234 }
194 } 235 }
195 sk_PKCS7_pop_free(asafes, PKCS7_free);
196 asafes = NULL;
197
198 /* Repack safe: save old safe in case of error */
199 236
200 p12_data_tmp = p12->authsafes->d.data; 237 if (!pkcs12_repack_safe(p12, newsafes, newpass))
201 if (!(p12->authsafes->d.data = ASN1_OCTET_STRING_new())) {
202 p12->authsafes->d.data = p12_data_tmp;
203 goto err; 238 goto err;
204 }
205 if (!PKCS12_pack_authsafes(p12, newsafes))
206 goto saferr;
207
208 if (!PKCS12_gen_mac(p12, newpass, -1, mac, &maclen))
209 goto saferr;
210 if (!(macnew = ASN1_OCTET_STRING_new()))
211 goto saferr;
212 if (!ASN1_OCTET_STRING_set(macnew, mac, maclen))
213 goto saferr;
214 ASN1_OCTET_STRING_free(p12->mac->dinfo->digest);
215 p12->mac->dinfo->digest = macnew;
216 ASN1_OCTET_STRING_free(p12_data_tmp);
217
218 return 1;
219 239
220saferr: 240 ret = 1;
221 /* Restore old safe */
222 ASN1_OCTET_STRING_free(p12->authsafes->d.data);
223 ASN1_OCTET_STRING_free(macnew);
224 p12->authsafes->d.data = p12_data_tmp;
225 return 0;
226 241
227err: 242 err:
228 sk_PKCS7_pop_free(asafes, PKCS7_free); 243 sk_PKCS7_pop_free(asafes, PKCS7_free);
229 sk_PKCS7_pop_free(newsafes, PKCS7_free); 244 sk_PKCS7_pop_free(newsafes, PKCS7_free);
230 245
231 return 0; 246 return ret;
232} 247}
233 248
234 249