evp_pbe.c revision 215697
155714Skris/* evp_pbe.c */ 2194206Ssimon/* Written by Dr Stephen N Henson (steve@openssl.org) 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> 60109998Smarkm#include "cryptlib.h" 6155714Skris#include <openssl/evp.h> 6255714Skris#include <openssl/x509.h> 6355714Skris 6455714Skris/* Password based encryption (PBE) functions */ 6555714Skris 6655714Skrisstatic STACK *pbe_algs; 6755714Skris 6855714Skris/* Setup a cipher context from a PBE algorithm */ 6955714Skris 7055714Skristypedef struct { 7155714Skrisint pbe_nid; 72109998Smarkmconst EVP_CIPHER *cipher; 73109998Smarkmconst EVP_MD *md; 7455714SkrisEVP_PBE_KEYGEN *keygen; 7555714Skris} EVP_PBE_CTL; 7655714Skris 77160814Ssimonint EVP_PBE_CipherInit(ASN1_OBJECT *pbe_obj, const char *pass, int passlen, 7855714Skris ASN1_TYPE *param, EVP_CIPHER_CTX *ctx, int en_de) 7955714Skris{ 8055714Skris 8155714Skris EVP_PBE_CTL *pbetmp, pbelu; 8255714Skris int i; 8355714Skris pbelu.pbe_nid = OBJ_obj2nid(pbe_obj); 8455714Skris if (pbelu.pbe_nid != NID_undef) i = sk_find(pbe_algs, (char *)&pbelu); 8555714Skris else i = -1; 8655714Skris 8755714Skris if (i == -1) { 8855714Skris char obj_tmp[80]; 8955714Skris EVPerr(EVP_F_EVP_PBE_CIPHERINIT,EVP_R_UNKNOWN_PBE_ALGORITHM); 90127128Snectar if (!pbe_obj) BUF_strlcpy (obj_tmp, "NULL", sizeof obj_tmp); 91109998Smarkm else i2t_ASN1_OBJECT(obj_tmp, sizeof obj_tmp, pbe_obj); 9255714Skris ERR_add_error_data(2, "TYPE=", obj_tmp); 9355714Skris return 0; 9455714Skris } 9568651Skris if(!pass) passlen = 0; 9668651Skris else if (passlen == -1) passlen = strlen(pass); 9755714Skris pbetmp = (EVP_PBE_CTL *)sk_value (pbe_algs, i); 9855714Skris i = (*pbetmp->keygen)(ctx, pass, passlen, param, pbetmp->cipher, 9955714Skris pbetmp->md, en_de); 10055714Skris if (!i) { 10155714Skris EVPerr(EVP_F_EVP_PBE_CIPHERINIT,EVP_R_KEYGEN_FAILURE); 10255714Skris return 0; 10355714Skris } 10455714Skris return 1; 10555714Skris} 10655714Skris 10768651Skrisstatic int pbe_cmp(const char * const *a, const char * const *b) 10855714Skris{ 109160814Ssimon const EVP_PBE_CTL * const *pbe1 = (const EVP_PBE_CTL * const *) a, 110160814Ssimon * const *pbe2 = (const EVP_PBE_CTL * const *)b; 11155714Skris return ((*pbe1)->pbe_nid - (*pbe2)->pbe_nid); 11255714Skris} 11355714Skris 11455714Skris/* Add a PBE algorithm */ 11555714Skris 116109998Smarkmint EVP_PBE_alg_add(int nid, const EVP_CIPHER *cipher, const EVP_MD *md, 11755714Skris EVP_PBE_KEYGEN *keygen) 11855714Skris{ 119215697Ssimon EVP_PBE_CTL *pbe_tmp = NULL, pbelu; 120215697Ssimon int i; 121215697Ssimon if (!pbe_algs) 122215697Ssimon { 123215697Ssimon pbe_algs = sk_new(pbe_cmp); 124215697Ssimon if (!pbe_algs) 125215697Ssimon { 126215697Ssimon EVPerr(EVP_F_EVP_PBE_ALG_ADD,ERR_R_MALLOC_FAILURE); 127215697Ssimon return 0; 128215697Ssimon } 129215697Ssimon } 130215697Ssimon else 131215697Ssimon { 132215697Ssimon /* Check if already present */ 133215697Ssimon pbelu.pbe_nid = nid; 134215697Ssimon i = sk_find(pbe_algs, (char *)&pbelu); 135215697Ssimon if (i >= 0) 136215697Ssimon { 137215697Ssimon pbe_tmp = (EVP_PBE_CTL *)sk_value(pbe_algs, i); 138215697Ssimon /* If everything identical leave alone */ 139215697Ssimon if (pbe_tmp->cipher == cipher 140215697Ssimon && pbe_tmp->md == md 141215697Ssimon && pbe_tmp->keygen == keygen) 142215697Ssimon return 1; 143215697Ssimon } 144215697Ssimon } 145215697Ssimon 146215697Ssimon if (!pbe_tmp) 147215697Ssimon { 148215697Ssimon pbe_tmp = OPENSSL_malloc (sizeof(EVP_PBE_CTL)); 149215697Ssimon if (!pbe_tmp) 150215697Ssimon { 151215697Ssimon EVPerr(EVP_F_EVP_PBE_ALG_ADD,ERR_R_MALLOC_FAILURE); 152215697Ssimon return 0; 153215697Ssimon } 154215697Ssimon /* If adding a new PBE, set nid, append and sort */ 155215697Ssimon pbe_tmp->pbe_nid = nid; 156215697Ssimon sk_push (pbe_algs, (char *)pbe_tmp); 157215697Ssimon sk_sort(pbe_algs); 158215697Ssimon } 159215697Ssimon 16055714Skris pbe_tmp->cipher = cipher; 16155714Skris pbe_tmp->md = md; 16255714Skris pbe_tmp->keygen = keygen; 16355714Skris return 1; 16455714Skris} 16555714Skris 16655714Skrisvoid EVP_PBE_cleanup(void) 16755714Skris{ 16868651Skris sk_pop_free(pbe_algs, OPENSSL_freeFunc); 16955714Skris pbe_algs = NULL; 17055714Skris} 171