2

ここで何が間違っていますか?証明書コンテキストを作成するときに、エラー 0x8009310b (ASN1 bad tag value met) が発生し続けます。テストしている証明書が優れていることはわかっています。DER、Base-64、および P7B 形式を使用して中間ストアからエクスポートしました。3 つのシナリオはすべて失敗します。

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[0] ではなく args[1] です。exeは基本的にそれ自体をロードしていました。

于 2013-10-15T23:24:22.470 に答える