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