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 66f579bf8eSKris Kennaway /* Size of an SSL signature: MD5+SHA1 */ 67f579bf8eSKris Kennaway #define SSL_SIG_LENGTH 36 68f579bf8eSKris Kennaway 6997b2ed56SKris Kennaway int RSA_sign(int type, unsigned char *m, unsigned int m_len, 7097b2ed56SKris Kennaway unsigned char *sigret, unsigned int *siglen, RSA *rsa) 7197b2ed56SKris Kennaway { 7297b2ed56SKris Kennaway X509_SIG sig; 7397b2ed56SKris Kennaway ASN1_TYPE parameter; 7497b2ed56SKris Kennaway int i,j,ret=1; 75f579bf8eSKris Kennaway unsigned char *p,*s = NULL; 7697b2ed56SKris Kennaway X509_ALGOR algor; 7797b2ed56SKris Kennaway ASN1_OCTET_STRING digest; 78f579bf8eSKris Kennaway if(rsa->flags & RSA_FLAG_SIGN_VER) 79f579bf8eSKris Kennaway return rsa->meth->rsa_sign(type, m, m_len, sigret, siglen, rsa); 80f579bf8eSKris Kennaway /* Special case: SSL signature, just check the length */ 81f579bf8eSKris Kennaway if(type == NID_md5_sha1) { 82f579bf8eSKris Kennaway if(m_len != SSL_SIG_LENGTH) { 83f579bf8eSKris Kennaway RSAerr(RSA_F_RSA_SIGN,RSA_R_INVALID_MESSAGE_LENGTH); 84f579bf8eSKris Kennaway return(0); 85f579bf8eSKris Kennaway } 86f579bf8eSKris Kennaway i = SSL_SIG_LENGTH; 87f579bf8eSKris Kennaway s = m; 88f579bf8eSKris Kennaway } else { 8997b2ed56SKris Kennaway sig.algor= &algor; 9097b2ed56SKris Kennaway sig.algor->algorithm=OBJ_nid2obj(type); 9197b2ed56SKris Kennaway if (sig.algor->algorithm == NULL) 9297b2ed56SKris Kennaway { 9397b2ed56SKris Kennaway RSAerr(RSA_F_RSA_SIGN,RSA_R_UNKNOWN_ALGORITHM_TYPE); 9497b2ed56SKris Kennaway return(0); 9597b2ed56SKris Kennaway } 9697b2ed56SKris Kennaway if (sig.algor->algorithm->length == 0) 9797b2ed56SKris Kennaway { 9897b2ed56SKris Kennaway RSAerr(RSA_F_RSA_SIGN,RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD); 9997b2ed56SKris Kennaway return(0); 10097b2ed56SKris Kennaway } 10197b2ed56SKris Kennaway parameter.type=V_ASN1_NULL; 10297b2ed56SKris Kennaway parameter.value.ptr=NULL; 10397b2ed56SKris Kennaway sig.algor->parameter= ¶meter; 10497b2ed56SKris Kennaway 10597b2ed56SKris Kennaway sig.digest= &digest; 10697b2ed56SKris Kennaway sig.digest->data=m; 10797b2ed56SKris Kennaway sig.digest->length=m_len; 10897b2ed56SKris Kennaway 10997b2ed56SKris Kennaway i=i2d_X509_SIG(&sig,NULL); 110f579bf8eSKris Kennaway } 11197b2ed56SKris Kennaway j=RSA_size(rsa); 11297b2ed56SKris Kennaway if ((i-RSA_PKCS1_PADDING) > j) 11397b2ed56SKris Kennaway { 11497b2ed56SKris Kennaway RSAerr(RSA_F_RSA_SIGN,RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY); 11597b2ed56SKris Kennaway return(0); 11697b2ed56SKris Kennaway } 117f579bf8eSKris Kennaway if(type != NID_md5_sha1) { 11897b2ed56SKris Kennaway s=(unsigned char *)Malloc((unsigned int)j+1); 11997b2ed56SKris Kennaway if (s == NULL) 12097b2ed56SKris Kennaway { 12197b2ed56SKris Kennaway RSAerr(RSA_F_RSA_SIGN,ERR_R_MALLOC_FAILURE); 12297b2ed56SKris Kennaway return(0); 12397b2ed56SKris Kennaway } 12497b2ed56SKris Kennaway p=s; 12597b2ed56SKris Kennaway i2d_X509_SIG(&sig,&p); 126f579bf8eSKris Kennaway } 12797b2ed56SKris Kennaway i=RSA_private_encrypt(i,s,sigret,rsa,RSA_PKCS1_PADDING); 12897b2ed56SKris Kennaway if (i <= 0) 12997b2ed56SKris Kennaway ret=0; 13097b2ed56SKris Kennaway else 13197b2ed56SKris Kennaway *siglen=i; 13297b2ed56SKris Kennaway 133f579bf8eSKris Kennaway if(type != NID_md5_sha1) { 13497b2ed56SKris Kennaway memset(s,0,(unsigned int)j+1); 13597b2ed56SKris Kennaway Free(s); 136f579bf8eSKris Kennaway } 13797b2ed56SKris Kennaway return(ret); 13897b2ed56SKris Kennaway } 13997b2ed56SKris Kennaway 14097b2ed56SKris Kennaway int RSA_verify(int dtype, unsigned char *m, unsigned int m_len, 14197b2ed56SKris Kennaway unsigned char *sigbuf, unsigned int siglen, RSA *rsa) 14297b2ed56SKris Kennaway { 14397b2ed56SKris Kennaway int i,ret=0,sigtype; 14497b2ed56SKris Kennaway unsigned char *p,*s; 14597b2ed56SKris Kennaway X509_SIG *sig=NULL; 14697b2ed56SKris Kennaway 14797b2ed56SKris Kennaway if (siglen != (unsigned int)RSA_size(rsa)) 14897b2ed56SKris Kennaway { 14997b2ed56SKris Kennaway RSAerr(RSA_F_RSA_VERIFY,RSA_R_WRONG_SIGNATURE_LENGTH); 15097b2ed56SKris Kennaway return(0); 15197b2ed56SKris Kennaway } 15297b2ed56SKris Kennaway 153f579bf8eSKris Kennaway if(rsa->flags & RSA_FLAG_SIGN_VER) 154f579bf8eSKris Kennaway return rsa->meth->rsa_verify(dtype, m, m_len, sigbuf, siglen, rsa); 155f579bf8eSKris Kennaway 15697b2ed56SKris Kennaway s=(unsigned char *)Malloc((unsigned int)siglen); 15797b2ed56SKris Kennaway if (s == NULL) 15897b2ed56SKris Kennaway { 15997b2ed56SKris Kennaway RSAerr(RSA_F_RSA_VERIFY,ERR_R_MALLOC_FAILURE); 16097b2ed56SKris Kennaway goto err; 16197b2ed56SKris Kennaway } 162f579bf8eSKris Kennaway if((dtype == NID_md5_sha1) && (m_len != SSL_SIG_LENGTH) ) { 163f579bf8eSKris Kennaway RSAerr(RSA_F_RSA_VERIFY,RSA_R_INVALID_MESSAGE_LENGTH); 164f579bf8eSKris Kennaway return(0); 165f579bf8eSKris Kennaway } 16697b2ed56SKris Kennaway i=RSA_public_decrypt((int)siglen,sigbuf,s,rsa,RSA_PKCS1_PADDING); 16797b2ed56SKris Kennaway 16897b2ed56SKris Kennaway if (i <= 0) goto err; 16997b2ed56SKris Kennaway 170f579bf8eSKris Kennaway /* Special case: SSL signature */ 171f579bf8eSKris Kennaway if(dtype == NID_md5_sha1) { 172f579bf8eSKris Kennaway if((i != SSL_SIG_LENGTH) || memcmp(s, m, SSL_SIG_LENGTH)) 173f579bf8eSKris Kennaway RSAerr(RSA_F_RSA_VERIFY,RSA_R_BAD_SIGNATURE); 174f579bf8eSKris Kennaway else ret = 1; 175f579bf8eSKris Kennaway } else { 17697b2ed56SKris Kennaway p=s; 17797b2ed56SKris Kennaway sig=d2i_X509_SIG(NULL,&p,(long)i); 17897b2ed56SKris Kennaway 17997b2ed56SKris Kennaway if (sig == NULL) goto err; 18097b2ed56SKris Kennaway sigtype=OBJ_obj2nid(sig->algor->algorithm); 18197b2ed56SKris Kennaway 18297b2ed56SKris Kennaway 18397b2ed56SKris Kennaway #ifdef RSA_DEBUG 184f579bf8eSKris Kennaway /* put a backward compatibility flag in EAY */ 18597b2ed56SKris Kennaway fprintf(stderr,"in(%s) expect(%s)\n",OBJ_nid2ln(sigtype), 18697b2ed56SKris Kennaway OBJ_nid2ln(dtype)); 18797b2ed56SKris Kennaway #endif 18897b2ed56SKris Kennaway if (sigtype != dtype) 18997b2ed56SKris Kennaway { 19097b2ed56SKris Kennaway if (((dtype == NID_md5) && 19197b2ed56SKris Kennaway (sigtype == NID_md5WithRSAEncryption)) || 19297b2ed56SKris Kennaway ((dtype == NID_md2) && 19397b2ed56SKris Kennaway (sigtype == NID_md2WithRSAEncryption))) 19497b2ed56SKris Kennaway { 19597b2ed56SKris Kennaway /* ok, we will let it through */ 19697b2ed56SKris Kennaway #if !defined(NO_STDIO) && !defined(WIN16) 19797b2ed56SKris Kennaway fprintf(stderr,"signature has problems, re-make with post SSLeay045\n"); 19897b2ed56SKris Kennaway #endif 19997b2ed56SKris Kennaway } 20097b2ed56SKris Kennaway else 20197b2ed56SKris Kennaway { 202f579bf8eSKris Kennaway RSAerr(RSA_F_RSA_VERIFY, 203f579bf8eSKris Kennaway RSA_R_ALGORITHM_MISMATCH); 20497b2ed56SKris Kennaway goto err; 20597b2ed56SKris Kennaway } 20697b2ed56SKris Kennaway } 20797b2ed56SKris Kennaway if ( ((unsigned int)sig->digest->length != m_len) || 20897b2ed56SKris Kennaway (memcmp(m,sig->digest->data,m_len) != 0)) 20997b2ed56SKris Kennaway { 21097b2ed56SKris Kennaway RSAerr(RSA_F_RSA_VERIFY,RSA_R_BAD_SIGNATURE); 21197b2ed56SKris Kennaway } 21297b2ed56SKris Kennaway else 21397b2ed56SKris Kennaway ret=1; 214f579bf8eSKris Kennaway } 21597b2ed56SKris Kennaway err: 21697b2ed56SKris Kennaway if (sig != NULL) X509_SIG_free(sig); 21797b2ed56SKris Kennaway memset(s,0,(unsigned int)siglen); 21897b2ed56SKris Kennaway Free(s); 21997b2ed56SKris Kennaway return(ret); 22097b2ed56SKris Kennaway } 22197b2ed56SKris Kennaway 222