VirtualBox

source: vbox/trunk/src/libs/openssl-3.0.2/crypto/evp/evp_lib.c@ 95165

Last change on this file since 95165 was 94082, checked in by vboxsync, 3 years ago

libs/openssl-3.0.1: started applying and adjusting our OpenSSL changes to 3.0.1. bugref:10128

File size: 32.0 KB
Line 
1/*
2 * Copyright 1995-2021 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 "e_os.h" /* strcasecmp */
19#include "internal/cryptlib.h"
20#include <openssl/evp.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 "internal/provider.h"
29#include "evp_local.h"
30
31#if !defined(FIPS_MODULE)
32# include "crypto/asn1.h"
33
34int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
35{
36 return evp_cipher_param_to_asn1_ex(c, type, NULL);
37}
38
39int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
40{
41 return evp_cipher_asn1_to_param_ex(c, type, NULL);
42}
43
44int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX *ctx, ASN1_TYPE *type)
45{
46 int i = 0;
47 unsigned int l;
48
49 if (type != NULL) {
50 unsigned char iv[EVP_MAX_IV_LENGTH];
51
52 l = EVP_CIPHER_CTX_get_iv_length(ctx);
53 if (!ossl_assert(l <= sizeof(iv)))
54 return -1;
55 i = ASN1_TYPE_get_octetstring(type, iv, l);
56 if (i != (int)l)
57 return -1;
58
59 if (!EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1))
60 return -1;
61 }
62 return i;
63}
64
65int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
66{
67 int i = 0;
68 unsigned int j;
69 unsigned char *oiv = NULL;
70
71 if (type != NULL) {
72 oiv = (unsigned char *)EVP_CIPHER_CTX_original_iv(c);
73 j = EVP_CIPHER_CTX_get_iv_length(c);
74 OPENSSL_assert(j <= sizeof(c->iv));
75 i = ASN1_TYPE_set_octetstring(type, oiv, j);
76 }
77 return i;
78}
79
80int evp_cipher_param_to_asn1_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
81 evp_cipher_aead_asn1_params *asn1_params)
82{
83 int ret = -1; /* Assume the worst */
84 const EVP_CIPHER *cipher = c->cipher;
85
86 /*
87 * For legacy implementations, we detect custom AlgorithmIdentifier
88 * parameter handling by checking if the function pointer
89 * cipher->set_asn1_parameters is set. We know that this pointer
90 * is NULL for provided implementations.
91 *
92 * Otherwise, 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 if (cipher->set_asn1_parameters != NULL) {
103 ret = cipher->set_asn1_parameters(c, type);
104 } else if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_CUSTOM_ASN1) == 0) {
105 switch (EVP_CIPHER_get_mode(cipher)) {
106 case EVP_CIPH_WRAP_MODE:
107 if (EVP_CIPHER_is_a(cipher, SN_id_smime_alg_CMS3DESwrap))
108 ASN1_TYPE_set(type, V_ASN1_NULL, NULL);
109 ret = 1;
110 break;
111
112 case EVP_CIPH_GCM_MODE:
113 ret = evp_cipher_set_asn1_aead_params(c, type, asn1_params);
114 break;
115
116 case EVP_CIPH_CCM_MODE:
117 case EVP_CIPH_XTS_MODE:
118 case EVP_CIPH_OCB_MODE:
119 ret = -2;
120 break;
121
122 default:
123 ret = EVP_CIPHER_set_asn1_iv(c, type);
124 }
125 } else if (cipher->prov != NULL) {
126 OSSL_PARAM params[3], *p = params;
127 unsigned char *der = NULL, *derp;
128
129 /*
130 * We make two passes, the first to get the appropriate buffer size,
131 * and the second to get the actual value.
132 */
133 *p++ = OSSL_PARAM_construct_octet_string(
134 OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS,
135 NULL, 0);
136 *p = OSSL_PARAM_construct_end();
137
138 if (!EVP_CIPHER_CTX_get_params(c, params))
139 goto err;
140
141 /* ... but, we should get a return size too! */
142 if (OSSL_PARAM_modified(params)
143 && params[0].return_size != 0
144 && (der = OPENSSL_malloc(params[0].return_size)) != NULL) {
145 params[0].data = der;
146 params[0].data_size = params[0].return_size;
147 OSSL_PARAM_set_all_unmodified(params);
148 derp = der;
149 if (EVP_CIPHER_CTX_get_params(c, params)
150 && OSSL_PARAM_modified(params)
151 && d2i_ASN1_TYPE(&type, (const unsigned char **)&derp,
152 params[0].return_size) != NULL) {
153 ret = 1;
154 }
155 OPENSSL_free(der);
156 }
157 } else {
158 ret = -2;
159 }
160
161 err:
162 if (ret == -2)
163 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER);
164 else if (ret <= 0)
165 ERR_raise(ERR_LIB_EVP, EVP_R_CIPHER_PARAMETER_ERROR);
166 if (ret < -1)
167 ret = -1;
168 return ret;
169}
170
171int evp_cipher_asn1_to_param_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
172 evp_cipher_aead_asn1_params *asn1_params)
173{
174 int ret = -1; /* Assume the worst */
175 const EVP_CIPHER *cipher = c->cipher;
176
177 /*
178 * For legacy implementations, we detect custom AlgorithmIdentifier
179 * parameter handling by checking if there the function pointer
180 * cipher->get_asn1_parameters is set. We know that this pointer
181 * is NULL for provided implementations.
182 *
183 * Otherwise, for any implementation, we check the flag
184 * EVP_CIPH_FLAG_CUSTOM_ASN1. If it isn't set, we apply
185 * default AI parameter creation.
186 *
187 * Otherwise, for provided implementations, we get the AI parameter
188 * in DER encoded form from the implementation by requesting the
189 * appropriate OSSL_PARAM and converting the result to a ASN1_TYPE.
190 *
191 * If none of the above applies, this operation is unsupported.
192 */
193 if (cipher->get_asn1_parameters != NULL) {
194 ret = cipher->get_asn1_parameters(c, type);
195 } else if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_CUSTOM_ASN1) == 0) {
196 switch (EVP_CIPHER_get_mode(cipher)) {
197 case EVP_CIPH_WRAP_MODE:
198 ret = 1;
199 break;
200
201 case EVP_CIPH_GCM_MODE:
202 ret = evp_cipher_get_asn1_aead_params(c, type, asn1_params);
203 break;
204
205 case EVP_CIPH_CCM_MODE:
206 case EVP_CIPH_XTS_MODE:
207 case EVP_CIPH_OCB_MODE:
208 ret = -2;
209 break;
210
211 default:
212 ret = EVP_CIPHER_get_asn1_iv(c, type);
213 }
214 } else if (cipher->prov != NULL) {
215 OSSL_PARAM params[3], *p = params;
216 unsigned char *der = NULL;
217 int derl = -1;
218
219 if ((derl = i2d_ASN1_TYPE(type, &der)) >= 0) {
220 *p++ =
221 OSSL_PARAM_construct_octet_string(
222 OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS,
223 der, (size_t)derl);
224 *p = OSSL_PARAM_construct_end();
225 if (EVP_CIPHER_CTX_set_params(c, params))
226 ret = 1;
227 OPENSSL_free(der);
228 }
229 } else {
230 ret = -2;
231 }
232
233 if (ret == -2)
234 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER);
235 else if (ret <= 0)
236 ERR_raise(ERR_LIB_EVP, EVP_R_CIPHER_PARAMETER_ERROR);
237 if (ret < -1)
238 ret = -1;
239 return ret;
240}
241
242int evp_cipher_get_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
243 evp_cipher_aead_asn1_params *asn1_params)
244{
245 int i = 0;
246 long tl;
247 unsigned char iv[EVP_MAX_IV_LENGTH];
248
249 if (type == NULL || asn1_params == NULL)
250 return 0;
251
252 i = ossl_asn1_type_get_octetstring_int(type, &tl, NULL, EVP_MAX_IV_LENGTH);
253 if (i <= 0)
254 return -1;
255 ossl_asn1_type_get_octetstring_int(type, &tl, iv, i);
256
257 memcpy(asn1_params->iv, iv, i);
258 asn1_params->iv_len = i;
259
260 return i;
261}
262
263int evp_cipher_set_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
264 evp_cipher_aead_asn1_params *asn1_params)
265{
266 if (type == NULL || asn1_params == NULL)
267 return 0;
268
269 return ossl_asn1_type_set_octetstring_int(type, asn1_params->tag_len,
270 asn1_params->iv,
271 asn1_params->iv_len);
272}
273#endif /* !defined(FIPS_MODULE) */
274
275/* Convert the various cipher NIDs and dummies to a proper OID NID */
276int EVP_CIPHER_get_type(const EVP_CIPHER *cipher)
277{
278 int nid;
279 nid = EVP_CIPHER_get_nid(cipher);
280
281 switch (nid) {
282
283 case NID_rc2_cbc:
284 case NID_rc2_64_cbc:
285 case NID_rc2_40_cbc:
286
287 return NID_rc2_cbc;
288
289 case NID_rc4:
290 case NID_rc4_40:
291
292 return NID_rc4;
293
294 case NID_aes_128_cfb128:
295 case NID_aes_128_cfb8:
296 case NID_aes_128_cfb1:
297
298 return NID_aes_128_cfb128;
299
300 case NID_aes_192_cfb128:
301 case NID_aes_192_cfb8:
302 case NID_aes_192_cfb1:
303
304 return NID_aes_192_cfb128;
305
306 case NID_aes_256_cfb128:
307 case NID_aes_256_cfb8:
308 case NID_aes_256_cfb1:
309
310 return NID_aes_256_cfb128;
311
312 case NID_des_cfb64:
313 case NID_des_cfb8:
314 case NID_des_cfb1:
315
316 return NID_des_cfb64;
317
318 case NID_des_ede3_cfb64:
319 case NID_des_ede3_cfb8:
320 case NID_des_ede3_cfb1:
321
322 return NID_des_cfb64;
323
324 default:
325#ifdef FIPS_MODULE
326 return NID_undef;
327#else
328 {
329 /* Check it has an OID and it is valid */
330 ASN1_OBJECT *otmp = OBJ_nid2obj(nid);
331
332 if (OBJ_get0_data(otmp) == NULL)
333 nid = NID_undef;
334 ASN1_OBJECT_free(otmp);
335 return nid;
336 }
337#endif
338 }
339}
340
341int evp_cipher_cache_constants(EVP_CIPHER *cipher)
342{
343 int ok, aead = 0, custom_iv = 0, cts = 0, multiblock = 0, randkey = 0;
344 size_t ivlen = 0;
345 size_t blksz = 0;
346 size_t keylen = 0;
347 unsigned int mode = 0;
348 OSSL_PARAM params[10];
349
350 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_BLOCK_SIZE, &blksz);
351 params[1] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &ivlen);
352 params[2] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &keylen);
353 params[3] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_MODE, &mode);
354 params[4] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_AEAD, &aead);
355 params[5] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_CUSTOM_IV,
356 &custom_iv);
357 params[6] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_CTS, &cts);
358 params[7] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK,
359 &multiblock);
360 params[8] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_HAS_RAND_KEY,
361 &randkey);
362 params[9] = OSSL_PARAM_construct_end();
363 ok = evp_do_ciph_getparams(cipher, params) > 0;
364 if (ok) {
365 cipher->block_size = blksz;
366 cipher->iv_len = ivlen;
367 cipher->key_len = keylen;
368 cipher->flags = mode;
369 if (aead)
370 cipher->flags |= EVP_CIPH_FLAG_AEAD_CIPHER;
371 if (custom_iv)
372 cipher->flags |= EVP_CIPH_CUSTOM_IV;
373 if (cts)
374 cipher->flags |= EVP_CIPH_FLAG_CTS;
375 if (multiblock)
376 cipher->flags |= EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK;
377 if (cipher->ccipher != NULL)
378 cipher->flags |= EVP_CIPH_FLAG_CUSTOM_CIPHER;
379 if (randkey)
380 cipher->flags |= EVP_CIPH_RAND_KEY;
381 if (OSSL_PARAM_locate_const(EVP_CIPHER_gettable_ctx_params(cipher),
382 OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS))
383 cipher->flags |= EVP_CIPH_FLAG_CUSTOM_ASN1;
384 }
385 return ok;
386}
387
388int EVP_CIPHER_get_block_size(const EVP_CIPHER *cipher)
389{
390 return cipher->block_size;
391}
392
393int EVP_CIPHER_CTX_get_block_size(const EVP_CIPHER_CTX *ctx)
394{
395 return EVP_CIPHER_get_block_size(ctx->cipher);
396}
397
398int EVP_CIPHER_impl_ctx_size(const EVP_CIPHER *e)
399{
400 return e->ctx_size;
401}
402
403int EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
404 const unsigned char *in, unsigned int inl)
405{
406 if (ctx->cipher->prov != NULL) {
407 /*
408 * If the provided implementation has a ccipher function, we use it,
409 * and translate its return value like this: 0 => -1, 1 => outlen
410 *
411 * Otherwise, we call the cupdate function if in != NULL, or cfinal
412 * if in == NULL. Regardless of which, we return what we got.
413 */
414 int ret = -1;
415 size_t outl = 0;
416 size_t blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
417
418 if (ctx->cipher->ccipher != NULL)
419 ret = ctx->cipher->ccipher(ctx->algctx, out, &outl,
420 inl + (blocksize == 1 ? 0 : blocksize),
421 in, (size_t)inl)
422 ? (int)outl : -1;
423 else if (in != NULL)
424 ret = ctx->cipher->cupdate(ctx->algctx, out, &outl,
425 inl + (blocksize == 1 ? 0 : blocksize),
426 in, (size_t)inl);
427 else
428 ret = ctx->cipher->cfinal(ctx->algctx, out, &outl,
429 blocksize == 1 ? 0 : blocksize);
430
431 return ret;
432 }
433
434 return ctx->cipher->do_cipher(ctx, out, in, inl);
435}
436
437#ifndef OPENSSL_NO_DEPRECATED_3_0
438const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx)
439{
440 if (ctx == NULL)
441 return NULL;
442 return ctx->cipher;
443}
444#endif
445
446const EVP_CIPHER *EVP_CIPHER_CTX_get0_cipher(const EVP_CIPHER_CTX *ctx)
447{
448 if (ctx == NULL)
449 return NULL;
450 return ctx->cipher;
451}
452
453EVP_CIPHER *EVP_CIPHER_CTX_get1_cipher(EVP_CIPHER_CTX *ctx)
454{
455 EVP_CIPHER *cipher;
456
457 if (ctx == NULL)
458 return NULL;
459 cipher = (EVP_CIPHER *)ctx->cipher;
460 if (!EVP_CIPHER_up_ref(cipher))
461 return NULL;
462 return cipher;
463}
464
465int EVP_CIPHER_CTX_is_encrypting(const EVP_CIPHER_CTX *ctx)
466{
467 return ctx->encrypt;
468}
469
470unsigned long EVP_CIPHER_get_flags(const EVP_CIPHER *cipher)
471{
472 return cipher->flags;
473}
474
475void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx)
476{
477 return ctx->app_data;
478}
479
480void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
481{
482 ctx->app_data = data;
483}
484
485void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX *ctx)
486{
487 return ctx->cipher_data;
488}
489
490void *EVP_CIPHER_CTX_set_cipher_data(EVP_CIPHER_CTX *ctx, void *cipher_data)
491{
492 void *old_cipher_data;
493
494 old_cipher_data = ctx->cipher_data;
495 ctx->cipher_data = cipher_data;
496
497 return old_cipher_data;
498}
499
500int EVP_CIPHER_get_iv_length(const EVP_CIPHER *cipher)
501{
502 return cipher->iv_len;
503}
504
505int EVP_CIPHER_CTX_get_iv_length(const EVP_CIPHER_CTX *ctx)
506{
507 int rv, len = EVP_CIPHER_get_iv_length(ctx->cipher);
508 size_t v = len;
509 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
510
511 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &v);
512 rv = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
513 if (rv == EVP_CTRL_RET_UNSUPPORTED)
514 goto legacy;
515 return rv != 0 ? (int)v : -1;
516 /* Code below to be removed when legacy support is dropped. */
517legacy:
518 if ((EVP_CIPHER_get_flags(ctx->cipher) & EVP_CIPH_CUSTOM_IV_LENGTH) != 0) {
519 rv = EVP_CIPHER_CTX_ctrl((EVP_CIPHER_CTX *)ctx, EVP_CTRL_GET_IVLEN,
520 0, &len);
521 return (rv == 1) ? len : -1;
522 }
523 return len;
524}
525
526int EVP_CIPHER_CTX_get_tag_length(const EVP_CIPHER_CTX *ctx)
527{
528 int ret;
529 size_t v = 0;
530 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
531
532 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, &v);
533 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
534 return ret == 1 ? (int)v : 0;
535}
536
537#ifndef OPENSSL_NO_DEPRECATED_3_0
538const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX *ctx)
539{
540 int ok;
541 const unsigned char *v = ctx->oiv;
542 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
543
544 params[0] =
545 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV,
546 (void **)&v, sizeof(ctx->oiv));
547 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
548
549 return ok != 0 ? v : NULL;
550}
551
552/*
553 * OSSL_PARAM_OCTET_PTR gets us the pointer to the running IV in the provider
554 */
555const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx)
556{
557 int ok;
558 const unsigned char *v = ctx->iv;
559 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
560
561 params[0] =
562 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_UPDATED_IV,
563 (void **)&v, sizeof(ctx->iv));
564 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
565
566 return ok != 0 ? v : NULL;
567}
568
569unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
570{
571 int ok;
572 unsigned char *v = ctx->iv;
573 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
574
575 params[0] =
576 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_UPDATED_IV,
577 (void **)&v, sizeof(ctx->iv));
578 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
579
580 return ok != 0 ? v : NULL;
581}
582#endif /* OPENSSL_NO_DEPRECATED_3_0_0 */
583
584int EVP_CIPHER_CTX_get_updated_iv(EVP_CIPHER_CTX *ctx, void *buf, size_t len)
585{
586 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
587
588 params[0] =
589 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_UPDATED_IV, buf, len);
590 return evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
591}
592
593int EVP_CIPHER_CTX_get_original_iv(EVP_CIPHER_CTX *ctx, void *buf, size_t len)
594{
595 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
596
597 params[0] =
598 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_IV, buf, len);
599 return evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
600}
601
602unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX *ctx)
603{
604 return ctx->buf;
605}
606
607int EVP_CIPHER_CTX_get_num(const EVP_CIPHER_CTX *ctx)
608{
609 int ok;
610 unsigned int v = (unsigned int)ctx->num;
611 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
612
613 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &v);
614 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
615
616 return ok != 0 ? (int)v : EVP_CTRL_RET_UNSUPPORTED;
617}
618
619int EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num)
620{
621 int ok;
622 unsigned int n = (unsigned int)num;
623 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
624
625 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &n);
626 ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
627
628 if (ok != 0)
629 ctx->num = (int)n;
630 return ok != 0;
631}
632
633int EVP_CIPHER_get_key_length(const EVP_CIPHER *cipher)
634{
635 return cipher->key_len;
636}
637
638int EVP_CIPHER_CTX_get_key_length(const EVP_CIPHER_CTX *ctx)
639{
640 int ok;
641 size_t v = ctx->key_len;
642 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
643
644 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &v);
645 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
646
647 return ok != 0 ? (int)v : EVP_CTRL_RET_UNSUPPORTED;
648}
649
650int EVP_CIPHER_get_nid(const EVP_CIPHER *cipher)
651{
652 return cipher->nid;
653}
654
655int EVP_CIPHER_CTX_get_nid(const EVP_CIPHER_CTX *ctx)
656{
657 return ctx->cipher->nid;
658}
659
660int EVP_CIPHER_is_a(const EVP_CIPHER *cipher, const char *name)
661{
662 if (cipher->prov != NULL)
663 return evp_is_a(cipher->prov, cipher->name_id, NULL, name);
664 return evp_is_a(NULL, 0, EVP_CIPHER_get0_name(cipher), name);
665}
666
667int evp_cipher_get_number(const EVP_CIPHER *cipher)
668{
669 return cipher->name_id;
670}
671
672const char *EVP_CIPHER_get0_name(const EVP_CIPHER *cipher)
673{
674 if (cipher->type_name != NULL)
675 return cipher->type_name;
676#ifndef FIPS_MODULE
677 return OBJ_nid2sn(EVP_CIPHER_get_nid(cipher));
678#else
679 return NULL;
680#endif
681}
682
683const char *EVP_CIPHER_get0_description(const EVP_CIPHER *cipher)
684{
685 if (cipher->description != NULL)
686 return cipher->description;
687#ifndef FIPS_MODULE
688 return OBJ_nid2ln(EVP_CIPHER_get_nid(cipher));
689#else
690 return NULL;
691#endif
692}
693
694int EVP_CIPHER_names_do_all(const EVP_CIPHER *cipher,
695 void (*fn)(const char *name, void *data),
696 void *data)
697{
698 if (cipher->prov != NULL)
699 return evp_names_do_all(cipher->prov, cipher->name_id, fn, data);
700
701 return 1;
702}
703
704const OSSL_PROVIDER *EVP_CIPHER_get0_provider(const EVP_CIPHER *cipher)
705{
706 return cipher->prov;
707}
708
709int EVP_CIPHER_get_mode(const EVP_CIPHER *cipher)
710{
711 return EVP_CIPHER_get_flags(cipher) & EVP_CIPH_MODE;
712}
713
714int EVP_MD_is_a(const EVP_MD *md, const char *name)
715{
716 if (md->prov != NULL)
717 return evp_is_a(md->prov, md->name_id, NULL, name);
718 return evp_is_a(NULL, 0, EVP_MD_get0_name(md), name);
719}
720
721int evp_md_get_number(const EVP_MD *md)
722{
723 return md->name_id;
724}
725
726const char *EVP_MD_get0_description(const EVP_MD *md)
727{
728 if (md->description != NULL)
729 return md->description;
730#ifndef FIPS_MODULE
731 return OBJ_nid2ln(EVP_MD_nid(md));
732#else
733 return NULL;
734#endif
735}
736
737const char *EVP_MD_get0_name(const EVP_MD *md)
738{
739 if (md == NULL)
740 return NULL;
741 if (md->type_name != NULL)
742 return md->type_name;
743#ifndef FIPS_MODULE
744 return OBJ_nid2sn(EVP_MD_nid(md));
745#else
746 return NULL;
747#endif
748}
749
750int EVP_MD_names_do_all(const EVP_MD *md,
751 void (*fn)(const char *name, void *data),
752 void *data)
753{
754 if (md->prov != NULL)
755 return evp_names_do_all(md->prov, md->name_id, fn, data);
756
757 return 1;
758}
759
760const OSSL_PROVIDER *EVP_MD_get0_provider(const EVP_MD *md)
761{
762 return md->prov;
763}
764
765int EVP_MD_get_type(const EVP_MD *md)
766{
767 return md->type;
768}
769
770int EVP_MD_get_pkey_type(const EVP_MD *md)
771{
772 return md->pkey_type;
773}
774
775int EVP_MD_get_block_size(const EVP_MD *md)
776{
777 if (md == NULL) {
778 ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
779 return -1;
780 }
781 return md->block_size;
782}
783
784int EVP_MD_get_size(const EVP_MD *md)
785{
786 if (md == NULL) {
787 ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
788 return -1;
789 }
790 return md->md_size;
791}
792
793unsigned long EVP_MD_get_flags(const EVP_MD *md)
794{
795 return md->flags;
796}
797
798EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type)
799{
800 EVP_MD *md = evp_md_new();
801
802 if (md != NULL) {
803 md->type = md_type;
804 md->pkey_type = pkey_type;
805 md->origin = EVP_ORIG_METH;
806 }
807 return md;
808}
809
810EVP_MD *EVP_MD_meth_dup(const EVP_MD *md)
811{
812 EVP_MD *to = NULL;
813
814 /*
815 * Non-legacy EVP_MDs can't be duplicated like this.
816 * Use EVP_MD_up_ref() instead.
817 */
818 if (md->prov != NULL)
819 return NULL;
820
821 if ((to = EVP_MD_meth_new(md->type, md->pkey_type)) != NULL) {
822 CRYPTO_RWLOCK *lock = to->lock;
823
824 memcpy(to, md, sizeof(*to));
825 to->lock = lock;
826 to->origin = EVP_ORIG_METH;
827 }
828 return to;
829}
830
831void evp_md_free_int(EVP_MD *md)
832{
833 OPENSSL_free(md->type_name);
834 ossl_provider_free(md->prov);
835 CRYPTO_THREAD_lock_free(md->lock);
836 OPENSSL_free(md);
837}
838
839void EVP_MD_meth_free(EVP_MD *md)
840{
841 if (md == NULL || md->origin != EVP_ORIG_METH)
842 return;
843
844 evp_md_free_int(md);
845}
846
847int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize)
848{
849 if (md->block_size != 0)
850 return 0;
851
852 md->block_size = blocksize;
853 return 1;
854}
855int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize)
856{
857 if (md->md_size != 0)
858 return 0;
859
860 md->md_size = resultsize;
861 return 1;
862}
863int EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize)
864{
865 if (md->ctx_size != 0)
866 return 0;
867
868 md->ctx_size = datasize;
869 return 1;
870}
871int EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags)
872{
873 if (md->flags != 0)
874 return 0;
875
876 md->flags = flags;
877 return 1;
878}
879int EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx))
880{
881 if (md->init != NULL)
882 return 0;
883
884 md->init = init;
885 return 1;
886}
887int EVP_MD_meth_set_update(EVP_MD *md, int (*update)(EVP_MD_CTX *ctx,
888 const void *data,
889 size_t count))
890{
891 if (md->update != NULL)
892 return 0;
893
894 md->update = update;
895 return 1;
896}
897int EVP_MD_meth_set_final(EVP_MD *md, int (*final)(EVP_MD_CTX *ctx,
898 unsigned char *md))
899{
900 if (md->final != NULL)
901 return 0;
902
903 md->final = final;
904 return 1;
905}
906int EVP_MD_meth_set_copy(EVP_MD *md, int (*copy)(EVP_MD_CTX *to,
907 const EVP_MD_CTX *from))
908{
909 if (md->copy != NULL)
910 return 0;
911
912 md->copy = copy;
913 return 1;
914}
915int EVP_MD_meth_set_cleanup(EVP_MD *md, int (*cleanup)(EVP_MD_CTX *ctx))
916{
917 if (md->cleanup != NULL)
918 return 0;
919
920 md->cleanup = cleanup;
921 return 1;
922}
923int EVP_MD_meth_set_ctrl(EVP_MD *md, int (*ctrl)(EVP_MD_CTX *ctx, int cmd,
924 int p1, void *p2))
925{
926 if (md->md_ctrl != NULL)
927 return 0;
928
929 md->md_ctrl = ctrl;
930 return 1;
931}
932
933int EVP_MD_meth_get_input_blocksize(const EVP_MD *md)
934{
935 return md->block_size;
936}
937int EVP_MD_meth_get_result_size(const EVP_MD *md)
938{
939 return md->md_size;
940}
941int EVP_MD_meth_get_app_datasize(const EVP_MD *md)
942{
943 return md->ctx_size;
944}
945unsigned long EVP_MD_meth_get_flags(const EVP_MD *md)
946{
947 return md->flags;
948}
949int (*EVP_MD_meth_get_init(const EVP_MD *md))(EVP_MD_CTX *ctx)
950{
951 return md->init;
952}
953int (*EVP_MD_meth_get_update(const EVP_MD *md))(EVP_MD_CTX *ctx,
954 const void *data,
955 size_t count)
956{
957 return md->update;
958}
959int (*EVP_MD_meth_get_final(const EVP_MD *md))(EVP_MD_CTX *ctx,
960 unsigned char *md)
961{
962 return md->final;
963}
964int (*EVP_MD_meth_get_copy(const EVP_MD *md))(EVP_MD_CTX *to,
965 const EVP_MD_CTX *from)
966{
967 return md->copy;
968}
969int (*EVP_MD_meth_get_cleanup(const EVP_MD *md))(EVP_MD_CTX *ctx)
970{
971 return md->cleanup;
972}
973int (*EVP_MD_meth_get_ctrl(const EVP_MD *md))(EVP_MD_CTX *ctx, int cmd,
974 int p1, void *p2)
975{
976 return md->md_ctrl;
977}
978
979#ifndef OPENSSL_NO_DEPRECATED_3_0
980const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx)
981{
982 if (ctx == NULL)
983 return NULL;
984 return ctx->reqdigest;
985}
986#endif
987
988const EVP_MD *EVP_MD_CTX_get0_md(const EVP_MD_CTX *ctx)
989{
990 if (ctx == NULL)
991 return NULL;
992 return ctx->reqdigest;
993}
994
995EVP_MD *EVP_MD_CTX_get1_md(EVP_MD_CTX *ctx)
996{
997 EVP_MD *md;
998
999 if (ctx == NULL)
1000 return NULL;
1001 md = (EVP_MD *)ctx->reqdigest;
1002 if (md == NULL || !EVP_MD_up_ref(md))
1003 return NULL;
1004 return md;
1005}
1006
1007EVP_PKEY_CTX *EVP_MD_CTX_get_pkey_ctx(const EVP_MD_CTX *ctx)
1008{
1009 return ctx->pctx;
1010}
1011
1012#if !defined(FIPS_MODULE)
1013void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx)
1014{
1015 /*
1016 * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so
1017 * we have to deal with the cleanup job here.
1018 */
1019 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
1020 EVP_PKEY_CTX_free(ctx->pctx);
1021
1022 ctx->pctx = pctx;
1023
1024 if (pctx != NULL) {
1025 /* make sure pctx is not freed when destroying EVP_MD_CTX */
1026 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
1027 } else {
1028 EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
1029 }
1030}
1031#endif /* !defined(FIPS_MODULE) */
1032
1033void *EVP_MD_CTX_get0_md_data(const EVP_MD_CTX *ctx)
1034{
1035 return ctx->md_data;
1036}
1037
1038int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
1039 const void *data, size_t count)
1040{
1041 return ctx->update;
1042}
1043
1044void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
1045 int (*update) (EVP_MD_CTX *ctx,
1046 const void *data, size_t count))
1047{
1048 ctx->update = update;
1049}
1050
1051void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags)
1052{
1053 ctx->flags |= flags;
1054}
1055
1056void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags)
1057{
1058 ctx->flags &= ~flags;
1059}
1060
1061int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags)
1062{
1063 return (ctx->flags & flags);
1064}
1065
1066static int evp_cipher_ctx_enable_use_bits(EVP_CIPHER_CTX *ctx,
1067 unsigned int enable)
1068{
1069 OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
1070
1071 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_USE_BITS, &enable);
1072 return EVP_CIPHER_CTX_set_params(ctx, params);
1073}
1074
1075void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags)
1076{
1077 int oldflags = ctx->flags;
1078
1079 ctx->flags |= flags;
1080 if (((oldflags ^ ctx->flags) & EVP_CIPH_FLAG_LENGTH_BITS) != 0)
1081 evp_cipher_ctx_enable_use_bits(ctx, 1);
1082}
1083
1084void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags)
1085{
1086 int oldflags = ctx->flags;
1087
1088 ctx->flags &= ~flags;
1089 if (((oldflags ^ ctx->flags) & EVP_CIPH_FLAG_LENGTH_BITS) != 0)
1090 evp_cipher_ctx_enable_use_bits(ctx, 0);
1091}
1092
1093int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags)
1094{
1095 return (ctx->flags & flags);
1096}
1097
1098int EVP_PKEY_CTX_set_group_name(EVP_PKEY_CTX *ctx, const char *name)
1099{
1100 OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
1101
1102 if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1103 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1104 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1105 return -2;
1106 }
1107
1108 if (name == NULL)
1109 return -1;
1110
1111 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
1112 (char *)name, 0);
1113 return EVP_PKEY_CTX_set_params(ctx, params);
1114}
1115
1116int EVP_PKEY_CTX_get_group_name(EVP_PKEY_CTX *ctx, char *name, size_t namelen)
1117{
1118 OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
1119 OSSL_PARAM *p = params;
1120
1121 if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1122 /* There is no legacy support for this */
1123 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1124 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1125 return -2;
1126 }
1127
1128 if (name == NULL)
1129 return -1;
1130
1131 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
1132 name, namelen);
1133 if (!EVP_PKEY_CTX_get_params(ctx, params))
1134 return -1;
1135 return 1;
1136}
1137
1138/*
1139 * evp_pkey_keygen() abstracts from the explicit use of B<EVP_PKEY_CTX>
1140 * while providing a generic way of generating a new asymmetric key pair
1141 * of algorithm type I<name> (e.g., C<RSA> or C<EC>).
1142 * The library context I<libctx> and property query I<propq>
1143 * are used when fetching algorithms from providers.
1144 * The I<params> specify algorithm-specific parameters
1145 * such as the RSA modulus size or the name of an EC curve.
1146 */
1147static EVP_PKEY *evp_pkey_keygen(OSSL_LIB_CTX *libctx, const char *name,
1148 const char *propq, const OSSL_PARAM *params)
1149{
1150 EVP_PKEY *pkey = NULL;
1151 EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name(libctx, name, propq);
1152
1153 if (ctx != NULL
1154 && EVP_PKEY_keygen_init(ctx) > 0
1155 && EVP_PKEY_CTX_set_params(ctx, params))
1156 (void)EVP_PKEY_generate(ctx, &pkey);
1157
1158 EVP_PKEY_CTX_free(ctx);
1159 return pkey;
1160}
1161
1162EVP_PKEY *EVP_PKEY_Q_keygen(OSSL_LIB_CTX *libctx, const char *propq,
1163 const char *type, ...)
1164{
1165 va_list args;
1166 size_t bits;
1167 char *name;
1168 OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
1169 EVP_PKEY *ret = NULL;
1170
1171 va_start(args, type);
1172
1173 if (strcasecmp(type, "RSA") == 0) {
1174 bits = va_arg(args, size_t);
1175 params[0] = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_BITS, &bits);
1176 } else if (strcasecmp(type, "EC") == 0) {
1177 name = va_arg(args, char *);
1178 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
1179 name, 0);
1180 } else if (strcasecmp(type, "ED25519") != 0
1181 && strcasecmp(type, "X25519") != 0
1182 && strcasecmp(type, "ED448") != 0
1183 && strcasecmp(type, "X448") != 0) {
1184 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_INVALID_ARGUMENT);
1185 goto end;
1186 }
1187 ret = evp_pkey_keygen(libctx, type, propq, params);
1188
1189 end:
1190 va_end(args);
1191 return ret;
1192}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette