Hi, I think I have find a memory leaks in PKCS12_parse(), the code is below, please use the debug library(compile with -DCRYPTO_MDEBUG) to check it:
 
        #define CRYPTO_MDEBUG
        #include "openssl\bio.h"
        #include "openssl\err.h"
        #include "openssl\pem.h"
        #include "openssl\pkcs12.h"
        void main()
        {
           EVP_PKEY *pkey = NULL;
           BIO *bio = NULL;
           PKCS12 *p12 = NULL;
 
           MemCheck_start();
           OpenSSL_add_all_algorithms();
           bio = BIO_new_file( "test.p12", "r" );
           p12 = d2i_PKCS12_bio( bio, NULL );
           PKCS12_parse( p12, "test", &pkey, NULL, NULL );
           if ( p12 ) PKCS12_free( p12 );
           if ( bio ) BIO_free( bio );
           if ( pkey ) EVP_PKEY_free( pkey );
           OBJ_cleanup();
           ERR_remove_state(0);
           EVP_cleanup();
           ERR_free_strings();
           CRYPTO_mem_leaks_fp( stderr );
        }
 
output is below:
 
  461 file=.\crypto\asn1\a_object.c, line=268, number=24, address=004A8F00
  489 file=.\crypto\asn1\a_object.c, line=242, number=3, address=004A9700
  499 file=.\crypto\asn1\a_object.c, line=242, number=3, address=004AAED0
  ...
  428 file=.\crypto\asn1\a_object.c, line=268, number=24, address=004A6A60
2739 bytes leaked in 121 chunks
 

the patch is below:
 
diff -Nur openssl-0.9.6a-beta3/crypto/pkcs12/p12_kiss.c openssl-0.9.6a-beta3-1/crypto/pkcs12/p12_kiss.c
--- openssl-0.9.6a-beta3/crypto/pkcs12/p12_kiss.c Mon Sep 18 02:21:10 2000
+++ openssl-0.9.6a-beta3-1/crypto/pkcs12/p12_kiss.c Tue Apr 03 16:13:01 2001
@@ -264,6 +264,7 @@
   if (lkey) {
    *keymatch |= MATCH_CERT;
    if (cert) *cert = x509;
+   else X509_free(x509);
   } else {
    if(ca) sk_X509_push (*ca, x509);
    else X509_free(x509);
 
this bug also exist in openssl-0.9.6.
 
regards,
Shijin

test.p12

  461 file=.\crypto\asn1\a_object.c, line=268, number=24, address=004A8F00
  489 file=.\crypto\asn1\a_object.c, line=242, number=3, address=004A9700
  499 file=.\crypto\asn1\a_object.c, line=242, number=3, address=004AAED0
  431 file=.\crypto\asn1\x_name.c, line=233, number=16, address=004A67F0
  517 file=.\crypto\asn1\a_object.c, line=242, number=9, address=004ABE70
  462 file=.\crypto\asn1\a_object.c, line=242, number=3, address=004A8E40
  527 file=.\crypto\asn1\asn1_lib.c, line=341, number=5, address=004AB5E0
  442 file=.\crypto\asn1\asn1_lib.c, line=371, number=16, address=004A7ED0
  463 file=.\crypto\asn1\a_bytes.c, line=114, number=3, address=004A8D80
  501 file=.\crypto\asn1\x_exten.c, line=123, number=20, address=004AAD20
  423 file=.\crypto\asn1\x_algor.c, line=102, number=8, address=004A6E40
  482 file=.\crypto\asn1\a_type.c, line=277, number=8, address=004A9CF0
  433 file=.\crypto\asn1\a_object.c, line=268, number=24, address=004A6640
  492 file=.\crypto\asn1\asn1_lib.c, line=371, number=16, address=004A9490
  418 file=.\crypto\stack\stack.c, line=124, number=32, address=004A8490
  414 file=.\crypto\asn1\asn1_lib.c, line=371, number=16, address=004A55B0
  434 file=.\crypto\asn1\a_object.c, line=242, number=3, address=004A6580
  511 file=.\crypto\asn1\x_exten.c, line=123, number=20, address=004AA430
  405 file=.\crypto\asn1\x_x509.c, line=117, number=84, address=004A5D20
  522 file=.\crypto\asn1\x_x509a.c, line=97, number=20, address=004ABBD0
  454 file=.\crypto\asn1\x_name.c, line=233, number=16, address=004A74F0
  503 file=.\crypto\asn1\a_object.c, line=268, number=24, address=004AAB70
  435 file=.\crypto\asn1\a_bytes.c, line=114, number=3, address=004A64C0
  475 file=.\crypto\asn1\asn1_lib.c, line=371, number=16, address=004A8340
  484 file=.\crypto\stack\stack.c, line=122, number=20, address=004A9B10
  406 file=.\crypto\asn1\x_cinf.c, line=171, number=40, address=004A5C30
  504 file=.\crypto\asn1\a_object.c, line=242, number=3, address=004AAAB0
  446 file=.\crypto\asn1\x_name.c, line=233, number=16, address=004A7BD0
  513 file=.\crypto\asn1\a_object.c, line=268, number=24, address=004AA280
  495 file=.\crypto\asn1\a_bytes.c, line=222, number=13, address=004A9220
  427 file=.\crypto\asn1\a_int.c, line=232, number=11, address=004A6B40
  456 file=.\crypto\asn1\a_object.c, line=268, number=24, address=004A7340
  417 file=.\crypto\stack\stack.c, line=122, number=20, address=004A5340
  523 file=.\crypto\asn1\asn1_lib.c, line=371, number=16, address=004AB930
  448 file=.\crypto\asn1\a_object.c, line=268, number=24, address=004A7A20
  478 file=.\crypto\asn1\a_bytes.c, line=114, number=5, address=004A80D0
  469 file=.\crypto\asn1\x_name.c, line=233, number=16, address=004A88A0
  487 file=.\crypto\asn1\asn1_lib.c, line=371, number=16, address=004A98A0
  485 file=.\crypto\stack\stack.c, line=124, number=32, address=004AA8A0
  409 file=.\crypto\asn1\x_name.c, line=219, number=16, address=004A59C0
  429 file=.\crypto\asn1\a_object.c, line=242, number=9, address=004A6990
  458 file=.\crypto\asn1\a_bytes.c, line=114, number=22, address=004A7190
  449 file=.\crypto\asn1\a_object.c, line=242, number=3, address=004A7960
  460 file=.\crypto\asn1\asn1_lib.c, line=371, number=16, address=004A8FE0
  420 file=.\crypto\asn1\x_pubkey.c, line=101, number=12, address=004A50D0
  471 file=.\crypto\asn1\a_object.c, line=268, number=24, address=004A86F0
  508 file=.\crypto\asn1\a_object.c, line=268, number=24, address=004AA6F0
  421 file=.\crypto\asn1\x_algor.c, line=102, number=8, address=004A6FE0
  480 file=.\crypto\asn1\a_object.c, line=268, number=24, address=004A9E90
  411 file=.\crypto\stack\stack.c, line=124, number=32, address=004A77B0
  472 file=.\crypto\asn1\a_object.c, line=242, number=3, address=004A8630
  490 file=.\crypto\asn1\a_bytes.c, line=222, number=5, address=004A9630
  509 file=.\crypto\asn1\a_object.c, line=242, number=3, address=004AA630
  412 file=.\crypto\buffer\buffer.c, line=67, number=12, address=004A5750
  432 file=.\crypto\asn1\asn1_lib.c, line=371, number=16, address=004A6720
  451 file=.\crypto\buffer\buffer.c, line=110, number=92, address=004A7720
  518 file=.\crypto\asn1\a_type.c, line=277, number=8, address=004ABDA0
  473 file=.\crypto\asn1\a_bytes.c, line=114, number=4, address=004A8570
  452 file=.\crypto\asn1\a_bytes.c, line=222, number=14, address=004A7690
  464 file=.\crypto\asn1\x_name.c, line=233, number=16, address=004A8CB0
  497 file=.\crypto\asn1\asn1_lib.c, line=371, number=16, address=004A9080
  502 file=.\crypto\asn1\asn1_lib.c, line=371, number=16, address=004AAC50
  419 file=.\crypto\buffer\buffer.c, line=67, number=12, address=004A51A0
  524 file=.\crypto\asn1\asn1_lib.c, line=341, number=21, address=004AB850
  424 file=.\crypto\asn1\asn1_lib.c, line=371, number=16, address=004A6D70
  488 file=.\crypto\asn1\a_object.c, line=268, number=24, address=004A97C0
  415 file=.\crypto\asn1\asn1_lib.c, line=371, number=16, address=004A54E0
  498 file=.\crypto\asn1\a_object.c, line=268, number=24, address=004AAF90
  512 file=.\crypto\asn1\asn1_lib.c, line=371, number=16, address=004AA360
  440 file=.\crypto\asn1\a_bytes.c, line=114, number=6, address=004A60B0
  466 file=.\crypto\asn1\a_object.c, line=268, number=24, address=004A8B00
  455 file=.\crypto\asn1\asn1_lib.c, line=371, number=16, address=004A7420
  436 file=.\crypto\asn1\x_name.c, line=233, number=16, address=004A63F0
  467 file=.\crypto\asn1\a_object.c, line=242, number=3, address=004A8A40
  506 file=.\crypto\asn1\x_exten.c, line=123, number=20, address=004A9A40
  407 file=.\crypto\asn1\asn1_lib.c, line=371, number=16, address=004A5B60
  514 file=.\crypto\asn1\a_object.c, line=242, number=8, address=004AA1B0
  447 file=.\crypto\asn1\asn1_lib.c, line=371, number=16, address=004A7B00
  505 file=.\crypto\asn1\a_bytes.c, line=222, number=115, address=004AA980
  496 file=.\crypto\asn1\x_exten.c, line=123, number=20, address=004A9150
  457 file=.\crypto\asn1\a_object.c, line=242, number=9, address=004A7270
  438 file=.\crypto\asn1\a_object.c, line=268, number=24, address=004A6240
  515 file=.\crypto\asn1\a_bytes.c, line=222, number=138, address=004AA060
  439 file=.\crypto\asn1\a_object.c, line=242, number=3, address=004A6180
  470 file=.\crypto\asn1\asn1_lib.c, line=371, number=16, address=004A87D0
  507 file=.\crypto\asn1\asn1_lib.c, line=371, number=16, address=004AA7D0
  410 file=.\crypto\stack\stack.c, line=122, number=20, address=004A58F0
  479 file=.\crypto\buffer\buffer.c, line=110, number=132, address=004A9F70
  459 file=.\crypto\asn1\x_name.c, line=233, number=16, address=004A70C0
  430 file=.\crypto\asn1\a_type.c, line=277, number=8, address=004A68C0
  516 file=.\crypto\asn1\a_object.c, line=268, number=24, address=004ABF40
  450 file=.\crypto\asn1\a_bytes.c, line=114, number=13, address=004A7890
  426 file=.\crypto\asn1\a_int.c, line=232, number=2, address=004A4030
  444 file=.\crypto\asn1\a_object.c, line=242, number=3, address=004A6030
  526 file=.\crypto\asn1\asn1_lib.c, line=371, number=16, address=004AB6B0
  441 file=.\crypto\asn1\x_name.c, line=233, number=16, address=004A7FA0
  500 file=.\crypto\asn1\a_bytes.c, line=222, number=23, address=004AADF0
  481 file=.\crypto\asn1\a_object.c, line=242, number=9, address=004A9DC0
  422 file=.\crypto\asn1\asn1_lib.c, line=371, number=16, address=004A6F10
  491 file=.\crypto\asn1\x_exten.c, line=123, number=20, address=004A9560
  413 file=.\crypto\asn1\x_val.c, line=95, number=8, address=004A5680
  510 file=.\crypto\asn1\a_bytes.c, line=222, number=111, address=004AA500
  443 file=.\crypto\asn1\a_object.c, line=268, number=24, address=004A7DF0
  519 file=.\crypto\asn1\a_bitstr.c, line=190, number=64, address=004ABCA0
  453 file=.\crypto\asn1\a_bytes.c, line=222, number=14, address=004A75C0
  474 file=.\crypto\asn1\x_name.c, line=233, number=16, address=004A8410
  483 file=.\crypto\asn1\a_bitstr.c, line=190, number=74, address=004A9BE0
  465 file=.\crypto\asn1\asn1_lib.c, line=371, number=16, address=004A8BE0
  493 file=.\crypto\asn1\a_object.c, line=268, number=24, address=004A93B0
  445 file=.\crypto\asn1\a_bytes.c, line=114, number=4, address=004A7CA0
  425 file=.\crypto\asn1\asn1_lib.c, line=371, number=16, address=004A6CA0
  494 file=.\crypto\asn1\a_object.c, line=242, number=3, address=004A92F0
  416 file=.\crypto\asn1\x_name.c, line=219, number=16, address=004A5410
  476 file=.\crypto\asn1\a_object.c, line=268, number=24, address=004A8260
  521 file=.\crypto\buffer\buffer.c, line=110, number=268, address=004ABA00
  437 file=.\crypto\asn1\asn1_lib.c, line=371, number=16, address=004A6320
  477 file=.\crypto\asn1\a_object.c, line=242, number=3, address=004A81A0
  486 file=.\crypto\asn1\x_exten.c, line=123, number=20, address=004A9970
  468 file=.\crypto\asn1\a_bytes.c, line=114, number=6, address=004A8970
  408 file=.\crypto\asn1\x_algor.c, line=102, number=8, address=004A5A90
  428 file=.\crypto\asn1\a_object.c, line=268, number=24, address=004A6A60
2739 bytes leaked in 121 chunks

pkcs12_parse.patch

test.c

Hi, I think I have find a memory leaks in PKCS12_parse(), the code is below, please 
use the debug library(compile with -DCRYPTO_MDEBUG) to check it:

        #define CRYPTO_MDEBUG
        #include "openssl\bio.h"
        #include "openssl\err.h"
        #include "openssl\pem.h"
        #include "openssl\pkcs12.h"
        void main()
        {
           EVP_PKEY *pkey = NULL;
           BIO *bio = NULL;
           PKCS12 *p12 = NULL;

           MemCheck_start();
           OpenSSL_add_all_algorithms();
           bio = BIO_new_file( "test.p12", "r" );
           p12 = d2i_PKCS12_bio( bio, NULL );
           PKCS12_parse( p12, "test", &pkey, NULL, NULL );
           if ( p12 ) PKCS12_free( p12 );
           if ( bio ) BIO_free( bio );
           if ( pkey ) EVP_PKEY_free( pkey );
           OBJ_cleanup();
           ERR_remove_state(0);
           EVP_cleanup();
           ERR_free_strings();
           CRYPTO_mem_leaks_fp( stderr );
        }

output is below:

  461 file=.\crypto\asn1\a_object.c, line=268, number=24, address=004A8F00
  489 file=.\crypto\asn1\a_object.c, line=242, number=3, address=004A9700
  499 file=.\crypto\asn1\a_object.c, line=242, number=3, address=004AAED0
  ...
  428 file=.\crypto\asn1\a_object.c, line=268, number=24, address=004A6A60
2739 bytes leaked in 121 chunks


the patch is below:

diff -Nur openssl-0.9.6a-beta3/crypto/pkcs12/p12_kiss.c 
openssl-0.9.6a-beta3-1/crypto/pkcs12/p12_kiss.c
--- openssl-0.9.6a-beta3/crypto/pkcs12/p12_kiss.c       Mon Sep 18 02:21:10 2000
+++ openssl-0.9.6a-beta3-1/crypto/pkcs12/p12_kiss.c     Tue Apr 03 16:13:01 2001
@@ -264,6 +264,7 @@
                if (lkey) {
                        *keymatch |= MATCH_CERT;
                        if (cert) *cert = x509;
+                       else X509_free(x509);
                } else {
                        if(ca) sk_X509_push (*ca, x509);
                        else X509_free(x509);

this bug also exist in openssl-0.9.6.

regards,
Shijin

Reply via email to