views:

113

answers:

4

When generating public key and then reading it with function openssl_pkey_get_public - $publicKeyResource = bool(false) and message: error:0906D06C:PEM routines:PEM_read_bio:no start line

$privateKey = openssl_pkey_new(array('private_key_bits' => 2048, 'private_key_type' => OPENSSL_KEYTYPE_RSA));

$keyDetails = openssl_pkey_get_details($privateKey);

$publicKeyResource = openssl_pkey_get_public($keyDetails['key']);

What is wrong?

P.S.

privateKey = 

array(3) {
  ["bits"]=>int(2048)

  ["key"]=>
  string(451) "-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApo5lpSuSQmAOXfqAmexj
IzjdGnd1X1gCKj5ko2DHgcR4XBlj1hbFNs1pzXx+R/UvLXTeF7dNQ+9AgXjEeRa6
71VbNxrUgvb/PHjEANwce7xBsnbu+dcSazyNHzx4ahWyEF4f3HyaJkGrT/Dgzcut
DO+yFAH9u8Hx26cj/8kyrtIHxazemnD+IDHRa3zOjKDmTfoDRKtOMTPVgFAsYBXn
tKcLyamCSBgpwfQwKfUUcYhfY1xD9UMhVXabSSiNQOiTMuOIZUHueO8UCp/tdK6a
LprUDBQ/tVmiV7ZMeZYMjh6XnK7higJ3WZp8RmD4PPeKbtG6j2AuGpbF/ddzD62T
XwIDAQAB
-----END PUBLIC KEY-----
"

  ["type"]=>
  int(0)
}
A: 

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.

shamittomar
I don't use any files.Generated key in variable and used in next line, witout saving.
slamer
+1  A: 
notedshow
Thank you! It's work!
slamer
A: 

I suspect that this must be a bug in some version of either PHP or OpenSSL, because your posted code works fine for me (with PHP 5.2.6 and OpenSSL 0.9.8g).

caf
I'll try to upgrade today and will let you know...
BarsMonster
Very latest php_openssl gives same result.You may not see an error, if you are not checking for it. print(openssl_error_string()."<br/>");
BarsMonster
@BarsMonster: Nope, definitely no error. `$publicKeyResource` ends up with a valid public key resource, and there's no OpenSSL error set. I was actually thinking that the bug might have been something introduced in a *newer* version (of either PHP or OpenSSL), since mine is not particularly recent.
caf
Redeployed everything with recent versions of PHP, OpenSSL. Same error. Tried to generate public key out of working private one just before verification, still same result...
BarsMonster
@BarsMonster: I'd try *older* versions, since it could well be a regression. As another thought, could it be a character set issue of some kind? Are you using any kind of localisation?
caf
@caf: No localisation of any kind, plain vanilla PHP. I've already was on some 0.98? and 1.0.0a... Will try older...
BarsMonster
@BarsMonster: I tend to suspect PHP more than OpenSSL - what versions of PHP have you tested with?
caf
@caf: Some 5.2.x, currently 5.3.1
BarsMonster
+1  A: 
Archimedix
I confirm that it's right solution :-)
BarsMonster