解析来自mozilla.rsa文件的插件ID

问题描述 投票:7回答:1

我想读mozilla.rsa文件,并使用C ++解析插件标识。

我的努力:

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

有什么办法来读取mozilla.rsa文件附加ID使用C或C ++?

c++ certificate firefox-addon mozilla
1个回答
3
投票

事实上,该文件可以被读取并与Windows CryptoAPI的解析。

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

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

签名文件包含证书链。他们中的一个具有附加ID在主题领域的CN组件。

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值更好的方法,例如正则表达式。

© www.soinside.com 2019 - 2024. All rights reserved.