Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/crypto/evp/evp_lib.c
Line
Count
Source
1
/*
2
 * Copyright 1995-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
/*
11
 * EVP _meth_ APIs are deprecated for public use, but still ok for
12
 * internal use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <stdio.h>
17
#include <string.h>
18
#include "internal/cryptlib.h"
19
#include <openssl/evp.h>
20
#include <openssl/x509.h>
21
#include <openssl/objects.h>
22
#include <openssl/params.h>
23
#include <openssl/core_names.h>
24
#include <openssl/rsa.h>
25
#include <openssl/dh.h>
26
#include <openssl/ec.h>
27
#include "crypto/evp.h"
28
#include "crypto/cryptlib.h"
29
#include "internal/provider.h"
30
#include "evp_local.h"
31
32
#if !defined(FIPS_MODULE)
33
#include "crypto/asn1.h"
34
35
int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
36
0
{
37
0
    return evp_cipher_param_to_asn1_ex(c, type, NULL);
38
0
}
39
40
int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
41
188
{
42
188
    return evp_cipher_asn1_to_param_ex(c, type, NULL);
43
188
}
44
45
int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX *ctx, ASN1_TYPE *type)
46
128
{
47
128
    int i = 0;
48
128
    unsigned int l;
49
50
128
    if (type != NULL) {
51
128
        unsigned char iv[EVP_MAX_IV_LENGTH];
52
53
128
        l = EVP_CIPHER_CTX_get_iv_length(ctx);
54
128
        if (!ossl_assert(l <= sizeof(iv)))
55
0
            return -1;
56
128
        i = ASN1_TYPE_get_octetstring(type, iv, l);
57
128
        if (i != (int)l)
58
18
            return -1;
59
60
110
        if (!EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1))
61
0
            return -1;
62
110
    }
63
110
    return i;
64
128
}
65
66
int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
67
0
{
68
0
    int i = 0;
69
0
    unsigned int j;
70
0
    unsigned char *oiv = NULL;
71
72
0
    if (type != NULL) {
73
0
        oiv = (unsigned char *)EVP_CIPHER_CTX_original_iv(c);
74
0
        j = EVP_CIPHER_CTX_get_iv_length(c);
75
0
        OPENSSL_assert(j <= sizeof(c->iv));
76
0
        i = ASN1_TYPE_set_octetstring(type, oiv, j);
77
0
    }
78
0
    return i;
79
0
}
80
81
int evp_cipher_param_to_asn1_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
82
    evp_cipher_aead_asn1_params *asn1_params)
83
0
{
84
0
    int ret = -1; /* Assume the worst */
85
0
    const EVP_CIPHER *cipher;
86
87
0
    if (c == NULL || c->cipher == NULL)
88
0
        goto err;
89
90
0
    cipher = c->cipher;
91
    /*
92
     * For legacy implementations, we detect custom AlgorithmIdentifier
93
     * parameter handling by checking if the function pointer
94
     * cipher->set_asn1_parameters is set.  We know that this pointer
95
     * is NULL for provided implementations.
96
     *
97
     * Otherwise, for any implementation, we check the flag
98
     * EVP_CIPH_FLAG_CUSTOM_ASN1.  If it isn't set, we apply
99
     * default AI parameter extraction.
100
     *
101
     * Otherwise, for provided implementations, we convert |type| to
102
     * a DER encoded blob and pass to the implementation in OSSL_PARAM
103
     * form.
104
     *
105
     * If none of the above applies, this operation is unsupported.
106
     */
107
0
    if (cipher->set_asn1_parameters != NULL) {
108
0
        ret = cipher->set_asn1_parameters(c, type);
109
0
    } else if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_CUSTOM_ASN1) == 0) {
110
0
        switch (EVP_CIPHER_get_mode(cipher)) {
111
0
        case EVP_CIPH_WRAP_MODE:
112
0
            if (EVP_CIPHER_is_a(cipher, SN_id_smime_alg_CMS3DESwrap))
113
0
                ASN1_TYPE_set(type, V_ASN1_NULL, NULL);
114
0
            ret = 1;
115
0
            break;
116
117
0
        case EVP_CIPH_GCM_MODE:
118
0
            ret = evp_cipher_set_asn1_aead_params(c, type, asn1_params);
119
0
            break;
120
121
0
        case EVP_CIPH_CCM_MODE:
122
0
        case EVP_CIPH_XTS_MODE:
123
0
        case EVP_CIPH_OCB_MODE:
124
0
            ret = -2;
125
0
            break;
126
127
0
        default:
128
0
            ret = EVP_CIPHER_set_asn1_iv(c, type);
129
0
        }
130
0
    } else if (cipher->prov != NULL) {
131
        /* We cheat, there's no need for an object ID for this use */
132
0
        X509_ALGOR alg;
133
134
0
        alg.algorithm = NULL;
135
0
        alg.parameter = type;
136
137
0
        ret = EVP_CIPHER_CTX_get_algor_params(c, &alg);
138
0
    } else {
139
0
        ret = -2;
140
0
    }
141
142
0
err:
143
0
    if (ret == -2)
144
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER);
145
0
    else if (ret <= 0)
146
0
        ERR_raise(ERR_LIB_EVP, EVP_R_CIPHER_PARAMETER_ERROR);
147
0
    if (ret < -1)
148
0
        ret = -1;
149
0
    return ret;
150
0
}
151
152
int evp_cipher_asn1_to_param_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
153
    evp_cipher_aead_asn1_params *asn1_params)
154
0
{
155
0
    int ret = -1; /* Assume the worst */
156
0
    const EVP_CIPHER *cipher;
157
158
0
    if (c == NULL || c->cipher == NULL)
159
0
        goto err;
160
161
0
    cipher = c->cipher;
162
    /*
163
     * For legacy implementations, we detect custom AlgorithmIdentifier
164
     * parameter handling by checking if there the function pointer
165
     * cipher->get_asn1_parameters is set.  We know that this pointer
166
     * is NULL for provided implementations.
167
     *
168
     * Otherwise, for any implementation, we check the flag
169
     * EVP_CIPH_FLAG_CUSTOM_ASN1.  If it isn't set, we apply
170
     * default AI parameter creation.
171
     *
172
     * Otherwise, for provided implementations, we get the AI parameter
173
     * in DER encoded form from the implementation by requesting the
174
     * appropriate OSSL_PARAM and converting the result to a ASN1_TYPE.
175
     *
176
     * If none of the above applies, this operation is unsupported.
177
     */
178
0
    if (cipher->get_asn1_parameters != NULL) {
179
0
        ret = cipher->get_asn1_parameters(c, type);
180
0
    } else if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_CUSTOM_ASN1) == 0) {
181
0
        switch (EVP_CIPHER_get_mode(cipher)) {
182
0
        case EVP_CIPH_WRAP_MODE:
183
0
            ret = 1;
184
0
            break;
185
186
0
        case EVP_CIPH_GCM_MODE:
187
0
            ret = evp_cipher_get_asn1_aead_params(c, type, asn1_params);
188
0
            break;
189
190
0
        case EVP_CIPH_CCM_MODE:
191
0
        case EVP_CIPH_XTS_MODE:
192
0
        case EVP_CIPH_OCB_MODE:
193
0
            ret = -2;
194
0
            break;
195
196
0
        default:
197
0
            ret = EVP_CIPHER_get_asn1_iv(c, type) >= 0 ? 1 : -1;
198
0
        }
199
0
    } else if (cipher->prov != NULL) {
200
        /* We cheat, there's no need for an object ID for this use */
201
0
        X509_ALGOR alg;
202
203
0
        alg.algorithm = NULL;
204
0
        alg.parameter = type;
205
206
0
        ret = EVP_CIPHER_CTX_set_algor_params(c, &alg);
207
0
    } else {
208
0
        ret = -2;
209
0
    }
210
211
0
err:
212
0
    if (ret == -2)
213
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER);
214
0
    else if (ret <= 0)
215
0
        ERR_raise(ERR_LIB_EVP, EVP_R_CIPHER_PARAMETER_ERROR);
216
0
    if (ret < -1)
217
0
        ret = -1;
218
0
    return ret;
219
0
}
220
221
int evp_cipher_get_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
222
    evp_cipher_aead_asn1_params *asn1_params)
223
12
{
224
12
    int i = 0;
225
12
    long tl;
226
12
    unsigned char iv[EVP_MAX_IV_LENGTH];
227
228
12
    if (type == NULL || asn1_params == NULL)
229
12
        return 0;
230
231
0
    i = ossl_asn1_type_get_octetstring_int(type, &tl, iv, EVP_MAX_IV_LENGTH);
232
0
    if (i <= 0 || i > EVP_MAX_IV_LENGTH)
233
0
        return -1;
234
235
0
    memcpy(asn1_params->iv, iv, i);
236
0
    asn1_params->iv_len = i;
237
238
0
    return i;
239
0
}
240
241
int evp_cipher_set_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
242
    evp_cipher_aead_asn1_params *asn1_params)
243
0
{
244
0
    if (type == NULL || asn1_params == NULL)
245
0
        return 0;
246
247
0
    return ossl_asn1_type_set_octetstring_int(type, asn1_params->tag_len,
248
0
        asn1_params->iv,
249
0
        asn1_params->iv_len);
250
0
}
251
#endif /* !defined(FIPS_MODULE) */
252
253
/* Convert the various cipher NIDs and dummies to a proper OID NID */
254
int EVP_CIPHER_get_type(const EVP_CIPHER *cipher)
255
18.2k
{
256
18.2k
    int nid;
257
18.2k
    nid = EVP_CIPHER_get_nid(cipher);
258
259
18.2k
    switch (nid) {
260
261
300
    case NID_rc2_cbc:
262
500
    case NID_rc2_64_cbc:
263
700
    case NID_rc2_40_cbc:
264
265
700
        return NID_rc2_cbc;
266
267
100
    case NID_rc4:
268
200
    case NID_rc4_40:
269
270
200
        return NID_rc4;
271
272
100
    case NID_aes_128_cfb128:
273
200
    case NID_aes_128_cfb8:
274
300
    case NID_aes_128_cfb1:
275
276
300
        return NID_aes_128_cfb128;
277
278
100
    case NID_aes_192_cfb128:
279
200
    case NID_aes_192_cfb8:
280
300
    case NID_aes_192_cfb1:
281
282
300
        return NID_aes_192_cfb128;
283
284
100
    case NID_aes_256_cfb128:
285
200
    case NID_aes_256_cfb8:
286
300
    case NID_aes_256_cfb1:
287
288
300
        return NID_aes_256_cfb128;
289
290
100
    case NID_des_cfb64:
291
200
    case NID_des_cfb8:
292
300
    case NID_des_cfb1:
293
294
300
        return NID_des_cfb64;
295
296
100
    case NID_des_ede3_cfb64:
297
200
    case NID_des_ede3_cfb8:
298
300
    case NID_des_ede3_cfb1:
299
300
300
        return NID_des_cfb64;
301
302
15.8k
    default:
303
#ifdef FIPS_MODULE
304
        return NID_undef;
305
#else
306
15.8k
    {
307
        /* Check it has an OID and it is valid */
308
15.8k
        ASN1_OBJECT *otmp = OBJ_nid2obj(nid);
309
310
15.8k
        if (OBJ_get0_data(otmp) == NULL)
311
4.80k
            nid = NID_undef;
312
15.8k
        ASN1_OBJECT_free(otmp);
313
15.8k
        return nid;
314
200
    }
315
18.2k
#endif
316
18.2k
    }
317
18.2k
}
318
319
int evp_cipher_cache_constants(EVP_CIPHER *cipher)
320
4.79k
{
321
4.79k
    int ok, aead = 0, custom_iv = 0, cts = 0, multiblock = 0, randkey = 0;
322
4.79k
    size_t ivlen = 0;
323
4.79k
    size_t blksz = 0;
324
4.79k
    size_t keylen = 0;
325
4.79k
    unsigned int mode = 0;
326
4.79k
    OSSL_PARAM params[10];
327
328
4.79k
    params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_BLOCK_SIZE, &blksz);
329
4.79k
    params[1] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &ivlen);
330
4.79k
    params[2] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &keylen);
331
4.79k
    params[3] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_MODE, &mode);
332
4.79k
    params[4] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_AEAD, &aead);
333
4.79k
    params[5] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_CUSTOM_IV,
334
4.79k
        &custom_iv);
335
4.79k
    params[6] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_CTS, &cts);
336
4.79k
    params[7] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK,
337
4.79k
        &multiblock);
338
4.79k
    params[8] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_HAS_RAND_KEY,
339
4.79k
        &randkey);
340
4.79k
    params[9] = OSSL_PARAM_construct_end();
341
4.79k
    ok = evp_do_ciph_getparams(cipher, params) > 0;
342
4.79k
    if (ok) {
343
4.79k
        cipher->block_size = blksz;
344
4.79k
        cipher->iv_len = ivlen;
345
4.79k
        cipher->key_len = keylen;
346
4.79k
        cipher->flags = mode;
347
4.79k
        if (aead)
348
1.02k
            cipher->flags |= EVP_CIPH_FLAG_AEAD_CIPHER;
349
4.79k
        if (custom_iv)
350
1.49k
            cipher->flags |= EVP_CIPH_CUSTOM_IV;
351
4.79k
        if (cts)
352
222
            cipher->flags |= EVP_CIPH_FLAG_CTS;
353
4.79k
        if (multiblock)
354
148
            cipher->flags |= EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK;
355
4.79k
        if (cipher->ccipher != NULL)
356
4.34k
            cipher->flags |= EVP_CIPH_FLAG_CUSTOM_CIPHER;
357
4.79k
        if (randkey)
358
407
            cipher->flags |= EVP_CIPH_RAND_KEY;
359
4.79k
        if (OSSL_PARAM_locate_const(EVP_CIPHER_gettable_ctx_params(cipher),
360
4.79k
                OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS))
361
0
            cipher->flags |= EVP_CIPH_FLAG_CUSTOM_ASN1;
362
4.79k
    }
363
4.79k
    return ok;
364
4.79k
}
365
366
int EVP_CIPHER_get_block_size(const EVP_CIPHER *cipher)
367
7.79M
{
368
7.79M
    return (cipher == NULL) ? 0 : cipher->block_size;
369
7.79M
}
370
371
int EVP_CIPHER_CTX_get_block_size(const EVP_CIPHER_CTX *ctx)
372
3.87M
{
373
3.87M
    return (ctx == NULL) ? 0 : EVP_CIPHER_get_block_size(ctx->cipher);
374
3.87M
}
375
376
int EVP_CIPHER_impl_ctx_size(const EVP_CIPHER *e)
377
0
{
378
0
    return e->ctx_size;
379
0
}
380
381
int EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
382
    const unsigned char *in, unsigned int inl)
383
6.03k
{
384
6.03k
    if (ctx == NULL || ctx->cipher == NULL)
385
0
        return 0;
386
387
6.03k
    if (ctx->cipher->prov != NULL) {
388
        /*
389
         * If the provided implementation has a ccipher function, we use it,
390
         * and translate its return value like this: 0 => -1, 1 => outlen
391
         *
392
         * Otherwise, we call the cupdate function if in != NULL, or cfinal
393
         * if in == NULL.  Regardless of which, we return what we got.
394
         */
395
6.03k
        int ret = -1;
396
6.03k
        size_t outl = 0;
397
6.03k
        size_t blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
398
399
6.03k
        if (blocksize == 0)
400
0
            return 0;
401
402
6.03k
        if (ctx->cipher->ccipher != NULL)
403
6.03k
            ret = ctx->cipher->ccipher(ctx->algctx, out, &outl,
404
6.03k
                      inl + (blocksize == 1 ? 0 : blocksize),
405
6.03k
                      in, (size_t)inl)
406
6.03k
                ? (int)outl
407
6.03k
                : -1;
408
0
        else if (in != NULL)
409
0
            ret = ctx->cipher->cupdate(ctx->algctx, out, &outl,
410
0
                inl + (blocksize == 1 ? 0 : blocksize),
411
0
                in, (size_t)inl);
412
0
        else
413
0
            ret = ctx->cipher->cfinal(ctx->algctx, out, &outl,
414
0
                blocksize == 1 ? 0 : blocksize);
415
416
6.03k
        return ret;
417
6.03k
    }
418
419
0
    return ctx->cipher->do_cipher(ctx, out, in, inl);
420
6.03k
}
421
422
#ifndef OPENSSL_NO_DEPRECATED_3_0
423
const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx)
424
0
{
425
0
    if (ctx == NULL)
426
0
        return NULL;
427
0
    return ctx->cipher;
428
0
}
429
#endif
430
431
const EVP_CIPHER *EVP_CIPHER_CTX_get0_cipher(const EVP_CIPHER_CTX *ctx)
432
2.85M
{
433
2.85M
    if (ctx == NULL)
434
0
        return NULL;
435
2.85M
    return ctx->cipher;
436
2.85M
}
437
438
EVP_CIPHER *EVP_CIPHER_CTX_get1_cipher(EVP_CIPHER_CTX *ctx)
439
0
{
440
0
    EVP_CIPHER *cipher;
441
442
0
    if (ctx == NULL || ctx->cipher == NULL)
443
0
        return NULL;
444
0
    cipher = (EVP_CIPHER *)ctx->cipher;
445
0
    if (!EVP_CIPHER_up_ref(cipher))
446
0
        return NULL;
447
0
    return cipher;
448
0
}
449
450
int EVP_CIPHER_CTX_is_encrypting(const EVP_CIPHER_CTX *ctx)
451
0
{
452
0
    return ctx->encrypt;
453
0
}
454
455
unsigned long EVP_CIPHER_get_flags(const EVP_CIPHER *cipher)
456
2.74M
{
457
2.74M
    return cipher == NULL ? 0 : cipher->flags;
458
2.74M
}
459
460
void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx)
461
0
{
462
0
    return ctx->app_data;
463
0
}
464
465
void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
466
0
{
467
0
    ctx->app_data = data;
468
0
}
469
470
void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX *ctx)
471
0
{
472
0
    return ctx->cipher_data;
473
0
}
474
475
void *EVP_CIPHER_CTX_set_cipher_data(EVP_CIPHER_CTX *ctx, void *cipher_data)
476
0
{
477
0
    void *old_cipher_data;
478
479
0
    old_cipher_data = ctx->cipher_data;
480
0
    ctx->cipher_data = cipher_data;
481
482
0
    return old_cipher_data;
483
0
}
484
485
int EVP_CIPHER_get_iv_length(const EVP_CIPHER *cipher)
486
1.10M
{
487
1.10M
    return (cipher == NULL) ? 0 : cipher->iv_len;
488
1.10M
}
489
490
int EVP_CIPHER_CTX_get_iv_length(const EVP_CIPHER_CTX *ctx)
491
7.67M
{
492
7.67M
    if (ctx->cipher == NULL)
493
0
        return 0;
494
495
7.67M
    if (ctx->iv_len < 0) {
496
723k
        int rv, len = EVP_CIPHER_get_iv_length(ctx->cipher);
497
723k
        size_t v = len;
498
723k
        OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
499
500
723k
        if (ctx->cipher->get_ctx_params != NULL) {
501
723k
            params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN,
502
723k
                &v);
503
723k
            rv = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
504
723k
            if (rv > 0) {
505
723k
                if (OSSL_PARAM_modified(params)
506
723k
                    && !OSSL_PARAM_get_int(params, &len))
507
0
                    return -1;
508
723k
            } else if (rv != EVP_CTRL_RET_UNSUPPORTED) {
509
0
                return -1;
510
0
            }
511
723k
        }
512
        /* Code below to be removed when legacy support is dropped. */
513
0
        else if ((EVP_CIPHER_get_flags(ctx->cipher)
514
0
                     & EVP_CIPH_CUSTOM_IV_LENGTH)
515
0
            != 0) {
516
0
            rv = EVP_CIPHER_CTX_ctrl((EVP_CIPHER_CTX *)ctx, EVP_CTRL_GET_IVLEN,
517
0
                0, &len);
518
0
            if (rv <= 0)
519
0
                return -1;
520
0
        }
521
        /*-
522
         * Casting away the const is annoying but required here.  We need to
523
         * cache the result for performance reasons.
524
         */
525
723k
        ((EVP_CIPHER_CTX *)ctx)->iv_len = len;
526
723k
    }
527
7.67M
    return ctx->iv_len;
528
7.67M
}
529
530
int EVP_CIPHER_CTX_get_tag_length(const EVP_CIPHER_CTX *ctx)
531
0
{
532
0
    int ret;
533
0
    size_t v = 0;
534
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
535
536
0
    params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, &v);
537
0
    ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
538
0
    return ret == 1 ? (int)v : 0;
539
0
}
540
541
#ifndef OPENSSL_NO_DEPRECATED_3_0
542
const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX *ctx)
543
0
{
544
0
    int ok;
545
0
    const unsigned char *v = ctx->oiv;
546
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
547
548
0
    params[0] = OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV,
549
0
        (void **)&v, sizeof(ctx->oiv));
550
0
    ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
551
552
0
    return ok != 0 ? v : NULL;
553
0
}
554
555
/*
556
 * OSSL_PARAM_OCTET_PTR gets us the pointer to the running IV in the provider
557
 */
558
const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx)
559
0
{
560
0
    int ok;
561
0
    const unsigned char *v = ctx->iv;
562
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
563
564
0
    params[0] = OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_UPDATED_IV,
565
0
        (void **)&v, sizeof(ctx->iv));
566
0
    ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
567
568
0
    return ok != 0 ? v : NULL;
569
0
}
570
571
unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
572
0
{
573
0
    int ok;
574
0
    unsigned char *v = ctx->iv;
575
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
576
577
0
    params[0] = OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_UPDATED_IV,
578
0
        (void **)&v, sizeof(ctx->iv));
579
0
    ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
580
581
0
    return ok != 0 ? v : NULL;
582
0
}
583
#endif /* OPENSSL_NO_DEPRECATED_3_0_0 */
584
585
int EVP_CIPHER_CTX_get_updated_iv(EVP_CIPHER_CTX *ctx, void *buf, size_t len)
586
0
{
587
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
588
589
0
    params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_UPDATED_IV, buf, len);
590
0
    return evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params) > 0;
591
0
}
592
593
int EVP_CIPHER_CTX_get_original_iv(EVP_CIPHER_CTX *ctx, void *buf, size_t len)
594
0
{
595
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
596
597
0
    params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_IV, buf, len);
598
0
    return evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params) > 0;
599
0
}
600
601
unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX *ctx)
602
0
{
603
0
    return ctx->buf;
604
0
}
605
606
int EVP_CIPHER_CTX_get_num(const EVP_CIPHER_CTX *ctx)
607
0
{
608
0
    int ok;
609
0
    unsigned int v = (unsigned int)ctx->num;
610
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
611
612
0
    params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &v);
613
0
    ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
614
615
0
    return ok != 0 ? (int)v : EVP_CTRL_RET_UNSUPPORTED;
616
0
}
617
618
int EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num)
619
0
{
620
0
    int ok;
621
0
    unsigned int n = (unsigned int)num;
622
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
623
624
0
    params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &n);
625
0
    ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
626
627
0
    if (ok != 0)
628
0
        ctx->num = (int)n;
629
0
    return ok != 0;
630
0
}
631
632
int EVP_CIPHER_get_key_length(const EVP_CIPHER *cipher)
633
570k
{
634
570k
    return cipher->key_len;
635
570k
}
636
637
int EVP_CIPHER_CTX_get_key_length(const EVP_CIPHER_CTX *ctx)
638
1.05M
{
639
1.05M
    if (ctx->cipher == NULL)
640
8
        return 0;
641
642
1.05M
    if (ctx->key_len <= 0 && ctx->cipher->prov != NULL) {
643
853k
        int ok;
644
853k
        OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
645
853k
        size_t len;
646
647
853k
        params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &len);
648
853k
        ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
649
853k
        if (ok <= 0)
650
0
            return EVP_CTRL_RET_UNSUPPORTED;
651
652
        /*-
653
         * The if branch should never be taken since EVP_MAX_KEY_LENGTH is
654
         * less than INT_MAX but best to be safe.
655
         *
656
         * Casting away the const is annoying but required here.  We need to
657
         * cache the result for performance reasons.
658
         */
659
853k
        if (!OSSL_PARAM_get_int(params, &((EVP_CIPHER_CTX *)ctx)->key_len))
660
0
            return -1;
661
853k
        ((EVP_CIPHER_CTX *)ctx)->key_len = (int)len;
662
853k
    }
663
1.05M
    return ctx->key_len;
664
1.05M
}
665
666
int EVP_CIPHER_get_nid(const EVP_CIPHER *cipher)
667
25.7k
{
668
25.7k
    return (cipher == NULL) ? NID_undef : cipher->nid;
669
25.7k
}
670
671
int EVP_CIPHER_CTX_get_nid(const EVP_CIPHER_CTX *ctx)
672
0
{
673
0
    return EVP_CIPHER_get_nid(ctx->cipher);
674
0
}
675
676
int EVP_CIPHER_is_a(const EVP_CIPHER *cipher, const char *name)
677
255k
{
678
255k
    if (cipher == NULL)
679
0
        return 0;
680
255k
    if (cipher->prov != NULL)
681
255k
        return evp_is_a(cipher->prov, cipher->name_id, NULL, name);
682
0
    return evp_is_a(NULL, 0, EVP_CIPHER_get0_name(cipher), name);
683
255k
}
684
685
int evp_cipher_get_number(const EVP_CIPHER *cipher)
686
0
{
687
0
    return cipher->name_id;
688
0
}
689
690
const char *EVP_CIPHER_get0_name(const EVP_CIPHER *cipher)
691
0
{
692
0
    if (cipher->type_name != NULL)
693
0
        return cipher->type_name;
694
0
#ifndef FIPS_MODULE
695
0
    return OBJ_nid2sn(EVP_CIPHER_get_nid(cipher));
696
#else
697
    return NULL;
698
#endif
699
0
}
700
701
const char *EVP_CIPHER_get0_description(const EVP_CIPHER *cipher)
702
0
{
703
0
    if (cipher->description != NULL)
704
0
        return cipher->description;
705
0
#ifndef FIPS_MODULE
706
0
    return OBJ_nid2ln(EVP_CIPHER_get_nid(cipher));
707
#else
708
    return NULL;
709
#endif
710
0
}
711
712
int EVP_CIPHER_names_do_all(const EVP_CIPHER *cipher,
713
    void (*fn)(const char *name, void *data),
714
    void *data)
715
0
{
716
0
    if (cipher->prov != NULL)
717
0
        return evp_names_do_all(cipher->prov, cipher->name_id, fn, data);
718
719
0
    return 1;
720
0
}
721
722
const OSSL_PROVIDER *EVP_CIPHER_get0_provider(const EVP_CIPHER *cipher)
723
3.02M
{
724
3.02M
    return cipher->prov;
725
3.02M
}
726
727
int EVP_CIPHER_get_mode(const EVP_CIPHER *cipher)
728
1.91M
{
729
1.91M
    return EVP_CIPHER_get_flags(cipher) & EVP_CIPH_MODE;
730
1.91M
}
731
732
int EVP_MD_is_a(const EVP_MD *md, const char *name)
733
1.15M
{
734
1.15M
    if (md == NULL)
735
0
        return 0;
736
1.15M
    if (md->prov != NULL)
737
730k
        return evp_is_a(md->prov, md->name_id, NULL, name);
738
422k
    return evp_is_a(NULL, 0, EVP_MD_get0_name(md), name);
739
1.15M
}
740
741
int evp_md_get_number(const EVP_MD *md)
742
0
{
743
0
    return md->name_id;
744
0
}
745
746
const char *EVP_MD_get0_description(const EVP_MD *md)
747
0
{
748
0
    if (md->description != NULL)
749
0
        return md->description;
750
0
#ifndef FIPS_MODULE
751
0
    return OBJ_nid2ln(EVP_MD_nid(md));
752
#else
753
    return NULL;
754
#endif
755
0
}
756
757
const char *EVP_MD_get0_name(const EVP_MD *md)
758
2.49M
{
759
2.49M
    if (md == NULL)
760
0
        return NULL;
761
2.49M
    if (md->type_name != NULL)
762
1.62M
        return md->type_name;
763
864k
#ifndef FIPS_MODULE
764
864k
    return OBJ_nid2sn(EVP_MD_nid(md));
765
#else
766
    return NULL;
767
#endif
768
2.49M
}
769
770
int EVP_MD_names_do_all(const EVP_MD *md,
771
    void (*fn)(const char *name, void *data),
772
    void *data)
773
0
{
774
0
    if (md->prov != NULL)
775
0
        return evp_names_do_all(md->prov, md->name_id, fn, data);
776
777
0
    return 1;
778
0
}
779
780
const OSSL_PROVIDER *EVP_MD_get0_provider(const EVP_MD *md)
781
3.98M
{
782
3.98M
    return md->prov;
783
3.98M
}
784
785
int EVP_MD_get_type(const EVP_MD *md)
786
5.10M
{
787
5.10M
    return md->type;
788
5.10M
}
789
790
int EVP_MD_get_pkey_type(const EVP_MD *md)
791
0
{
792
0
    return md->pkey_type;
793
0
}
794
795
int EVP_MD_get_block_size(const EVP_MD *md)
796
4.72M
{
797
4.72M
    if (md == NULL) {
798
0
        ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
799
0
        return -1;
800
0
    }
801
4.72M
    return md->block_size;
802
4.72M
}
803
804
int EVP_MD_get_size(const EVP_MD *md)
805
316M
{
806
316M
    if (md == NULL) {
807
5
        ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
808
5
        return -1;
809
5
    }
810
316M
    return md->md_size;
811
316M
}
812
813
int EVP_MD_xof(const EVP_MD *md)
814
2.78M
{
815
2.78M
    return md != NULL && ((EVP_MD_get_flags(md) & EVP_MD_FLAG_XOF) != 0);
816
2.78M
}
817
818
unsigned long EVP_MD_get_flags(const EVP_MD *md)
819
3.10M
{
820
3.10M
    return md->flags;
821
3.10M
}
822
823
EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type)
824
0
{
825
0
    EVP_MD *md = evp_md_new();
826
827
0
    if (md != NULL) {
828
0
        md->type = md_type;
829
0
        md->pkey_type = pkey_type;
830
0
        md->origin = EVP_ORIG_METH;
831
0
    }
832
0
    return md;
833
0
}
834
835
EVP_MD *EVP_MD_meth_dup(const EVP_MD *md)
836
0
{
837
0
    EVP_MD *to = NULL;
838
839
    /*
840
     * Non-legacy EVP_MDs can't be duplicated like this.
841
     * Use EVP_MD_up_ref() instead.
842
     */
843
0
    if (md->prov != NULL)
844
0
        return NULL;
845
846
0
    if ((to = EVP_MD_meth_new(md->type, md->pkey_type)) != NULL) {
847
0
        CRYPTO_REF_COUNT refcnt = to->refcnt;
848
849
0
        memcpy(to, md, sizeof(*to));
850
0
        to->refcnt = refcnt;
851
0
        to->origin = EVP_ORIG_METH;
852
0
    }
853
0
    return to;
854
0
}
855
856
void evp_md_free_int(EVP_MD *md)
857
1.74k
{
858
1.74k
    OPENSSL_free(md->type_name);
859
1.74k
    ossl_provider_free(md->prov);
860
1.74k
    CRYPTO_FREE_REF(&md->refcnt);
861
1.74k
    OPENSSL_free(md);
862
1.74k
}
863
864
void EVP_MD_meth_free(EVP_MD *md)
865
0
{
866
0
    if (md == NULL || md->origin != EVP_ORIG_METH)
867
0
        return;
868
869
0
    evp_md_free_int(md);
870
0
}
871
872
int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize)
873
0
{
874
0
    if (md->block_size != 0)
875
0
        return 0;
876
877
0
    md->block_size = blocksize;
878
0
    return 1;
879
0
}
880
int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize)
881
0
{
882
0
    if (md->md_size != 0)
883
0
        return 0;
884
885
0
    md->md_size = resultsize;
886
0
    return 1;
887
0
}
888
int EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize)
889
0
{
890
0
    if (md->ctx_size != 0)
891
0
        return 0;
892
893
0
    md->ctx_size = datasize;
894
0
    return 1;
895
0
}
896
int EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags)
897
0
{
898
0
    if (md->flags != 0)
899
0
        return 0;
900
901
0
    md->flags = flags;
902
0
    return 1;
903
0
}
904
int EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx))
905
0
{
906
0
    if (md->init != NULL)
907
0
        return 0;
908
909
0
    md->init = init;
910
0
    return 1;
911
0
}
912
int EVP_MD_meth_set_update(EVP_MD *md, int (*update)(EVP_MD_CTX *ctx, const void *data, size_t count))
913
0
{
914
0
    if (md->update != NULL)
915
0
        return 0;
916
917
0
    md->update = update;
918
0
    return 1;
919
0
}
920
int EVP_MD_meth_set_final(EVP_MD *md, int (*final)(EVP_MD_CTX *ctx, unsigned char *md))
921
0
{
922
0
    if (md->final != NULL)
923
0
        return 0;
924
925
0
    md->final = final;
926
0
    return 1;
927
0
}
928
int EVP_MD_meth_set_copy(EVP_MD *md, int (*copy)(EVP_MD_CTX *to, const EVP_MD_CTX *from))
929
0
{
930
0
    if (md->copy != NULL)
931
0
        return 0;
932
933
0
    md->copy = copy;
934
0
    return 1;
935
0
}
936
int EVP_MD_meth_set_cleanup(EVP_MD *md, int (*cleanup)(EVP_MD_CTX *ctx))
937
0
{
938
0
    if (md->cleanup != NULL)
939
0
        return 0;
940
941
0
    md->cleanup = cleanup;
942
0
    return 1;
943
0
}
944
int EVP_MD_meth_set_ctrl(EVP_MD *md, int (*ctrl)(EVP_MD_CTX *ctx, int cmd, int p1, void *p2))
945
0
{
946
0
    if (md->md_ctrl != NULL)
947
0
        return 0;
948
949
0
    md->md_ctrl = ctrl;
950
0
    return 1;
951
0
}
952
953
int EVP_MD_meth_get_input_blocksize(const EVP_MD *md)
954
0
{
955
0
    return md->block_size;
956
0
}
957
int EVP_MD_meth_get_result_size(const EVP_MD *md)
958
0
{
959
0
    return md->md_size;
960
0
}
961
int EVP_MD_meth_get_app_datasize(const EVP_MD *md)
962
0
{
963
0
    return md->ctx_size;
964
0
}
965
unsigned long EVP_MD_meth_get_flags(const EVP_MD *md)
966
0
{
967
0
    return md->flags;
968
0
}
969
int (*EVP_MD_meth_get_init(const EVP_MD *md))(EVP_MD_CTX *ctx)
970
0
{
971
0
    return md->init;
972
0
}
973
int (*EVP_MD_meth_get_update(const EVP_MD *md))(EVP_MD_CTX *ctx,
974
    const void *data,
975
    size_t count)
976
0
{
977
0
    return md->update;
978
0
}
979
int (*EVP_MD_meth_get_final(const EVP_MD *md))(EVP_MD_CTX *ctx,
980
    unsigned char *md)
981
0
{
982
0
    return md->final;
983
0
}
984
int (*EVP_MD_meth_get_copy(const EVP_MD *md))(EVP_MD_CTX *to,
985
    const EVP_MD_CTX *from)
986
0
{
987
0
    return md->copy;
988
0
}
989
int (*EVP_MD_meth_get_cleanup(const EVP_MD *md))(EVP_MD_CTX *ctx)
990
0
{
991
0
    return md->cleanup;
992
0
}
993
int (*EVP_MD_meth_get_ctrl(const EVP_MD *md))(EVP_MD_CTX *ctx, int cmd,
994
    int p1, void *p2)
995
0
{
996
0
    return md->md_ctrl;
997
0
}
998
999
#ifndef OPENSSL_NO_DEPRECATED_3_0
1000
const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx)
1001
0
{
1002
0
    if (ctx == NULL)
1003
0
        return NULL;
1004
0
    return ctx->reqdigest;
1005
0
}
1006
#endif
1007
1008
const EVP_MD *EVP_MD_CTX_get0_md(const EVP_MD_CTX *ctx)
1009
305M
{
1010
305M
    if (ctx == NULL)
1011
56.8k
        return NULL;
1012
305M
    return ctx->reqdigest;
1013
305M
}
1014
1015
EVP_MD *EVP_MD_CTX_get1_md(EVP_MD_CTX *ctx)
1016
0
{
1017
0
    EVP_MD *md;
1018
1019
0
    if (ctx == NULL)
1020
0
        return NULL;
1021
0
    md = (EVP_MD *)ctx->reqdigest;
1022
0
    if (md == NULL || !EVP_MD_up_ref(md))
1023
0
        return NULL;
1024
0
    return md;
1025
0
}
1026
1027
int EVP_MD_CTX_get_size_ex(const EVP_MD_CTX *ctx)
1028
85.1M
{
1029
85.1M
    EVP_MD_CTX *c = (EVP_MD_CTX *)ctx;
1030
85.1M
    const OSSL_PARAM *gettables;
1031
1032
85.1M
    gettables = EVP_MD_CTX_gettable_params(c);
1033
85.1M
    if (gettables != NULL
1034
100k
        && OSSL_PARAM_locate_const(gettables,
1035
100k
               OSSL_DIGEST_PARAM_SIZE)
1036
100k
            != NULL) {
1037
100k
        OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1038
100k
        size_t sz = 0;
1039
1040
        /*
1041
         * For XOF's EVP_MD_get_size() returns 0
1042
         * So try to get the xoflen instead. This will return -1 if the
1043
         * xof length has not been set.
1044
         */
1045
100k
        params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE, &sz);
1046
100k
        if (EVP_MD_CTX_get_params(c, params) != 1
1047
100k
            || sz == SIZE_MAX
1048
100k
            || sz == 0)
1049
13
            return -1;
1050
100k
        return sz;
1051
100k
    }
1052
    /* Normal digests have a constant fixed size output */
1053
85.0M
    return EVP_MD_get_size(EVP_MD_CTX_get0_md(ctx));
1054
85.1M
}
1055
1056
EVP_PKEY_CTX *EVP_MD_CTX_get_pkey_ctx(const EVP_MD_CTX *ctx)
1057
208k
{
1058
208k
    return ctx->pctx;
1059
208k
}
1060
1061
#if !defined(FIPS_MODULE)
1062
void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx)
1063
14.6k
{
1064
    /*
1065
     * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so
1066
     * we have to deal with the cleanup job here.
1067
     */
1068
14.6k
    if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
1069
14.6k
        EVP_PKEY_CTX_free(ctx->pctx);
1070
1071
14.6k
    ctx->pctx = pctx;
1072
1073
14.6k
    if (pctx != NULL) {
1074
        /* make sure pctx is not freed when destroying EVP_MD_CTX */
1075
14.6k
        EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
1076
14.6k
    } else {
1077
0
        EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
1078
0
    }
1079
14.6k
}
1080
#endif /* !defined(FIPS_MODULE) */
1081
1082
void *EVP_MD_CTX_get0_md_data(const EVP_MD_CTX *ctx)
1083
0
{
1084
0
    return ctx->md_data;
1085
0
}
1086
1087
int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
1088
    const void *data, size_t count)
1089
0
{
1090
0
    return ctx->update;
1091
0
}
1092
1093
void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
1094
    int (*update)(EVP_MD_CTX *ctx,
1095
        const void *data, size_t count))
1096
0
{
1097
0
    ctx->update = update;
1098
0
}
1099
1100
void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags)
1101
12.0M
{
1102
12.0M
    ctx->flags |= flags;
1103
12.0M
}
1104
1105
void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags)
1106
502M
{
1107
502M
    ctx->flags &= ~flags;
1108
502M
}
1109
1110
int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags)
1111
29.1M
{
1112
29.1M
    return (ctx->flags & flags);
1113
29.1M
}
1114
1115
static int evp_cipher_ctx_enable_use_bits(EVP_CIPHER_CTX *ctx,
1116
    unsigned int enable)
1117
0
{
1118
0
    OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
1119
1120
0
    params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_USE_BITS, &enable);
1121
0
    return EVP_CIPHER_CTX_set_params(ctx, params);
1122
0
}
1123
1124
void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags)
1125
18.6k
{
1126
18.6k
    int oldflags = ctx->flags;
1127
1128
18.6k
    ctx->flags |= flags;
1129
18.6k
    if (((oldflags ^ ctx->flags) & EVP_CIPH_FLAG_LENGTH_BITS) != 0)
1130
0
        evp_cipher_ctx_enable_use_bits(ctx, 1);
1131
18.6k
}
1132
1133
void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags)
1134
0
{
1135
0
    int oldflags = ctx->flags;
1136
1137
0
    ctx->flags &= ~flags;
1138
0
    if (((oldflags ^ ctx->flags) & EVP_CIPH_FLAG_LENGTH_BITS) != 0)
1139
0
        evp_cipher_ctx_enable_use_bits(ctx, 0);
1140
0
}
1141
1142
int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags)
1143
0
{
1144
0
    return (ctx->flags & flags);
1145
0
}
1146
1147
#if !defined(FIPS_MODULE)
1148
1149
int EVP_PKEY_CTX_set_group_name(EVP_PKEY_CTX *ctx, const char *name)
1150
133k
{
1151
133k
    OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
1152
1153
133k
    if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1154
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1155
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1156
0
        return -2;
1157
0
    }
1158
1159
133k
    if (name == NULL)
1160
0
        return -1;
1161
1162
133k
    params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
1163
133k
        (char *)name, 0);
1164
133k
    return EVP_PKEY_CTX_set_params(ctx, params);
1165
133k
}
1166
1167
int EVP_PKEY_CTX_get_group_name(EVP_PKEY_CTX *ctx, char *name, size_t namelen)
1168
0
{
1169
0
    OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
1170
0
    OSSL_PARAM *p = params;
1171
1172
0
    if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1173
        /* There is no legacy support for this */
1174
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1175
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1176
0
        return -2;
1177
0
    }
1178
1179
0
    if (name == NULL)
1180
0
        return -1;
1181
1182
0
    *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
1183
0
        name, namelen);
1184
0
    if (!EVP_PKEY_CTX_get_params(ctx, params))
1185
0
        return -1;
1186
0
    return 1;
1187
0
}
1188
#endif /* !FIPS_MODULE */
1189
1190
/*
1191
 * evp_pkey_keygen() abstracts from the explicit use of B<EVP_PKEY_CTX>
1192
 * while providing a generic way of generating a new asymmetric key pair
1193
 * of algorithm type I<name> (e.g., C<RSA> or C<EC>).
1194
 * The library context I<libctx> and property query I<propq>
1195
 * are used when fetching algorithms from providers.
1196
 * The I<params> specify algorithm-specific parameters
1197
 * such as the RSA modulus size or the name of an EC curve.
1198
 */
1199
static EVP_PKEY *evp_pkey_keygen(OSSL_LIB_CTX *libctx, const char *name,
1200
    const char *propq, const OSSL_PARAM *params)
1201
99.2k
{
1202
99.2k
    EVP_PKEY *pkey = NULL;
1203
99.2k
    EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name(libctx, name, propq);
1204
1205
99.2k
    if (ctx != NULL
1206
99.2k
        && EVP_PKEY_keygen_init(ctx) > 0
1207
99.2k
        && EVP_PKEY_CTX_set_params(ctx, params))
1208
99.2k
        (void)EVP_PKEY_generate(ctx, &pkey);
1209
1210
99.2k
    EVP_PKEY_CTX_free(ctx);
1211
99.2k
    return pkey;
1212
99.2k
}
1213
1214
EVP_PKEY *EVP_PKEY_Q_keygen(OSSL_LIB_CTX *libctx, const char *propq,
1215
    const char *type, ...)
1216
99.2k
{
1217
99.2k
    va_list args;
1218
99.2k
    size_t bits;
1219
99.2k
    char *name;
1220
99.2k
    OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
1221
99.2k
    EVP_PKEY *ret = NULL;
1222
1223
99.2k
    va_start(args, type);
1224
1225
99.2k
    if (OPENSSL_strcasecmp(type, "RSA") == 0) {
1226
0
        bits = va_arg(args, size_t);
1227
0
        params[0] = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_BITS, &bits);
1228
99.2k
    } else if (OPENSSL_strcasecmp(type, "EC") == 0) {
1229
0
        name = va_arg(args, char *);
1230
0
        params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
1231
0
            name, 0);
1232
0
    }
1233
1234
99.2k
    ret = evp_pkey_keygen(libctx, type, propq, params);
1235
1236
99.2k
    va_end(args);
1237
99.2k
    return ret;
1238
99.2k
}
1239
1240
#if !defined(FIPS_MODULE)
1241
int EVP_CIPHER_CTX_set_algor_params(EVP_CIPHER_CTX *ctx, const X509_ALGOR *alg)
1242
0
{
1243
0
    int ret = -1; /* Assume the worst */
1244
0
    unsigned char *der = NULL;
1245
0
    int derl = -1;
1246
1247
0
    if ((derl = i2d_ASN1_TYPE(alg->parameter, &der)) >= 0) {
1248
0
        const char *k_old = OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS_OLD;
1249
0
        const char *k_new = OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS;
1250
0
        OSSL_PARAM params[3];
1251
1252
        /*
1253
         * Passing the same data with both the old (deprecated) and the
1254
         * new AlgID parameters OSSL_PARAM key.
1255
         */
1256
0
        params[0] = OSSL_PARAM_construct_octet_string(k_old, der, (size_t)derl);
1257
0
        params[1] = OSSL_PARAM_construct_octet_string(k_new, der, (size_t)derl);
1258
0
        params[2] = OSSL_PARAM_construct_end();
1259
0
        ret = EVP_CIPHER_CTX_set_params(ctx, params);
1260
0
    }
1261
0
    OPENSSL_free(der);
1262
0
    return ret;
1263
0
}
1264
1265
int EVP_CIPHER_CTX_get_algor_params(EVP_CIPHER_CTX *ctx, X509_ALGOR *alg)
1266
0
{
1267
0
    int ret = -1; /* Assume the worst */
1268
0
    unsigned char *der = NULL;
1269
0
    size_t derl;
1270
0
    ASN1_TYPE *type = NULL;
1271
0
    int i = -1;
1272
0
    const char *k_old = OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS_OLD;
1273
0
    const char *k_new = OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS;
1274
0
    const char *derk;
1275
0
    OSSL_PARAM params[3];
1276
1277
    /*
1278
     * We make two passes, the first to get the appropriate buffer size,
1279
     * and the second to get the actual value.
1280
     * Also, using both the old (deprecated) and the new AlgID parameters
1281
     * OSSL_PARAM key, and using whichever the provider responds to.
1282
     * Should the provider respond on both, the new key takes priority.
1283
     */
1284
0
    params[0] = OSSL_PARAM_construct_octet_string(k_old, NULL, 0);
1285
0
    params[1] = OSSL_PARAM_construct_octet_string(k_new, NULL, 0);
1286
0
    params[2] = OSSL_PARAM_construct_end();
1287
1288
0
    if (!EVP_CIPHER_CTX_get_params(ctx, params))
1289
0
        goto err;
1290
1291
    /* ... but, we should get a return size too! */
1292
0
    if (OSSL_PARAM_modified(&params[0]) && params[0].return_size != 0)
1293
0
        i = 0;
1294
0
    if (OSSL_PARAM_modified(&params[1]) && params[1].return_size != 0)
1295
0
        i = 1;
1296
0
    if (i < 0)
1297
0
        goto err;
1298
1299
    /*
1300
     * If alg->parameter is non-NULL, it will be changed by d2i_ASN1_TYPE()
1301
     * below.  If it is NULL, the d2i_ASN1_TYPE() call will allocate new
1302
     * space for it.  Either way, alg->parameter can be safely assigned
1303
     * with type after the d2i_ASN1_TYPE() call, with the safety that it
1304
     * will be ok.
1305
     */
1306
0
    type = alg->parameter;
1307
1308
0
    derk = params[i].key;
1309
0
    derl = params[i].return_size;
1310
0
    if ((der = OPENSSL_malloc(derl)) != NULL) {
1311
0
        unsigned char *derp = der;
1312
1313
0
        params[i] = OSSL_PARAM_construct_octet_string(derk, der, derl);
1314
0
        if (EVP_CIPHER_CTX_get_params(ctx, params)
1315
0
            && OSSL_PARAM_modified(&params[i])
1316
0
            && d2i_ASN1_TYPE(&type, (const unsigned char **)&derp,
1317
0
                   (int)derl)
1318
0
                != NULL) {
1319
            /*
1320
             * Don't free alg->parameter, see comment further up.
1321
             * Worst case, alg->parameter gets assigned its own value.
1322
             */
1323
0
            alg->parameter = type;
1324
0
            ret = 1;
1325
0
        }
1326
0
    }
1327
0
err:
1328
0
    OPENSSL_free(der);
1329
0
    return ret;
1330
0
}
1331
1332
int EVP_CIPHER_CTX_get_algor(EVP_CIPHER_CTX *ctx, X509_ALGOR **alg)
1333
0
{
1334
0
    int ret = -1; /* Assume the worst */
1335
0
    OSSL_PARAM params[2];
1336
0
    size_t aid_len = 0;
1337
0
    const char *k_aid = OSSL_SIGNATURE_PARAM_ALGORITHM_ID;
1338
1339
0
    params[0] = OSSL_PARAM_construct_octet_string(k_aid, NULL, 0);
1340
0
    params[1] = OSSL_PARAM_construct_end();
1341
1342
0
    if (EVP_CIPHER_CTX_get_params(ctx, params) <= 0)
1343
0
        goto err;
1344
1345
0
    if (OSSL_PARAM_modified(&params[0]))
1346
0
        aid_len = params[0].return_size;
1347
0
    if (aid_len == 0) {
1348
0
        ERR_raise(ERR_LIB_EVP, EVP_R_GETTING_ALGORITHMIDENTIFIER_NOT_SUPPORTED);
1349
0
        ret = -2;
1350
0
        goto err;
1351
0
    }
1352
0
    if (alg != NULL) {
1353
0
        unsigned char *aid = NULL;
1354
0
        const unsigned char *pp = NULL;
1355
1356
0
        if ((aid = OPENSSL_malloc(aid_len)) != NULL) {
1357
0
            params[0] = OSSL_PARAM_construct_octet_string(k_aid, aid, aid_len);
1358
0
            pp = aid;
1359
0
            if (EVP_CIPHER_CTX_get_params(ctx, params)
1360
0
                && OSSL_PARAM_modified(&params[0])
1361
0
                && d2i_X509_ALGOR(alg, &pp, aid_len) != NULL)
1362
0
                ret = 1;
1363
0
        }
1364
0
        OPENSSL_free(aid);
1365
0
    }
1366
0
err:
1367
0
    return ret;
1368
0
}
1369
1370
int EVP_PKEY_CTX_set_algor_params(EVP_PKEY_CTX *ctx, const X509_ALGOR *alg)
1371
0
{
1372
0
    int ret = -1; /* Assume the worst */
1373
0
    unsigned char *der = NULL;
1374
0
    int derl = -1;
1375
1376
0
    if ((derl = i2d_ASN1_TYPE(alg->parameter, &der)) >= 0) {
1377
0
        const char *k = OSSL_PKEY_PARAM_ALGORITHM_ID_PARAMS;
1378
0
        OSSL_PARAM params[2];
1379
1380
        /*
1381
         * Passing the same data with both the old (deprecated) and the
1382
         * new AlgID parameters OSSL_PARAM key.
1383
         */
1384
0
        params[0] = OSSL_PARAM_construct_octet_string(k, der, (size_t)derl);
1385
0
        params[1] = OSSL_PARAM_construct_end();
1386
0
        ret = EVP_PKEY_CTX_set_params(ctx, params);
1387
0
    }
1388
0
    OPENSSL_free(der);
1389
0
    return ret;
1390
0
}
1391
1392
int EVP_PKEY_CTX_get_algor_params(EVP_PKEY_CTX *ctx, X509_ALGOR *alg)
1393
0
{
1394
0
    int ret = -1; /* Assume the worst */
1395
0
    OSSL_PARAM params[2];
1396
0
    unsigned char *der = NULL;
1397
0
    size_t derl;
1398
0
    ASN1_TYPE *type = NULL;
1399
0
    const char *k = OSSL_PKEY_PARAM_ALGORITHM_ID_PARAMS;
1400
1401
    /*
1402
     * We make two passes, the first to get the appropriate buffer size,
1403
     * and the second to get the actual value.
1404
     * Also, using both the old (deprecated) and the new AlgID parameters
1405
     * OSSL_PARAM key, and using whichever the provider responds to.
1406
     * Should the provider respond on both, the new key takes priority.
1407
     */
1408
0
    params[0] = OSSL_PARAM_construct_octet_string(k, NULL, 0);
1409
0
    params[1] = OSSL_PARAM_construct_end();
1410
1411
0
    if (!EVP_PKEY_CTX_get_params(ctx, params))
1412
0
        goto err;
1413
1414
    /*
1415
     * If alg->parameter is non-NULL, it will be changed by d2i_ASN1_TYPE()
1416
     * below.  If it is NULL, the d2i_ASN1_TYPE() call will allocate new
1417
     * space for it.  Either way, alg->parameter can be safely assigned
1418
     * with type after the d2i_ASN1_TYPE() call, with the safety that it
1419
     * will be ok.
1420
     */
1421
0
    type = alg->parameter;
1422
1423
0
    derl = params[0].return_size;
1424
0
    if (OSSL_PARAM_modified(&params[0])
1425
        /* ... but, we should get a return size too! */
1426
0
        && derl != 0
1427
0
        && (der = OPENSSL_malloc(derl)) != NULL) {
1428
0
        unsigned char *derp = der;
1429
1430
0
        params[0] = OSSL_PARAM_construct_octet_string(k, der, derl);
1431
0
        if (EVP_PKEY_CTX_get_params(ctx, params)
1432
0
            && OSSL_PARAM_modified(&params[0])
1433
0
            && d2i_ASN1_TYPE(&type, (const unsigned char **)&derp,
1434
0
                   derl)
1435
0
                != NULL) {
1436
            /*
1437
             * Don't free alg->parameter, see comment further up.
1438
             * Worst case, alg->parameter gets assigned its own value.
1439
             */
1440
0
            alg->parameter = type;
1441
0
            ret = 1;
1442
0
        }
1443
0
    }
1444
0
err:
1445
0
    OPENSSL_free(der);
1446
0
    return ret;
1447
0
}
1448
1449
int EVP_PKEY_CTX_get_algor(EVP_PKEY_CTX *ctx, X509_ALGOR **alg)
1450
0
{
1451
0
    int ret = -1; /* Assume the worst */
1452
0
    OSSL_PARAM params[2];
1453
0
    size_t aid_len = 0;
1454
0
    const char *k_aid = OSSL_SIGNATURE_PARAM_ALGORITHM_ID;
1455
1456
0
    params[0] = OSSL_PARAM_construct_octet_string(k_aid, NULL, 0);
1457
0
    params[1] = OSSL_PARAM_construct_end();
1458
1459
0
    if (EVP_PKEY_CTX_get_params(ctx, params) <= 0)
1460
0
        goto err;
1461
1462
0
    if (OSSL_PARAM_modified(&params[0]))
1463
0
        aid_len = params[0].return_size;
1464
0
    if (aid_len == 0) {
1465
0
        ERR_raise(ERR_LIB_EVP, EVP_R_GETTING_ALGORITHMIDENTIFIER_NOT_SUPPORTED);
1466
0
        ret = -2;
1467
0
        goto err;
1468
0
    }
1469
0
    if (alg != NULL) {
1470
0
        unsigned char *aid = NULL;
1471
0
        const unsigned char *pp = NULL;
1472
1473
0
        if ((aid = OPENSSL_malloc(aid_len)) != NULL) {
1474
0
            params[0] = OSSL_PARAM_construct_octet_string(k_aid, aid, aid_len);
1475
0
            pp = aid;
1476
0
            if (EVP_PKEY_CTX_get_params(ctx, params)
1477
0
                && OSSL_PARAM_modified(&params[0])
1478
0
                && d2i_X509_ALGOR(alg, &pp, aid_len) != NULL)
1479
0
                ret = 1;
1480
0
        }
1481
0
        OPENSSL_free(aid);
1482
0
    }
1483
0
err:
1484
0
    return ret;
1485
0
}
1486
1487
#endif /* !defined(FIPS_MODULE) */