Coverage Report

Created: 2024-07-20 06:51

/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
183
{
287
183
    int nid;
288
183
    nid = EVP_CIPHER_get_nid(cipher);
289
290
183
    switch (nid) {
291
292
3
    case NID_rc2_cbc:
293
5
    case NID_rc2_64_cbc:
294
7
    case NID_rc2_40_cbc:
295
296
7
        return NID_rc2_cbc;
297
298
1
    case NID_rc4:
299
2
    case NID_rc4_40:
300
301
2
        return NID_rc4;
302
303
1
    case NID_aes_128_cfb128:
304
2
    case NID_aes_128_cfb8:
305
3
    case NID_aes_128_cfb1:
306
307
3
        return NID_aes_128_cfb128;
308
309
1
    case NID_aes_192_cfb128:
310
2
    case NID_aes_192_cfb8:
311
3
    case NID_aes_192_cfb1:
312
313
3
        return NID_aes_192_cfb128;
314
315
1
    case NID_aes_256_cfb128:
316
2
    case NID_aes_256_cfb8:
317
3
    case NID_aes_256_cfb1:
318
319
3
        return NID_aes_256_cfb128;
320
321
1
    case NID_des_cfb64:
322
2
    case NID_des_cfb8:
323
3
    case NID_des_cfb1:
324
325
3
        return NID_des_cfb64;
326
327
1
    case NID_des_ede3_cfb64:
328
2
    case NID_des_ede3_cfb8:
329
3
    case NID_des_ede3_cfb1:
330
331
3
        return NID_des_cfb64;
332
333
159
    default:
334
#ifdef FIPS_MODULE
335
        return NID_undef;
336
#else
337
159
        {
338
            /* Check it has an OID and it is valid */
339
159
            ASN1_OBJECT *otmp = OBJ_nid2obj(nid);
340
341
159
            if (OBJ_get0_data(otmp) == NULL)
342
48
                nid = NID_undef;
343
159
            ASN1_OBJECT_free(otmp);
344
159
            return nid;
345
2
        }
346
183
#endif
347
183
    }
348
183
}
349
350
int evp_cipher_cache_constants(EVP_CIPHER *cipher)
351
130
{
352
130
    int ok, aead = 0, custom_iv = 0, cts = 0, multiblock = 0, randkey = 0;
353
130
    size_t ivlen = 0;
354
130
    size_t blksz = 0;
355
130
    size_t keylen = 0;
356
130
    unsigned int mode = 0;
357
130
    OSSL_PARAM params[10];
358
359
130
    params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_BLOCK_SIZE, &blksz);
360
130
    params[1] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &ivlen);
361
130
    params[2] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &keylen);
362
130
    params[3] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_MODE, &mode);
363
130
    params[4] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_AEAD, &aead);
364
130
    params[5] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_CUSTOM_IV,
365
130
                                         &custom_iv);
366
130
    params[6] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_CTS, &cts);
367
130
    params[7] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK,
368
130
                                         &multiblock);
369
130
    params[8] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_HAS_RAND_KEY,
370
130
                                         &randkey);
371
130
    params[9] = OSSL_PARAM_construct_end();
372
130
    ok = evp_do_ciph_getparams(cipher, params) > 0;
373
130
    if (ok) {
374
130
        cipher->block_size = blksz;
375
130
        cipher->iv_len = ivlen;
376
130
        cipher->key_len = keylen;
377
130
        cipher->flags = mode;
378
130
        if (aead)
379
28
            cipher->flags |= EVP_CIPH_FLAG_AEAD_CIPHER;
380
130
        if (custom_iv)
381
41
            cipher->flags |= EVP_CIPH_CUSTOM_IV;
382
130
        if (cts)
383
6
            cipher->flags |= EVP_CIPH_FLAG_CTS;
384
130
        if (multiblock)
385
4
            cipher->flags |= EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK;
386
130
        if (cipher->ccipher != NULL)
387
118
            cipher->flags |= EVP_CIPH_FLAG_CUSTOM_CIPHER;
388
130
        if (randkey)
389
11
            cipher->flags |= EVP_CIPH_RAND_KEY;
390
130
        if (OSSL_PARAM_locate_const(EVP_CIPHER_gettable_ctx_params(cipher),
391
130
                                    OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS))
392
0
            cipher->flags |= EVP_CIPH_FLAG_CUSTOM_ASN1;
393
130
    }
394
130
    return ok;
395
130
}
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
1.24k
{
673
1.24k
    if (ctx->cipher == NULL)
674
0
        return 0;
675
676
1.24k
    if (ctx->key_len <= 0 && ctx->cipher->prov != NULL) {
677
1.24k
        int ok;
678
1.24k
        OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
679
1.24k
        size_t len;
680
681
1.24k
        params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &len);
682
1.24k
        ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
683
1.24k
        if (ok <= 0)
684
0
            return EVP_CTRL_RET_UNSUPPORTED;
685
686
        /*-
687
         * The if branch should never be taken since EVP_MAX_KEY_LENGTH is
688
         * less than INT_MAX but best to be safe.
689
         *
690
         * Casting away the const is annoying but required here.  We need to
691
         * cache the result for performance reasons.
692
         */
693
1.24k
        if (!OSSL_PARAM_get_int(params, &((EVP_CIPHER_CTX *)ctx)->key_len))
694
0
            return -1;
695
1.24k
        ((EVP_CIPHER_CTX *)ctx)->key_len = (int)len;
696
1.24k
    }
697
1.24k
    return ctx->key_len;
698
1.24k
}
699
700
int EVP_CIPHER_get_nid(const EVP_CIPHER *cipher)
701
317
{
702
317
    return (cipher == NULL) ? NID_undef : cipher->nid;
703
317
}
704
705
int EVP_CIPHER_CTX_get_nid(const EVP_CIPHER_CTX *ctx)
706
0
{
707
0
    return EVP_CIPHER_get_nid(ctx->cipher);
708
0
}
709
710
int EVP_CIPHER_is_a(const EVP_CIPHER *cipher, const char *name)
711
0
{
712
0
    if (cipher == NULL)
713
0
        return 0;
714
0
    if (cipher->prov != NULL)
715
0
        return evp_is_a(cipher->prov, cipher->name_id, NULL, name);
716
0
    return evp_is_a(NULL, 0, EVP_CIPHER_get0_name(cipher), name);
717
0
}
718
719
int evp_cipher_get_number(const EVP_CIPHER *cipher)
720
0
{
721
0
    return cipher->name_id;
722
0
}
723
724
const char *EVP_CIPHER_get0_name(const EVP_CIPHER *cipher)
725
0
{
726
0
    if (cipher->type_name != NULL)
727
0
        return cipher->type_name;
728
0
#ifndef FIPS_MODULE
729
0
    return OBJ_nid2sn(EVP_CIPHER_get_nid(cipher));
730
#else
731
    return NULL;
732
#endif
733
0
}
734
735
const char *EVP_CIPHER_get0_description(const EVP_CIPHER *cipher)
736
0
{
737
0
    if (cipher->description != NULL)
738
0
        return cipher->description;
739
0
#ifndef FIPS_MODULE
740
0
    return OBJ_nid2ln(EVP_CIPHER_get_nid(cipher));
741
#else
742
    return NULL;
743
#endif
744
0
}
745
746
int EVP_CIPHER_names_do_all(const EVP_CIPHER *cipher,
747
                            void (*fn)(const char *name, void *data),
748
                            void *data)
749
0
{
750
0
    if (cipher->prov != NULL)
751
0
        return evp_names_do_all(cipher->prov, cipher->name_id, fn, data);
752
753
0
    return 1;
754
0
}
755
756
const OSSL_PROVIDER *EVP_CIPHER_get0_provider(const EVP_CIPHER *cipher)
757
130
{
758
130
    return cipher->prov;
759
130
}
760
761
int EVP_CIPHER_get_mode(const EVP_CIPHER *cipher)
762
0
{
763
0
    return EVP_CIPHER_get_flags(cipher) & EVP_CIPH_MODE;
764
0
}
765
766
int EVP_MD_is_a(const EVP_MD *md, const char *name)
767
0
{
768
0
    if (md == NULL)
769
0
        return 0;
770
0
    if (md->prov != NULL)
771
0
        return evp_is_a(md->prov, md->name_id, NULL, name);
772
0
    return evp_is_a(NULL, 0, EVP_MD_get0_name(md), name);
773
0
}
774
775
int evp_md_get_number(const EVP_MD *md)
776
0
{
777
0
    return md->name_id;
778
0
}
779
780
const char *EVP_MD_get0_description(const EVP_MD *md)
781
0
{
782
0
    if (md->description != NULL)
783
0
        return md->description;
784
0
#ifndef FIPS_MODULE
785
0
    return OBJ_nid2ln(EVP_MD_nid(md));
786
#else
787
    return NULL;
788
#endif
789
0
}
790
791
const char *EVP_MD_get0_name(const EVP_MD *md)
792
0
{
793
0
    if (md == NULL)
794
0
        return NULL;
795
0
    if (md->type_name != NULL)
796
0
        return md->type_name;
797
0
#ifndef FIPS_MODULE
798
0
    return OBJ_nid2sn(EVP_MD_nid(md));
799
#else
800
    return NULL;
801
#endif
802
0
}
803
804
int EVP_MD_names_do_all(const EVP_MD *md,
805
                        void (*fn)(const char *name, void *data),
806
                        void *data)
807
0
{
808
0
    if (md->prov != NULL)
809
0
        return evp_names_do_all(md->prov, md->name_id, fn, data);
810
811
0
    return 1;
812
0
}
813
814
const OSSL_PROVIDER *EVP_MD_get0_provider(const EVP_MD *md)
815
0
{
816
0
    return md->prov;
817
0
}
818
819
int EVP_MD_get_type(const EVP_MD *md)
820
59
{
821
59
    return md->type;
822
59
}
823
824
int EVP_MD_get_pkey_type(const EVP_MD *md)
825
0
{
826
0
    return md->pkey_type;
827
0
}
828
829
int EVP_MD_get_block_size(const EVP_MD *md)
830
0
{
831
0
    if (md == NULL) {
832
0
        ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
833
0
        return -1;
834
0
    }
835
0
    return md->block_size;
836
0
}
837
838
int EVP_MD_get_size(const EVP_MD *md)
839
0
{
840
0
    if (md == NULL) {
841
0
        ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
842
0
        return -1;
843
0
    }
844
0
    return md->md_size;
845
0
}
846
847
unsigned long EVP_MD_get_flags(const EVP_MD *md)
848
0
{
849
0
    return md->flags;
850
0
}
851
852
EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type)
853
0
{
854
0
    EVP_MD *md = evp_md_new();
855
856
0
    if (md != NULL) {
857
0
        md->type = md_type;
858
0
        md->pkey_type = pkey_type;
859
0
        md->origin = EVP_ORIG_METH;
860
0
    }
861
0
    return md;
862
0
}
863
864
EVP_MD *EVP_MD_meth_dup(const EVP_MD *md)
865
0
{
866
0
    EVP_MD *to = NULL;
867
868
    /*
869
     * Non-legacy EVP_MDs can't be duplicated like this.
870
     * Use EVP_MD_up_ref() instead.
871
     */
872
0
    if (md->prov != NULL)
873
0
        return NULL;
874
875
0
    if ((to = EVP_MD_meth_new(md->type, md->pkey_type)) != NULL) {
876
0
        CRYPTO_REF_COUNT refcnt = to->refcnt;
877
878
0
        memcpy(to, md, sizeof(*to));
879
0
        to->refcnt = refcnt;
880
0
        to->origin = EVP_ORIG_METH;
881
0
    }
882
0
    return to;
883
0
}
884
885
void evp_md_free_int(EVP_MD *md)
886
0
{
887
0
    OPENSSL_free(md->type_name);
888
0
    ossl_provider_free(md->prov);
889
0
    CRYPTO_FREE_REF(&md->refcnt);
890
0
    OPENSSL_free(md);
891
0
}
892
893
void EVP_MD_meth_free(EVP_MD *md)
894
0
{
895
0
    if (md == NULL || md->origin != EVP_ORIG_METH)
896
0
       return;
897
898
0
    evp_md_free_int(md);
899
0
}
900
901
int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize)
902
0
{
903
0
    if (md->block_size != 0)
904
0
        return 0;
905
906
0
    md->block_size = blocksize;
907
0
    return 1;
908
0
}
909
int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize)
910
0
{
911
0
    if (md->md_size != 0)
912
0
        return 0;
913
914
0
    md->md_size = resultsize;
915
0
    return 1;
916
0
}
917
int EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize)
918
0
{
919
0
    if (md->ctx_size != 0)
920
0
        return 0;
921
922
0
    md->ctx_size = datasize;
923
0
    return 1;
924
0
}
925
int EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags)
926
0
{
927
0
    if (md->flags != 0)
928
0
        return 0;
929
930
0
    md->flags = flags;
931
0
    return 1;
932
0
}
933
int EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx))
934
0
{
935
0
    if (md->init != NULL)
936
0
        return 0;
937
938
0
    md->init = init;
939
0
    return 1;
940
0
}
941
int EVP_MD_meth_set_update(EVP_MD *md, int (*update)(EVP_MD_CTX *ctx,
942
                                                     const void *data,
943
                                                     size_t count))
944
0
{
945
0
    if (md->update != NULL)
946
0
        return 0;
947
948
0
    md->update = update;
949
0
    return 1;
950
0
}
951
int EVP_MD_meth_set_final(EVP_MD *md, int (*final)(EVP_MD_CTX *ctx,
952
                                                   unsigned char *md))
953
0
{
954
0
    if (md->final != NULL)
955
0
        return 0;
956
957
0
    md->final = final;
958
0
    return 1;
959
0
}
960
int EVP_MD_meth_set_copy(EVP_MD *md, int (*copy)(EVP_MD_CTX *to,
961
                                                 const EVP_MD_CTX *from))
962
0
{
963
0
    if (md->copy != NULL)
964
0
        return 0;
965
966
0
    md->copy = copy;
967
0
    return 1;
968
0
}
969
int EVP_MD_meth_set_cleanup(EVP_MD *md, int (*cleanup)(EVP_MD_CTX *ctx))
970
0
{
971
0
    if (md->cleanup != NULL)
972
0
        return 0;
973
974
0
    md->cleanup = cleanup;
975
0
    return 1;
976
0
}
977
int EVP_MD_meth_set_ctrl(EVP_MD *md, int (*ctrl)(EVP_MD_CTX *ctx, int cmd,
978
                                                 int p1, void *p2))
979
0
{
980
0
    if (md->md_ctrl != NULL)
981
0
        return 0;
982
983
0
    md->md_ctrl = ctrl;
984
0
    return 1;
985
0
}
986
987
int EVP_MD_meth_get_input_blocksize(const EVP_MD *md)
988
0
{
989
0
    return md->block_size;
990
0
}
991
int EVP_MD_meth_get_result_size(const EVP_MD *md)
992
0
{
993
0
    return md->md_size;
994
0
}
995
int EVP_MD_meth_get_app_datasize(const EVP_MD *md)
996
0
{
997
0
    return md->ctx_size;
998
0
}
999
unsigned long EVP_MD_meth_get_flags(const EVP_MD *md)
1000
0
{
1001
0
    return md->flags;
1002
0
}
1003
int (*EVP_MD_meth_get_init(const EVP_MD *md))(EVP_MD_CTX *ctx)
1004
0
{
1005
0
    return md->init;
1006
0
}
1007
int (*EVP_MD_meth_get_update(const EVP_MD *md))(EVP_MD_CTX *ctx,
1008
                                                const void *data,
1009
                                                size_t count)
1010
0
{
1011
0
    return md->update;
1012
0
}
1013
int (*EVP_MD_meth_get_final(const EVP_MD *md))(EVP_MD_CTX *ctx,
1014
                                               unsigned char *md)
1015
0
{
1016
0
    return md->final;
1017
0
}
1018
int (*EVP_MD_meth_get_copy(const EVP_MD *md))(EVP_MD_CTX *to,
1019
                                              const EVP_MD_CTX *from)
1020
0
{
1021
0
    return md->copy;
1022
0
}
1023
int (*EVP_MD_meth_get_cleanup(const EVP_MD *md))(EVP_MD_CTX *ctx)
1024
0
{
1025
0
    return md->cleanup;
1026
0
}
1027
int (*EVP_MD_meth_get_ctrl(const EVP_MD *md))(EVP_MD_CTX *ctx, int cmd,
1028
                                              int p1, void *p2)
1029
0
{
1030
0
    return md->md_ctrl;
1031
0
}
1032
1033
#ifndef OPENSSL_NO_DEPRECATED_3_0
1034
const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx)
1035
0
{
1036
0
    if (ctx == NULL)
1037
0
        return NULL;
1038
0
    return ctx->reqdigest;
1039
0
}
1040
#endif
1041
1042
const EVP_MD *EVP_MD_CTX_get0_md(const EVP_MD_CTX *ctx)
1043
0
{
1044
0
    if (ctx == NULL)
1045
0
        return NULL;
1046
0
    return ctx->reqdigest;
1047
0
}
1048
1049
EVP_MD *EVP_MD_CTX_get1_md(EVP_MD_CTX *ctx)
1050
0
{
1051
0
    EVP_MD *md;
1052
1053
0
    if (ctx == NULL)
1054
0
        return NULL;
1055
0
    md = (EVP_MD *)ctx->reqdigest;
1056
0
    if (md == NULL || !EVP_MD_up_ref(md))
1057
0
        return NULL;
1058
0
    return md;
1059
0
}
1060
1061
EVP_PKEY_CTX *EVP_MD_CTX_get_pkey_ctx(const EVP_MD_CTX *ctx)
1062
0
{
1063
0
    return ctx->pctx;
1064
0
}
1065
1066
#if !defined(FIPS_MODULE)
1067
void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx)
1068
0
{
1069
    /*
1070
     * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so
1071
     * we have to deal with the cleanup job here.
1072
     */
1073
0
    if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
1074
0
        EVP_PKEY_CTX_free(ctx->pctx);
1075
1076
0
    ctx->pctx = pctx;
1077
1078
0
    if (pctx != NULL) {
1079
        /* make sure pctx is not freed when destroying EVP_MD_CTX */
1080
0
        EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
1081
0
    } else {
1082
0
        EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
1083
0
    }
1084
0
}
1085
#endif /* !defined(FIPS_MODULE) */
1086
1087
void *EVP_MD_CTX_get0_md_data(const EVP_MD_CTX *ctx)
1088
0
{
1089
0
    return ctx->md_data;
1090
0
}
1091
1092
int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
1093
                                             const void *data, size_t count)
1094
0
{
1095
0
    return ctx->update;
1096
0
}
1097
1098
void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
1099
                              int (*update) (EVP_MD_CTX *ctx,
1100
                                             const void *data, size_t count))
1101
0
{
1102
0
    ctx->update = update;
1103
0
}
1104
1105
void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags)
1106
0
{
1107
0
    ctx->flags |= flags;
1108
0
}
1109
1110
void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags)
1111
0
{
1112
0
    ctx->flags &= ~flags;
1113
0
}
1114
1115
int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags)
1116
0
{
1117
0
    return (ctx->flags & flags);
1118
0
}
1119
1120
static int evp_cipher_ctx_enable_use_bits(EVP_CIPHER_CTX *ctx,
1121
                                          unsigned int enable)
1122
0
{
1123
0
    OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
1124
1125
0
    params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_USE_BITS, &enable);
1126
0
    return EVP_CIPHER_CTX_set_params(ctx, params);
1127
0
}
1128
1129
void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags)
1130
0
{
1131
0
    int oldflags = ctx->flags;
1132
1133
0
    ctx->flags |= flags;
1134
0
    if (((oldflags ^ ctx->flags) & EVP_CIPH_FLAG_LENGTH_BITS) != 0)
1135
0
        evp_cipher_ctx_enable_use_bits(ctx, 1);
1136
0
}
1137
1138
void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags)
1139
0
{
1140
0
    int oldflags = ctx->flags;
1141
1142
0
    ctx->flags &= ~flags;
1143
0
    if (((oldflags ^ ctx->flags) & EVP_CIPH_FLAG_LENGTH_BITS) != 0)
1144
0
        evp_cipher_ctx_enable_use_bits(ctx, 0);
1145
0
}
1146
1147
int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags)
1148
0
{
1149
0
    return (ctx->flags & flags);
1150
0
}
1151
1152
#if !defined(FIPS_MODULE)
1153
1154
int EVP_PKEY_CTX_set_group_name(EVP_PKEY_CTX *ctx, const char *name)
1155
0
{
1156
0
    OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
1157
1158
0
    if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1159
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1160
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1161
0
        return -2;
1162
0
    }
1163
1164
0
    if (name == NULL)
1165
0
        return -1;
1166
1167
0
    params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
1168
0
                                                 (char *)name, 0);
1169
0
    return EVP_PKEY_CTX_set_params(ctx, params);
1170
0
}
1171
1172
int EVP_PKEY_CTX_get_group_name(EVP_PKEY_CTX *ctx, char *name, size_t namelen)
1173
0
{
1174
0
    OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
1175
0
    OSSL_PARAM *p = params;
1176
1177
0
    if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1178
        /* There is no legacy support for this */
1179
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1180
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1181
0
        return -2;
1182
0
    }
1183
1184
0
    if (name == NULL)
1185
0
        return -1;
1186
1187
0
    *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
1188
0
                                            name, namelen);
1189
0
    if (!EVP_PKEY_CTX_get_params(ctx, params))
1190
0
        return -1;
1191
0
    return 1;
1192
0
}
1193
1194
/*
1195
 * evp_pkey_keygen() abstracts from the explicit use of B<EVP_PKEY_CTX>
1196
 * while providing a generic way of generating a new asymmetric key pair
1197
 * of algorithm type I<name> (e.g., C<RSA> or C<EC>).
1198
 * The library context I<libctx> and property query I<propq>
1199
 * are used when fetching algorithms from providers.
1200
 * The I<params> specify algorithm-specific parameters
1201
 * such as the RSA modulus size or the name of an EC curve.
1202
 */
1203
static EVP_PKEY *evp_pkey_keygen(OSSL_LIB_CTX *libctx, const char *name,
1204
                                 const char *propq, const OSSL_PARAM *params)
1205
0
{
1206
0
    EVP_PKEY *pkey = NULL;
1207
0
    EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name(libctx, name, propq);
1208
1209
0
    if (ctx != NULL
1210
0
            && EVP_PKEY_keygen_init(ctx) > 0
1211
0
            && EVP_PKEY_CTX_set_params(ctx, params))
1212
0
        (void)EVP_PKEY_generate(ctx, &pkey);
1213
1214
0
    EVP_PKEY_CTX_free(ctx);
1215
0
    return pkey;
1216
0
}
1217
1218
EVP_PKEY *EVP_PKEY_Q_keygen(OSSL_LIB_CTX *libctx, const char *propq,
1219
                            const char *type, ...)
1220
0
{
1221
0
    va_list args;
1222
0
    size_t bits;
1223
0
    char *name;
1224
0
    OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
1225
0
    EVP_PKEY *ret = NULL;
1226
1227
0
    va_start(args, type);
1228
1229
0
    if (OPENSSL_strcasecmp(type, "RSA") == 0) {
1230
0
        bits = va_arg(args, size_t);
1231
0
        params[0] = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_BITS, &bits);
1232
0
    } else if (OPENSSL_strcasecmp(type, "EC") == 0) {
1233
0
        name = va_arg(args, char *);
1234
0
        params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
1235
0
                                                     name, 0);
1236
0
    } else if (OPENSSL_strcasecmp(type, "ED25519") != 0
1237
0
               && OPENSSL_strcasecmp(type, "X25519") != 0
1238
0
               && OPENSSL_strcasecmp(type, "ED448") != 0
1239
0
               && OPENSSL_strcasecmp(type, "X448") != 0
1240
0
               && OPENSSL_strcasecmp(type, "SM2") != 0) {
1241
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_INVALID_ARGUMENT);
1242
0
        goto end;
1243
0
    }
1244
0
    ret = evp_pkey_keygen(libctx, type, propq, params);
1245
1246
0
 end:
1247
0
    va_end(args);
1248
0
    return ret;
1249
0
}
1250
1251
#endif /* !defined(FIPS_MODULE) */