gpt4 book ai didi

c++ - wc_RsaSSL_Verify 返回 BAD_FUNC_ARG,我不知道为什么

转载 作者:行者123 更新时间:2023-11-30 03:25:50 25 4
gpt4 key购买 nike

我正在尝试使用 RSA 公钥解密使用 wolfcrypt 的签名文件 - 是的,我可能会或可能不会滥用 RSA 的“签名/验证”功能以使用私钥加密单独的 AES key 并使用公钥解密键。

不幸的是,我被困在 wc_RsaSSL_Verify() - 终究我无法弄清楚为什么它会返回 BAD_FUNC_ARG - 我认为这样的错误应该立即被其他人看到,所以我决定打电话依靠 StackOverflow 的集体力量。

据我所知,我为函数提供了它所要求的 - 输入缓冲区、输出缓冲区、每个缓冲区的大小以及指向 RsaKey 结构的指针。这是相关函数的代码片段:

bool VerifyWorker::GetAESKey()
{
bool result = true;
uint8_t en_aes_file_buff[VerifyWorkerLocal::RSA_KEY_SIZE];
uint8_t de_aes_file_buff[VerifyWorkerLocal::RSA_KEY_SIZE];

uint8_t* aes_iv_ptr = NULL;

// keyfile filestream
std::fstream aes_file;

// rsa_key must be initialized
if(rsa_key == NULL)
{
result = false;
}

// Open the key file and read it into a local buffer, then decrypt it and use it to initialize the
// aes struct
if(result)
{
aes_file.open(this->aes_key_file, std::ios_base::in | std::ios_base::binary);

if(aes_file.fail())
{
// Unable to open file - perror?
perror("GetAESKey");
result = false;
}
else
{
aes_file.read(reinterpret_cast<char*>(en_aes_file_buff), VerifyWorkerLocal::RSA_KEY_SIZE + 1);
if(!aes_file.eof())
{
// we didn't have enough space to read the whole signature!
std::cerr << "aes_file read failed! " << aes_file.rdstate() << std::endl;
result = false;
}
}
}

// "Unsign" the aes key file with RSA verify, and load the aes struct with the result
if(result)
{
int wc_ret = 0;
wc_ret = wc_RsaSSL_Verify(const_cast<const byte*>(en_aes_file_buff),
VerifyWorkerLocal::RSA_KEY_SIZE, reinterpret_cast<byte*>(&de_aes_file_buff),
VerifyWorkerLocal::RSA_KEY_SIZE, rsa_key);

rsa_key 是一个私有(private)成员,在一个带有公钥 DER 文件的单独函数中初始化(成功地使用 wc_PublicKeyDecode())。我使用 OpenSSL 生成了公钥和私钥 - 它应该默认使用 PKCS#1 v1.5 b 正确填充我的 AES key 和 iv 文件。

我还应该提到我使用的是 wolfssl 版本 3.9.8。谢谢!

最佳答案

我发现问题是我用 RSA key 签名的文件没有正确签名。当我使用 OpenSSL 签署文件时,我的 cli 调用是

openssl rsautl -in keyfile -out keyfile -inkey private.pem -sign

显然,openssl 不喜欢您为 -in 和 -out 指定相同的文件。当我把它改成类似的东西时

openssl rsautl -in keyfile -out keyfile_signed -inkey private.pem -sign

我实际上能够使用 wc_RsaSSL_Verify 验证文件。

所以,就像大多数愚蠢的深夜、最后一小时的软件问题一样,我完全找错了地方。我对返回的 BAD_FUNC_ARG 有点失望,认为它必须明确地处理函数参数的格式,而不一定是它们的内容。希望这个答案对其他人也有用。

关于c++ - wc_RsaSSL_Verify 返回 BAD_FUNC_ARG,我不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48778196/

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