- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在搜索互联网和 openssl 文档几天后,我遇到了瓶颈。我正在尝试使用私钥签署消息,然后使用公钥验证消息的真实性。
key 生成代码:
//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 Init\n");
return false;
}
if (EVP_SignUpdate(m_RSASignCtx, Msg, MsgLen) <= 0)
{
printf("Failed Update\n");
return false;
}
if (EVP_SignFinal(m_RSASignCtx, *EncMsg, &MsgLenEnc, m_ServerPrivateKey) <=0)
{
printf("Failed Final Sign\n");
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 Init\n");
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 %s\n",ERR_error_string(ERR_get_error(),NULL));
return false;
}
EVP_MD_CTX_cleanup(m_RSAVerifyCtx);
return true;
}
key 生成成功,我查看了它创建的 key ,它们似乎是有效的并且格式为 -----BEGIN 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 Init\n");
return false;
}
if (EVP_DigestSignUpdate(m_RSASignCtx, Msg, MsgLen) <= 0)
{
printf("Failed Update\n");
return false;
}
//Get Hash Size
if (EVP_DigestSignFinal(m_RSASignCtx, NULL,MsgLenEnc) <=0)
{
printf("Failed Final Sign\n");
return false;
}
//Allocate Space for hash
*EncMsg = (unsigned char*)malloc(*MsgLenEnc);
if (EVP_DigestSignFinal(m_RSASignCtx, *EncMsg, MsgLenEnc) <= 0)
{
printf("Failed Final Sign 1\n");
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 Init\n");
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("Error\n");
*Authentic = false;
EVP_MD_CTX_cleanup(m_RSAVerifyCtx);
return false;
}
}
我不敢相信我错过了一个如此明显的例子,谢谢你们的帮助,抱歉我看起来很愚蠢哈哈。
关于c++ - OpenSSL 在 EVP_VerifyFinal 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27954979/
如果我使用open ssl命令 sudo openssl genrsa -out privkey.pem 2048 要生成rsa key ,它仅生成1个文件。这是私钥。我如何获得公钥。 最佳答案 回答
三个不同版本的 openssl 正在同时更新 openssl.org : 0.98, 1.0.0, 1.0.1?它们之间有什么区别,我该如何选择要使用的版本? 最佳答案 https://en.wiki
我有以下命令用于 OpenSSL 生成私钥和公钥: openssl genrsa –aes-128-cbc –out priv.pem –passout pass:[privateKeyPass] 2
我正在尝试使用对应的 gcc (arm-none-eabi-5_4-2016q2) 为 cortex m3 机器交叉编译 openssl。机器应该有能力做 TCP 请求,我们希望在一天结束时做 HTT
我正在尝试使用 openssl dsa 实现,但我对以下细节感到非常困惑: 命令 openssl dsa .... 的选项“-text”:输出中的十六进制数字,我是否正确地假设这些是字节,因此它们是按
我正在尝试制作一个假 CA 并用它签署一个证书以与 stunnel 一起使用(这似乎只是调用 OpenSSL 例程,因此您可能不需要了解该程序来提供帮助:)。然而,stunnel 一直拒绝我的证书,说
不幸的是,Perl 在尝试安装 OpenSSL 的手册页(例如 OpenSSL_1_0_1g)时不知何故遇到了错误。因为我不需要它们 - 我只想使用 OpenSSL 作为 C 库,我想我可以通过完全跳
OpenSSL 中的 BIO 对到底是什么?它的用途是什么?我已经检查过 OpenSSL 文档,但任何细节都很少。 最佳答案 OpenSSL 中的 BIO 类似于文件句柄。您可以使用一对它们来安全地相
openssl ca 和 openssl x509 命令有什么区别?我正在使用它来创建和签署我的 root-ca、intermed-ca 和客户端证书,但是 openssl ca 命令不会在证书上注册
如何(如果有的话)为 OpenSSL 定义一个单一的可信证书文件在 Windows(Win-7,OpenSSL 1.0.1c)上使用 SSL_CERT_FILE 环境变量? 各种研究促使我下载了 Mo
我有一个自签名证书,其中显示了列出的基本约束,但从中生成的签名请求不显示这些属性,例如 [v3_req]。我怎样才能让它可见?我正在使用 openssl 生成证书。 场景: 我使用以下方法创建自签名证
这个问题在这里已经有了答案: Check if a connection is TLSv1 vs SSLv3 (SSL_CIPHER_description/SSL_CIPHER_get_name)
是否有更简单的方法来确定在构建 openssl 期间指定的选项,例如当时是否定义了 OPENSSL_NO_SRTP? 我只能从以下方面获得有限的信息: openssl 版本 -a 命令。但是,如果我只
我们正在与 AWS Nitro 合作,仅提供 3 小时的证书。 我们正在寻找一种可以跳过验证中的过期部分并仍然确认证书链有效的方法。 最佳答案 根据 openssl-verify 文档
嗨,我如何在 Easyphp 中启用 openssl,因为我收到错误消息无法发送。Mailer 错误:缺少扩展:opensslTime:使用 phpmailer 时。谢谢 最佳答案 在您的 php.i
我正在尝试以编程方式读取 OpenSSL 警报消息,但无法找到执行此操作的方法。 OpenSSL API 提供如下功能: const char *SSL_alert_type_string(int v
我跑了openssl speed在我的 Ubuntu 计算机上。一些结果: Doing md4 for 3s on 16 size blocks: 9063888 md4's in 3.00s Doi
我编译了带有cryptodev支持(即硬件加速)的OpenSSL,但不幸的是默认引擎仍然是软件。 time openssl speed -evp aes-128-cbc -engine cryptod
我需要从 RedHat Linux 服务器连接到 Microsoft Dynamics CRM 服务器。地址是xxx.api.crm4.dynamics.com。服务器接受 TLSv1 但不接受 1.
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 上个月关闭。 Improve thi
我是一名优秀的程序员,十分优秀!