Coverage Report

Created: 2026-02-22 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/evp/p5_crpt2.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/kdf.h>
16
#include <openssl/hmac.h>
17
#include <openssl/trace.h>
18
#include <openssl/core_names.h>
19
#include "crypto/evp.h"
20
#include "evp_local.h"
21
22
int ossl_pkcs5_pbkdf2_hmac_ex(const char *pass, int passlen,
23
    const unsigned char *salt, int saltlen, int iter,
24
    const EVP_MD *digest, int keylen, unsigned char *out,
25
    OSSL_LIB_CTX *libctx, const char *propq)
26
0
{
27
0
    const char *empty = "";
28
0
    int rv = 1, mode = 1;
29
0
    EVP_KDF *kdf;
30
0
    EVP_KDF_CTX *kctx;
31
0
    const char *mdname = EVP_MD_get0_name(digest);
32
0
    OSSL_PARAM params[6], *p = params;
33
34
    /* Keep documented behaviour. */
35
0
    if (pass == NULL) {
36
0
        pass = empty;
37
0
        passlen = 0;
38
0
    } else if (passlen == -1) {
39
0
        passlen = (int)strlen(pass);
40
0
    }
41
0
    if (salt == NULL && saltlen == 0)
42
0
        salt = (unsigned char *)empty;
43
44
0
    kdf = EVP_KDF_fetch(libctx, OSSL_KDF_NAME_PBKDF2, propq);
45
0
    if (kdf == NULL)
46
0
        return 0;
47
0
    kctx = EVP_KDF_CTX_new(kdf);
48
0
    EVP_KDF_free(kdf);
49
0
    if (kctx == NULL)
50
0
        return 0;
51
0
    *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD,
52
0
        (char *)pass, (size_t)passlen);
53
0
    *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_PKCS5, &mode);
54
0
    *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
55
0
        (unsigned char *)salt, saltlen);
56
0
    *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_ITER, &iter);
57
0
    *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
58
0
        (char *)mdname, 0);
59
0
    *p = OSSL_PARAM_construct_end();
60
0
    if (EVP_KDF_derive(kctx, out, keylen, params) != 1)
61
0
        rv = 0;
62
63
0
    EVP_KDF_CTX_free(kctx);
64
65
0
    OSSL_TRACE_BEGIN(PKCS5V2)
66
0
    {
67
0
        BIO_printf(trc_out, "Password:\n");
68
0
        BIO_hex_string(trc_out,
69
0
            0, passlen, pass, passlen);
70
0
        BIO_printf(trc_out, "\n");
71
0
        BIO_printf(trc_out, "Salt:\n");
72
0
        BIO_hex_string(trc_out,
73
0
            0, saltlen, salt, saltlen);
74
0
        BIO_printf(trc_out, "\n");
75
0
        BIO_printf(trc_out, "Iteration count %d\n", iter);
76
0
        BIO_printf(trc_out, "Key:\n");
77
0
        BIO_hex_string(trc_out,
78
0
            0, keylen, out, keylen);
79
0
        BIO_printf(trc_out, "\n");
80
0
    }
81
0
    OSSL_TRACE_END(PKCS5V2);
82
0
    return rv;
83
0
}
84
85
int PKCS5_PBKDF2_HMAC(const char *pass, int passlen, const unsigned char *salt,
86
    int saltlen, int iter, const EVP_MD *digest, int keylen,
87
    unsigned char *out)
88
0
{
89
0
    return ossl_pkcs5_pbkdf2_hmac_ex(pass, passlen, salt, saltlen, iter, digest,
90
0
        keylen, out, NULL, NULL);
91
0
}
92
93
int PKCS5_PBKDF2_HMAC_SHA1(const char *pass, int passlen,
94
    const unsigned char *salt, int saltlen, int iter,
95
    int keylen, unsigned char *out)
96
0
{
97
0
    EVP_MD *digest;
98
0
    int r = 0;
99
100
0
    if ((digest = EVP_MD_fetch(NULL, SN_sha1, NULL)) != NULL)
101
0
        r = ossl_pkcs5_pbkdf2_hmac_ex(pass, passlen, salt, saltlen, iter,
102
0
            digest, keylen, out, NULL, NULL);
103
0
    EVP_MD_free(digest);
104
0
    return r;
105
0
}
106
107
/*
108
 * Now the key derivation function itself. This is a bit evil because it has
109
 * to check the ASN1 parameters are valid: and there are quite a few of
110
 * them...
111
 */
112
113
int PKCS5_v2_PBE_keyivgen_ex(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
114
    ASN1_TYPE *param, const EVP_CIPHER *c,
115
    const EVP_MD *md, int en_de,
116
    OSSL_LIB_CTX *libctx, const char *propq)
117
0
{
118
0
    PBE2PARAM *pbe2 = NULL;
119
0
    char ciph_name[80];
120
0
    EVP_CIPHER *cipher = NULL;
121
0
    EVP_PBE_KEYGEN_EX *kdf;
122
123
0
    int rv = 0;
124
125
0
    pbe2 = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBE2PARAM), param);
126
0
    if (pbe2 == NULL) {
127
0
        ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
128
0
        goto err;
129
0
    }
130
131
    /* See if we recognise the key derivation function */
132
0
    if (!EVP_PBE_find_ex(EVP_PBE_TYPE_KDF, OBJ_obj2nid(pbe2->keyfunc->algorithm),
133
0
            NULL, NULL, NULL, &kdf)) {
134
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_DERIVATION_FUNCTION);
135
0
        goto err;
136
0
    }
137
138
    /*
139
     * lets see if we recognise the encryption algorithm.
140
     */
141
0
    if (OBJ_obj2txt(ciph_name, sizeof(ciph_name), pbe2->encryption->algorithm, 0) <= 0) {
142
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER);
143
0
        goto err;
144
0
    }
145
146
0
    cipher = EVP_CIPHER_fetch(libctx, ciph_name, propq);
147
148
0
    if (cipher == NULL) {
149
0
        (void)ERR_clear_last_mark();
150
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER);
151
0
        goto err;
152
0
    }
153
0
    (void)ERR_pop_to_mark();
154
155
    /* Fixup cipher based on AlgorithmIdentifier */
156
0
    if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, en_de))
157
0
        goto err;
158
0
    if (EVP_CIPHER_asn1_to_param(ctx, pbe2->encryption->parameter) <= 0) {
159
0
        ERR_raise(ERR_LIB_EVP, EVP_R_CIPHER_PARAMETER_ERROR);
160
0
        goto err;
161
0
    }
162
0
    rv = kdf(ctx, pass, passlen, pbe2->keyfunc->parameter, NULL, NULL, en_de, libctx, propq);
163
0
err:
164
0
    EVP_CIPHER_free(cipher);
165
0
    PBE2PARAM_free(pbe2);
166
0
    return rv;
167
0
}
168
169
int PKCS5_v2_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
170
    ASN1_TYPE *param, const EVP_CIPHER *c,
171
    const EVP_MD *md, int en_de)
172
0
{
173
0
    return PKCS5_v2_PBE_keyivgen_ex(ctx, pass, passlen, param, c, md, en_de, NULL, NULL);
174
0
}
175
176
int PKCS5_v2_PBKDF2_keyivgen_ex(EVP_CIPHER_CTX *ctx, const char *pass,
177
    int passlen, ASN1_TYPE *param,
178
    const EVP_CIPHER *c, const EVP_MD *md, int en_de,
179
    OSSL_LIB_CTX *libctx, const char *propq)
180
0
{
181
0
    unsigned char *salt, key[EVP_MAX_KEY_LENGTH];
182
0
    int saltlen, iter, t;
183
0
    int rv = 0;
184
0
    unsigned int keylen = 0;
185
0
    int prf_nid, hmac_md_nid;
186
0
    PBKDF2PARAM *kdf = NULL;
187
0
    EVP_MD *prfmd = NULL;
188
189
0
    if (EVP_CIPHER_CTX_get0_cipher(ctx) == NULL) {
190
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
191
0
        goto err;
192
0
    }
193
0
    keylen = EVP_CIPHER_CTX_get_key_length(ctx);
194
0
    OPENSSL_assert(keylen <= sizeof(key));
195
196
    /* Decode parameter */
197
198
0
    kdf = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBKDF2PARAM), param);
199
200
0
    if (kdf == NULL) {
201
0
        ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
202
0
        goto err;
203
0
    }
204
205
0
    t = EVP_CIPHER_CTX_get_key_length(ctx);
206
0
    if (t < 0) {
207
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
208
0
        goto err;
209
0
    }
210
0
    keylen = t;
211
212
    /* Now check the parameters of the kdf */
213
214
0
    if (kdf->keylength && (ASN1_INTEGER_get(kdf->keylength) != (int)keylen)) {
215
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEYLENGTH);
216
0
        goto err;
217
0
    }
218
219
0
    if (kdf->prf)
220
0
        prf_nid = OBJ_obj2nid(kdf->prf->algorithm);
221
0
    else
222
0
        prf_nid = NID_hmacWithSHA1;
223
224
0
    if (!EVP_PBE_find(EVP_PBE_TYPE_PRF, prf_nid, NULL, &hmac_md_nid, 0)) {
225
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_PRF);
226
0
        goto err;
227
0
    }
228
229
0
    prfmd = EVP_MD_fetch(libctx, OBJ_nid2sn(hmac_md_nid), propq);
230
0
    if (prfmd == NULL) {
231
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_PRF);
232
0
        goto err;
233
0
    }
234
235
0
    if (kdf->salt->type != V_ASN1_OCTET_STRING) {
236
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_SALT_TYPE);
237
0
        goto err;
238
0
    }
239
240
    /* it seems that its all OK */
241
0
    salt = kdf->salt->value.octet_string->data;
242
0
    saltlen = kdf->salt->value.octet_string->length;
243
0
    iter = ASN1_INTEGER_get(kdf->iter);
244
0
    if (!ossl_pkcs5_pbkdf2_hmac_ex(pass, passlen, salt, saltlen, iter, prfmd,
245
0
            keylen, key, libctx, propq))
246
0
        goto err;
247
0
    rv = EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, en_de);
248
0
err:
249
0
    OPENSSL_cleanse(key, keylen);
250
0
    PBKDF2PARAM_free(kdf);
251
0
    EVP_MD_free(prfmd);
252
0
    return rv;
253
0
}
254
255
int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
256
    int passlen, ASN1_TYPE *param,
257
    const EVP_CIPHER *c, const EVP_MD *md, int en_de)
258
0
{
259
0
    return PKCS5_v2_PBKDF2_keyivgen_ex(ctx, pass, passlen, param, c, md, en_de,
260
0
        NULL, NULL);
261
0
}