我正在尝试使用来自 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;
}
最佳答案
您是否使用受密码保护的公钥?
如果是,您需要将回调函数作为第三个参数传递给 PEM_read_DSA_PUBKEY
,因此如果提供的密码匹配,它将能够正确加载您的 key 。
更新:
或者,如 Hasturkun 所指出的那样,您可以传递一个以 null 结尾的字符串作为第四个参数。引用 official documentation :
If the cb parameters is set to NULL and the u parameter is not NULL then the u parameter is interpreted as a null terminated string to use as the passphrase. If both cb and u are NULL then the default callback routine is used which will typically prompt for the passphrase on the current terminal with echoing turned off.
关于c - 使用 OpenSSL 进行 DSA 签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5867939/