Coverage Report

Created: 2026-08-31 06:56

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