Coverage Report

Created: 2025-06-13 06:58

/src/openssl30/crypto/evp/p5_crpt.c
Line
Count
Source (jump to first uncovered line)
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 <stdlib.h>
12
#include "internal/cryptlib.h"
13
#include <openssl/x509.h>
14
#include <openssl/evp.h>
15
#include <openssl/core_names.h>
16
#include <openssl/kdf.h>
17
18
/*
19
 * Doesn't do anything now: Builtin PBE algorithms in static table.
20
 */
21
22
void PKCS5_PBE_add(void)
23
0
{
24
0
}
25
26
int PKCS5_PBE_keyivgen_ex(EVP_CIPHER_CTX *cctx, const char *pass, int passlen,
27
                          ASN1_TYPE *param, const EVP_CIPHER *cipher,
28
                          const EVP_MD *md, int en_de, OSSL_LIB_CTX *libctx,
29
                          const char *propq)
30
0
{
31
0
    unsigned char md_tmp[EVP_MAX_MD_SIZE];
32
0
    unsigned char key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH];
33
0
    int ivl, kl;
34
0
    PBEPARAM *pbe = NULL;
35
0
    int saltlen, iter;
36
0
    unsigned char *salt;
37
0
    int mdsize;
38
0
    int rv = 0;
39
0
    EVP_KDF *kdf;
40
0
    EVP_KDF_CTX *kctx = NULL;
41
0
    OSSL_PARAM params[5], *p = params;
42
0
    const char *mdname = EVP_MD_name(md);
43
44
    /* Extract useful info from parameter */
45
0
    if (param == NULL || param->type != V_ASN1_SEQUENCE ||
46
0
        param->value.sequence == NULL) {
47
0
        ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
48
0
        return 0;
49
0
    }
50
51
0
    pbe = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBEPARAM), param);
52
0
    if (pbe == NULL) {
53
0
        ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
54
0
        return 0;
55
0
    }
56
57
0
    ivl = EVP_CIPHER_get_iv_length(cipher);
58
0
    if (ivl < 0 || ivl > 16) {
59
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_IV_LENGTH);
60
0
        goto err;
61
0
    }
62
0
    kl = EVP_CIPHER_get_key_length(cipher);
63
0
    if (kl < 0 || kl > (int)sizeof(md_tmp)) {
64
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
65
0
        goto err;
66
0
    }
67
68
0
    if (pbe->iter == NULL)
69
0
        iter = 1;
70
0
    else
71
0
        iter = ASN1_INTEGER_get(pbe->iter);
72
0
    salt = pbe->salt->data;
73
0
    saltlen = pbe->salt->length;
74
75
0
    if (pass == NULL)
76
0
        passlen = 0;
77
0
    else if (passlen == -1)
78
0
        passlen = strlen(pass);
79
80
0
    mdsize = EVP_MD_get_size(md);
81
0
    if (mdsize < 0)
82
0
        goto err;
83
84
0
    kdf = EVP_KDF_fetch(libctx, OSSL_KDF_NAME_PBKDF1, propq);
85
0
    kctx = EVP_KDF_CTX_new(kdf);
86
0
    EVP_KDF_free(kdf);
87
0
    if (kctx == NULL)
88
0
        goto err;
89
0
    *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD,
90
0
                                             (char *)pass, (size_t)passlen);
91
0
    *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
92
0
                                             salt, saltlen);
93
0
    *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_ITER, &iter);
94
0
    *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
95
0
                                            (char *)mdname, 0);
96
0
    *p = OSSL_PARAM_construct_end();
97
0
    if (EVP_KDF_derive(kctx, md_tmp, mdsize, params) != 1)
98
0
        goto err;
99
0
    memcpy(key, md_tmp, kl);
100
0
    memcpy(iv, md_tmp + (16 - ivl), ivl);
101
0
    if (!EVP_CipherInit_ex(cctx, cipher, NULL, key, iv, en_de))
102
0
        goto err;
103
0
    OPENSSL_cleanse(md_tmp, EVP_MAX_MD_SIZE);
104
0
    OPENSSL_cleanse(key, EVP_MAX_KEY_LENGTH);
105
0
    OPENSSL_cleanse(iv, EVP_MAX_IV_LENGTH);
106
0
    rv = 1;
107
0
 err:
108
0
    EVP_KDF_CTX_free(kctx);
109
0
    PBEPARAM_free(pbe);
110
0
    return rv;
111
0
}
112
113
int PKCS5_PBE_keyivgen(EVP_CIPHER_CTX *cctx, const char *pass, int passlen,
114
                       ASN1_TYPE *param, const EVP_CIPHER *cipher,
115
                       const EVP_MD *md, int en_de)
116
0
{
117
0
    return PKCS5_PBE_keyivgen_ex(cctx, pass, passlen, param, cipher, md, en_de,
118
0
                                 NULL, NULL);
119
0
}
120