【发布时间】:2013-03-11 14:24:05
【问题描述】:
问题: 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 标头版本 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:
编辑:
- 感谢@Gordon,我现在可以看到使用
openssl_error_string的open_ssl 错误 - 完全卸载 EasyPHP。手动安装稳定版本的 PHP/Apache。结果一样!在 Windows 上实现 openssl 绝对是我做错了。
- 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 变量或在脚本执行期间,它都会不断出错,提示找不到文件。我可以通过在openssl_pkey_new 的config_args 数组中传入config 参数来解决它。这是一个测试能否成功使用 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.cnf 文件,如果我将 config 设置为一个不存在的文件,我不会收到文件错误。所以我认为问题出在 openssl.cnf、apache 权限或 openssl 二进制文件丢失/权限... /2days
-
我必须把它放进去,除非你使用 MS 特定的产品,比如 MSSQL 和 .NET 的东西,否则请在服务器环境中使用 Linux。如果您无法为其设置盒子,请使用虚拟化(例如 vmware、virtualbox)
-
@Populus '改变你的操作系统' 不是现实世界的解决方案。
-
但是您可以轻松制作虚拟环境。任何想在 Windows 上使用 Apache 的人都应该重新考虑他们的计划:x 所以我只能假设这是一个测试环境。
-
这是一个开发环境。虽然生产将在 linux 中进行,但不幸的是,这是我坚持开发的环境。虽然我同意,但我也想找到答案,因为我知道这可以在 Windows 上运行。
标签: php apache apache2 openssl easyphp