xref: /freebsd/crypto/openssl/crypto/rsa/rsa_sign.c (revision 97b2ed56f81c44973772e4cf3869f4f70e2b5d78)
197b2ed56SKris Kennaway /* crypto/rsa/rsa_sign.c */
297b2ed56SKris Kennaway /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
397b2ed56SKris Kennaway  * All rights reserved.
497b2ed56SKris Kennaway  *
597b2ed56SKris Kennaway  * This package is an SSL implementation written
697b2ed56SKris Kennaway  * by Eric Young (eay@cryptsoft.com).
797b2ed56SKris Kennaway  * The implementation was written so as to conform with Netscapes SSL.
897b2ed56SKris Kennaway  *
997b2ed56SKris Kennaway  * This library is free for commercial and non-commercial use as long as
1097b2ed56SKris Kennaway  * the following conditions are aheared to.  The following conditions
1197b2ed56SKris Kennaway  * apply to all code found in this distribution, be it the RC4, RSA,
1297b2ed56SKris Kennaway  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1397b2ed56SKris Kennaway  * included with this distribution is covered by the same copyright terms
1497b2ed56SKris Kennaway  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1597b2ed56SKris Kennaway  *
1697b2ed56SKris Kennaway  * Copyright remains Eric Young's, and as such any Copyright notices in
1797b2ed56SKris Kennaway  * the code are not to be removed.
1897b2ed56SKris Kennaway  * If this package is used in a product, Eric Young should be given attribution
1997b2ed56SKris Kennaway  * as the author of the parts of the library used.
2097b2ed56SKris Kennaway  * This can be in the form of a textual message at program startup or
2197b2ed56SKris Kennaway  * in documentation (online or textual) provided with the package.
2297b2ed56SKris Kennaway  *
2397b2ed56SKris Kennaway  * Redistribution and use in source and binary forms, with or without
2497b2ed56SKris Kennaway  * modification, are permitted provided that the following conditions
2597b2ed56SKris Kennaway  * are met:
2697b2ed56SKris Kennaway  * 1. Redistributions of source code must retain the copyright
2797b2ed56SKris Kennaway  *    notice, this list of conditions and the following disclaimer.
2897b2ed56SKris Kennaway  * 2. Redistributions in binary form must reproduce the above copyright
2997b2ed56SKris Kennaway  *    notice, this list of conditions and the following disclaimer in the
3097b2ed56SKris Kennaway  *    documentation and/or other materials provided with the distribution.
3197b2ed56SKris Kennaway  * 3. All advertising materials mentioning features or use of this software
3297b2ed56SKris Kennaway  *    must display the following acknowledgement:
3397b2ed56SKris Kennaway  *    "This product includes cryptographic software written by
3497b2ed56SKris Kennaway  *     Eric Young (eay@cryptsoft.com)"
3597b2ed56SKris Kennaway  *    The word 'cryptographic' can be left out if the rouines from the library
3697b2ed56SKris Kennaway  *    being used are not cryptographic related :-).
3797b2ed56SKris Kennaway  * 4. If you include any Windows specific code (or a derivative thereof) from
3897b2ed56SKris Kennaway  *    the apps directory (application code) you must include an acknowledgement:
3997b2ed56SKris Kennaway  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4097b2ed56SKris Kennaway  *
4197b2ed56SKris Kennaway  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4297b2ed56SKris Kennaway  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4397b2ed56SKris Kennaway  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4497b2ed56SKris Kennaway  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4597b2ed56SKris Kennaway  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4697b2ed56SKris Kennaway  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4797b2ed56SKris Kennaway  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4897b2ed56SKris Kennaway  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4997b2ed56SKris Kennaway  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5097b2ed56SKris Kennaway  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5197b2ed56SKris Kennaway  * SUCH DAMAGE.
5297b2ed56SKris Kennaway  *
5397b2ed56SKris Kennaway  * The licence and distribution terms for any publically available version or
5497b2ed56SKris Kennaway  * derivative of this code cannot be changed.  i.e. this code cannot simply be
5597b2ed56SKris Kennaway  * copied and put under another distribution licence
5697b2ed56SKris Kennaway  * [including the GNU Public Licence.]
5797b2ed56SKris Kennaway  */
5897b2ed56SKris Kennaway 
5997b2ed56SKris Kennaway #include <stdio.h>
6097b2ed56SKris Kennaway #include "cryptlib.h"
6197b2ed56SKris Kennaway #include <openssl/bn.h>
6297b2ed56SKris Kennaway #include <openssl/rsa.h>
6397b2ed56SKris Kennaway #include <openssl/objects.h>
6497b2ed56SKris Kennaway #include <openssl/x509.h>
6597b2ed56SKris Kennaway 
6697b2ed56SKris Kennaway int RSA_sign(int type, unsigned char *m, unsigned int m_len,
6797b2ed56SKris Kennaway 	     unsigned char *sigret, unsigned int *siglen, RSA *rsa)
6897b2ed56SKris Kennaway 	{
6997b2ed56SKris Kennaway 	X509_SIG sig;
7097b2ed56SKris Kennaway 	ASN1_TYPE parameter;
7197b2ed56SKris Kennaway 	int i,j,ret=1;
7297b2ed56SKris Kennaway 	unsigned char *p,*s;
7397b2ed56SKris Kennaway 	X509_ALGOR algor;
7497b2ed56SKris Kennaway 	ASN1_OCTET_STRING digest;
7597b2ed56SKris Kennaway 
7697b2ed56SKris Kennaway 	sig.algor= &algor;
7797b2ed56SKris Kennaway 	sig.algor->algorithm=OBJ_nid2obj(type);
7897b2ed56SKris Kennaway 	if (sig.algor->algorithm == NULL)
7997b2ed56SKris Kennaway 		{
8097b2ed56SKris Kennaway 		RSAerr(RSA_F_RSA_SIGN,RSA_R_UNKNOWN_ALGORITHM_TYPE);
8197b2ed56SKris Kennaway 		return(0);
8297b2ed56SKris Kennaway 		}
8397b2ed56SKris Kennaway 	if (sig.algor->algorithm->length == 0)
8497b2ed56SKris Kennaway 		{
8597b2ed56SKris Kennaway 		RSAerr(RSA_F_RSA_SIGN,RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD);
8697b2ed56SKris Kennaway 		return(0);
8797b2ed56SKris Kennaway 		}
8897b2ed56SKris Kennaway 	parameter.type=V_ASN1_NULL;
8997b2ed56SKris Kennaway 	parameter.value.ptr=NULL;
9097b2ed56SKris Kennaway 	sig.algor->parameter= &parameter;
9197b2ed56SKris Kennaway 
9297b2ed56SKris Kennaway 	sig.digest= &digest;
9397b2ed56SKris Kennaway 	sig.digest->data=m;
9497b2ed56SKris Kennaway 	sig.digest->length=m_len;
9597b2ed56SKris Kennaway 
9697b2ed56SKris Kennaway 	i=i2d_X509_SIG(&sig,NULL);
9797b2ed56SKris Kennaway 	j=RSA_size(rsa);
9897b2ed56SKris Kennaway 	if ((i-RSA_PKCS1_PADDING) > j)
9997b2ed56SKris Kennaway 		{
10097b2ed56SKris Kennaway 		RSAerr(RSA_F_RSA_SIGN,RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
10197b2ed56SKris Kennaway 		return(0);
10297b2ed56SKris Kennaway 		}
10397b2ed56SKris Kennaway 	s=(unsigned char *)Malloc((unsigned int)j+1);
10497b2ed56SKris Kennaway 	if (s == NULL)
10597b2ed56SKris Kennaway 		{
10697b2ed56SKris Kennaway 		RSAerr(RSA_F_RSA_SIGN,ERR_R_MALLOC_FAILURE);
10797b2ed56SKris Kennaway 		return(0);
10897b2ed56SKris Kennaway 		}
10997b2ed56SKris Kennaway 	p=s;
11097b2ed56SKris Kennaway 	i2d_X509_SIG(&sig,&p);
11197b2ed56SKris Kennaway 	i=RSA_private_encrypt(i,s,sigret,rsa,RSA_PKCS1_PADDING);
11297b2ed56SKris Kennaway 	if (i <= 0)
11397b2ed56SKris Kennaway 		ret=0;
11497b2ed56SKris Kennaway 	else
11597b2ed56SKris Kennaway 		*siglen=i;
11697b2ed56SKris Kennaway 
11797b2ed56SKris Kennaway 	memset(s,0,(unsigned int)j+1);
11897b2ed56SKris Kennaway 	Free(s);
11997b2ed56SKris Kennaway 	return(ret);
12097b2ed56SKris Kennaway 	}
12197b2ed56SKris Kennaway 
12297b2ed56SKris Kennaway int RSA_verify(int dtype, unsigned char *m, unsigned int m_len,
12397b2ed56SKris Kennaway 	     unsigned char *sigbuf, unsigned int siglen, RSA *rsa)
12497b2ed56SKris Kennaway 	{
12597b2ed56SKris Kennaway 	int i,ret=0,sigtype;
12697b2ed56SKris Kennaway 	unsigned char *p,*s;
12797b2ed56SKris Kennaway 	X509_SIG *sig=NULL;
12897b2ed56SKris Kennaway 
12997b2ed56SKris Kennaway 	if (siglen != (unsigned int)RSA_size(rsa))
13097b2ed56SKris Kennaway 		{
13197b2ed56SKris Kennaway 		RSAerr(RSA_F_RSA_VERIFY,RSA_R_WRONG_SIGNATURE_LENGTH);
13297b2ed56SKris Kennaway 		return(0);
13397b2ed56SKris Kennaway 		}
13497b2ed56SKris Kennaway 
13597b2ed56SKris Kennaway 	s=(unsigned char *)Malloc((unsigned int)siglen);
13697b2ed56SKris Kennaway 	if (s == NULL)
13797b2ed56SKris Kennaway 		{
13897b2ed56SKris Kennaway 		RSAerr(RSA_F_RSA_VERIFY,ERR_R_MALLOC_FAILURE);
13997b2ed56SKris Kennaway 		goto err;
14097b2ed56SKris Kennaway 		}
14197b2ed56SKris Kennaway 	i=RSA_public_decrypt((int)siglen,sigbuf,s,rsa,RSA_PKCS1_PADDING);
14297b2ed56SKris Kennaway 
14397b2ed56SKris Kennaway 	if (i <= 0) goto err;
14497b2ed56SKris Kennaway 
14597b2ed56SKris Kennaway 	p=s;
14697b2ed56SKris Kennaway 	sig=d2i_X509_SIG(NULL,&p,(long)i);
14797b2ed56SKris Kennaway 
14897b2ed56SKris Kennaway 	if (sig == NULL) goto err;
14997b2ed56SKris Kennaway 	sigtype=OBJ_obj2nid(sig->algor->algorithm);
15097b2ed56SKris Kennaway 
15197b2ed56SKris Kennaway 
15297b2ed56SKris Kennaway #ifdef RSA_DEBUG
15397b2ed56SKris Kennaway 	/* put a backward compatability flag in EAY */
15497b2ed56SKris Kennaway 	fprintf(stderr,"in(%s) expect(%s)\n",OBJ_nid2ln(sigtype),
15597b2ed56SKris Kennaway 		OBJ_nid2ln(dtype));
15697b2ed56SKris Kennaway #endif
15797b2ed56SKris Kennaway 	if (sigtype != dtype)
15897b2ed56SKris Kennaway 		{
15997b2ed56SKris Kennaway 		if (((dtype == NID_md5) &&
16097b2ed56SKris Kennaway 			(sigtype == NID_md5WithRSAEncryption)) ||
16197b2ed56SKris Kennaway 			((dtype == NID_md2) &&
16297b2ed56SKris Kennaway 			(sigtype == NID_md2WithRSAEncryption)))
16397b2ed56SKris Kennaway 			{
16497b2ed56SKris Kennaway 			/* ok, we will let it through */
16597b2ed56SKris Kennaway #if !defined(NO_STDIO) && !defined(WIN16)
16697b2ed56SKris Kennaway 			fprintf(stderr,"signature has problems, re-make with post SSLeay045\n");
16797b2ed56SKris Kennaway #endif
16897b2ed56SKris Kennaway 			}
16997b2ed56SKris Kennaway 		else
17097b2ed56SKris Kennaway 			{
17197b2ed56SKris Kennaway 			RSAerr(RSA_F_RSA_VERIFY,RSA_R_ALGORITHM_MISMATCH);
17297b2ed56SKris Kennaway 			goto err;
17397b2ed56SKris Kennaway 			}
17497b2ed56SKris Kennaway 		}
17597b2ed56SKris Kennaway 	if (	((unsigned int)sig->digest->length != m_len) ||
17697b2ed56SKris Kennaway 		(memcmp(m,sig->digest->data,m_len) != 0))
17797b2ed56SKris Kennaway 		{
17897b2ed56SKris Kennaway 		RSAerr(RSA_F_RSA_VERIFY,RSA_R_BAD_SIGNATURE);
17997b2ed56SKris Kennaway 		}
18097b2ed56SKris Kennaway 	else
18197b2ed56SKris Kennaway 		ret=1;
18297b2ed56SKris Kennaway err:
18397b2ed56SKris Kennaway 	if (sig != NULL) X509_SIG_free(sig);
18497b2ed56SKris Kennaway 	memset(s,0,(unsigned int)siglen);
18597b2ed56SKris Kennaway 	Free(s);
18697b2ed56SKris Kennaway 	return(ret);
18797b2ed56SKris Kennaway 	}
18897b2ed56SKris Kennaway 
189