Hi!

I'm using openssl in a project for cryptographic purposes.
Everything works fine except of some memory leaks...
I tried to reconstruct those in a smaller example.
Look at this:
======
#include <openssl/evp.h>
#include <openssl/rsa.h>

int main(void)
{
        OpenSSL_add_all_algorithms();
        OpenSSL_add_all_ciphers();
        OpenSSL_add_all_digests();

        FILE *fp;
        fp = fopen("foo.pub","r");
        if(fp==NULL)
        {
          printf("couldnt open...\n");
          goto out;
        }

        RSA *rsa;
        rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL);
        if(rsa == NULL)
        {
                printf("Pem read failed...\n");
                goto out;
        }

        RSA_free(rsa);

        fclose(fp);
        printf("All ok\n");
out:
        EVP_cleanup();
        return 0;
}
======
Compiled with gcc -o test test.c -lssl -lcrypto

This is a snippet of valgrind's output:
==6722== HEAP SUMMARY:
==6722==     in use at exit: 528 bytes in 10 blocks
==6722==   total heap usage: 863 allocs, 853 frees, 27,551 bytes allocated

foo.pub was generated with:
openssl genrsa -out foo.pem 512
openssl rsa -pubout -in foo.pem -out foo.pub

When I comment out from RSA *rsa to RSA_free then valgrind says that
everything was freed correctly:
==6808== HEAP SUMMARY:
==6808==     in use at exit: 0 bytes in 0 blocks
==6808==   total heap usage: 833 allocs, 833 frees, 24,536 bytes allocated

When I add the line CRYPTO_cleanup_all_ex_data() everything works fine, too.
But according to http://www.openssl.org/support/faq.html#PROG13 this is
a thread-unsafe and "Brutal" method.

Are there any better methods for freeing?

Tanks in advance!

Ralf
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           majord...@openssl.org

Reply via email to