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