2015-01-22 11:40:55 +08:00
|
|
|
/*
|
2023-09-07 16:59:15 +08:00
|
|
|
* Copyright 1999-2023 The OpenSSL Project Authors. All Rights Reserved.
|
1999-03-29 01:46:10 +08:00
|
|
|
*
|
2018-12-06 20:17:34 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:51:34 +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-03-29 01:46:10 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
2000-12-09 03:09:35 +08:00
|
|
|
#include <openssl/asn1t.h>
|
1999-07-22 06:10:23 +08:00
|
|
|
#include <openssl/x509.h>
|
1999-04-24 06:13:45 +08:00
|
|
|
#include <openssl/rand.h>
|
2023-08-28 10:47:51 +08:00
|
|
|
#include "crypto/evp.h"
|
1999-03-29 01:46:10 +08:00
|
|
|
|
|
|
|
/* PKCS#5 password based encryption structure */
|
|
|
|
|
2000-12-09 03:09:35 +08:00
|
|
|
ASN1_SEQUENCE(PBEPARAM) = {
|
2015-01-22 11:40:55 +08:00
|
|
|
ASN1_SIMPLE(PBEPARAM, salt, ASN1_OCTET_STRING),
|
|
|
|
ASN1_SIMPLE(PBEPARAM, iter, ASN1_INTEGER)
|
2001-02-23 20:47:06 +08:00
|
|
|
} ASN1_SEQUENCE_END(PBEPARAM)
|
1999-03-29 01:46:10 +08:00
|
|
|
|
2000-12-09 03:09:35 +08:00
|
|
|
IMPLEMENT_ASN1_FUNCTIONS(PBEPARAM)
|
1999-03-29 07:17:34 +08:00
|
|
|
|
2008-03-13 05:14:28 +08:00
|
|
|
/* Set an algorithm identifier for a PKCS#5 PBE algorithm */
|
|
|
|
|
2021-02-17 15:56:36 +08:00
|
|
|
int PKCS5_pbe_set0_algor_ex(X509_ALGOR *algor, int alg, int iter,
|
|
|
|
const unsigned char *salt, int saltlen,
|
|
|
|
OSSL_LIB_CTX *ctx)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
PBEPARAM *pbe = NULL;
|
|
|
|
ASN1_STRING *pbe_str = NULL;
|
2016-08-16 21:06:48 +08:00
|
|
|
unsigned char *sstr = NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
pbe = PBEPARAM_new();
|
2015-10-30 19:12:26 +08:00
|
|
|
if (pbe == NULL) {
|
2022-09-29 19:57:34 +08:00
|
|
|
/* ERR_R_ASN1_LIB, because PBEPARAM_new() is defined in crypto/asn1 */
|
|
|
|
ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (iter <= 0)
|
|
|
|
iter = PKCS5_DEFAULT_ITER;
|
|
|
|
if (!ASN1_INTEGER_set(pbe->iter, iter)) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!saltlen)
|
2023-08-28 10:47:51 +08:00
|
|
|
saltlen = PKCS5_DEFAULT_PBE1_SALT_LEN;
|
2021-04-12 11:58:14 +08:00
|
|
|
if (saltlen < 0)
|
|
|
|
goto err;
|
2016-08-16 21:06:48 +08:00
|
|
|
|
|
|
|
sstr = OPENSSL_malloc(saltlen);
|
2022-09-29 19:57:34 +08:00
|
|
|
if (sstr == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
if (salt)
|
|
|
|
memcpy(sstr, salt, saltlen);
|
2021-05-28 12:46:40 +08:00
|
|
|
else if (RAND_bytes_ex(ctx, sstr, saltlen, 0) <= 0)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
|
2016-08-16 21:06:48 +08:00
|
|
|
ASN1_STRING_set0(pbe->salt, sstr, saltlen);
|
|
|
|
sstr = NULL;
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (!ASN1_item_pack(pbe, ASN1_ITEM_rptr(PBEPARAM), &pbe_str)) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
PBEPARAM_free(pbe);
|
|
|
|
pbe = NULL;
|
|
|
|
|
|
|
|
if (X509_ALGOR_set0(algor, OBJ_nid2obj(alg), V_ASN1_SEQUENCE, pbe_str))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
err:
|
2016-08-16 21:06:48 +08:00
|
|
|
OPENSSL_free(sstr);
|
2015-05-02 02:37:16 +08:00
|
|
|
PBEPARAM_free(pbe);
|
2015-03-24 19:52:24 +08:00
|
|
|
ASN1_STRING_free(pbe_str);
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2021-02-17 15:56:36 +08:00
|
|
|
int PKCS5_pbe_set0_algor(X509_ALGOR *algor, int alg, int iter,
|
|
|
|
const unsigned char *salt, int saltlen)
|
|
|
|
{
|
|
|
|
return PKCS5_pbe_set0_algor_ex(algor, alg, iter, salt, saltlen, NULL);
|
|
|
|
}
|
|
|
|
|
2008-03-13 05:14:28 +08:00
|
|
|
/* Return an algorithm identifier for a PKCS#5 PBE algorithm */
|
|
|
|
|
2021-02-17 15:56:36 +08:00
|
|
|
X509_ALGOR *PKCS5_pbe_set_ex(int alg, int iter,
|
|
|
|
const unsigned char *salt, int saltlen,
|
|
|
|
OSSL_LIB_CTX *ctx)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
X509_ALGOR *ret;
|
|
|
|
ret = X509_ALGOR_new();
|
2015-10-30 19:12:26 +08:00
|
|
|
if (ret == NULL) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ERR_R_X509_LIB);
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-02-17 15:56:36 +08:00
|
|
|
if (PKCS5_pbe_set0_algor_ex(ret, alg, iter, salt, saltlen, ctx))
|
2015-01-22 11:40:55 +08:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
X509_ALGOR_free(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-02-17 15:56:36 +08:00
|
|
|
|
|
|
|
X509_ALGOR *PKCS5_pbe_set(int alg, int iter,
|
|
|
|
const unsigned char *salt, int saltlen)
|
|
|
|
{
|
|
|
|
return PKCS5_pbe_set_ex(alg, iter, salt, saltlen, NULL);
|
|
|
|
}
|
|
|
|
|