wc_RsaSSL_Verify returns BAD_FUNC_ARG,我也说不出为什么

wc_RsaSSL_Verify returns BAD_FUNC_ARG, and I can't tell why

我正在尝试使用 RSA public 密钥解密使用 wolfcrypt 的签名文件 - 是的,我可能会也可能不会滥用 RSA 的 "sign/verify" 功能来使用私有加密单独的 AES 密钥密钥并使用 public 密钥解密。

不幸的是,我被困在 wc_RsaSSL_Verify() - 我一辈子都弄不明白为什么它会返回 BAD_FUNC_ARG - 我认为这样的错误应该立即可见给其他人,所以我决定呼吁 Whosebug 的集体力量。

据我所知,我正在为函数提供它所要求的 - 输入缓冲区、输出缓冲区、每个缓冲区的大小以及指向 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 是一个私有成员初始化(成功,使用 wc_PublicKeyDecode())在一个单独的函数中,带有 public 密钥 DER 文件。我使用 OpenSSL 生成了 public 和私钥 - 它应该使用默认的 PKCS#1 v1.5 b 正确填充我的 AES 密钥和 iv 文件。

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

我发现问题是我用 RSA 密钥签名的文件没有正确签名。当我使用 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 有点不适应,认为它必须明确地处理函数参数的格式,而不一定是它们的内容。希望这个答案对其他人也有用。

听起来您正在尝试使用 RSA_Sign 执行 AES 密钥的 "Encrypt"。那么我假设您要发送给远程合作伙伴或计算机,然后他们将 运行 一个 RSA_Verify 操作来解密 AES 密钥 我是否正确理解了这个场景?

如果是这样,我很抱歉,如果您最初搜索了如何执行此操作,但它没有显示出来,但实际上我们在这里有一个完全相同的示例:

https://github.com/wolfSSL/wolfssl-examples/tree/master/signature/encryption-through-signing

该示例包括两个单独的应用程序。第一个应用程序 "rsa-private-encrypt-app.c" 将对 "fake Aes Key" 进行签名(加密)并将结果输出到文件中。第二个应用程序 "rsa-public-decrypt-app.c" 然后打开输出的文件并对文件中包含的数据进行验证(解密)以恢复原始 "fake Aes Key".

I may or may not be abusing the "sign/verify" power of RSA to encrypt a separate AES key using the private key and decrypt using the public key.

完全没有,这是对 RSA 的有效使用 sign/verify 假设您正在使用固定长度的输入,例如 AES 密钥。

这就是我们创建示例的原因!事实上,不久前我们的论坛上有一位用户问了一个非常相似的问题,这导致我们做了这个例子。

关于您在使用 openssl 和 wolfssl 时遇到的问题,有一点需要注意,实际上在 README 中已经提到:

https://github.com/wolfSSL/wolfssl-examples/blob/master/signature/encryption-through-signing/README.md

... Keep in mind this is not a TRUE RSA ENCRYPT and will likely not inter-op with other libraries that offer a RSA_PRIVATE_ENCRYPT type API.

This is a true SIGN operation.

如果您有任何其他问题,请随时在此处 post 提出(当然还要添加 wolfssl 标签),或者您也可以随时给我们发送电子邮件至 support@wolfssl.com

免责声明:我在 wolfSSL Inc. 工作