Coverage Report

Created: 2025-12-31 06:58

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