2012-03-07 40 views
2

下面的PHP代码打印出这样的警告:为什么我无法从PHP中的X509证书中获取公钥?

警告:openssl_csr_get_public_key():提供的资源不是/home/swissbtc/www/bitcoins.ch/index.php有效的OpenSSL的X​​.509 CSR资源上线49

代码:

$Configs = array(  
     'digest_alg' => 'sha1', 
     'x509_extensions' => 'v3_ca', 
     'req_extensions' => 'v3_req', 
     'private_key_bits' => 2048, 
     'private_key_type' => OPENSSL_KEYTYPE_RSA, 
     'encrypt_key' => true, 
     'encrypt_key_cipher' => OPENSSL_CIPHER_3DES 
); 

//generate cert 
$dn  = array('commonName' => 'test'); 
$privkey = openssl_pkey_new($Configs); 
$csr  = openssl_csr_new($dn, $privkey, $Configs); 
$cert  = openssl_csr_sign($csr, null, $privkey, 365, $Configs); 

//try to get public key 
$publicKey = openssl_csr_get_public_key($cert); //line 49 

//try again to get the public key 
openssl_x509_export($cert, $certout); 
$publicKey = openssl_csr_get_public_key($certout); 

注:第一$公钥(49行)是EM pty和第二个$ publicKey(第53行)获取布尔值“false”

我的代码有什么问题?

+0

这要看你的密钥文件的内容。你如何生成它/你从哪里得到它? – Raptor 2012-03-07 02:07:08

+1

密钥和证书都在上面的代码中生成。 – Roland 2012-03-07 13:37:20

回答

2

这个工作对我来说:

$Configs = array(  
    'digest_alg' => 'sha1', 
    'x509_extensions' => 'v3_ca', 
    'req_extensions' => 'v3_req', 
    'private_key_bits' => 2048, 
    'private_key_type' => OPENSSL_KEYTYPE_RSA, 
    'encrypt_key' => true, 
    'encrypt_key_cipher' => OPENSSL_CIPHER_3DES 
); 

//generate cert 
$dn  = array('commonName' => 'test'); 
$privkey = openssl_pkey_new($Configs); 
$csr  = openssl_csr_new($dn, $privkey, $Configs); 
$cert  = openssl_csr_sign($csr, null, $privkey, 365, $Configs); 
$publicKey = openssl_pkey_get_public($cert); 

var_dump($publicKey); 
+0

看来您正在使用CSR(证书签名请求)来导出$ publicKey。我想从“真正的”x509证书中进行导出。 – Roland 2012-03-08 16:57:26

+1

我已经更新了答案 – Martin 2012-03-08 18:40:35

+0

谢谢,作品完美! – Roland 2012-03-11 01:06:52