gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-28 23:57:29 25 4
gpt4 key购买 nike

问题: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/en/openssl.installation.php
  • PHP.ini extension=php_openssl.dll
  • Openssl.cnf E:\wamp\php\extras\openssl.cnf
  • %PATH% E:\wamp\php
  • 重新启动
  • phpinfo:
    ----启用 OpenSSL 支持
    ----OpenSSL 库版本 OpenSSL 1.0.1e 2013 年 2 月 11 日
    ----OpenSSL header 版本 OpenSSL 0.9.8y 2013 年 2 月 5 日
  • configargs 中指定和不指定 config
  • 在 apache 配置中指定或不指定 <Directory E:\wamp\php\extras>
  • 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,我现在可以使用 openssl_error_string 查看 open_ssl 错误
  2. 完全卸载 EasyPHP。手动安装稳定版本的 PHP/Apache。同样的结果!在 Windows 上实现 openssl 绝对是我做错了什么。
  3. OpenSSL 手动部分...附加错误信息

最后的想法:
我设置了一个 linux box,但我遇到了同样的错误。经过一番尝试后,我发现即使它在 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 变量如何,它都会不断出现文件未找到的错误。我能够通过在 configconfig_args 数组中传递 openssl_pkey_new 参数来解决它。下面是一个测试能否成功使用 OpenSSL 的函数:

    /**
* 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>";
}

最佳答案

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

进一步注意,根据 http://www.php.net/manual/en/function.openssl-error-string.php当错误消息排队时,您可能会看到误导性错误:

Be careful when using this function to check errors, as it seems to read from a buffer of > errors, which could include errors from another script or process that was using openssl > functions. (I was surprised to find it returing error messages before I had called any > openssl_* functions)

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

关于php - OpenSSL 在 Windows 上不起作用,错误 0x02001003 0x2006D080 0x0E064002,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15558321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com