9dd0d4c7c708faac0527cf170b2d81a0626a860d
[openssl.git] / crypto / rsa / rsa_pmeth.c
1 /* crypto/rsa/rsa_pmeth.c */
2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) 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 #include "evp_locl.h"
67 #include "rsa_locl.h"
68
69 /* RSA pkey context structure */
70
71 typedef struct
72         {
73         /* Key gen parameters */
74         int nbits;
75         BIGNUM *pub_exp;
76         /* Keygen callback info */
77         int gentmp[2];
78         /* RSA padding mode */
79         int pad_mode;
80         /* message digest */
81         const EVP_MD *md;
82         /* PSS/OAEP salt length */
83         int saltlen;
84         /* Temp buffer */
85         unsigned char *tbuf;
86         } RSA_PKEY_CTX;
87
88 static int pkey_rsa_init(EVP_PKEY_CTX *ctx)
89         {
90         RSA_PKEY_CTX *rctx;
91         rctx = OPENSSL_malloc(sizeof(RSA_PKEY_CTX));
92         if (!rctx)
93                 return 0;
94         rctx->nbits = 1024;
95         rctx->pub_exp = NULL;
96         rctx->pad_mode = RSA_PKCS1_PADDING;
97         rctx->md = NULL;
98         rctx->tbuf = NULL;
99
100         rctx->saltlen = -2;
101
102         ctx->data = rctx;
103         ctx->keygen_info = rctx->gentmp;
104         ctx->keygen_info_count = 2;
105         
106         return 1;
107         }
108
109 static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
110         {
111         RSA_PKEY_CTX *dctx, *sctx;
112         if (!pkey_rsa_init(dst))
113                 return 0;
114         sctx = src->data;
115         dctx = dst->data;
116         dctx->nbits = sctx->nbits;
117         if (sctx->pub_exp)
118                 {
119                 dctx->pub_exp = BN_dup(sctx->pub_exp);
120                 if (!dctx->pub_exp)
121                         return 0;
122                 }
123         dctx->pad_mode = sctx->pad_mode;
124         dctx->md = sctx->md;
125         return 1;
126         }
127
128 static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk)
129         {
130         if (ctx->tbuf)
131                 return 1;
132         ctx->tbuf = OPENSSL_malloc(EVP_PKEY_size(pk->pkey));
133         if (!ctx->tbuf)
134                 return 0;
135         return 1;
136         }
137
138 static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx)
139         {
140         RSA_PKEY_CTX *rctx = ctx->data;
141         if (rctx)
142                 {
143                 if (rctx->pub_exp)
144                         BN_free(rctx->pub_exp);
145                 if (rctx->tbuf)
146                         OPENSSL_free(rctx->tbuf);
147                 OPENSSL_free(rctx);
148                 }
149         }
150
151 static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
152                                         const unsigned char *tbs, size_t tbslen)
153         {
154         int ret;
155         RSA_PKEY_CTX *rctx = ctx->data;
156         RSA *rsa = ctx->pkey->pkey.rsa;
157
158         if (rctx->md)
159                 {
160                 if (tbslen != (size_t)EVP_MD_size(rctx->md))
161                         {
162                         RSAerr(RSA_F_PKEY_RSA_SIGN,
163                                         RSA_R_INVALID_DIGEST_LENGTH);
164                         return -1;
165                         }
166                 if (rctx->pad_mode == RSA_X931_PADDING)
167                         {
168                         if (!setup_tbuf(rctx, ctx))
169                                 return -1;
170                         memcpy(rctx->tbuf, tbs, tbslen);
171                         rctx->tbuf[tbslen] =
172                                 RSA_X931_hash_id(EVP_MD_type(rctx->md));
173                         ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf,
174                                                 sig, rsa, RSA_X931_PADDING);
175                         }
176                 else if (rctx->pad_mode == RSA_PKCS1_PADDING)
177                         {
178                         unsigned int sltmp;
179                         ret = RSA_sign(EVP_MD_type(rctx->md),
180                                                 tbs, tbslen, sig, &sltmp, rsa);
181                         if (ret <= 0)
182                                 return ret;
183                         ret = sltmp;
184                         }
185                 else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING)
186                         {
187                         if (!setup_tbuf(rctx, ctx))
188                                 return -1;
189                         if (!RSA_padding_add_PKCS1_PSS(rsa, rctx->tbuf, tbs,
190                                                 rctx->md, rctx->saltlen))
191                                 return -1;
192                         ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
193                                                 sig, rsa, RSA_NO_PADDING);
194                         }
195                 else
196                         return -1;
197                 }
198         else
199                 ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa,
200                                                         rctx->pad_mode);
201         if (ret < 0)
202                 return ret;
203         *siglen = ret;
204         return 1;
205         }
206
207
208 static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
209                                         unsigned char *rout, size_t *routlen,
210                                         const unsigned char *sig, size_t siglen)
211         {
212         int ret;
213         RSA_PKEY_CTX *rctx = ctx->data;
214
215         if (rctx->md)
216                 {
217                 if (rctx->pad_mode == RSA_X931_PADDING)
218                         {
219                         if (!setup_tbuf(rctx, ctx))
220                                 return -1;
221                         ret = RSA_public_decrypt(siglen, sig,
222                                                 rctx->tbuf, ctx->pkey->pkey.rsa,
223                                                 RSA_X931_PADDING);
224                         if (ret < 1)
225                                 return 0;
226                         ret--;
227                         if (rctx->tbuf[ret] !=
228                                 RSA_X931_hash_id(EVP_MD_type(rctx->md)))
229                                 {
230                                 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
231                                                 RSA_R_ALGORITHM_MISMATCH);
232                                 return 0;
233                                 }
234                         if (ret != EVP_MD_size(rctx->md))
235                                 {
236                                 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
237                                         RSA_R_INVALID_DIGEST_LENGTH);
238                                 return 0;
239                                 }
240                         if (rout)
241                                 memcpy(rout, rctx->tbuf, ret);
242                         }
243                 else if (rctx->pad_mode == RSA_PKCS1_PADDING)
244                         {
245                         size_t sltmp;
246                         ret = int_rsa_verify(EVP_MD_type(rctx->md),
247                                                 NULL, 0, rout, &sltmp,
248                                         sig, siglen, ctx->pkey->pkey.rsa);
249                         ret = sltmp;
250                         }
251                 else
252                         return -1;
253                 }
254         else
255                 ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa,
256                                                         rctx->pad_mode);
257         if (ret < 0)
258                 return ret;
259         *routlen = ret;
260         return 1;
261         }
262
263 static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
264                                         const unsigned char *sig, size_t siglen,
265                                         const unsigned char *tbs, size_t tbslen)
266         {
267         RSA_PKEY_CTX *rctx = ctx->data;
268         RSA *rsa = ctx->pkey->pkey.rsa;
269         size_t rslen;
270         if (rctx->md)
271                 {
272                 if (rctx->pad_mode == RSA_PKCS1_PADDING)
273                         return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen,
274                                         sig, siglen, rsa);
275                 if (rctx->pad_mode == RSA_X931_PADDING)
276                         {
277                         if (pkey_rsa_verifyrecover(ctx, NULL, &rslen,
278                                         sig, siglen) <= 0)
279                                 return 0;
280                         }
281                 else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING)
282                         {
283                         int ret;
284                         if (!setup_tbuf(rctx, ctx))
285                                 return -1;
286                         ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
287                                                         rsa, RSA_NO_PADDING);
288                         if (ret <= 0)
289                                 return 0;
290                         ret = RSA_verify_PKCS1_PSS(rsa, tbs, rctx->md,
291                                                 rctx->tbuf, rctx->saltlen);
292                         if (ret <= 0)
293                                 return 0;
294                         return 1;
295                         }
296                 else
297                         return -1;
298                 }
299         else
300                 {
301                 if (!setup_tbuf(rctx, ctx))
302                         return -1;
303                 rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
304                                                 rsa, rctx->pad_mode);
305                 if (rslen == 0)
306                         return 0;
307                 }
308
309         if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
310                 return 0;
311
312         return 1;
313                         
314         }
315         
316
317 static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
318                                         unsigned char *out, size_t *outlen,
319                                         const unsigned char *in, size_t inlen)
320         {
321         int ret;
322         RSA_PKEY_CTX *rctx = ctx->data;
323         ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
324                                                         rctx->pad_mode);
325         if (ret < 0)
326                 return ret;
327         *outlen = ret;
328         return 1;
329         }
330
331 static int pkey_rsa_decrypt(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         ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
338                                                         rctx->pad_mode);
339         if (ret < 0)
340                 return ret;
341         *outlen = ret;
342         return 1;
343         }
344
345 static int check_padding_md(const EVP_MD *md, int padding)
346         {
347         if (!md)
348                 return 1;
349
350         if (padding == RSA_NO_PADDING)
351                 {
352                 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE);
353                 return 0;
354                 }
355
356         if (padding == RSA_X931_PADDING)
357                 {
358                 if (RSA_X931_hash_id(EVP_MD_type(md)) == -1)
359                         {
360                         RSAerr(RSA_F_CHECK_PADDING_MD,
361                                                 RSA_R_INVALID_X931_DIGEST);
362                         return 0;
363                         }
364                 return 1;
365                 }
366
367         return 1;
368         }
369                         
370
371 static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
372         {
373         RSA_PKEY_CTX *rctx = ctx->data;
374         switch (type)
375                 {
376                 case EVP_PKEY_CTRL_RSA_PADDING:
377                 if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING))
378                         {
379                         if (!check_padding_md(rctx->md, p1))
380                                 return 0;
381                         if (p1 == RSA_PKCS1_PSS_PADDING) 
382                                 {
383                                 if (!(ctx->operation &
384                                      (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
385                                         goto bad_pad;
386                                 if (!rctx->md)
387                                         rctx->md = EVP_sha1();
388                                 }
389                         if (p1 == RSA_PKCS1_OAEP_PADDING) 
390                                 {
391                                 if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
392                                         goto bad_pad;
393                                 if (!rctx->md)
394                                         rctx->md = EVP_sha1();
395                                 }
396                         rctx->pad_mode = p1;
397                         return 1;
398                         }
399                 bad_pad:
400                 RSAerr(RSA_F_PKEY_RSA_CTRL,
401                                 RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
402                 return -2;
403
404                 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
405                 if (p1 < -2)
406                         return -2;
407                 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING)
408                         {
409                         RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
410                         return -2;
411                         }
412                 rctx->saltlen = p1;
413                 return 1;
414
415                 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
416                 if (p1 < 256)
417                         {
418                         RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_KEYBITS);
419                         return -2;
420                         }
421                 rctx->nbits = p1;
422                 return 1;
423
424                 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
425                 if (!p2)
426                         return -2;
427                 rctx->pub_exp = p2;
428                 return 1;
429
430                 case EVP_PKEY_CTRL_MD:
431                 if (!check_padding_md(p2, rctx->pad_mode))
432                         return 0;
433                 rctx->md = p2;
434                 return 1;
435
436                 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
437                 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
438                 case EVP_PKEY_CTRL_PKCS7_SIGN:
439 #ifndef OPENSSL_NO_CMS
440                 case EVP_PKEY_CTRL_CMS_ENCRYPT:
441                 case EVP_PKEY_CTRL_CMS_DECRYPT:
442                 case EVP_PKEY_CTRL_CMS_SIGN:
443 #endif
444                 return 1;
445                 case EVP_PKEY_CTRL_PEER_KEY:
446                         RSAerr(RSA_F_PKEY_RSA_CTRL,
447                         RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
448                         return -2;      
449
450                 default:
451                 return -2;
452
453                 }
454         }
455                         
456 static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
457                         const char *type, const char *value)
458         {
459         if (!value)
460                 {
461                 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING);
462                 return 0;
463                 }
464         if (!strcmp(type, "rsa_padding_mode"))
465                 {
466                 int pm;
467                 if (!strcmp(value, "pkcs1"))
468                         pm = RSA_PKCS1_PADDING;
469                 else if (!strcmp(value, "sslv23"))
470                         pm = RSA_SSLV23_PADDING;
471                 else if (!strcmp(value, "none"))
472                         pm = RSA_NO_PADDING;
473                 else if (!strcmp(value, "oeap"))
474                         pm = RSA_PKCS1_OAEP_PADDING;
475                 else if (!strcmp(value, "x931"))
476                         pm = RSA_X931_PADDING;
477                 else if (!strcmp(value, "pss"))
478                         pm = RSA_PKCS1_PSS_PADDING;
479                 else
480                         {
481                         RSAerr(RSA_F_PKEY_RSA_CTRL_STR,
482                                                 RSA_R_UNKNOWN_PADDING_TYPE);
483                         return -2;
484                         }
485                 return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
486                 }
487
488         if (!strcmp(type, "rsa_pss_saltlen"))
489                 {
490                 int saltlen;
491                 saltlen = atoi(value);
492                 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
493                 }
494
495         if (!strcmp(type, "rsa_keygen_bits"))
496                 {
497                 int nbits;
498                 nbits = atoi(value);
499                 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
500                 }
501
502         if (!strcmp(type, "rsa_keygen_pubexp"))
503                 {
504                 int ret;
505                 BIGNUM *pubexp = NULL;
506                 if (!BN_asc2bn(&pubexp, value))
507                         return 0;
508                 ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp);
509                 if (ret <= 0)
510                         BN_free(pubexp);
511                 return ret;
512                 }
513
514         return -2;
515         }
516
517 static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
518         {
519         RSA *rsa = NULL;
520         RSA_PKEY_CTX *rctx = ctx->data;
521         BN_GENCB *pcb, cb;
522         int ret;
523         if (!rctx->pub_exp)
524                 {
525                 rctx->pub_exp = BN_new();
526                 if (!rctx->pub_exp || !BN_set_word(rctx->pub_exp, RSA_F4))
527                         return 0;
528                 }
529         rsa = RSA_new();
530         if (!rsa)
531                 return 0;
532         if (ctx->pkey_gencb)
533                 {
534                 pcb = &cb;
535                 evp_pkey_set_cb_translate(pcb, ctx);
536                 }
537         else
538                 pcb = NULL;
539         ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb);
540         if (ret > 0)
541                 EVP_PKEY_assign_RSA(pkey, rsa);
542         else
543                 RSA_free(rsa);
544         return ret;
545         }
546
547 const EVP_PKEY_METHOD rsa_pkey_meth = 
548         {
549         EVP_PKEY_RSA,
550         EVP_PKEY_FLAG_AUTOARGLEN,
551         pkey_rsa_init,
552         pkey_rsa_copy,
553         pkey_rsa_cleanup,
554
555         0,0,
556
557         0,
558         pkey_rsa_keygen,
559
560         0,
561         pkey_rsa_sign,
562
563         0,
564         pkey_rsa_verify,
565
566         0,
567         pkey_rsa_verifyrecover,
568
569
570         0,0,0,0,
571
572         0,
573         pkey_rsa_encrypt,
574
575         0,
576         pkey_rsa_decrypt,
577
578         0,0,
579
580         pkey_rsa_ctrl,
581         pkey_rsa_ctrl_str
582
583
584         };