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