gpt4 book ai didi

c++ - 使用 WinCrypt/CryptoAPI 验证基于 OpenPGP 的 RSA 签名

转载 作者:搜寻专家 更新时间:2023-10-31 01:43:06 25 4
gpt4 key购买 nike

我有解析 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 自动完成?如果没有,怎么办?

最佳答案

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

更多确认可以在CryptSignHash documentation中找到:

By default, the Microsoft RSA providers use the PKCS #1 padding method for the signature. The hash OID in the DigestInfo element of the signature is automatically set to the algorithm OID associated with the hash object. Using the CRYPT_NOHASHOID flag will cause this OID to be omitted from the signature.


通过查看 API 文档,以下内容引起了我的注意:

The native cryptography API uses little-endian byte order while the .NET Framework API uses big-endian byte order. If you are verifying a signature generated by using a .NET Framework API, you must swap the order of signature bytes before calling the CryptVerifySignature function to verify the signature.

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

关于c++ - 使用 WinCrypt/CryptoAPI 验证基于 OpenPGP 的 RSA 签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25814546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com