2015-01-22 11:40:55 +08:00
|
|
|
/*
|
2021-05-06 20:03:23 +08:00
|
|
|
* Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
|
2001-07-27 06:34:45 +08:00
|
|
|
*
|
2018-12-06 20:49:51 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:52:22 +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
|
2001-07-27 06:34:45 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
2021-02-17 15:56:36 +08:00
|
|
|
#include <openssl/core.h>
|
2001-07-27 06:34:45 +08:00
|
|
|
#include <openssl/pkcs12.h>
|
2019-09-28 06:45:33 +08:00
|
|
|
#include "crypto/x509.h"
|
2001-07-27 06:34:45 +08:00
|
|
|
|
2021-02-17 15:56:36 +08:00
|
|
|
X509_SIG *PKCS8_encrypt_ex(int pbe_nid, const EVP_CIPHER *cipher,
|
|
|
|
const char *pass, int passlen,
|
|
|
|
unsigned char *salt, int saltlen, int iter,
|
|
|
|
PKCS8_PRIV_KEY_INFO *p8inf,
|
|
|
|
OSSL_LIB_CTX *libctx, const char *propq)
|
2001-07-27 06:34:45 +08:00
|
|
|
{
|
2015-05-21 07:37:39 +08:00
|
|
|
X509_SIG *p8 = NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
X509_ALGOR *pbe;
|
2001-07-27 06:34:45 +08:00
|
|
|
|
2021-05-04 06:23:10 +08:00
|
|
|
if (pbe_nid == -1) {
|
|
|
|
if (cipher == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_PKCS12, ERR_R_PASSED_NULL_PARAMETER);
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-02-17 15:56:36 +08:00
|
|
|
pbe = PKCS5_pbe2_set_iv_ex(cipher, iter, salt, saltlen, NULL, -1,
|
|
|
|
libctx);
|
2021-05-04 06:23:10 +08:00
|
|
|
} else {
|
2021-05-13 01:15:27 +08:00
|
|
|
ERR_set_mark();
|
|
|
|
if (EVP_PBE_find(EVP_PBE_TYPE_PRF, pbe_nid, NULL, NULL, 0)) {
|
|
|
|
ERR_clear_last_mark();
|
|
|
|
if (cipher == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_PKCS12, ERR_R_PASSED_NULL_PARAMETER);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
pbe = PKCS5_pbe2_set_iv_ex(cipher, iter, salt, saltlen, NULL,
|
|
|
|
pbe_nid, libctx);
|
|
|
|
} else {
|
|
|
|
ERR_pop_to_mark();
|
|
|
|
pbe = PKCS5_pbe_set_ex(pbe_nid, iter, salt, saltlen, libctx);
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2019-09-17 03:28:57 +08:00
|
|
|
if (pbe == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
|
2015-05-21 07:37:39 +08:00
|
|
|
return NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2021-02-17 15:56:36 +08:00
|
|
|
p8 = PKCS8_set0_pbe_ex(pass, passlen, p8inf, pbe, libctx, propq);
|
2015-05-21 07:37:39 +08:00
|
|
|
if (p8 == NULL) {
|
|
|
|
X509_ALGOR_free(pbe);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return p8;
|
|
|
|
}
|
|
|
|
|
2021-02-17 15:56:36 +08:00
|
|
|
X509_SIG *PKCS8_encrypt(int pbe_nid, const EVP_CIPHER *cipher,
|
|
|
|
const char *pass, int passlen,
|
|
|
|
unsigned char *salt, int saltlen, int iter,
|
|
|
|
PKCS8_PRIV_KEY_INFO *p8inf)
|
|
|
|
{
|
|
|
|
return PKCS8_encrypt_ex(pbe_nid, cipher, pass, passlen, salt, saltlen, iter,
|
|
|
|
p8inf, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
X509_SIG *PKCS8_set0_pbe_ex(const char *pass, int passlen,
|
|
|
|
PKCS8_PRIV_KEY_INFO *p8inf, X509_ALGOR *pbe,
|
|
|
|
OSSL_LIB_CTX *ctx, const char *propq)
|
2015-05-21 07:37:39 +08:00
|
|
|
{
|
|
|
|
X509_SIG *p8;
|
|
|
|
ASN1_OCTET_STRING *enckey;
|
|
|
|
|
|
|
|
enckey =
|
2021-02-17 15:56:36 +08:00
|
|
|
PKCS12_item_i2d_encrypt_ex(pbe, ASN1_ITEM_rptr(PKCS8_PRIV_KEY_INFO),
|
|
|
|
pass, passlen, p8inf, 1, ctx, propq);
|
2015-05-21 07:37:39 +08:00
|
|
|
if (!enckey) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCRYPT_ERROR);
|
2015-05-21 07:37:39 +08:00
|
|
|
return NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2001-07-27 06:34:45 +08:00
|
|
|
|
2016-03-10 23:04:46 +08:00
|
|
|
p8 = OPENSSL_zalloc(sizeof(*p8));
|
|
|
|
|
|
|
|
if (p8 == NULL) {
|
2015-05-21 07:37:39 +08:00
|
|
|
ASN1_OCTET_STRING_free(enckey);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
p8->algor = pbe;
|
|
|
|
p8->digest = enckey;
|
2001-07-27 06:34:45 +08:00
|
|
|
|
2015-05-21 07:37:39 +08:00
|
|
|
return p8;
|
2001-07-27 06:34:45 +08:00
|
|
|
}
|
2021-02-17 15:56:36 +08:00
|
|
|
|
|
|
|
X509_SIG *PKCS8_set0_pbe(const char *pass, int passlen,
|
|
|
|
PKCS8_PRIV_KEY_INFO *p8inf, X509_ALGOR *pbe)
|
|
|
|
{
|
|
|
|
return PKCS8_set0_pbe_ex(pass, passlen, p8inf, pbe, NULL, NULL);
|
|
|
|
}
|