Hi All,

I’ve been finding it difficult to manage memory while using OpenSSL d2i functions.

 

 

I have written the following function to Sign OCSP request.

 

      unsigned char *in = new unsigned char[OCSPRequestData.GetLength()];

      memcpy(in, OCSPRequestData.GetData(), OCSPRequestData.GetLength());

      pOCSPRequest = d2i_OCSP_REQUEST(NULL, &in, OCSPRequestData.GetLength());

     

now when I try to use

      if(in)     

            delete[] in;

 

my application crashes and if I don’t delete then compiler tells me that it’s a memory leak.

 

More over if anyone can take a look at the code below and point out the place where I am not doing right to avoid memory leaks.

I have tried to free every memory though.

 

 

 

bool SignOCSPRequest(const std::string &strPFXPath, const std::string &strPFXPassword,

                                          CAscByteArray &OCSPRequestData, CAscByteArray &signedOCSPRequestdata) {

      bool bRetVal = true;

      EVP_PKEY  *pPrivateKey = NULL;

      X509 *pSignerCert = NULL;

      PKCS7 *pPKCS7 =  NULL;

      PKCS12 *pPKCS12 = NULL;

      STACK_OF(X509) *pskSignerCertChain = NULL;

      OCSP_REQUEST *pOCSPRequest = NULL;

      const EVP_MD *pDigest;

     

      // OpenSSL Algorithm Initialization process

      SSLeay_add_all_algorithms();

      // OpenSSL Error strings loading

      ERR_load_crypto_strings();

 

      // create OCSP request object from

     

      unsigned char *in = new unsigned char[OCSPRequestData.GetLength()];

      memcpy(in, OCSPRequestData.GetData(), OCSPRequestData.GetLength());

      pOCSPRequest = d2i_OCSP_REQUEST(NULL, &in, OCSPRequestData.GetLength());

 

      if(!pOCSPRequest)

      {

            m_strLastError = "Unable to create OCSP Request from Data";

            bRetVal = false;

      }

     

 

      if(bRetVal != false)

      {

            // this obejct will contain the p12 file pointer.

            FILE *pFile = NULL;

            pFile = fopen(strPFXPath.c_str(),"rb");

            if(!pFile)

            {

                  m_strLastError = "Unable to open the PFX File at "+strPFXPath;

                  bRetVal =  false;

            }

 

            if(bRetVal != false)

            {

                  pPKCS12 = d2i_PKCS12_fp(pFile,NULL);

                  if(!pPKCS12)

                  {

                        m_strLastError = "d2i_PKCS12_fp failed for: "+strPFXPath;

                        bRetVal = false;

                  }

                 

                  fclose(pFile);

           

                  if(bRetVal != false)

                  {

                        if(PKCS12_parse(pPKCS12, strPFXPassword.c_str(), &pPrivateKey,

                        &pSignerCert, &pskSignerCertChain) == PKCS12_ERROR)

                        {

                              m_strLastError = ERR_error_string(ERR_get_error(),NULL);

                              bRetVal = false;

 

                        }

 

                        if(bRetVal != false)

                        {

                              pDigest = EVP_get_digestbyname("sha1");

                              OCSP_request_sign(pOCSPRequest,pSignerCert,pPrivateKey,pDigest,NULL, 0);

                              int nSignedOCSPReqLen = i2d_OCSP_REQUEST(pOCSPRequest,NULL);

                              unsigned char *pDerEncodedOCSPReq = new unsigned char[nSignedOCSPReqLen];

                              i2d_OCSP_REQUEST(pOCSPRequest,&pDerEncodedOCSPReq);

                              pDerEncodedOCSPReq -= nSignedOCSPReqLen;

                             

                              CAscByteArray bTempArray(pDerEncodedOCSPReq,nSignedOCSPReqLen);

                              signedOCSPRequestdata = bTempArray;

                       

                              delete[] pDerEncodedOCSPReq;

 

                        }

                  }

            }

      }

     

      //if(pDigest)

            //EVP_MD_free(pDigest);

     

//    EVP_MD_cleanup(pDigest);

     

      if(pPKCS12)

            PKCS12_free(pPKCS12);

 

      if(pPrivateKey)

        EVP_PKEY_free(pPrivateKey);

     

      if(pOCSPRequest)

            OCSP_REQUEST_free(pOCSPRequest);

 

      if(pPKCS7)

            PKCS7_free(pPKCS7);

 

      if(pSignerCert)

            X509_free(pSignerCert);

 

      if(pskSignerCertChain)

            sk_X509_free(pskSignerCertChain);

       

      ERR_free_strings();

      EVP_cleanup();

      CRYPTO_cleanup_all_ex_data();

 

    return bRetVal;

     

}

 

And I am using the code below to detect memory leaks

 

const long memAllocNum = 0; // Enter the memory block allocation number that Microsoft declares is a leak here.

      // Set the debug flags for memory leak checking

      _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);

      int debugFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);

      debugFlag = _CrtSetDbgFlag(debugFlag | _CRTDBG_LEAK_CHECK_DF );

      // Set a breakpoint on the allocation request number

      if ((memAllocNum > 0) && IsDebuggerPresent())

            _CrtSetBreakAlloc(memAllocNum);

 

 

 

 

Regards

 

Muhammad Aftab Alam

 

 

 

Reply via email to