1999-04-23 15:01:15 +00:00
|
|
|
/* evp_pbe.c */
|
2008-11-05 18:36:57 +00:00
|
|
|
/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
|
1999-03-28 17:46:10 +00:00
|
|
|
* project 1999.
|
|
|
|
*/
|
|
|
|
/* ====================================================================
|
|
|
|
* Copyright (c) 1999 The OpenSSL Project. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* 3. All advertising materials mentioning features or use of this
|
|
|
|
* software must display the following acknowledgment:
|
|
|
|
* "This product includes software developed by the OpenSSL Project
|
|
|
|
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
|
|
|
|
*
|
|
|
|
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
|
|
|
|
* endorse or promote products derived from this software without
|
|
|
|
* prior written permission. For written permission, please contact
|
|
|
|
* licensing@OpenSSL.org.
|
|
|
|
*
|
|
|
|
* 5. Products derived from this software may not be called "OpenSSL"
|
|
|
|
* nor may "OpenSSL" appear in their names without prior written
|
|
|
|
* permission of the OpenSSL Project.
|
|
|
|
*
|
|
|
|
* 6. Redistributions of any form whatsoever must retain the following
|
|
|
|
* acknowledgment:
|
|
|
|
* "This product includes software developed by the OpenSSL Project
|
|
|
|
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
|
|
|
|
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
|
|
|
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
* ====================================================================
|
|
|
|
*
|
|
|
|
* This product includes cryptographic software written by Eric Young
|
|
|
|
* (eay@cryptsoft.com). This product includes software written by Tim
|
|
|
|
* Hudson (tjh@cryptsoft.com).
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2002-07-10 07:01:54 +00:00
|
|
|
#include "cryptlib.h"
|
1999-04-23 22:13:45 +00:00
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/x509.h>
|
1999-03-28 17:46:10 +00:00
|
|
|
|
|
|
|
/* Password based encryption (PBE) functions */
|
|
|
|
|
|
|
|
static STACK *pbe_algs;
|
|
|
|
|
|
|
|
/* Setup a cipher context from a PBE algorithm */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int pbe_nid;
|
2001-03-09 02:51:02 +00:00
|
|
|
const EVP_CIPHER *cipher;
|
|
|
|
const EVP_MD *md;
|
1999-03-28 17:46:10 +00:00
|
|
|
EVP_PBE_KEYGEN *keygen;
|
|
|
|
} EVP_PBE_CTL;
|
|
|
|
|
2005-05-11 03:45:39 +00:00
|
|
|
int EVP_PBE_CipherInit(ASN1_OBJECT *pbe_obj, const char *pass, int passlen,
|
1999-06-06 13:07:13 +00:00
|
|
|
ASN1_TYPE *param, EVP_CIPHER_CTX *ctx, int en_de)
|
1999-03-28 17:46:10 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
EVP_PBE_CTL *pbetmp, pbelu;
|
|
|
|
int i;
|
|
|
|
pbelu.pbe_nid = OBJ_obj2nid(pbe_obj);
|
1999-04-27 00:36:20 +00:00
|
|
|
if (pbelu.pbe_nid != NID_undef) i = sk_find(pbe_algs, (char *)&pbelu);
|
1999-03-28 17:46:10 +00:00
|
|
|
else i = -1;
|
|
|
|
|
|
|
|
if (i == -1) {
|
|
|
|
char obj_tmp[80];
|
|
|
|
EVPerr(EVP_F_EVP_PBE_CIPHERINIT,EVP_R_UNKNOWN_PBE_ALGORITHM);
|
2003-12-27 14:40:17 +00:00
|
|
|
if (!pbe_obj) BUF_strlcpy (obj_tmp, "NULL", sizeof obj_tmp);
|
2002-11-13 15:43:43 +00:00
|
|
|
else i2t_ASN1_OBJECT(obj_tmp, sizeof obj_tmp, pbe_obj);
|
1999-03-28 17:46:10 +00:00
|
|
|
ERR_add_error_data(2, "TYPE=", obj_tmp);
|
|
|
|
return 0;
|
|
|
|
}
|
2000-05-04 00:08:35 +00:00
|
|
|
if(!pass) passlen = 0;
|
|
|
|
else if (passlen == -1) passlen = strlen(pass);
|
1999-03-28 17:46:10 +00:00
|
|
|
pbetmp = (EVP_PBE_CTL *)sk_value (pbe_algs, i);
|
1999-06-06 18:41:52 +00:00
|
|
|
i = (*pbetmp->keygen)(ctx, pass, passlen, param, pbetmp->cipher,
|
|
|
|
pbetmp->md, en_de);
|
1999-03-28 17:46:10 +00:00
|
|
|
if (!i) {
|
|
|
|
EVPerr(EVP_F_EVP_PBE_CIPHERINIT,EVP_R_KEYGEN_FAILURE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2000-06-21 02:25:30 +00:00
|
|
|
static int pbe_cmp(const char * const *a, const char * const *b)
|
1999-03-28 17:46:10 +00:00
|
|
|
{
|
2005-04-05 10:29:43 +00:00
|
|
|
const EVP_PBE_CTL * const *pbe1 = (const EVP_PBE_CTL * const *) a,
|
|
|
|
* const *pbe2 = (const EVP_PBE_CTL * const *)b;
|
1999-03-28 17:46:10 +00:00
|
|
|
return ((*pbe1)->pbe_nid - (*pbe2)->pbe_nid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add a PBE algorithm */
|
|
|
|
|
2001-03-09 02:51:02 +00:00
|
|
|
int EVP_PBE_alg_add(int nid, const EVP_CIPHER *cipher, const EVP_MD *md,
|
1999-04-19 21:31:43 +00:00
|
|
|
EVP_PBE_KEYGEN *keygen)
|
1999-03-28 17:46:10 +00:00
|
|
|
{
|
|
|
|
EVP_PBE_CTL *pbe_tmp;
|
2000-06-21 02:25:30 +00:00
|
|
|
if (!pbe_algs) pbe_algs = sk_new(pbe_cmp);
|
2000-06-01 22:19:21 +00:00
|
|
|
if (!(pbe_tmp = (EVP_PBE_CTL*) OPENSSL_malloc (sizeof(EVP_PBE_CTL)))) {
|
1999-03-28 17:46:10 +00:00
|
|
|
EVPerr(EVP_F_EVP_PBE_ALG_ADD,ERR_R_MALLOC_FAILURE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
pbe_tmp->pbe_nid = nid;
|
|
|
|
pbe_tmp->cipher = cipher;
|
|
|
|
pbe_tmp->md = md;
|
|
|
|
pbe_tmp->keygen = keygen;
|
|
|
|
sk_push (pbe_algs, (char *)pbe_tmp);
|
|
|
|
return 1;
|
|
|
|
}
|
1999-03-29 17:50:26 +00:00
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
void EVP_PBE_cleanup(void)
|
1999-03-29 17:50:26 +00:00
|
|
|
{
|
2000-06-01 22:19:21 +00:00
|
|
|
sk_pop_free(pbe_algs, OPENSSL_freeFunc);
|
1999-04-27 00:36:20 +00:00
|
|
|
pbe_algs = NULL;
|
1999-03-29 17:50:26 +00:00
|
|
|
}
|