调用OpenSSL的PEM_write_PUBKEY || PEM_write_PrivateKey API使程序突然退出并显示消息“no OPENSSL_Applink”
我试图从OpenSSL使用EVP_ * API,但在尝试从EVP_PKEY结构中转储公钥/私钥时遇到了奇怪的奇怪行为。调用OpenSSL的PEM_write_PUBKEY || PEM_write_PrivateKey API使程序突然退出并显示消息“no OPENSSL_Applink”
问题 ::填充EVP_PKEY结构体后,调用PEM_write_PUBKEY
API(请参阅TRIAL1)后,程序将退出。在调用PEM_write_PrivateKey
API时也会发生同样的情况(请参阅TRIAL2)。
输出:我留下了一个temp.pem
0字节的文件,并说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):无OPENSSL_Applink
回答我的问题了未来的开发者
这里的关键问题是OpenSSL抛出的错误(读取,消息),即没有OPENSSL_Applink。
作为记录here,
按照0.9.8上述限制被消除了的.DLL。与一些特定的运行时间选项编译OpenSSL的 .DLL文件[我们坚持 默认/ MD]可以使用的应用程序不同 选项,甚至不同的编译器编译部署。 但有一个赶上!与以前版本 一样,您不得不重新编译OpenSSL工具包,而必须编译带编译器的小C代码片段和/或您选择的选项。该代码片段被安装为 /include/openssl/applink.c,并且应该被添加到您的应用程序项目的 中,或者简单地将#include -d添加到您的应用程序源文件中的一个[并且只有一个] 。如果未将此垫片模块链接到 ,则您的应用程序将自身显示为致命的“no OPENSSL_Applink” 运行时错误。明确提醒是因为在这种情况下 [混合编译器选项],在首次调用OpenSSL之前添加CRYPTO_malloc_init 同样重要。
你应该检查你的编译选项/MD
(假设你知道VC++ Project Properties
下Code Generation
选项)。
我做了同样的事情,但仍然没有解决我的问题。这个问题的答案是第二段https://www.openssl.org/docs/faq.html#PROG2]给出的链接[,它教导我们要包括<install-root>/include/openssl/applink.c
哪里得到这个
applink.c
文件,如果在OpenSSL的 DIR没有发现?
就我而言,我使用的exe安装二进制OpenSSL和我不能在我的<install-root>
地方找到这个特定applink.c
文件,所以我就开始寻找,发现它here on GitHub。
您所要做的就是将此文件作为源文件包含在您的项目中,或者将其更好地保存在openssl install-root目录中,以便您可以从其他项目的相同位置包含该文件。
// applink.c from https://github.com/openssl/openssl/blob/master/ms/applink.c
/*
* Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the OpenSSL license (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#define APPLINK_STDIN 1
#define APPLINK_STDOUT 2
#define APPLINK_STDERR 3
#define APPLINK_FPRINTF 4
#define APPLINK_FGETS 5
#define APPLINK_FREAD 6
#define APPLINK_FWRITE 7
#define APPLINK_FSETMOD 8
#define APPLINK_FEOF 9
#define APPLINK_FCLOSE 10 /* should not be used */
#define APPLINK_FOPEN 11 /* solely for completeness */
#define APPLINK_FSEEK 12
#define APPLINK_FTELL 13
#define APPLINK_FFLUSH 14
#define APPLINK_FERROR 15
#define APPLINK_CLEARERR 16
#define APPLINK_FILENO 17 /* to be used with below */
#define APPLINK_OPEN 18 /* formally can't be used, as flags can vary */
#define APPLINK_READ 19
#define APPLINK_WRITE 20
#define APPLINK_LSEEK 21
#define APPLINK_CLOSE 22
#define APPLINK_MAX 22 /* always same as last macro */
#ifndef APPMACROS_ONLY
# include <stdio.h>
# include <io.h>
# include <fcntl.h>
static void *app_stdin(void)
{
return stdin;
}
static void *app_stdout(void)
{
return stdout;
}
static void *app_stderr(void)
{
return stderr;
}
static int app_feof(FILE *fp)
{
return feof(fp);
}
static int app_ferror(FILE *fp)
{
return ferror(fp);
}
static void app_clearerr(FILE *fp)
{
clearerr(fp);
}
static int app_fileno(FILE *fp)
{
return _fileno(fp);
}
static int app_fsetmod(FILE *fp, char mod)
{
return _setmode(_fileno(fp), mod == 'b' ? _O_BINARY : _O_TEXT);
}
#ifdef __cplusplus
extern "C" {
#endif
__declspec(dllexport)
void **
# if defined(__BORLANDC__)
/*
* __stdcall appears to be the only way to get the name
* decoration right with Borland C. Otherwise it works
* purely incidentally, as we pass no parameters.
*/
__stdcall
# else
__cdecl
# endif
OPENSSL_Applink(void)
{
static int once = 1;
static void *OPENSSL_ApplinkTable[APPLINK_MAX + 1] =
{ (void *)APPLINK_MAX };
if (once) {
OPENSSL_ApplinkTable[APPLINK_STDIN] = app_stdin;
OPENSSL_ApplinkTable[APPLINK_STDOUT] = app_stdout;
OPENSSL_ApplinkTable[APPLINK_STDERR] = app_stderr;
OPENSSL_ApplinkTable[APPLINK_FPRINTF] = fprintf;
OPENSSL_ApplinkTable[APPLINK_FGETS] = fgets;
OPENSSL_ApplinkTable[APPLINK_FREAD] = fread;
OPENSSL_ApplinkTable[APPLINK_FWRITE] = fwrite;
OPENSSL_ApplinkTable[APPLINK_FSETMOD] = app_fsetmod;
OPENSSL_ApplinkTable[APPLINK_FEOF] = app_feof;
OPENSSL_ApplinkTable[APPLINK_FCLOSE] = fclose;
OPENSSL_ApplinkTable[APPLINK_FOPEN] = fopen;
OPENSSL_ApplinkTable[APPLINK_FSEEK] = fseek;
OPENSSL_ApplinkTable[APPLINK_FTELL] = ftell;
OPENSSL_ApplinkTable[APPLINK_FFLUSH] = fflush;
OPENSSL_ApplinkTable[APPLINK_FERROR] = app_ferror;
OPENSSL_ApplinkTable[APPLINK_CLEARERR] = app_clearerr;
OPENSSL_ApplinkTable[APPLINK_FILENO] = app_fileno;
OPENSSL_ApplinkTable[APPLINK_OPEN] = _open;
OPENSSL_ApplinkTable[APPLINK_READ] = _read;
OPENSSL_ApplinkTable[APPLINK_WRITE] = _write;
OPENSSL_ApplinkTable[APPLINK_LSEEK] = _lseek;
OPENSSL_ApplinkTable[APPLINK_CLOSE] = _close;
once = 0;
}
return OPENSSL_ApplinkTable;
}
#ifdef __cplusplus
}
#endif
#endif
就是这样,这将消除程序退出的奇怪行为。
那么,结果究竟是什么?一个带有0字节的文件“temp.pem”? – Ctx
@Ctx:的确是.. – Abhineet
这有帮助吗? https://www.openssl.org/docs/faq.html#PROG2 – Ctx