2008-11-06 02:39:08 +08:00
|
|
|
/*
|
2024-03-20 20:07:54 +08:00
|
|
|
* Copyright 1999-2024 The OpenSSL Project Authors. All Rights Reserved.
|
1999-12-03 11:46:18 +08:00
|
|
|
*
|
2018-12-06 20:49:51 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:52:22 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1999-12-03 11:46:18 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <openssl/pem.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/pkcs12.h>
|
2019-09-28 06:45:40 +08:00
|
|
|
#include "p12_local.h"
|
1999-12-03 11:46:18 +08:00
|
|
|
|
|
|
|
/* PKCS#12 password change routine */
|
|
|
|
|
2016-05-06 10:46:09 +08:00
|
|
|
static int newpass_p12(PKCS12 *p12, const char *oldpass, const char *newpass);
|
|
|
|
static int newpass_bags(STACK_OF(PKCS12_SAFEBAG) *bags, const char *oldpass,
|
2023-01-25 09:25:33 +08:00
|
|
|
const char *newpass,
|
|
|
|
OSSL_LIB_CTX *libctx, const char *propq);
|
2016-05-06 10:46:09 +08:00
|
|
|
static int newpass_bag(PKCS12_SAFEBAG *bag, const char *oldpass,
|
2023-01-25 09:25:33 +08:00
|
|
|
const char *newpass,
|
|
|
|
OSSL_LIB_CTX *libctx, const char *propq);
|
2016-08-18 00:27:05 +08:00
|
|
|
static int alg_get(const X509_ALGOR *alg, int *pnid, int *piter,
|
2023-01-25 09:25:33 +08:00
|
|
|
int *psaltlen, int *cipherid);
|
1999-12-03 11:46:18 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Change the password on a PKCS#12 structure.
|
|
|
|
*/
|
|
|
|
|
2016-05-06 10:46:09 +08:00
|
|
|
int PKCS12_newpass(PKCS12 *p12, const char *oldpass, const char *newpass)
|
1999-12-03 11:46:18 +08:00
|
|
|
{
|
2005-05-11 11:45:39 +08:00
|
|
|
/* Check for NULL PKCS12 structure */
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2019-09-17 03:28:57 +08:00
|
|
|
if (p12 == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS12, PKCS12_R_INVALID_NULL_PKCS12_POINTER);
|
2005-05-11 11:45:39 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2005-05-11 11:45:39 +08:00
|
|
|
/* Check the mac */
|
2023-01-25 09:25:33 +08:00
|
|
|
if (p12->mac != NULL) {
|
|
|
|
if (!PKCS12_verify_mac(p12, oldpass, -1)) {
|
|
|
|
ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_VERIFY_FAILURE);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-05-11 11:45:39 +08:00
|
|
|
}
|
|
|
|
if (!newpass_p12(p12, oldpass, newpass)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS12, PKCS12_R_PARSE_ERROR);
|
2005-05-11 11:45:39 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2005-05-11 11:45:39 +08:00
|
|
|
return 1;
|
1999-12-03 11:46:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse the outer PKCS#12 structure */
|
|
|
|
|
2016-05-06 10:46:09 +08:00
|
|
|
static int newpass_p12(PKCS12 *p12, const char *oldpass, const char *newpass)
|
1999-12-03 11:46:18 +08:00
|
|
|
{
|
2016-05-05 22:37:23 +08:00
|
|
|
STACK_OF(PKCS7) *asafes = NULL, *newsafes = NULL;
|
|
|
|
STACK_OF(PKCS12_SAFEBAG) *bags = NULL;
|
2023-01-25 09:25:33 +08:00
|
|
|
int i, bagnid, pbe_nid = 0, pbe_iter = 0, pbe_saltlen = 0, cipherid = NID_undef;
|
1999-12-03 11:46:18 +08:00
|
|
|
PKCS7 *p7, *p7new;
|
2016-03-10 23:04:46 +08:00
|
|
|
ASN1_OCTET_STRING *p12_data_tmp = NULL, *macoct = NULL;
|
1999-12-03 11:46:18 +08:00
|
|
|
unsigned char mac[EVP_MAX_MD_SIZE];
|
|
|
|
unsigned int maclen;
|
2016-05-05 22:37:23 +08:00
|
|
|
int rv = 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2015-05-07 01:43:59 +08:00
|
|
|
if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
|
2016-05-05 22:37:23 +08:00
|
|
|
goto err;
|
2015-05-07 01:43:59 +08:00
|
|
|
if ((newsafes = sk_PKCS7_new_null()) == NULL)
|
2016-05-05 22:37:23 +08:00
|
|
|
goto err;
|
2000-05-17 05:22:45 +08:00
|
|
|
for (i = 0; i < sk_PKCS7_num(asafes); i++) {
|
|
|
|
p7 = sk_PKCS7_value(asafes, i);
|
2023-01-25 09:25:33 +08:00
|
|
|
|
1999-12-03 11:46:18 +08:00
|
|
|
bagnid = OBJ_obj2nid(p7->type);
|
|
|
|
if (bagnid == NID_pkcs7_data) {
|
2000-12-31 09:13:04 +08:00
|
|
|
bags = PKCS12_unpack_p7data(p7);
|
1999-12-03 11:46:18 +08:00
|
|
|
} else if (bagnid == NID_pkcs7_encrypted) {
|
2000-12-31 09:13:04 +08:00
|
|
|
bags = PKCS12_unpack_p7encdata(p7, oldpass, -1);
|
2024-01-19 19:28:58 +08:00
|
|
|
if (p7->d.encrypted == NULL
|
|
|
|
|| !alg_get(p7->d.encrypted->enc_data->algorithm,
|
|
|
|
&pbe_nid, &pbe_iter, &pbe_saltlen, &cipherid))
|
2016-05-05 22:37:23 +08:00
|
|
|
goto err;
|
|
|
|
} else {
|
1999-12-03 11:46:18 +08:00
|
|
|
continue;
|
|
|
|
}
|
2016-05-05 22:37:23 +08:00
|
|
|
if (bags == NULL)
|
|
|
|
goto err;
|
2023-01-25 09:25:33 +08:00
|
|
|
if (!newpass_bags(bags, oldpass, newpass,
|
|
|
|
p7->ctx.libctx, p7->ctx.propq))
|
2016-05-05 22:37:23 +08:00
|
|
|
goto err;
|
1999-12-03 11:46:18 +08:00
|
|
|
/* Repack bag in same form with new password */
|
|
|
|
if (bagnid == NID_pkcs7_data)
|
|
|
|
p7new = PKCS12_pack_p7data(bags);
|
|
|
|
else
|
2023-01-25 09:25:33 +08:00
|
|
|
p7new = PKCS12_pack_p7encdata_ex(pbe_nid, newpass, -1, NULL,
|
|
|
|
pbe_saltlen, pbe_iter, bags,
|
|
|
|
p7->ctx.libctx, p7->ctx.propq);
|
2019-09-17 03:28:57 +08:00
|
|
|
if (p7new == NULL || !sk_PKCS7_push(newsafes, p7new))
|
2016-05-05 22:37:23 +08:00
|
|
|
goto err;
|
2000-05-17 03:53:50 +08:00
|
|
|
sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
|
2016-05-05 22:37:23 +08:00
|
|
|
bags = NULL;
|
1999-12-03 11:46:18 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
1999-12-03 11:46:18 +08:00
|
|
|
/* Repack safe: save old safe in case of error */
|
2015-01-22 11:40:55 +08:00
|
|
|
|
1999-12-03 11:46:18 +08:00
|
|
|
p12_data_tmp = p12->authsafes->d.data;
|
2015-05-07 01:43:59 +08:00
|
|
|
if ((p12->authsafes->d.data = ASN1_OCTET_STRING_new()) == NULL)
|
2016-05-05 22:37:23 +08:00
|
|
|
goto err;
|
2000-12-31 09:13:04 +08:00
|
|
|
if (!PKCS12_pack_authsafes(p12, newsafes))
|
2016-05-05 22:37:23 +08:00
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2023-01-25 09:25:33 +08:00
|
|
|
if (p12->mac != NULL) {
|
|
|
|
if (!PKCS12_gen_mac(p12, newpass, -1, mac, &maclen))
|
|
|
|
goto err;
|
|
|
|
X509_SIG_getm(p12->mac->dinfo, NULL, &macoct);
|
|
|
|
if (!ASN1_OCTET_STRING_set(macoct, mac, maclen))
|
|
|
|
goto err;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-05-05 22:37:23 +08:00
|
|
|
rv = 1;
|
1999-12-03 11:46:18 +08:00
|
|
|
|
2016-05-05 22:37:23 +08:00
|
|
|
err:
|
|
|
|
/* Restore old safe if necessary */
|
|
|
|
if (rv == 1) {
|
|
|
|
ASN1_OCTET_STRING_free(p12_data_tmp);
|
|
|
|
} else if (p12_data_tmp != NULL) {
|
|
|
|
ASN1_OCTET_STRING_free(p12->authsafes->d.data);
|
|
|
|
p12->authsafes->d.data = p12_data_tmp;
|
|
|
|
}
|
|
|
|
sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
|
|
|
|
sk_PKCS7_pop_free(asafes, PKCS7_free);
|
|
|
|
sk_PKCS7_pop_free(newsafes, PKCS7_free);
|
|
|
|
return rv;
|
1999-12-03 11:46:18 +08:00
|
|
|
}
|
|
|
|
|
2016-05-06 10:46:09 +08:00
|
|
|
static int newpass_bags(STACK_OF(PKCS12_SAFEBAG) *bags, const char *oldpass,
|
2023-01-25 09:25:33 +08:00
|
|
|
const char *newpass,
|
|
|
|
OSSL_LIB_CTX *libctx, const char *propq)
|
1999-12-03 11:46:18 +08:00
|
|
|
{
|
|
|
|
int i;
|
2000-05-17 03:53:50 +08:00
|
|
|
for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
|
2023-01-25 09:25:33 +08:00
|
|
|
if (!newpass_bag(sk_PKCS12_SAFEBAG_value(bags, i), oldpass, newpass,
|
|
|
|
libctx, propq))
|
2000-05-17 03:53:50 +08:00
|
|
|
return 0;
|
1999-12-03 11:46:18 +08:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Change password of safebag: only needs handle shrouded keybags */
|
|
|
|
|
2016-05-06 10:46:09 +08:00
|
|
|
static int newpass_bag(PKCS12_SAFEBAG *bag, const char *oldpass,
|
2023-01-25 09:25:33 +08:00
|
|
|
const char *newpass,
|
|
|
|
OSSL_LIB_CTX *libctx, const char *propq)
|
1999-12-03 11:46:18 +08:00
|
|
|
{
|
2023-01-25 09:25:33 +08:00
|
|
|
EVP_CIPHER *cipher = NULL;
|
1999-12-03 11:46:18 +08:00
|
|
|
PKCS8_PRIV_KEY_INFO *p8;
|
|
|
|
X509_SIG *p8new;
|
2023-01-25 09:25:33 +08:00
|
|
|
int p8_nid, p8_saltlen, p8_iter, cipherid = 0;
|
2016-08-18 00:27:05 +08:00
|
|
|
const X509_ALGOR *shalg;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2015-09-27 19:41:53 +08:00
|
|
|
if (PKCS12_SAFEBAG_get_nid(bag) != NID_pkcs8ShroudedKeyBag)
|
1999-12-03 11:46:18 +08:00
|
|
|
return 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2023-01-25 09:25:33 +08:00
|
|
|
if ((p8 = PKCS8_decrypt_ex(bag->value.shkeybag, oldpass, -1,
|
|
|
|
libctx, propq)) == NULL)
|
2000-12-31 09:13:04 +08:00
|
|
|
return 0;
|
2016-08-18 00:27:05 +08:00
|
|
|
X509_SIG_get0(bag->value.shkeybag, &shalg, NULL);
|
2023-01-25 09:25:33 +08:00
|
|
|
if (!alg_get(shalg, &p8_nid, &p8_iter, &p8_saltlen, &cipherid)) {
|
2020-09-14 05:40:58 +08:00
|
|
|
PKCS8_PRIV_KEY_INFO_free(p8);
|
2010-03-28 08:42:38 +08:00
|
|
|
return 0;
|
2020-09-14 05:40:58 +08:00
|
|
|
}
|
2023-01-25 09:25:33 +08:00
|
|
|
if (cipherid != NID_undef) {
|
|
|
|
cipher = EVP_CIPHER_fetch(libctx, OBJ_nid2sn(cipherid), propq);
|
|
|
|
if (cipher == NULL) {
|
|
|
|
PKCS8_PRIV_KEY_INFO_free(p8);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p8new = PKCS8_encrypt_ex(p8_nid, cipher, newpass, -1, NULL, p8_saltlen,
|
|
|
|
p8_iter, p8, libctx, propq);
|
2016-05-05 22:37:23 +08:00
|
|
|
PKCS8_PRIV_KEY_INFO_free(p8);
|
2023-01-25 09:25:33 +08:00
|
|
|
EVP_CIPHER_free(cipher);
|
2016-05-05 22:37:23 +08:00
|
|
|
if (p8new == NULL)
|
1999-12-03 11:46:18 +08:00
|
|
|
return 0;
|
|
|
|
X509_SIG_free(bag->value.shkeybag);
|
|
|
|
bag->value.shkeybag = p8new;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-08-18 00:27:05 +08:00
|
|
|
static int alg_get(const X509_ALGOR *alg, int *pnid, int *piter,
|
2023-01-25 09:25:33 +08:00
|
|
|
int *psaltlen, int *cipherid)
|
1999-12-03 11:46:18 +08:00
|
|
|
{
|
2023-01-25 09:25:33 +08:00
|
|
|
int ret = 0, pbenid, aparamtype;
|
|
|
|
int encnid, prfnid;
|
|
|
|
const ASN1_OBJECT *aoid;
|
|
|
|
const void *aparam;
|
|
|
|
PBEPARAM *pbe = NULL;
|
|
|
|
PBE2PARAM *pbe2 = NULL;
|
|
|
|
PBKDF2PARAM *kdf = NULL;
|
2019-09-17 03:28:57 +08:00
|
|
|
|
2023-01-25 09:25:33 +08:00
|
|
|
X509_ALGOR_get0(&aoid, &aparamtype, &aparam, alg);
|
|
|
|
pbenid = OBJ_obj2nid(aoid);
|
|
|
|
|
|
|
|
switch (pbenid) {
|
|
|
|
case NID_pbes2:
|
|
|
|
if (aparamtype == V_ASN1_SEQUENCE)
|
|
|
|
pbe2 = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBE2PARAM));
|
|
|
|
if (pbe2 == NULL)
|
|
|
|
goto done;
|
|
|
|
|
2024-01-12 15:32:06 +08:00
|
|
|
X509_ALGOR_get0(NULL, &aparamtype, &aparam, pbe2->keyfunc);
|
2023-01-25 09:25:33 +08:00
|
|
|
X509_ALGOR_get0(&aoid, NULL, NULL, pbe2->encryption);
|
|
|
|
encnid = OBJ_obj2nid(aoid);
|
|
|
|
|
|
|
|
if (aparamtype == V_ASN1_SEQUENCE)
|
|
|
|
kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBKDF2PARAM));
|
|
|
|
if (kdf == NULL)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* Only OCTET_STRING is supported */
|
|
|
|
if (kdf->salt->type != V_ASN1_OCTET_STRING)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
if (kdf->prf == NULL) {
|
|
|
|
prfnid = NID_hmacWithSHA1;
|
|
|
|
} else {
|
|
|
|
X509_ALGOR_get0(&aoid, NULL, NULL, kdf->prf);
|
|
|
|
prfnid = OBJ_obj2nid(aoid);
|
|
|
|
}
|
|
|
|
*psaltlen = kdf->salt->value.octet_string->length;
|
|
|
|
*piter = ASN1_INTEGER_get(kdf->iter);
|
|
|
|
*pnid = prfnid;
|
|
|
|
*cipherid = encnid;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
pbe = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBEPARAM), alg->parameter);
|
|
|
|
if (pbe == NULL)
|
|
|
|
goto done;
|
|
|
|
*pnid = OBJ_obj2nid(alg->algorithm);
|
|
|
|
*piter = ASN1_INTEGER_get(pbe->iter);
|
|
|
|
*psaltlen = pbe->salt->length;
|
|
|
|
*cipherid = NID_undef;
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ret = 1;
|
|
|
|
done:
|
|
|
|
if (kdf != NULL)
|
|
|
|
PBKDF2PARAM_free(kdf);
|
|
|
|
if (pbe2 != NULL)
|
|
|
|
PBE2PARAM_free(pbe2);
|
|
|
|
if (pbe != NULL)
|
|
|
|
PBEPARAM_free(pbe);
|
|
|
|
return ret;
|
1999-12-03 11:46:18 +08:00
|
|
|
}
|