777bc93ac305a94a215f1144caa303fc5d73576b
[openssl.git] / crypto / pkcs12 / p12_crpt.c
1 /*
2  * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/core.h>
13 #include <openssl/core_names.h>
14 #include "crypto/evp.h"
15 #include <openssl/pkcs12.h>
16
17 /* PKCS#12 PBE algorithms now in static table */
18
19 void PKCS12_PBE_add(void)
20 {
21 }
22
23 int PKCS12_PBE_keyivgen_ex(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
24                            ASN1_TYPE *param, const EVP_CIPHER *cipher,
25                            const EVP_MD *md, int en_de,
26                            OSSL_LIB_CTX *libctx, const char *propq)
27 {
28     PBEPARAM *pbe;
29     int saltlen, iter, ret;
30     unsigned char *salt;
31     unsigned char key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH];
32     unsigned char *piv = iv;
33
34     if (cipher == NULL)
35         return 0;
36
37     /* Extract useful info from parameter */
38
39     pbe = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBEPARAM), param);
40     if (pbe == NULL) {
41         ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR);
42         return 0;
43     }
44
45     if (pbe->iter == NULL)
46         iter = 1;
47     else
48         iter = ASN1_INTEGER_get(pbe->iter);
49     salt = pbe->salt->data;
50     saltlen = pbe->salt->length;
51     if (!PKCS12_key_gen_utf8_ex(pass, passlen, salt, saltlen, PKCS12_KEY_ID,
52                                 iter, EVP_CIPHER_key_length(cipher), key, md,
53                                 libctx, propq)) {
54         ERR_raise(ERR_LIB_PKCS12, PKCS12_R_KEY_GEN_ERROR);
55         PBEPARAM_free(pbe);
56         return 0;
57     }
58     if (EVP_CIPHER_iv_length(cipher) > 0) {
59         if (!PKCS12_key_gen_utf8_ex(pass, passlen, salt, saltlen, PKCS12_IV_ID,
60                                     iter, EVP_CIPHER_iv_length(cipher), iv, md,
61                                     libctx, propq)) {
62             ERR_raise(ERR_LIB_PKCS12, PKCS12_R_IV_GEN_ERROR);
63             PBEPARAM_free(pbe);
64             return 0;
65         }
66     } else {
67         piv = NULL;
68     }
69     PBEPARAM_free(pbe);
70     ret = EVP_CipherInit_ex(ctx, cipher, NULL, key, piv, en_de);
71     OPENSSL_cleanse(key, EVP_MAX_KEY_LENGTH);
72     OPENSSL_cleanse(iv, EVP_MAX_IV_LENGTH);
73     return ret;
74 }
75
76 int PKCS12_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
77                         ASN1_TYPE *param, const EVP_CIPHER *cipher,
78                         const EVP_MD *md, int en_de)
79 {
80     return PKCS12_PBE_keyivgen_ex(ctx, pass, passlen, param, cipher, md, en_de,
81                                   NULL, NULL);
82 }
83