【问题标题】:Error 0x2006D002:BIO routines:BIO_new_file:system lib with PHP on Windows错误 0x2006D002:BIO 例程:BIO_new_file:system lib with PHP on Windows
【发布时间】:2016-07-16 19:20:30
【问题描述】:

尝试通过简单方式加载我的私钥时出现以下错误。这是我的代码。

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() 返回error:2006D002:BIO routines:BIO_new_file:system lib

我在 php.iniextension=php_openssl.dll 中启用了 OpenSSL。

可能是什么问题?我该如何解决?

谢谢!

【问题讨论】:

标签: php windows openssl


【解决方案1】:

函数openssl_get_privatekey()openssl_pkey_get_private() 的别名。这个函数有两个参数;第一个是 URI 格式的文件名,或者是 PEM 格式的私钥的内容。第二个是密码。

您收到的错误表明尝试读取文件时出错;通常有问题的文件是included in the error message,因此您可能只在此处包含部分错误。由于您没有使用 OpenSSL 读取文件,因此最可能的罪魁祸首是 OpenSSL 配置文件;系统需要被告知去哪里寻找它。

  • 右键单击我的电脑并进入属性
  • 在“高级”选项卡上,单击“环境变量”按钮
  • 在系统变量下创建一个新条目
  • 变量名称应为“OPENSSL_CONF”
  • 变量值应该是文件的完整路径
  • 重启电脑

环境变量也可以从within your PHP code 设置,尽管它需要添加到您的所有代码中,因此可能不是可取的。此外,如前所述,您可以直接从函数调用中打开密钥文件;这是我建议尝试的方法:

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

【讨论】:

    猜你喜欢
    • 2011-05-05
    • 1970-01-01
    • 2012-09-07
    • 1970-01-01
    • 2022-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多