p5_pbe.c revision 55714
155714Skris/* p5_pbe.c */
255714Skris/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
355714Skris * project 1999.
455714Skris */
555714Skris/* ====================================================================
655714Skris * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
755714Skris *
855714Skris * Redistribution and use in source and binary forms, with or without
955714Skris * modification, are permitted provided that the following conditions
1055714Skris * are met:
1155714Skris *
1255714Skris * 1. Redistributions of source code must retain the above copyright
1355714Skris *    notice, this list of conditions and the following disclaimer.
1455714Skris *
1555714Skris * 2. Redistributions in binary form must reproduce the above copyright
1655714Skris *    notice, this list of conditions and the following disclaimer in
1755714Skris *    the documentation and/or other materials provided with the
1855714Skris *    distribution.
1955714Skris *
2055714Skris * 3. All advertising materials mentioning features or use of this
2155714Skris *    software must display the following acknowledgment:
2255714Skris *    "This product includes software developed by the OpenSSL Project
2355714Skris *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
2455714Skris *
2555714Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
2655714Skris *    endorse or promote products derived from this software without
2755714Skris *    prior written permission. For written permission, please contact
2855714Skris *    licensing@OpenSSL.org.
2955714Skris *
3055714Skris * 5. Products derived from this software may not be called "OpenSSL"
3155714Skris *    nor may "OpenSSL" appear in their names without prior written
3255714Skris *    permission of the OpenSSL Project.
3355714Skris *
3455714Skris * 6. Redistributions of any form whatsoever must retain the following
3555714Skris *    acknowledgment:
3655714Skris *    "This product includes software developed by the OpenSSL Project
3755714Skris *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
3855714Skris *
3955714Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
4055714Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4155714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
4255714Skris * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
4355714Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4455714Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4555714Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
4655714Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4755714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4855714Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4955714Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
5055714Skris * OF THE POSSIBILITY OF SUCH DAMAGE.
5155714Skris * ====================================================================
5255714Skris *
5355714Skris * This product includes cryptographic software written by Eric Young
5455714Skris * (eay@cryptsoft.com).  This product includes software written by Tim
5555714Skris * Hudson (tjh@cryptsoft.com).
5655714Skris *
5755714Skris */
5855714Skris
5955714Skris#include <stdio.h>
6055714Skris#include "cryptlib.h"
6155714Skris#include <openssl/asn1_mac.h>
6255714Skris#include <openssl/x509.h>
6355714Skris#include <openssl/rand.h>
6455714Skris
6555714Skris/* PKCS#5 password based encryption structure */
6655714Skris
6755714Skrisint i2d_PBEPARAM(PBEPARAM *a, unsigned char **pp)
6855714Skris{
6955714Skris	M_ASN1_I2D_vars(a);
7055714Skris	M_ASN1_I2D_len (a->salt, i2d_ASN1_OCTET_STRING);
7155714Skris	M_ASN1_I2D_len (a->iter, i2d_ASN1_INTEGER);
7255714Skris
7355714Skris	M_ASN1_I2D_seq_total ();
7455714Skris
7555714Skris	M_ASN1_I2D_put (a->salt, i2d_ASN1_OCTET_STRING);
7655714Skris	M_ASN1_I2D_put (a->iter, i2d_ASN1_INTEGER);
7755714Skris	M_ASN1_I2D_finish();
7855714Skris}
7955714Skris
8055714SkrisPBEPARAM *PBEPARAM_new(void)
8155714Skris{
8255714Skris	PBEPARAM *ret=NULL;
8355714Skris	ASN1_CTX c;
8455714Skris	M_ASN1_New_Malloc(ret, PBEPARAM);
8555714Skris	M_ASN1_New(ret->iter,ASN1_INTEGER_new);
8655714Skris	M_ASN1_New(ret->salt,ASN1_OCTET_STRING_new);
8755714Skris	return (ret);
8855714Skris	M_ASN1_New_Error(ASN1_F_PBEPARAM_NEW);
8955714Skris}
9055714Skris
9155714SkrisPBEPARAM *d2i_PBEPARAM(PBEPARAM **a, unsigned char **pp, long length)
9255714Skris{
9355714Skris	M_ASN1_D2I_vars(a,PBEPARAM *,PBEPARAM_new);
9455714Skris	M_ASN1_D2I_Init();
9555714Skris	M_ASN1_D2I_start_sequence();
9655714Skris	M_ASN1_D2I_get (ret->salt, d2i_ASN1_OCTET_STRING);
9755714Skris	M_ASN1_D2I_get (ret->iter, d2i_ASN1_INTEGER);
9855714Skris	M_ASN1_D2I_Finish(a, PBEPARAM_free, ASN1_F_D2I_PBEPARAM);
9955714Skris}
10055714Skris
10155714Skrisvoid PBEPARAM_free (PBEPARAM *a)
10255714Skris{
10355714Skris	if(a==NULL) return;
10455714Skris	ASN1_OCTET_STRING_free(a->salt);
10555714Skris	ASN1_INTEGER_free (a->iter);
10655714Skris	Free ((char *)a);
10755714Skris}
10855714Skris
10955714Skris/* Return an algorithm identifier for a PKCS#5 PBE algorithm */
11055714Skris
11155714SkrisX509_ALGOR *PKCS5_pbe_set(int alg, int iter, unsigned char *salt,
11255714Skris	     int saltlen)
11355714Skris{
11455714Skris	PBEPARAM *pbe;
11555714Skris	ASN1_OBJECT *al;
11655714Skris	X509_ALGOR *algor;
11755714Skris	ASN1_TYPE *astype;
11855714Skris
11955714Skris	if (!(pbe = PBEPARAM_new ())) {
12055714Skris		ASN1err(ASN1_F_ASN1_PBE_SET,ERR_R_MALLOC_FAILURE);
12155714Skris		return NULL;
12255714Skris	}
12355714Skris	if(iter <= 0) iter = PKCS5_DEFAULT_ITER;
12455714Skris	ASN1_INTEGER_set (pbe->iter, iter);
12555714Skris	if (!saltlen) saltlen = PKCS5_SALT_LEN;
12655714Skris	if (!(pbe->salt->data = Malloc (saltlen))) {
12755714Skris		ASN1err(ASN1_F_ASN1_PBE_SET,ERR_R_MALLOC_FAILURE);
12855714Skris		return NULL;
12955714Skris	}
13055714Skris	pbe->salt->length = saltlen;
13155714Skris	if (salt) memcpy (pbe->salt->data, salt, saltlen);
13255714Skris	else RAND_bytes (pbe->salt->data, saltlen);
13355714Skris
13455714Skris	if (!(astype = ASN1_TYPE_new())) {
13555714Skris		ASN1err(ASN1_F_ASN1_PBE_SET,ERR_R_MALLOC_FAILURE);
13655714Skris		return NULL;
13755714Skris	}
13855714Skris
13955714Skris	astype->type = V_ASN1_SEQUENCE;
14055714Skris	if(!ASN1_pack_string(pbe, i2d_PBEPARAM, &astype->value.sequence)) {
14155714Skris		ASN1err(ASN1_F_ASN1_PBE_SET,ERR_R_MALLOC_FAILURE);
14255714Skris		return NULL;
14355714Skris	}
14455714Skris	PBEPARAM_free (pbe);
14555714Skris
14655714Skris	al = OBJ_nid2obj(alg); /* never need to free al */
14755714Skris	if (!(algor = X509_ALGOR_new())) {
14855714Skris		ASN1err(ASN1_F_ASN1_PBE_SET,ERR_R_MALLOC_FAILURE);
14955714Skris		return NULL;
15055714Skris	}
15155714Skris	ASN1_OBJECT_free(algor->algorithm);
15255714Skris	algor->algorithm = al;
15355714Skris	algor->parameter = astype;
15455714Skris
15555714Skris	return (algor);
15655714Skris}
157