2015-01-22 03:40:55 +00:00
|
|
|
/*
|
2016-05-17 18:24:46 +00:00
|
|
|
* Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
|
1999-06-04 23:32:14 +00:00
|
|
|
*
|
2016-05-17 18:24:46 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1999-06-04 23:32:14 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
1999-06-04 23:32:14 +00:00
|
|
|
#include <openssl/x509.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* Doesn't do anything now: Builtin PBE algorithms in static table.
|
1999-06-04 23:32:14 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
void PKCS5_PBE_add(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
1999-06-06 18:41:52 +00:00
|
|
|
int PKCS5_PBE_keyivgen(EVP_CIPHER_CTX *cctx, const char *pass, int passlen,
|
2015-01-22 03:40:55 +00:00
|
|
|
ASN1_TYPE *param, const EVP_CIPHER *cipher,
|
|
|
|
const EVP_MD *md, int en_de)
|
1999-06-04 23:32:14 +00:00
|
|
|
{
|
2015-11-27 13:17:50 +00:00
|
|
|
EVP_MD_CTX *ctx;
|
2015-01-22 03:40:55 +00:00
|
|
|
unsigned char md_tmp[EVP_MAX_MD_SIZE];
|
|
|
|
unsigned char key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH];
|
|
|
|
int i;
|
|
|
|
PBEPARAM *pbe;
|
|
|
|
int saltlen, iter;
|
|
|
|
unsigned char *salt;
|
|
|
|
int mdsize;
|
|
|
|
int rv = 0;
|
1999-06-06 13:07:13 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/* Extract useful info from parameter */
|
|
|
|
if (param == NULL || param->type != V_ASN1_SEQUENCE ||
|
|
|
|
param->value.sequence == NULL) {
|
|
|
|
EVPerr(EVP_F_PKCS5_PBE_KEYIVGEN, EVP_R_DECODE_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-07-26 21:10:34 +00:00
|
|
|
|
2015-03-28 15:10:54 +00:00
|
|
|
pbe = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBEPARAM), param);
|
|
|
|
if (pbe == NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
EVPerr(EVP_F_PKCS5_PBE_KEYIVGEN, EVP_R_DECODE_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
1999-06-06 13:07:13 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!pbe->iter)
|
|
|
|
iter = 1;
|
|
|
|
else
|
|
|
|
iter = ASN1_INTEGER_get(pbe->iter);
|
|
|
|
salt = pbe->salt->data;
|
|
|
|
saltlen = pbe->salt->length;
|
1999-06-06 13:07:13 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!pass)
|
|
|
|
passlen = 0;
|
|
|
|
else if (passlen == -1)
|
|
|
|
passlen = strlen(pass);
|
2000-05-04 00:08:35 +00:00
|
|
|
|
2015-12-01 23:49:35 +00:00
|
|
|
ctx = EVP_MD_CTX_new();
|
2015-11-27 13:17:50 +00:00
|
|
|
if (ctx == NULL) {
|
|
|
|
EVPerr(EVP_F_PKCS5_PBE_KEYIVGEN, ERR_R_MALLOC_FAILURE);
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
2015-11-27 13:17:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!EVP_DigestInit_ex(ctx, md, NULL))
|
|
|
|
goto err;
|
|
|
|
if (!EVP_DigestUpdate(ctx, pass, passlen))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
2015-11-27 13:17:50 +00:00
|
|
|
if (!EVP_DigestUpdate(ctx, salt, saltlen))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
PBEPARAM_free(pbe);
|
2015-11-27 13:17:50 +00:00
|
|
|
if (!EVP_DigestFinal_ex(ctx, md_tmp, NULL))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
mdsize = EVP_MD_size(md);
|
|
|
|
if (mdsize < 0)
|
|
|
|
return 0;
|
|
|
|
for (i = 1; i < iter; i++) {
|
2015-11-27 13:17:50 +00:00
|
|
|
if (!EVP_DigestInit_ex(ctx, md, NULL))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
2015-11-27 13:17:50 +00:00
|
|
|
if (!EVP_DigestUpdate(ctx, md_tmp, mdsize))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
2015-11-27 13:17:50 +00:00
|
|
|
if (!EVP_DigestFinal_ex(ctx, md_tmp, NULL))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
OPENSSL_assert(EVP_CIPHER_key_length(cipher) <= (int)sizeof(md_tmp));
|
|
|
|
memcpy(key, md_tmp, EVP_CIPHER_key_length(cipher));
|
|
|
|
OPENSSL_assert(EVP_CIPHER_iv_length(cipher) <= 16);
|
|
|
|
memcpy(iv, md_tmp + (16 - EVP_CIPHER_iv_length(cipher)),
|
|
|
|
EVP_CIPHER_iv_length(cipher));
|
|
|
|
if (!EVP_CipherInit_ex(cctx, cipher, NULL, key, iv, en_de))
|
|
|
|
goto err;
|
|
|
|
OPENSSL_cleanse(md_tmp, EVP_MAX_MD_SIZE);
|
|
|
|
OPENSSL_cleanse(key, EVP_MAX_KEY_LENGTH);
|
|
|
|
OPENSSL_cleanse(iv, EVP_MAX_IV_LENGTH);
|
|
|
|
rv = 1;
|
|
|
|
err:
|
2015-12-01 23:49:35 +00:00
|
|
|
EVP_MD_CTX_free(ctx);
|
2015-01-22 03:40:55 +00:00
|
|
|
return rv;
|
1999-06-04 23:32:14 +00:00
|
|
|
}
|