Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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);
            $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!

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:

    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);

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.