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