Adapt diverse EVP_MD functions to use get_params and set_params interfaces
[openssl.git] / crypto / evp / evp_lib.c
1 /*
2  * Copyright 1995-2018 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 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/evp.h>
13 #include <openssl/objects.h>
14 #include <openssl/params.h>
15 #include <openssl/core_names.h>
16 #include <openssl/dh.h>
17 #include "internal/evp_int.h"
18 #include "internal/provider.h"
19 #include "evp_locl.h"
20
21 #if !defined(FIPS_MODE)
22 int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
23 {
24     int ret;
25     const EVP_CIPHER *cipher = c->cipher;
26
27     if (cipher->prov != NULL) {
28         /*
29          * The cipher has come from a provider and won't have the default flags.
30          * Find the implicit form so we can check the flags.
31          * TODO(3.0): This won't work for 3rd party ciphers we know nothing about
32          * We'll need to think of something else for those.
33          */
34         cipher = EVP_get_cipherbynid(cipher->nid);
35         if (cipher == NULL) {
36             EVPerr(EVP_F_EVP_CIPHER_PARAM_TO_ASN1, ASN1_R_UNSUPPORTED_CIPHER);
37             return -1;
38         }
39     }
40
41     if (cipher->set_asn1_parameters != NULL)
42         ret = cipher->set_asn1_parameters(c, type);
43     else if (cipher->flags & EVP_CIPH_FLAG_DEFAULT_ASN1) {
44         switch (EVP_CIPHER_mode(cipher)) {
45         case EVP_CIPH_WRAP_MODE:
46             if (EVP_CIPHER_nid(cipher) == NID_id_smime_alg_CMS3DESwrap)
47                 ASN1_TYPE_set(type, V_ASN1_NULL, NULL);
48             ret = 1;
49             break;
50
51         case EVP_CIPH_GCM_MODE:
52         case EVP_CIPH_CCM_MODE:
53         case EVP_CIPH_XTS_MODE:
54         case EVP_CIPH_OCB_MODE:
55             ret = -2;
56             break;
57
58         default:
59             ret = EVP_CIPHER_set_asn1_iv(c, type);
60         }
61     } else
62         ret = -1;
63     if (ret <= 0)
64         EVPerr(EVP_F_EVP_CIPHER_PARAM_TO_ASN1, ret == -2 ?
65                ASN1_R_UNSUPPORTED_CIPHER :
66                EVP_R_CIPHER_PARAMETER_ERROR);
67     if (ret < -1)
68         ret = -1;
69     return ret;
70 }
71
72 int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
73 {
74     int ret;
75     const EVP_CIPHER *cipher = c->cipher;
76
77     if (cipher->prov != NULL) {
78         /*
79          * The cipher has come from a provider and won't have the default flags.
80          * Find the implicit form so we can check the flags.
81          */
82         cipher = EVP_get_cipherbynid(cipher->nid);
83         if (cipher == NULL)
84             return -1;
85     }
86
87     if (cipher->get_asn1_parameters != NULL)
88         ret = cipher->get_asn1_parameters(c, type);
89     else if (cipher->flags & EVP_CIPH_FLAG_DEFAULT_ASN1) {
90         switch (EVP_CIPHER_mode(cipher)) {
91
92         case EVP_CIPH_WRAP_MODE:
93             ret = 1;
94             break;
95
96         case EVP_CIPH_GCM_MODE:
97         case EVP_CIPH_CCM_MODE:
98         case EVP_CIPH_XTS_MODE:
99         case EVP_CIPH_OCB_MODE:
100             ret = -2;
101             break;
102
103         default:
104             ret = EVP_CIPHER_get_asn1_iv(c, type);
105             break;
106         }
107     } else
108         ret = -1;
109     if (ret <= 0)
110         EVPerr(EVP_F_EVP_CIPHER_ASN1_TO_PARAM, ret == -2 ?
111                EVP_R_UNSUPPORTED_CIPHER :
112                EVP_R_CIPHER_PARAMETER_ERROR);
113     if (ret < -1)
114         ret = -1;
115     return ret;
116 }
117
118 int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX *ctx, ASN1_TYPE *type)
119 {
120     int i = 0;
121     unsigned int l;
122
123     if (type != NULL) {
124         unsigned char iv[EVP_MAX_IV_LENGTH];
125
126         l = EVP_CIPHER_CTX_iv_length(ctx);
127         if (!ossl_assert(l <= sizeof(iv)))
128             return -1;
129         i = ASN1_TYPE_get_octetstring(type, iv, l);
130         if (i != (int)l)
131             return -1;
132
133         if (!EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1))
134             return -1;
135     }
136     return i;
137 }
138
139 int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
140 {
141     int i = 0;
142     unsigned int j;
143
144     if (type != NULL) {
145         j = EVP_CIPHER_CTX_iv_length(c);
146         OPENSSL_assert(j <= sizeof(c->iv));
147         i = ASN1_TYPE_set_octetstring(type, c->oiv, j);
148     }
149     return i;
150 }
151 #endif /* !defined(FIPS_MODE) */
152
153 /* Convert the various cipher NIDs and dummies to a proper OID NID */
154 int EVP_CIPHER_type(const EVP_CIPHER *ctx)
155 {
156     int nid;
157     nid = EVP_CIPHER_nid(ctx);
158
159     switch (nid) {
160
161     case NID_rc2_cbc:
162     case NID_rc2_64_cbc:
163     case NID_rc2_40_cbc:
164
165         return NID_rc2_cbc;
166
167     case NID_rc4:
168     case NID_rc4_40:
169
170         return NID_rc4;
171
172     case NID_aes_128_cfb128:
173     case NID_aes_128_cfb8:
174     case NID_aes_128_cfb1:
175
176         return NID_aes_128_cfb128;
177
178     case NID_aes_192_cfb128:
179     case NID_aes_192_cfb8:
180     case NID_aes_192_cfb1:
181
182         return NID_aes_192_cfb128;
183
184     case NID_aes_256_cfb128:
185     case NID_aes_256_cfb8:
186     case NID_aes_256_cfb1:
187
188         return NID_aes_256_cfb128;
189
190     case NID_des_cfb64:
191     case NID_des_cfb8:
192     case NID_des_cfb1:
193
194         return NID_des_cfb64;
195
196     case NID_des_ede3_cfb64:
197     case NID_des_ede3_cfb8:
198     case NID_des_ede3_cfb1:
199
200         return NID_des_cfb64;
201
202     default:
203 #ifdef FIPS_MODE
204         return NID_undef;
205 #else
206         {
207             /* Check it has an OID and it is valid */
208             ASN1_OBJECT *otmp = OBJ_nid2obj(nid);
209
210             if (OBJ_get0_data(otmp) == NULL)
211                 nid = NID_undef;
212             ASN1_OBJECT_free(otmp);
213             return nid;
214         }
215 #endif
216     }
217 }
218
219 int EVP_CIPHER_block_size(const EVP_CIPHER *cipher)
220 {
221     int ok, v = cipher->block_size;
222     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
223
224     params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_BLOCK_SIZE, &v);
225     ok = evp_do_ciph_getparams(cipher, params);
226
227     return ok != 0 ? v : EVP_CTRL_RET_UNSUPPORTED;
228 }
229
230 int EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx)
231 {
232     return EVP_CIPHER_block_size(ctx->cipher);
233 }
234
235 int EVP_CIPHER_impl_ctx_size(const EVP_CIPHER *e)
236 {
237     return e->ctx_size;
238 }
239
240 int EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
241                const unsigned char *in, unsigned int inl)
242 {
243     if (ctx->cipher->prov != NULL) {
244         size_t outl = 0;         /* ignored */
245         int blocksize = EVP_CIPHER_CTX_block_size(ctx);
246
247         if (ctx->cipher->ccipher != NULL)
248             return
249                 ctx->cipher->ccipher(ctx->provctx, out, &outl,
250                                      inl + (blocksize == 1 ? 0 : blocksize),
251                                      in, (size_t)inl);
252         return 0;
253     }
254
255     return ctx->cipher->do_cipher(ctx, out, in, inl);
256 }
257
258 const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx)
259 {
260     return ctx->cipher;
261 }
262
263 int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX *ctx)
264 {
265     return ctx->encrypt;
266 }
267
268 unsigned long EVP_CIPHER_flags(const EVP_CIPHER *cipher)
269 {
270     int ok;
271     unsigned long v = cipher->flags;
272     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
273
274     params[0] = OSSL_PARAM_construct_ulong(OSSL_CIPHER_PARAM_FLAGS, &v);
275     ok = evp_do_ciph_getparams(cipher, params);
276
277     return ok != 0 ? v : 0;
278 }
279
280 void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx)
281 {
282     return ctx->app_data;
283 }
284
285 void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
286 {
287     ctx->app_data = data;
288 }
289
290 void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX *ctx)
291 {
292     return ctx->cipher_data;
293 }
294
295 void *EVP_CIPHER_CTX_set_cipher_data(EVP_CIPHER_CTX *ctx, void *cipher_data)
296 {
297     void *old_cipher_data;
298
299     old_cipher_data = ctx->cipher_data;
300     ctx->cipher_data = cipher_data;
301
302     return old_cipher_data;
303 }
304
305 int EVP_CIPHER_iv_length(const EVP_CIPHER *cipher)
306 {
307     int ok, v = cipher->iv_len;
308     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
309
310     params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_IVLEN, &v);
311     ok = evp_do_ciph_getparams(cipher, params);
312
313     return ok != 0 ? v : EVP_CTRL_RET_UNSUPPORTED;
314 }
315
316 int EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx)
317 {
318     int len, rv, v = EVP_CIPHER_iv_length(ctx->cipher);
319     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
320
321     params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_IVLEN, &v);
322     rv = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
323     if (rv == EVP_CTRL_RET_UNSUPPORTED)
324         goto legacy;
325     return rv != 0 ? v : -1;
326     /* TODO (3.0) Remove legacy support */
327 legacy:
328     if ((EVP_CIPHER_flags(ctx->cipher) & EVP_CIPH_CUSTOM_IV_LENGTH) != 0) {
329         rv = EVP_CIPHER_CTX_ctrl((EVP_CIPHER_CTX *)ctx, EVP_CTRL_GET_IVLEN,
330                                  0, &len);
331         return (rv == 1) ? len : -1;
332     }
333     return v;
334 }
335
336 const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX *ctx)
337 {
338     return ctx->oiv;
339 }
340
341 /*
342  * OSSL_PARAM_OCTET_PTR gets us the pointer to the running IV in the provider
343  */
344 const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx)
345 {
346     int ok;
347     const unsigned char *v = ctx->iv;
348     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
349
350     params[0] =
351         OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV, (void **)&v,
352                                        sizeof(ctx->iv));
353     ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
354
355     return ok != 0 ? v : NULL;
356 }
357
358 unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
359 {
360     int ok;
361     unsigned char *v = ctx->iv;
362     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
363
364     params[0] =
365         OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV, (void **)&v,
366                                        sizeof(ctx->iv));
367     ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
368
369     return ok != 0 ? v : NULL;
370 }
371
372 unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX *ctx)
373 {
374     return ctx->buf;
375 }
376
377 int EVP_CIPHER_CTX_num(const EVP_CIPHER_CTX *ctx)
378 {
379     int ok, v = ctx->num;
380     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
381
382     params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_NUM, &v);
383     ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
384
385     return ok != 0 ? v : EVP_CTRL_RET_UNSUPPORTED;
386 }
387
388 int EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num)
389 {
390     int ok;
391     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
392
393     params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_NUM, &num);
394     ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->provctx, params);
395
396     if (ok != 0)
397         ctx->num = num;
398     return ok != 0;
399 }
400
401 int EVP_CIPHER_key_length(const EVP_CIPHER *cipher)
402 {
403     int ok, v = cipher->key_len;
404     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
405
406     params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_KEYLEN, &v);
407     ok = evp_do_ciph_getparams(cipher, params);
408
409     return ok != 0 ? v : EVP_CTRL_RET_UNSUPPORTED;
410 }
411
412 int EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx)
413 {
414     int ok, v = ctx->key_len;
415     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
416
417     params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_KEYLEN, &v);
418     ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
419
420     return ok != 0 ? v : EVP_CTRL_RET_UNSUPPORTED;
421 }
422
423 int EVP_CIPHER_nid(const EVP_CIPHER *cipher)
424 {
425     return cipher->nid;
426 }
427
428 int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx)
429 {
430     return ctx->cipher->nid;
431 }
432
433 const char *EVP_CIPHER_name(const EVP_CIPHER *cipher)
434 {
435     if (cipher->prov != NULL)
436         return cipher->name;
437 #ifndef FIPS_MODE
438     return OBJ_nid2sn(EVP_CIPHER_nid(cipher));
439 #else
440     return NULL;
441 #endif
442 }
443
444 const OSSL_PROVIDER *EVP_CIPHER_provider(const EVP_CIPHER *cipher)
445 {
446     return cipher->prov;
447 }
448
449 int EVP_CIPHER_mode(const EVP_CIPHER *cipher)
450 {
451     int ok, v = EVP_CIPHER_flags(cipher) & EVP_CIPH_MODE;
452     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
453
454     params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_MODE, &v);
455     ok = evp_do_ciph_getparams(cipher, params);
456
457     return ok != 0 ? v : 0;
458 }
459
460 const char *EVP_MD_name(const EVP_MD *md)
461 {
462     if (md->prov != NULL)
463         return md->name;
464 #ifndef FIPS_MODE
465     return OBJ_nid2sn(EVP_MD_nid(md));
466 #else
467     return NULL;
468 #endif
469 }
470
471 const OSSL_PROVIDER *EVP_MD_provider(const EVP_MD *md)
472 {
473     return md->prov;
474 }
475
476 int EVP_MD_block_size(const EVP_MD *md)
477 {
478     int ok, v = md->block_size;
479     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
480
481     if (md == NULL) {
482         EVPerr(EVP_F_EVP_MD_BLOCK_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL);
483         return -1;
484     }
485
486     params[0] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_BLOCK_SIZE, &v);
487     ok = evp_do_md_getparams(md, params);
488
489     return ok != 0 ? v : -1;
490 }
491
492 int EVP_MD_type(const EVP_MD *md)
493 {
494     return md->type;
495 }
496
497 int EVP_MD_pkey_type(const EVP_MD *md)
498 {
499     return md->pkey_type;
500 }
501
502 int EVP_MD_size(const EVP_MD *md)
503 {
504     int ok, v = md->md_size;
505     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
506
507     if (md == NULL) {
508         EVPerr(EVP_F_EVP_MD_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL);
509         return -1;
510     }
511
512     params[0] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_SIZE, &v);
513     ok = evp_do_md_getparams(md, params);
514
515     return ok != 0 ? v : -1;
516 }
517
518 unsigned long EVP_MD_flags(const EVP_MD *md)
519 {
520     int ok;
521     unsigned long v = md->flags;
522     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
523
524     params[0] = OSSL_PARAM_construct_ulong(OSSL_CIPHER_PARAM_FLAGS, &v);
525     ok = evp_do_md_getparams(md, params);
526
527     return ok != 0 ? v : 0;
528 }
529
530 EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type)
531 {
532     EVP_MD *md = OPENSSL_zalloc(sizeof(*md));
533
534     if (md != NULL) {
535         md->type = md_type;
536         md->pkey_type = pkey_type;
537         md->lock = CRYPTO_THREAD_lock_new();
538         if (md->lock == NULL) {
539             OPENSSL_free(md);
540             return NULL;
541         }
542         md->refcnt = 1;
543     }
544     return md;
545 }
546
547 EVP_MD *EVP_MD_meth_dup(const EVP_MD *md)
548 {
549     EVP_MD *to = EVP_MD_meth_new(md->type, md->pkey_type);
550
551     if (to != NULL) {
552         CRYPTO_RWLOCK *lock = to->lock;
553         memcpy(to, md, sizeof(*to));
554         to->lock = lock;
555     }
556     return to;
557 }
558
559 int EVP_MD_up_ref(EVP_MD *md)
560 {
561     int ref = 0;
562
563     CRYPTO_UP_REF(&md->refcnt, &ref, md->lock);
564     return 1;
565 }
566
567 void EVP_MD_meth_free(EVP_MD *md)
568 {
569     if (md != NULL) {
570         int i;
571
572         CRYPTO_DOWN_REF(&md->refcnt, &i, md->lock);
573         if (i > 0)
574             return;
575         ossl_provider_free(md->prov);
576         OPENSSL_free(md->name);
577         CRYPTO_THREAD_lock_free(md->lock);
578         OPENSSL_free(md);
579     }
580 }
581 int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize)
582 {
583     md->block_size = blocksize;
584     return 1;
585 }
586 int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize)
587 {
588     md->md_size = resultsize;
589     return 1;
590 }
591 int EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize)
592 {
593     md->ctx_size = datasize;
594     return 1;
595 }
596 int EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags)
597 {
598     md->flags = flags;
599     return 1;
600 }
601 int EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx))
602 {
603     md->init = init;
604     return 1;
605 }
606 int EVP_MD_meth_set_update(EVP_MD *md, int (*update)(EVP_MD_CTX *ctx,
607                                                      const void *data,
608                                                      size_t count))
609 {
610     md->update = update;
611     return 1;
612 }
613 int EVP_MD_meth_set_final(EVP_MD *md, int (*final)(EVP_MD_CTX *ctx,
614                                                    unsigned char *md))
615 {
616     md->final = final;
617     return 1;
618 }
619 int EVP_MD_meth_set_copy(EVP_MD *md, int (*copy)(EVP_MD_CTX *to,
620                                                  const EVP_MD_CTX *from))
621 {
622     md->copy = copy;
623     return 1;
624 }
625 int EVP_MD_meth_set_cleanup(EVP_MD *md, int (*cleanup)(EVP_MD_CTX *ctx))
626 {
627     md->cleanup = cleanup;
628     return 1;
629 }
630 int EVP_MD_meth_set_ctrl(EVP_MD *md, int (*ctrl)(EVP_MD_CTX *ctx, int cmd,
631                                                  int p1, void *p2))
632 {
633     md->md_ctrl = ctrl;
634     return 1;
635 }
636
637 int EVP_MD_meth_get_input_blocksize(const EVP_MD *md)
638 {
639     return md->block_size;
640 }
641 int EVP_MD_meth_get_result_size(const EVP_MD *md)
642 {
643     return md->md_size;
644 }
645 int EVP_MD_meth_get_app_datasize(const EVP_MD *md)
646 {
647     return md->ctx_size;
648 }
649 unsigned long EVP_MD_meth_get_flags(const EVP_MD *md)
650 {
651     return md->flags;
652 }
653 int (*EVP_MD_meth_get_init(const EVP_MD *md))(EVP_MD_CTX *ctx)
654 {
655     return md->init;
656 }
657 int (*EVP_MD_meth_get_update(const EVP_MD *md))(EVP_MD_CTX *ctx,
658                                                 const void *data,
659                                                 size_t count)
660 {
661     return md->update;
662 }
663 int (*EVP_MD_meth_get_final(const EVP_MD *md))(EVP_MD_CTX *ctx,
664                                                unsigned char *md)
665 {
666     return md->final;
667 }
668 int (*EVP_MD_meth_get_copy(const EVP_MD *md))(EVP_MD_CTX *to,
669                                               const EVP_MD_CTX *from)
670 {
671     return md->copy;
672 }
673 int (*EVP_MD_meth_get_cleanup(const EVP_MD *md))(EVP_MD_CTX *ctx)
674 {
675     return md->cleanup;
676 }
677 int (*EVP_MD_meth_get_ctrl(const EVP_MD *md))(EVP_MD_CTX *ctx, int cmd,
678                                               int p1, void *p2)
679 {
680     return md->md_ctrl;
681 }
682
683 const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx)
684 {
685     if (ctx == NULL)
686         return NULL;
687     return ctx->reqdigest;
688 }
689
690 EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx)
691 {
692     return ctx->pctx;
693 }
694
695 #if !defined(FIPS_MODE)
696 /* TODO(3.0): EVP_DigestSign* not yet supported in FIPS module */
697 void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx)
698 {
699     /*
700      * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so
701      * we have to deal with the cleanup job here.
702      */
703     if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
704         EVP_PKEY_CTX_free(ctx->pctx);
705
706     ctx->pctx = pctx;
707
708     if (pctx != NULL) {
709         /* make sure pctx is not freed when destroying EVP_MD_CTX */
710         EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
711     } else {
712         EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
713     }
714 }
715 #endif /* !defined(FIPS_MODE) */
716
717 void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx)
718 {
719     return ctx->md_data;
720 }
721
722 int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
723                                              const void *data, size_t count)
724 {
725     return ctx->update;
726 }
727
728 void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
729                               int (*update) (EVP_MD_CTX *ctx,
730                                              const void *data, size_t count))
731 {
732     ctx->update = update;
733 }
734
735 void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags)
736 {
737     ctx->flags |= flags;
738 }
739
740 void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags)
741 {
742     ctx->flags &= ~flags;
743 }
744
745 int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags)
746 {
747     return (ctx->flags & flags);
748 }
749
750 void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags)
751 {
752     ctx->flags |= flags;
753 }
754
755 void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags)
756 {
757     ctx->flags &= ~flags;
758 }
759
760 int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags)
761 {
762     return (ctx->flags & flags);
763 }
764
765 int EVP_str2ctrl(int (*cb)(void *ctx, int cmd, void *buf, size_t buflen),
766                  void *ctx, int cmd, const char *value)
767 {
768     size_t len;
769
770     len = strlen(value);
771     if (len > INT_MAX)
772         return -1;
773     return cb(ctx, cmd, (void *)value, len);
774 }
775
776 int EVP_hex2ctrl(int (*cb)(void *ctx, int cmd, void *buf, size_t buflen),
777                  void *ctx, int cmd, const char *hex)
778 {
779     unsigned char *bin;
780     long binlen;
781     int rv = -1;
782
783     bin = OPENSSL_hexstr2buf(hex, &binlen);
784     if (bin == NULL)
785         return 0;
786     if (binlen <= INT_MAX)
787         rv = cb(ctx, cmd, bin, binlen);
788     OPENSSL_free(bin);
789     return rv;
790 }