p5_pbe.c revision 109998
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"
61109998Smarkm#include <openssl/asn1t.h>
6255714Skris#include <openssl/x509.h>
6355714Skris#include <openssl/rand.h>
6455714Skris
6555714Skris/* PKCS#5 password based encryption structure */
6655714Skris
67109998SmarkmASN1_SEQUENCE(PBEPARAM) = {
68109998Smarkm	ASN1_SIMPLE(PBEPARAM, salt, ASN1_OCTET_STRING),
69109998Smarkm	ASN1_SIMPLE(PBEPARAM, iter, ASN1_INTEGER)
70109998Smarkm} ASN1_SEQUENCE_END(PBEPARAM)
7155714Skris
72109998SmarkmIMPLEMENT_ASN1_FUNCTIONS(PBEPARAM)
7355714Skris
7455714Skris/* Return an algorithm identifier for a PKCS#5 PBE algorithm */
7555714Skris
7655714SkrisX509_ALGOR *PKCS5_pbe_set(int alg, int iter, unsigned char *salt,
7755714Skris	     int saltlen)
7855714Skris{
7955714Skris	PBEPARAM *pbe;
8055714Skris	ASN1_OBJECT *al;
8155714Skris	X509_ALGOR *algor;
8255714Skris	ASN1_TYPE *astype;
8355714Skris
8455714Skris	if (!(pbe = PBEPARAM_new ())) {
8555714Skris		ASN1err(ASN1_F_ASN1_PBE_SET,ERR_R_MALLOC_FAILURE);
8655714Skris		return NULL;
8755714Skris	}
8855714Skris	if(iter <= 0) iter = PKCS5_DEFAULT_ITER;
8955714Skris	ASN1_INTEGER_set (pbe->iter, iter);
9055714Skris	if (!saltlen) saltlen = PKCS5_SALT_LEN;
9168651Skris	if (!(pbe->salt->data = OPENSSL_malloc (saltlen))) {
9255714Skris		ASN1err(ASN1_F_ASN1_PBE_SET,ERR_R_MALLOC_FAILURE);
9355714Skris		return NULL;
9455714Skris	}
9555714Skris	pbe->salt->length = saltlen;
9655714Skris	if (salt) memcpy (pbe->salt->data, salt, saltlen);
9759191Skris	else if (RAND_pseudo_bytes (pbe->salt->data, saltlen) < 0)
9859191Skris		return NULL;
9955714Skris
10055714Skris	if (!(astype = ASN1_TYPE_new())) {
10155714Skris		ASN1err(ASN1_F_ASN1_PBE_SET,ERR_R_MALLOC_FAILURE);
10255714Skris		return NULL;
10355714Skris	}
10455714Skris
10555714Skris	astype->type = V_ASN1_SEQUENCE;
10655714Skris	if(!ASN1_pack_string(pbe, i2d_PBEPARAM, &astype->value.sequence)) {
10755714Skris		ASN1err(ASN1_F_ASN1_PBE_SET,ERR_R_MALLOC_FAILURE);
10855714Skris		return NULL;
10955714Skris	}
11055714Skris	PBEPARAM_free (pbe);
11155714Skris
11255714Skris	al = OBJ_nid2obj(alg); /* never need to free al */
11355714Skris	if (!(algor = X509_ALGOR_new())) {
11455714Skris		ASN1err(ASN1_F_ASN1_PBE_SET,ERR_R_MALLOC_FAILURE);
11555714Skris		return NULL;
11655714Skris	}
11755714Skris	ASN1_OBJECT_free(algor->algorithm);
11855714Skris	algor->algorithm = al;
11955714Skris	algor->parameter = astype;
12055714Skris
12155714Skris	return (algor);
12255714Skris}
123