155714Skris/* p12_decr.c */ 2296341Sdelphij/* 3296341Sdelphij * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project 4296341Sdelphij * 1999. 555714Skris */ 655714Skris/* ==================================================================== 755714Skris * Copyright (c) 1999 The OpenSSL Project. All rights reserved. 855714Skris * 955714Skris * Redistribution and use in source and binary forms, with or without 1055714Skris * modification, are permitted provided that the following conditions 1155714Skris * are met: 1255714Skris * 1355714Skris * 1. Redistributions of source code must retain the above copyright 14296341Sdelphij * notice, this list of conditions and the following disclaimer. 1555714Skris * 1655714Skris * 2. Redistributions in binary form must reproduce the above copyright 1755714Skris * notice, this list of conditions and the following disclaimer in 1855714Skris * the documentation and/or other materials provided with the 1955714Skris * distribution. 2055714Skris * 2155714Skris * 3. All advertising materials mentioning features or use of this 2255714Skris * software must display the following acknowledgment: 2355714Skris * "This product includes software developed by the OpenSSL Project 2455714Skris * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 2555714Skris * 2655714Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 2755714Skris * endorse or promote products derived from this software without 2855714Skris * prior written permission. For written permission, please contact 2955714Skris * licensing@OpenSSL.org. 3055714Skris * 3155714Skris * 5. Products derived from this software may not be called "OpenSSL" 3255714Skris * nor may "OpenSSL" appear in their names without prior written 3355714Skris * permission of the OpenSSL Project. 3455714Skris * 3555714Skris * 6. Redistributions of any form whatsoever must retain the following 3655714Skris * acknowledgment: 3755714Skris * "This product includes software developed by the OpenSSL Project 3855714Skris * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 3955714Skris * 4055714Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 4155714Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4255714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 4355714Skris * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 4455714Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 4555714Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 4655714Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 4755714Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 4955714Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 5055714Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 5155714Skris * OF THE POSSIBILITY OF SUCH DAMAGE. 5255714Skris * ==================================================================== 5355714Skris * 5455714Skris * This product includes cryptographic software written by Eric Young 5555714Skris * (eay@cryptsoft.com). This product includes software written by Tim 5655714Skris * Hudson (tjh@cryptsoft.com). 5755714Skris * 5855714Skris */ 5955714Skris 6055714Skris#include <stdio.h> 6155714Skris#include "cryptlib.h" 6255714Skris#include <openssl/pkcs12.h> 6355714Skris 6455714Skris/* Define this to dump decrypted output to files called DERnnn */ 65296341Sdelphij/* 66296341Sdelphij * #define DEBUG_DECRYPT 67296341Sdelphij */ 6855714Skris 69296341Sdelphij/* 70296341Sdelphij * Encrypt/Decrypt a buffer based on password and algor, result in a 7168651Skris * OPENSSL_malloc'ed buffer 7255714Skris */ 7355714Skris 74296341Sdelphijunsigned char *PKCS12_pbe_crypt(X509_ALGOR *algor, const char *pass, 75296341Sdelphij int passlen, unsigned char *in, int inlen, 76296341Sdelphij unsigned char **data, int *datalen, int en_de) 7755714Skris{ 78296341Sdelphij unsigned char *out; 79296341Sdelphij int outlen, i; 80296341Sdelphij EVP_CIPHER_CTX ctx; 8155714Skris 82296341Sdelphij EVP_CIPHER_CTX_init(&ctx); 83296341Sdelphij /* Decrypt data */ 84296341Sdelphij if (!EVP_PBE_CipherInit(algor->algorithm, pass, passlen, 85296341Sdelphij algor->parameter, &ctx, en_de)) { 86296341Sdelphij PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT, 87296341Sdelphij PKCS12_R_PKCS12_ALGOR_CIPHERINIT_ERROR); 88296341Sdelphij return NULL; 89296341Sdelphij } 9055714Skris 91296341Sdelphij if (!(out = OPENSSL_malloc(inlen + EVP_CIPHER_CTX_block_size(&ctx)))) { 92296341Sdelphij PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT, ERR_R_MALLOC_FAILURE); 93296341Sdelphij goto err; 94296341Sdelphij } 9555714Skris 96296341Sdelphij if (!EVP_CipherUpdate(&ctx, out, &i, in, inlen)) { 97296341Sdelphij OPENSSL_free(out); 98296341Sdelphij out = NULL; 99296341Sdelphij PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT, ERR_R_EVP_LIB); 100296341Sdelphij goto err; 101296341Sdelphij } 102238405Sjkim 103296341Sdelphij outlen = i; 104296341Sdelphij if (!EVP_CipherFinal_ex(&ctx, out + i, &i)) { 105296341Sdelphij OPENSSL_free(out); 106296341Sdelphij out = NULL; 107296341Sdelphij PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT, 108296341Sdelphij PKCS12_R_PKCS12_CIPHERFINAL_ERROR); 109296341Sdelphij goto err; 110296341Sdelphij } 111296341Sdelphij outlen += i; 112296341Sdelphij if (datalen) 113296341Sdelphij *datalen = outlen; 114296341Sdelphij if (data) 115296341Sdelphij *data = out; 116296341Sdelphij err: 117296341Sdelphij EVP_CIPHER_CTX_cleanup(&ctx); 118296341Sdelphij return out; 11955714Skris 12055714Skris} 12155714Skris 122296341Sdelphij/* 123296341Sdelphij * Decrypt an OCTET STRING and decode ASN1 structure if zbuf set zero buffer 124296341Sdelphij * after use. 12555714Skris */ 12655714Skris 127296341Sdelphijvoid *PKCS12_item_decrypt_d2i(X509_ALGOR *algor, const ASN1_ITEM *it, 128296341Sdelphij const char *pass, int passlen, 129296341Sdelphij ASN1_OCTET_STRING *oct, int zbuf) 13055714Skris{ 131296341Sdelphij unsigned char *out; 132296341Sdelphij const unsigned char *p; 133296341Sdelphij void *ret; 134296341Sdelphij int outlen; 13555714Skris 136296341Sdelphij if (!PKCS12_pbe_crypt(algor, pass, passlen, oct->data, oct->length, 137296341Sdelphij &out, &outlen, 0)) { 138296341Sdelphij PKCS12err(PKCS12_F_PKCS12_ITEM_DECRYPT_D2I, 139296341Sdelphij PKCS12_R_PKCS12_PBE_CRYPT_ERROR); 140296341Sdelphij return NULL; 141296341Sdelphij } 142296341Sdelphij p = out; 14355714Skris#ifdef DEBUG_DECRYPT 144296341Sdelphij { 145296341Sdelphij FILE *op; 14655714Skris 147296341Sdelphij char fname[30]; 148296341Sdelphij static int fnm = 1; 149296341Sdelphij sprintf(fname, "DER%d", fnm++); 150296341Sdelphij op = fopen(fname, "wb"); 151296341Sdelphij fwrite(p, 1, outlen, op); 152296341Sdelphij fclose(op); 153296341Sdelphij } 15455714Skris#endif 155296341Sdelphij ret = ASN1_item_d2i(NULL, &p, outlen, it); 156296341Sdelphij if (zbuf) 157296341Sdelphij OPENSSL_cleanse(out, outlen); 158296341Sdelphij if (!ret) 159296341Sdelphij PKCS12err(PKCS12_F_PKCS12_ITEM_DECRYPT_D2I, PKCS12_R_DECODE_ERROR); 160296341Sdelphij OPENSSL_free(out); 161296341Sdelphij return ret; 16255714Skris} 16355714Skris 164296341Sdelphij/* 165296341Sdelphij * Encode ASN1 structure and encrypt, return OCTET STRING if zbuf set zero 166296341Sdelphij * encoding. 16755714Skris */ 16855714Skris 169296341SdelphijASN1_OCTET_STRING *PKCS12_item_i2d_encrypt(X509_ALGOR *algor, 170296341Sdelphij const ASN1_ITEM *it, 171296341Sdelphij const char *pass, int passlen, 172296341Sdelphij void *obj, int zbuf) 17355714Skris{ 174296341Sdelphij ASN1_OCTET_STRING *oct; 175296341Sdelphij unsigned char *in = NULL; 176296341Sdelphij int inlen; 177296341Sdelphij if (!(oct = M_ASN1_OCTET_STRING_new())) { 178296341Sdelphij PKCS12err(PKCS12_F_PKCS12_ITEM_I2D_ENCRYPT, ERR_R_MALLOC_FAILURE); 179296341Sdelphij return NULL; 180296341Sdelphij } 181296341Sdelphij inlen = ASN1_item_i2d(obj, &in, it); 182296341Sdelphij if (!in) { 183296341Sdelphij PKCS12err(PKCS12_F_PKCS12_ITEM_I2D_ENCRYPT, PKCS12_R_ENCODE_ERROR); 184296341Sdelphij return NULL; 185296341Sdelphij } 186296341Sdelphij if (!PKCS12_pbe_crypt(algor, pass, passlen, in, inlen, &oct->data, 187296341Sdelphij &oct->length, 1)) { 188296341Sdelphij PKCS12err(PKCS12_F_PKCS12_ITEM_I2D_ENCRYPT, PKCS12_R_ENCRYPT_ERROR); 189296341Sdelphij OPENSSL_free(in); 190296341Sdelphij return NULL; 191296341Sdelphij } 192296341Sdelphij if (zbuf) 193296341Sdelphij OPENSSL_cleanse(in, inlen); 194296341Sdelphij OPENSSL_free(in); 195296341Sdelphij return oct; 19655714Skris} 19768651Skris 19868651SkrisIMPLEMENT_PKCS12_STACK_OF(PKCS7) 199