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