197b2ed56SKris Kennaway /* crypto/rsa/rsa_saos.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_ASN1_OCTET_STRING(int type, unsigned char *m, unsigned int m_len, 6797b2ed56SKris Kennaway unsigned char *sigret, unsigned int *siglen, RSA *rsa) 6897b2ed56SKris Kennaway { 6997b2ed56SKris Kennaway ASN1_OCTET_STRING sig; 7097b2ed56SKris Kennaway int i,j,ret=1; 7197b2ed56SKris Kennaway unsigned char *p,*s; 7297b2ed56SKris Kennaway 7397b2ed56SKris Kennaway sig.type=V_ASN1_OCTET_STRING; 7497b2ed56SKris Kennaway sig.length=m_len; 7597b2ed56SKris Kennaway sig.data=m; 7697b2ed56SKris Kennaway 7797b2ed56SKris Kennaway i=i2d_ASN1_OCTET_STRING(&sig,NULL); 7897b2ed56SKris Kennaway j=RSA_size(rsa); 7997b2ed56SKris Kennaway if ((i-RSA_PKCS1_PADDING) > j) 8097b2ed56SKris Kennaway { 8197b2ed56SKris Kennaway RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING,RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY); 8297b2ed56SKris Kennaway return(0); 8397b2ed56SKris Kennaway } 8497b2ed56SKris Kennaway s=(unsigned char *)Malloc((unsigned int)j+1); 8597b2ed56SKris Kennaway if (s == NULL) 8697b2ed56SKris Kennaway { 8797b2ed56SKris Kennaway RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING,ERR_R_MALLOC_FAILURE); 8897b2ed56SKris Kennaway return(0); 8997b2ed56SKris Kennaway } 9097b2ed56SKris Kennaway p=s; 9197b2ed56SKris Kennaway i2d_ASN1_OCTET_STRING(&sig,&p); 9297b2ed56SKris Kennaway i=RSA_private_encrypt(i,s,sigret,rsa,RSA_PKCS1_PADDING); 9397b2ed56SKris Kennaway if (i <= 0) 9497b2ed56SKris Kennaway ret=0; 9597b2ed56SKris Kennaway else 9697b2ed56SKris Kennaway *siglen=i; 9797b2ed56SKris Kennaway 9897b2ed56SKris Kennaway memset(s,0,(unsigned int)j+1); 9997b2ed56SKris Kennaway Free(s); 10097b2ed56SKris Kennaway return(ret); 10197b2ed56SKris Kennaway } 10297b2ed56SKris Kennaway 10397b2ed56SKris Kennaway int RSA_verify_ASN1_OCTET_STRING(int dtype, unsigned char *m, 10497b2ed56SKris Kennaway unsigned int m_len, unsigned char *sigbuf, unsigned int siglen, 10597b2ed56SKris Kennaway RSA *rsa) 10697b2ed56SKris Kennaway { 10797b2ed56SKris Kennaway int i,ret=0; 10897b2ed56SKris Kennaway unsigned char *p,*s; 10997b2ed56SKris Kennaway ASN1_OCTET_STRING *sig=NULL; 11097b2ed56SKris Kennaway 11197b2ed56SKris Kennaway if (siglen != (unsigned int)RSA_size(rsa)) 11297b2ed56SKris Kennaway { 11397b2ed56SKris Kennaway RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,RSA_R_WRONG_SIGNATURE_LENGTH); 11497b2ed56SKris Kennaway return(0); 11597b2ed56SKris Kennaway } 11697b2ed56SKris Kennaway 11797b2ed56SKris Kennaway s=(unsigned char *)Malloc((unsigned int)siglen); 11897b2ed56SKris Kennaway if (s == NULL) 11997b2ed56SKris Kennaway { 12097b2ed56SKris Kennaway RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,ERR_R_MALLOC_FAILURE); 12197b2ed56SKris Kennaway goto err; 12297b2ed56SKris Kennaway } 12397b2ed56SKris Kennaway i=RSA_public_decrypt((int)siglen,sigbuf,s,rsa,RSA_PKCS1_PADDING); 12497b2ed56SKris Kennaway 12597b2ed56SKris Kennaway if (i <= 0) goto err; 12697b2ed56SKris Kennaway 12797b2ed56SKris Kennaway p=s; 12897b2ed56SKris Kennaway sig=d2i_ASN1_OCTET_STRING(NULL,&p,(long)i); 12997b2ed56SKris Kennaway if (sig == NULL) goto err; 13097b2ed56SKris Kennaway 13197b2ed56SKris Kennaway if ( ((unsigned int)sig->length != m_len) || 13297b2ed56SKris Kennaway (memcmp(m,sig->data,m_len) != 0)) 13397b2ed56SKris Kennaway { 13497b2ed56SKris Kennaway RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,RSA_R_BAD_SIGNATURE); 13597b2ed56SKris Kennaway } 13697b2ed56SKris Kennaway else 13797b2ed56SKris Kennaway ret=1; 13897b2ed56SKris Kennaway err: 13997b2ed56SKris Kennaway if (sig != NULL) ASN1_OCTET_STRING_free(sig); 14097b2ed56SKris Kennaway memset(s,0,(unsigned int)siglen); 14197b2ed56SKris Kennaway Free(s); 14297b2ed56SKris Kennaway return(ret); 14397b2ed56SKris Kennaway } 14497b2ed56SKris Kennaway 145