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