3

I am getting the following error while trying to load my private key by simple way. This is my code.

public function loadPrivateKey($fileName, $password = null){
        if(!is_file($fileName))
            throw new SignException('Private key not found', SignException::KEY_NOT_FOUND);

        $fileContent = file_get_contents($fileName);
        if(!is_null($password))
            $this->prvKey = openssl_get_privatekey($fileContent, $password);
        else
            $this->prvKey = openssl_get_privatekey($fileContent);

        if(!empty(openssl_error_string()))
            throw new SignException('OpenSSL Error: '.openssl_error_string());

        if(!is_resource($this->prvKey))
            throw new SignException('Private key is not resourse', SignException::EXTERNAL_ERROR);
    }

openssl_error_string() returns error:2006D002:BIO routines:BIO_new_file:system lib.

I enabled OpenSSL in my php.ini with extension=php_openssl.dll.

What could be the problem? How do I fix it?

Thank you!

jww
  • 97,681
  • 90
  • 411
  • 885
Tigran
  • 633
  • 4
  • 17
  • 26
  • Possible duplicate of [OpenSSL not working on Windows](http://stackoverflow.com/questions/15558321/openssl-not-working-on-windows). – jww Mar 29 '16 at 10:25
  • Also see [OpenSSL and error in reading openssl.conf file](http://stackoverflow.com/a/11241669/608639). – jww Mar 29 '16 at 10:58

1 Answers1

1

The function openssl_get_privatekey() is an alias for openssl_pkey_get_private(). This function takes two arguments; the first is either a filename in URI format, or the contents of a PEM-formatted private key. The second is a passphrase.

The error you're getting indicates an error trying to read a file; typically the file in question is included in the error message, so it's possible you're only including part of the error here. Since you're not reading the file with OpenSSL, the most likely culprit is the OpenSSL configuration file; the system needs to be told where to look for it.

  • Right click on My Computer and go into Properties
  • On the Advanced tab, click the Environment Variables button
  • Create a new entry under System Variables
  • The Variable name should be "OPENSSL_CONF"
  • The Variable value should be the full path to the file
  • Restart the computer

Environment variables can also be set from within your PHP code, though it will need to be added to all your code, so may not be preferable. Also, as mentioned, you can open the key file directly from the function call; here's what I'd suggest trying:

<?php
public function loadPrivateKey($fileName, $password = "") {
    // I just used the value from my system here
    putenv("OPENSSL_CONF=C:\\OpenSSL\\bin\\openssl.cfg");
    if (!is_readable($fileName)) {
        throw new SignException("Private key not found or not readable", SignException::KEY_NOT_FOUND);
    }

    $fileName = "file://$fileName";

    $this->prvKey = openssl_get_privatekey($fileName, $password);

    if (!empty(openssl_error_string())) {
        throw new SignException("OpenSSL error: " . openssl_error_string());
    }

    if (!is_resource($this->prvKey)) {
        throw new SignException("Private key is not resource", SignException::EXTERNAL_ERROR);
    }
}
miken32
  • 42,008
  • 16
  • 111
  • 154