从mozilla.rsa文件中解析插件ID

Jay*_*esh 7 c++ mozilla certificate firefox-addon

我试图阅读mozilla.rsa文件并使用C++解析插件ID.

我的努力:

std::string rsaPath = xpiDir + "\\META-INF\\mozilla.rsa";

int rets = system(("CertUtil " + rsaPath + " | findstr " + "S=CA").c_str());

//
.....
My additional logic.............
.....
///
Run Code Online (Sandbox Code Playgroud)

它在Windows 7及更高版本上运行良好.但是,在Windows XP上没有.

CERTUTIL

有没有办法使用C或C++ 从mozilla.rsa文件中读取插件ID ?

voi*_*oid 4

事实上,可以使用 Windows CryptoAPI 读取和解析该文件。

mozilla.rsaMozilla扩展的文件是PKCS#7签名。在Linux上可以使用以下命令查看:

openssl pkcs7 -print -inform der -in META-INF/mozilla.rsa

签名文件包含证书链。其中之一在其主题字段的 CN 组件中具有插件 ID。

这个 Stack Overflow 答案解释了如何使用 CryptoAPICryptQueryObject()函数解析 PKCS#7 数据。

作为参考,微软支持还有更详细的解析示例: https: //support.microsoft.com/en-us/help/323809/how-to-get-information-from-authenticode-signed-executables

使用所有这些源,可以编译以下代码来打印所需的 ID:

#include <windows.h>
#include <wincrypt.h>
#pragma comment(lib, "crypt32.lib")

...

std::string rsaPath = xpiDir + "\\META-INF\\mozilla.rsa";
std::wstring wRsaPath(rsaPath.begin(), rsaPath.end());

HCERTSTORE hStore = NULL;
HCRYPTMSG hMsg = NULL;
BOOL res = CryptQueryObject(
    CERT_QUERY_OBJECT_FILE,
    wRsaPath.c_str(),
    CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED,
    CERT_QUERY_FORMAT_FLAG_BINARY,
    0,
    NULL,
    NULL,
    NULL,
    &hStore,
    &hMsg,
    NULL
);
if (!res) {
    std::cout << "Error decoding PKCS#7 file: " << GetLastError() << std::endl;
    return -1;
}

PCCERT_CONTEXT next_cert = NULL;
while ((next_cert = CertEnumCertificatesInStore(hStore, next_cert)) != NULL)
{
    WCHAR szName[1024];
    // Get subject name
    if (!CertGetNameString(
        next_cert,
        CERT_NAME_SIMPLE_DISPLAY_TYPE,
        0,
        NULL,
        szName,
        1024
    )) {
        std::cout << "CertGetNameString failed.\n";
        return -1;
    }

    // only process names looking like IDs, e.g. "CN={212b458b-a608-452b-be1f-a09658163cbf}"
    if (szName[0] == L'{') {
        std::wcout << szName << std::endl;
    }
}

CryptMsgClose(hMsg);
CertCloseStore(hStore, 0);
Run Code Online (Sandbox Code Playgroud)

该比较szName[0] == L'{'不是很可靠,只是为了代码简单而使用。人们可能想要使用更好的方法来检测插件 ID 值,例如正则表达式。