为什么可以';t我从PHP中的X509证书中获取公钥


Why can't I get the public key out of a X509 certificate in PHP?

以下PHP代码打印出此警告:

警告:openssl_csr_get_public_key():在第49行的/home/swissbtc/www/bitcoins.ch/index.php中,提供的资源不是有效的openssl X.509 csr资源

代码:

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

注意:第一个$publicKey(第49行)为空,第二个$publicKey(第53行)获得布尔值"false"

我的代码出了什么问题?

这对我有效:

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