Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/evp/evp_lib.c

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