2015-01-22 03:40:55 +00:00
|
|
|
/*
|
2016-05-17 18:52:22 +00:00
|
|
|
* Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
|
2001-07-26 22:34:45 +00:00
|
|
|
*
|
2016-05-17 18:52:22 +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
|
2001-07-26 22:34:45 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
2001-07-26 22:34:45 +00:00
|
|
|
#include <openssl/pkcs12.h>
|
2019-09-27 21:57:58 +00:00
|
|
|
#include "crypto/x509.h"
|
2001-07-26 22:34:45 +00:00
|
|
|
|
|
|
|
X509_SIG *PKCS8_encrypt(int pbe_nid, const EVP_CIPHER *cipher,
|
2015-01-22 03:40:55 +00:00
|
|
|
const char *pass, int passlen,
|
|
|
|
unsigned char *salt, int saltlen, int iter,
|
|
|
|
PKCS8_PRIV_KEY_INFO *p8inf)
|
2001-07-26 22:34:45 +00:00
|
|
|
{
|
2015-05-20 23:37:39 +00:00
|
|
|
X509_SIG *p8 = NULL;
|
2015-01-22 03:40:55 +00:00
|
|
|
X509_ALGOR *pbe;
|
2001-07-26 22:34:45 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (pbe_nid == -1)
|
|
|
|
pbe = PKCS5_pbe2_set(cipher, iter, salt, saltlen);
|
|
|
|
else if (EVP_PBE_find(EVP_PBE_TYPE_PRF, pbe_nid, NULL, NULL, 0))
|
|
|
|
pbe = PKCS5_pbe2_set_iv(cipher, iter, salt, saltlen, NULL, pbe_nid);
|
|
|
|
else {
|
|
|
|
ERR_clear_error();
|
|
|
|
pbe = PKCS5_pbe_set(pbe_nid, iter, salt, saltlen);
|
|
|
|
}
|
|
|
|
if (!pbe) {
|
|
|
|
PKCS12err(PKCS12_F_PKCS8_ENCRYPT, ERR_R_ASN1_LIB);
|
2015-05-20 23:37:39 +00:00
|
|
|
return NULL;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2015-05-20 23:37:39 +00:00
|
|
|
p8 = PKCS8_set0_pbe(pass, passlen, p8inf, pbe);
|
|
|
|
if (p8 == NULL) {
|
|
|
|
X509_ALGOR_free(pbe);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return p8;
|
|
|
|
}
|
|
|
|
|
|
|
|
X509_SIG *PKCS8_set0_pbe(const char *pass, int passlen,
|
|
|
|
PKCS8_PRIV_KEY_INFO *p8inf, X509_ALGOR *pbe)
|
|
|
|
{
|
|
|
|
X509_SIG *p8;
|
|
|
|
ASN1_OCTET_STRING *enckey;
|
|
|
|
|
|
|
|
enckey =
|
2015-01-22 03:40:55 +00:00
|
|
|
PKCS12_item_i2d_encrypt(pbe, ASN1_ITEM_rptr(PKCS8_PRIV_KEY_INFO),
|
|
|
|
pass, passlen, p8inf, 1);
|
2015-05-20 23:37:39 +00:00
|
|
|
if (!enckey) {
|
|
|
|
PKCS12err(PKCS12_F_PKCS8_SET0_PBE, PKCS12_R_ENCRYPT_ERROR);
|
|
|
|
return NULL;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2001-07-26 22:34:45 +00:00
|
|
|
|
2016-03-10 15:04:46 +00:00
|
|
|
p8 = OPENSSL_zalloc(sizeof(*p8));
|
|
|
|
|
|
|
|
if (p8 == NULL) {
|
2015-05-20 23:37:39 +00:00
|
|
|
PKCS12err(PKCS12_F_PKCS8_SET0_PBE, ERR_R_MALLOC_FAILURE);
|
|
|
|
ASN1_OCTET_STRING_free(enckey);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
p8->algor = pbe;
|
|
|
|
p8->digest = enckey;
|
2001-07-26 22:34:45 +00:00
|
|
|
|
2015-05-20 23:37:39 +00:00
|
|
|
return p8;
|
2001-07-26 22:34:45 +00:00
|
|
|
}
|