Hi,
I'm decrypting a string (test12345678) found in an input-file.
When I decrypt using two file-BIO's (in the following code-extract this means
replacing out=BIO_new(BIO_s_mem) by out=BIO_new(BIO_s_file) and a
BIO_write_filename(out,outf) ), then the outputfile contains the full decrypted string
"test12345678".
When I do the same using a mem-BIO (see code below), the decrypted string returned is
always 8 characters, and if the encrypted string is smaller then 8 characters
nothing happens ! I must be overlooking something MEM_BUF-related, but I just don't
see it.
Any ideas/hints on how to resolve this ?
Thanks ! & kind regards,
dirk L.
example code:
#include <stdio.h>
#include <string.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/buffer.h>
#include <openssl/md5.h>
#define ATTRMAX 50
#define BUFFERSIZE 256
#define BSIZE (8*1024)
main(argc, argv)
int argc;
char *argv[];
{
char inf[19] = "test_encrypted.pas";
char *pw=NULL;
int ret;
ret = get_passphrase(inf,&pw);
if(ret == 0){
fprintf(stdout,"the password is %s\n",pw);
}else{
fprintf(stdout,"Unable to retrieve the password from file %s\n",inf);
}
}
int get_passphrase(char *inf,char **pass){
int done = 0,inl;
int bsize=BSIZE;
char cipher_name[5]="des3";
const EVP_CIPHER *cipher=NULL;
char str[5] ="test";
unsigned char *buff=NULL;
BUF_MEM *bptr;
BIO *in=NULL,*benc=NULL,*out=NULL;
unsigned char key[24],iv[MD5_DIGEST_LENGTH];
fprintf(stdout,"Begin decrypt\n");
OpenSSL_add_all_ciphers();
cipher=EVP_get_cipherbyname(cipher_name);
if (cipher == NULL){
fprintf(stdout,"%s is an unknown cipher\n",cipher_name);
return 1;
}
/*read input-filename*/
if(inf==NULL){
fprintf(stdout,"input-filename not specified\n");
return 1;
}else{
in=BIO_new(BIO_s_file());
if(in==NULL){
fprintf(stdout,"unable to open input-filename %s\n",inf);
return 1;
}else{
if (BIO_read_filename(in,inf) <= 0){
fprintf(stdout,"1)unable to read from input-filename %s\n",inf);
return 1;
}else{
out=BIO_new(BIO_s_mem());
if(out==NULL){
fprintf(stdout,"unable to create mem-BIO\n");
return 1;
}
if((cipher!=NULL)&&(str!=NULL)){
EVP_BytesToKey(cipher,EVP_md5(),NULL, (unsigned char *)str,
strlen(str),1,key,iv);
/*zero the variable str*/
memset(str,0,strlen(str));
/*create the encrypt/decrypt BIO*/
if ((benc=BIO_new(BIO_f_cipher())) == NULL){
fprintf(stdout,"unable to create decrypt-BIO \n");
return 1;
}else{
BIO_set_cipher(benc,cipher,key,iv,0);
/* decrypt on the go */
if (benc != NULL)
out=BIO_push(benc,out);
buff=(unsigned char *)OPENSSL_malloc(EVP_ENCODE_LENGTH(bsize));
if (buff == NULL){
fprintf(stdout,"unable to assign buffer\n");
return 1;
}
for (;;) {
inl=BIO_read(in,(char *)buff,bsize);
if (inl <= 0) break;
if (BIO_write(out,(char *)buff,inl) != inl){
fprintf(stdout,"error writing output file\n");
goto end;
}
}
BIO_get_mem_ptr(out, &bptr);
fprintf(stdout,"read from out %s, length %d\n",bptr->data,bptr->length);
*pass=strdup(bptr->data);
if (!BIO_flush(out)) {
fprintf(stdout,"bad decrypt\n");
goto end;
}
end:
fprintf(stdout,"passphrase successfully decrypted\n");
}
}else{
fprintf(stdout,"2)unable to read from input-filename %s\n",inf);
return 1;
}
}
}
}
EVP_cleanup();
if (buff != NULL) OPENSSL_free(buff);
if (in != NULL) BIO_free(in);
if (out != NULL) BIO_free_all(out);
fprintf(stdout,"End decrypt\n");
return 0;
}
*****************
Dirk Laurijssen
Syntegra, creating winners in the digital economy.
+32 2 247 92 20 - Check us out at www.syntegra.be
*****************
______________________________________________________________________
OpenSSL Project http://www.openssl.org
User Support Mailing List [EMAIL PROTECTED]
Automated List Manager [EMAIL PROTECTED]