openssl_pkey_new引发错误配置文件例程:NCONF_get_string:无值

时间:2018-08-29 11:52:29

标签: php openssl

我发现了几篇有关此问题的帖子,但是我没有使用他们的解决方案解决问题。

这是我的测试脚本:

<%= collection_select("country", "id", @countries , :id, :name, {:prompt => true}, :id => 'xx', :onchange => "location.href = window.location.href") %>

也尝试过sha256和sha512。

cnf文件:

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

错误:

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

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

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.cnf,但也没有成功:

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

可能是个问题吗?

使用openssl_pkey_new后是否可以忽略此错误并清除它们,是否安全?

提前谢谢

3 个答案:

答案 0 :(得分:1)

问题分析

看着openssl_pkey_new() documentation,它提到:

  

有关配置参数的更多信息,请参见openssl_csr_new()

事实证明,openssl_pkey_new()openssl_csr_new()实现共享用于读取配置的代码。您可以通过符号PHP_SSL_REQ_PARSE扩展为php_openssl_parse_config在PHP源代码here中看到其调用。它的第一个参数是x509_request类型。 (CSR代表证书签名请求,有关更多信息,请参见OpenSSL req app documentation

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

为了简化工作,我直接检测了OpenSSL crypto库,以打印有关任何失败的配置字符串查找的信息。使用该设置运行脚本会导致以下结果(在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>

解决方案

从分析来看,好像在主要部分中为设置oid_file和在default_md部分中的req_extensionsencrypt_rsa_key[req]中添加值openssl.cnf中的可以解决错误。确实,这样做之后,结果如下。

$ php conftest.php 

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

*** Errors after calling openssl_pkey_new

结论

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

答案 1 :(得分:0)

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

解决方案

我未在解决该问题的 openssl.cnf 文件中注释以下值

default_bits        = 2048

答案 2 :(得分:0)

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

#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