rsa_sign.c 8.7 KB
Newer Older
1
/* crypto/rsa/rsa_sign.c */
2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
 * All rights reserved.
 *
 * This package is an SSL implementation written
 * by Eric Young (eay@cryptsoft.com).
 * The implementation was written so as to conform with Netscapes SSL.
 * 
 * This library is free for commercial and non-commercial use as long as
 * the following conditions are aheared to.  The following conditions
 * apply to all code found in this distribution, be it the RC4, RSA,
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
 * included with this distribution is covered by the same copyright terms
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
 * 
 * Copyright remains Eric Young's, and as such any Copyright notices in
 * the code are not to be removed.
 * If this package is used in a product, Eric Young should be given attribution
 * as the author of the parts of the library used.
 * This can be in the form of a textual message at program startup or
 * in documentation (online or textual) provided with the package.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    "This product includes cryptographic software written by
 *     Eric Young (eay@cryptsoft.com)"
 *    The word 'cryptographic' can be left out if the rouines from the library
 *    being used are not cryptographic related :-).
 * 4. If you include any Windows specific code (or a derivative thereof) from 
 *    the apps directory (application code) you must include an acknowledgement:
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
 * 
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * 
 * The licence and distribution terms for any publically available version or
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
 * copied and put under another distribution licence
 * [including the GNU Public Licence.]
 */

#include <stdio.h>
#include "cryptlib.h"
61 62 63 64
#include <openssl/bn.h>
#include <openssl/rsa.h>
#include <openssl/objects.h>
#include <openssl/x509.h>
65
#include "rsa_locl.h"
66

67 68 69
/* Size of an SSL signature: MD5+SHA1 */
#define SSL_SIG_LENGTH	36

R
Richard Levitte 已提交
70
int RSA_sign(int type, const unsigned char *m, unsigned int m_len,
71
	     unsigned char *sigret, unsigned int *siglen, RSA *rsa)
72 73 74 75
	{
	X509_SIG sig;
	ASN1_TYPE parameter;
	int i,j,ret=1;
R
Richard Levitte 已提交
76 77
	unsigned char *p, *tmps = NULL;
	const unsigned char *s = NULL;
78 79
	X509_ALGOR algor;
	ASN1_OCTET_STRING digest;
D
Dr. Stephen Henson 已提交
80
	if((rsa->flags & RSA_FLAG_SIGN_VER) && rsa->meth->rsa_sign)
81 82 83 84
		{
		return rsa->meth->rsa_sign(type, m, m_len,
			sigret, siglen, rsa);
		}
85 86 87 88 89
	/* Special case: SSL signature, just check the length */
	if(type == NID_md5_sha1) {
		if(m_len != SSL_SIG_LENGTH) {
			RSAerr(RSA_F_RSA_SIGN,RSA_R_INVALID_MESSAGE_LENGTH);
			return(0);
90
		}
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
		i = SSL_SIG_LENGTH;
		s = m;
	} else {
		sig.algor= &algor;
		sig.algor->algorithm=OBJ_nid2obj(type);
		if (sig.algor->algorithm == NULL)
			{
			RSAerr(RSA_F_RSA_SIGN,RSA_R_UNKNOWN_ALGORITHM_TYPE);
			return(0);
			}
		if (sig.algor->algorithm->length == 0)
			{
			RSAerr(RSA_F_RSA_SIGN,RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD);
			return(0);
			}
		parameter.type=V_ASN1_NULL;
		parameter.value.ptr=NULL;
		sig.algor->parameter= &parameter;
109

110
		sig.digest= &digest;
R
Richard Levitte 已提交
111
		sig.digest->data=(unsigned char *)m; /* TMP UGLY CAST */
112
		sig.digest->length=m_len;
113

114 115
		i=i2d_X509_SIG(&sig,NULL);
	}
116
	j=RSA_size(rsa);
117
	if (i > (j-RSA_PKCS1_PADDING_SIZE))
118 119 120 121
		{
		RSAerr(RSA_F_RSA_SIGN,RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
		return(0);
		}
122
	if(type != NID_md5_sha1) {
R
Richard Levitte 已提交
123 124
		tmps=(unsigned char *)OPENSSL_malloc((unsigned int)j+1);
		if (tmps == NULL)
125 126 127 128
			{
			RSAerr(RSA_F_RSA_SIGN,ERR_R_MALLOC_FAILURE);
			return(0);
			}
R
Richard Levitte 已提交
129
		p=tmps;
130
		i2d_X509_SIG(&sig,&p);
R
Richard Levitte 已提交
131
		s=tmps;
132
	}
133 134 135 136 137 138
	i=RSA_private_encrypt(i,s,sigret,rsa,RSA_PKCS1_PADDING);
	if (i <= 0)
		ret=0;
	else
		*siglen=i;

139
	if(type != NID_md5_sha1) {
140
		OPENSSL_cleanse(tmps,(unsigned int)j+1);
R
Richard Levitte 已提交
141
		OPENSSL_free(tmps);
142
	}
143 144 145
	return(ret);
	}

146 147
int int_rsa_verify(int dtype, const unsigned char *m,
			  unsigned int m_len,
148 149
			  unsigned char *rm, size_t *prm_len,
			  const unsigned char *sigbuf, size_t siglen,
150
			  RSA *rsa)
151 152
	{
	int i,ret=0,sigtype;
153
	unsigned char *s;
154 155 156 157
	X509_SIG *sig=NULL;

	if (siglen != (unsigned int)RSA_size(rsa))
		{
158
		RSAerr(RSA_F_INT_RSA_VERIFY,RSA_R_WRONG_SIGNATURE_LENGTH);
159 160 161
		return(0);
		}

162
	if((dtype == NID_md5_sha1) && rm)
163
		{
164 165 166 167 168 169
		i = RSA_public_decrypt((int)siglen,
					sigbuf,rm,rsa,RSA_PKCS1_PADDING);
		if (i <= 0)
			return 0;
		*prm_len = i;
		return 1;
170
		}
171

172
	s=(unsigned char *)OPENSSL_malloc((unsigned int)siglen);
173 174
	if (s == NULL)
		{
175
		RSAerr(RSA_F_INT_RSA_VERIFY,ERR_R_MALLOC_FAILURE);
176 177
		goto err;
		}
178
	if((dtype == NID_md5_sha1) && (m_len != SSL_SIG_LENGTH) ) {
179
			RSAerr(RSA_F_INT_RSA_VERIFY,RSA_R_INVALID_MESSAGE_LENGTH);
180
			goto err;
181
	}
182 183 184
	i=RSA_public_decrypt((int)siglen,sigbuf,s,rsa,RSA_PKCS1_PADDING);

	if (i <= 0) goto err;
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
	/* Oddball MDC2 case: signature can be OCTET STRING.
	 * check for correct tag and length octets.
	 */
	if (dtype == NID_mdc2 && i == 18 && s[0] == 0x04 && s[1] == 0x10)
		{
		if (rm)
			{
			memcpy(rm, s + 2, 16);
			*prm_len = 16;
			ret = 1;
			}
		else if(memcmp(m, s + 2, 16))
			RSAerr(RSA_F_INT_RSA_VERIFY,RSA_R_BAD_SIGNATURE);
		else
			ret = 1;
		}
201

202 203 204
	/* Special case: SSL signature */
	if(dtype == NID_md5_sha1) {
		if((i != SSL_SIG_LENGTH) || memcmp(s, m, SSL_SIG_LENGTH))
205
				RSAerr(RSA_F_INT_RSA_VERIFY,RSA_R_BAD_SIGNATURE);
206 207
		else ret = 1;
	} else {
208
		const unsigned char *p=s;
209
		sig=d2i_X509_SIG(NULL,&p,(long)i);
210

211
		if (sig == NULL) goto err;
212 213 214 215

		/* Excess data can be used to create forgeries */
		if(p != s+i)
			{
B
Bodo Möller 已提交
216
			RSAerr(RSA_F_INT_RSA_VERIFY,RSA_R_BAD_SIGNATURE);
217 218 219 220 221 222 223 224
			goto err;
			}

		/* Parameters to the signature algorithm can also be used to
		   create forgeries */
		if(sig->algor->parameter
		   && ASN1_TYPE_get(sig->algor->parameter) != V_ASN1_NULL)
			{
B
Bodo Möller 已提交
225
			RSAerr(RSA_F_INT_RSA_VERIFY,RSA_R_BAD_SIGNATURE);
226 227 228
			goto err;
			}

229
		sigtype=OBJ_obj2nid(sig->algor->algorithm);
230

231

232
	#ifdef RSA_DEBUG
U
Ulf Möller 已提交
233
		/* put a backward compatibility flag in EAY */
234 235 236 237
		fprintf(stderr,"in(%s) expect(%s)\n",OBJ_nid2ln(sigtype),
			OBJ_nid2ln(dtype));
	#endif
		if (sigtype != dtype)
238
			{
239 240 241 242 243 244
			if (((dtype == NID_md5) &&
				(sigtype == NID_md5WithRSAEncryption)) ||
				((dtype == NID_md2) &&
				(sigtype == NID_md2WithRSAEncryption)))
				{
				/* ok, we will let it through */
245
#if !defined(OPENSSL_NO_STDIO) && !defined(OPENSSL_SYS_WIN16)
246
				fprintf(stderr,"signature has problems, re-make with post SSLeay045\n");
247
#endif
248 249 250
				}
			else
				{
251
				RSAerr(RSA_F_INT_RSA_VERIFY,
252 253 254
						RSA_R_ALGORITHM_MISMATCH);
				goto err;
				}
255
			}
256 257
		if (rm)
			{
258 259 260
			const EVP_MD *md;
			md = EVP_get_digestbynid(dtype);
			if (md && (EVP_MD_size(md) != sig->digest->length))
261
				RSAerr(RSA_F_INT_RSA_VERIFY,
262 263 264 265 266 267 268 269
						RSA_R_INVALID_DIGEST_LENGTH);
			else
				{
				memcpy(rm, sig->digest->data,
							sig->digest->length);
				*prm_len = sig->digest->length;
				ret = 1;
				}
270 271
			}
		else if (((unsigned int)sig->digest->length != m_len) ||
272
			(memcmp(m,sig->digest->data,m_len) != 0))
273
			{
274
			RSAerr(RSA_F_INT_RSA_VERIFY,RSA_R_BAD_SIGNATURE);
275
			}
276 277 278
		else
			ret=1;
	}
279 280
err:
	if (sig != NULL) X509_SIG_free(sig);
281 282 283 284 285
	if (s != NULL)
		{
		OPENSSL_cleanse(s,(unsigned int)siglen);
		OPENSSL_free(s);
		}
286 287 288
	return(ret);
	}

289
int RSA_verify(int dtype, const unsigned char *m, unsigned int m_len,
290
		const unsigned char *sigbuf, unsigned int siglen,
291 292 293 294 295 296 297 298 299 300 301
		RSA *rsa)
	{

	if((rsa->flags & RSA_FLAG_SIGN_VER) && rsa->meth->rsa_verify)
		{
		return rsa->meth->rsa_verify(dtype, m, m_len,
			sigbuf, siglen, rsa);
		}

	return int_rsa_verify(dtype, m, m_len, NULL, NULL, sigbuf, siglen, rsa);
	}