2015-01-22 11:40:55 +08:00
|
|
|
/*
|
2020-04-23 20:55:52 +08:00
|
|
|
* Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
|
1999-06-05 07:32:14 +08:00
|
|
|
*
|
2018-12-06 20:40:06 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:24:46 +08:00
|
|
|
* 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-05 07:32:14 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
1999-06-05 07:32:14 +08:00
|
|
|
#include <openssl/x509.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
/*
|
|
|
|
* Doesn't do anything now: Builtin PBE algorithms in static table.
|
1999-06-05 07:32:14 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
void PKCS5_PBE_add(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
1999-06-07 02:41:52 +08:00
|
|
|
int PKCS5_PBE_keyivgen(EVP_CIPHER_CTX *cctx, const char *pass, int passlen,
|
2015-01-22 11:40:55 +08:00
|
|
|
ASN1_TYPE *param, const EVP_CIPHER *cipher,
|
|
|
|
const EVP_MD *md, int en_de)
|
1999-06-05 07:32:14 +08:00
|
|
|
{
|
2015-11-27 21:17:50 +08:00
|
|
|
EVP_MD_CTX *ctx;
|
2015-01-22 11:40:55 +08:00
|
|
|
unsigned char md_tmp[EVP_MAX_MD_SIZE];
|
|
|
|
unsigned char key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH];
|
2019-05-07 08:42:58 +08:00
|
|
|
int i, ivl, kl;
|
2015-01-22 11:40:55 +08:00
|
|
|
PBEPARAM *pbe;
|
|
|
|
int saltlen, iter;
|
|
|
|
unsigned char *salt;
|
|
|
|
int mdsize;
|
|
|
|
int rv = 0;
|
1999-06-06 21:07:13 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
/* Extract useful info from parameter */
|
|
|
|
if (param == NULL || param->type != V_ASN1_SEQUENCE ||
|
|
|
|
param->value.sequence == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2005-07-27 05:10:34 +08:00
|
|
|
|
2015-03-28 23:10:54 +08:00
|
|
|
pbe = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBEPARAM), param);
|
|
|
|
if (pbe == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|
1999-06-06 21:07:13 +08:00
|
|
|
|
2019-05-07 08:42:58 +08:00
|
|
|
ivl = EVP_CIPHER_iv_length(cipher);
|
|
|
|
if (ivl < 0 || ivl > 16) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_IV_LENGTH);
|
2020-01-12 16:35:39 +08:00
|
|
|
PBEPARAM_free(pbe);
|
2019-05-07 08:42:58 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
kl = EVP_CIPHER_key_length(cipher);
|
|
|
|
if (kl < 0 || kl > (int)sizeof(md_tmp)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
|
2020-01-12 16:35:39 +08:00
|
|
|
PBEPARAM_free(pbe);
|
2019-05-07 08:42:58 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-09-17 03:28:57 +08:00
|
|
|
if (pbe->iter == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
iter = 1;
|
|
|
|
else
|
|
|
|
iter = ASN1_INTEGER_get(pbe->iter);
|
|
|
|
salt = pbe->salt->data;
|
|
|
|
saltlen = pbe->salt->length;
|
1999-06-06 21:07:13 +08:00
|
|
|
|
2019-09-17 03:28:57 +08:00
|
|
|
if (pass == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
passlen = 0;
|
|
|
|
else if (passlen == -1)
|
|
|
|
passlen = strlen(pass);
|
2000-05-04 08:08:35 +08:00
|
|
|
|
2015-12-02 07:49:35 +08:00
|
|
|
ctx = EVP_MD_CTX_new();
|
2015-11-27 21:17:50 +08:00
|
|
|
if (ctx == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2015-11-27 21:17:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!EVP_DigestInit_ex(ctx, md, NULL))
|
|
|
|
goto err;
|
|
|
|
if (!EVP_DigestUpdate(ctx, pass, passlen))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2015-11-27 21:17:50 +08:00
|
|
|
if (!EVP_DigestUpdate(ctx, salt, saltlen))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
PBEPARAM_free(pbe);
|
2020-01-12 16:35:39 +08:00
|
|
|
pbe = NULL;
|
2015-11-27 21:17:50 +08:00
|
|
|
if (!EVP_DigestFinal_ex(ctx, md_tmp, NULL))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
mdsize = EVP_MD_size(md);
|
|
|
|
if (mdsize < 0)
|
2020-09-14 05:44:45 +08:00
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
for (i = 1; i < iter; i++) {
|
2015-11-27 21:17:50 +08:00
|
|
|
if (!EVP_DigestInit_ex(ctx, md, NULL))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2015-11-27 21:17:50 +08:00
|
|
|
if (!EVP_DigestUpdate(ctx, md_tmp, mdsize))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2015-11-27 21:17:50 +08:00
|
|
|
if (!EVP_DigestFinal_ex(ctx, md_tmp, NULL))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2019-05-07 08:42:58 +08:00
|
|
|
memcpy(key, md_tmp, kl);
|
|
|
|
memcpy(iv, md_tmp + (16 - ivl), ivl);
|
2015-01-22 11:40:55 +08:00
|
|
|
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:
|
2020-01-12 16:35:39 +08:00
|
|
|
PBEPARAM_free(pbe);
|
2015-12-02 07:49:35 +08:00
|
|
|
EVP_MD_CTX_free(ctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
return rv;
|
1999-06-05 07:32:14 +08:00
|
|
|
}
|