使用 OpenSSL 进行 DSA 签名
我正在尝试使用 OpenSSL 的 DSA 进行签名。我有包含公钥和私钥的文件。
首先,我建立了单播连接,一切都很好。之后我需要一个多播 UDP 连接并且我想对数据包进行签名。我正在尝试使用函数 PEM_read_DSA_PUBKEY()
来从我的证书加载我的公钥,但它不起作用。它始终返回 NULL 而不是 DSA 结构。
这里有一个简单版本的代码。我这样编译:
gcc -Wall -g -lm prueba.c -o prueba -lcrypto
有什么想法吗?谢谢你!
#include <stdio.h>
#include <openssl/dsa.h>
#include <openssl/pem.h>
int main()
{
FILE *DSA_cert_file = fopen("./certs/cert.pem", "r");
if (DSA_cert_file == NULL)
return 1;
printf("Certificate read\n");
DSA *dsa = DSA_new();
if((dsa = PEM_read_DSA_PUBKEY(DSA_cert_file, 0, 0, 0)) == NULL)
return 1;
printf("DSA public key read\n");
return 0;
}
I'm tryng to sign using DSA from OpenSSL. I have the files containing public and private keys.
First of all I make an unicast connection and every thing is fine. After that I need a multicast UDP connection and I want to sign the packets. I'm trying to use function PEM_read_DSA_PUBKEY()
in order to load my public key from my cert but it doesn't work. It returns always NULL
instead of a DSA struct.
Here you have a simplistic version of the code. I compile like this:
gcc -Wall -g -lm prueba.c -o prueba -lcrypto
Any idea? Thank you!
#include <stdio.h>
#include <openssl/dsa.h>
#include <openssl/pem.h>
int main()
{
FILE *DSA_cert_file = fopen("./certs/cert.pem", "r");
if (DSA_cert_file == NULL)
return 1;
printf("Certificate read\n");
DSA *dsa = DSA_new();
if((dsa = PEM_read_DSA_PUBKEY(DSA_cert_file, 0, 0, 0)) == NULL)
return 1;
printf("DSA public key read\n");
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否使用受密码保护的公钥?
如果是,您需要将回调函数作为第三个参数传递给
PEM_read_DSA_PUBKEY
,因此如果提供的密码匹配,它将能够正确加载您的密钥。更新:
或者,正如 Hasturkun 所指出的,您可以传递一个以 null 结尾的字符串作为第四个论点。引用官方文档:
Are you using a password-protected public key?
If yes, you are required to pass a callback function as the third argument to
PEM_read_DSA_PUBKEY
, so if the provided password matches, it will be able to properly load your key.Update:
Alternatively, as pointed by Hasturkun, you can pass a null-terminated string as the fourth argument. Quoting the official documentation:
您的 cert.pem 是否包含 X.509 证书?看起来
PEM_read_DSA_PUBKEY
需要 PEM 编码的 DSA 公钥,而无需 X.509 容器。尝试类似的方法:
Does your cert.pem contains a X.509 certificate ? It looks like
PEM_read_DSA_PUBKEY
expects a PEM-encoded DSA public key without the X.509 container.Try something like that instead: