OpenSSL 在EVP_VerifyFinal时失败
OpenSSL Fails at EVP_VerifyFinal
经过几天的互联网搜索和openssl 文档,我碰壁了。我正在尝试使用私钥对消息进行签名,然后使用公钥验证消息的真实性。
密钥生成代码:
//Generate RSA Keys
EVP_PKEY_CTX* KeyCtx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
if (KeyCtx == nullptr)
return false;
if (EVP_PKEY_keygen_init(KeyCtx) <= 0)
return false;
if (EVP_PKEY_CTX_set_rsa_keygen_bits(KeyCtx, RSA_KeyLen) <= 0)
return false;
if (EVP_PKEY_keygen(KeyCtx, &m_ServerKeyPair) <= 0)
return false;
if (EVP_PKEY_keygen(KeyCtx, &m_ClientKeyPair) <= 0)
return false;
//Write Keys to EVP_PKEYS for actual internal encryption,BIO mem is wiped after read so we need a new copy
PEM_read_bio_PrivateKey(KeyToPrivBio(m_ServerKeyPair), &m_ServerPrivateKey, NULL, NULL);
PEM_read_bio_PUBKEY(KeyToPubBio(m_ServerKeyPair), &m_ServerPublicKey, NULL, NULL);
PEM_read_bio_PrivateKey(KeyToPrivBio(m_ClientKeyPair), &m_ClientPrivateKey, NULL, NULL);
PEM_read_bio_PUBKEY(KeyToPubBio(m_ClientKeyPair), &m_ClientPublicKey, NULL, NULL);
RSA 标志:
bool SecureCrypto::RSASign(const unsigned char* Msg, size_t MsgLen,unsigned char** EncMsg, size_t& MsgLenEnc)
{
EVP_PKEY_size(m_ServerPrivateKey);
*EncMsg = (unsigned char*)malloc(EVP_PKEY_size(m_ServerPrivateKey));
if (EVP_SignInit_ex(m_RSASignCtx, EVP_sha1(), 0) <= 0)
{
printf("Failed Initn");
return false;
}
if (EVP_SignUpdate(m_RSASignCtx, Msg, MsgLen) <= 0)
{
printf("Failed Updaten");
return false;
}
if (EVP_SignFinal(m_RSASignCtx, *EncMsg, &MsgLenEnc, m_ServerPrivateKey) <=0)
{
printf("Failed Final Signn");
return false;
}
EVP_MD_CTX_cleanup(m_RSASignCtx);
return true;
}
RSA 验证:
bool SecureCrypto::RSAVerifySignature(const unsigned char* MsgHash, size_t MsgHashLen,unsigned char* Msg, size_t MsgLen)
{
if (EVP_VerifyInit(m_RSAVerifyCtx, EVP_sha1()) <= 0)
{
printf("Failed Verify Initn");
return false;
}
if (EVP_VerifyUpdate(m_RSAVerifyCtx, Msg, MsgLen) <= 0)
{
printf("Failed Verify n");
return false;
}
if (EVP_VerifyFinal(m_RSAVerifyCtx, MsgHash, MsgHashLen, m_ServerPublicKey)<=0);
{
printf("Failed Final Verify %sn",ERR_error_string(ERR_get_error(),NULL));
return false;
}
EVP_MD_CTX_cleanup(m_RSAVerifyCtx);
return true;
}
密钥生成成功,我已经查看了它创建的密钥,它们似乎是有效的,并且格式为 -----BEGIN PRIVATE KEY-----
,一些大的素数,-----END PRIVATE KEY-----
.符号似乎成功并返回哈希。EVP_Verify在最后一步EVP_VerifyFinal失败,ERR_error_string报告以下内容:
error:00000000:lib(0):func(0):reason(0)
这显然没有帮助,有人可以指出我可能做错了什么。这是我第一次深入研究openssl,所以这里可能会有一些大问题。
多亏了
JWW 和 indiv,我才能解决我的问题,这是我使用旧 API 的问题,以及不正确的返回检查。溶液:
bool SecureCrypto::RSASign(const unsigned char* Msg, size_t MsgLen,unsigned char** EncMsg, size_t* MsgLenEnc)
{
if (EVP_DigestSignInit(m_RSASignCtx,NULL, EVP_sha256(), NULL,m_ServerPrivateKey) <= 0)
{
printf("Failed Initn");
return false;
}
if (EVP_DigestSignUpdate(m_RSASignCtx, Msg, MsgLen) <= 0)
{
printf("Failed Updaten");
return false;
}
//Get Hash Size
if (EVP_DigestSignFinal(m_RSASignCtx, NULL,MsgLenEnc) <=0)
{
printf("Failed Final Signn");
return false;
}
//Allocate Space for hash
*EncMsg = (unsigned char*)malloc(*MsgLenEnc);
if (EVP_DigestSignFinal(m_RSASignCtx, *EncMsg, MsgLenEnc) <= 0)
{
printf("Failed Final Sign 1n");
return false;
}
EVP_MD_CTX_cleanup(m_RSASignCtx);
return true;
}
bool SecureCrypto::RSAVerifySignature(const unsigned char* MsgHash, size_t MsgHashLen,unsigned char* Msg, size_t MsgLen,bool* Authentic)
{
if (EVP_DigestVerifyInit(m_RSAVerifyCtx,NULL, EVP_sha256(),NULL,m_ServerPrivateKey) <= 0)
{
printf("Failed Verify Initn");
return false;
}
if (EVP_DigestVerifyUpdate(m_RSAVerifyCtx, Msg, MsgLen) <= 0)
{
printf("Failed Verify n");
return false;
}
int AuthStatus = EVP_DigestVerifyFinal(m_RSAVerifyCtx, (unsigned char*)MsgHash, MsgHashLen);
if (AuthStatus==1)
{
//Message Authentic
*Authentic = true;
EVP_MD_CTX_cleanup(m_RSAVerifyCtx);
return true;
} else if(AuthStatus==0){
//Message Not Authentic
*Authentic = false;
EVP_MD_CTX_cleanup(m_RSAVerifyCtx);
return true; //Still did our job correctly
} else{
printf("Errorn");
*Authentic = false;
EVP_MD_CTX_cleanup(m_RSAVerifyCtx);
return false;
}
}
我不敢相信我错过了一个如此明显的例子,谢谢你们的帮助,对不起,我看起来很愚蠢,哈哈。
相关文章:
- 如果没有malloc,链表实现将失败
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- 视图中的参数推导失败:take_while
- 链接到自行创建的dll失败
- 带有特殊路径部分的"std::filesystem::weakly_canonical"失败
- GetShortPathName在网络驱动器上使用中文文件夹时失败
- gcc和c++17的过载解析失败
- 为什么使用 P/Invoke 调用 dll 时,某些计算机中的 LoadLibrary 失败?
- 在WSL:configure_file上对config_file的每次调用都失败:配置文件时出现问题
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- 链接阶段在Ubuntu上失败,但在MacOS上失败
- 从父数组测试用例构造二叉树失败
- LibGit2 SSH身份验证失败
- 如何让LLDB在成功时退出,在失败时等待
- VS2017,C++包含目录与附加包含目录,子文件夹包含失败-但为什么
- 生成MRPT库时cmake配置失败
- 为什么除非添加括号,否则构造函数上的模板替换会失败?
- variadic模板中的模板参数推导失败