aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/evp/evp_pbe.c
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/evp/evp_pbe.c')
-rw-r--r--crypto/evp/evp_pbe.c49
1 files changed, 41 insertions, 8 deletions
diff --git a/crypto/evp/evp_pbe.c b/crypto/evp/evp_pbe.c
index 5e830be65fff..766ea42b5aac 100644
--- a/crypto/evp/evp_pbe.c
+++ b/crypto/evp/evp_pbe.c
@@ -116,17 +116,50 @@ static int pbe_cmp(const char * const *a, const char * const *b)
int EVP_PBE_alg_add(int nid, const EVP_CIPHER *cipher, const EVP_MD *md,
EVP_PBE_KEYGEN *keygen)
{
- EVP_PBE_CTL *pbe_tmp;
- if (!pbe_algs) pbe_algs = sk_new(pbe_cmp);
- if (!(pbe_tmp = (EVP_PBE_CTL*) OPENSSL_malloc (sizeof(EVP_PBE_CTL)))) {
- EVPerr(EVP_F_EVP_PBE_ALG_ADD,ERR_R_MALLOC_FAILURE);
- return 0;
- }
- pbe_tmp->pbe_nid = nid;
+ EVP_PBE_CTL *pbe_tmp = NULL, pbelu;
+ int i;
+ if (!pbe_algs)
+ {
+ pbe_algs = sk_new(pbe_cmp);
+ if (!pbe_algs)
+ {
+ EVPerr(EVP_F_EVP_PBE_ALG_ADD,ERR_R_MALLOC_FAILURE);
+ return 0;
+ }
+ }
+ else
+ {
+ /* Check if already present */
+ pbelu.pbe_nid = nid;
+ i = sk_find(pbe_algs, (char *)&pbelu);
+ if (i >= 0)
+ {
+ pbe_tmp = (EVP_PBE_CTL *)sk_value(pbe_algs, i);
+ /* If everything identical leave alone */
+ if (pbe_tmp->cipher == cipher
+ && pbe_tmp->md == md
+ && pbe_tmp->keygen == keygen)
+ return 1;
+ }
+ }
+
+ if (!pbe_tmp)
+ {
+ pbe_tmp = OPENSSL_malloc (sizeof(EVP_PBE_CTL));
+ if (!pbe_tmp)
+ {
+ EVPerr(EVP_F_EVP_PBE_ALG_ADD,ERR_R_MALLOC_FAILURE);
+ return 0;
+ }
+ /* If adding a new PBE, set nid, append and sort */
+ pbe_tmp->pbe_nid = nid;
+ sk_push (pbe_algs, (char *)pbe_tmp);
+ sk_sort(pbe_algs);
+ }
+
pbe_tmp->cipher = cipher;
pbe_tmp->md = md;
pbe_tmp->keygen = keygen;
- sk_push (pbe_algs, (char *)pbe_tmp);
return 1;
}