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