Coverage Report

Created: 2026-03-09 06:55

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