简体   繁体   English

openssl_verify和“错误:0906D06C:PEM例程:PEM_read_bio:无起始行”

[英]openssl_verify and “error:0906D06C:PEM routines:PEM_read_bio:no start line”

I am trying to use OpenSSL function for RSA sign/verify in PHP. 我试图在PHP中使用OpenSSL函数进行RSA签名/验证。 When I try to do openssl_verify using my public key, I am getting this error: error:0906D06C:PEM routines:PEM_read_bio:no start line , but the function itself works correctly (returns 0 if messages was modified, and 1 if intact). 当我尝试使用我的公钥执行openssl_verify ,我收到此错误: error:0906D06C:PEM routines:PEM_read_bio:no start line ,但函数本身正常工作(如果消息被修改则返回0,如果完整则返回1)。 openssl_sign works fine. openssl_sign工作正常。

How can I fix it? 我该如何解决?

Currently, I use public key generated by openssl: 目前,我使用openssl生成的公钥:

define("SC_MSG_PUBLIC", <<<EOD
-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALjPcOckMHDVLiUstcRwwx8kF5PzsiEs
rskyndWisbXMLU9BHomXwL7Qg2L91jE+sNSobkzBDF41CbwDiNlofZUCAwEAAQ==
-----END PUBLIC KEY-----
EOD
);

Any ideas why this error triggers, but things works fine? 任何想法为什么这个错误触发,但事情工作正常?

Tried to generate public key out of private, and use it, but it appeared to be exactly the same, same error message :-S 尝试从私有生成公钥,并使用它,但它似乎完全相同,相同的错误消息:-S

$pkey = openssl_pkey_get_private(SC_MSG_PRIVATE);
$keyDetails = openssl_pkey_get_details($pkey);
file_put_contents('c:\publickey', $keyDetails['key']);

Also, I've tried to install newer versions of everything (PHP 5.3.1, OpenSSL 1.0.0a) - same result. 此外,我已经尝试安装所有新版本(PHP 5.3.1,OpenSSL 1.0.0a) - 结果相同。 And, I am on windows. 而且,我在窗户上。

Have you tried to call openssl_verify() with a (maybe self-signed) certificate containing your public key instead of a pure public key ? 您是否尝试使用包含公钥而不是纯公钥的(可能是自签名)证书来调用openssl_verify()

As far as I know, some PHP OpenSSL functions do not properly support naked public keys although it seems strange that it does verify correctly in spite of the error. 据我所知,一些PHP OpenSSL函数不能正确支持裸公钥,尽管尽管出现错误但它确实能够正确验证似乎很奇怪。

<?php
$private = openssl_pkey_get_private(file_get_contents('private'), 'passphrase');

// This causes the "no start line" error when using a naked public key:
$public  = openssl_pkey_get_public(file_get_contents('public')); // <-- this should be cert

echo openssl_error_string()."\n";

openssl_sign('Test', $sig, $private);
var_dump(openssl_verify('Test', $sig, $public));

echo openssl_error_string()."\n";
?>

Example for converting a public key to a simple certificate in a Linux/UNIX shell such as bash (refer to the OpenSSL documentation or some tutorials for more): 将公钥转换为Linux / UNIX shell(如bash)中的简单证书的示例(有关更多信息,请参阅OpenSSL文档或一些教程):

# Create certificate request
openssl req -new -days 3600 -key [PRIVATE-KEY-FILE] -out [REQUEST-TMP-FILE]

# Create certificate from request
RANDFILE=[RANDOM-TMP-FILE] openssl x509 -req -in [REQUEST-TMP-FILE] -signkey [PRIVATE-KEY-FILE] -out [CERTIFICATE-OUT-FILE]

This will also create temporary files you might want to delete afterwards, namely [REQUEST-TMP-FILE] and [RANDOM-TMP-FILE] . 这也将创建您之后可能要删除的临时文件,即[REQUEST-TMP-FILE][RANDOM-TMP-FILE]

PHP sample code can be found at http://de.php.net/manual/en/function.openssl-csr-new.php . PHP示例代码可以在http://de.php.net/manual/en/function.openssl-csr-new.php找到。

Where everyone else has an errno that is reset to zero automatically by successful operations, OpenSSL has an "error stack", that you need to empty manually. 其他人都有一个错误的错误,由于成功的操作自动重置为零,OpenSSL有一个“错误堆栈”,您需要手动清空。 See function openssl_error_string which is implemented in terms of ERR_get_error . 见功能openssl_error_string这是实现来讲ERR_get_error Chances are that the error message that you are seeing has nothing to do with your code; 您可能看到的错误消息与您的代码无关; try adding this before your code: 尝试在代码之前添加:

while ($msg = openssl_error_string()) {};

and in between each line: 在每一行之间:

while ($msg = openssl_error_string())
    echo "OpenSSL error when doing foo:" . $msg . "<br />\n";

You might have an easier time using phpseclib for signature creation / verification: 您可以更轻松地使用phpseclib进行签名创建/验证:

http://phpseclib.sourceforge.net/documentation/misc_crypt.html#misc_crypt_rsa_examples http://phpseclib.sourceforge.net/documentation/misc_crypt.html#misc_crypt_rsa_examples

Reason: 原因:

This error is usually caused by one corrupt character at the beginning of the .crt file. 此错误通常是由.crt文件开头的一个损坏字符引起的。 So, the chances are that you have an extra space, an extra character, an extra line, etc. in either the SSL Certificate file (.crt) or the SSL key file (.key). 因此,您可能在SSL证书文件(.crt)或SSL密钥文件(.key)中有额外的空格,额外的字符,额外的行等。

Possible Solution(s): 可能的解决方案):

  1. Check your .crt file. 检查.crt文件。
  2. The character problem may be in your key, try this (without linebreaks, etc): 角色问题可能在你的密钥中,试试这个(没有换行符等):

.

define("SC_MSG_PUBLIC", "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALjPcOckMHDVLiUstcRwwx8kF5PzsiEsrskyndWisbXMLU9BHomXwL7Qg2L91jE+sNSobkzBDF41CbwDiNlofZUCAwEAAQ==");

暂无
暂无

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

相关问题 Firebase PHP JWT“OpenSSL无法验证数据:错误:0906D06C:PEM例程:PEM_read_bio:无启动行” - Firebase PHP JWT “OpenSSL unable to verify data: error:0906D06C:PEM routines:PEM_read_bio:no start line” 将 PEM 格式的公钥传递给 openssl_pkey_get_public 会给出错误:0906D06C:PEM 例程:PEM_read_bio:no start line - Passing public key in PEM format to openssl_pkey_get_public gives error:0906D06C:PEM routines:PEM_read_bio:no start line openssl_verify():不能将提供的密钥参数强制转换为 .pem 文件的公钥 - openssl_verify(): supplied key param cannot be coerced into a public key for a .pem file ECDSA:将 60 字节的二进制公钥转换为 PEM 格式,以便与 PHP openssl_verify() 一起使用 - ECDSA: Convert a binary public key of 60 bytes into PEM format for using it with PHP openssl_verify() lombucci/jwt 无法解析您的密钥,原因:error:0909006C:PEMroutines:get_name:no start line - lombucci/jwt It was not possible to parse your key, reason: error:0909006C:PEM routines:get_name:no start line openssl_private_encrypt和“错误:0E06D06C…NCONF_get_string:无值” - openssl_private_encrypt and “error:0E06D06C…NCONF_get_string:no value” PHP:openssl_verify无法与ECDSA密钥一起使用 - PHP: openssl_verify not working with ECDSA keys PHP OpenSSL 无法读取 PEM 格式的公钥 - PHP OpenSSL cannot read public key in PEM format 是否有Coldfusion替代php函数openssl_verify()? - Is there a Coldfusion alternative to the php function openssl_verify()? 在php函数openssl_pkcs7_sign中打开pem文件时出错 - Error opening pem file in php function openssl_pkcs7_sign
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM