2

我在这里做错了什么?创建证书上下文时,我不断收到错误 0x8009310b(遇到 ASN1 错误标记值)。我知道我正在测试的证书很好。我使用 DER、Base-64 和 P7B 格式从中间存储中导出了它。这三种情况都失败了。

int _tmain(int argc, _TCHAR* argv[])
{
    const int       MAX_CERT_FILE_SIZE=81920;
    HANDLE          certFileHandle;
    DWORD           certEncodedRead = 0L;
    BYTE            certData[MAX_CERT_FILE_SIZE] = {0};
    PCCERT_CONTEXT  pCertContext = NULL;
    HCERTSTORE      hSystemStore = NULL;
    int             exitCode = 0;

    fprintf(stdout, "Importing X509 certificate file to root store: %s \n\n", argv[0]);

    try {

        // Create a handle to the certificate given in the command line argument
        BeginTask("Creating certificate handle...");
        certFileHandle = CreateFile(argv[0],
            GENERIC_READ,
            0,
            NULL,
            OPEN_EXISTING,
            FILE_ATTRIBUTE_NORMAL,
            NULL);

        if (INVALID_HANDLE_VALUE == certFileHandle){
            throw "Could not create a handle to the specified certificate file.";
        } 

        // Read the certificate file
        NextTask("Reading certificate file into buffer...");
        memset(certData, 0, MAX_CERT_FILE_SIZE);
        BOOL result = ReadFile(certFileHandle,
                certData,
                MAX_CERT_FILE_SIZE,
                &certEncodedRead,
                NULL);
        fprintf(stdout, "Read %d bytes from certificate file...", certEncodedRead);

        if (!result) {
            throw "Could not read the certificate file.";
        } 

        // Create a certificate context from the buffer
        NextTask("Creating certificate context...");
        pCertContext = CertCreateCertificateContext(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, certData, certEncodedRead);

        if (!pCertContext){
            throw "Could not create a certificate context.";    
        }

        // Open the system certificate store
        NextTask("Opening local machine certificate store...");
        hSystemStore = CertOpenSystemStore(NULL, L"CA"); 
        if (!hSystemStore){
            throw "Could not open the local machine certificate store.";        
        }

        // Add certificate context to store
        NextTask("Adding certificate context to store...");     
        //CertAddCertificateContextToStore(hSystemStore, 
        //  pCertContext, 
        //  CERT_STORE_ADD_REPLACE_EXISTING, 
        //  NULL);

    } catch (ERRMSG msg) {
        Result(false);
        HandleError(msg);
        exitCode = 1;
    } 

    // Clean-up all resources
    if (hSystemStore) {
        NextTask("Closing certificate store...");
        Result(CertCloseStore(hSystemStore, 0));
    }
    if (pCertContext) {
        NextTask("Freeing certificate store...");
        Result(CertFreeCertificateContext(pCertContext));
    }
    if (certFileHandle) {
        NextTask("Closing certificate file...");
        Result(CloseHandle(certFileHandle));
    }

    fprintf(stdout, "\n\nProgram complete-exiting with code %x", exitCode);
    return exitCode;
}

[编辑添加控制台输出]

Importing X509 certificate file to root store: DOD-CA-12.cer

Creating certificate handle...Success.
Reading certificate file into buffer...Read 41472 bytes from certificate file...Success.
Creating certificate context...Failed.
An error occurred while importing the X509 certificate.
Narrative: Could not create a certificate context.
GetLastError reported: 8009310b.
Success.
Closing certificate file...Success.


Program complete-exiting with code 1
4

1 回答 1

2

感谢WhozCraig注意到文件大小。

这里的问题是基本的 C++ 101,其中作为命令行参数的文件是 args[1] 而不是 args[0]。该exe基本上是在加载自己。

于 2013-10-15T23:24:22.470 回答