fips module header inclusion fine-tunning
[openssl.git] / crypto / rsa / rsa_pmeth.c
1 /*
2  * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /*
11  * RSA low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15
16 #include "internal/constant_time.h"
17
18 #include <stdio.h>
19 #include "internal/cryptlib.h"
20 #include <openssl/asn1t.h>
21 #include <openssl/x509.h>
22 #include <openssl/rsa.h>
23 #include <openssl/bn.h>
24 #include <openssl/evp.h>
25 #include <openssl/x509v3.h>
26 #include <openssl/cms.h>
27 #include "crypto/evp.h"
28 #include "crypto/rsa.h"
29 #include "rsa_local.h"
30
31 /* RSA pkey context structure */
32
33 typedef struct {
34     /* Key gen parameters */
35     int nbits;
36     BIGNUM *pub_exp;
37     int primes;
38     /* Keygen callback info */
39     int gentmp[2];
40     /* RSA padding mode */
41     int pad_mode;
42     /* message digest */
43     const EVP_MD *md;
44     /* message digest for MGF1 */
45     const EVP_MD *mgf1md;
46     /* PSS salt length */
47     int saltlen;
48     /* Minimum salt length or -1 if no PSS parameter restriction */
49     int min_saltlen;
50     /* Temp buffer */
51     unsigned char *tbuf;
52     /* OAEP label */
53     unsigned char *oaep_label;
54     size_t oaep_labellen;
55 } RSA_PKEY_CTX;
56
57 /* True if PSS parameters are restricted */
58 #define rsa_pss_restricted(rctx) (rctx->min_saltlen != -1)
59
60 static int pkey_rsa_init(EVP_PKEY_CTX *ctx)
61 {
62     RSA_PKEY_CTX *rctx = OPENSSL_zalloc(sizeof(*rctx));
63
64     if (rctx == NULL)
65         return 0;
66     rctx->nbits = 2048;
67     rctx->primes = RSA_DEFAULT_PRIME_NUM;
68     if (pkey_ctx_is_pss(ctx))
69         rctx->pad_mode = RSA_PKCS1_PSS_PADDING;
70     else
71         rctx->pad_mode = RSA_PKCS1_PADDING;
72     /* Maximum for sign, auto for verify */
73     rctx->saltlen = RSA_PSS_SALTLEN_AUTO;
74     rctx->min_saltlen = -1;
75     ctx->data = rctx;
76     ctx->keygen_info = rctx->gentmp;
77     ctx->keygen_info_count = 2;
78
79     return 1;
80 }
81
82 static int pkey_rsa_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
83 {
84     RSA_PKEY_CTX *dctx, *sctx;
85
86     if (!pkey_rsa_init(dst))
87         return 0;
88     sctx = src->data;
89     dctx = dst->data;
90     dctx->nbits = sctx->nbits;
91     if (sctx->pub_exp) {
92         dctx->pub_exp = BN_dup(sctx->pub_exp);
93         if (!dctx->pub_exp)
94             return 0;
95     }
96     dctx->pad_mode = sctx->pad_mode;
97     dctx->md = sctx->md;
98     dctx->mgf1md = sctx->mgf1md;
99     dctx->saltlen = sctx->saltlen;
100     if (sctx->oaep_label) {
101         OPENSSL_free(dctx->oaep_label);
102         dctx->oaep_label = OPENSSL_memdup(sctx->oaep_label, sctx->oaep_labellen);
103         if (!dctx->oaep_label)
104             return 0;
105         dctx->oaep_labellen = sctx->oaep_labellen;
106     }
107     return 1;
108 }
109
110 static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk)
111 {
112     if (ctx->tbuf != NULL)
113         return 1;
114     if ((ctx->tbuf = OPENSSL_malloc(RSA_size(pk->pkey->pkey.rsa))) == NULL) {
115         ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
116         return 0;
117     }
118     return 1;
119 }
120
121 static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx)
122 {
123     RSA_PKEY_CTX *rctx = ctx->data;
124     if (rctx) {
125         BN_free(rctx->pub_exp);
126         OPENSSL_free(rctx->tbuf);
127         OPENSSL_free(rctx->oaep_label);
128         OPENSSL_free(rctx);
129     }
130 }
131
132 static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
133                          size_t *siglen, const unsigned char *tbs,
134                          size_t tbslen)
135 {
136     int ret;
137     RSA_PKEY_CTX *rctx = ctx->data;
138     RSA *rsa = ctx->pkey->pkey.rsa;
139
140     if (rctx->md) {
141         if (tbslen != (size_t)EVP_MD_get_size(rctx->md)) {
142             ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);
143             return -1;
144         }
145
146         if (EVP_MD_get_type(rctx->md) == NID_mdc2) {
147             unsigned int sltmp;
148             if (rctx->pad_mode != RSA_PKCS1_PADDING)
149                 return -1;
150             ret = RSA_sign_ASN1_OCTET_STRING(0,
151                                              tbs, tbslen, sig, &sltmp, rsa);
152
153             if (ret <= 0)
154                 return ret;
155             ret = sltmp;
156         } else if (rctx->pad_mode == RSA_X931_PADDING) {
157             if ((size_t)RSA_size(rsa) < tbslen + 1) {
158                 ERR_raise(ERR_LIB_RSA, RSA_R_KEY_SIZE_TOO_SMALL);
159                 return -1;
160             }
161             if (!setup_tbuf(rctx, ctx)) {
162                 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
163                 return -1;
164             }
165             memcpy(rctx->tbuf, tbs, tbslen);
166             rctx->tbuf[tbslen] = RSA_X931_hash_id(EVP_MD_get_type(rctx->md));
167             ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf,
168                                       sig, rsa, RSA_X931_PADDING);
169         } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
170             unsigned int sltmp;
171             ret = RSA_sign(EVP_MD_get_type(rctx->md),
172                            tbs, tbslen, sig, &sltmp, rsa);
173             if (ret <= 0)
174                 return ret;
175             ret = sltmp;
176         } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
177             if (!setup_tbuf(rctx, ctx))
178                 return -1;
179             if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa,
180                                                 rctx->tbuf, tbs,
181                                                 rctx->md, rctx->mgf1md,
182                                                 rctx->saltlen))
183                 return -1;
184             ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
185                                       sig, rsa, RSA_NO_PADDING);
186         } else {
187             return -1;
188         }
189     } else {
190         ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa,
191                                   rctx->pad_mode);
192     }
193     if (ret < 0)
194         return ret;
195     *siglen = ret;
196     return 1;
197 }
198
199 static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
200                                   unsigned char *rout, size_t *routlen,
201                                   const unsigned char *sig, size_t siglen)
202 {
203     int ret;
204     RSA_PKEY_CTX *rctx = ctx->data;
205
206     if (rctx->md) {
207         if (rctx->pad_mode == RSA_X931_PADDING) {
208             if (!setup_tbuf(rctx, ctx))
209                 return -1;
210             ret = RSA_public_decrypt(siglen, sig,
211                                      rctx->tbuf, ctx->pkey->pkey.rsa,
212                                      RSA_X931_PADDING);
213             if (ret < 1)
214                 return 0;
215             ret--;
216             if (rctx->tbuf[ret] != RSA_X931_hash_id(EVP_MD_get_type(rctx->md))) {
217                 ERR_raise(ERR_LIB_RSA, RSA_R_ALGORITHM_MISMATCH);
218                 return 0;
219             }
220             if (ret != EVP_MD_get_size(rctx->md)) {
221                 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);
222                 return 0;
223             }
224             if (rout)
225                 memcpy(rout, rctx->tbuf, ret);
226         } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
227             size_t sltmp;
228             ret = ossl_rsa_verify(EVP_MD_get_type(rctx->md),
229                                   NULL, 0, rout, &sltmp,
230                                   sig, siglen, ctx->pkey->pkey.rsa);
231             if (ret <= 0)
232                 return 0;
233             ret = sltmp;
234         } else {
235             return -1;
236         }
237     } else {
238         ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa,
239                                  rctx->pad_mode);
240     }
241     if (ret < 0)
242         return ret;
243     *routlen = ret;
244     return 1;
245 }
246
247 static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
248                            const unsigned char *sig, size_t siglen,
249                            const unsigned char *tbs, size_t tbslen)
250 {
251     RSA_PKEY_CTX *rctx = ctx->data;
252     RSA *rsa = ctx->pkey->pkey.rsa;
253     size_t rslen;
254
255     if (rctx->md) {
256         if (rctx->pad_mode == RSA_PKCS1_PADDING)
257             return RSA_verify(EVP_MD_get_type(rctx->md), tbs, tbslen,
258                               sig, siglen, rsa);
259         if (tbslen != (size_t)EVP_MD_get_size(rctx->md)) {
260             ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);
261             return -1;
262         }
263         if (rctx->pad_mode == RSA_X931_PADDING) {
264             if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig, siglen) <= 0)
265                 return 0;
266         } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
267             int ret;
268             if (!setup_tbuf(rctx, ctx))
269                 return -1;
270             ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
271                                      rsa, RSA_NO_PADDING);
272             if (ret <= 0)
273                 return 0;
274             ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs,
275                                             rctx->md, rctx->mgf1md,
276                                             rctx->tbuf, rctx->saltlen);
277             if (ret <= 0)
278                 return 0;
279             return 1;
280         } else {
281             return -1;
282         }
283     } else {
284         if (!setup_tbuf(rctx, ctx))
285             return -1;
286         rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
287                                    rsa, rctx->pad_mode);
288         if (rslen == 0)
289             return 0;
290     }
291
292     if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
293         return 0;
294
295     return 1;
296
297 }
298
299 static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
300                             unsigned char *out, size_t *outlen,
301                             const unsigned char *in, size_t inlen)
302 {
303     int ret;
304     RSA_PKEY_CTX *rctx = ctx->data;
305
306     if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
307         int klen = RSA_size(ctx->pkey->pkey.rsa);
308         if (!setup_tbuf(rctx, ctx))
309             return -1;
310         if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen,
311                                              in, inlen,
312                                              rctx->oaep_label,
313                                              rctx->oaep_labellen,
314                                              rctx->md, rctx->mgf1md))
315             return -1;
316         ret = RSA_public_encrypt(klen, rctx->tbuf, out,
317                                  ctx->pkey->pkey.rsa, RSA_NO_PADDING);
318     } else {
319         ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
320                                  rctx->pad_mode);
321     }
322     if (ret < 0)
323         return ret;
324     *outlen = ret;
325     return 1;
326 }
327
328 static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,
329                             unsigned char *out, size_t *outlen,
330                             const unsigned char *in, size_t inlen)
331 {
332     int ret;
333     RSA_PKEY_CTX *rctx = ctx->data;
334
335     if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
336         if (!setup_tbuf(rctx, ctx))
337             return -1;
338         ret = RSA_private_decrypt(inlen, in, rctx->tbuf,
339                                   ctx->pkey->pkey.rsa, RSA_NO_PADDING);
340         if (ret <= 0)
341             return ret;
342         ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, rctx->tbuf,
343                                                 ret, ret,
344                                                 rctx->oaep_label,
345                                                 rctx->oaep_labellen,
346                                                 rctx->md, rctx->mgf1md);
347     } else {
348         ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
349                                   rctx->pad_mode);
350     }
351     *outlen = constant_time_select_s(constant_time_msb_s(ret), *outlen, ret);
352     ret = constant_time_select_int(constant_time_msb(ret), ret, 1);
353     return ret;
354 }
355
356 static int check_padding_md(const EVP_MD *md, int padding)
357 {
358     int mdnid;
359
360     if (!md)
361         return 1;
362
363     mdnid = EVP_MD_get_type(md);
364
365     if (padding == RSA_NO_PADDING) {
366         ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING_MODE);
367         return 0;
368     }
369
370     if (padding == RSA_X931_PADDING) {
371         if (RSA_X931_hash_id(mdnid) == -1) {
372             ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_X931_DIGEST);
373             return 0;
374         }
375     } else {
376         switch(mdnid) {
377         /* List of all supported RSA digests */
378         case NID_sha1:
379         case NID_sha224:
380         case NID_sha256:
381         case NID_sha384:
382         case NID_sha512:
383         case NID_sha512_224:
384         case NID_sha512_256:
385         case NID_md5:
386         case NID_md5_sha1:
387         case NID_md2:
388         case NID_md4:
389         case NID_mdc2:
390         case NID_ripemd160:
391         case NID_sha3_224:
392         case NID_sha3_256:
393         case NID_sha3_384:
394         case NID_sha3_512:
395             return 1;
396
397         default:
398             ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST);
399             return 0;
400
401         }
402     }
403
404     return 1;
405 }
406
407 static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
408 {
409     RSA_PKEY_CTX *rctx = ctx->data;
410
411     switch (type) {
412     case EVP_PKEY_CTRL_RSA_PADDING:
413         if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) {
414             if (!check_padding_md(rctx->md, p1))
415                 return 0;
416             if (p1 == RSA_PKCS1_PSS_PADDING) {
417                 if (!(ctx->operation &
418                       (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
419                     goto bad_pad;
420                 if (!rctx->md)
421                     rctx->md = EVP_sha1();
422             } else if (pkey_ctx_is_pss(ctx)) {
423                 goto bad_pad;
424             }
425             if (p1 == RSA_PKCS1_OAEP_PADDING) {
426                 if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
427                     goto bad_pad;
428                 if (!rctx->md)
429                     rctx->md = EVP_sha1();
430             }
431             rctx->pad_mode = p1;
432             return 1;
433         }
434  bad_pad:
435         ERR_raise(ERR_LIB_RSA, RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
436         return -2;
437
438     case EVP_PKEY_CTRL_GET_RSA_PADDING:
439         *(int *)p2 = rctx->pad_mode;
440         return 1;
441
442     case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
443     case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
444         if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
445             ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PSS_SALTLEN);
446             return -2;
447         }
448         if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN) {
449             *(int *)p2 = rctx->saltlen;
450         } else {
451             if (p1 < RSA_PSS_SALTLEN_MAX)
452                 return -2;
453             if (rsa_pss_restricted(rctx)) {
454                 if (p1 == RSA_PSS_SALTLEN_AUTO
455                     && ctx->operation == EVP_PKEY_OP_VERIFY) {
456                     ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PSS_SALTLEN);
457                     return -2;
458                 }
459                 if ((p1 == RSA_PSS_SALTLEN_DIGEST
460                      && rctx->min_saltlen > EVP_MD_get_size(rctx->md))
461                     || (p1 >= 0 && p1 < rctx->min_saltlen)) {
462                     ERR_raise(ERR_LIB_RSA, RSA_R_PSS_SALTLEN_TOO_SMALL);
463                     return 0;
464                 }
465             }
466             rctx->saltlen = p1;
467         }
468         return 1;
469
470     case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
471         if (p1 < RSA_MIN_MODULUS_BITS) {
472             ERR_raise(ERR_LIB_RSA, RSA_R_KEY_SIZE_TOO_SMALL);
473             return -2;
474         }
475         rctx->nbits = p1;
476         return 1;
477
478     case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
479         if (p2 == NULL || !BN_is_odd((BIGNUM *)p2) || BN_is_one((BIGNUM *)p2)) {
480             ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
481             return -2;
482         }
483         BN_free(rctx->pub_exp);
484         rctx->pub_exp = p2;
485         return 1;
486
487     case EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES:
488         if (p1 < RSA_DEFAULT_PRIME_NUM || p1 > RSA_MAX_PRIME_NUM) {
489             ERR_raise(ERR_LIB_RSA, RSA_R_KEY_PRIME_NUM_INVALID);
490             return -2;
491         }
492         rctx->primes = p1;
493         return 1;
494
495     case EVP_PKEY_CTRL_RSA_OAEP_MD:
496     case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
497         if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
498             ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING_MODE);
499             return -2;
500         }
501         if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD)
502             *(const EVP_MD **)p2 = rctx->md;
503         else
504             rctx->md = p2;
505         return 1;
506
507     case EVP_PKEY_CTRL_MD:
508         if (!check_padding_md(p2, rctx->pad_mode))
509             return 0;
510         if (rsa_pss_restricted(rctx)) {
511             if (EVP_MD_get_type(rctx->md) == EVP_MD_get_type(p2))
512                 return 1;
513             ERR_raise(ERR_LIB_RSA, RSA_R_DIGEST_NOT_ALLOWED);
514             return 0;
515         }
516         rctx->md = p2;
517         return 1;
518
519     case EVP_PKEY_CTRL_GET_MD:
520         *(const EVP_MD **)p2 = rctx->md;
521         return 1;
522
523     case EVP_PKEY_CTRL_RSA_MGF1_MD:
524     case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
525         if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING
526             && rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
527             ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_MGF1_MD);
528             return -2;
529         }
530         if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
531             if (rctx->mgf1md)
532                 *(const EVP_MD **)p2 = rctx->mgf1md;
533             else
534                 *(const EVP_MD **)p2 = rctx->md;
535         } else {
536             if (rsa_pss_restricted(rctx)) {
537                 if (EVP_MD_get_type(rctx->mgf1md) == EVP_MD_get_type(p2))
538                     return 1;
539                 ERR_raise(ERR_LIB_RSA, RSA_R_MGF1_DIGEST_NOT_ALLOWED);
540                 return 0;
541             }
542             rctx->mgf1md = p2;
543         }
544         return 1;
545
546     case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
547         if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
548             ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING_MODE);
549             return -2;
550         }
551         OPENSSL_free(rctx->oaep_label);
552         if (p2 && p1 > 0) {
553             rctx->oaep_label = p2;
554             rctx->oaep_labellen = p1;
555         } else {
556             rctx->oaep_label = NULL;
557             rctx->oaep_labellen = 0;
558         }
559         return 1;
560
561     case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
562         if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
563             ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING_MODE);
564             return -2;
565         }
566         *(unsigned char **)p2 = rctx->oaep_label;
567         return rctx->oaep_labellen;
568
569     case EVP_PKEY_CTRL_DIGESTINIT:
570     case EVP_PKEY_CTRL_PKCS7_SIGN:
571 #ifndef OPENSSL_NO_CMS
572     case EVP_PKEY_CTRL_CMS_SIGN:
573 #endif
574     return 1;
575
576     case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
577     case EVP_PKEY_CTRL_PKCS7_DECRYPT:
578 #ifndef OPENSSL_NO_CMS
579     case EVP_PKEY_CTRL_CMS_DECRYPT:
580     case EVP_PKEY_CTRL_CMS_ENCRYPT:
581 #endif
582     if (!pkey_ctx_is_pss(ctx))
583         return 1;
584     /* fall through */
585     case EVP_PKEY_CTRL_PEER_KEY:
586         ERR_raise(ERR_LIB_RSA, RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
587         return -2;
588
589     default:
590         return -2;
591
592     }
593 }
594
595 static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
596                              const char *type, const char *value)
597 {
598     if (value == NULL) {
599         ERR_raise(ERR_LIB_RSA, RSA_R_VALUE_MISSING);
600         return 0;
601     }
602     if (strcmp(type, "rsa_padding_mode") == 0) {
603         int pm;
604
605         if (strcmp(value, "pkcs1") == 0) {
606             pm = RSA_PKCS1_PADDING;
607         } else if (strcmp(value, "none") == 0) {
608             pm = RSA_NO_PADDING;
609         } else if (strcmp(value, "oeap") == 0) {
610             pm = RSA_PKCS1_OAEP_PADDING;
611         } else if (strcmp(value, "oaep") == 0) {
612             pm = RSA_PKCS1_OAEP_PADDING;
613         } else if (strcmp(value, "x931") == 0) {
614             pm = RSA_X931_PADDING;
615         } else if (strcmp(value, "pss") == 0) {
616             pm = RSA_PKCS1_PSS_PADDING;
617         } else {
618             ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
619             return -2;
620         }
621         return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
622     }
623
624     if (strcmp(type, "rsa_pss_saltlen") == 0) {
625         int saltlen;
626
627         if (!strcmp(value, "digest"))
628             saltlen = RSA_PSS_SALTLEN_DIGEST;
629         else if (!strcmp(value, "max"))
630             saltlen = RSA_PSS_SALTLEN_MAX;
631         else if (!strcmp(value, "auto"))
632             saltlen = RSA_PSS_SALTLEN_AUTO;
633         else
634             saltlen = atoi(value);
635         return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
636     }
637
638     if (strcmp(type, "rsa_keygen_bits") == 0) {
639         int nbits = atoi(value);
640
641         return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
642     }
643
644     if (strcmp(type, "rsa_keygen_pubexp") == 0) {
645         int ret;
646
647         BIGNUM *pubexp = NULL;
648         if (!BN_asc2bn(&pubexp, value))
649             return 0;
650         ret = EVP_PKEY_CTX_set1_rsa_keygen_pubexp(ctx, pubexp);
651         BN_free(pubexp);
652         return ret;
653     }
654
655     if (strcmp(type, "rsa_keygen_primes") == 0) {
656         int nprimes = atoi(value);
657
658         return EVP_PKEY_CTX_set_rsa_keygen_primes(ctx, nprimes);
659     }
660
661     if (strcmp(type, "rsa_mgf1_md") == 0)
662         return EVP_PKEY_CTX_md(ctx,
663                                EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
664                                EVP_PKEY_CTRL_RSA_MGF1_MD, value);
665
666     if (pkey_ctx_is_pss(ctx)) {
667
668         if (strcmp(type, "rsa_pss_keygen_mgf1_md") == 0)
669             return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN,
670                                    EVP_PKEY_CTRL_RSA_MGF1_MD, value);
671
672         if (strcmp(type, "rsa_pss_keygen_md") == 0)
673             return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN,
674                                    EVP_PKEY_CTRL_MD, value);
675
676         if (strcmp(type, "rsa_pss_keygen_saltlen") == 0) {
677             int saltlen = atoi(value);
678
679             return EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(ctx, saltlen);
680         }
681     }
682
683     if (strcmp(type, "rsa_oaep_md") == 0)
684         return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_CRYPT,
685                                EVP_PKEY_CTRL_RSA_OAEP_MD, value);
686
687     if (strcmp(type, "rsa_oaep_label") == 0) {
688         unsigned char *lab;
689         long lablen;
690         int ret;
691
692         lab = OPENSSL_hexstr2buf(value, &lablen);
693         if (!lab)
694             return 0;
695         ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen);
696         if (ret <= 0)
697             OPENSSL_free(lab);
698         return ret;
699     }
700
701     return -2;
702 }
703
704 /* Set PSS parameters when generating a key, if necessary */
705 static int rsa_set_pss_param(RSA *rsa, EVP_PKEY_CTX *ctx)
706 {
707     RSA_PKEY_CTX *rctx = ctx->data;
708
709     if (!pkey_ctx_is_pss(ctx))
710         return 1;
711     /* If all parameters are default values don't set pss */
712     if (rctx->md == NULL && rctx->mgf1md == NULL && rctx->saltlen == -2)
713         return 1;
714     rsa->pss = ossl_rsa_pss_params_create(rctx->md, rctx->mgf1md,
715                                           rctx->saltlen == -2
716                                           ? 0 : rctx->saltlen);
717     if (rsa->pss == NULL)
718         return 0;
719     return 1;
720 }
721
722 static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
723 {
724     RSA *rsa = NULL;
725     RSA_PKEY_CTX *rctx = ctx->data;
726     BN_GENCB *pcb;
727     int ret;
728
729     if (rctx->pub_exp == NULL) {
730         rctx->pub_exp = BN_new();
731         if (rctx->pub_exp == NULL || !BN_set_word(rctx->pub_exp, RSA_F4))
732             return 0;
733     }
734     rsa = RSA_new();
735     if (rsa == NULL)
736         return 0;
737     if (ctx->pkey_gencb) {
738         pcb = BN_GENCB_new();
739         if (pcb == NULL) {
740             RSA_free(rsa);
741             return 0;
742         }
743         evp_pkey_set_cb_translate(pcb, ctx);
744     } else {
745         pcb = NULL;
746     }
747     ret = RSA_generate_multi_prime_key(rsa, rctx->nbits, rctx->primes,
748                                        rctx->pub_exp, pcb);
749     BN_GENCB_free(pcb);
750     if (ret > 0 && !rsa_set_pss_param(rsa, ctx)) {
751         RSA_free(rsa);
752         return 0;
753     }
754     if (ret > 0)
755         EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, rsa);
756     else
757         RSA_free(rsa);
758     return ret;
759 }
760
761 static const EVP_PKEY_METHOD rsa_pkey_meth = {
762     EVP_PKEY_RSA,
763     EVP_PKEY_FLAG_AUTOARGLEN,
764     pkey_rsa_init,
765     pkey_rsa_copy,
766     pkey_rsa_cleanup,
767
768     0, 0,
769
770     0,
771     pkey_rsa_keygen,
772
773     0,
774     pkey_rsa_sign,
775
776     0,
777     pkey_rsa_verify,
778
779     0,
780     pkey_rsa_verifyrecover,
781
782     0, 0, 0, 0,
783
784     0,
785     pkey_rsa_encrypt,
786
787     0,
788     pkey_rsa_decrypt,
789
790     0, 0,
791
792     pkey_rsa_ctrl,
793     pkey_rsa_ctrl_str
794 };
795
796 const EVP_PKEY_METHOD *ossl_rsa_pkey_method(void)
797 {
798     return &rsa_pkey_meth;
799 }
800
801 /*
802  * Called for PSS sign or verify initialisation: checks PSS parameter
803  * sanity and sets any restrictions on key usage.
804  */
805
806 static int pkey_pss_init(EVP_PKEY_CTX *ctx)
807 {
808     RSA *rsa;
809     RSA_PKEY_CTX *rctx = ctx->data;
810     const EVP_MD *md;
811     const EVP_MD *mgf1md;
812     int min_saltlen, max_saltlen;
813
814     /* Should never happen */
815     if (!pkey_ctx_is_pss(ctx))
816         return 0;
817     rsa = ctx->pkey->pkey.rsa;
818     /* If no restrictions just return */
819     if (rsa->pss == NULL)
820         return 1;
821     /* Get and check parameters */
822     if (!ossl_rsa_pss_get_param(rsa->pss, &md, &mgf1md, &min_saltlen))
823         return 0;
824
825     /* See if minimum salt length exceeds maximum possible */
826     max_saltlen = RSA_size(rsa) - EVP_MD_get_size(md);
827     if ((RSA_bits(rsa) & 0x7) == 1)
828         max_saltlen--;
829     if (min_saltlen > max_saltlen) {
830         ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_SALT_LENGTH);
831         return 0;
832     }
833
834     rctx->min_saltlen = min_saltlen;
835
836     /*
837      * Set PSS restrictions as defaults: we can then block any attempt to
838      * use invalid values in pkey_rsa_ctrl
839      */
840
841     rctx->md = md;
842     rctx->mgf1md = mgf1md;
843     rctx->saltlen = min_saltlen;
844
845     return 1;
846 }
847
848 static const EVP_PKEY_METHOD rsa_pss_pkey_meth = {
849     EVP_PKEY_RSA_PSS,
850     EVP_PKEY_FLAG_AUTOARGLEN,
851     pkey_rsa_init,
852     pkey_rsa_copy,
853     pkey_rsa_cleanup,
854
855     0, 0,
856
857     0,
858     pkey_rsa_keygen,
859
860     pkey_pss_init,
861     pkey_rsa_sign,
862
863     pkey_pss_init,
864     pkey_rsa_verify,
865
866     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
867
868     pkey_rsa_ctrl,
869     pkey_rsa_ctrl_str
870 };
871
872 const EVP_PKEY_METHOD *ossl_rsa_pss_pkey_method(void)
873 {
874     return &rsa_pss_pkey_meth;
875 }