16f9291ceSJung-uk Kim /*
2b077aed3SPierre Pronchery * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
35c87c606SMark Murray *
4b077aed3SPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use
5e71b7053SJung-uk Kim * this file except in compliance with the License. You can obtain a copy
6e71b7053SJung-uk Kim * in the file LICENSE in the source distribution or at
7e71b7053SJung-uk Kim * https://www.openssl.org/source/license.html
85c87c606SMark Murray */
95c87c606SMark Murray
105c87c606SMark Murray #include <stdio.h>
11e71b7053SJung-uk Kim #include "internal/cryptlib.h"
12b077aed3SPierre Pronchery #include <openssl/core.h>
135c87c606SMark Murray #include <openssl/pkcs12.h>
1417f01e99SJung-uk Kim #include "crypto/x509.h"
155c87c606SMark Murray
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)16b077aed3SPierre Pronchery X509_SIG *PKCS8_encrypt_ex(int pbe_nid, const EVP_CIPHER *cipher,
175c87c606SMark Murray const char *pass, int passlen,
185c87c606SMark Murray unsigned char *salt, int saltlen, int iter,
19b077aed3SPierre Pronchery PKCS8_PRIV_KEY_INFO *p8inf,
20b077aed3SPierre Pronchery OSSL_LIB_CTX *libctx, const char *propq)
215c87c606SMark Murray {
225c87c606SMark Murray X509_SIG *p8 = NULL;
235c87c606SMark Murray X509_ALGOR *pbe;
245c87c606SMark Murray
25b077aed3SPierre Pronchery if (pbe_nid == -1) {
26b077aed3SPierre Pronchery if (cipher == NULL) {
27b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PKCS12, ERR_R_PASSED_NULL_PARAMETER);
28e71b7053SJung-uk Kim return NULL;
295c87c606SMark Murray }
30b077aed3SPierre Pronchery pbe = PKCS5_pbe2_set_iv_ex(cipher, iter, salt, saltlen, NULL, -1,
31b077aed3SPierre Pronchery libctx);
32b077aed3SPierre Pronchery } else {
33b077aed3SPierre Pronchery ERR_set_mark();
34b077aed3SPierre Pronchery if (EVP_PBE_find(EVP_PBE_TYPE_PRF, pbe_nid, NULL, NULL, 0)) {
35b077aed3SPierre Pronchery ERR_clear_last_mark();
36b077aed3SPierre Pronchery if (cipher == NULL) {
37b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PKCS12, ERR_R_PASSED_NULL_PARAMETER);
38b077aed3SPierre Pronchery return NULL;
39b077aed3SPierre Pronchery }
40b077aed3SPierre Pronchery pbe = PKCS5_pbe2_set_iv_ex(cipher, iter, salt, saltlen, NULL,
41b077aed3SPierre Pronchery pbe_nid, libctx);
42b077aed3SPierre Pronchery } else {
43b077aed3SPierre Pronchery ERR_pop_to_mark();
44b077aed3SPierre Pronchery pbe = PKCS5_pbe_set_ex(pbe_nid, iter, salt, saltlen, libctx);
45b077aed3SPierre Pronchery }
46b077aed3SPierre Pronchery }
47b077aed3SPierre Pronchery if (pbe == NULL) {
48b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
49b077aed3SPierre Pronchery return NULL;
50b077aed3SPierre Pronchery }
51b077aed3SPierre Pronchery p8 = PKCS8_set0_pbe_ex(pass, passlen, p8inf, pbe, libctx, propq);
52e71b7053SJung-uk Kim if (p8 == NULL) {
53e71b7053SJung-uk Kim X509_ALGOR_free(pbe);
54e71b7053SJung-uk Kim return NULL;
555c87c606SMark Murray }
565c87c606SMark Murray
575c87c606SMark Murray return p8;
58e71b7053SJung-uk Kim }
595c87c606SMark Murray
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)60b077aed3SPierre Pronchery X509_SIG *PKCS8_encrypt(int pbe_nid, const EVP_CIPHER *cipher,
61b077aed3SPierre Pronchery const char *pass, int passlen,
62b077aed3SPierre Pronchery unsigned char *salt, int saltlen, int iter,
63b077aed3SPierre Pronchery PKCS8_PRIV_KEY_INFO *p8inf)
64b077aed3SPierre Pronchery {
65b077aed3SPierre Pronchery return PKCS8_encrypt_ex(pbe_nid, cipher, pass, passlen, salt, saltlen, iter,
66b077aed3SPierre Pronchery p8inf, NULL, NULL);
67b077aed3SPierre Pronchery }
68b077aed3SPierre Pronchery
PKCS8_set0_pbe_ex(const char * pass,int passlen,PKCS8_PRIV_KEY_INFO * p8inf,X509_ALGOR * pbe,OSSL_LIB_CTX * ctx,const char * propq)69b077aed3SPierre Pronchery X509_SIG *PKCS8_set0_pbe_ex(const char *pass, int passlen,
70b077aed3SPierre Pronchery PKCS8_PRIV_KEY_INFO *p8inf, X509_ALGOR *pbe,
71b077aed3SPierre Pronchery OSSL_LIB_CTX *ctx, const char *propq)
72e71b7053SJung-uk Kim {
73e71b7053SJung-uk Kim X509_SIG *p8;
74e71b7053SJung-uk Kim ASN1_OCTET_STRING *enckey;
75e71b7053SJung-uk Kim
76e71b7053SJung-uk Kim enckey =
77b077aed3SPierre Pronchery PKCS12_item_i2d_encrypt_ex(pbe, ASN1_ITEM_rptr(PKCS8_PRIV_KEY_INFO),
78b077aed3SPierre Pronchery pass, passlen, p8inf, 1, ctx, propq);
79e71b7053SJung-uk Kim if (!enckey) {
80b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCRYPT_ERROR);
815c87c606SMark Murray return NULL;
825c87c606SMark Murray }
83e71b7053SJung-uk Kim
84e71b7053SJung-uk Kim p8 = OPENSSL_zalloc(sizeof(*p8));
85e71b7053SJung-uk Kim
86e71b7053SJung-uk Kim if (p8 == NULL) {
87b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
88e71b7053SJung-uk Kim ASN1_OCTET_STRING_free(enckey);
89e71b7053SJung-uk Kim return NULL;
90e71b7053SJung-uk Kim }
91e71b7053SJung-uk Kim p8->algor = pbe;
92e71b7053SJung-uk Kim p8->digest = enckey;
93e71b7053SJung-uk Kim
94e71b7053SJung-uk Kim return p8;
95e71b7053SJung-uk Kim }
96b077aed3SPierre Pronchery
PKCS8_set0_pbe(const char * pass,int passlen,PKCS8_PRIV_KEY_INFO * p8inf,X509_ALGOR * pbe)97b077aed3SPierre Pronchery X509_SIG *PKCS8_set0_pbe(const char *pass, int passlen,
98b077aed3SPierre Pronchery PKCS8_PRIV_KEY_INFO *p8inf, X509_ALGOR *pbe)
99b077aed3SPierre Pronchery {
100b077aed3SPierre Pronchery return PKCS8_set0_pbe_ex(pass, passlen, p8inf, pbe, NULL, NULL);
101b077aed3SPierre Pronchery }
102