【问题标题】:Verify OpenPGP based RSA signature with WinCrypt/CryptoAPI使用 WinCrypt/CryptoAPI 验证基于 OpenPGP 的 RSA 签名
【发布时间】:2014-09-12 18:27:23
【问题描述】:

我有解析 OpenPGP 数据包的代码,我有公钥数据包的ne 以及签名数据包的s 作为字节数组。

为了验证签名我首先初始化CryptAcquireContext(我也尝试使用PROV_RSA_FULL而不是PROV_RSA_AES

HCRYPTPROV hCryptProv;
CryptAcquireContext(&hCryptProv, nullptr, nullptr, PROV_RSA_AES, CRYPT_VERIFYCONTEXT);

然后创建一个哈希

HCRYPTHASH hHash;
CryptCreateHash(hCryptProv, CALG_SHA1, 0, 0, &hHash); // as the digest algorithm of the signature was 2 => SHA1

并使用CryptHashData 填充它。到目前为止,这与使用 CryptImportKey 解析和导入公钥一样有效。

typedef struct _RSAKEY
{
    BLOBHEADER blobheader;
    RSAPUBKEY rsapubkey;
    BYTE n[4096 / 8];
} RSAKEY;

static int verify_signature_rsa(HCRYPTPROV hCryptProv, HCRYPTHASH hHash, public_key_t &p_pkey, signature_packet_t &p_sig)
{
    int i_n_len = mpi_len(p_pkey.key.sig.rsa.n); // = 512; p_pkey.key.sig.rsa.n is of type uint8_t n[2 + 4096 / 8];
    int i_s_len = mpi_len(p_sig.algo_specific.rsa.s); // = 256; p_sig.algo_specific.rsa.s is of type uint8_t s[2 + 4096 / 8]

    HCRYPTKEY hPubKey;
    RSAKEY rsakey;
    rsakey.blobheader.bType = PUBLICKEYBLOB; // 0x06
    rsakey.blobheader.bVersion = CUR_BLOB_VERSION; // 0x02
    rsakey.blobheader.reserved = 0;
    rsakey.blobheader.aiKeyAlg = CALG_RSA_KEYX;
    rsakey.rsapubkey.magic = 0x31415352;// ASCII for RSA1
    rsakey.rsapubkey.bitlen = i_n_len * 8; // = 4096
    rsakey.rsapubkey.pubexp = 65537;

    memcpy(rsakey.n, p_pkey.key.sig.rsa.n + 2, i_n_len); // skip first two byte which are MPI length
    std::reverse(rsakey.n, rsakey.n + i_n_len); // need to convert to little endian for WinCrypt

    CryptImportKey(hCryptProv, (BYTE*)&rsakey, sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + i_n_len, 0, 0, &hPubKey); // no error

    std::unique_ptr<BYTE[]> pSig(new BYTE[i_s_len]);
    memcpy(pSig.get(), p_sig.algo_specific.rsa.s + 2, i_s_len); // skip first two byte which are MPI length
    std::reverse(p_sig.algo_specific.rsa.s, p_sig.algo_specific.rsa.s + i_s_len); // need to convert to little endian for WinCrypt

    if (!CryptVerifySignature(hHash, pSig.get(), i_s_len, hPubKey, nullptr, 0))
    {
        DWORD err = GetLastError(); // err=2148073478 -> INVALID_SIGNATURE
        CryptDestroyKey(hPubKey);
        return -1;
    }

    CryptDestroyKey(hPubKey);
    return 0;
}

CryptVerifySignature 失败,GetLastError() 解码为 INVALID_SIGNATURE

https://www.rfc-editor.org/rfc/rfc4880#section-5.2.2我读过

With RSA signatures, the hash value is encoded using PKCS#1 encoding
type EMSA-PKCS1-v1_5 as described in Section 9.2 of RFC 3447.  This
requires inserting the hash value as an octet string into an ASN.1
structure.

这是需要还是由CryptVerifySignature 自动完成?如果没有,该怎么做?

【问题讨论】:

    标签: c++ cryptography rsa cryptoapi mscapi


    【解决方案1】:

    PKCS#1 填充不太可能是问题所在。默认情况下它使用 OID 作为哈希算法的提示是指向 PKCS#1 v1.5 类型的签名,所以我认为您可以放心使用正确的填充。

    更多确认可在CryptSignHash documentation:

    默认情况下,Microsoft RSA 提供程序使用 PKCS #1 填充方法进行签名。签名的 DigestInfo 元素中的哈希 OID 自动设置为与哈希对象关联的算法 OID。使用 CRYPT_NOHASHOID 标志将导致此 OID 从签名中省略。


    翻阅 API 文档,以下内容引起了我的注意:

    本机加密 API 使用 little-endian 字节顺序,而 .NET Framework API 使用 big-endian 字节顺序。如果您正在验证使用 .NET Framework API 生成的签名,则必须在调用 CryptVerifySignature 函数来验证签名之前交换签名字节的顺序。

    这确实意味着 API 不符合 PKCS#1 v1.5,因为其中明确指定了字节顺序。因此,这当然是需要注意的事情,并且可能是解决方案的一部分。

    【讨论】:

    • .NET 稍微好一点,但我看不出一家公司如何能够摆脱如此糟糕的 API 文档。
    • 感谢您的回答和您的努力。错误是由拼写错误和您认为它的位置引起的......
    • 你所说的“我想它在哪里?”是什么意思,与大/小字节序有关吗?
    • 查看我提供的答案。我的(愚蠢的)错误是在字节序转换中。
    • 啊,好吧,你确实做了字节序转换,但它返回了错误的结果,应该继续阅读:) 很高兴你解决了。
    【解决方案2】:

    错误在这一行

    std::reverse(p_sig.algo_specific.rsa.s, p_sig.algo_specific.rsa.s + i_s_len); // need to convert to little endian for WinCrypt
    

    应该读什么

    std::reverse(pSig.get(), pSig.get() + i_s_len); // need to convert to little endian for WinCrypt
    

    因为将字节源从大端转换为小端不会在复制后转换另一个缓冲区。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-15
      • 1970-01-01
      • 2017-07-30
      相关资源
      最近更新 更多