Coverage Report

Created: 2025-12-10 06:24

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