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

发布于 2024-09-17 00:23:15 字数 881 浏览 6 评论 0原文

我正在尝试在 PHP 中使用 OpenSSL 函数进行 RSA 签名/验证。 当我尝试使用公钥执行 openssl_verify 时,收到此错误:错误:0906D06C:PEM 例程:PEM_read_bio:无起始行< /em>,但函数本身可以正常工作(如果消息被修改则返回 0,如果完好则返回 1)。 openssl_sign 工作正常。

我该如何修复它?

目前,我使用 openssl 生成的公钥:

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

有什么想法为什么会触发此错误,但一切正常?

尝试从私有生成公钥并使用它,但它似乎完全相同,相同的错误消息:-S

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

另外,我尝试安装所有内容的较新版本(PHP 5.3.1、OpenSSL 1.0.0a ) - 相同的结果。而且,我在窗户上。

I am trying to use OpenSSL function for RSA sign/verify in PHP.
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_sign works fine.

How can I fix it?

Currently, I use public key generated by 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

$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. And, I am on windows.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不喜欢何必死缠烂打 2024-09-24 00:24:11

原因:

此错误通常是由 .crt 文件开头的一个损坏字符引起的。因此,SSL 证书文件 (.crt) 或 SSL 密钥文件 (.key) 中可能存在额外的空格、额外的字符、额外的行等。

可能的解决方案:

  1. 检查您的 .crt 文件。
  2. 字符问题可能出在你的键上,试试这个(没有换行符等):

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

Reason:

This error is usually caused by one corrupt character at the beginning of the .crt file. 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).

Possible Solution(s):

  1. Check your .crt file.
  2. The character problem may be in your key, try this (without linebreaks, etc):

.

define("SC_MSG_PUBLIC", "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALjPcOckMHDVLiUstcRwwx8kF5PzsiEsrskyndWisbXMLU9BHomXwL7Qg2L91jE+sNSobkzBDF41CbwDiNlofZUCAwEAAQ==");
娇纵 2024-09-24 00:24:02

您可能会更轻松地使用 phpseclib 进行签名创建/验证:

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

You might have an easier time using phpseclib for signature creation / verification:

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

蓦然回首 2024-09-24 00:23:53

其他人都有一个 errno,成功操作后会自动重置为零,而 OpenSSL 有一个“错误堆栈”,您需要手动清空它。请参阅函数 openssl_error_string ,它是 根据 ERR_get_error。您看到的错误消息很可能与您的代码无关;尝试在代码之前添加此内容:

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

以及在每行之间:

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

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. See function openssl_error_string which is implemented in terms of 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";
Oo萌小芽oO 2024-09-24 00:23:43

您是否尝试使用包含您的公钥的(可能是自签名的)证书而不是纯公钥来调用 openssl_verify()

据我所知,一些 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";
?>

在 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]

这还将创建您之后可能想要删除的临时文件,即 [请求-TMP-文件]<​​/em> 和[随机-TMP-文件]<​​/em>。

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

Have you tried to call openssl_verify() with a (maybe self-signed) certificate containing your public key instead of a pure public key ?

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
$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):

# 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].

PHP sample code can be found at http://de.php.net/manual/en/function.openssl-csr-new.php.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文