Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/asn1/p5_pbev2.c
Line
Count
Source
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 "internal/cryptlib.h"
12
#include <openssl/asn1t.h>
13
#include <openssl/core.h>
14
#include <openssl/core_names.h>
15
#include <openssl/x509.h>
16
#include <openssl/rand.h>
17
18
/* PKCS#5 v2.0 password based encryption structures */
19
20
ASN1_SEQUENCE(PBE2PARAM) = {
21
    ASN1_SIMPLE(PBE2PARAM, keyfunc, X509_ALGOR),
22
    ASN1_SIMPLE(PBE2PARAM, encryption, X509_ALGOR)
23
71.4k
} ASN1_SEQUENCE_END(PBE2PARAM)
24
71.4k
25
71.4k
IMPLEMENT_ASN1_FUNCTIONS(PBE2PARAM)
26
71.4k
27
71.4k
ASN1_SEQUENCE(PBKDF2PARAM) = {
28
71.4k
    ASN1_SIMPLE(PBKDF2PARAM, salt, ASN1_ANY),
29
71.4k
    ASN1_SIMPLE(PBKDF2PARAM, iter, ASN1_INTEGER),
30
71.4k
    ASN1_OPT(PBKDF2PARAM, keylength, ASN1_INTEGER),
31
71.4k
    ASN1_OPT(PBKDF2PARAM, prf, X509_ALGOR)
32
71.4k
} ASN1_SEQUENCE_END(PBKDF2PARAM)
33
71.4k
34
71.4k
IMPLEMENT_ASN1_FUNCTIONS(PBKDF2PARAM)
35
71.4k
36
71.4k
/*
37
71.4k
 * Return an algorithm identifier for a PKCS#5 v2.0 PBE algorithm: yes I know
38
71.4k
 * this is horrible! Extended version to allow application supplied PRF NID
39
71.4k
 * and IV.
40
71.4k
 */
41
71.4k
42
71.4k
X509_ALGOR *PKCS5_pbe2_set_iv_ex(const EVP_CIPHER *cipher, int iter,
43
71.4k
    unsigned char *salt, int saltlen,
44
71.4k
    unsigned char *aiv, int prf_nid,
45
71.4k
    OSSL_LIB_CTX *libctx)
46
71.4k
{
47
0
    X509_ALGOR *scheme = NULL, *ret = NULL;
48
0
    int alg_nid, keylen, ivlen;
49
0
    EVP_CIPHER_CTX *ctx = NULL;
50
0
    unsigned char iv[EVP_MAX_IV_LENGTH];
51
0
    PBE2PARAM *pbe2 = NULL;
52
53
0
    alg_nid = EVP_CIPHER_get_type(cipher);
54
0
    if (alg_nid == NID_undef) {
55
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
56
0
        goto err;
57
0
    }
58
59
0
    if ((pbe2 = PBE2PARAM_new()) == NULL)
60
0
        goto merr;
61
62
    /* Setup the AlgorithmIdentifier for the encryption scheme */
63
0
    scheme = pbe2->encryption;
64
0
    scheme->algorithm = OBJ_nid2obj(alg_nid);
65
0
    if ((scheme->parameter = ASN1_TYPE_new()) == NULL)
66
0
        goto merr;
67
68
    /* Create random IV */
69
0
    ivlen = EVP_CIPHER_get_iv_length(cipher);
70
0
    if (ivlen > 0) {
71
0
        if (aiv)
72
0
            memcpy(iv, aiv, ivlen);
73
0
        else if (RAND_bytes_ex(libctx, iv, ivlen, 0) <= 0)
74
0
            goto err;
75
0
    }
76
77
0
    ctx = EVP_CIPHER_CTX_new();
78
0
    if (ctx == NULL)
79
0
        goto merr;
80
81
    /* Dummy cipherinit to just setup the IV, and PRF */
82
0
    if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, iv, 0))
83
0
        goto err;
84
0
    if (EVP_CIPHER_param_to_asn1(ctx, scheme->parameter) <= 0) {
85
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ERROR_SETTING_CIPHER_PARAMS);
86
0
        goto err;
87
0
    }
88
    /*
89
     * If prf NID unspecified see if cipher has a preference. An error is OK
90
     * here: just means use default PRF.
91
     */
92
0
    ERR_set_mark();
93
0
    if ((prf_nid == -1) && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PBE_PRF_NID, 0, &prf_nid) <= 0) {
94
0
        prf_nid = NID_hmacWithSHA256;
95
0
    }
96
0
    ERR_pop_to_mark();
97
0
    EVP_CIPHER_CTX_free(ctx);
98
0
    ctx = NULL;
99
100
    /* If its RC2 then we'd better setup the key length */
101
102
0
    if (alg_nid == NID_rc2_cbc)
103
0
        keylen = EVP_CIPHER_get_key_length(cipher);
104
0
    else
105
0
        keylen = -1;
106
107
    /* Setup keyfunc */
108
109
0
    X509_ALGOR_free(pbe2->keyfunc);
110
111
0
    pbe2->keyfunc = PKCS5_pbkdf2_set_ex(iter, salt, saltlen, prf_nid, keylen,
112
0
        libctx);
113
114
0
    if (pbe2->keyfunc == NULL)
115
0
        goto merr;
116
117
    /* Now set up top level AlgorithmIdentifier */
118
119
0
    if ((ret = X509_ALGOR_new()) == NULL)
120
0
        goto merr;
121
122
0
    ret->algorithm = OBJ_nid2obj(NID_pbes2);
123
124
    /* Encode PBE2PARAM into parameter */
125
126
0
    if (!ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(PBE2PARAM), pbe2,
127
0
            &ret->parameter))
128
0
        goto merr;
129
130
0
    PBE2PARAM_free(pbe2);
131
0
    pbe2 = NULL;
132
133
0
    return ret;
134
135
0
merr:
136
0
    ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
137
138
0
err:
139
0
    EVP_CIPHER_CTX_free(ctx);
140
0
    PBE2PARAM_free(pbe2);
141
    /* Note 'scheme' is freed as part of pbe2 */
142
0
    X509_ALGOR_free(ret);
143
144
0
    return NULL;
145
0
}
146
147
X509_ALGOR *PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter,
148
    unsigned char *salt, int saltlen,
149
    unsigned char *aiv, int prf_nid)
150
0
{
151
0
    return PKCS5_pbe2_set_iv_ex(cipher, iter, salt, saltlen, aiv, prf_nid,
152
0
        NULL);
153
0
}
154
155
X509_ALGOR *PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter,
156
    unsigned char *salt, int saltlen)
157
0
{
158
0
    return PKCS5_pbe2_set_iv_ex(cipher, iter, salt, saltlen, NULL, -1,
159
0
        NULL);
160
0
}
161
162
X509_ALGOR *PKCS5_pbkdf2_set_ex(int iter, unsigned char *salt, int saltlen,
163
    int prf_nid, int keylen,
164
    OSSL_LIB_CTX *libctx)
165
0
{
166
0
    X509_ALGOR *keyfunc = NULL;
167
0
    PBKDF2PARAM *kdf = NULL;
168
0
    ASN1_OCTET_STRING *osalt = NULL;
169
170
0
    if ((kdf = PBKDF2PARAM_new()) == NULL)
171
0
        goto merr;
172
0
    if ((osalt = ASN1_OCTET_STRING_new()) == NULL)
173
0
        goto merr;
174
175
0
    kdf->salt->value.octet_string = osalt;
176
0
    kdf->salt->type = V_ASN1_OCTET_STRING;
177
178
0
    if (saltlen < 0)
179
0
        goto merr;
180
0
    if (saltlen == 0)
181
0
        saltlen = PKCS5_SALT_LEN;
182
0
    if ((osalt->data = OPENSSL_malloc(saltlen)) == NULL)
183
0
        goto merr;
184
185
0
    osalt->length = saltlen;
186
187
0
    if (salt)
188
0
        memcpy(osalt->data, salt, saltlen);
189
0
    else if (RAND_bytes_ex(libctx, osalt->data, saltlen, 0) <= 0)
190
0
        goto merr;
191
192
0
    if (iter <= 0)
193
0
        iter = PKCS5_DEFAULT_ITER;
194
195
0
    if (!ASN1_INTEGER_set(kdf->iter, iter))
196
0
        goto merr;
197
198
    /* If have a key len set it up */
199
200
0
    if (keylen > 0) {
201
0
        if ((kdf->keylength = ASN1_INTEGER_new()) == NULL)
202
0
            goto merr;
203
0
        if (!ASN1_INTEGER_set(kdf->keylength, keylen))
204
0
            goto merr;
205
0
    }
206
207
    /* prf can stay NULL if we are using hmacWithSHA1 */
208
0
    if (prf_nid > 0 && prf_nid != NID_hmacWithSHA1) {
209
0
        kdf->prf = X509_ALGOR_new();
210
0
        if (kdf->prf == NULL)
211
0
            goto merr;
212
0
        X509_ALGOR_set0(kdf->prf, OBJ_nid2obj(prf_nid), V_ASN1_NULL, NULL);
213
0
    }
214
215
    /* Finally setup the keyfunc structure */
216
217
0
    keyfunc = X509_ALGOR_new();
218
0
    if (keyfunc == NULL)
219
0
        goto merr;
220
221
0
    keyfunc->algorithm = OBJ_nid2obj(NID_id_pbkdf2);
222
223
    /* Encode PBKDF2PARAM into parameter of pbe2 */
224
225
0
    if (!ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(PBKDF2PARAM), kdf,
226
0
            &keyfunc->parameter))
227
0
        goto merr;
228
229
0
    PBKDF2PARAM_free(kdf);
230
0
    return keyfunc;
231
232
0
merr:
233
0
    ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
234
0
    PBKDF2PARAM_free(kdf);
235
0
    X509_ALGOR_free(keyfunc);
236
0
    return NULL;
237
0
}
238
239
X509_ALGOR *PKCS5_pbkdf2_set(int iter, unsigned char *salt, int saltlen,
240
    int prf_nid, int keylen)
241
0
{
242
    return PKCS5_pbkdf2_set_ex(iter, salt, saltlen, prf_nid, keylen, NULL);
243
0
}