【发布时间】:2017-06-01 05:33:08
【问题描述】:
我正在尝试使用 OpenSSL 中的 EVP_* API,但在尝试从 EVP_PKEY 结构中转储公钥/私钥时遇到了奇怪的奇怪行为。
问题:: 填充 EVP_PKEY 结构后,在调用 PEM_write_PUBKEY API(参见 TRIAL1)时,程序退出。调用PEM_write_PrivateKey API 时也会发生同样的情况(参见 TRIAL2)。
输出:我留下了一个temp.pem 0 字节的文件,并在cmd 提示符上显示一条消息OPENSSL_Uplink(5D8C7000,08): no OPENSSL_Applink
#define TRIAL1
void InitOpenSSLLib(void)
{
SSL_library_init();
SSL_load_error_strings();
OpenSSL_add_all_algorithms();
}
int main(int argc, char** argv)
{
EVP_PKEY_CTX* ctx = NULL;
EVP_PKEY* pKeyPair = EVP_PKEY_new();
BIO *mem = BIO_new(BIO_s_mem());
FILE* fp = fopen("temp.pem", "wb");
InitOpenSSLLib();
ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, 0);
EVP_PKEY_keygen_init(ctx);
EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 2048);
EVP_PKEY_keygen(ctx, &pKeyPair);
// Succeeds till here... all of the above called APIs return value greater than 0
#ifdef TRIAL1
// Program exits even before printing any error
if (PEM_write_PUBKEY(fp, pKeyPair) <= 0)
printf("PEM_write_PUBKEY failed\n");
#elif TRIAL2
// same behavior with this API too
PEM_write_PrivateKey(fp, pKeyPair, NULL, NULL, 0, 0, NULL);
#endif
// Tried this too.... but the control never reaches here
fflush(fp);
// Tried this too... most of the mem struct members are NULL.
EVP_PKEY_print_private(mem, pKeyPair, 0, 0);
//.... Cleanup codes
fclose(fp);
BIO_free_all(mem);
return 0;
}
有什么建议吗?我在这里做错什么了吗?有没有其他方法可以将私钥/公钥或 PEM 对格式转储到文件中?
我使用的是 VC++ 2015。此外,在点击 Ctrl+F5 时,提示会显示以下消息:OPENSSL_Uplink(5D8C7000,08): no OPENSSL_Applink
为未来的开发者回答我自己的问题
【问题讨论】:
-
那么,结果到底是什么?一个 0 字节的文件“temp.pem”?
-
@Ctx:完全正确..
-
我已经经历过了。我正在使用 /MD 标志进行编译。我无法理解他们在第二段中写的内容
applink.c。 -
您是否尝试在代码顶部使用
#include <openssl/applink.c>?
标签: c openssl cryptography rsa public-key-encryption