10Sstevel@tonic-gate /* p12_npas.c */
20Sstevel@tonic-gate /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
30Sstevel@tonic-gate * project 1999.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate /* ====================================================================
60Sstevel@tonic-gate * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate * are met:
110Sstevel@tonic-gate *
120Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
130Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
140Sstevel@tonic-gate *
150Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in
170Sstevel@tonic-gate * the documentation and/or other materials provided with the
180Sstevel@tonic-gate * distribution.
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this
210Sstevel@tonic-gate * software must display the following acknowledgment:
220Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
230Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
240Sstevel@tonic-gate *
250Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
260Sstevel@tonic-gate * endorse or promote products derived from this software without
270Sstevel@tonic-gate * prior written permission. For written permission, please contact
280Sstevel@tonic-gate * licensing@OpenSSL.org.
290Sstevel@tonic-gate *
300Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL"
310Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written
320Sstevel@tonic-gate * permission of the OpenSSL Project.
330Sstevel@tonic-gate *
340Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following
350Sstevel@tonic-gate * acknowledgment:
360Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
370Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
380Sstevel@tonic-gate *
390Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
400Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
410Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
420Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
430Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
440Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
450Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
460Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
470Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
480Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
490Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
500Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE.
510Sstevel@tonic-gate * ====================================================================
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young
540Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim
550Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com).
560Sstevel@tonic-gate *
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include <stdlib.h>
610Sstevel@tonic-gate #include <string.h>
620Sstevel@tonic-gate #include <openssl/pem.h>
630Sstevel@tonic-gate #include <openssl/err.h>
640Sstevel@tonic-gate #include <openssl/pkcs12.h>
650Sstevel@tonic-gate
660Sstevel@tonic-gate /* PKCS#12 password change routine */
670Sstevel@tonic-gate
680Sstevel@tonic-gate static int newpass_p12(PKCS12 *p12, char *oldpass, char *newpass);
690Sstevel@tonic-gate static int newpass_bags(STACK_OF(PKCS12_SAFEBAG) *bags, char *oldpass,
700Sstevel@tonic-gate char *newpass);
710Sstevel@tonic-gate static int newpass_bag(PKCS12_SAFEBAG *bag, char *oldpass, char *newpass);
720Sstevel@tonic-gate static int alg_get(X509_ALGOR *alg, int *pnid, int *piter, int *psaltlen);
730Sstevel@tonic-gate
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate * Change the password on a PKCS#12 structure.
760Sstevel@tonic-gate */
770Sstevel@tonic-gate
PKCS12_newpass(PKCS12 * p12,char * oldpass,char * newpass)780Sstevel@tonic-gate int PKCS12_newpass(PKCS12 *p12, char *oldpass, char *newpass)
790Sstevel@tonic-gate {
80*2139Sjp161948 /* Check for NULL PKCS12 structure */
810Sstevel@tonic-gate
82*2139Sjp161948 if(!p12) {
83*2139Sjp161948 PKCS12err(PKCS12_F_PKCS12_NEWPASS,PKCS12_R_INVALID_NULL_PKCS12_POINTER);
84*2139Sjp161948 return 0;
85*2139Sjp161948 }
860Sstevel@tonic-gate
87*2139Sjp161948 /* Check the mac */
88*2139Sjp161948
89*2139Sjp161948 if (!PKCS12_verify_mac(p12, oldpass, -1)) {
90*2139Sjp161948 PKCS12err(PKCS12_F_PKCS12_NEWPASS,PKCS12_R_MAC_VERIFY_FAILURE);
91*2139Sjp161948 return 0;
92*2139Sjp161948 }
930Sstevel@tonic-gate
94*2139Sjp161948 if (!newpass_p12(p12, oldpass, newpass)) {
95*2139Sjp161948 PKCS12err(PKCS12_F_PKCS12_NEWPASS,PKCS12_R_PARSE_ERROR);
96*2139Sjp161948 return 0;
97*2139Sjp161948 }
980Sstevel@tonic-gate
99*2139Sjp161948 return 1;
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate /* Parse the outer PKCS#12 structure */
1030Sstevel@tonic-gate
newpass_p12(PKCS12 * p12,char * oldpass,char * newpass)1040Sstevel@tonic-gate static int newpass_p12(PKCS12 *p12, char *oldpass, char *newpass)
1050Sstevel@tonic-gate {
1060Sstevel@tonic-gate STACK_OF(PKCS7) *asafes, *newsafes;
1070Sstevel@tonic-gate STACK_OF(PKCS12_SAFEBAG) *bags;
1080Sstevel@tonic-gate int i, bagnid, pbe_nid = 0, pbe_iter = 0, pbe_saltlen = 0;
1090Sstevel@tonic-gate PKCS7 *p7, *p7new;
1100Sstevel@tonic-gate ASN1_OCTET_STRING *p12_data_tmp = NULL, *macnew = NULL;
1110Sstevel@tonic-gate unsigned char mac[EVP_MAX_MD_SIZE];
1120Sstevel@tonic-gate unsigned int maclen;
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate if (!(asafes = PKCS12_unpack_authsafes(p12))) return 0;
1150Sstevel@tonic-gate if(!(newsafes = sk_PKCS7_new_null())) return 0;
1160Sstevel@tonic-gate for (i = 0; i < sk_PKCS7_num (asafes); i++) {
1170Sstevel@tonic-gate p7 = sk_PKCS7_value(asafes, i);
1180Sstevel@tonic-gate bagnid = OBJ_obj2nid(p7->type);
1190Sstevel@tonic-gate if (bagnid == NID_pkcs7_data) {
1200Sstevel@tonic-gate bags = PKCS12_unpack_p7data(p7);
1210Sstevel@tonic-gate } else if (bagnid == NID_pkcs7_encrypted) {
1220Sstevel@tonic-gate bags = PKCS12_unpack_p7encdata(p7, oldpass, -1);
1230Sstevel@tonic-gate alg_get(p7->d.encrypted->enc_data->algorithm,
1240Sstevel@tonic-gate &pbe_nid, &pbe_iter, &pbe_saltlen);
1250Sstevel@tonic-gate } else continue;
1260Sstevel@tonic-gate if (!bags) {
1270Sstevel@tonic-gate sk_PKCS7_pop_free(asafes, PKCS7_free);
1280Sstevel@tonic-gate return 0;
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate if (!newpass_bags(bags, oldpass, newpass)) {
1310Sstevel@tonic-gate sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
1320Sstevel@tonic-gate sk_PKCS7_pop_free(asafes, PKCS7_free);
1330Sstevel@tonic-gate return 0;
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate /* Repack bag in same form with new password */
1360Sstevel@tonic-gate if (bagnid == NID_pkcs7_data) p7new = PKCS12_pack_p7data(bags);
1370Sstevel@tonic-gate else p7new = PKCS12_pack_p7encdata(pbe_nid, newpass, -1, NULL,
1380Sstevel@tonic-gate pbe_saltlen, pbe_iter, bags);
1390Sstevel@tonic-gate sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
1400Sstevel@tonic-gate if(!p7new) {
1410Sstevel@tonic-gate sk_PKCS7_pop_free(asafes, PKCS7_free);
1420Sstevel@tonic-gate return 0;
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate sk_PKCS7_push(newsafes, p7new);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate sk_PKCS7_pop_free(asafes, PKCS7_free);
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate /* Repack safe: save old safe in case of error */
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate p12_data_tmp = p12->authsafes->d.data;
1510Sstevel@tonic-gate if(!(p12->authsafes->d.data = ASN1_OCTET_STRING_new())) goto saferr;
1520Sstevel@tonic-gate if(!PKCS12_pack_authsafes(p12, newsafes)) goto saferr;
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate if(!PKCS12_gen_mac(p12, newpass, -1, mac, &maclen)) goto saferr;
1550Sstevel@tonic-gate if(!(macnew = ASN1_OCTET_STRING_new())) goto saferr;
1560Sstevel@tonic-gate if(!ASN1_OCTET_STRING_set(macnew, mac, maclen)) goto saferr;
1570Sstevel@tonic-gate ASN1_OCTET_STRING_free(p12->mac->dinfo->digest);
1580Sstevel@tonic-gate p12->mac->dinfo->digest = macnew;
1590Sstevel@tonic-gate ASN1_OCTET_STRING_free(p12_data_tmp);
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate return 1;
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate saferr:
1640Sstevel@tonic-gate /* Restore old safe */
1650Sstevel@tonic-gate ASN1_OCTET_STRING_free(p12->authsafes->d.data);
1660Sstevel@tonic-gate ASN1_OCTET_STRING_free(macnew);
1670Sstevel@tonic-gate p12->authsafes->d.data = p12_data_tmp;
1680Sstevel@tonic-gate return 0;
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate
newpass_bags(STACK_OF (PKCS12_SAFEBAG)* bags,char * oldpass,char * newpass)1730Sstevel@tonic-gate static int newpass_bags(STACK_OF(PKCS12_SAFEBAG) *bags, char *oldpass,
1740Sstevel@tonic-gate char *newpass)
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate int i;
1770Sstevel@tonic-gate for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
1780Sstevel@tonic-gate if (!newpass_bag(sk_PKCS12_SAFEBAG_value(bags, i),
1790Sstevel@tonic-gate oldpass, newpass))
1800Sstevel@tonic-gate return 0;
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate return 1;
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate /* Change password of safebag: only needs handle shrouded keybags */
1860Sstevel@tonic-gate
newpass_bag(PKCS12_SAFEBAG * bag,char * oldpass,char * newpass)1870Sstevel@tonic-gate static int newpass_bag(PKCS12_SAFEBAG *bag, char *oldpass, char *newpass)
1880Sstevel@tonic-gate {
1890Sstevel@tonic-gate PKCS8_PRIV_KEY_INFO *p8;
1900Sstevel@tonic-gate X509_SIG *p8new;
1910Sstevel@tonic-gate int p8_nid, p8_saltlen, p8_iter;
1920Sstevel@tonic-gate
1930Sstevel@tonic-gate if(M_PKCS12_bag_type(bag) != NID_pkcs8ShroudedKeyBag) return 1;
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate if (!(p8 = PKCS8_decrypt(bag->value.shkeybag, oldpass, -1))) return 0;
1960Sstevel@tonic-gate alg_get(bag->value.shkeybag->algor, &p8_nid, &p8_iter, &p8_saltlen);
1970Sstevel@tonic-gate if(!(p8new = PKCS8_encrypt(p8_nid, NULL, newpass, -1, NULL, p8_saltlen,
1980Sstevel@tonic-gate p8_iter, p8))) return 0;
1990Sstevel@tonic-gate X509_SIG_free(bag->value.shkeybag);
2000Sstevel@tonic-gate bag->value.shkeybag = p8new;
2010Sstevel@tonic-gate return 1;
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate
alg_get(X509_ALGOR * alg,int * pnid,int * piter,int * psaltlen)2040Sstevel@tonic-gate static int alg_get(X509_ALGOR *alg, int *pnid, int *piter, int *psaltlen)
2050Sstevel@tonic-gate {
2060Sstevel@tonic-gate PBEPARAM *pbe;
207*2139Sjp161948 const unsigned char *p;
208*2139Sjp161948
2090Sstevel@tonic-gate p = alg->parameter->value.sequence->data;
2100Sstevel@tonic-gate pbe = d2i_PBEPARAM(NULL, &p, alg->parameter->value.sequence->length);
2110Sstevel@tonic-gate *pnid = OBJ_obj2nid(alg->algorithm);
2120Sstevel@tonic-gate *piter = ASN1_INTEGER_get(pbe->iter);
2130Sstevel@tonic-gate *psaltlen = pbe->salt->length;
2140Sstevel@tonic-gate PBEPARAM_free(pbe);
2150Sstevel@tonic-gate return 0;
2160Sstevel@tonic-gate }
217