Coverage Report

Created: 2025-12-10 06:24

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
/*
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 || param->value.sequence == NULL) {
46
0
        ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
47
0
        return 0;
48
0
    }
49
50
0
    pbe = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBEPARAM), param);
51
0
    if (pbe == NULL) {
52
0
        ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
53
0
        return 0;
54
0
    }
55
56
0
    ivl = EVP_CIPHER_get_iv_length(cipher);
57
0
    if (ivl < 0 || ivl > 16) {
58
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_IV_LENGTH);
59
0
        goto err;
60
0
    }
61
0
    kl = EVP_CIPHER_get_key_length(cipher);
62
0
    if (kl < 0 || kl > (int)sizeof(md_tmp)) {
63
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
64
0
        goto err;
65
0
    }
66
67
0
    if (pbe->iter == NULL)
68
0
        iter = 1;
69
0
    else
70
0
        iter = ASN1_INTEGER_get(pbe->iter);
71
0
    salt = pbe->salt->data;
72
0
    saltlen = pbe->salt->length;
73
74
0
    if (pass == NULL)
75
0
        passlen = 0;
76
0
    else if (passlen == -1)
77
0
        passlen = (int)strlen(pass);
78
79
0
    mdsize = EVP_MD_get_size(md);
80
0
    if (mdsize <= 0)
81
0
        goto err;
82
83
0
    kdf = EVP_KDF_fetch(libctx, OSSL_KDF_NAME_PBKDF1, propq);
84
0
    kctx = EVP_KDF_CTX_new(kdf);
85
0
    EVP_KDF_free(kdf);
86
0
    if (kctx == NULL)
87
0
        goto err;
88
0
    *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD,
89
0
        (char *)pass, (size_t)passlen);
90
0
    *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
91
0
        salt, saltlen);
92
0
    *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_ITER, &iter);
93
0
    *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
94
0
        (char *)mdname, 0);
95
0
    *p = OSSL_PARAM_construct_end();
96
0
    if (EVP_KDF_derive(kctx, md_tmp, mdsize, params) != 1)
97
0
        goto err;
98
0
    memcpy(key, md_tmp, kl);
99
0
    memcpy(iv, md_tmp + (16 - ivl), ivl);
100
0
    if (!EVP_CipherInit_ex(cctx, cipher, NULL, key, iv, en_de))
101
0
        goto err;
102
0
    OPENSSL_cleanse(md_tmp, EVP_MAX_MD_SIZE);
103
0
    OPENSSL_cleanse(key, EVP_MAX_KEY_LENGTH);
104
0
    OPENSSL_cleanse(iv, EVP_MAX_IV_LENGTH);
105
0
    rv = 1;
106
0
err:
107
0
    EVP_KDF_CTX_free(kctx);
108
0
    PBEPARAM_free(pbe);
109
0
    return rv;
110
0
}
111
112
int PKCS5_PBE_keyivgen(EVP_CIPHER_CTX *cctx, const char *pass, int passlen,
113
    ASN1_TYPE *param, const EVP_CIPHER *cipher,
114
    const EVP_MD *md, int en_de)
115
0
{
116
0
    return PKCS5_PBE_keyivgen_ex(cctx, pass, passlen, param, cipher, md, en_de,
117
0
        NULL, NULL);
118
0
}