10Sstevel@tonic-gate /* p12_decr.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 "cryptlib.h"
610Sstevel@tonic-gate #include <openssl/pkcs12.h>
620Sstevel@tonic-gate
630Sstevel@tonic-gate /* Define this to dump decrypted output to files called DERnnn */
640Sstevel@tonic-gate /*#define DEBUG_DECRYPT*/
650Sstevel@tonic-gate
660Sstevel@tonic-gate
670Sstevel@tonic-gate /* Encrypt/Decrypt a buffer based on password and algor, result in a
680Sstevel@tonic-gate * OPENSSL_malloc'ed buffer
690Sstevel@tonic-gate */
700Sstevel@tonic-gate
PKCS12_pbe_crypt(X509_ALGOR * algor,const char * pass,int passlen,unsigned char * in,int inlen,unsigned char ** data,int * datalen,int en_de)710Sstevel@tonic-gate unsigned char * PKCS12_pbe_crypt(X509_ALGOR *algor, const char *pass,
720Sstevel@tonic-gate int passlen, unsigned char *in, int inlen, unsigned char **data,
730Sstevel@tonic-gate int *datalen, int en_de)
740Sstevel@tonic-gate {
750Sstevel@tonic-gate unsigned char *out;
760Sstevel@tonic-gate int outlen, i;
770Sstevel@tonic-gate EVP_CIPHER_CTX ctx;
780Sstevel@tonic-gate
790Sstevel@tonic-gate EVP_CIPHER_CTX_init(&ctx);
800Sstevel@tonic-gate /* Decrypt data */
810Sstevel@tonic-gate if (!EVP_PBE_CipherInit(algor->algorithm, pass, passlen,
820Sstevel@tonic-gate algor->parameter, &ctx, en_de)) {
830Sstevel@tonic-gate PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT,PKCS12_R_PKCS12_ALGOR_CIPHERINIT_ERROR);
840Sstevel@tonic-gate return NULL;
850Sstevel@tonic-gate }
860Sstevel@tonic-gate
870Sstevel@tonic-gate if(!(out = OPENSSL_malloc(inlen + EVP_CIPHER_CTX_block_size(&ctx)))) {
880Sstevel@tonic-gate PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT,ERR_R_MALLOC_FAILURE);
890Sstevel@tonic-gate goto err;
900Sstevel@tonic-gate }
910Sstevel@tonic-gate
920Sstevel@tonic-gate EVP_CipherUpdate(&ctx, out, &i, in, inlen);
930Sstevel@tonic-gate outlen = i;
940Sstevel@tonic-gate if(!EVP_CipherFinal_ex(&ctx, out + i, &i)) {
950Sstevel@tonic-gate OPENSSL_free(out);
960Sstevel@tonic-gate out = NULL;
970Sstevel@tonic-gate PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT,PKCS12_R_PKCS12_CIPHERFINAL_ERROR);
980Sstevel@tonic-gate goto err;
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate outlen += i;
1010Sstevel@tonic-gate if (datalen) *datalen = outlen;
1020Sstevel@tonic-gate if (data) *data = out;
1030Sstevel@tonic-gate err:
1040Sstevel@tonic-gate EVP_CIPHER_CTX_cleanup(&ctx);
1050Sstevel@tonic-gate return out;
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate /* Decrypt an OCTET STRING and decode ASN1 structure
1100Sstevel@tonic-gate * if zbuf set zero buffer after use.
1110Sstevel@tonic-gate */
1120Sstevel@tonic-gate
PKCS12_item_decrypt_d2i(X509_ALGOR * algor,const ASN1_ITEM * it,const char * pass,int passlen,ASN1_OCTET_STRING * oct,int zbuf)1130Sstevel@tonic-gate void * PKCS12_item_decrypt_d2i(X509_ALGOR *algor, const ASN1_ITEM *it,
1140Sstevel@tonic-gate const char *pass, int passlen, ASN1_OCTET_STRING *oct, int zbuf)
1150Sstevel@tonic-gate {
116*2139Sjp161948 unsigned char *out;
117*2139Sjp161948 const unsigned char *p;
1180Sstevel@tonic-gate void *ret;
1190Sstevel@tonic-gate int outlen;
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate if (!PKCS12_pbe_crypt(algor, pass, passlen, oct->data, oct->length,
1220Sstevel@tonic-gate &out, &outlen, 0)) {
123*2139Sjp161948 PKCS12err(PKCS12_F_PKCS12_ITEM_DECRYPT_D2I,PKCS12_R_PKCS12_PBE_CRYPT_ERROR);
1240Sstevel@tonic-gate return NULL;
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate p = out;
1270Sstevel@tonic-gate #ifdef DEBUG_DECRYPT
1280Sstevel@tonic-gate {
1290Sstevel@tonic-gate FILE *op;
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate char fname[30];
1320Sstevel@tonic-gate static int fnm = 1;
1330Sstevel@tonic-gate sprintf(fname, "DER%d", fnm++);
1340Sstevel@tonic-gate op = fopen(fname, "wb");
1350Sstevel@tonic-gate fwrite (p, 1, outlen, op);
1360Sstevel@tonic-gate fclose(op);
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate #endif
1390Sstevel@tonic-gate ret = ASN1_item_d2i(NULL, &p, outlen, it);
1400Sstevel@tonic-gate if (zbuf) OPENSSL_cleanse(out, outlen);
141*2139Sjp161948 if(!ret) PKCS12err(PKCS12_F_PKCS12_ITEM_DECRYPT_D2I,PKCS12_R_DECODE_ERROR);
1420Sstevel@tonic-gate OPENSSL_free(out);
1430Sstevel@tonic-gate return ret;
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate /* Encode ASN1 structure and encrypt, return OCTET STRING
1470Sstevel@tonic-gate * if zbuf set zero encoding.
1480Sstevel@tonic-gate */
1490Sstevel@tonic-gate
PKCS12_item_i2d_encrypt(X509_ALGOR * algor,const ASN1_ITEM * it,const char * pass,int passlen,void * obj,int zbuf)1500Sstevel@tonic-gate ASN1_OCTET_STRING *PKCS12_item_i2d_encrypt(X509_ALGOR *algor, const ASN1_ITEM *it,
1510Sstevel@tonic-gate const char *pass, int passlen,
1520Sstevel@tonic-gate void *obj, int zbuf)
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate ASN1_OCTET_STRING *oct;
1550Sstevel@tonic-gate unsigned char *in = NULL;
1560Sstevel@tonic-gate int inlen;
1570Sstevel@tonic-gate if (!(oct = M_ASN1_OCTET_STRING_new ())) {
158*2139Sjp161948 PKCS12err(PKCS12_F_PKCS12_ITEM_I2D_ENCRYPT,ERR_R_MALLOC_FAILURE);
1590Sstevel@tonic-gate return NULL;
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate inlen = ASN1_item_i2d(obj, &in, it);
1620Sstevel@tonic-gate if (!in) {
163*2139Sjp161948 PKCS12err(PKCS12_F_PKCS12_ITEM_I2D_ENCRYPT,PKCS12_R_ENCODE_ERROR);
1640Sstevel@tonic-gate return NULL;
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate if (!PKCS12_pbe_crypt(algor, pass, passlen, in, inlen, &oct->data,
1670Sstevel@tonic-gate &oct->length, 1)) {
168*2139Sjp161948 PKCS12err(PKCS12_F_PKCS12_ITEM_I2D_ENCRYPT,PKCS12_R_ENCRYPT_ERROR);
1690Sstevel@tonic-gate OPENSSL_free(in);
1700Sstevel@tonic-gate return NULL;
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate if (zbuf) OPENSSL_cleanse(in, inlen);
1730Sstevel@tonic-gate OPENSSL_free(in);
1740Sstevel@tonic-gate return oct;
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate IMPLEMENT_PKCS12_STACK_OF(PKCS7)
178