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