Add control to retrieve signature MD.
[openssl.git] / crypto / rsa / rsa_pmeth.c
1 /* crypto/rsa/rsa_pmeth.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project 2006.
4  */
5 /* ====================================================================
6  * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58
59 #include <stdio.h>
60 #include "cryptlib.h"
61 #include <openssl/asn1t.h>
62 #include <openssl/x509.h>
63 #include <openssl/rsa.h>
64 #include <openssl/bn.h>
65 #include <openssl/evp.h>
66 #ifndef OPENSSL_NO_CMS
67 #include <openssl/cms.h>
68 #endif
69 #ifdef OPENSSL_FIPS
70 #include <openssl/fips.h>
71 #endif
72 #include "evp_locl.h"
73 #include "rsa_locl.h"
74
75 /* RSA pkey context structure */
76
77 typedef struct
78         {
79         /* Key gen parameters */
80         int nbits;
81         BIGNUM *pub_exp;
82         /* Keygen callback info */
83         int gentmp[2];
84         /* RSA padding mode */
85         int pad_mode;
86         /* message digest */
87         const EVP_MD *md;
88         /* message digest for MGF1 */
89         const EVP_MD *mgf1md;
90         /* PSS/OAEP salt length */
91         int saltlen;
92         /* Temp buffer */
93         unsigned char *tbuf;
94         } RSA_PKEY_CTX;
95
96 static int pkey_rsa_init(EVP_PKEY_CTX *ctx)
97         {
98         RSA_PKEY_CTX *rctx;
99         rctx = OPENSSL_malloc(sizeof(RSA_PKEY_CTX));
100         if (!rctx)
101                 return 0;
102         rctx->nbits = 1024;
103         rctx->pub_exp = NULL;
104         rctx->pad_mode = RSA_PKCS1_PADDING;
105         rctx->md = NULL;
106         rctx->mgf1md = NULL;
107         rctx->tbuf = NULL;
108
109         rctx->saltlen = -2;
110
111         ctx->data = rctx;
112         ctx->keygen_info = rctx->gentmp;
113         ctx->keygen_info_count = 2;
114         
115         return 1;
116         }
117
118 static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
119         {
120         RSA_PKEY_CTX *dctx, *sctx;
121         if (!pkey_rsa_init(dst))
122                 return 0;
123         sctx = src->data;
124         dctx = dst->data;
125         dctx->nbits = sctx->nbits;
126         if (sctx->pub_exp)
127                 {
128                 dctx->pub_exp = BN_dup(sctx->pub_exp);
129                 if (!dctx->pub_exp)
130                         return 0;
131                 }
132         dctx->pad_mode = sctx->pad_mode;
133         dctx->md = sctx->md;
134         return 1;
135         }
136
137 static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk)
138         {
139         if (ctx->tbuf)
140                 return 1;
141         ctx->tbuf = OPENSSL_malloc(EVP_PKEY_size(pk->pkey));
142         if (!ctx->tbuf)
143                 return 0;
144         return 1;
145         }
146
147 static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx)
148         {
149         RSA_PKEY_CTX *rctx = ctx->data;
150         if (rctx)
151                 {
152                 if (rctx->pub_exp)
153                         BN_free(rctx->pub_exp);
154                 if (rctx->tbuf)
155                         OPENSSL_free(rctx->tbuf);
156                 OPENSSL_free(rctx);
157                 }
158         }
159 #ifdef OPENSSL_FIPS
160 /* FIP checker. Return value indicates status of context parameters:
161  * 1  : redirect to FIPS.
162  * 0  : don't redirect to FIPS.
163  * -1 : illegal operation in FIPS mode.
164  */
165
166 static int pkey_fips_check_ctx(EVP_PKEY_CTX *ctx)
167         {
168         RSA_PKEY_CTX *rctx = ctx->data;
169         RSA *rsa = ctx->pkey->pkey.rsa;
170         int rv = -1;
171         if (!FIPS_mode())
172                 return 0;
173         if (rsa->flags & RSA_FLAG_NON_FIPS_ALLOW)
174                 rv = 0;
175         if (!(rsa->meth->flags & RSA_FLAG_FIPS_METHOD) && rv)
176                 return -1;
177         if (rctx->md)
178                 {
179                 const EVP_MD *fmd;
180                 fmd = FIPS_get_digestbynid(EVP_MD_type(rctx->md));
181                 if (!fmd || !(fmd->flags & EVP_MD_FLAG_FIPS))
182                         return rv;
183                 }
184         if (rctx->mgf1md && !(rctx->mgf1md->flags & EVP_MD_FLAG_FIPS))
185                 {
186                 const EVP_MD *fmd;
187                 fmd = FIPS_get_digestbynid(EVP_MD_type(rctx->mgf1md));
188                 if (!fmd || !(fmd->flags & EVP_MD_FLAG_FIPS))
189                         return rv;
190                 }
191         return 1;
192         }
193 #endif
194
195 static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
196                                         const unsigned char *tbs, size_t tbslen)
197         {
198         int ret;
199         RSA_PKEY_CTX *rctx = ctx->data;
200         RSA *rsa = ctx->pkey->pkey.rsa;
201
202 #ifdef OPENSSL_FIPS
203         ret = pkey_fips_check_ctx(ctx);
204         if (ret < 0)
205                 {
206                 RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE);
207                 return -1;
208                 }
209 #endif
210
211         if (rctx->md)
212                 {
213                 if (tbslen != (size_t)EVP_MD_size(rctx->md))
214                         {
215                         RSAerr(RSA_F_PKEY_RSA_SIGN,
216                                         RSA_R_INVALID_DIGEST_LENGTH);
217                         return -1;
218                         }
219 #ifdef OPENSSL_FIPS
220                 if (ret > 0)
221                         {
222                         unsigned int slen;
223                         ret = FIPS_rsa_sign_digest(rsa, tbs, tbslen, rctx->md,
224                                                         rctx->pad_mode,
225                                                         rctx->saltlen,
226                                                         rctx->mgf1md,
227                                                         sig, &slen);
228                         if (ret > 0)
229                                 *siglen = slen;
230                         else
231                                 *siglen = 0;
232                         return ret;
233                         }
234 #endif
235
236                 if (EVP_MD_type(rctx->md) == NID_mdc2)
237                         {
238                         unsigned int sltmp;
239                         if (rctx->pad_mode != RSA_PKCS1_PADDING)
240                                 return -1;
241                         ret = RSA_sign_ASN1_OCTET_STRING(NID_mdc2,
242                                                 tbs, tbslen, sig, &sltmp, rsa);
243
244                         if (ret <= 0)
245                                 return ret;
246                         ret = sltmp;
247                         }
248                 else if (rctx->pad_mode == RSA_X931_PADDING)
249                         {
250                         if (!setup_tbuf(rctx, ctx))
251                                 return -1;
252                         memcpy(rctx->tbuf, tbs, tbslen);
253                         rctx->tbuf[tbslen] =
254                                 RSA_X931_hash_id(EVP_MD_type(rctx->md));
255                         ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf,
256                                                 sig, rsa, RSA_X931_PADDING);
257                         }
258                 else if (rctx->pad_mode == RSA_PKCS1_PADDING)
259                         {
260                         unsigned int sltmp;
261                         ret = RSA_sign(EVP_MD_type(rctx->md),
262                                                 tbs, tbslen, sig, &sltmp, rsa);
263                         if (ret <= 0)
264                                 return ret;
265                         ret = sltmp;
266                         }
267                 else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING)
268                         {
269                         if (!setup_tbuf(rctx, ctx))
270                                 return -1;
271                         if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa,
272                                                 rctx->tbuf, tbs,
273                                                 rctx->md, rctx->mgf1md,
274                                                 rctx->saltlen))
275                                 return -1;
276                         ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
277                                                 sig, rsa, RSA_NO_PADDING);
278                         }
279                 else
280                         return -1;
281                 }
282         else
283                 ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa,
284                                                         rctx->pad_mode);
285         if (ret < 0)
286                 return ret;
287         *siglen = ret;
288         return 1;
289         }
290
291
292 static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
293                                         unsigned char *rout, size_t *routlen,
294                                         const unsigned char *sig, size_t siglen)
295         {
296         int ret;
297         RSA_PKEY_CTX *rctx = ctx->data;
298
299         if (rctx->md)
300                 {
301                 if (rctx->pad_mode == RSA_X931_PADDING)
302                         {
303                         if (!setup_tbuf(rctx, ctx))
304                                 return -1;
305                         ret = RSA_public_decrypt(siglen, sig,
306                                                 rctx->tbuf, ctx->pkey->pkey.rsa,
307                                                 RSA_X931_PADDING);
308                         if (ret < 1)
309                                 return 0;
310                         ret--;
311                         if (rctx->tbuf[ret] !=
312                                 RSA_X931_hash_id(EVP_MD_type(rctx->md)))
313                                 {
314                                 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
315                                                 RSA_R_ALGORITHM_MISMATCH);
316                                 return 0;
317                                 }
318                         if (ret != EVP_MD_size(rctx->md))
319                                 {
320                                 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
321                                         RSA_R_INVALID_DIGEST_LENGTH);
322                                 return 0;
323                                 }
324                         if (rout)
325                                 memcpy(rout, rctx->tbuf, ret);
326                         }
327                 else if (rctx->pad_mode == RSA_PKCS1_PADDING)
328                         {
329                         size_t sltmp;
330                         ret = int_rsa_verify(EVP_MD_type(rctx->md),
331                                                 NULL, 0, rout, &sltmp,
332                                         sig, siglen, ctx->pkey->pkey.rsa);
333                         if (ret <= 0)
334                                 return 0;
335                         ret = sltmp;
336                         }
337                 else
338                         return -1;
339                 }
340         else
341                 ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa,
342                                                         rctx->pad_mode);
343         if (ret < 0)
344                 return ret;
345         *routlen = ret;
346         return 1;
347         }
348
349 static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
350                                         const unsigned char *sig, size_t siglen,
351                                         const unsigned char *tbs, size_t tbslen)
352         {
353         RSA_PKEY_CTX *rctx = ctx->data;
354         RSA *rsa = ctx->pkey->pkey.rsa;
355         size_t rslen;
356 #ifdef OPENSSL_FIPS
357         int rv;
358         rv = pkey_fips_check_ctx(ctx);
359         if (rv < 0)
360                 {
361                 RSAerr(RSA_F_PKEY_RSA_VERIFY, RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE);
362                 return -1;
363                 }
364 #endif
365         if (rctx->md)
366                 {
367 #ifdef OPENSSL_FIPS
368                 if (rv > 0)
369                         {
370                         return FIPS_rsa_verify_digest(rsa,
371                                                         tbs, tbslen,
372                                                         rctx->md,
373                                                         rctx->pad_mode,
374                                                         rctx->saltlen,
375                                                         rctx->mgf1md,
376                                                         sig, siglen);
377                                                         
378                         }
379 #endif
380                 if (rctx->pad_mode == RSA_PKCS1_PADDING)
381                         return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen,
382                                         sig, siglen, rsa);
383                 if (rctx->pad_mode == RSA_X931_PADDING)
384                         {
385                         if (pkey_rsa_verifyrecover(ctx, NULL, &rslen,
386                                         sig, siglen) <= 0)
387                                 return 0;
388                         }
389                 else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING)
390                         {
391                         int ret;
392                         if (!setup_tbuf(rctx, ctx))
393                                 return -1;
394                         ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
395                                                         rsa, RSA_NO_PADDING);
396                         if (ret <= 0)
397                                 return 0;
398                         ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs,
399                                                 rctx->md, rctx->mgf1md,
400                                                 rctx->tbuf, rctx->saltlen);
401                         if (ret <= 0)
402                                 return 0;
403                         return 1;
404                         }
405                 else
406                         return -1;
407                 }
408         else
409                 {
410                 if (!setup_tbuf(rctx, ctx))
411                         return -1;
412                 rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
413                                                 rsa, rctx->pad_mode);
414                 if (rslen == 0)
415                         return 0;
416                 }
417
418         if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
419                 return 0;
420
421         return 1;
422                         
423         }
424         
425
426 static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
427                                         unsigned char *out, size_t *outlen,
428                                         const unsigned char *in, size_t inlen)
429         {
430         int ret;
431         RSA_PKEY_CTX *rctx = ctx->data;
432         ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
433                                                         rctx->pad_mode);
434         if (ret < 0)
435                 return ret;
436         *outlen = ret;
437         return 1;
438         }
439
440 static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,
441                                         unsigned char *out, size_t *outlen,
442                                         const unsigned char *in, size_t inlen)
443         {
444         int ret;
445         RSA_PKEY_CTX *rctx = ctx->data;
446         ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
447                                                         rctx->pad_mode);
448         if (ret < 0)
449                 return ret;
450         *outlen = ret;
451         return 1;
452         }
453
454 static int check_padding_md(const EVP_MD *md, int padding)
455         {
456         if (!md)
457                 return 1;
458
459         if (padding == RSA_NO_PADDING)
460                 {
461                 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE);
462                 return 0;
463                 }
464
465         if (padding == RSA_X931_PADDING)
466                 {
467                 if (RSA_X931_hash_id(EVP_MD_type(md)) == -1)
468                         {
469                         RSAerr(RSA_F_CHECK_PADDING_MD,
470                                                 RSA_R_INVALID_X931_DIGEST);
471                         return 0;
472                         }
473                 return 1;
474                 }
475
476         return 1;
477         }
478                         
479
480 static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
481         {
482         RSA_PKEY_CTX *rctx = ctx->data;
483         switch (type)
484                 {
485                 case EVP_PKEY_CTRL_RSA_PADDING:
486                 if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING))
487                         {
488                         if (!check_padding_md(rctx->md, p1))
489                                 return 0;
490                         if (p1 == RSA_PKCS1_PSS_PADDING) 
491                                 {
492                                 if (!(ctx->operation &
493                                      (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
494                                         goto bad_pad;
495                                 if (!rctx->md)
496                                         rctx->md = EVP_sha1();
497                                 }
498                         if (p1 == RSA_PKCS1_OAEP_PADDING) 
499                                 {
500                                 if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
501                                         goto bad_pad;
502                                 if (!rctx->md)
503                                         rctx->md = EVP_sha1();
504                                 }
505                         rctx->pad_mode = p1;
506                         return 1;
507                         }
508                 bad_pad:
509                 RSAerr(RSA_F_PKEY_RSA_CTRL,
510                                 RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
511                 return -2;
512
513                 case EVP_PKEY_CTRL_GET_RSA_PADDING:
514                 *(int *)p2 = rctx->pad_mode;
515                 return 1;
516
517                 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
518                 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
519                 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING)
520                         {
521                         RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
522                         return -2;
523                         }
524                 if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN)
525                         *(int *)p2 = rctx->saltlen;
526                 else
527                         {
528                         if (p1 < -2)
529                                 return -2;
530                         rctx->saltlen = p1;
531                         }
532                 return 1;
533
534                 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
535                 if (p1 < 256)
536                         {
537                         RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_KEYBITS);
538                         return -2;
539                         }
540                 rctx->nbits = p1;
541                 return 1;
542
543                 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
544                 if (!p2)
545                         return -2;
546                 rctx->pub_exp = p2;
547                 return 1;
548
549                 case EVP_PKEY_CTRL_MD:
550                 if (!check_padding_md(p2, rctx->pad_mode))
551                         return 0;
552                 rctx->md = p2;
553                 return 1;
554
555                 case EVP_PKEY_CTRL_GET_MD:
556                 *(const EVP_MD **)p2 = rctx->md;
557                 return 1;
558
559                 case EVP_PKEY_CTRL_RSA_MGF1_MD:
560                 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
561                 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING)
562                         {
563                         RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_MGF1_MD);
564                         return -2;
565                         }
566                 if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD)
567                         {
568                         if (rctx->mgf1md)
569                                 *(const EVP_MD **)p2 = rctx->mgf1md;
570                         else
571                                 *(const EVP_MD **)p2 = rctx->md;
572                         }
573                 else
574                         rctx->mgf1md = p2;
575                 return 1;
576
577                 case EVP_PKEY_CTRL_DIGESTINIT:
578                 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
579                 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
580                 case EVP_PKEY_CTRL_PKCS7_SIGN:
581                 return 1;
582 #ifndef OPENSSL_NO_CMS
583                 case EVP_PKEY_CTRL_CMS_DECRYPT:
584                 {
585                 X509_ALGOR *alg = NULL;
586                 ASN1_OBJECT *encalg = NULL;
587                 if (p2)
588                         CMS_RecipientInfo_ktri_get0_algs(p2, NULL, NULL, &alg);
589                 if (alg)
590                         X509_ALGOR_get0(&encalg, NULL, NULL, alg);
591                 if (encalg && OBJ_obj2nid(encalg) == NID_rsaesOaep)
592                         rctx->pad_mode = RSA_PKCS1_OAEP_PADDING;
593                 }
594                 case EVP_PKEY_CTRL_CMS_ENCRYPT:
595                 case EVP_PKEY_CTRL_CMS_SIGN:
596                 return 1;
597 #endif
598                 case EVP_PKEY_CTRL_PEER_KEY:
599                         RSAerr(RSA_F_PKEY_RSA_CTRL,
600                         RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
601                         return -2;      
602
603                 default:
604                 return -2;
605
606                 }
607         }
608                         
609 static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
610                         const char *type, const char *value)
611         {
612         if (!value)
613                 {
614                 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING);
615                 return 0;
616                 }
617         if (!strcmp(type, "rsa_padding_mode"))
618                 {
619                 int pm;
620                 if (!strcmp(value, "pkcs1"))
621                         pm = RSA_PKCS1_PADDING;
622                 else if (!strcmp(value, "sslv23"))
623                         pm = RSA_SSLV23_PADDING;
624                 else if (!strcmp(value, "none"))
625                         pm = RSA_NO_PADDING;
626                 else if (!strcmp(value, "oeap"))
627                         pm = RSA_PKCS1_OAEP_PADDING;
628                 else if (!strcmp(value, "oaep"))
629                         pm = RSA_PKCS1_OAEP_PADDING;
630                 else if (!strcmp(value, "x931"))
631                         pm = RSA_X931_PADDING;
632                 else if (!strcmp(value, "pss"))
633                         pm = RSA_PKCS1_PSS_PADDING;
634                 else
635                         {
636                         RSAerr(RSA_F_PKEY_RSA_CTRL_STR,
637                                                 RSA_R_UNKNOWN_PADDING_TYPE);
638                         return -2;
639                         }
640                 return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
641                 }
642
643         if (!strcmp(type, "rsa_pss_saltlen"))
644                 {
645                 int saltlen;
646                 saltlen = atoi(value);
647                 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
648                 }
649
650         if (!strcmp(type, "rsa_keygen_bits"))
651                 {
652                 int nbits;
653                 nbits = atoi(value);
654                 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
655                 }
656
657         if (!strcmp(type, "rsa_keygen_pubexp"))
658                 {
659                 int ret;
660                 BIGNUM *pubexp = NULL;
661                 if (!BN_asc2bn(&pubexp, value))
662                         return 0;
663                 ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp);
664                 if (ret <= 0)
665                         BN_free(pubexp);
666                 return ret;
667                 }
668
669         return -2;
670         }
671
672 static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
673         {
674         RSA *rsa = NULL;
675         RSA_PKEY_CTX *rctx = ctx->data;
676         BN_GENCB *pcb, cb;
677         int ret;
678         if (!rctx->pub_exp)
679                 {
680                 rctx->pub_exp = BN_new();
681                 if (!rctx->pub_exp || !BN_set_word(rctx->pub_exp, RSA_F4))
682                         return 0;
683                 }
684         rsa = RSA_new();
685         if (!rsa)
686                 return 0;
687         if (ctx->pkey_gencb)
688                 {
689                 pcb = &cb;
690                 evp_pkey_set_cb_translate(pcb, ctx);
691                 }
692         else
693                 pcb = NULL;
694         ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb);
695         if (ret > 0)
696                 EVP_PKEY_assign_RSA(pkey, rsa);
697         else
698                 RSA_free(rsa);
699         return ret;
700         }
701
702 const EVP_PKEY_METHOD rsa_pkey_meth = 
703         {
704         EVP_PKEY_RSA,
705         EVP_PKEY_FLAG_AUTOARGLEN,
706         pkey_rsa_init,
707         pkey_rsa_copy,
708         pkey_rsa_cleanup,
709
710         0,0,
711
712         0,
713         pkey_rsa_keygen,
714
715         0,
716         pkey_rsa_sign,
717
718         0,
719         pkey_rsa_verify,
720
721         0,
722         pkey_rsa_verifyrecover,
723
724
725         0,0,0,0,
726
727         0,
728         pkey_rsa_encrypt,
729
730         0,
731         pkey_rsa_decrypt,
732
733         0,0,
734
735         pkey_rsa_ctrl,
736         pkey_rsa_ctrl_str
737
738
739         };