1 /* $OpenBSD: p12_npas.c,v 1.14 2021/11/01 20:53:08 tb Exp $ */ 2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3 * project 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 #include "x509_lcl.h" 67 68 /* PKCS#12 password change routine */ 69 70 static int newpass_p12(PKCS12 *p12, const char *oldpass, const char *newpass); 71 static int newpass_bags(STACK_OF(PKCS12_SAFEBAG) *bags, const char *oldpass, 72 const char *newpass); 73 static int newpass_bag(PKCS12_SAFEBAG *bag, const char *oldpass, 74 const char *newpass); 75 static int alg_get(X509_ALGOR *alg, int *pnid, int *piter, int *psaltlen); 76 77 /* 78 * Change the password on a PKCS#12 structure. 79 */ 80 81 int 82 PKCS12_newpass(PKCS12 *p12, const char *oldpass, const char *newpass) 83 { 84 /* Check for NULL PKCS12 structure */ 85 86 if (!p12) { 87 PKCS12error(PKCS12_R_INVALID_NULL_PKCS12_POINTER); 88 return 0; 89 } 90 91 /* Check the mac */ 92 93 if (!PKCS12_verify_mac(p12, oldpass, -1)) { 94 PKCS12error(PKCS12_R_MAC_VERIFY_FAILURE); 95 return 0; 96 } 97 98 if (!newpass_p12(p12, oldpass, newpass)) { 99 PKCS12error(PKCS12_R_PARSE_ERROR); 100 return 0; 101 } 102 103 return 1; 104 } 105 106 /* Parse the outer PKCS#12 structure */ 107 108 static int 109 newpass_p12(PKCS12 *p12, const char *oldpass, const char *newpass) 110 { 111 STACK_OF(PKCS7) *asafes, *newsafes; 112 STACK_OF(PKCS12_SAFEBAG) *bags; 113 int i, bagnid, pbe_nid = 0, pbe_iter = 0, pbe_saltlen = 0; 114 PKCS7 *p7, *p7new; 115 ASN1_OCTET_STRING *p12_data_tmp = NULL, *macnew = NULL; 116 unsigned char mac[EVP_MAX_MD_SIZE]; 117 unsigned int maclen; 118 119 if (!(asafes = PKCS12_unpack_authsafes(p12))) 120 return 0; 121 if (!(newsafes = sk_PKCS7_new_null())) 122 return 0; 123 for (i = 0; i < sk_PKCS7_num(asafes); i++) { 124 p7 = sk_PKCS7_value(asafes, i); 125 bagnid = OBJ_obj2nid(p7->type); 126 if (bagnid == NID_pkcs7_data) { 127 bags = PKCS12_unpack_p7data(p7); 128 } else if (bagnid == NID_pkcs7_encrypted) { 129 bags = PKCS12_unpack_p7encdata(p7, oldpass, -1); 130 if (!alg_get(p7->d.encrypted->enc_data->algorithm, 131 &pbe_nid, &pbe_iter, &pbe_saltlen)) { 132 sk_PKCS12_SAFEBAG_pop_free(bags, 133 PKCS12_SAFEBAG_free); 134 bags = NULL; 135 } 136 } else 137 continue; 138 if (bags == NULL) 139 goto err; 140 if (!newpass_bags(bags, oldpass, newpass)) { 141 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free); 142 goto err; 143 } 144 /* Repack bag in same form with new password */ 145 if (bagnid == NID_pkcs7_data) 146 p7new = PKCS12_pack_p7data(bags); 147 else 148 p7new = PKCS12_pack_p7encdata(pbe_nid, newpass, -1, 149 NULL, pbe_saltlen, pbe_iter, bags); 150 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free); 151 if (p7new == NULL) 152 goto err; 153 if (sk_PKCS7_push(newsafes, p7new) == 0) 154 goto err; 155 } 156 sk_PKCS7_pop_free(asafes, PKCS7_free); 157 158 /* Repack safe: save old safe in case of error */ 159 160 p12_data_tmp = p12->authsafes->d.data; 161 if (!(p12->authsafes->d.data = ASN1_OCTET_STRING_new())) { 162 p12->authsafes->d.data = p12_data_tmp; 163 goto err; 164 } 165 if (!PKCS12_pack_authsafes(p12, newsafes)) 166 goto saferr; 167 168 if (!PKCS12_gen_mac(p12, newpass, -1, mac, &maclen)) 169 goto saferr; 170 if (!(macnew = ASN1_OCTET_STRING_new())) 171 goto saferr; 172 if (!ASN1_OCTET_STRING_set(macnew, mac, maclen)) 173 goto saferr; 174 ASN1_OCTET_STRING_free(p12->mac->dinfo->digest); 175 p12->mac->dinfo->digest = macnew; 176 ASN1_OCTET_STRING_free(p12_data_tmp); 177 178 return 1; 179 180 saferr: 181 /* Restore old safe */ 182 ASN1_OCTET_STRING_free(p12->authsafes->d.data); 183 ASN1_OCTET_STRING_free(macnew); 184 p12->authsafes->d.data = p12_data_tmp; 185 return 0; 186 187 err: 188 sk_PKCS7_pop_free(asafes, PKCS7_free); 189 sk_PKCS7_pop_free(newsafes, PKCS7_free); 190 return 0; 191 } 192 193 194 static int 195 newpass_bags(STACK_OF(PKCS12_SAFEBAG) *bags, const char *oldpass, 196 const char *newpass) 197 { 198 int i; 199 200 for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) { 201 if (!newpass_bag(sk_PKCS12_SAFEBAG_value(bags, i), 202 oldpass, newpass)) 203 return 0; 204 } 205 return 1; 206 } 207 208 /* Change password of safebag: only needs handle shrouded keybags */ 209 210 static int 211 newpass_bag(PKCS12_SAFEBAG *bag, const char *oldpass, const char *newpass) 212 { 213 PKCS8_PRIV_KEY_INFO *p8; 214 X509_SIG *p8new; 215 int p8_nid, p8_saltlen, p8_iter; 216 217 if (OBJ_obj2nid(bag->type) != NID_pkcs8ShroudedKeyBag) 218 return 1; 219 220 if (!(p8 = PKCS8_decrypt(bag->value.shkeybag, oldpass, -1))) 221 return 0; 222 if (!alg_get(bag->value.shkeybag->algor, &p8_nid, &p8_iter, 223 &p8_saltlen)) 224 return 0; 225 if (!(p8new = PKCS8_encrypt(p8_nid, NULL, newpass, -1, NULL, p8_saltlen, 226 p8_iter, p8))) return 0; 227 X509_SIG_free(bag->value.shkeybag); 228 bag->value.shkeybag = p8new; 229 return 1; 230 } 231 232 static int 233 alg_get(X509_ALGOR *alg, int *pnid, int *piter, int *psaltlen) 234 { 235 PBEPARAM *pbe; 236 const unsigned char *p; 237 238 p = alg->parameter->value.sequence->data; 239 pbe = d2i_PBEPARAM(NULL, &p, alg->parameter->value.sequence->length); 240 if (!pbe) 241 return 0; 242 *pnid = OBJ_obj2nid(alg->algorithm); 243 *piter = ASN1_INTEGER_get(pbe->iter); 244 *psaltlen = pbe->salt->length; 245 PBEPARAM_free(pbe); 246 return 1; 247 } 248