rsa_saos.c revision 160814
1179187Sjb/* crypto/rsa/rsa_saos.c */ 2179187Sjb/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3179187Sjb * All rights reserved. 4179187Sjb * 5179187Sjb * This package is an SSL implementation written 6179187Sjb * by Eric Young (eay@cryptsoft.com). 7179187Sjb * The implementation was written so as to conform with Netscapes SSL. 8179187Sjb * 9179187Sjb * This library is free for commercial and non-commercial use as long as 10179187Sjb * the following conditions are aheared to. The following conditions 11179187Sjb * apply to all code found in this distribution, be it the RC4, RSA, 12179187Sjb * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13179187Sjb * included with this distribution is covered by the same copyright terms 14179187Sjb * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15179187Sjb * 16179187Sjb * Copyright remains Eric Young's, and as such any Copyright notices in 17179187Sjb * the code are not to be removed. 18179187Sjb * If this package is used in a product, Eric Young should be given attribution 19179187Sjb * as the author of the parts of the library used. 20179187Sjb * This can be in the form of a textual message at program startup or 21179187Sjb * in documentation (online or textual) provided with the package. 22179187Sjb * 23179187Sjb * Redistribution and use in source and binary forms, with or without 24179187Sjb * modification, are permitted provided that the following conditions 25179187Sjb * are met: 26179187Sjb * 1. Redistributions of source code must retain the copyright 27179187Sjb * notice, this list of conditions and the following disclaimer. 28179187Sjb * 2. Redistributions in binary form must reproduce the above copyright 29179187Sjb * notice, this list of conditions and the following disclaimer in the 30179187Sjb * documentation and/or other materials provided with the distribution. 31179187Sjb * 3. All advertising materials mentioning features or use of this software 32179187Sjb * must display the following acknowledgement: 33179187Sjb * "This product includes cryptographic software written by 34179187Sjb * Eric Young (eay@cryptsoft.com)" 35179187Sjb * The word 'cryptographic' can be left out if the rouines from the library 36179187Sjb * being used are not cryptographic related :-). 37179187Sjb * 4. If you include any Windows specific code (or a derivative thereof) from 38179187Sjb * the apps directory (application code) you must include an acknowledgement: 39179187Sjb * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40179187Sjb * 41179187Sjb * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42179187Sjb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43179187Sjb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44179187Sjb * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45179187Sjb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46179187Sjb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47179187Sjb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48179187Sjb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49179187Sjb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50179187Sjb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51179187Sjb * SUCH DAMAGE. 52179187Sjb * 53179187Sjb * The licence and distribution terms for any publically available version or 54179187Sjb * derivative of this code cannot be changed. i.e. this code cannot simply be 55179187Sjb * copied and put under another distribution licence 56179187Sjb * [including the GNU Public Licence.] 57179187Sjb */ 58179187Sjb 59179187Sjb#include <stdio.h> 60179187Sjb#include "cryptlib.h" 61179187Sjb#include <openssl/bn.h> 62179187Sjb#include <openssl/rsa.h> 63179187Sjb#include <openssl/objects.h> 64179187Sjb#include <openssl/x509.h> 65179187Sjb 66179187Sjbint RSA_sign_ASN1_OCTET_STRING(int type, 67179187Sjb const unsigned char *m, unsigned int m_len, 68179187Sjb unsigned char *sigret, unsigned int *siglen, RSA *rsa) 69179187Sjb { 70179187Sjb ASN1_OCTET_STRING sig; 71179187Sjb int i,j,ret=1; 72 unsigned char *p,*s; 73 74 sig.type=V_ASN1_OCTET_STRING; 75 sig.length=m_len; 76 sig.data=(unsigned char *)m; 77 78 i=i2d_ASN1_OCTET_STRING(&sig,NULL); 79 j=RSA_size(rsa); 80 if (i > (j-RSA_PKCS1_PADDING_SIZE)) 81 { 82 RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING,RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY); 83 return(0); 84 } 85 s=(unsigned char *)OPENSSL_malloc((unsigned int)j+1); 86 if (s == NULL) 87 { 88 RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING,ERR_R_MALLOC_FAILURE); 89 return(0); 90 } 91 p=s; 92 i2d_ASN1_OCTET_STRING(&sig,&p); 93 i=RSA_private_encrypt(i,s,sigret,rsa,RSA_PKCS1_PADDING); 94 if (i <= 0) 95 ret=0; 96 else 97 *siglen=i; 98 99 OPENSSL_cleanse(s,(unsigned int)j+1); 100 OPENSSL_free(s); 101 return(ret); 102 } 103 104int RSA_verify_ASN1_OCTET_STRING(int dtype, 105 const unsigned char *m, 106 unsigned int m_len, unsigned char *sigbuf, unsigned int siglen, 107 RSA *rsa) 108 { 109 int i,ret=0; 110 unsigned char *s; 111 const unsigned char *p; 112 ASN1_OCTET_STRING *sig=NULL; 113 114 if (siglen != (unsigned int)RSA_size(rsa)) 115 { 116 RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,RSA_R_WRONG_SIGNATURE_LENGTH); 117 return(0); 118 } 119 120 s=(unsigned char *)OPENSSL_malloc((unsigned int)siglen); 121 if (s == NULL) 122 { 123 RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,ERR_R_MALLOC_FAILURE); 124 goto err; 125 } 126 i=RSA_public_decrypt((int)siglen,sigbuf,s,rsa,RSA_PKCS1_PADDING); 127 128 if (i <= 0) goto err; 129 130 p=s; 131 sig=d2i_ASN1_OCTET_STRING(NULL,&p,(long)i); 132 if (sig == NULL) goto err; 133 134 if ( ((unsigned int)sig->length != m_len) || 135 (memcmp(m,sig->data,m_len) != 0)) 136 { 137 RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,RSA_R_BAD_SIGNATURE); 138 } 139 else 140 ret=1; 141err: 142 if (sig != NULL) M_ASN1_OCTET_STRING_free(sig); 143 if (s != NULL) 144 { 145 OPENSSL_cleanse(s,(unsigned int)siglen); 146 OPENSSL_free(s); 147 } 148 return(ret); 149 } 150 151