繁体   English   中英

openssl_pkey_new 抛出错误配置文件routines:NCONF_get_string:no value

[英]openssl_pkey_new throws error configuration file routines:NCONF_get_string:no value

我发现了几个有这个问题的帖子,但我没有使用他们的解决方案解决我的问题。

这是我的测试脚本:

<?php
echo "\n*** Errors before calling openssl_pkey_new\n";
// no errors
while (($e = openssl_error_string()) !== false) {
    var_dump($e);
}

$config = [
    'config' => '/etc/ssl/openssl.cnf',
    "digest_alg" => "sha1",
    "private_key_bits" => 4096,
    "private_key_type" => OPENSSL_KEYTYPE_RSA,
];
var_dump(openssl_pkey_new($config));

echo "\n*** Errors after calling openssl_pkey_new\n";
while (($e = openssl_error_string()) !== false) {
    echo "<pre>";print_r($e);echo "</pre>";
}

还尝试了 sha256 和 sha512。

.cnf 文件:

ls /etc/ssl/openssl.cnf
-rw-r--r-- 1 root root 10835 Jun 20  2014 /etc/ssl/openssl.cnf

错误:

error:0E06D06C:configuration file routines:NCONF_get_string:no value</pre><pre>error:0E06D06C:configuration file routines:NCONF_get_string:no value</pre><pre>error:0E06D06C:configuration file routines:NCONF_get_string:no value

我还尝试在脚本顶部设置 OPENSSL_CONF 但没有成功:

putenv('OPENSSL_CONF=/etc/ssl/openssl.cnf');

我也尝试使用自定义 openssl.cnf 但也没有成功:

cat /var/www/openssl.cnf 
distinguished_name  = req_distinguished_name
[req_distinguished_name]
[v3_req]
[v3_ca]

有什么问题?

使用 openssl_pkey_new 后忽略此错误并清除它们是否安全或有解决方案?

先感谢您

问题分析

查看openssl_pkey_new() 文档,它提到:

有关openssl_csr_new()的更多信息,请参阅openssl_csr_new()

事实证明openssl_pkey_new()openssl_csr_new()实现共享读取配置的代码。 你可以看到它在PHP源代码调用这里通过符号PHP_SSL_REQ_PARSE其扩展到php_openssl_parse_config 它的第一个参数是x509_request类型。 (CSR 代表证书签名请求,有关更多信息,请参阅OpenSSL req 应用程序文档

筛选php_openssl_parse_config的实现,结果php_openssl_parse_config有很多尝试读取与 CSR 相关的配置参数,而不仅仅是密钥生成。 其中许多失败并产生与您指出的相同的错误。

为了让生活更轻松,我直接检测了 OpenSSL 加密库以打印有关任何失败的配置字符串查找的信息。 使用该设置运行脚本会产生以下结果(在 Ubuntu 18.04 上,使用/etc/ssl/openssl.cnf的配置):

$ php conftest.php 
_CONF_get_string failed for section "(null)", name "openssl_conf"

*** Errors before calling openssl_pkey_new
_CONF_get_string failed for section "(null)", name "oid_file"
_CONF_get_string failed for section "req", name "default_md"
_CONF_get_string failed for section "req", name "req_extensions"
_CONF_get_string failed for section "req", name "encrypt_rsa_key"
_CONF_get_string failed for section "req", name "encrypt_key"
_CONF_get_string failed for section "req", name "default_md"
resource(4) of type (OpenSSL key)

*** Errors after calling openssl_pkey_new
<pre>error:0E06D06C:configuration file routines:NCONF_get_string:no value</pre><pre>error:0E06D06C:configuration file routines:NCONF_get_string:no value</pre><pre>error:0E06D06C:configuration file routines:NCONF_get_string:no value</pre><pre>error:0E06D06C:configuration file routines:NCONF_get_string:no value</pre><pre>error:0E06D06C:configuration file routines:NCONF_get_string:no value</pre><pre>error:0E06D06C:configuration file routines:NCONF_get_string:no value</pre>

一个办法

从分析req_extensionsencrypt_rsa_keyopenssl.cnf[req]部分中为 main 部分中的设置oid_filedefault_mdreq_extensionsencrypt_rsa_key添加值应该可以解决错误。 确实,这样做之后,结果如下。

$ php conftest.php 

*** Errors before calling openssl_pkey_new
resource(4) of type (OpenSSL key)

*** Errors after calling openssl_pkey_new

结论

我认为您可以放心地忽略 PHP 对无关配置设置的错误调用。

在 mac 上更新 Mojave 后,我在我的系统上遇到了这个问题。

解决方案

我在我的openssl.cnf文件中取消了以下值的注释,它解决了这个问题

default_bits        = 2048

这是一个最低限度的配置,您可以在默认配置之上使用它来消除所有这些警告:

#PHP shim for an otherwise beautiful openssl.cnf
RANDFILE    = /dev/null #PHP warns if this doesn't exist
oid_file    = /dev/null #PHP warns if this doesn't exist
#PHP warns if oid_section isn't in the default section
#PHP warns if oid_section is used in another section (only on initialization)
oid_section = php_oids  #set an empty OID section
.include /etc/ssl/openssl.cnf    #include our working conf
[ req ]
  #differs from attr format
  attributes         = php_attr #openssl_csr_new()
  #not set in include
  encrypt_rsa_key    = yes #encrypt_key
  #uncomment to override include
  #req_extensions     = php_req_extension #req_extensions
  #x509_extensions    = php_x509_extension #x509_extensions
  #default_bits       = 4096          #private_key_bits
  #default_md         = sha512        #digest_alg
  #string_mask        = utf8only      #string_mask
  #distinguished_name = php_distinguished_name #openssl_csr_new()
[ php_attr ] #empty attributes section (supports callengePassword,unstructuredName)
[ php_oids ] #empty OID section
[ php_distinguished_name ] #empty DN section (supports both DN conf formats)
[ php_x509_extension ] #empty x509 extension section
  subjectKeyIdentifier   = hash #at least one value required
[ php_req_extension ] #empty req extension section
  subjectKeyIdentifier   = hash #at least one value required

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM