[英]PEM_write_PrivateKey() function is not storing the RSA private key in private.pem file
[英]Calling OpenSSL's PEM_write_PUBKEY || PEM_write_PrivateKey API makes the program to exit abruptly with message “no OPENSSL_Applink”
我正在嘗試使用OpenSSL的EVP_ * API,但是在嘗試從EVP_PKEY結構中轉儲公鑰/私鑰時遇到了奇怪的奇怪行為。
問題 ::填充EVP_PKEY結構后,調用PEM_write_PUBKEY
API(請參閱TRIAL1)后,程序退出。 調用PEM_write_PrivateKey
API時PEM_write_PrivateKey
發生相同的情況(請參閱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):否OPENSSL_Applink
為未來的開發者回答我自己的問題
這里的關鍵問題是OpenSSL引發的錯誤(讀取,消息),即沒有OPENSSL_Applink 。
如此處所述 ,
根據0.9.8,消除了.DLL的上述限制。 使用某些特定的運行時選項編譯的OpenSSL .DLL [ 我們堅持使用默認的/ MD ]可以與使用其他選項甚至不同的編譯器編譯的應用程序一起部署。 但是有一個問題! 您不必像以前的版本那樣重新編譯OpenSSL工具箱,而必須使用選擇的編譯器和/或選項來編譯小型C代碼段。 該代碼段以/include/openssl/applink.c的形式安裝,應添加到您的應用程序項目中,或僅在其中一個(也只有一個)應用程序源文件中#include-d。 未能將此墊片模塊鏈接到您的應用程序中,將自身顯示為致命的“無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
如果在openssl的目錄中找不到此
applink.c
文件,該applink.c
哪里獲取?
就我而言,我使用exe二進制文件安裝了OpenSSL,但在<install-root>
任何位置都找不到此特定的applink.c
文件,因此我開始在GitHub上查找並找到它。
您所要做的就是將該文件作為源文件包含在您的項目中,或者最好將其保存在openssl install-root dir中,以便您也可以從同一位置將其包含在其他項目中。
// 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
就是這樣,這將消除程序退出的奇怪行為。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.