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 695c87c606SMark Murray int RSA_sign(int type, const 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; 755c87c606SMark Murray unsigned char *p, *tmps = NULL; 765c87c606SMark Murray const unsigned char *s = NULL; 7797b2ed56SKris Kennaway X509_ALGOR algor; 7897b2ed56SKris Kennaway ASN1_OCTET_STRING digest; 7950ef0093SJacques Vidrine if((rsa->flags & RSA_FLAG_SIGN_VER) && rsa->meth->rsa_sign) 8050ef0093SJacques Vidrine { 8150ef0093SJacques Vidrine return rsa->meth->rsa_sign(type, m, m_len, 8250ef0093SJacques Vidrine sigret, siglen, rsa); 8350ef0093SJacques Vidrine } 84f579bf8eSKris Kennaway /* Special case: SSL signature, just check the length */ 85f579bf8eSKris Kennaway if(type == NID_md5_sha1) { 86f579bf8eSKris Kennaway if(m_len != SSL_SIG_LENGTH) { 87f579bf8eSKris Kennaway RSAerr(RSA_F_RSA_SIGN,RSA_R_INVALID_MESSAGE_LENGTH); 88f579bf8eSKris Kennaway return(0); 89f579bf8eSKris Kennaway } 90f579bf8eSKris Kennaway i = SSL_SIG_LENGTH; 91f579bf8eSKris Kennaway s = m; 92f579bf8eSKris Kennaway } else { 9397b2ed56SKris Kennaway sig.algor= &algor; 9497b2ed56SKris Kennaway sig.algor->algorithm=OBJ_nid2obj(type); 9597b2ed56SKris Kennaway if (sig.algor->algorithm == NULL) 9697b2ed56SKris Kennaway { 9797b2ed56SKris Kennaway RSAerr(RSA_F_RSA_SIGN,RSA_R_UNKNOWN_ALGORITHM_TYPE); 9897b2ed56SKris Kennaway return(0); 9997b2ed56SKris Kennaway } 10097b2ed56SKris Kennaway if (sig.algor->algorithm->length == 0) 10197b2ed56SKris Kennaway { 10297b2ed56SKris Kennaway RSAerr(RSA_F_RSA_SIGN,RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD); 10397b2ed56SKris Kennaway return(0); 10497b2ed56SKris Kennaway } 10597b2ed56SKris Kennaway parameter.type=V_ASN1_NULL; 10697b2ed56SKris Kennaway parameter.value.ptr=NULL; 10797b2ed56SKris Kennaway sig.algor->parameter= ¶meter; 10897b2ed56SKris Kennaway 10997b2ed56SKris Kennaway sig.digest= &digest; 1105c87c606SMark Murray sig.digest->data=(unsigned char *)m; /* TMP UGLY CAST */ 11197b2ed56SKris Kennaway sig.digest->length=m_len; 11297b2ed56SKris Kennaway 11397b2ed56SKris Kennaway i=i2d_X509_SIG(&sig,NULL); 114f579bf8eSKris Kennaway } 11597b2ed56SKris Kennaway j=RSA_size(rsa); 1165c87c606SMark Murray if (i > (j-RSA_PKCS1_PADDING_SIZE)) 11797b2ed56SKris Kennaway { 11897b2ed56SKris Kennaway RSAerr(RSA_F_RSA_SIGN,RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY); 11997b2ed56SKris Kennaway return(0); 12097b2ed56SKris Kennaway } 121f579bf8eSKris Kennaway if(type != NID_md5_sha1) { 1225c87c606SMark Murray tmps=(unsigned char *)OPENSSL_malloc((unsigned int)j+1); 1235c87c606SMark Murray if (tmps == NULL) 12497b2ed56SKris Kennaway { 12597b2ed56SKris Kennaway RSAerr(RSA_F_RSA_SIGN,ERR_R_MALLOC_FAILURE); 12697b2ed56SKris Kennaway return(0); 12797b2ed56SKris Kennaway } 1285c87c606SMark Murray p=tmps; 12997b2ed56SKris Kennaway i2d_X509_SIG(&sig,&p); 1305c87c606SMark Murray s=tmps; 131f579bf8eSKris Kennaway } 13297b2ed56SKris Kennaway i=RSA_private_encrypt(i,s,sigret,rsa,RSA_PKCS1_PADDING); 13397b2ed56SKris Kennaway if (i <= 0) 13497b2ed56SKris Kennaway ret=0; 13597b2ed56SKris Kennaway else 13697b2ed56SKris Kennaway *siglen=i; 13797b2ed56SKris Kennaway 138f579bf8eSKris Kennaway if(type != NID_md5_sha1) { 1395c87c606SMark Murray OPENSSL_cleanse(tmps,(unsigned int)j+1); 1405c87c606SMark Murray OPENSSL_free(tmps); 141f579bf8eSKris Kennaway } 14297b2ed56SKris Kennaway return(ret); 14397b2ed56SKris Kennaway } 14497b2ed56SKris Kennaway 1455c87c606SMark Murray int RSA_verify(int dtype, const unsigned char *m, unsigned int m_len, 14697b2ed56SKris Kennaway unsigned char *sigbuf, unsigned int siglen, RSA *rsa) 14797b2ed56SKris Kennaway { 14897b2ed56SKris Kennaway int i,ret=0,sigtype; 1493b4e3dcbSSimon L. B. Nielsen unsigned char *s; 15097b2ed56SKris Kennaway X509_SIG *sig=NULL; 15197b2ed56SKris Kennaway 15297b2ed56SKris Kennaway if (siglen != (unsigned int)RSA_size(rsa)) 15397b2ed56SKris Kennaway { 15497b2ed56SKris Kennaway RSAerr(RSA_F_RSA_VERIFY,RSA_R_WRONG_SIGNATURE_LENGTH); 15597b2ed56SKris Kennaway return(0); 15697b2ed56SKris Kennaway } 15797b2ed56SKris Kennaway 15850ef0093SJacques Vidrine if((rsa->flags & RSA_FLAG_SIGN_VER) && rsa->meth->rsa_verify) 15950ef0093SJacques Vidrine { 16050ef0093SJacques Vidrine return rsa->meth->rsa_verify(dtype, m, m_len, 16150ef0093SJacques Vidrine sigbuf, siglen, rsa); 16250ef0093SJacques Vidrine } 163f579bf8eSKris Kennaway 164ddd58736SKris Kennaway s=(unsigned char *)OPENSSL_malloc((unsigned int)siglen); 16597b2ed56SKris Kennaway if (s == NULL) 16697b2ed56SKris Kennaway { 16797b2ed56SKris Kennaway RSAerr(RSA_F_RSA_VERIFY,ERR_R_MALLOC_FAILURE); 16897b2ed56SKris Kennaway goto err; 16997b2ed56SKris Kennaway } 170f579bf8eSKris Kennaway if((dtype == NID_md5_sha1) && (m_len != SSL_SIG_LENGTH) ) { 171f579bf8eSKris Kennaway RSAerr(RSA_F_RSA_VERIFY,RSA_R_INVALID_MESSAGE_LENGTH); 1723b4e3dcbSSimon L. B. Nielsen goto err; 173f579bf8eSKris Kennaway } 17497b2ed56SKris Kennaway i=RSA_public_decrypt((int)siglen,sigbuf,s,rsa,RSA_PKCS1_PADDING); 17597b2ed56SKris Kennaway 17697b2ed56SKris Kennaway if (i <= 0) goto err; 17797b2ed56SKris Kennaway 178f579bf8eSKris Kennaway /* Special case: SSL signature */ 179f579bf8eSKris Kennaway if(dtype == NID_md5_sha1) { 180f579bf8eSKris Kennaway if((i != SSL_SIG_LENGTH) || memcmp(s, m, SSL_SIG_LENGTH)) 181f579bf8eSKris Kennaway RSAerr(RSA_F_RSA_VERIFY,RSA_R_BAD_SIGNATURE); 182f579bf8eSKris Kennaway else ret = 1; 183f579bf8eSKris Kennaway } else { 1843b4e3dcbSSimon L. B. Nielsen const unsigned char *p=s; 18597b2ed56SKris Kennaway sig=d2i_X509_SIG(NULL,&p,(long)i); 18697b2ed56SKris Kennaway 18797b2ed56SKris Kennaway if (sig == NULL) goto err; 18897b2ed56SKris Kennaway sigtype=OBJ_obj2nid(sig->algor->algorithm); 18997b2ed56SKris Kennaway 19097b2ed56SKris Kennaway 19197b2ed56SKris Kennaway #ifdef RSA_DEBUG 192f579bf8eSKris Kennaway /* put a backward compatibility flag in EAY */ 19397b2ed56SKris Kennaway fprintf(stderr,"in(%s) expect(%s)\n",OBJ_nid2ln(sigtype), 19497b2ed56SKris Kennaway OBJ_nid2ln(dtype)); 19597b2ed56SKris Kennaway #endif 19697b2ed56SKris Kennaway if (sigtype != dtype) 19797b2ed56SKris Kennaway { 19897b2ed56SKris Kennaway if (((dtype == NID_md5) && 19997b2ed56SKris Kennaway (sigtype == NID_md5WithRSAEncryption)) || 20097b2ed56SKris Kennaway ((dtype == NID_md2) && 20197b2ed56SKris Kennaway (sigtype == NID_md2WithRSAEncryption))) 20297b2ed56SKris Kennaway { 20397b2ed56SKris Kennaway /* ok, we will let it through */ 2045c87c606SMark Murray #if !defined(OPENSSL_NO_STDIO) && !defined(OPENSSL_SYS_WIN16) 20597b2ed56SKris Kennaway fprintf(stderr,"signature has problems, re-make with post SSLeay045\n"); 20697b2ed56SKris Kennaway #endif 20797b2ed56SKris Kennaway } 20897b2ed56SKris Kennaway else 20997b2ed56SKris Kennaway { 210f579bf8eSKris Kennaway RSAerr(RSA_F_RSA_VERIFY, 211f579bf8eSKris Kennaway RSA_R_ALGORITHM_MISMATCH); 21297b2ed56SKris Kennaway goto err; 21397b2ed56SKris Kennaway } 21497b2ed56SKris Kennaway } 21597b2ed56SKris Kennaway if ( ((unsigned int)sig->digest->length != m_len) || 21697b2ed56SKris Kennaway (memcmp(m,sig->digest->data,m_len) != 0)) 21797b2ed56SKris Kennaway { 21897b2ed56SKris Kennaway RSAerr(RSA_F_RSA_VERIFY,RSA_R_BAD_SIGNATURE); 21997b2ed56SKris Kennaway } 22097b2ed56SKris Kennaway else 22197b2ed56SKris Kennaway ret=1; 222f579bf8eSKris Kennaway } 22397b2ed56SKris Kennaway err: 22497b2ed56SKris Kennaway if (sig != NULL) X509_SIG_free(sig); 2253b4e3dcbSSimon L. B. Nielsen if (s != NULL) 2263b4e3dcbSSimon L. B. Nielsen { 2275c87c606SMark Murray OPENSSL_cleanse(s,(unsigned int)siglen); 228ddd58736SKris Kennaway OPENSSL_free(s); 2293b4e3dcbSSimon L. B. Nielsen } 23097b2ed56SKris Kennaway return(ret); 23197b2ed56SKris Kennaway } 23297b2ed56SKris Kennaway 233