Coverage Report

Created: 2024-05-21 06:33

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