Coverage Report

Created: 2026-09-12 06:55

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