diff options
| author | tb <> | 2024-01-25 14:15:05 +0000 |
|---|---|---|
| committer | tb <> | 2024-01-25 14:15:05 +0000 |
| commit | 7a375dd140c22e6ca97aa47dd5babb32a24b1290 (patch) | |
| tree | c1452f0bdd3483ceb550ad4d080770e10c201b6f /src | |
| parent | 1a31e924d6855ac9c7889be7eb243fb7d3b6eb76 (diff) | |
| download | openbsd-7a375dd140c22e6ca97aa47dd5babb32a24b1290.tar.gz openbsd-7a375dd140c22e6ca97aa47dd5babb32a24b1290.tar.bz2 openbsd-7a375dd140c22e6ca97aa47dd5babb32a24b1290.zip | |
p12_npas.c: hoist some helpers from the bottom to the top in reverse order
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/libcrypto/pkcs12/p12_npas.c | 117 |
1 files changed, 53 insertions, 64 deletions
diff --git a/src/lib/libcrypto/pkcs12/p12_npas.c b/src/lib/libcrypto/pkcs12/p12_npas.c index 927b33a42f..25f85d0809 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.25 2024/01/25 14:09:26 tb Exp $ */ | 1 | /* $OpenBSD: p12_npas.c,v 1.26 2024/01/25 14:15:05 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 | */ |
| @@ -68,15 +68,59 @@ | |||
| 68 | 68 | ||
| 69 | /* PKCS#12 password change routine */ | 69 | /* PKCS#12 password change routine */ |
| 70 | 70 | ||
| 71 | static int newpass_bags(STACK_OF(PKCS12_SAFEBAG) *bags, const char *oldpass, | 71 | static int |
| 72 | const char *newpass); | 72 | alg_get(X509_ALGOR *alg, int *pnid, int *piter, int *psaltlen) |
| 73 | static int newpass_bag(PKCS12_SAFEBAG *bag, const char *oldpass, | 73 | { |
| 74 | const char *newpass); | 74 | PBEPARAM *pbe; |
| 75 | static int alg_get(X509_ALGOR *alg, int *pnid, int *piter, int *psaltlen); | 75 | const unsigned char *p; |
| 76 | 76 | ||
| 77 | /* | 77 | p = alg->parameter->value.sequence->data; |
| 78 | * Change the password on a PKCS#12 structure. | 78 | pbe = d2i_PBEPARAM(NULL, &p, alg->parameter->value.sequence->length); |
| 79 | */ | 79 | if (!pbe) |
| 80 | return 0; | ||
| 81 | *pnid = OBJ_obj2nid(alg->algorithm); | ||
| 82 | *piter = ASN1_INTEGER_get(pbe->iter); | ||
| 83 | *psaltlen = pbe->salt->length; | ||
| 84 | PBEPARAM_free(pbe); | ||
| 85 | return 1; | ||
| 86 | } | ||
| 87 | |||
| 88 | /* Change password of safebag: only needs handle shrouded keybags */ | ||
| 89 | static int | ||
| 90 | newpass_bag(PKCS12_SAFEBAG *bag, const char *oldpass, const char *newpass) | ||
| 91 | { | ||
| 92 | PKCS8_PRIV_KEY_INFO *p8; | ||
| 93 | X509_SIG *p8new; | ||
| 94 | int p8_nid, p8_saltlen, p8_iter; | ||
| 95 | |||
| 96 | if (OBJ_obj2nid(bag->type) != NID_pkcs8ShroudedKeyBag) | ||
| 97 | return 1; | ||
| 98 | |||
| 99 | if (!(p8 = PKCS8_decrypt(bag->value.shkeybag, oldpass, -1))) | ||
| 100 | return 0; | ||
| 101 | if (!alg_get(bag->value.shkeybag->algor, &p8_nid, &p8_iter, | ||
| 102 | &p8_saltlen)) | ||
| 103 | return 0; | ||
| 104 | if (!(p8new = PKCS8_encrypt(p8_nid, NULL, newpass, -1, NULL, p8_saltlen, | ||
| 105 | p8_iter, p8))) return 0; | ||
| 106 | X509_SIG_free(bag->value.shkeybag); | ||
| 107 | bag->value.shkeybag = p8new; | ||
| 108 | return 1; | ||
| 109 | } | ||
| 110 | |||
| 111 | static int | ||
| 112 | newpass_bags(STACK_OF(PKCS12_SAFEBAG) *bags, const char *oldpass, | ||
| 113 | const char *newpass) | ||
| 114 | { | ||
| 115 | int i; | ||
| 116 | |||
| 117 | for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) { | ||
| 118 | if (!newpass_bag(sk_PKCS12_SAFEBAG_value(bags, i), | ||
| 119 | oldpass, newpass)) | ||
| 120 | return 0; | ||
| 121 | } | ||
| 122 | return 1; | ||
| 123 | } | ||
| 80 | 124 | ||
| 81 | static int | 125 | static int |
| 82 | pkcs7_repack_data(PKCS7 *pkcs7, STACK_OF(PKCS7) *safes, const char *oldpass, | 126 | pkcs7_repack_data(PKCS7 *pkcs7, STACK_OF(PKCS7) *safes, const char *oldpass, |
| @@ -229,58 +273,3 @@ PKCS12_newpass(PKCS12 *pkcs12, const char *oldpass, const char *newpass) | |||
| 229 | return ret; | 273 | return ret; |
| 230 | } | 274 | } |
| 231 | LCRYPTO_ALIAS(PKCS12_newpass); | 275 | LCRYPTO_ALIAS(PKCS12_newpass); |
| 232 | |||
| 233 | static int | ||
| 234 | newpass_bags(STACK_OF(PKCS12_SAFEBAG) *bags, const char *oldpass, | ||
| 235 | const char *newpass) | ||
| 236 | { | ||
| 237 | int i; | ||
| 238 | |||
| 239 | for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) { | ||
| 240 | if (!newpass_bag(sk_PKCS12_SAFEBAG_value(bags, i), | ||
| 241 | oldpass, newpass)) | ||
| 242 | return 0; | ||
| 243 | } | ||
| 244 | return 1; | ||
| 245 | } | ||
| 246 | |||
| 247 | /* Change password of safebag: only needs handle shrouded keybags */ | ||
| 248 | |||
| 249 | static int | ||
| 250 | newpass_bag(PKCS12_SAFEBAG *bag, const char *oldpass, const char *newpass) | ||
| 251 | { | ||
| 252 | PKCS8_PRIV_KEY_INFO *p8; | ||
| 253 | X509_SIG *p8new; | ||
| 254 | int p8_nid, p8_saltlen, p8_iter; | ||
| 255 | |||
| 256 | if (OBJ_obj2nid(bag->type) != NID_pkcs8ShroudedKeyBag) | ||
| 257 | return 1; | ||
| 258 | |||
| 259 | if (!(p8 = PKCS8_decrypt(bag->value.shkeybag, oldpass, -1))) | ||
| 260 | return 0; | ||
| 261 | if (!alg_get(bag->value.shkeybag->algor, &p8_nid, &p8_iter, | ||
| 262 | &p8_saltlen)) | ||
| 263 | return 0; | ||
| 264 | if (!(p8new = PKCS8_encrypt(p8_nid, NULL, newpass, -1, NULL, p8_saltlen, | ||
| 265 | p8_iter, p8))) return 0; | ||
| 266 | X509_SIG_free(bag->value.shkeybag); | ||
| 267 | bag->value.shkeybag = p8new; | ||
| 268 | return 1; | ||
| 269 | } | ||
| 270 | |||
| 271 | static int | ||
| 272 | alg_get(X509_ALGOR *alg, int *pnid, int *piter, int *psaltlen) | ||
| 273 | { | ||
| 274 | PBEPARAM *pbe; | ||
| 275 | const unsigned char *p; | ||
| 276 | |||
| 277 | p = alg->parameter->value.sequence->data; | ||
| 278 | pbe = d2i_PBEPARAM(NULL, &p, alg->parameter->value.sequence->length); | ||
| 279 | if (!pbe) | ||
| 280 | return 0; | ||
| 281 | *pnid = OBJ_obj2nid(alg->algorithm); | ||
| 282 | *piter = ASN1_INTEGER_get(pbe->iter); | ||
| 283 | *psaltlen = pbe->salt->length; | ||
| 284 | PBEPARAM_free(pbe); | ||
| 285 | return 1; | ||
| 286 | } | ||
