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