Good day everyone!

I do have the problem that the OpenSSL function "PEM_read_RSAPrivateKey"
does nothing, not even returning. Since I can not find any more hints
in the net I am asking here for help. I wrote a minimalistic example
program and hope somebody can give me some advice based on that.

I am using Visual Studio .net 2003 v7.1.3088 on Windows XP Pro SP 2.

For OpenSSL I am using Win32 OpenSSL v0.9.8a, the precompiled and
installer-packed binaries you can find under
http://www.slproweb.com/products/Win32OpenSSL.html

What I did:

Visual Studio Application Wizard: Win32 Console Application,
starting as empty project
added Additional Include Directories: "..\openssl\include"
added Additional Library Directories: "..\openssl\lib\vc"
(NOT "..\openssl\lib\vc\static" !)
added Additional Dependencies: "libeay32.lib ssleay32.lib"
set Runtime Library on "Multi-threaded Debug DLL (/MDd)" and
"Multi-threaded DLL (/MD)"
copied "applink.c" into project directory and added to VS project
created new file "example.cpp" and added to VS project

-------------------- snip --------------------

// SSL
#include "openssl/ssl.h"
#include "openssl/rand.h"
#include "openssl/err.h"
#include "openssl/rsa.h"

// STD
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

// TCHAR
#include <tchar.h>

int _tmain(int argc, _TCHAR* argv[])
{
  // initialization
  {
    SSL_load_error_strings();

    SSL_library_init();
    // note: replaces OpenSSL_add_all_algorithms() since 0.9.8a

    cout << "Seed random number generator..." << endl;
    const char SeedBuffer[16] = "123456789ABCDEF";
    RAND_seed(SeedBuffer, 16);
  }

  // generate and write keys
  {
    cout << "Generate RSA key..." << endl;
    RSA* pRSA = RSA_generate_key(1024, 65537, NULL, NULL);
    if (pRSA == NULL)
    {
      cout
        << "RSA_generate_key failed: "
        << ERR_get_error()
        << endl;
      return false;
    }

    cout << "Size: " << RSA_size(pRSA) * 8 << " Bit" << endl;

    FILE* pPrivateKeyFile = fopen("RSAPrivateKey.txt", "w");
    if (pPrivateKeyFile == NULL)
    {
      cout << "fopen failed" << endl;
      return 1;
    }

    cout << "Write private key file..." << endl;
    if (PEM_write_RSAPrivateKey(
      pPrivateKeyFile, pRSA, NULL, NULL, 0, NULL, NULL) == 0)
    {
      cout
        << "PEM_write_RSAPrivateKey failed: "
        << ERR_get_error()
        << endl;
      return 1;
    }

    fclose(pPrivateKeyFile);

    // note: file "RSAPrivateKey.txt" now exists, is plaintext
    // and obviously contains a key

  }

  // read keys
  {
    FILE* pPrivateKeyFile = fopen("RSAPrivateKey.txt", "r");
    if (pPrivateKeyFile == NULL)
    {
      cout << "fopen failed" << endl;
      return 1;
    }

    cout << "Read private key file..." << endl;
    RSA* pPrivateKeyRSA = PEM_read_RSAPrivateKey(
      pPrivateKeyFile, NULL, NULL, NULL);

    // !!!!! program never comes here !!!!!

    // note: I tried every combination of callback function
    // for the password but the program never got there, too

    if (pPrivateKeyRSA == NULL)
    {
      cout
        << "PEM_read_RSAPrivateKey failed: "
        << ERR_get_error()
        << endl;
      return 1;
    }

    fclose(pPrivateKeyFile);

    cout
      << "Size: " << RSA_size(pPrivateKeyRSA) * 8 << " Bit"
      << endl;
  }

  cout << "Done." << endl;
  return 0;
}

-------------------- snap --------------------

Thanks,
Matthias

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to