簡體   English   中英

PEM_write_PrivateKey()函數未將RSA私鑰存儲在private.pem文件中

[英]PEM_write_PrivateKey() function is not storing the RSA private key in private.pem file

這是我使用的代碼,參考其他用戶從這里詢問的堆棧溢出問題。 但是當我嘗試使用PEM_write_PrivateKey()函數將私鑰寫入文件時。 它不是這樣做的。 在此函數調用后,控制台屏幕將自動關閉。 而private.pem文件不包含任何內容。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/evp.h>
#include <openssl/rsa.h>
#include <openssl/bio.h>
#include <openssl/pem.h>
#include <openssl/objects.h>
#include <openssl/err.h>
#include <openssl/x509.h>

int main()
{

//
// Local variables definition
//
EVP_PKEY_CTX    *evp_ctx    = NULL;
EVP_PKEY        *ppkey      = NULL;
FILE            *fpPri      = NULL;
FILE            *fpPub      = NULL;
int             retValue    = 1;

for (;;)
{
    //
    // Function allocates public key algorithm context using the algorithm
    // specified by id
    //
    evp_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
    if (NULL == evp_ctx)
    {
        printf("RSA Public key algorithm context is not allocated\n");
        break;
    } // if
    printf("RSA Public key algoritm context allocated\n");

    //
    // Function initializes a public key algorithm context using key pkey
    // for a key genration operation
    //
    retValue = EVP_PKEY_keygen_init(evp_ctx);
    if (1 != retValue)
    {
        printf("Initialization of public key alogorithm context failed\n");
        break;
    } // if
    printf("Public key alogorithm context initialized\n");

    //
    // Setting RSA key bit to 2048
    //
    retValue = EVP_PKEY_CTX_set_rsa_keygen_bits(evp_ctx, 2048);
    if (1 != retValue)
    {
        printf("RSA key bits not set to 2048\n");
        break;
    } // if
    printf("RSA key bits set to 2048\n");

    //
    // Function performs a key generation operation
    //
    retValue = EVP_PKEY_keygen(evp_ctx, &ppkey);
    if (1 != retValue)
    {
        printf("Key generation operation failed\n");
        break;
    } // if
    printf("Key generated successfully\n");

    //
    // Creating a file to store RSA private key
    //
    fpPri = fopen("./private.pem", "w+");
    if (NULL == fpPri)
    {
        printf("File pointer of private.pem file is not opened\n");
        break;
    } // if
    printf("File pointer or private.pem file opened\n");

    retValue = PEM_write_PrivateKey(fpPri, ppkey, NULL, NULL, 0, 0, NULL);
    if (1 != retValue)
    {
        printf("Private key is not written to file private.pem\n");
        break;
    } // if
    printf("Private key written to file private.pem\n");

    //
    // Final break statement
    //
    break;
} // for

//
// Releasing all the memory allocations and the handles
//

getchar();

return 0;
 }

我不確定這是否與您的問題有關,但我遇到了類似的問題,最終在此頁面上試圖弄清楚問題可能是什么。 所以我想我會分享我的發現,希望能幫助別人。 我還在其他一些論壇上看到人們遇到了我遇到的同樣問題。

問題:在使用Visual Studio 2015編譯的應用程序上調用PEM_write_PrivateKey時,應用程序退出時沒有任何錯誤代碼。使用VS2010編譯時,相同的應用程序和代碼工作正常。

我首先發現微軟對VS2015中的FILE句柄做了一些重大修改。

FILE封裝在以前的版本中,FILE類型已完全定義,因此用戶代碼可以訪問FILE並修改其內部。 stdio庫已更改為隱藏實現詳細信息。 作為其中的一部分,現在定義的FILE是一個不透明類型,其成員無法從CRT本身外部訪問。 https://msdn.microsoft.com/en-us/library/bb531344%28v=vs.140%29.aspx?f=255&MSPPError=-2147217396

通過在OpenSSL中調試,我發現處理程序“APPLINK_FSETMOD”設置為“不支持”。 這讓我了解了OpenSSL中的AppLink技術實現。

這讓我發現我的問題的解決方案是在我的應用程序中添加以下代碼。

#include "openssl/applink.c" 

重要說明:您必須將include包含在.exe源文件之一中,因為GetModuleHandle傳遞為NULL。

使用PEM_write_bio_RSAPrivateKey()創建PEM_write_PrivateKey(),用於將私鑰寫入文件

    BIO* pOut = BIO_new_file( "key.pem", "w");
    if (!pOut)
    {
        return false;
    }
    /* Write the key to disk. */
    if( !PEM_write_bio_RSAPrivateKey( pOut, pRSA, NULL, NULL, 0, NULL, NULL))
    {
        return false;
    }
    BIO_free_all( pOut );
    /* Open the PEM file for writing the certificate to disk. */
    BIO * x509file = BIO_new_file( "cer.pem", "w" );
    if (!x509file )
    {
        return false;
    }
    if (!PEM_write_bio_X509( x509file, pX509Cert ))
    {
        return false;
    }
    BIO_free_all( x509file );

也許是因為你從未調用過SSL_library_init()。

此外,您的緩沖文件可能在程序退出之前不會被刷新。 在程序結束之前,您應該致電:

fflush(fpPri);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM