不断增加物理内存 Visual C++证书获取证书链

发布于 2024-11-30 10:40:33 字数 6805 浏览 0 评论 0原文

我正在使用 C++ 和 Visual Studio 2005。

我不知道为什么,我尝试更改代码顺序,但这看起来很奇怪。运行一个简单的项目时物理内存会增加。

//has to include crypt32.lib in links
#include <windows.h>

void memoryUP2( PCCERT_CONTEXT __pCert )
{
    PCCERT_CHAIN_CONTEXT     _pChainContext     = NULL;
    CERT_ENHKEY_USAGE        _EnhkeyUsage;
    CERT_USAGE_MATCH         _CertUsage;  
    CERT_CHAIN_PARA          _ChainPara;
    DWORD                    _dwFlags           = CERT_CHAIN_RETURN_LOWER_QUALITY_CONTEXTS; //0;

    _EnhkeyUsage.cUsageIdentifier = 0;
    _EnhkeyUsage.rgpszUsageIdentifier=NULL;
    _CertUsage.dwType = USAGE_MATCH_TYPE_AND;
    _CertUsage.Usage  = _EnhkeyUsage;
    _ChainPara.cbSize = sizeof(CERT_CHAIN_PARA);
    _ChainPara.RequestedUsage=_CertUsage;

    {
        if(!CertGetCertificateChain(
            NULL,                  // default engine
            __pCert,   // certificate ctx
            NULL,                  // time
            NULL,                  // default store
            &_ChainPara,            // use AND logic and enhanced key usage 
                                    //  as indicated in the ChainPara 
                                    //  data structure
            _dwFlags,
            NULL,                  // currently reserved
            &_pChainContext))       // return a pointer to the chain created
        {
            //Error... Do nothing
        }
    }

    if (_pChainContext != NULL)
    {
        CertFreeCertificateChain(_pChainContext);
    }
}

使用这个,“内存不增加”:

int main(int argc, char** argv)
{
    MyFile myfile = myReadFile("c:\\certificate.cer");

    for(int i=0;i<550000;++i)
    {

        PCCERT_CONTEXT certCtx = CertCreateCertificateContext( (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING),
                                                                      (const BYTE *)myfile._data, 
                                                                      myfile._length);

        if(certCtx!=NULL)
            CertFreeCertificateContext(certCtx);
    }

    return 0;
}

使用这个,“内存不断增加”:

int main(int argc, char** argv)
{
    MyFile myfile = myReadFile("c:\\certificate.cer");

    for(int i=0;i<80000;++i)
    {

        PCCERT_CONTEXT certCtx = CertCreateCertificateContext( (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING),
                                                                      (const BYTE *)myfile._data, 
                                                                      myfile._length);
        memoryUP2(certCtx);

        if(certCtx!=NULL)
            CertFreeCertificateContext(certCtx);
    }

    return 0;
}

使用这个,“内存不增加”:

int main(int argc, char** argv)
{
    MyFile myfile = myReadFile("c:\\certificate.cer");

    PCCERT_CONTEXT certCtx = CertCreateCertificateContext( (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING),
                                                                      (const BYTE *)myfile._data, 
                                                                      myfile._length);
    for(int i=0;i<80000;++i)
    {
        memoryUP2(certCtx);
    }

    if(certCtx!=NULL)
            CertFreeCertificateContext(certCtx);

    return 0;
}

使用这个,“内存不断增加”:

int main(int argc, char** argv)
{
    MyFile myfile = myReadFile("c:\\certificate.cer");

    for(int j=0;j<5000;j++)
    {
        PCCERT_CONTEXT certCtx = CertCreateCertificateContext( (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING),
                                                                          (const BYTE *)myfile._data, 
                                                                          myfile._length);
        for(int i=0;i<50;++i)
        {
            memoryUP2(certCtx);
        }

        if(certCtx!=NULL)
                CertFreeCertificateContext(certCtx);
    }

    return 0;
}

我如何使用这个 没有内存泄漏?

for(int i=0;i<80000;++i)
{

    PCCERT_CONTEXT certCtx = CertCreateCertificateContext( (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING),
                                                                  (const BYTE *)myfile._data, 
                                                                  myfile._length);
    memoryUP2(certCtx);

    if(certCtx!=NULL)
        CertFreeCertificateContext(certCtx);
}

为什么会出现这种情况?

更新 #1

我认为这与 CertCreateCertificateChainEngine 有关,请查看 MSDN 创建证书链

我将代码更改为:

void memoryUP2( PCCERT_CONTEXT __pCert )
{
    PCCERT_CHAIN_CONTEXT     _pChainContext     = NULL;
    CERT_ENHKEY_USAGE        _EnhkeyUsage;
    CERT_USAGE_MATCH         _CertUsage;  
    CERT_CHAIN_PARA          _ChainPara;
    DWORD                    _dwFlags           = CERT_CHAIN_RETURN_LOWER_QUALITY_CONTEXTS; //0;

    HCERTCHAINENGINE         hChainEngine;
    CERT_CHAIN_ENGINE_CONFIG ChainConfig;

    ChainConfig.cbSize = sizeof(CERT_CHAIN_ENGINE_CONFIG);
    ChainConfig.hRestrictedRoot= NULL ;
    ChainConfig.hRestrictedTrust= NULL ;
    ChainConfig.hRestrictedOther= NULL ;
    ChainConfig.cAdditionalStore=0 ;
    ChainConfig.rghAdditionalStore = NULL ;
    ChainConfig.dwFlags = CERT_CHAIN_CACHE_END_CERT;
    ChainConfig.dwUrlRetrievalTimeout= 0 ;
    ChainConfig.MaximumCachedCertificates=0 ;
    ChainConfig.CycleDetectionModulus = 0;

    CertCreateCertificateChainEngine(
         &ChainConfig,
         &hChainEngine);

    _EnhkeyUsage.cUsageIdentifier = 0;
    _EnhkeyUsage.rgpszUsageIdentifier=NULL;
    _CertUsage.dwType = USAGE_MATCH_TYPE_AND;
    _CertUsage.Usage  = _EnhkeyUsage;
    _ChainPara.cbSize = sizeof(CERT_CHAIN_PARA);
    _ChainPara.RequestedUsage=_CertUsage;

    {
        if(!CertGetCertificateChain(
            NULL,                  // default engine
            __pCert,   // certificate ctx
            NULL,                  // time
            NULL,                  // default store
            &_ChainPara,            // use AND logic and enhanced key usage 
                                    //  as indicated in the ChainPara 
                                    //  data structure
            _dwFlags,
            NULL,                  // currently reserved
            &_pChainContext))       // return a pointer to the chain created
        {
            //Error... Do nothing
        }
    }

    if (_pChainContext != NULL)
    {
        CertFreeCertificateChain(_pChainContext);
        _pChainContext = NULL;
    }

    CertFreeCertificateChainEngine(hChainEngine);
}

内存“不会增加”,仅“很少”。

为什么默认引擎会导致内存泄漏?
为什么声明引擎并使用 CertGetCertificateChain(NULL,...) 参数?

I am using C++ and Visual Studio 2005.

I dont know why, I tried to change the code order but this seems strange. The phisycal memory increases on run a simple project.

//has to include crypt32.lib in links
#include <windows.h>

void memoryUP2( PCCERT_CONTEXT __pCert )
{
    PCCERT_CHAIN_CONTEXT     _pChainContext     = NULL;
    CERT_ENHKEY_USAGE        _EnhkeyUsage;
    CERT_USAGE_MATCH         _CertUsage;  
    CERT_CHAIN_PARA          _ChainPara;
    DWORD                    _dwFlags           = CERT_CHAIN_RETURN_LOWER_QUALITY_CONTEXTS; //0;

    _EnhkeyUsage.cUsageIdentifier = 0;
    _EnhkeyUsage.rgpszUsageIdentifier=NULL;
    _CertUsage.dwType = USAGE_MATCH_TYPE_AND;
    _CertUsage.Usage  = _EnhkeyUsage;
    _ChainPara.cbSize = sizeof(CERT_CHAIN_PARA);
    _ChainPara.RequestedUsage=_CertUsage;

    {
        if(!CertGetCertificateChain(
            NULL,                  // default engine
            __pCert,   // certificate ctx
            NULL,                  // time
            NULL,                  // default store
            &_ChainPara,            // use AND logic and enhanced key usage 
                                    //  as indicated in the ChainPara 
                                    //  data structure
            _dwFlags,
            NULL,                  // currently reserved
            &_pChainContext))       // return a pointer to the chain created
        {
            //Error... Do nothing
        }
    }

    if (_pChainContext != NULL)
    {
        CertFreeCertificateChain(_pChainContext);
    }
}

Using this, "the memory Doesn't increases":

int main(int argc, char** argv)
{
    MyFile myfile = myReadFile("c:\\certificate.cer");

    for(int i=0;i<550000;++i)
    {

        PCCERT_CONTEXT certCtx = CertCreateCertificateContext( (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING),
                                                                      (const BYTE *)myfile._data, 
                                                                      myfile._length);

        if(certCtx!=NULL)
            CertFreeCertificateContext(certCtx);
    }

    return 0;
}

Using this, "the memory increases continuously":

int main(int argc, char** argv)
{
    MyFile myfile = myReadFile("c:\\certificate.cer");

    for(int i=0;i<80000;++i)
    {

        PCCERT_CONTEXT certCtx = CertCreateCertificateContext( (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING),
                                                                      (const BYTE *)myfile._data, 
                                                                      myfile._length);
        memoryUP2(certCtx);

        if(certCtx!=NULL)
            CertFreeCertificateContext(certCtx);
    }

    return 0;
}

Using this, "the memory Doesn't increases":

int main(int argc, char** argv)
{
    MyFile myfile = myReadFile("c:\\certificate.cer");

    PCCERT_CONTEXT certCtx = CertCreateCertificateContext( (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING),
                                                                      (const BYTE *)myfile._data, 
                                                                      myfile._length);
    for(int i=0;i<80000;++i)
    {
        memoryUP2(certCtx);
    }

    if(certCtx!=NULL)
            CertFreeCertificateContext(certCtx);

    return 0;
}

Using this, "the memory increases continuously":

int main(int argc, char** argv)
{
    MyFile myfile = myReadFile("c:\\certificate.cer");

    for(int j=0;j<5000;j++)
    {
        PCCERT_CONTEXT certCtx = CertCreateCertificateContext( (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING),
                                                                          (const BYTE *)myfile._data, 
                                                                          myfile._length);
        for(int i=0;i<50;++i)
        {
            memoryUP2(certCtx);
        }

        if(certCtx!=NULL)
                CertFreeCertificateContext(certCtx);
    }

    return 0;
}

How can I use this Without memory leak?

for(int i=0;i<80000;++i)
{

    PCCERT_CONTEXT certCtx = CertCreateCertificateContext( (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING),
                                                                  (const BYTE *)myfile._data, 
                                                                  myfile._length);
    memoryUP2(certCtx);

    if(certCtx!=NULL)
        CertFreeCertificateContext(certCtx);
}

Why this happens?

Update #1

I think this is related to CertCreateCertificateChainEngine, looking on MSDN Creating a Certificate Chain

I change the code to:

void memoryUP2( PCCERT_CONTEXT __pCert )
{
    PCCERT_CHAIN_CONTEXT     _pChainContext     = NULL;
    CERT_ENHKEY_USAGE        _EnhkeyUsage;
    CERT_USAGE_MATCH         _CertUsage;  
    CERT_CHAIN_PARA          _ChainPara;
    DWORD                    _dwFlags           = CERT_CHAIN_RETURN_LOWER_QUALITY_CONTEXTS; //0;

    HCERTCHAINENGINE         hChainEngine;
    CERT_CHAIN_ENGINE_CONFIG ChainConfig;

    ChainConfig.cbSize = sizeof(CERT_CHAIN_ENGINE_CONFIG);
    ChainConfig.hRestrictedRoot= NULL ;
    ChainConfig.hRestrictedTrust= NULL ;
    ChainConfig.hRestrictedOther= NULL ;
    ChainConfig.cAdditionalStore=0 ;
    ChainConfig.rghAdditionalStore = NULL ;
    ChainConfig.dwFlags = CERT_CHAIN_CACHE_END_CERT;
    ChainConfig.dwUrlRetrievalTimeout= 0 ;
    ChainConfig.MaximumCachedCertificates=0 ;
    ChainConfig.CycleDetectionModulus = 0;

    CertCreateCertificateChainEngine(
         &ChainConfig,
         &hChainEngine);

    _EnhkeyUsage.cUsageIdentifier = 0;
    _EnhkeyUsage.rgpszUsageIdentifier=NULL;
    _CertUsage.dwType = USAGE_MATCH_TYPE_AND;
    _CertUsage.Usage  = _EnhkeyUsage;
    _ChainPara.cbSize = sizeof(CERT_CHAIN_PARA);
    _ChainPara.RequestedUsage=_CertUsage;

    {
        if(!CertGetCertificateChain(
            NULL,                  // default engine
            __pCert,   // certificate ctx
            NULL,                  // time
            NULL,                  // default store
            &_ChainPara,            // use AND logic and enhanced key usage 
                                    //  as indicated in the ChainPara 
                                    //  data structure
            _dwFlags,
            NULL,                  // currently reserved
            &_pChainContext))       // return a pointer to the chain created
        {
            //Error... Do nothing
        }
    }

    if (_pChainContext != NULL)
    {
        CertFreeCertificateChain(_pChainContext);
        _pChainContext = NULL;
    }

    CertFreeCertificateChainEngine(hChainEngine);
}

Memory "Doesn't increases" Only "very little".

Why default engine cause memory leak?
Why declare engine and use CertGetCertificateChain(NULL,...) param?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文