style issues
[openssl.git] / crypto / rsa / rsa_ameth.c
1 /*
2  * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/asn1t.h>
13 #include <openssl/x509.h>
14 #include <openssl/bn.h>
15 #include <openssl/cms.h>
16 #include "internal/asn1_int.h"
17 #include "internal/evp_int.h"
18 #include "rsa_locl.h"
19
20 #ifndef OPENSSL_NO_CMS
21 static int rsa_cms_sign(CMS_SignerInfo *si);
22 static int rsa_cms_verify(CMS_SignerInfo *si);
23 static int rsa_cms_decrypt(CMS_RecipientInfo *ri);
24 static int rsa_cms_encrypt(CMS_RecipientInfo *ri);
25 #endif
26
27 static RSA_PSS_PARAMS *rsa_pss_decode(const X509_ALGOR *alg);
28
29 /* Set any parameters associated with pkey */
30 static int rsa_param_encode(const EVP_PKEY *pkey,
31                             ASN1_STRING **pstr, int *pstrtype)
32 {
33     const RSA *rsa = pkey->pkey.rsa;
34
35     *pstr = NULL;
36     /* If RSA it's just NULL type */
37     if (pkey->ameth->pkey_id == EVP_PKEY_RSA) {
38         *pstrtype = V_ASN1_NULL;
39         return 1;
40     }
41     /* If no PSS parameters we omit parameters entirely */
42     if (rsa->pss == NULL) {
43         *pstrtype = V_ASN1_UNDEF;
44         return 1;
45     }
46     /* Encode PSS parameters */
47     if (!ASN1_item_pack(rsa->pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), pstr)) {
48         ASN1_STRING_free(*pstr);
49         *pstr = NULL;
50         return 0;
51     }
52
53     *pstrtype = V_ASN1_SEQUENCE;
54     return 1;
55 }
56 /* Decode any parameters and set them in RSA structure */
57 static int rsa_param_decode(RSA *rsa, const X509_ALGOR *alg)
58 {
59     const ASN1_OBJECT *algoid;
60     const void *algp;
61     int algptype;
62
63     X509_ALGOR_get0(&algoid, &algptype, &algp, alg);
64     if (OBJ_obj2nid(algoid) == EVP_PKEY_RSA)
65         return 1;
66     if (algptype == V_ASN1_UNDEF)
67         return 1;
68     if (algptype != V_ASN1_SEQUENCE) {
69         RSAerr(RSA_F_RSA_PARAM_DECODE, RSA_R_INVALID_PSS_PARAMETERS);
70         return 0;
71     }
72     rsa->pss = rsa_pss_decode(alg);
73     if (rsa->pss == NULL)
74         return 0;
75     return 1;
76 }
77
78 static int rsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
79 {
80     unsigned char *penc = NULL;
81     int penclen;
82     ASN1_STRING *str;
83     int strtype;
84
85     if (!rsa_param_encode(pkey, &str, &strtype))
86         return 0;
87     penclen = i2d_RSAPublicKey(pkey->pkey.rsa, &penc);
88     if (penclen <= 0)
89         return 0;
90     if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(pkey->ameth->pkey_id),
91                                strtype, str, penc, penclen))
92         return 1;
93
94     OPENSSL_free(penc);
95     return 0;
96 }
97
98 static int rsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
99 {
100     const unsigned char *p;
101     int pklen;
102     X509_ALGOR *alg;
103     RSA *rsa = NULL;
104
105     if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &alg, pubkey))
106         return 0;
107     if ((rsa = d2i_RSAPublicKey(NULL, &p, pklen)) == NULL) {
108         RSAerr(RSA_F_RSA_PUB_DECODE, ERR_R_RSA_LIB);
109         return 0;
110     }
111     if (!rsa_param_decode(rsa, alg)) {
112         RSA_free(rsa);
113         return 0;
114     }
115     EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa);
116     return 1;
117 }
118
119 static int rsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
120 {
121     if (BN_cmp(b->pkey.rsa->n, a->pkey.rsa->n) != 0
122         || BN_cmp(b->pkey.rsa->e, a->pkey.rsa->e) != 0)
123         return 0;
124     return 1;
125 }
126
127 static int old_rsa_priv_decode(EVP_PKEY *pkey,
128                                const unsigned char **pder, int derlen)
129 {
130     RSA *rsa;
131
132     if ((rsa = d2i_RSAPrivateKey(NULL, pder, derlen)) == NULL) {
133         RSAerr(RSA_F_OLD_RSA_PRIV_DECODE, ERR_R_RSA_LIB);
134         return 0;
135     }
136     EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa);
137     return 1;
138 }
139
140 static int old_rsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
141 {
142     return i2d_RSAPrivateKey(pkey->pkey.rsa, pder);
143 }
144
145 static int rsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
146 {
147     unsigned char *rk = NULL;
148     int rklen;
149     ASN1_STRING *str;
150     int strtype;
151
152     if (!rsa_param_encode(pkey, &str, &strtype))
153         return 0;
154     rklen = i2d_RSAPrivateKey(pkey->pkey.rsa, &rk);
155
156     if (rklen <= 0) {
157         RSAerr(RSA_F_RSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
158         ASN1_STRING_free(str);
159         return 0;
160     }
161
162     if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(pkey->ameth->pkey_id), 0,
163                          strtype, str, rk, rklen)) {
164         RSAerr(RSA_F_RSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
165         ASN1_STRING_free(str);
166         return 0;
167     }
168
169     return 1;
170 }
171
172 static int rsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
173 {
174     const unsigned char *p;
175     RSA *rsa;
176     int pklen;
177     const X509_ALGOR *alg;
178
179     if (!PKCS8_pkey_get0(NULL, &p, &pklen, &alg, p8))
180         return 0;
181     rsa = d2i_RSAPrivateKey(NULL, &p, pklen);
182     if (rsa == NULL) {
183         RSAerr(RSA_F_RSA_PRIV_DECODE, ERR_R_RSA_LIB);
184         return 0;
185     }
186     if (!rsa_param_decode(rsa, alg)) {
187         RSA_free(rsa);
188         return 0;
189     }
190     EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa);
191     return 1;
192 }
193
194 static int int_rsa_size(const EVP_PKEY *pkey)
195 {
196     return RSA_size(pkey->pkey.rsa);
197 }
198
199 static int rsa_bits(const EVP_PKEY *pkey)
200 {
201     return BN_num_bits(pkey->pkey.rsa->n);
202 }
203
204 static int rsa_security_bits(const EVP_PKEY *pkey)
205 {
206     return RSA_security_bits(pkey->pkey.rsa);
207 }
208
209 static void int_rsa_free(EVP_PKEY *pkey)
210 {
211     RSA_free(pkey->pkey.rsa);
212 }
213
214 static X509_ALGOR *rsa_mgf1_decode(X509_ALGOR *alg)
215 {
216     if (OBJ_obj2nid(alg->algorithm) != NID_mgf1)
217         return NULL;
218     return ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR),
219                                      alg->parameter);
220 }
221
222 static int rsa_pss_param_print(BIO *bp, int pss_key, RSA_PSS_PARAMS *pss,
223                                int indent)
224 {
225     int rv = 0;
226     X509_ALGOR *maskHash = NULL;
227
228     if (!BIO_indent(bp, indent, 128))
229         goto err;
230     if (pss_key) {
231         if (pss == NULL) {
232             if (BIO_puts(bp, "No PSS parameter restrictions\n") <= 0)
233                 return 0;
234             return 1;
235         } else {
236             if (BIO_puts(bp, "PSS parameter restrictions:") <= 0)
237                 return 0;
238         }
239     } else if (pss == NULL) {
240         if (BIO_puts(bp,"(INVALID PSS PARAMETERS)\n") <= 0)
241             return 0;
242         return 1;
243     }
244     if (BIO_puts(bp, "\n") <= 0)
245         goto err;
246     if (pss_key)
247         indent += 2;
248     if (!BIO_indent(bp, indent, 128))
249         goto err;
250     if (BIO_puts(bp, "Hash Algorithm: ") <= 0)
251         goto err;
252
253     if (pss->hashAlgorithm) {
254         if (i2a_ASN1_OBJECT(bp, pss->hashAlgorithm->algorithm) <= 0)
255             goto err;
256     } else if (BIO_puts(bp, "sha1 (default)") <= 0)
257         goto err;
258
259     if (BIO_puts(bp, "\n") <= 0)
260         goto err;
261
262     if (!BIO_indent(bp, indent, 128))
263         goto err;
264
265     if (BIO_puts(bp, "Mask Algorithm: ") <= 0)
266         goto err;
267     if (pss->maskGenAlgorithm) {
268         if (i2a_ASN1_OBJECT(bp, pss->maskGenAlgorithm->algorithm) <= 0)
269             goto err;
270         if (BIO_puts(bp, " with ") <= 0)
271             goto err;
272         maskHash = rsa_mgf1_decode(pss->maskGenAlgorithm);
273         if (maskHash != NULL) {
274             if (i2a_ASN1_OBJECT(bp, maskHash->algorithm) <= 0)
275                 goto err;
276         } else if (BIO_puts(bp, "INVALID") <= 0)
277             goto err;
278     } else if (BIO_puts(bp, "mgf1 with sha1 (default)") <= 0)
279         goto err;
280     BIO_puts(bp, "\n");
281
282     if (!BIO_indent(bp, indent, 128))
283         goto err;
284     if (BIO_printf(bp, "%s Salt Length: 0x", pss_key ? "Minimum" : "") <= 0)
285         goto err;
286     if (pss->saltLength) {
287         if (i2a_ASN1_INTEGER(bp, pss->saltLength) <= 0)
288             goto err;
289     } else if (BIO_puts(bp, "14 (default)") <= 0)
290         goto err;
291     BIO_puts(bp, "\n");
292
293     if (!BIO_indent(bp, indent, 128))
294         goto err;
295     if (BIO_puts(bp, "Trailer Field: 0x") <= 0)
296         goto err;
297     if (pss->trailerField) {
298         if (i2a_ASN1_INTEGER(bp, pss->trailerField) <= 0)
299             goto err;
300     } else if (BIO_puts(bp, "BC (default)") <= 0)
301         goto err;
302     BIO_puts(bp, "\n");
303
304     rv = 1;
305
306  err:
307     X509_ALGOR_free(maskHash);
308     return rv;
309
310 }
311
312 static int pkey_rsa_print(BIO *bp, const EVP_PKEY *pkey, int off, int priv)
313 {
314     const RSA *x = pkey->pkey.rsa;
315     char *str;
316     const char *s;
317     int ret = 0, mod_len = 0;
318
319     if (x->n != NULL)
320         mod_len = BN_num_bits(x->n);
321
322     if (!BIO_indent(bp, off, 128))
323         goto err;
324
325     if (BIO_printf(bp, "%s ", pkey_is_pss(pkey) ?  "RSA-PSS" : "RSA") <= 0)
326         goto err;
327
328     if (priv && x->d) {
329         if (BIO_printf(bp, "Private-Key: (%d bit)\n", mod_len) <= 0)
330             goto err;
331         str = "modulus:";
332         s = "publicExponent:";
333     } else {
334         if (BIO_printf(bp, "Public-Key: (%d bit)\n", mod_len) <= 0)
335             goto err;
336         str = "Modulus:";
337         s = "Exponent:";
338     }
339     if (!ASN1_bn_print(bp, str, x->n, NULL, off))
340         goto err;
341     if (!ASN1_bn_print(bp, s, x->e, NULL, off))
342         goto err;
343     if (priv) {
344         if (!ASN1_bn_print(bp, "privateExponent:", x->d, NULL, off))
345             goto err;
346         if (!ASN1_bn_print(bp, "prime1:", x->p, NULL, off))
347             goto err;
348         if (!ASN1_bn_print(bp, "prime2:", x->q, NULL, off))
349             goto err;
350         if (!ASN1_bn_print(bp, "exponent1:", x->dmp1, NULL, off))
351             goto err;
352         if (!ASN1_bn_print(bp, "exponent2:", x->dmq1, NULL, off))
353             goto err;
354         if (!ASN1_bn_print(bp, "coefficient:", x->iqmp, NULL, off))
355             goto err;
356     }
357     if (pkey_is_pss(pkey) && !rsa_pss_param_print(bp, 1, x->pss, off))
358         goto err;
359     ret = 1;
360  err:
361     return ret;
362 }
363
364 static int rsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
365                          ASN1_PCTX *ctx)
366 {
367     return pkey_rsa_print(bp, pkey, indent, 0);
368 }
369
370 static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
371                           ASN1_PCTX *ctx)
372 {
373     return pkey_rsa_print(bp, pkey, indent, 1);
374 }
375
376 static RSA_PSS_PARAMS *rsa_pss_decode(const X509_ALGOR *alg)
377 {
378     RSA_PSS_PARAMS *pss;
379
380     pss = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(RSA_PSS_PARAMS),
381                                     alg->parameter);
382
383     if (pss == NULL)
384         return NULL;
385
386     if (pss->maskGenAlgorithm != NULL) {
387         pss->maskHash = rsa_mgf1_decode(pss->maskGenAlgorithm);
388         if (pss->maskHash == NULL) {
389             RSA_PSS_PARAMS_free(pss);
390             return NULL;
391         }
392     }
393
394     return pss;
395 }
396
397 static int rsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
398                          const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx)
399 {
400     if (OBJ_obj2nid(sigalg->algorithm) == EVP_PKEY_RSA_PSS) {
401         int rv;
402         RSA_PSS_PARAMS *pss = rsa_pss_decode(sigalg);
403         rv = rsa_pss_param_print(bp, 0, pss, indent);
404         RSA_PSS_PARAMS_free(pss);
405         if (!rv)
406             return 0;
407     } else if (!sig && BIO_puts(bp, "\n") <= 0) {
408         return 0;
409     }
410     if (sig)
411         return X509_signature_dump(bp, sig, indent);
412     return 1;
413 }
414
415 static int rsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
416 {
417     X509_ALGOR *alg = NULL;
418
419     switch (op) {
420
421     case ASN1_PKEY_CTRL_PKCS7_SIGN:
422         if (arg1 == 0)
423             PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, NULL, &alg);
424         break;
425
426     case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
427         if (pkey_is_pss(pkey))
428             return -2;
429         if (arg1 == 0)
430             PKCS7_RECIP_INFO_get0_alg(arg2, &alg);
431         break;
432 #ifndef OPENSSL_NO_CMS
433     case ASN1_PKEY_CTRL_CMS_SIGN:
434         if (arg1 == 0)
435             return rsa_cms_sign(arg2);
436         else if (arg1 == 1)
437             return rsa_cms_verify(arg2);
438         break;
439
440     case ASN1_PKEY_CTRL_CMS_ENVELOPE:
441         if (pkey_is_pss(pkey))
442             return -2;
443         if (arg1 == 0)
444             return rsa_cms_encrypt(arg2);
445         else if (arg1 == 1)
446             return rsa_cms_decrypt(arg2);
447         break;
448
449     case ASN1_PKEY_CTRL_CMS_RI_TYPE:
450         if (pkey_is_pss(pkey))
451             return -2;
452         *(int *)arg2 = CMS_RECIPINFO_TRANS;
453         return 1;
454 #endif
455
456     case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
457         *(int *)arg2 = NID_sha256;
458         return 1;
459
460     default:
461         return -2;
462
463     }
464
465     if (alg)
466         X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
467
468     return 1;
469
470 }
471
472 /* allocate and set algorithm ID from EVP_MD, default SHA1 */
473 static int rsa_md_to_algor(X509_ALGOR **palg, const EVP_MD *md)
474 {
475     if (md == NULL || EVP_MD_type(md) == NID_sha1)
476         return 1;
477     *palg = X509_ALGOR_new();
478     if (*palg == NULL)
479         return 0;
480     X509_ALGOR_set_md(*palg, md);
481     return 1;
482 }
483
484 /* Allocate and set MGF1 algorithm ID from EVP_MD */
485 static int rsa_md_to_mgf1(X509_ALGOR **palg, const EVP_MD *mgf1md)
486 {
487     X509_ALGOR *algtmp = NULL;
488     ASN1_STRING *stmp = NULL;
489
490     *palg = NULL;
491     if (mgf1md == NULL || EVP_MD_type(mgf1md) == NID_sha1)
492         return 1;
493     /* need to embed algorithm ID inside another */
494     if (!rsa_md_to_algor(&algtmp, mgf1md))
495         goto err;
496     if (!ASN1_item_pack(algtmp, ASN1_ITEM_rptr(X509_ALGOR), &stmp))
497          goto err;
498     *palg = X509_ALGOR_new();
499     if (*palg == NULL)
500         goto err;
501     X509_ALGOR_set0(*palg, OBJ_nid2obj(NID_mgf1), V_ASN1_SEQUENCE, stmp);
502     stmp = NULL;
503  err:
504     ASN1_STRING_free(stmp);
505     X509_ALGOR_free(algtmp);
506     if (*palg)
507         return 1;
508     return 0;
509 }
510
511 /* convert algorithm ID to EVP_MD, default SHA1 */
512 static const EVP_MD *rsa_algor_to_md(X509_ALGOR *alg)
513 {
514     const EVP_MD *md;
515
516     if (!alg)
517         return EVP_sha1();
518     md = EVP_get_digestbyobj(alg->algorithm);
519     if (md == NULL)
520         RSAerr(RSA_F_RSA_ALGOR_TO_MD, RSA_R_UNKNOWN_DIGEST);
521     return md;
522 }
523
524 /*
525  * Convert EVP_PKEY_CTX in PSS mode into corresponding algorithm parameter,
526  * suitable for setting an AlgorithmIdentifier.
527  */
528
529 static RSA_PSS_PARAMS *rsa_ctx_to_pss(EVP_PKEY_CTX *pkctx)
530 {
531     const EVP_MD *sigmd, *mgf1md;
532     EVP_PKEY *pk = EVP_PKEY_CTX_get0_pkey(pkctx);
533     int saltlen;
534
535     if (EVP_PKEY_CTX_get_signature_md(pkctx, &sigmd) <= 0)
536         return NULL;
537     if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0)
538         return NULL;
539     if (!EVP_PKEY_CTX_get_rsa_pss_saltlen(pkctx, &saltlen))
540         return NULL;
541     if (saltlen == -1)
542         saltlen = EVP_MD_size(sigmd);
543     else if (saltlen == -2) {
544         saltlen = EVP_PKEY_size(pk) - EVP_MD_size(sigmd) - 2;
545         if (((EVP_PKEY_bits(pk) - 1) & 0x7) == 0)
546             saltlen--;
547     }
548
549     return rsa_pss_params_create(sigmd, mgf1md, saltlen);
550 }
551
552 RSA_PSS_PARAMS *rsa_pss_params_create(const EVP_MD *sigmd,
553                                       const EVP_MD *mgf1md, int saltlen)
554 {
555     RSA_PSS_PARAMS *pss = RSA_PSS_PARAMS_new();
556
557     if (pss == NULL)
558         goto err;
559     if (saltlen != 20) {
560         pss->saltLength = ASN1_INTEGER_new();
561         if (pss->saltLength == NULL)
562             goto err;
563         if (!ASN1_INTEGER_set(pss->saltLength, saltlen))
564             goto err;
565     }
566     if (!rsa_md_to_algor(&pss->hashAlgorithm, sigmd))
567         goto err;
568     if (mgf1md == NULL)
569         mgf1md = sigmd;
570     if (!rsa_md_to_mgf1(&pss->maskGenAlgorithm, mgf1md))
571         goto err;
572     return pss;
573  err:
574     RSA_PSS_PARAMS_free(pss);
575     return NULL;
576 }
577
578 static ASN1_STRING *rsa_ctx_to_pss_string(EVP_PKEY_CTX *pkctx)
579 {
580     RSA_PSS_PARAMS *pss = rsa_ctx_to_pss(pkctx);
581     ASN1_STRING *os = NULL;
582
583     if (pss == NULL)
584         return NULL;
585
586     if (!ASN1_item_pack(pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), &os)) {
587         ASN1_STRING_free(os);
588         os = NULL;
589     }
590     RSA_PSS_PARAMS_free(pss);
591     return os;
592 }
593
594 /*
595  * From PSS AlgorithmIdentifier set public key parameters. If pkey isn't NULL
596  * then the EVP_MD_CTX is setup and initialised. If it is NULL parameters are
597  * passed to pkctx instead.
598  */
599
600 static int rsa_pss_to_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pkctx,
601                           X509_ALGOR *sigalg, EVP_PKEY *pkey)
602 {
603     int rv = -1;
604     int saltlen;
605     const EVP_MD *mgf1md = NULL, *md = NULL;
606     RSA_PSS_PARAMS *pss;
607
608     /* Sanity check: make sure it is PSS */
609     if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS) {
610         RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_UNSUPPORTED_SIGNATURE_TYPE);
611         return -1;
612     }
613     /* Decode PSS parameters */
614     pss = rsa_pss_decode(sigalg);
615
616     if (!rsa_pss_get_param(pss, &md, &mgf1md, &saltlen)) {
617         RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_INVALID_PSS_PARAMETERS);
618         goto err;
619     }
620
621     /* We have all parameters now set up context */
622     if (pkey) {
623         if (!EVP_DigestVerifyInit(ctx, &pkctx, md, NULL, pkey))
624             goto err;
625     } else {
626         const EVP_MD *checkmd;
627         if (EVP_PKEY_CTX_get_signature_md(pkctx, &checkmd) <= 0)
628             goto err;
629         if (EVP_MD_type(md) != EVP_MD_type(checkmd)) {
630             RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_DIGEST_DOES_NOT_MATCH);
631             goto err;
632         }
633     }
634
635     if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_PSS_PADDING) <= 0)
636         goto err;
637
638     if (EVP_PKEY_CTX_set_rsa_pss_saltlen(pkctx, saltlen) <= 0)
639         goto err;
640
641     if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0)
642         goto err;
643     /* Carry on */
644     rv = 1;
645
646  err:
647     RSA_PSS_PARAMS_free(pss);
648     return rv;
649 }
650
651 int rsa_pss_get_param(const RSA_PSS_PARAMS *pss, const EVP_MD **pmd,
652                       const EVP_MD **pmgf1md, int *psaltlen)
653 {
654     if (pss == NULL)
655         return 0;
656     *pmd = rsa_algor_to_md(pss->hashAlgorithm);
657     if (*pmd == NULL)
658         return 0;
659     *pmgf1md = rsa_algor_to_md(pss->maskHash);
660     if (*pmgf1md == NULL)
661         return 0;
662     if (pss->saltLength) {
663         *psaltlen = ASN1_INTEGER_get(pss->saltLength);
664         if (*psaltlen < 0) {
665             RSAerr(RSA_F_RSA_PSS_GET_PARAM, RSA_R_INVALID_SALT_LENGTH);
666             return 0;
667         }
668     } else
669         *psaltlen = 20;
670
671     /*
672      * low-level routines support only trailer field 0xbc (value 1) and
673      * PKCS#1 says we should reject any other value anyway.
674      */
675     if (pss->trailerField && ASN1_INTEGER_get(pss->trailerField) != 1) {
676         RSAerr(RSA_F_RSA_PSS_GET_PARAM, RSA_R_INVALID_TRAILER);
677         return 0;
678     }
679
680     return 1;
681 }
682
683 #ifndef OPENSSL_NO_CMS
684 static int rsa_cms_verify(CMS_SignerInfo *si)
685 {
686     int nid, nid2;
687     X509_ALGOR *alg;
688     EVP_PKEY_CTX *pkctx = CMS_SignerInfo_get0_pkey_ctx(si);
689
690     CMS_SignerInfo_get0_algs(si, NULL, NULL, NULL, &alg);
691     nid = OBJ_obj2nid(alg->algorithm);
692     if (nid == EVP_PKEY_RSA_PSS)
693         return rsa_pss_to_ctx(NULL, pkctx, alg, NULL);
694     /* Only PSS allowed for PSS keys */
695     if (pkey_ctx_is_pss(pkctx)) {
696         RSAerr(RSA_F_RSA_CMS_VERIFY, RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
697         return 0;
698     }
699     if (nid == NID_rsaEncryption)
700         return 1;
701     /* Workaround for some implementation that use a signature OID */
702     if (OBJ_find_sigid_algs(nid, NULL, &nid2)) {
703         if (nid2 == NID_rsaEncryption)
704             return 1;
705     }
706     return 0;
707 }
708 #endif
709
710 /*
711  * Customised RSA item verification routine. This is called when a signature
712  * is encountered requiring special handling. We currently only handle PSS.
713  */
714
715 static int rsa_item_verify(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
716                            X509_ALGOR *sigalg, ASN1_BIT_STRING *sig,
717                            EVP_PKEY *pkey)
718 {
719     /* Sanity check: make sure it is PSS */
720     if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS) {
721         RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNSUPPORTED_SIGNATURE_TYPE);
722         return -1;
723     }
724     if (rsa_pss_to_ctx(ctx, NULL, sigalg, pkey) > 0) {
725         /* Carry on */
726         return 2;
727     }
728     return -1;
729 }
730
731 #ifndef OPENSSL_NO_CMS
732 static int rsa_cms_sign(CMS_SignerInfo *si)
733 {
734     int pad_mode = RSA_PKCS1_PADDING;
735     X509_ALGOR *alg;
736     EVP_PKEY_CTX *pkctx = CMS_SignerInfo_get0_pkey_ctx(si);
737     ASN1_STRING *os = NULL;
738
739     CMS_SignerInfo_get0_algs(si, NULL, NULL, NULL, &alg);
740     if (pkctx) {
741         if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
742             return 0;
743     }
744     if (pad_mode == RSA_PKCS1_PADDING) {
745         X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
746         return 1;
747     }
748     /* We don't support it */
749     if (pad_mode != RSA_PKCS1_PSS_PADDING)
750         return 0;
751     os = rsa_ctx_to_pss_string(pkctx);
752     if (!os)
753         return 0;
754     X509_ALGOR_set0(alg, OBJ_nid2obj(EVP_PKEY_RSA_PSS), V_ASN1_SEQUENCE, os);
755     return 1;
756 }
757 #endif
758
759 static int rsa_item_sign(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
760                          X509_ALGOR *alg1, X509_ALGOR *alg2,
761                          ASN1_BIT_STRING *sig)
762 {
763     int pad_mode;
764     EVP_PKEY_CTX *pkctx = EVP_MD_CTX_pkey_ctx(ctx);
765
766     if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
767         return 0;
768     if (pad_mode == RSA_PKCS1_PADDING)
769         return 2;
770     if (pad_mode == RSA_PKCS1_PSS_PADDING) {
771         ASN1_STRING *os1 = NULL;
772         os1 = rsa_ctx_to_pss_string(pkctx);
773         if (!os1)
774             return 0;
775         /* Duplicate parameters if we have to */
776         if (alg2) {
777             ASN1_STRING *os2 = ASN1_STRING_dup(os1);
778             if (!os2) {
779                 ASN1_STRING_free(os1);
780                 return 0;
781             }
782             X509_ALGOR_set0(alg2, OBJ_nid2obj(EVP_PKEY_RSA_PSS),
783                             V_ASN1_SEQUENCE, os2);
784         }
785         X509_ALGOR_set0(alg1, OBJ_nid2obj(EVP_PKEY_RSA_PSS),
786                         V_ASN1_SEQUENCE, os1);
787         return 3;
788     }
789     return 2;
790 }
791
792 #ifndef OPENSSL_NO_CMS
793 static RSA_OAEP_PARAMS *rsa_oaep_decode(const X509_ALGOR *alg)
794 {
795     RSA_OAEP_PARAMS *oaep;
796
797     oaep = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(RSA_OAEP_PARAMS),
798                                      alg->parameter);
799
800     if (oaep == NULL)
801         return NULL;
802
803     if (oaep->maskGenFunc != NULL) {
804         oaep->maskHash = rsa_mgf1_decode(oaep->maskGenFunc);
805         if (oaep->maskHash == NULL) {
806             RSA_OAEP_PARAMS_free(oaep);
807             return NULL;
808         }
809     }
810     return oaep;
811 }
812
813 static int rsa_cms_decrypt(CMS_RecipientInfo *ri)
814 {
815     EVP_PKEY_CTX *pkctx;
816     X509_ALGOR *cmsalg;
817     int nid;
818     int rv = -1;
819     unsigned char *label = NULL;
820     int labellen = 0;
821     const EVP_MD *mgf1md = NULL, *md = NULL;
822     RSA_OAEP_PARAMS *oaep;
823
824     pkctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
825     if (pkctx == NULL)
826         return 0;
827     if (!CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &cmsalg))
828         return -1;
829     nid = OBJ_obj2nid(cmsalg->algorithm);
830     if (nid == NID_rsaEncryption)
831         return 1;
832     if (nid != NID_rsaesOaep) {
833         RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_UNSUPPORTED_ENCRYPTION_TYPE);
834         return -1;
835     }
836     /* Decode OAEP parameters */
837     oaep = rsa_oaep_decode(cmsalg);
838
839     if (oaep == NULL) {
840         RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_INVALID_OAEP_PARAMETERS);
841         goto err;
842     }
843
844     mgf1md = rsa_algor_to_md(oaep->maskHash);
845     if (mgf1md == NULL)
846         goto err;
847     md = rsa_algor_to_md(oaep->hashFunc);
848     if (md == NULL)
849         goto err;
850
851     if (oaep->pSourceFunc != NULL) {
852         X509_ALGOR *plab = oaep->pSourceFunc;
853
854         if (OBJ_obj2nid(plab->algorithm) != NID_pSpecified) {
855             RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_UNSUPPORTED_LABEL_SOURCE);
856             goto err;
857         }
858         if (plab->parameter->type != V_ASN1_OCTET_STRING) {
859             RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_INVALID_LABEL);
860             goto err;
861         }
862
863         label = plab->parameter->value.octet_string->data;
864         /* Stop label being freed when OAEP parameters are freed */
865         plab->parameter->value.octet_string->data = NULL;
866         labellen = plab->parameter->value.octet_string->length;
867     }
868
869     if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_OAEP_PADDING) <= 0)
870         goto err;
871     if (EVP_PKEY_CTX_set_rsa_oaep_md(pkctx, md) <= 0)
872         goto err;
873     if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0)
874         goto err;
875     if (EVP_PKEY_CTX_set0_rsa_oaep_label(pkctx, label, labellen) <= 0)
876         goto err;
877     /* Carry on */
878     rv = 1;
879
880  err:
881     RSA_OAEP_PARAMS_free(oaep);
882     return rv;
883 }
884
885 static int rsa_cms_encrypt(CMS_RecipientInfo *ri)
886 {
887     const EVP_MD *md, *mgf1md;
888     RSA_OAEP_PARAMS *oaep = NULL;
889     ASN1_STRING *os = NULL;
890     X509_ALGOR *alg;
891     EVP_PKEY_CTX *pkctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
892     int pad_mode = RSA_PKCS1_PADDING, rv = 0, labellen;
893     unsigned char *label;
894
895     CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &alg);
896     if (pkctx) {
897         if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
898             return 0;
899     }
900     if (pad_mode == RSA_PKCS1_PADDING) {
901         X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
902         return 1;
903     }
904     /* Not supported */
905     if (pad_mode != RSA_PKCS1_OAEP_PADDING)
906         return 0;
907     if (EVP_PKEY_CTX_get_rsa_oaep_md(pkctx, &md) <= 0)
908         goto err;
909     if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0)
910         goto err;
911     labellen = EVP_PKEY_CTX_get0_rsa_oaep_label(pkctx, &label);
912     if (labellen < 0)
913         goto err;
914     oaep = RSA_OAEP_PARAMS_new();
915     if (oaep == NULL)
916         goto err;
917     if (!rsa_md_to_algor(&oaep->hashFunc, md))
918         goto err;
919     if (!rsa_md_to_mgf1(&oaep->maskGenFunc, mgf1md))
920         goto err;
921     if (labellen > 0) {
922         ASN1_OCTET_STRING *los;
923         oaep->pSourceFunc = X509_ALGOR_new();
924         if (oaep->pSourceFunc == NULL)
925             goto err;
926         los = ASN1_OCTET_STRING_new();
927         if (los == NULL)
928             goto err;
929         if (!ASN1_OCTET_STRING_set(los, label, labellen)) {
930             ASN1_OCTET_STRING_free(los);
931             goto err;
932         }
933         X509_ALGOR_set0(oaep->pSourceFunc, OBJ_nid2obj(NID_pSpecified),
934                         V_ASN1_OCTET_STRING, los);
935     }
936     /* create string with pss parameter encoding. */
937     if (!ASN1_item_pack(oaep, ASN1_ITEM_rptr(RSA_OAEP_PARAMS), &os))
938          goto err;
939     X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaesOaep), V_ASN1_SEQUENCE, os);
940     os = NULL;
941     rv = 1;
942  err:
943     RSA_OAEP_PARAMS_free(oaep);
944     ASN1_STRING_free(os);
945     return rv;
946 }
947 #endif
948
949 const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[2] = {
950     {
951      EVP_PKEY_RSA,
952      EVP_PKEY_RSA,
953      ASN1_PKEY_SIGPARAM_NULL,
954
955      "RSA",
956      "OpenSSL RSA method",
957
958      rsa_pub_decode,
959      rsa_pub_encode,
960      rsa_pub_cmp,
961      rsa_pub_print,
962
963      rsa_priv_decode,
964      rsa_priv_encode,
965      rsa_priv_print,
966
967      int_rsa_size,
968      rsa_bits,
969      rsa_security_bits,
970
971      0, 0, 0, 0, 0, 0,
972
973      rsa_sig_print,
974      int_rsa_free,
975      rsa_pkey_ctrl,
976      old_rsa_priv_decode,
977      old_rsa_priv_encode,
978      rsa_item_verify,
979      rsa_item_sign},
980
981     {
982      EVP_PKEY_RSA2,
983      EVP_PKEY_RSA,
984      ASN1_PKEY_ALIAS}
985 };
986
987 const EVP_PKEY_ASN1_METHOD rsa_pss_asn1_meth = {
988      EVP_PKEY_RSA_PSS,
989      EVP_PKEY_RSA_PSS,
990      ASN1_PKEY_SIGPARAM_NULL,
991
992      "RSA-PSS",
993      "OpenSSL RSA-PSS method",
994
995      rsa_pub_decode,
996      rsa_pub_encode,
997      rsa_pub_cmp,
998      rsa_pub_print,
999
1000      rsa_priv_decode,
1001      rsa_priv_encode,
1002      rsa_priv_print,
1003
1004      int_rsa_size,
1005      rsa_bits,
1006      rsa_security_bits,
1007
1008      0, 0, 0, 0, 0, 0,
1009
1010      rsa_sig_print,
1011      int_rsa_free,
1012      rsa_pkey_ctrl,
1013      0, 0,
1014      rsa_item_verify,
1015      rsa_item_sign,
1016 };