Openssl Speicherlöcher
-
Hallo,
ich habe in unserer Verschlüsselungsfunktion Speicherlöcher und komme nicht dahinter wie ich sie beseitigen kann.unsigned char* EncryptData(unsigned char* pbPlainTextData, unsigned long ulLengthOfPlainTextData, unsigned char** ppbEncryptedData, unsigned long* pulLengthOfEncryptedData, void* pVoid) { PKCS7 *pkcs7=0; const EVP_CIPHER *cipher=0; STACK_OF (X509) * certs=0; X509 *tmp=0; BIO* bio=0, *in=0, *out=0; /* choose the AES 128 cipher block chaining cipher and read in all certificates as encryption targets */ cipher = EVP_aes_128_cbc (); certs = sk_X509_new_null (); //#### gibt es hierfür eine remove-Funktion ? OpenSSL_add_all_algorithms (); //Put the plaintext in a buffer which will be treated as a standard in //This is for the public key (which is in fact not public) bio = BIO_new(BIO_s_mem()); //This is for the plaintext in = BIO_new(BIO_s_mem()); BIO_write(in,pbPlainTextData,ulLengthOfPlainTextData); //This will hold the cipher out = BIO_new(BIO_s_mem()); //Put the public key in the bio BIO_puts(bio, PUBLIC_KEY); /* if (!(tmp=PEM_read_bio_X509(bio,NULL, 0, NULL))) { goto err; } */ //Push the cert in the stack sk_X509_push (certs, tmp); //Encrypt the PlainTextData /* if (!(pkcs7 = PKCS7_encrypt (certs, in, cipher, 0))) { goto err; } */ /* if (PEM_write_bio_PKCS7(out, pkcs7)!= 1) { goto err; } //Let's alocate some memory and save the PKCS#7 in to it *pulLengthOfEncryptedData=(out->num_write); *ppbEncryptedData=(unsigned char*) malloc(out->num_write); BIO_read(out,*ppbEncryptedData,out->num_write); //Destroy all BIO's X509_free(tmp); */ // PKCS7_free(pkcs7); BIO_free(out); BIO_free(in); BIO_free(bio); sk_X509_free(certs); return *ppbEncryptedData; err: return NULL; }ich hab schon einiges auskommentiert,
wenn OpenSSL_add_all_algorithms (); drin ist fängt es an mit den Speicherlöchern, die anderen auskommentierten Codestellen sollen
hinterher auch wieder rein. (Natürlich so verändert das ich keine Speicherlöcher mehr habe)Die Funktion nutzt folgende Header
#include <stdio.h> #include <stdlib.h> #include <openssl/crypto.h> #include <openssl/err.h> #include <openssl/pem.h> #include <openssl/rand.h> // hier liegen die Keys #include "public.h" #include "private.h"Leider konnte ich noch keine gute Doku für dies Openssl finden.
-
Also die einfachste Variante wäre, dass du immer vor dein goto err die Einträge zur Speicherfreigabe machst von den Dingen, die bis zu diesem Zeitpunkt mit deiner BIO_NEW-Funktion geholt wurde.
Besser wäre natürlich eine andere Struktur zu machen.
-
hab etwas gefunden:
CRYPTO_cleanup_all_ex_data();
räumt den Speicher auf !