首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >openssl_pkey_new抛出错误配置文件routines:NCONF_get_string:no值

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

Stack Overflow用户
提问于 2018-08-29 19:52:29
回答 3查看 3.6K关注 0票数 3

我已经找到了几篇有这个问题的帖子,但我还没有使用他们的解决方案来解决我的问题。

这是我的测试脚本:

代码语言:javascript
复制
<?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文件:

代码语言:javascript
复制
ls /etc/ssl/openssl.cnf
-rw-r--r-- 1 root root 10835 Jun 20  2014 /etc/ssl/openssl.cnf

错误:

代码语言:javascript
复制
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设置在脚本的顶部,但没有成功:

代码语言:javascript
复制
putenv('OPENSSL_CONF=/etc/ssl/openssl.cnf');

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

代码语言:javascript
复制
cat /var/www/openssl.cnf 
distinguished_name  = req_distinguished_name
[req_distinguished_name]
[v3_req]
[v3_ca]

会有什么问题呢?

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

提前谢谢你

EN

回答 3

Stack Overflow用户

发布于 2018-08-30 08:25:59

对问题的分析

查看openssl_pkey_new() documentation,它提到:

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

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

通过对php_openssl_parse_config实现的筛选,发现有很多尝试读取与CSR相关的配置参数,但不仅仅是密钥生成。其中许多都会失败,并生成与您所指出的相同的错误。

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

代码语言:javascript
复制
$ 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>

一种解决方案

从分析中可以看出,在main部分中添加设置oid_file的值,在openssl.cnf[req]部分中添加default_mdreq_extensionsencrypt_rsa_key应该可以解决错误。实际上,在这样做之后,结果如下所示。

代码语言:javascript
复制
$ php conftest.php 

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

*** Errors after calling openssl_pkey_new

结论

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

票数 3
EN

Stack Overflow用户

发布于 2019-03-01 14:46:14

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

解决方案

我在我的openssl.cnf文件中取消了对下列值的注释,该文件修复了该问题

代码语言:javascript
复制
default_bits        = 2048
票数 1
EN

Stack Overflow用户

发布于 2020-02-10 16:52:57

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

代码语言:javascript
复制
#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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52076800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档