evp_pbe.c revision 68651
1193323Sed/* evp_pbe.c */
2193323Sed/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3193323Sed * project 1999.
4193323Sed */
5193323Sed/* ====================================================================
6193323Sed * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
7193323Sed *
8193323Sed * Redistribution and use in source and binary forms, with or without
9193323Sed * modification, are permitted provided that the following conditions
10193323Sed * are met:
11193323Sed *
12193323Sed * 1. Redistributions of source code must retain the above copyright
13193323Sed *    notice, this list of conditions and the following disclaimer.
14193323Sed *
15193323Sed * 2. Redistributions in binary form must reproduce the above copyright
16193323Sed *    notice, this list of conditions and the following disclaimer in
17193323Sed *    the documentation and/or other materials provided with the
18193323Sed *    distribution.
19193323Sed *
20235633Sdim * 3. All advertising materials mentioning features or use of this
21193323Sed *    software must display the following acknowledgment:
22193323Sed *    "This product includes software developed by the OpenSSL Project
23193323Sed *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24193323Sed *
25252723Sdim * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26252723Sdim *    endorse or promote products derived from this software without
27193323Sed *    prior written permission. For written permission, please contact
28193323Sed *    licensing@OpenSSL.org.
29193323Sed *
30193323Sed * 5. Products derived from this software may not be called "OpenSSL"
31193323Sed *    nor may "OpenSSL" appear in their names without prior written
32193323Sed *    permission of the OpenSSL Project.
33193323Sed *
34193323Sed * 6. Redistributions of any form whatsoever must retain the following
35193323Sed *    acknowledgment:
36193323Sed *    "This product includes software developed by the OpenSSL Project
37193323Sed *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38193323Sed *
39193323Sed * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40198090Srdivacky * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41198090Srdivacky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42193323Sed * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43193323Sed * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44193323Sed * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45193323Sed * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46193323Sed * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47193323Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48193323Sed * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49193323Sed * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50193323Sed * OF THE POSSIBILITY OF SUCH DAMAGE.
51193323Sed * ====================================================================
52193323Sed *
53193323Sed * This product includes cryptographic software written by Eric Young
54193323Sed * (eay@cryptsoft.com).  This product includes software written by Tim
55193323Sed * Hudson (tjh@cryptsoft.com).
56193323Sed *
57193323Sed */
58235633Sdim
59193323Sed#include <stdio.h>
60193323Sed#include <openssl/evp.h>
61193323Sed#include <openssl/x509.h>
62193323Sed#include "cryptlib.h"
63193323Sed
64193323Sed/* Password based encryption (PBE) functions */
65193323Sed
66193323Sedstatic STACK *pbe_algs;
67193323Sed
68193323Sed/* Setup a cipher context from a PBE algorithm */
69193323Sed
70193323Sedtypedef struct {
71193323Sedint pbe_nid;
72193323SedEVP_CIPHER *cipher;
73193323SedEVP_MD *md;
74193323SedEVP_PBE_KEYGEN *keygen;
75193323Sed} EVP_PBE_CTL;
76193323Sed
77193323Sedint EVP_PBE_CipherInit (ASN1_OBJECT *pbe_obj, const char *pass, int passlen,
78193323Sed	     ASN1_TYPE *param, EVP_CIPHER_CTX *ctx, int en_de)
79193323Sed{
80193323Sed
81193323Sed	EVP_PBE_CTL *pbetmp, pbelu;
82193323Sed	int i;
83193323Sed	pbelu.pbe_nid = OBJ_obj2nid(pbe_obj);
84193323Sed	if (pbelu.pbe_nid != NID_undef) i = sk_find(pbe_algs, (char *)&pbelu);
85193323Sed	else i = -1;
86193323Sed
87193323Sed	if (i == -1) {
88193323Sed		char obj_tmp[80];
89193323Sed		EVPerr(EVP_F_EVP_PBE_CIPHERINIT,EVP_R_UNKNOWN_PBE_ALGORITHM);
90193323Sed		if (!pbe_obj) strcpy (obj_tmp, "NULL");
91193323Sed		else i2t_ASN1_OBJECT(obj_tmp, 80, pbe_obj);
92252723Sdim		ERR_add_error_data(2, "TYPE=", obj_tmp);
93252723Sdim		return 0;
94252723Sdim	}
95252723Sdim	if(!pass) passlen = 0;
96252723Sdim	else if (passlen == -1) passlen = strlen(pass);
97252723Sdim	pbetmp = (EVP_PBE_CTL *)sk_value (pbe_algs, i);
98252723Sdim	i = (*pbetmp->keygen)(ctx, pass, passlen, param, pbetmp->cipher,
99252723Sdim						 pbetmp->md, en_de);
100252723Sdim	if (!i) {
101193323Sed		EVPerr(EVP_F_EVP_PBE_CIPHERINIT,EVP_R_KEYGEN_FAILURE);
102193323Sed		return 0;
103193323Sed	}
104193323Sed	return 1;
105193323Sed}
106193323Sed
107193323Sedstatic int pbe_cmp(const char * const *a, const char * const *b)
108193323Sed{
109193323Sed	EVP_PBE_CTL **pbe1 = (EVP_PBE_CTL **) a,  **pbe2 = (EVP_PBE_CTL **)b;
110193323Sed	return ((*pbe1)->pbe_nid - (*pbe2)->pbe_nid);
111193323Sed}
112193323Sed
113193323Sed/* Add a PBE algorithm */
114193323Sed
115193323Sedint EVP_PBE_alg_add (int nid, EVP_CIPHER *cipher, EVP_MD *md,
116193323Sed	     EVP_PBE_KEYGEN *keygen)
117193323Sed{
118193323Sed	EVP_PBE_CTL *pbe_tmp;
119193323Sed	if (!pbe_algs) pbe_algs = sk_new(pbe_cmp);
120193323Sed	if (!(pbe_tmp = (EVP_PBE_CTL*) OPENSSL_malloc (sizeof(EVP_PBE_CTL)))) {
121193323Sed		EVPerr(EVP_F_EVP_PBE_ALG_ADD,ERR_R_MALLOC_FAILURE);
122193323Sed		return 0;
123193323Sed	}
124193323Sed	pbe_tmp->pbe_nid = nid;
125193323Sed	pbe_tmp->cipher = cipher;
126193323Sed	pbe_tmp->md = md;
127193323Sed	pbe_tmp->keygen = keygen;
128193323Sed	sk_push (pbe_algs, (char *)pbe_tmp);
129193323Sed	return 1;
130}
131
132void EVP_PBE_cleanup(void)
133{
134	sk_pop_free(pbe_algs, OPENSSL_freeFunc);
135	pbe_algs = NULL;
136}
137