Hello,

I have the following code nothing fancy just opening a pkcs12 file and then trying to free every thing. when I run the code though purify I get an error on sk_X509_pop_free, Freeing unallocated memeory, Freeing memory read. If I don't uses sk_X509_free I get a memory leek but I don't see these errors.

Can someone please tell me the correct why to free a stack of certs?

int PKCS12PasswordVerify( char *p12FilePath, char *password )
{
  PKCS12         *p12   = (PKCS12 *)NULL;
  BIO            *fp    = (BIO *)NULL;
  EVP_PKEY       *pkey  = (EVP_PKEY *)NULL;
  X509           *cert  = (X509 *)NULL;
  STACK_OF(X509) *ca    = NULL;
  int            status = PKCS12_ERR_GENERAL_FAILURE;
  static int     first  = PKCS12_TRUE;

 /* Loads all openssl needed algorithms and crypto strings */
 if( PKCS12_TRUE == first )
 {
   OpenSSL_add_all_algorithms();
   ERR_load_crypto_strings();
   first = PKCS12_FALSE;
 }

 /* Opens file pointer to pkcs12 file */
 fp = BIO_new_file(p12FilePath, "rb");
 if( (BIO *)NULL != fp )
 {
   p12 = d2i_PKCS12_bio( fp, NULL );
   if( (PKCS12 *)NULL != p12 )
   {
     if( 1 == PKCS12_parse(p12, password, &pkey, &cert, &ca) )
     {
       status = PKCS12_SUCCESS;      }
     else
     {
       status = PKCS12_ERR_PARSING_PKCS12_FILE;
     }
   }
   else
   {
      /* Bad PKCS12 file */
      status = PKCS12_ERR_LOADING_PKCS12_FILE;
   }
 }
 else
 {
   /* Error Opening file */
   status = PKCS12_ERR_OPENING_PKCS12_FILE;
 }

 /* Free every thing */
 if( (BIO *)NULL != fp )
 {
   BIO_free( fp );
   fp = (BIO *)NULL;
 }
 if( NULL != ca )
 {
   sk_X509_pop_free( ca, X509_free );
   ca = NULL;
 }  if( (X509 *)NULL != cert )
 {
   X509_free(cert);
   cert = (X509 *)NULL;
 }
 if( (EVP_PKEY *)NULL != pkey )
 {
   EVP_PKEY_free(pkey);
   pkey = (EVP_PKEY *)NULL;
 }
 if( (PKCS12 *)NULL != p12 )
 {
   PKCS12_free(p12);
   p12 = (PKCS12 *)NULL;
 }

 return status;
}

Thanks,
Perry

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

Reply via email to