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