Hello there,

I have been working on the rsautls.c on IBM MVS in the Unix subsystem mode. the version of Openssl that I have been using is 0.9.6. Public Key creation goes fine.....infact every thing goes fine except   the call to RSA_public_encrypt(). the call returns -1. I have seeded the random number generator... but still its not working. The same code was working perfect on windows. here is the snippet of code that i have been using

int MVS_PubKeyPasswordEncrypt(
unsigned char* MVS_PubKeyBuffer,
int MVS_PubKeyBufferLen,
unsigned char* PassWordBuffer,
unsigned char** EncPassWordBuffer,
int* EncPassLen,
int Pubkeyform,
int PlainPassLen
 )
{

 int RetCode  = MVS_SUCCESS; /* Assume every thing goes fine */
 EVP_PKEY *pkey = NULL;
 RSA *rsa  = NULL;
 unsigned char *rsa_in = NULL;
 unsigned char *rsa_out = NULL;
 unsigned char pad;
 int rsa_inlen = 0;
 int rsa_outlen = 0;
 int keysize  = 0;
 BIO *bio_err = NULL;
 BIO* in   = NULL;

 pad = RSA_PKCS1_PADDING;
 //pad = RSA_NO_PADDING;

 OpenSSL_add_all_algorithms();             
 MVS_app_RAND_load_file(NULL, bio_err, 0); 
 ERR_load_crypto_strings();                

 pkey = MVS_load_pubkeyFromBuff(MVS_PubKeyBuffer, MVS_PubKeyBufferLen, Pubkeyform);
 if(!pkey) {
  /*Error loading key*/
  RetCode = MVS_RSA_PUBKEYLOAD_ERROR;
  goto end;
 }

 rsa = EVP_PKEY_get1_RSA(pkey);
 EVP_PKEY_free(pkey);
 if(!rsa) {
  /*Error getting RSA key*/
  RetCode  = MVS_RSA_PUBKEYLOAD_ERROR;
  goto end;
 }
 
 keysize = RSA_size(rsa);
 rsa_in = OPENSSL_malloc(keysize * 2);
 rsa_out = OPENSSL_malloc(keysize);
 in = BIO_new_mem_buf(PassWordBuffer, PlainPassLen);
 /* Read the contents to be encrypted */
 rsa_inlen = BIO_read(in, rsa_in, keysize * 2);
 if(rsa_inlen <= 0) {
  /*"Error reading input Data\n"*/
  RetCode  = MVS_RSA_ENC_ERROR;
  goto end;

 }
 
 /* this is where it gets bad !!!!!/*
 rsa_outlen  = RSA_public_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
 if(rsa_outlen <= 0) {
  /*RSA operation error*/
  RetCode  = MVS_RSA_ENC_ERROR;
  goto end;
 }
 
 *EncPassWordBuffer  = (unsigned char*) malloc( rsa_outlen );
 memset(*EncPassWordBuffer, 0, rsa_outlen);
 memcpy(*EncPassWordBuffer, rsa_out, rsa_outlen);
 *EncPassLen = rsa_outlen ;
  
end:
 
 RSA_free(rsa);
 BIO_free(in);
 if(rsa_in) OPENSSL_free(rsa_in);
 if(rsa_out) OPENSSL_free(rsa_out);
 return RetCode;
}
Please tell me what is it that i am doing wrong in the code

Regards,

Sudhir Kakulla



Do You Yahoo!?
Yahoo! Health - Feel better, live better

Reply via email to