156083Skris/* crypto/rsa/rsa_saos.c */
256083Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
356083Skris * All rights reserved.
456083Skris *
556083Skris * This package is an SSL implementation written
656083Skris * by Eric Young (eay@cryptsoft.com).
756083Skris * The implementation was written so as to conform with Netscapes SSL.
856083Skris *
956083Skris * This library is free for commercial and non-commercial use as long as
1056083Skris * the following conditions are aheared to.  The following conditions
1156083Skris * apply to all code found in this distribution, be it the RC4, RSA,
1256083Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1356083Skris * included with this distribution is covered by the same copyright terms
1456083Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1556083Skris *
1656083Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1756083Skris * the code are not to be removed.
1856083Skris * If this package is used in a product, Eric Young should be given attribution
1956083Skris * as the author of the parts of the library used.
2056083Skris * This can be in the form of a textual message at program startup or
2156083Skris * in documentation (online or textual) provided with the package.
2256083Skris *
2356083Skris * Redistribution and use in source and binary forms, with or without
2456083Skris * modification, are permitted provided that the following conditions
2556083Skris * are met:
2656083Skris * 1. Redistributions of source code must retain the copyright
2756083Skris *    notice, this list of conditions and the following disclaimer.
2856083Skris * 2. Redistributions in binary form must reproduce the above copyright
2956083Skris *    notice, this list of conditions and the following disclaimer in the
3056083Skris *    documentation and/or other materials provided with the distribution.
3156083Skris * 3. All advertising materials mentioning features or use of this software
3256083Skris *    must display the following acknowledgement:
3356083Skris *    "This product includes cryptographic software written by
3456083Skris *     Eric Young (eay@cryptsoft.com)"
3556083Skris *    The word 'cryptographic' can be left out if the rouines from the library
3656083Skris *    being used are not cryptographic related :-).
3756083Skris * 4. If you include any Windows specific code (or a derivative thereof) from
3856083Skris *    the apps directory (application code) you must include an acknowledgement:
3956083Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4056083Skris *
4156083Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4256083Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4356083Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4456083Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4556083Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4656083Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4756083Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4856083Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4956083Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5056083Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5156083Skris * SUCH DAMAGE.
5256083Skris *
5356083Skris * The licence and distribution terms for any publically available version or
5456083Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5556083Skris * copied and put under another distribution licence
5656083Skris * [including the GNU Public Licence.]
5756083Skris */
5856083Skris
5956083Skris#include <stdio.h>
6056083Skris#include "cryptlib.h"
6156083Skris#include <openssl/bn.h>
6256083Skris#include <openssl/rsa.h>
6356083Skris#include <openssl/objects.h>
6456083Skris#include <openssl/x509.h>
6556083Skris
66109998Smarkmint RSA_sign_ASN1_OCTET_STRING(int type,
67109998Smarkm	const unsigned char *m, unsigned int m_len,
68109998Smarkm	unsigned char *sigret, unsigned int *siglen, RSA *rsa)
6956083Skris	{
7056083Skris	ASN1_OCTET_STRING sig;
7156083Skris	int i,j,ret=1;
7256083Skris	unsigned char *p,*s;
7356083Skris
7456083Skris	sig.type=V_ASN1_OCTET_STRING;
7556083Skris	sig.length=m_len;
76109998Smarkm	sig.data=(unsigned char *)m;
7756083Skris
7856083Skris	i=i2d_ASN1_OCTET_STRING(&sig,NULL);
7956083Skris	j=RSA_size(rsa);
80109998Smarkm	if (i > (j-RSA_PKCS1_PADDING_SIZE))
8156083Skris		{
8256083Skris		RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING,RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
8356083Skris		return(0);
8456083Skris		}
8568651Skris	s=(unsigned char *)OPENSSL_malloc((unsigned int)j+1);
8656083Skris	if (s == NULL)
8756083Skris		{
8856083Skris		RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING,ERR_R_MALLOC_FAILURE);
8956083Skris		return(0);
9056083Skris		}
9156083Skris	p=s;
9256083Skris	i2d_ASN1_OCTET_STRING(&sig,&p);
9356083Skris	i=RSA_private_encrypt(i,s,sigret,rsa,RSA_PKCS1_PADDING);
9456083Skris	if (i <= 0)
9556083Skris		ret=0;
9656083Skris	else
9756083Skris		*siglen=i;
9856083Skris
99109998Smarkm	OPENSSL_cleanse(s,(unsigned int)j+1);
10068651Skris	OPENSSL_free(s);
10156083Skris	return(ret);
10256083Skris	}
10356083Skris
104109998Smarkmint RSA_verify_ASN1_OCTET_STRING(int dtype,
105109998Smarkm	const unsigned char *m,
106109998Smarkm	unsigned int m_len, unsigned char *sigbuf, unsigned int siglen,
107109998Smarkm	RSA *rsa)
10856083Skris	{
10956083Skris	int i,ret=0;
110160814Ssimon	unsigned char *s;
111160814Ssimon	const unsigned char *p;
11256083Skris	ASN1_OCTET_STRING *sig=NULL;
11356083Skris
11456083Skris	if (siglen != (unsigned int)RSA_size(rsa))
11556083Skris		{
11656083Skris		RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,RSA_R_WRONG_SIGNATURE_LENGTH);
11756083Skris		return(0);
11856083Skris		}
11956083Skris
12068651Skris	s=(unsigned char *)OPENSSL_malloc((unsigned int)siglen);
12156083Skris	if (s == NULL)
12256083Skris		{
12356083Skris		RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,ERR_R_MALLOC_FAILURE);
12456083Skris		goto err;
12556083Skris		}
12656083Skris	i=RSA_public_decrypt((int)siglen,sigbuf,s,rsa,RSA_PKCS1_PADDING);
12756083Skris
12856083Skris	if (i <= 0) goto err;
12956083Skris
13056083Skris	p=s;
13156083Skris	sig=d2i_ASN1_OCTET_STRING(NULL,&p,(long)i);
13256083Skris	if (sig == NULL) goto err;
13356083Skris
13456083Skris	if (	((unsigned int)sig->length != m_len) ||
13556083Skris		(memcmp(m,sig->data,m_len) != 0))
13656083Skris		{
13756083Skris		RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,RSA_R_BAD_SIGNATURE);
13856083Skris		}
13956083Skris	else
14056083Skris		ret=1;
14156083Skriserr:
14259191Skris	if (sig != NULL) M_ASN1_OCTET_STRING_free(sig);
143160814Ssimon	if (s != NULL)
144160814Ssimon		{
145160814Ssimon		OPENSSL_cleanse(s,(unsigned int)siglen);
146160814Ssimon		OPENSSL_free(s);
147160814Ssimon		}
14856083Skris	return(ret);
14956083Skris	}
15056083Skris
151