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