Coverage Report

Created: 2026-07-16 06:59

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