Final cleanup after move to leaner EVP_PKEY methods
[openssl.git] / crypto / evp / pmeth_fn.c
1 /*
2  * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <openssl/objects.h>
13 #include <openssl/evp.h>
14 #include "internal/cryptlib.h"
15 #include "crypto/evp.h"
16 #include "internal/provider.h"
17 #include "evp_local.h"
18
19 static EVP_SIGNATURE *evp_signature_new(OSSL_PROVIDER *prov)
20 {
21     EVP_SIGNATURE *signature = OPENSSL_zalloc(sizeof(EVP_SIGNATURE));
22
23     signature->lock = CRYPTO_THREAD_lock_new();
24     if (signature->lock == NULL) {
25         OPENSSL_free(signature);
26         return NULL;
27     }
28     signature->prov = prov;
29     ossl_provider_up_ref(prov);
30     signature->refcnt = 1;
31
32     return signature;
33 }
34
35 static void *evp_signature_from_dispatch(int name_id,
36                                          const OSSL_DISPATCH *fns,
37                                          OSSL_PROVIDER *prov)
38 {
39     EVP_SIGNATURE *signature = NULL;
40     int ctxfncnt = 0, signfncnt = 0, verifyfncnt = 0, verifyrecfncnt = 0;
41     int digsignfncnt = 0, digverifyfncnt = 0;
42     int gparamfncnt = 0, sparamfncnt = 0, gmdparamfncnt = 0, smdparamfncnt = 0;
43
44     if ((signature = evp_signature_new(prov)) == NULL) {
45         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
46         goto err;
47     }
48
49     signature->name_id = name_id;
50
51     for (; fns->function_id != 0; fns++) {
52         switch (fns->function_id) {
53         case OSSL_FUNC_SIGNATURE_NEWCTX:
54             if (signature->newctx != NULL)
55                 break;
56             signature->newctx = OSSL_get_OP_signature_newctx(fns);
57             ctxfncnt++;
58             break;
59         case OSSL_FUNC_SIGNATURE_SIGN_INIT:
60             if (signature->sign_init != NULL)
61                 break;
62             signature->sign_init = OSSL_get_OP_signature_sign_init(fns);
63             signfncnt++;
64             break;
65         case OSSL_FUNC_SIGNATURE_SIGN:
66             if (signature->sign != NULL)
67                 break;
68             signature->sign = OSSL_get_OP_signature_sign(fns);
69             signfncnt++;
70             break;
71         case OSSL_FUNC_SIGNATURE_VERIFY_INIT:
72             if (signature->verify_init != NULL)
73                 break;
74             signature->verify_init = OSSL_get_OP_signature_verify_init(fns);
75             verifyfncnt++;
76             break;
77         case OSSL_FUNC_SIGNATURE_VERIFY:
78             if (signature->verify != NULL)
79                 break;
80             signature->verify = OSSL_get_OP_signature_verify(fns);
81             verifyfncnt++;
82             break;
83         case OSSL_FUNC_SIGNATURE_VERIFY_RECOVER_INIT:
84             if (signature->verify_recover_init != NULL)
85                 break;
86             signature->verify_recover_init
87                 = OSSL_get_OP_signature_verify_recover_init(fns);
88             verifyrecfncnt++;
89             break;
90         case OSSL_FUNC_SIGNATURE_VERIFY_RECOVER:
91             if (signature->verify_recover != NULL)
92                 break;
93             signature->verify_recover
94                 = OSSL_get_OP_signature_verify_recover(fns);
95             verifyrecfncnt++;
96             break;
97         case OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT:
98             if (signature->digest_sign_init != NULL)
99                 break;
100             signature->digest_sign_init
101                 = OSSL_get_OP_signature_digest_sign_init(fns);
102             digsignfncnt++;
103             break;
104         case OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE:
105             if (signature->digest_sign_update != NULL)
106                 break;
107             signature->digest_sign_update
108                 = OSSL_get_OP_signature_digest_sign_update(fns);
109             digsignfncnt++;
110             break;
111         case OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL:
112             if (signature->digest_sign_final != NULL)
113                 break;
114             signature->digest_sign_final
115                 = OSSL_get_OP_signature_digest_sign_final(fns);
116             digsignfncnt++;
117             break;
118         case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT:
119             if (signature->digest_verify_init != NULL)
120                 break;
121             signature->digest_verify_init
122                 = OSSL_get_OP_signature_digest_verify_init(fns);
123             digverifyfncnt++;
124             break;
125         case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE:
126             if (signature->digest_verify_update != NULL)
127                 break;
128             signature->digest_verify_update
129                 = OSSL_get_OP_signature_digest_verify_update(fns);
130             digverifyfncnt++;
131             break;
132         case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL:
133             if (signature->digest_verify_final != NULL)
134                 break;
135             signature->digest_verify_final
136                 = OSSL_get_OP_signature_digest_verify_final(fns);
137             digverifyfncnt++;
138             break;
139         case OSSL_FUNC_SIGNATURE_FREECTX:
140             if (signature->freectx != NULL)
141                 break;
142             signature->freectx = OSSL_get_OP_signature_freectx(fns);
143             ctxfncnt++;
144             break;
145         case OSSL_FUNC_SIGNATURE_DUPCTX:
146             if (signature->dupctx != NULL)
147                 break;
148             signature->dupctx = OSSL_get_OP_signature_dupctx(fns);
149             break;
150         case OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS:
151             if (signature->get_ctx_params != NULL)
152                 break;
153             signature->get_ctx_params
154                 = OSSL_get_OP_signature_get_ctx_params(fns);
155             gparamfncnt++;
156             break;
157         case OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS:
158             if (signature->gettable_ctx_params != NULL)
159                 break;
160             signature->gettable_ctx_params
161                 = OSSL_get_OP_signature_gettable_ctx_params(fns);
162             gparamfncnt++;
163             break;
164         case OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS:
165             if (signature->set_ctx_params != NULL)
166                 break;
167             signature->set_ctx_params
168                 = OSSL_get_OP_signature_set_ctx_params(fns);
169             sparamfncnt++;
170             break;
171         case OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS:
172             if (signature->settable_ctx_params != NULL)
173                 break;
174             signature->settable_ctx_params
175                 = OSSL_get_OP_signature_settable_ctx_params(fns);
176             sparamfncnt++;
177             break;
178         case OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS:
179             if (signature->get_ctx_md_params != NULL)
180                 break;
181             signature->get_ctx_md_params
182                 = OSSL_get_OP_signature_get_ctx_md_params(fns);
183             gmdparamfncnt++;
184             break;
185         case OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS:
186             if (signature->gettable_ctx_md_params != NULL)
187                 break;
188             signature->gettable_ctx_md_params
189                 = OSSL_get_OP_signature_gettable_ctx_md_params(fns);
190             gmdparamfncnt++;
191             break;
192         case OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS:
193             if (signature->set_ctx_md_params != NULL)
194                 break;
195             signature->set_ctx_md_params
196                 = OSSL_get_OP_signature_set_ctx_md_params(fns);
197             smdparamfncnt++;
198             break;
199         case OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS:
200             if (signature->settable_ctx_md_params != NULL)
201                 break;
202             signature->settable_ctx_md_params
203                 = OSSL_get_OP_signature_settable_ctx_md_params(fns);
204             smdparamfncnt++;
205             break;
206         }
207     }
208     if (ctxfncnt != 2
209         || (signfncnt == 0
210             && verifyfncnt == 0
211             && verifyrecfncnt == 0
212             && digsignfncnt == 0
213             && digverifyfncnt == 0)
214         || (signfncnt != 0 && signfncnt != 2)
215         || (verifyfncnt != 0 && verifyfncnt != 2)
216         || (verifyrecfncnt != 0 && verifyrecfncnt != 2)
217         || (digsignfncnt != 0 && digsignfncnt != 3)
218         || (digverifyfncnt != 0 && digverifyfncnt != 3)
219         || (gparamfncnt != 0 && gparamfncnt != 2)
220         || (sparamfncnt != 0 && sparamfncnt != 2)
221         || (gmdparamfncnt != 0 && gmdparamfncnt != 2)
222         || (smdparamfncnt != 0 && smdparamfncnt != 2)) {
223         /*
224          * In order to be a consistent set of functions we must have at least
225          * a set of context functions (newctx and freectx) as well as a set of
226          * "signature" functions:
227          *  (sign_init, sign) or
228          *  (verify_init verify) or
229          *  (verify_recover_init, verify_recover) or
230          *  (digest_sign_init, digest_sign_update, digest_sign_final) or
231          *  (digest_verify_init, digest_verify_update, digest_verify_final).
232          *
233          * set_ctx_params and settable_ctx_params are optional, but if one of
234          * them is present then the other one must also be present. The same
235          * applies to get_ctx_params and gettable_ctx_params. The same rules
236          * apply to the "md_params" functions. The dupctx function is optional.
237          */
238         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
239         goto err;
240     }
241
242     return signature;
243  err:
244     EVP_SIGNATURE_free(signature);
245     return NULL;
246 }
247
248 void EVP_SIGNATURE_free(EVP_SIGNATURE *signature)
249 {
250     if (signature != NULL) {
251         int i;
252
253         CRYPTO_DOWN_REF(&signature->refcnt, &i, signature->lock);
254         if (i > 0)
255             return;
256         ossl_provider_free(signature->prov);
257         CRYPTO_THREAD_lock_free(signature->lock);
258         OPENSSL_free(signature);
259     }
260 }
261
262 int EVP_SIGNATURE_up_ref(EVP_SIGNATURE *signature)
263 {
264     int ref = 0;
265
266     CRYPTO_UP_REF(&signature->refcnt, &ref, signature->lock);
267     return 1;
268 }
269
270 OSSL_PROVIDER *EVP_SIGNATURE_provider(const EVP_SIGNATURE *signature)
271 {
272     return signature->prov;
273 }
274
275 EVP_SIGNATURE *EVP_SIGNATURE_fetch(OPENSSL_CTX *ctx, const char *algorithm,
276                                    const char *properties)
277 {
278     return evp_generic_fetch(ctx, OSSL_OP_SIGNATURE, algorithm, properties,
279                              evp_signature_from_dispatch,
280                              (int (*)(void *))EVP_SIGNATURE_up_ref,
281                              (void (*)(void *))EVP_SIGNATURE_free);
282 }
283
284 int EVP_SIGNATURE_is_a(const EVP_SIGNATURE *signature, const char *name)
285 {
286     return evp_is_a(signature->prov, signature->name_id, name);
287 }
288
289 int EVP_SIGNATURE_number(const EVP_SIGNATURE *signature)
290 {
291     return signature->name_id;
292 }
293
294 void EVP_SIGNATURE_do_all_provided(OPENSSL_CTX *libctx,
295                                    void (*fn)(EVP_SIGNATURE *signature,
296                                               void *arg),
297                                    void *arg)
298 {
299     evp_generic_do_all(libctx, OSSL_OP_SIGNATURE,
300                        (void (*)(void *, void *))fn, arg,
301                        evp_signature_from_dispatch,
302                        (void (*)(void *))EVP_SIGNATURE_free);
303 }
304
305
306 void EVP_SIGNATURE_names_do_all(const EVP_SIGNATURE *signature,
307                                 void (*fn)(const char *name, void *data),
308                                 void *data)
309 {
310     if (signature->prov != NULL)
311         evp_names_do_all(signature->prov, signature->name_id, fn, data);
312 }
313
314 static int evp_pkey_signature_init(EVP_PKEY_CTX *ctx, int operation)
315 {
316     int ret = 0;
317     void *provkey = NULL;
318     EVP_SIGNATURE *signature = NULL;
319
320     if (ctx == NULL) {
321         EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
322         return -2;
323     }
324
325     evp_pkey_ctx_free_old_ops(ctx);
326     ctx->operation = operation;
327
328     if (ctx->algorithm == NULL)
329         goto legacy;
330
331     /*
332      * Because we cleared out old ops, we shouldn't need to worry about
333      * checking if signature is already there.  Keymgmt is a different
334      * matter, as it isn't tied to a specific EVP_PKEY op.
335      */
336     signature = EVP_SIGNATURE_fetch(ctx->libctx, ctx->algorithm,
337                                     ctx->propquery);
338     if (signature != NULL && ctx->keymgmt == NULL) {
339         int name_id = EVP_SIGNATURE_number(signature);
340
341         ctx->keymgmt = evp_keymgmt_fetch_by_number(ctx->libctx, name_id,
342                                                    ctx->propquery);
343     }
344
345     if (ctx->keymgmt == NULL
346         || signature == NULL
347         || (EVP_KEYMGMT_provider(ctx->keymgmt)
348             != EVP_SIGNATURE_provider(signature))) {
349         /*
350          * We don't have the full support we need with provided methods,
351          * let's go see if legacy does.  Also, we don't need to free
352          * ctx->keymgmt here, as it's not necessarily tied to this
353          * operation.  It will be freed by EVP_PKEY_CTX_free().
354          */
355         EVP_SIGNATURE_free(signature);
356         goto legacy;
357     }
358
359     ctx->op.sig.signature = signature;
360
361     if (ctx->pkey != NULL) {
362         provkey =
363             evp_keymgmt_export_to_provider(ctx->pkey, ctx->keymgmt, 0);
364         if (provkey == NULL) {
365             EVPerr(0, EVP_R_INITIALIZATION_ERROR);
366             goto err;
367         }
368     }
369     ctx->op.sig.sigprovctx = signature->newctx(ossl_provider_ctx(signature->prov));
370     if (ctx->op.sig.sigprovctx == NULL) {
371         /* The provider key can stay in the cache */
372         EVPerr(0, EVP_R_INITIALIZATION_ERROR);
373         goto err;
374     }
375
376     switch (operation) {
377     case EVP_PKEY_OP_SIGN:
378         if (signature->sign_init == NULL) {
379             EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
380             ret = -2;
381             goto err;
382         }
383         ret = signature->sign_init(ctx->op.sig.sigprovctx, provkey);
384         break;
385     case EVP_PKEY_OP_VERIFY:
386         if (signature->verify_init == NULL) {
387             EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
388             ret = -2;
389             goto err;
390         }
391         ret = signature->verify_init(ctx->op.sig.sigprovctx, provkey);
392         break;
393     case EVP_PKEY_OP_VERIFYRECOVER:
394         if (signature->verify_recover_init == NULL) {
395             EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
396             ret = -2;
397             goto err;
398         }
399         ret = signature->verify_recover_init(ctx->op.sig.sigprovctx, provkey);
400         break;
401     default:
402         EVPerr(0, EVP_R_INITIALIZATION_ERROR);
403         goto err;
404     }
405
406     if (ret <= 0) {
407         signature->freectx(ctx->op.sig.sigprovctx);
408         ctx->op.sig.sigprovctx = NULL;
409         goto err;
410     }
411     return 1;
412
413  legacy:
414     if (ctx->pmeth == NULL
415             || (operation == EVP_PKEY_OP_SIGN && ctx->pmeth->sign == NULL)
416             || (operation == EVP_PKEY_OP_VERIFY && ctx->pmeth->verify == NULL)
417             || (operation == EVP_PKEY_OP_VERIFYRECOVER
418                 && ctx->pmeth->verify_recover == NULL)) {
419         EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
420         return -2;
421     }
422
423     switch (operation) {
424     case EVP_PKEY_OP_SIGN:
425         if (ctx->pmeth->sign_init == NULL)
426             return 1;
427         ret = ctx->pmeth->sign_init(ctx);
428         break;
429     case EVP_PKEY_OP_VERIFY:
430         if (ctx->pmeth->verify_init == NULL)
431             return 1;
432         ret = ctx->pmeth->verify_init(ctx);
433         break;
434     case EVP_PKEY_OP_VERIFYRECOVER:
435         if (ctx->pmeth->verify_recover_init == NULL)
436             return 1;
437         ret = ctx->pmeth->verify_recover_init(ctx);
438         break;
439     default:
440         EVPerr(0, EVP_R_INITIALIZATION_ERROR);
441         goto err;
442     }
443     if (ret <= 0)
444         goto err;
445     return ret;
446
447  err:
448     ctx->operation = EVP_PKEY_OP_UNDEFINED;
449     return ret;
450 }
451
452 int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx)
453 {
454     return evp_pkey_signature_init(ctx, EVP_PKEY_OP_SIGN);
455 }
456
457 int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
458                   unsigned char *sig, size_t *siglen,
459                   const unsigned char *tbs, size_t tbslen)
460 {
461     int ret;
462
463     if (ctx == NULL) {
464         EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
465         return -2;
466     }
467
468     if (ctx->operation != EVP_PKEY_OP_SIGN) {
469         EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED);
470         return -1;
471     }
472
473     if (ctx->op.sig.sigprovctx == NULL)
474         goto legacy;
475
476     ret = ctx->op.sig.signature->sign(ctx->op.sig.sigprovctx, sig, siglen,
477                                       SIZE_MAX, tbs, tbslen);
478
479     return ret;
480  legacy:
481  
482     if (ctx->pmeth == NULL || ctx->pmeth->sign == NULL) {
483         EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
484         return -2;
485     }
486
487     M_check_autoarg(ctx, sig, siglen, EVP_F_EVP_PKEY_SIGN)
488         return ctx->pmeth->sign(ctx, sig, siglen, tbs, tbslen);
489 }
490
491 int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx)
492 {
493     return evp_pkey_signature_init(ctx, EVP_PKEY_OP_VERIFY);
494 }
495
496 int EVP_PKEY_verify(EVP_PKEY_CTX *ctx,
497                     const unsigned char *sig, size_t siglen,
498                     const unsigned char *tbs, size_t tbslen)
499 {
500     int ret;
501
502     if (ctx == NULL) {
503         EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
504         return -2;
505     }
506
507     if (ctx->operation != EVP_PKEY_OP_VERIFY) {
508         EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED);
509         return -1;
510     }
511
512     if (ctx->op.sig.sigprovctx == NULL)
513         goto legacy;
514
515     ret = ctx->op.sig.signature->verify(ctx->op.sig.sigprovctx, sig, siglen,
516                                         tbs, tbslen);
517
518     return ret;
519  legacy:
520     if (ctx->pmeth == NULL || ctx->pmeth->verify == NULL) {
521         EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
522         return -2;
523     }
524
525     return ctx->pmeth->verify(ctx, sig, siglen, tbs, tbslen);
526 }
527
528 int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx)
529 {
530     return evp_pkey_signature_init(ctx, EVP_PKEY_OP_VERIFYRECOVER);
531 }
532
533 int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx,
534                             unsigned char *rout, size_t *routlen,
535                             const unsigned char *sig, size_t siglen)
536 {
537     int ret;
538
539     if (ctx == NULL) {
540         EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
541         return -2;
542     }
543
544     if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER) {
545         EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED);
546         return -1;
547     }
548
549     if (ctx->op.sig.sigprovctx == NULL)
550         goto legacy;
551
552     ret = ctx->op.sig.signature->verify_recover(ctx->op.sig.sigprovctx, rout,
553                                                 routlen,
554                                                 (rout == NULL ? 0 : *routlen),
555                                                 sig, siglen);
556     return ret;
557  legacy:
558     if (ctx->pmeth == NULL || ctx->pmeth->verify_recover == NULL) {
559         EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
560         return -2;
561     }
562     M_check_autoarg(ctx, rout, routlen, EVP_F_EVP_PKEY_VERIFY_RECOVER)
563         return ctx->pmeth->verify_recover(ctx, rout, routlen, sig, siglen);
564 }
565
566 int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx)
567 {
568     int ret;
569     if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
570         EVPerr(EVP_F_EVP_PKEY_ENCRYPT_INIT,
571                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
572         return -2;
573     }
574     ctx->operation = EVP_PKEY_OP_ENCRYPT;
575     if (!ctx->pmeth->encrypt_init)
576         return 1;
577     ret = ctx->pmeth->encrypt_init(ctx);
578     if (ret <= 0)
579         ctx->operation = EVP_PKEY_OP_UNDEFINED;
580     return ret;
581 }
582
583 int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx,
584                      unsigned char *out, size_t *outlen,
585                      const unsigned char *in, size_t inlen)
586 {
587     if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
588         EVPerr(EVP_F_EVP_PKEY_ENCRYPT,
589                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
590         return -2;
591     }
592     if (ctx->operation != EVP_PKEY_OP_ENCRYPT) {
593         EVPerr(EVP_F_EVP_PKEY_ENCRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
594         return -1;
595     }
596     M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_ENCRYPT)
597         return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
598 }
599
600 int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx)
601 {
602     int ret;
603     if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
604         EVPerr(EVP_F_EVP_PKEY_DECRYPT_INIT,
605                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
606         return -2;
607     }
608     ctx->operation = EVP_PKEY_OP_DECRYPT;
609     if (!ctx->pmeth->decrypt_init)
610         return 1;
611     ret = ctx->pmeth->decrypt_init(ctx);
612     if (ret <= 0)
613         ctx->operation = EVP_PKEY_OP_UNDEFINED;
614     return ret;
615 }
616
617 int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx,
618                      unsigned char *out, size_t *outlen,
619                      const unsigned char *in, size_t inlen)
620 {
621     if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
622         EVPerr(EVP_F_EVP_PKEY_DECRYPT,
623                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
624         return -2;
625     }
626     if (ctx->operation != EVP_PKEY_OP_DECRYPT) {
627         EVPerr(EVP_F_EVP_PKEY_DECRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
628         return -1;
629     }
630     M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_DECRYPT)
631         return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);
632 }