c++ - 从 mozilla.rsa 文件中解析插件 ID

标签 c++ certificate firefox-addon mozilla

我正在尝试读取 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.............
.....
///

它在 Windows 7 和更高版本上运行良好。但是,Windows xp 上没有。

CertUtil

有什么方法可以使用 C 或 C++ 从 mozilla.rsa 文件中读取插件 ID?

最佳答案

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

Mozilla 扩展的文件 mozilla.rsaa PKCS#7 signature .在 Linux 上可以使用以下命令查看:

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

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

This Stack Overflow answer说明如何使用 CryptoAPI CryptQueryObject() 函数解析 PKCS#7 数据。

作为引用,Microsoft 支持还提供了更详细的解析示例: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);

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

关于c++ - 从 mozilla.rsa 文件中解析插件 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54089986/

相关文章:

C++ libCurl : Post callback function to send more than one byte

c++ - SSL_set_tlsext_host_name 崩溃

c++ - 查明文件路径是映射/远程还是本地

iphone 应用程序 : how to renew an expired distribution certificate through the iOS provisioning portal

firefox-addon - 如何在我的扩展侧边栏中打开 Firefox 开发者工具?

firefox - 避免在 Firefox 扩展中自动隐藏面板

c++ - 在 SDL 2.0 中创建 1x1 纹理

ubuntu -/etc/nginx/sites-enabled/default 中 "ssl_certificate_key"指令中的参数数量无效

java - DefaultHttpClient、证书、Https 和发布问题!

javascript - Firefox Addon 中的 JQuery 导致多个警告