PHP

OpenSSL在Windows上不起作用,错误0x02001003 0x2006D080 0x0E064002

发布于 2021-02-02 11:58:58

问题:
OpenSSL在我的Windows环境中不起作用。OpenSSL反复报告错误0x02001003、0x2006D080和0x0E064002。

环境:

Windows NT x 6.1 build 7601 (Windows 7 Business Edition Service Pack 1) i586
Apache/2.4.4 (Win32)
PHP/5.4.13 x86
PHP Directory: E:\wamp\php\
Virtual Host Directory: E:\Projects\1\public_html

我尝试过的

  • 安装说明 http://www.php.net/manual/zh/openssl.installation.php
  • PHP.ini extension=php_openssl.dll
  • Openssl.cnf E:\wamp\php\extras\openssl.cnf
  • %路径% E:\wamp\php
  • 重新启动
  • phpinfo:
    -—已启用OpenSSL支持
    -— OpenSSL库版本OpenSSL 1.0.1e 2013年2月11日
    -— OpenSSL标头版本OpenSSL 0.9.8y 2013年2月5日

  • 在有和没有指定 config的 情况下configargs

  • 有和没有<Directory E:\wamp\php\extras>在apache配置中指定
  • 复制openssl.cnf到virtualhost public_html,指出并仍然得到相同的错误
  • 没有登录 error_log
  • 已研究: 最近2天,我一直在对此进行研究,很惊讶没有更多信息,所以我在这里发布。似乎是OpenSSL配置或apache / php无法正确读取配置的问题。

码:

$privateKey = openssl_pkey_new();
while($message = openssl_error_string()){
    echo $message.'<br />'.PHP_EOL;
}

结果:

error:02001003:system library:fopen:No such process
error:2006D080:BIO routines:BIO_new_file:no such file
error:0E064002:configuration file routines:CONF_load:system lib
error:02001003:system library:fopen:No such process
error:2006D080:BIO routines:BIO_new_file:no such file
error:0E064002:configuration file routines:CONF_load:system lib

手动OpenSSL:

E:\wamp\apache\bin>openssl.exe pkey
WARNING: can't open config file: c:/openssl-1.0.1e/ssl/openssl.cnf

E:\wamp\apache\bin>set OPENSSL_CONF="E:\wamp\php\extras\openssl.cnf"

E:\wamp\apache\bin>openssl.exe pkey
3484:error:0200107B:system library:fopen:Unknown error:.\crypto\bio\bss_file.c:169:fopen('"E:\wamp\php\extras\openssl.cnf"','rb')
3484:error:2006D002:BIO routines:BIO_new_file:system lib:.\crypto\bio\bss_file.c:174:
3484:error:0E078002:configuration file routines:DEF_LOAD:system lib:.\crypto\conf\conf_def.c:199:

编辑:

  1. 多亏@Gordon,我现在可以使用来查看open_ssl错误 openssl_error_string
  2. 完全卸载EasyPHP。手动安装稳定版本的PHP / Apache。结果一样!在Windows上实现openssl肯定是我做错了。
  3. 手动OpenSSL部分…其他错误信息

最后的想法:
我安装了一个Linux机器,并且遇到了同样的错误。经过一番摸索后,我发现即使在openssl_pkey_new上抛出错误,它最终仍会创建我的测试p12文件。长话短说,错误会引起误解,它必须更多地处理您
如何 使用openssl函数,而不是服务器端配置。

最终代码:

// Create the keypair
$res=openssl_pkey_new();

// Get private key
openssl_pkey_export($res, $privkey);

// Get public key
$pubkey=openssl_pkey_get_details($res);
$pubkey=$pubkey["key"];

// Actual file
$Private_Key = null;
$Unsigned_Cert = openssl_csr_new($Info,$Private_Key,$Configs);
$Signed_Cert = openssl_csr_sign($Unsigned_Cert,null,$Private_Key,365,$Configs);
openssl_pkcs12_export_to_file($Signed_Cert,"test.p12",$Private_Key,"123456");

靠近

一年后…

因此,我发现自己在一年后再次执行此操作,无论我在计算机上或在脚本执行期间设置了什么PATH变量,它始终会导致找不到文件的错误。通过在中传递数组中的config参数,我能够解决它。这是一个测试成功使用OpenSSL的功能的函数:config_args``openssl_pkey_new

    /**
     * Tests the ability to 1) create pub/priv key pair 2) extract pub/priv keys 3) encrypt plaintext using keys 4) decrypt using keys
     * 
     * @return boolean|string False if fails, string if success
     */
    function testOpenSSL($opensslConfigPath = NULL)
    {
        if ($opensslConfigPath == NULL)
        {
            $opensslConfigPath = "E:/Services/Apache/httpd-2.4.9-win32-VC11/conf/openssl.cnf";
        }
        $config = array(
            "config" => $opensslConfigPath,
            "digest_alg" => "sha512",
            "private_key_bits" => 4096,
            "private_key_type" => OPENSSL_KEYTYPE_RSA,
        );

        $res = openssl_pkey_new($config); // <-- CONFIG ARRAY
        if (empty($res)) {return false;}

        // Extract the private key from $res to $privKey
        openssl_pkey_export($res, $privKey, NULL, $config); // <-- CONFIG ARRAY

        // Extract the public key from $res to $pubKey
        $pubKey = openssl_pkey_get_details($res);
        if ($pubKey === FALSE){return false;}

        $pubKey = $pubKey["key"];

        $data = 'plaintext data goes here';

        // Encrypt the data to $encrypted using the public key
        $res = openssl_public_encrypt($data, $encrypted, $pubKey);
        if ($res === FALSE){return false;}

        // Decrypt the data using the private key and store the results in $decrypted
        $res = openssl_private_decrypt($encrypted, $decrypted, $privKey);
        if ($res === FALSE){return false;}

        return $decrypted;
    }

    // Example usage:
    $res = testOpenSSL();
    if ($res === FALSE)
    {
        echo "<span style='background-color: red;'>Fail</span>";
    } else {
        echo "<span style='background-color: green;'>Pass: ".$res."</span>";
    }
关注者
0
被浏览
74
1 个回答
  • 面试哥
    面试哥 2021-02-02
    为面试而生,有面试问题,就找面试哥。

    下面的代码按预期方式工作。但是,如果您在运行openssl_error_string()openssl方法后运行它,则会显示出error:0E06D06C:configuration file routines:NCONF_get_string:no value这是我无法找到文档的一些注意。

    进一步请注意,根据http://www.php.net/manual/zh-CN/function.openssl-error-
    string.php
    ,由于错误消息已排队,您可能会看到误导性错误:

    使用此函数检查错误时要格外小心,因为它似乎是从错误列表中读取的,错误可能包括其他脚本或使用openssl>函数的进程中的错误。(我很惊讶地发现它在调用任何>
    openssl_ *函数之前会重现错误消息)

    <?php
    /* Create the private and public key */
    $res = openssl_pkey_new();
    openssl_error_string(); // May throw error even though its working fine!
    
    /* Extract the private key from $res to $privKey */
    openssl_pkey_export($res, $privKey);
    openssl_error_string(); // May throw error even though its working fine!
    
    /* Extract the public key from $res to $pubKey */
    $pubKey = openssl_pkey_get_details($res);
    $pubKey = $pubKey["key"];
    
    $data = 'i.amniels.com is a great website!';
    
    /* Encrypt the data using the public key
     * The encrypted data is stored in $encrypted */
    openssl_public_encrypt($data, $encrypted, $pubKey);
    
    /* Decrypt the data using the private key and store the
     * result in $decrypted. */
    openssl_private_decrypt($encrypted, $decrypted, $privKey);
    
    echo $decrypted;
    ?>
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看