我有一个证书(.cer),我必须读取证书的基本信息,如有效期。
我用php写的这段代码。
class FirmaElectronica {
public function abrirCertificado( $path ){
$cert_content = file_get_contents( $path );
$res = openssl_x509_read( $cert_content );
$data = openssl_x509_parse( $res );
var_dump( $data );
}
}
$firma = new FirmaElectronica();
$firma->abrirCertificado('gohl881206rga.cer');但是总是得到这个警告和一个空的数据数组
PHP Warning: openssl_x509_read(): supplied parameter cannot be coerced into an X509 certificate!如果我执行此命令,我将获得所有证书数据
openssl x509 -in gohl881206rga.cer -noout -text -inform der用php获取证书数据的正确方式是什么?
发布于 2014-11-03 22:33:41
使用phpseclib..
<?php
include('File/X509.php');
$x509 = new File_X509();
$cert = $x509->loadX509('...');
echo $cert['tbsCertificate']['validity']['notBefore'] . "\r\n";
echo $cert['tbsCertificate']['validity']['notAfter'];如果这不起作用,你能发布你试图从其中获取此信息的实际证书吗?
发布于 2015-11-18 07:40:49
好的。要解决这个问题,您需要确保您拥有cer (或pem)的私钥。
回答
1将证书和密钥导出到.p12
在操作系统上,例如:打开密钥链->,选择您的证书和包含密钥的->,按住Ctrl键并单击导出->导出2对象。
2转换您的证书:
openssl pkcs12 -in cert.p12 -out cert.pem -nodes -clcerts3要安全
cat cert.pem 您应该会看到类似这样的内容:
Bag Attributes
friendlyName: blablub
localKeyID: SOME ID HERE
subject=/UID=(name)/CN=(a name)/OU=(a id)/C=(locale)
issuer=/C=(locale)/O=(auth)/OU=(authname)/CN=(name)
-----BEGIN CERTIFICATE-----
ABCD.....(cert here).....
-----END CERTIFICATE-----
Bag Attributes
friendlyName: blubbla
localKeyID: SOME ID HERE
Key Attributes: <No Attributes>
-----BEGIN RSA PRIVATE KEY-----
AB....(key here)....
-----END RSA PRIVATE KEY-----现在你已经准备好在php中使用你的原始代码而不需要另外的库了:
$cert_content = file_get_contents( $path );
$res = openssl_x509_read( $cert_content );发布于 2017-10-30 17:24:57
我知道这个问题已经过时了,但我想分享这个问题的一个可能的解决方案。
First:我也有同样的问题。证书是正确的,我的PHP代码也正确。
我发现问题出在证书的格式化上。为了解决这个问题,我创建了一个函数并正确地编辑了证书:
function createCertificate($cert) {
$a = "-----BEGIN CERTIFICATE-----\n";
$b = "\n-----END CERTIFICATE-----";
$withoutFirsLine = substr($cert, strlen('-----BEGINCERTIFICATE-----'));
$count = (strlen($withoutFirsLine) - strlen('-----ENDCERTIFICATE-----'));
$withoutFirstAndLastLine = substr($withoutFirsLine, 0, $count);
$withoutFirstAndLastLine = wordwrap($withoutFirstAndLastLine, 64, "\n", true);
return $a.$withoutFirstAndLastLine . $b;
}我希望我能帮助其他有同样问题的人!
https://stackoverflow.com/questions/26661083
复制相似问题