91dc668ce78a6a2599abb1c2a26f8e1362394985
[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 "internal/evp_int.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 ((size_t)EVP_PKEY_size(ctx->pkey) < tbslen + 1) {
199                 RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_KEY_SIZE_TOO_SMALL);
200                 return -1;
201             }
202             if (!setup_tbuf(rctx, ctx)) {
203                 RSAerr(RSA_F_PKEY_RSA_SIGN, ERR_R_MALLOC_FAILURE);
204                 return -1;
205             }
206             memcpy(rctx->tbuf, tbs, tbslen);
207             rctx->tbuf[tbslen] = RSA_X931_hash_id(EVP_MD_type(rctx->md));
208             ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf,
209                                       sig, rsa, RSA_X931_PADDING);
210         } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
211             unsigned int sltmp;
212             ret = RSA_sign(EVP_MD_type(rctx->md),
213                            tbs, tbslen, sig, &sltmp, rsa);
214             if (ret <= 0)
215                 return ret;
216             ret = sltmp;
217         } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
218             if (!setup_tbuf(rctx, ctx))
219                 return -1;
220             if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa,
221                                                 rctx->tbuf, tbs,
222                                                 rctx->md, rctx->mgf1md,
223                                                 rctx->saltlen))
224                 return -1;
225             ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
226                                       sig, rsa, RSA_NO_PADDING);
227         } else
228             return -1;
229     } else
230         ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa,
231                                   rctx->pad_mode);
232     if (ret < 0)
233         return ret;
234     *siglen = ret;
235     return 1;
236 }
237
238 static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
239                                   unsigned char *rout, size_t *routlen,
240                                   const unsigned char *sig, size_t siglen)
241 {
242     int ret;
243     RSA_PKEY_CTX *rctx = ctx->data;
244
245     if (rctx->md) {
246         if (rctx->pad_mode == RSA_X931_PADDING) {
247             if (!setup_tbuf(rctx, ctx))
248                 return -1;
249             ret = RSA_public_decrypt(siglen, sig,
250                                      rctx->tbuf, ctx->pkey->pkey.rsa,
251                                      RSA_X931_PADDING);
252             if (ret < 1)
253                 return 0;
254             ret--;
255             if (rctx->tbuf[ret] != RSA_X931_hash_id(EVP_MD_type(rctx->md))) {
256                 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
257                        RSA_R_ALGORITHM_MISMATCH);
258                 return 0;
259             }
260             if (ret != EVP_MD_size(rctx->md)) {
261                 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
262                        RSA_R_INVALID_DIGEST_LENGTH);
263                 return 0;
264             }
265             if (rout)
266                 memcpy(rout, rctx->tbuf, ret);
267         } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
268             size_t sltmp;
269             ret = int_rsa_verify(EVP_MD_type(rctx->md),
270                                  NULL, 0, rout, &sltmp,
271                                  sig, siglen, ctx->pkey->pkey.rsa);
272             if (ret <= 0)
273                 return 0;
274             ret = sltmp;
275         } else
276             return -1;
277     } else
278         ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa,
279                                  rctx->pad_mode);
280     if (ret < 0)
281         return ret;
282     *routlen = ret;
283     return 1;
284 }
285
286 static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
287                            const unsigned char *sig, size_t siglen,
288                            const unsigned char *tbs, size_t tbslen)
289 {
290     RSA_PKEY_CTX *rctx = ctx->data;
291     RSA *rsa = ctx->pkey->pkey.rsa;
292     size_t rslen;
293     if (rctx->md) {
294         if (rctx->pad_mode == RSA_PKCS1_PADDING)
295             return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen,
296                               sig, siglen, rsa);
297         if (rctx->pad_mode == RSA_X931_PADDING) {
298             if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig, siglen) <= 0)
299                 return 0;
300         } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
301             int ret;
302             if (!setup_tbuf(rctx, ctx))
303                 return -1;
304             ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
305                                      rsa, RSA_NO_PADDING);
306             if (ret <= 0)
307                 return 0;
308             ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs,
309                                             rctx->md, rctx->mgf1md,
310                                             rctx->tbuf, rctx->saltlen);
311             if (ret <= 0)
312                 return 0;
313             return 1;
314         } else
315             return -1;
316     } else {
317         if (!setup_tbuf(rctx, ctx))
318             return -1;
319         rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
320                                    rsa, rctx->pad_mode);
321         if (rslen == 0)
322             return 0;
323     }
324
325     if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
326         return 0;
327
328     return 1;
329
330 }
331
332 static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
333                             unsigned char *out, size_t *outlen,
334                             const unsigned char *in, size_t inlen)
335 {
336     int ret;
337     RSA_PKEY_CTX *rctx = ctx->data;
338     if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
339         int klen = RSA_size(ctx->pkey->pkey.rsa);
340         if (!setup_tbuf(rctx, ctx))
341             return -1;
342         if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen,
343                                              in, inlen,
344                                              rctx->oaep_label,
345                                              rctx->oaep_labellen,
346                                              rctx->md, rctx->mgf1md))
347             return -1;
348         ret = RSA_public_encrypt(klen, rctx->tbuf, out,
349                                  ctx->pkey->pkey.rsa, RSA_NO_PADDING);
350     } else
351         ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
352                                  rctx->pad_mode);
353     if (ret < 0)
354         return ret;
355     *outlen = ret;
356     return 1;
357 }
358
359 static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,
360                             unsigned char *out, size_t *outlen,
361                             const unsigned char *in, size_t inlen)
362 {
363     int ret;
364     RSA_PKEY_CTX *rctx = ctx->data;
365     if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
366         int i;
367         if (!setup_tbuf(rctx, ctx))
368             return -1;
369         ret = RSA_private_decrypt(inlen, in, rctx->tbuf,
370                                   ctx->pkey->pkey.rsa, RSA_NO_PADDING);
371         if (ret <= 0)
372             return ret;
373         for (i = 0; i < ret; i++) {
374             if (rctx->tbuf[i])
375                 break;
376         }
377         ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, rctx->tbuf + i,
378                                                 ret - i, ret,
379                                                 rctx->oaep_label,
380                                                 rctx->oaep_labellen,
381                                                 rctx->md, rctx->mgf1md);
382     } else
383         ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
384                                   rctx->pad_mode);
385     if (ret < 0)
386         return ret;
387     *outlen = ret;
388     return 1;
389 }
390
391 static int check_padding_md(const EVP_MD *md, int padding)
392 {
393     if (!md)
394         return 1;
395
396     if (padding == RSA_NO_PADDING) {
397         RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE);
398         return 0;
399     }
400
401     if (padding == RSA_X931_PADDING) {
402         if (RSA_X931_hash_id(EVP_MD_type(md)) == -1) {
403             RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_X931_DIGEST);
404             return 0;
405         }
406         return 1;
407     }
408
409     return 1;
410 }
411
412 static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
413 {
414     RSA_PKEY_CTX *rctx = ctx->data;
415     switch (type) {
416     case EVP_PKEY_CTRL_RSA_PADDING:
417         if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) {
418             if (!check_padding_md(rctx->md, p1))
419                 return 0;
420             if (p1 == RSA_PKCS1_PSS_PADDING) {
421                 if (!(ctx->operation &
422                       (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
423                     goto bad_pad;
424                 if (!rctx->md)
425                     rctx->md = EVP_sha1();
426             }
427             if (p1 == RSA_PKCS1_OAEP_PADDING) {
428                 if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
429                     goto bad_pad;
430                 if (!rctx->md)
431                     rctx->md = EVP_sha1();
432             }
433             rctx->pad_mode = p1;
434             return 1;
435         }
436  bad_pad:
437         RSAerr(RSA_F_PKEY_RSA_CTRL,
438                RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
439         return -2;
440
441     case EVP_PKEY_CTRL_GET_RSA_PADDING:
442         *(int *)p2 = rctx->pad_mode;
443         return 1;
444
445     case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
446     case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
447         if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
448             RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
449             return -2;
450         }
451         if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN)
452             *(int *)p2 = rctx->saltlen;
453         else {
454             if (p1 < -2)
455                 return -2;
456             rctx->saltlen = p1;
457         }
458         return 1;
459
460     case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
461         if (p1 < 512) {
462             RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_KEY_SIZE_TOO_SMALL);
463             return -2;
464         }
465         rctx->nbits = p1;
466         return 1;
467
468     case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
469         if (!p2)
470             return -2;
471         BN_free(rctx->pub_exp);
472         rctx->pub_exp = p2;
473         return 1;
474
475     case EVP_PKEY_CTRL_RSA_OAEP_MD:
476     case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
477         if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
478             RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
479             return -2;
480         }
481         if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD)
482             *(const EVP_MD **)p2 = rctx->md;
483         else
484             rctx->md = p2;
485         return 1;
486
487     case EVP_PKEY_CTRL_MD:
488         if (!check_padding_md(p2, rctx->pad_mode))
489             return 0;
490         rctx->md = p2;
491         return 1;
492
493     case EVP_PKEY_CTRL_GET_MD:
494         *(const EVP_MD **)p2 = rctx->md;
495         return 1;
496
497     case EVP_PKEY_CTRL_RSA_MGF1_MD:
498     case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
499         if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING
500             && rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
501             RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_MGF1_MD);
502             return -2;
503         }
504         if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
505             if (rctx->mgf1md)
506                 *(const EVP_MD **)p2 = rctx->mgf1md;
507             else
508                 *(const EVP_MD **)p2 = rctx->md;
509         } else
510             rctx->mgf1md = p2;
511         return 1;
512
513     case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
514         if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
515             RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
516             return -2;
517         }
518         if (rctx->oaep_label)
519             OPENSSL_free(rctx->oaep_label);
520         if (p2 && p1 > 0) {
521             rctx->oaep_label = p2;
522             rctx->oaep_labellen = p1;
523         } else {
524             rctx->oaep_label = NULL;
525             rctx->oaep_labellen = 0;
526         }
527         return 1;
528
529     case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
530         if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
531             RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
532             return -2;
533         }
534         *(unsigned char **)p2 = rctx->oaep_label;
535         return rctx->oaep_labellen;
536
537     case EVP_PKEY_CTRL_DIGESTINIT:
538     case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
539     case EVP_PKEY_CTRL_PKCS7_DECRYPT:
540     case EVP_PKEY_CTRL_PKCS7_SIGN:
541         return 1;
542 #ifndef OPENSSL_NO_CMS
543     case EVP_PKEY_CTRL_CMS_DECRYPT:
544     case EVP_PKEY_CTRL_CMS_ENCRYPT:
545     case EVP_PKEY_CTRL_CMS_SIGN:
546         return 1;
547 #endif
548     case EVP_PKEY_CTRL_PEER_KEY:
549         RSAerr(RSA_F_PKEY_RSA_CTRL,
550                RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
551         return -2;
552
553     default:
554         return -2;
555
556     }
557 }
558
559 static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
560                              const char *type, const char *value)
561 {
562     if (!value) {
563         RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING);
564         return 0;
565     }
566     if (!strcmp(type, "rsa_padding_mode")) {
567         int pm;
568         if (!strcmp(value, "pkcs1"))
569             pm = RSA_PKCS1_PADDING;
570         else if (!strcmp(value, "sslv23"))
571             pm = RSA_SSLV23_PADDING;
572         else if (!strcmp(value, "none"))
573             pm = RSA_NO_PADDING;
574         else if (!strcmp(value, "oeap"))
575             pm = RSA_PKCS1_OAEP_PADDING;
576         else if (!strcmp(value, "oaep"))
577             pm = RSA_PKCS1_OAEP_PADDING;
578         else if (!strcmp(value, "x931"))
579             pm = RSA_X931_PADDING;
580         else if (!strcmp(value, "pss"))
581             pm = RSA_PKCS1_PSS_PADDING;
582         else {
583             RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_UNKNOWN_PADDING_TYPE);
584             return -2;
585         }
586         return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
587     }
588
589     if (!strcmp(type, "rsa_pss_saltlen")) {
590         int saltlen;
591         saltlen = atoi(value);
592         return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
593     }
594
595     if (!strcmp(type, "rsa_keygen_bits")) {
596         int nbits;
597         nbits = atoi(value);
598         return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
599     }
600
601     if (!strcmp(type, "rsa_keygen_pubexp")) {
602         int ret;
603         BIGNUM *pubexp = NULL;
604         if (!BN_asc2bn(&pubexp, value))
605             return 0;
606         ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp);
607         if (ret <= 0)
608             BN_free(pubexp);
609         return ret;
610     }
611
612     if (!strcmp(type, "rsa_mgf1_md")) {
613         const EVP_MD *md;
614         if (!(md = EVP_get_digestbyname(value))) {
615             RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_INVALID_DIGEST);
616             return 0;
617         }
618         return EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, md);
619     }
620
621     if (!strcmp(type, "rsa_oaep_md")) {
622         const EVP_MD *md;
623         if (!(md = EVP_get_digestbyname(value))) {
624             RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_INVALID_DIGEST);
625             return 0;
626         }
627         return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, md);
628     }
629     if (!strcmp(type, "rsa_oaep_label")) {
630         unsigned char *lab;
631         long lablen;
632         int ret;
633         lab = string_to_hex(value, &lablen);
634         if (!lab)
635             return 0;
636         ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen);
637         if (ret <= 0)
638             OPENSSL_free(lab);
639         return ret;
640     }
641
642     return -2;
643 }
644
645 static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
646 {
647     RSA *rsa = NULL;
648     RSA_PKEY_CTX *rctx = ctx->data;
649     BN_GENCB *pcb;
650     int ret;
651     if (!rctx->pub_exp) {
652         rctx->pub_exp = BN_new();
653         if (!rctx->pub_exp || !BN_set_word(rctx->pub_exp, RSA_F4))
654             return 0;
655     }
656     rsa = RSA_new();
657     if (!rsa)
658         return 0;
659     if (ctx->pkey_gencb) {
660         pcb = BN_GENCB_new();
661         if (!pcb) {
662             RSA_free(rsa);
663             return 0;
664         }
665         evp_pkey_set_cb_translate(pcb, ctx);
666     } else
667         pcb = NULL;
668     ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb);
669     BN_GENCB_free(pcb);
670     if (ret > 0)
671         EVP_PKEY_assign_RSA(pkey, rsa);
672     else
673         RSA_free(rsa);
674     return ret;
675 }
676
677 const EVP_PKEY_METHOD rsa_pkey_meth = {
678     EVP_PKEY_RSA,
679     EVP_PKEY_FLAG_AUTOARGLEN,
680     pkey_rsa_init,
681     pkey_rsa_copy,
682     pkey_rsa_cleanup,
683
684     0, 0,
685
686     0,
687     pkey_rsa_keygen,
688
689     0,
690     pkey_rsa_sign,
691
692     0,
693     pkey_rsa_verify,
694
695     0,
696     pkey_rsa_verifyrecover,
697
698     0, 0, 0, 0,
699
700     0,
701     pkey_rsa_encrypt,
702
703     0,
704     pkey_rsa_decrypt,
705
706     0, 0,
707
708     pkey_rsa_ctrl,
709     pkey_rsa_ctrl_str
710 };