Move EVP_PKEY algorithm implementations into a union
[openssl.git] / crypto / evp / pmeth_lib.c
1
2 /*
3  * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
4  *
5  * Licensed under the Apache License 2.0 (the "License").  You may not use
6  * this file except in compliance with the License.  You can obtain a copy
7  * in the file LICENSE in the source distribution or at
8  * https://www.openssl.org/source/license.html
9  */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <openssl/engine.h>
14 #include <openssl/evp.h>
15 #include <openssl/x509v3.h>
16 #include <openssl/core_names.h>
17 #include <openssl/dh.h>
18 #include "internal/cryptlib.h"
19 #include "internal/asn1_int.h"
20 #include "internal/evp_int.h"
21 #include "internal/numbers.h"
22 #include "internal/provider.h"
23 #include "evp_locl.h"
24
25 typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
26
27 static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
28
29 /* This array needs to be in order of NIDs */
30 static const EVP_PKEY_METHOD *standard_methods[] = {
31 #ifndef OPENSSL_NO_RSA
32     &rsa_pkey_meth,
33 #endif
34 #ifndef OPENSSL_NO_DH
35     &dh_pkey_meth,
36 #endif
37 #ifndef OPENSSL_NO_DSA
38     &dsa_pkey_meth,
39 #endif
40 #ifndef OPENSSL_NO_EC
41     &ec_pkey_meth,
42 #endif
43     &hmac_pkey_meth,
44 #ifndef OPENSSL_NO_CMAC
45     &cmac_pkey_meth,
46 #endif
47 #ifndef OPENSSL_NO_RSA
48     &rsa_pss_pkey_meth,
49 #endif
50 #ifndef OPENSSL_NO_DH
51     &dhx_pkey_meth,
52 #endif
53 #ifndef OPENSSL_NO_SCRYPT
54     &scrypt_pkey_meth,
55 #endif
56     &tls1_prf_pkey_meth,
57 #ifndef OPENSSL_NO_EC
58     &ecx25519_pkey_meth,
59     &ecx448_pkey_meth,
60 #endif
61     &hkdf_pkey_meth,
62 #ifndef OPENSSL_NO_POLY1305
63     &poly1305_pkey_meth,
64 #endif
65 #ifndef OPENSSL_NO_SIPHASH
66     &siphash_pkey_meth,
67 #endif
68 #ifndef OPENSSL_NO_EC
69     &ed25519_pkey_meth,
70     &ed448_pkey_meth,
71 #endif
72 #ifndef OPENSSL_NO_SM2
73     &sm2_pkey_meth,
74 #endif
75 };
76
77 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
78                            pmeth);
79
80 static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
81                      const EVP_PKEY_METHOD *const *b)
82 {
83     return ((*a)->pkey_id - (*b)->pkey_id);
84 }
85
86 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
87                              pmeth);
88
89 const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
90 {
91     EVP_PKEY_METHOD tmp;
92     const EVP_PKEY_METHOD *t = &tmp, **ret;
93     tmp.pkey_id = type;
94     if (app_pkey_methods) {
95         int idx;
96         idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
97         if (idx >= 0)
98             return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
99     }
100     ret = OBJ_bsearch_pmeth(&t, standard_methods,
101                             sizeof(standard_methods) /
102                             sizeof(EVP_PKEY_METHOD *));
103     if (!ret || !*ret)
104         return NULL;
105     return *ret;
106 }
107
108 static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id)
109 {
110     EVP_PKEY_CTX *ret;
111     const EVP_PKEY_METHOD *pmeth = NULL;
112
113     /*
114      * When using providers, the context is bound to the algo implementation
115      * later.
116      */
117     if (pkey == NULL && e == NULL && id == -1)
118         goto common;
119
120     /* TODO(3.0) Legacy code should be removed when all is provider based */
121     /* BEGIN legacy */
122     if (id == -1) {
123         if (pkey == NULL)
124             return 0;
125         id = pkey->type;
126     }
127 #ifndef OPENSSL_NO_ENGINE
128     if (e == NULL && pkey != NULL)
129         e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine;
130     /* Try to find an ENGINE which implements this method */
131     if (e) {
132         if (!ENGINE_init(e)) {
133             EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB);
134             return NULL;
135         }
136     } else {
137         e = ENGINE_get_pkey_meth_engine(id);
138     }
139
140     /*
141      * If an ENGINE handled this method look it up. Otherwise use internal
142      * tables.
143      */
144     if (e)
145         pmeth = ENGINE_get_pkey_meth(e, id);
146     else
147 #endif
148         pmeth = EVP_PKEY_meth_find(id);
149
150     if (pmeth == NULL) {
151 #ifndef OPENSSL_NO_ENGINE
152         ENGINE_finish(e);
153 #endif
154         EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM);
155         return NULL;
156     }
157     /* END legacy */
158
159  common:
160     ret = OPENSSL_zalloc(sizeof(*ret));
161     if (ret == NULL) {
162 #ifndef OPENSSL_NO_ENGINE
163         ENGINE_finish(e);
164 #endif
165         EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE);
166         return NULL;
167     }
168     ret->engine = e;
169     ret->pmeth = pmeth;
170     ret->operation = EVP_PKEY_OP_UNDEFINED;
171     ret->pkey = pkey;
172     if (pkey != NULL)
173         EVP_PKEY_up_ref(pkey);
174
175     if (pmeth != NULL && pmeth->init != NULL) {
176         if (pmeth->init(ret) <= 0) {
177             ret->pmeth = NULL;
178             EVP_PKEY_CTX_free(ret);
179             return NULL;
180         }
181     }
182
183     return ret;
184 }
185
186 void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx)
187 {
188     if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
189         if (ctx->op.kex.exchprovctx != NULL && ctx->op.kex.exchange != NULL)
190             ctx->op.kex.exchange->freectx(ctx->op.kex.exchprovctx);
191         EVP_KEYEXCH_free(ctx->op.kex.exchange);
192     } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
193         if (ctx->op.sig.sigprovctx != NULL && ctx->op.sig.signature != NULL)
194             ctx->op.sig.signature->freectx(ctx->op.sig.sigprovctx);
195         EVP_SIGNATURE_free(ctx->op.sig.signature);
196     }
197 }
198
199 EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
200 {
201     EVP_PKEY_METHOD *pmeth;
202
203     pmeth = OPENSSL_zalloc(sizeof(*pmeth));
204     if (pmeth == NULL) {
205         EVPerr(EVP_F_EVP_PKEY_METH_NEW, ERR_R_MALLOC_FAILURE);
206         return NULL;
207     }
208
209     pmeth->pkey_id = id;
210     pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
211     return pmeth;
212 }
213
214 void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
215                              const EVP_PKEY_METHOD *meth)
216 {
217     if (ppkey_id)
218         *ppkey_id = meth->pkey_id;
219     if (pflags)
220         *pflags = meth->flags;
221 }
222
223 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
224 {
225
226     dst->init = src->init;
227     dst->copy = src->copy;
228     dst->cleanup = src->cleanup;
229
230     dst->paramgen_init = src->paramgen_init;
231     dst->paramgen = src->paramgen;
232
233     dst->keygen_init = src->keygen_init;
234     dst->keygen = src->keygen;
235
236     dst->sign_init = src->sign_init;
237     dst->sign = src->sign;
238
239     dst->verify_init = src->verify_init;
240     dst->verify = src->verify;
241
242     dst->verify_recover_init = src->verify_recover_init;
243     dst->verify_recover = src->verify_recover;
244
245     dst->signctx_init = src->signctx_init;
246     dst->signctx = src->signctx;
247
248     dst->verifyctx_init = src->verifyctx_init;
249     dst->verifyctx = src->verifyctx;
250
251     dst->encrypt_init = src->encrypt_init;
252     dst->encrypt = src->encrypt;
253
254     dst->decrypt_init = src->decrypt_init;
255     dst->decrypt = src->decrypt;
256
257     dst->derive_init = src->derive_init;
258     dst->derive = src->derive;
259
260     dst->ctrl = src->ctrl;
261     dst->ctrl_str = src->ctrl_str;
262
263     dst->check = src->check;
264 }
265
266 void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
267 {
268     if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
269         OPENSSL_free(pmeth);
270 }
271
272 EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
273 {
274     return int_ctx_new(pkey, e, -1);
275 }
276
277 EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
278 {
279     return int_ctx_new(NULL, e, id);
280 }
281
282 EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx)
283 {
284     EVP_PKEY_CTX *rctx;
285
286     if (((pctx->pmeth == NULL) || (pctx->pmeth->copy == NULL))
287             && ((EVP_PKEY_CTX_IS_DERIVE_OP(pctx)
288                  && pctx->op.kex.exchprovctx == NULL)
289                 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)
290                     && pctx->op.sig.sigprovctx == NULL)))
291         return NULL;
292 #ifndef OPENSSL_NO_ENGINE
293     /* Make sure it's safe to copy a pkey context using an ENGINE */
294     if (pctx->engine && !ENGINE_init(pctx->engine)) {
295         EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
296         return 0;
297     }
298 #endif
299     rctx = OPENSSL_zalloc(sizeof(*rctx));
300     if (rctx == NULL) {
301         EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_MALLOC_FAILURE);
302         return NULL;
303     }
304
305     if (pctx->pkey != NULL)
306         EVP_PKEY_up_ref(pctx->pkey);
307     rctx->pkey = pctx->pkey;
308     rctx->operation = pctx->operation;
309
310     if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) {
311         if (pctx->op.kex.exchange != NULL) {
312             rctx->op.kex.exchange = pctx->op.kex.exchange;
313             if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange)) {
314                 OPENSSL_free(rctx);
315                 return NULL;
316             }
317         }
318         if (pctx->op.kex.exchprovctx != NULL) {
319             if (!ossl_assert(pctx->op.kex.exchange != NULL))
320                 return NULL;
321             rctx->op.kex.exchprovctx
322                 = pctx->op.kex.exchange->dupctx(pctx->op.kex.exchprovctx);
323             if (rctx->op.kex.exchprovctx == NULL) {
324                 EVP_KEYEXCH_free(rctx->op.kex.exchange);
325                 OPENSSL_free(rctx);
326                 return NULL;
327             }
328             return rctx;
329         }
330     } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
331         if (pctx->op.sig.signature != NULL) {
332             rctx->op.sig.signature = pctx->op.sig.signature;
333             if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature)) {
334                 OPENSSL_free(rctx);
335                 return NULL;
336             }
337         }
338         if (pctx->op.sig.sigprovctx != NULL) {
339             if (!ossl_assert(pctx->op.sig.signature != NULL))
340                 return NULL;
341             rctx->op.sig.sigprovctx
342                 = pctx->op.sig.signature->dupctx(pctx->op.sig.sigprovctx);
343             if (rctx->op.sig.sigprovctx == NULL) {
344                 EVP_SIGNATURE_free(rctx->op.sig.signature);
345                 OPENSSL_free(rctx);
346                 return NULL;
347             }
348             return rctx;
349         }
350     }
351
352     rctx->pmeth = pctx->pmeth;
353 #ifndef OPENSSL_NO_ENGINE
354     rctx->engine = pctx->engine;
355 #endif
356
357     if (pctx->peerkey)
358         EVP_PKEY_up_ref(pctx->peerkey);
359     rctx->peerkey = pctx->peerkey;
360
361     if (pctx->pmeth->copy(rctx, pctx) > 0)
362         return rctx;
363
364     rctx->pmeth = NULL;
365     EVP_PKEY_CTX_free(rctx);
366     return NULL;
367
368 }
369
370 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
371 {
372     if (app_pkey_methods == NULL) {
373         app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
374         if (app_pkey_methods == NULL){
375             EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
376             return 0;
377         }
378     }
379     if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
380         EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
381         return 0;
382     }
383     sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
384     return 1;
385 }
386
387 void evp_app_cleanup_int(void)
388 {
389     if (app_pkey_methods != NULL)
390         sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
391 }
392
393 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
394 {
395     const EVP_PKEY_METHOD *ret;
396
397     ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
398
399     return ret == NULL ? 0 : 1;
400 }
401
402 size_t EVP_PKEY_meth_get_count(void)
403 {
404     size_t rv = OSSL_NELEM(standard_methods);
405
406     if (app_pkey_methods)
407         rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
408     return rv;
409 }
410
411 const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
412 {
413     if (idx < OSSL_NELEM(standard_methods))
414         return standard_methods[idx];
415     if (app_pkey_methods == NULL)
416         return NULL;
417     idx -= OSSL_NELEM(standard_methods);
418     if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
419         return NULL;
420     return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
421 }
422
423 void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
424 {
425     if (ctx == NULL)
426         return;
427     if (ctx->pmeth && ctx->pmeth->cleanup)
428         ctx->pmeth->cleanup(ctx);
429
430     evp_pkey_ctx_free_old_ops(ctx);
431
432     EVP_PKEY_free(ctx->pkey);
433     EVP_PKEY_free(ctx->peerkey);
434 #ifndef OPENSSL_NO_ENGINE
435     ENGINE_finish(ctx->engine);
436 #endif
437     OPENSSL_free(ctx);
438 }
439
440 int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
441 {
442     if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
443             && ctx->op.sig.sigprovctx != NULL
444             && ctx->op.sig.signature != NULL
445             && ctx->op.sig.signature->get_ctx_params != NULL)
446         return ctx->op.sig.signature->get_ctx_params(ctx->op.sig.sigprovctx,
447                                                      params);
448     return 0;
449 }
450
451 const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX *ctx)
452 {
453     if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
454             && ctx->op.sig.signature != NULL
455             && ctx->op.sig.signature->gettable_ctx_params != NULL)
456         return ctx->op.sig.signature->gettable_ctx_params();
457
458     return NULL;
459 }
460
461 int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
462 {
463     if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
464             && ctx->op.kex.exchprovctx != NULL
465             && ctx->op.kex.exchange != NULL
466             && ctx->op.kex.exchange->set_ctx_params != NULL)
467         return ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.exchprovctx,
468                                                     params);
469     if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
470             && ctx->op.sig.sigprovctx != NULL
471             && ctx->op.sig.signature != NULL
472             && ctx->op.sig.signature->set_ctx_params != NULL)
473         return ctx->op.sig.signature->set_ctx_params(ctx->op.sig.sigprovctx,
474                                                      params);
475     return 0;
476 }
477
478 const OSSL_PARAM *EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX *ctx)
479 {
480     if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
481             && ctx->op.kex.exchange != NULL
482             && ctx->op.kex.exchange->settable_ctx_params != NULL)
483         return ctx->op.kex.exchange->settable_ctx_params();
484     if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
485             && ctx->op.sig.signature != NULL
486             && ctx->op.sig.signature->settable_ctx_params != NULL)
487         return ctx->op.sig.signature->settable_ctx_params();
488
489     return NULL;
490 }
491
492 #ifndef OPENSSL_NO_DH
493 int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX *ctx, int pad)
494 {
495     OSSL_PARAM dh_pad_params[2];
496     unsigned int upad = pad;
497
498     /* We use EVP_PKEY_CTX_ctrl return values */
499     if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
500         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
501         return -2;
502     }
503
504     /* TODO(3.0): Remove this eventually when no more legacy */
505     if (ctx->op.kex.exchprovctx == NULL)
506         return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_DERIVE,
507                                  EVP_PKEY_CTRL_DH_PAD, pad, NULL);
508
509     dh_pad_params[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD, &upad);
510     dh_pad_params[1] = OSSL_PARAM_construct_end();
511
512     return EVP_PKEY_CTX_set_params(ctx, dh_pad_params);
513 }
514 #endif
515
516 int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
517 {
518     OSSL_PARAM sig_md_params[3], *p = sig_md_params;
519     /* 80 should be big enough */
520     char name[80] = "";
521     const EVP_MD *tmp;
522
523     if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
524         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
525         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
526         return -2;
527     }
528
529     /* TODO(3.0): Remove this eventually when no more legacy */
530     if (ctx->op.sig.sigprovctx == NULL)
531         return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
532                                  EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
533
534     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
535                                             name,
536                                             sizeof(name));
537     *p++ = OSSL_PARAM_construct_end();
538
539     if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
540         return 0;
541
542     tmp = EVP_get_digestbyname(name);
543     if (tmp == NULL)
544         return 0;
545
546     *md = tmp;
547
548     return 1;
549 }
550
551 int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
552 {
553     OSSL_PARAM sig_md_params[3], *p = sig_md_params;
554     size_t mdsize;
555     const char *name;
556
557     if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
558         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
559         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
560         return -2;
561     }
562
563     /* TODO(3.0): Remove this eventually when no more legacy */
564     if (ctx->op.sig.sigprovctx == NULL)
565         return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
566                                  EVP_PKEY_CTRL_MD, 0, (void *)(md));
567
568     if (md == NULL) {
569         name = "";
570         mdsize = 0;
571     } else {
572         mdsize = EVP_MD_size(md);
573         name = EVP_MD_name(md);
574     }
575
576     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
577                                             /*
578                                              * Cast away the const. This is read
579                                              * only so should be safe
580                                              */
581                                             (char *)name,
582                                             strlen(name) + 1);
583     *p++ = OSSL_PARAM_construct_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE,
584                                        &mdsize);
585     *p++ = OSSL_PARAM_construct_end();
586
587     return EVP_PKEY_CTX_set_params(ctx, sig_md_params);
588 }
589
590 static int legacy_ctrl_to_param(EVP_PKEY_CTX *ctx, int keytype, int optype,
591                                 int cmd, int p1, void *p2)
592 {
593     switch (cmd) {
594 #ifndef OPENSSL_NO_DH
595     case EVP_PKEY_CTRL_DH_PAD:
596         return EVP_PKEY_CTX_set_dh_pad(ctx, p1);
597 #endif
598     case EVP_PKEY_CTRL_MD:
599         return EVP_PKEY_CTX_set_signature_md(ctx, p2);
600     case EVP_PKEY_CTRL_GET_MD:
601         return EVP_PKEY_CTX_get_signature_md(ctx, p2);
602     }
603     return 0;
604 }
605
606 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
607                       int cmd, int p1, void *p2)
608 {
609     int ret;
610
611     if (ctx == NULL) {
612         EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
613         return -2;
614     }
615
616     if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
617             || (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
618                 && ctx->op.sig.sigprovctx != NULL))
619         return legacy_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
620
621     if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
622         EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
623         return -2;
624     }
625     if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
626         return -1;
627
628     /* Skip the operation checks since this is called in a very early stage */
629     if (ctx->pmeth->digest_custom != NULL)
630         goto doit;
631
632     if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
633         EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
634         return -1;
635     }
636
637     if ((optype != -1) && !(ctx->operation & optype)) {
638         EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
639         return -1;
640     }
641
642  doit:
643     ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
644
645     if (ret == -2)
646         EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
647
648     return ret;
649 }
650
651 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
652                              int cmd, uint64_t value)
653 {
654     return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
655 }
656
657 static int legacy_ctrl_str_to_param(EVP_PKEY_CTX *ctx, const char *name,
658                                     const char *value)
659 {
660 #ifndef OPENSSL_NO_DH
661     if (strcmp(name, "dh_pad") == 0) {
662         int pad;
663
664         pad = atoi(value);
665         return EVP_PKEY_CTX_set_dh_pad(ctx, pad);
666     }
667 #endif
668     if (strcmp(name, "digest") == 0) {
669         int ret;
670         EVP_MD *md;
671
672         if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) || ctx->op.sig.signature == NULL)
673             return 0;
674         md = EVP_MD_fetch(ossl_provider_library_context(ctx->op.sig.signature->prov),
675                           value, NULL);
676         if (md == NULL)
677             return 0;
678         ret = EVP_PKEY_CTX_set_signature_md(ctx, md);
679         EVP_MD_meth_free(md);
680         return ret;
681     }
682
683     return 0;
684 }
685
686 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
687                           const char *name, const char *value)
688 {
689     if (ctx == NULL) {
690         EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
691         return -2;
692     }
693
694     if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
695             || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
696                 && ctx->op.sig.sigprovctx != NULL))
697         return legacy_ctrl_str_to_param(ctx, name, value);
698
699     if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
700         EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
701         return -2;
702     }
703     if (strcmp(name, "digest") == 0)
704         return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD,
705                                value);
706     return ctx->pmeth->ctrl_str(ctx, name, value);
707 }
708
709 /* Utility functions to send a string of hex string to a ctrl */
710
711 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
712 {
713     size_t len;
714
715     len = strlen(str);
716     if (len > INT_MAX)
717         return -1;
718     return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
719 }
720
721 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
722 {
723     unsigned char *bin;
724     long binlen;
725     int rv = -1;
726
727     bin = OPENSSL_hexstr2buf(hex, &binlen);
728     if (bin == NULL)
729         return 0;
730     if (binlen <= INT_MAX)
731         rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
732     OPENSSL_free(bin);
733     return rv;
734 }
735
736 /* Pass a message digest to a ctrl */
737 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
738 {
739     const EVP_MD *m;
740
741     if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
742         EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
743         return 0;
744     }
745     return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
746 }
747
748 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
749 {
750     return ctx->operation;
751 }
752
753 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
754 {
755     ctx->keygen_info = dat;
756     ctx->keygen_info_count = datlen;
757 }
758
759 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
760 {
761     ctx->data = data;
762 }
763
764 void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
765 {
766     return ctx->data;
767 }
768
769 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
770 {
771     return ctx->pkey;
772 }
773
774 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
775 {
776     return ctx->peerkey;
777 }
778
779 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
780 {
781     ctx->app_data = data;
782 }
783
784 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
785 {
786     return ctx->app_data;
787 }
788
789 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
790                             int (*init) (EVP_PKEY_CTX *ctx))
791 {
792     pmeth->init = init;
793 }
794
795 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
796                             int (*copy) (EVP_PKEY_CTX *dst,
797                                          const EVP_PKEY_CTX *src))
798 {
799     pmeth->copy = copy;
800 }
801
802 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
803                                void (*cleanup) (EVP_PKEY_CTX *ctx))
804 {
805     pmeth->cleanup = cleanup;
806 }
807
808 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
809                                 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
810                                 int (*paramgen) (EVP_PKEY_CTX *ctx,
811                                                  EVP_PKEY *pkey))
812 {
813     pmeth->paramgen_init = paramgen_init;
814     pmeth->paramgen = paramgen;
815 }
816
817 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
818                               int (*keygen_init) (EVP_PKEY_CTX *ctx),
819                               int (*keygen) (EVP_PKEY_CTX *ctx,
820                                              EVP_PKEY *pkey))
821 {
822     pmeth->keygen_init = keygen_init;
823     pmeth->keygen = keygen;
824 }
825
826 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
827                             int (*sign_init) (EVP_PKEY_CTX *ctx),
828                             int (*sign) (EVP_PKEY_CTX *ctx,
829                                          unsigned char *sig, size_t *siglen,
830                                          const unsigned char *tbs,
831                                          size_t tbslen))
832 {
833     pmeth->sign_init = sign_init;
834     pmeth->sign = sign;
835 }
836
837 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
838                               int (*verify_init) (EVP_PKEY_CTX *ctx),
839                               int (*verify) (EVP_PKEY_CTX *ctx,
840                                              const unsigned char *sig,
841                                              size_t siglen,
842                                              const unsigned char *tbs,
843                                              size_t tbslen))
844 {
845     pmeth->verify_init = verify_init;
846     pmeth->verify = verify;
847 }
848
849 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
850                                       int (*verify_recover_init) (EVP_PKEY_CTX
851                                                                   *ctx),
852                                       int (*verify_recover) (EVP_PKEY_CTX
853                                                              *ctx,
854                                                              unsigned char
855                                                              *sig,
856                                                              size_t *siglen,
857                                                              const unsigned
858                                                              char *tbs,
859                                                              size_t tbslen))
860 {
861     pmeth->verify_recover_init = verify_recover_init;
862     pmeth->verify_recover = verify_recover;
863 }
864
865 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
866                                int (*signctx_init) (EVP_PKEY_CTX *ctx,
867                                                     EVP_MD_CTX *mctx),
868                                int (*signctx) (EVP_PKEY_CTX *ctx,
869                                                unsigned char *sig,
870                                                size_t *siglen,
871                                                EVP_MD_CTX *mctx))
872 {
873     pmeth->signctx_init = signctx_init;
874     pmeth->signctx = signctx;
875 }
876
877 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
878                                  int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
879                                                         EVP_MD_CTX *mctx),
880                                  int (*verifyctx) (EVP_PKEY_CTX *ctx,
881                                                    const unsigned char *sig,
882                                                    int siglen,
883                                                    EVP_MD_CTX *mctx))
884 {
885     pmeth->verifyctx_init = verifyctx_init;
886     pmeth->verifyctx = verifyctx;
887 }
888
889 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
890                                int (*encrypt_init) (EVP_PKEY_CTX *ctx),
891                                int (*encryptfn) (EVP_PKEY_CTX *ctx,
892                                                  unsigned char *out,
893                                                  size_t *outlen,
894                                                  const unsigned char *in,
895                                                  size_t inlen))
896 {
897     pmeth->encrypt_init = encrypt_init;
898     pmeth->encrypt = encryptfn;
899 }
900
901 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
902                                int (*decrypt_init) (EVP_PKEY_CTX *ctx),
903                                int (*decrypt) (EVP_PKEY_CTX *ctx,
904                                                unsigned char *out,
905                                                size_t *outlen,
906                                                const unsigned char *in,
907                                                size_t inlen))
908 {
909     pmeth->decrypt_init = decrypt_init;
910     pmeth->decrypt = decrypt;
911 }
912
913 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
914                               int (*derive_init) (EVP_PKEY_CTX *ctx),
915                               int (*derive) (EVP_PKEY_CTX *ctx,
916                                              unsigned char *key,
917                                              size_t *keylen))
918 {
919     pmeth->derive_init = derive_init;
920     pmeth->derive = derive;
921 }
922
923 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
924                             int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
925                                          void *p2),
926                             int (*ctrl_str) (EVP_PKEY_CTX *ctx,
927                                              const char *type,
928                                              const char *value))
929 {
930     pmeth->ctrl = ctrl;
931     pmeth->ctrl_str = ctrl_str;
932 }
933
934 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
935                              int (*check) (EVP_PKEY *pkey))
936 {
937     pmeth->check = check;
938 }
939
940 void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
941                                     int (*check) (EVP_PKEY *pkey))
942 {
943     pmeth->public_check = check;
944 }
945
946 void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
947                                    int (*check) (EVP_PKEY *pkey))
948 {
949     pmeth->param_check = check;
950 }
951
952 void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
953                                      int (*digest_custom) (EVP_PKEY_CTX *ctx,
954                                                            EVP_MD_CTX *mctx))
955 {
956     pmeth->digest_custom = digest_custom;
957 }
958
959 void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
960                             int (**pinit) (EVP_PKEY_CTX *ctx))
961 {
962     *pinit = pmeth->init;
963 }
964
965 void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
966                             int (**pcopy) (EVP_PKEY_CTX *dst,
967                                            const EVP_PKEY_CTX *src))
968 {
969     *pcopy = pmeth->copy;
970 }
971
972 void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
973                                void (**pcleanup) (EVP_PKEY_CTX *ctx))
974 {
975     *pcleanup = pmeth->cleanup;
976 }
977
978 void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
979                                 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
980                                 int (**pparamgen) (EVP_PKEY_CTX *ctx,
981                                                    EVP_PKEY *pkey))
982 {
983     if (pparamgen_init)
984         *pparamgen_init = pmeth->paramgen_init;
985     if (pparamgen)
986         *pparamgen = pmeth->paramgen;
987 }
988
989 void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
990                               int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
991                               int (**pkeygen) (EVP_PKEY_CTX *ctx,
992                                                EVP_PKEY *pkey))
993 {
994     if (pkeygen_init)
995         *pkeygen_init = pmeth->keygen_init;
996     if (pkeygen)
997         *pkeygen = pmeth->keygen;
998 }
999
1000 void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
1001                             int (**psign_init) (EVP_PKEY_CTX *ctx),
1002                             int (**psign) (EVP_PKEY_CTX *ctx,
1003                                            unsigned char *sig, size_t *siglen,
1004                                            const unsigned char *tbs,
1005                                            size_t tbslen))
1006 {
1007     if (psign_init)
1008         *psign_init = pmeth->sign_init;
1009     if (psign)
1010         *psign = pmeth->sign;
1011 }
1012
1013 void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
1014                               int (**pverify_init) (EVP_PKEY_CTX *ctx),
1015                               int (**pverify) (EVP_PKEY_CTX *ctx,
1016                                                const unsigned char *sig,
1017                                                size_t siglen,
1018                                                const unsigned char *tbs,
1019                                                size_t tbslen))
1020 {
1021     if (pverify_init)
1022         *pverify_init = pmeth->verify_init;
1023     if (pverify)
1024         *pverify = pmeth->verify;
1025 }
1026
1027 void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
1028                                       int (**pverify_recover_init) (EVP_PKEY_CTX
1029                                                                     *ctx),
1030                                       int (**pverify_recover) (EVP_PKEY_CTX
1031                                                                *ctx,
1032                                                                unsigned char
1033                                                                *sig,
1034                                                                size_t *siglen,
1035                                                                const unsigned
1036                                                                char *tbs,
1037                                                                size_t tbslen))
1038 {
1039     if (pverify_recover_init)
1040         *pverify_recover_init = pmeth->verify_recover_init;
1041     if (pverify_recover)
1042         *pverify_recover = pmeth->verify_recover;
1043 }
1044
1045 void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
1046                                int (**psignctx_init) (EVP_PKEY_CTX *ctx,
1047                                                       EVP_MD_CTX *mctx),
1048                                int (**psignctx) (EVP_PKEY_CTX *ctx,
1049                                                  unsigned char *sig,
1050                                                  size_t *siglen,
1051                                                  EVP_MD_CTX *mctx))
1052 {
1053     if (psignctx_init)
1054         *psignctx_init = pmeth->signctx_init;
1055     if (psignctx)
1056         *psignctx = pmeth->signctx;
1057 }
1058
1059 void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
1060                                  int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
1061                                                           EVP_MD_CTX *mctx),
1062                                  int (**pverifyctx) (EVP_PKEY_CTX *ctx,
1063                                                      const unsigned char *sig,
1064                                                      int siglen,
1065                                                      EVP_MD_CTX *mctx))
1066 {
1067     if (pverifyctx_init)
1068         *pverifyctx_init = pmeth->verifyctx_init;
1069     if (pverifyctx)
1070         *pverifyctx = pmeth->verifyctx;
1071 }
1072
1073 void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
1074                                int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
1075                                int (**pencryptfn) (EVP_PKEY_CTX *ctx,
1076                                                    unsigned char *out,
1077                                                    size_t *outlen,
1078                                                    const unsigned char *in,
1079                                                    size_t inlen))
1080 {
1081     if (pencrypt_init)
1082         *pencrypt_init = pmeth->encrypt_init;
1083     if (pencryptfn)
1084         *pencryptfn = pmeth->encrypt;
1085 }
1086
1087 void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
1088                                int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
1089                                int (**pdecrypt) (EVP_PKEY_CTX *ctx,
1090                                                  unsigned char *out,
1091                                                  size_t *outlen,
1092                                                  const unsigned char *in,
1093                                                  size_t inlen))
1094 {
1095     if (pdecrypt_init)
1096         *pdecrypt_init = pmeth->decrypt_init;
1097     if (pdecrypt)
1098         *pdecrypt = pmeth->decrypt;
1099 }
1100
1101 void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
1102                               int (**pderive_init) (EVP_PKEY_CTX *ctx),
1103                               int (**pderive) (EVP_PKEY_CTX *ctx,
1104                                                unsigned char *key,
1105                                                size_t *keylen))
1106 {
1107     if (pderive_init)
1108         *pderive_init = pmeth->derive_init;
1109     if (pderive)
1110         *pderive = pmeth->derive;
1111 }
1112
1113 void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
1114                             int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1115                                            void *p2),
1116                             int (**pctrl_str) (EVP_PKEY_CTX *ctx,
1117                                                const char *type,
1118                                                const char *value))
1119 {
1120     if (pctrl)
1121         *pctrl = pmeth->ctrl;
1122     if (pctrl_str)
1123         *pctrl_str = pmeth->ctrl_str;
1124 }
1125
1126 void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
1127                              int (**pcheck) (EVP_PKEY *pkey))
1128 {
1129     if (pcheck != NULL)
1130         *pcheck = pmeth->check;
1131 }
1132
1133 void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
1134                                     int (**pcheck) (EVP_PKEY *pkey))
1135 {
1136     if (pcheck != NULL)
1137         *pcheck = pmeth->public_check;
1138 }
1139
1140 void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
1141                                    int (**pcheck) (EVP_PKEY *pkey))
1142 {
1143     if (pcheck != NULL)
1144         *pcheck = pmeth->param_check;
1145 }
1146
1147 void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD *pmeth,
1148                                      int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
1149                                                              EVP_MD_CTX *mctx))
1150 {
1151     if (pdigest_custom != NULL)
1152         *pdigest_custom = pmeth->digest_custom;
1153 }