<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Chaos Engine &#187; public key</title>
	<atom:link href="http://dev.modmancer.com/index.php/tag/public-key/feed/" rel="self" type="application/rss+xml" />
	<link>http://dev.modmancer.com</link>
	<description>Sometimes I drink to forget.. but then I forget to drink..</description>
	<lastBuildDate>Mon, 06 Feb 2012 16:11:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>PHP and OpenSSL key format (&#8220;key parameter is not a valid public key&#8221;)</title>
		<link>http://dev.modmancer.com/index.php/2010/07/07/php-and-openssl-key-format/</link>
		<comments>http://dev.modmancer.com/index.php/2010/07/07/php-and-openssl-key-format/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 22:27:34 +0000</pubDate>
		<dc:creator>akiko</dc:creator>
				<category><![CDATA[OpenSSL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[openssl_pkey_get_public error]]></category>
		<category><![CDATA[private key]]></category>
		<category><![CDATA[public key]]></category>
		<category><![CDATA[ssh]]></category>

		<guid isPermaLink="false">http://dev.modmancer.com/?p=73</guid>
		<description><![CDATA[PHP Warning: openssl_public_encrypt(): key parameter is not a valid public key in /script.php on line 175 If you experienced problems with OpenSSL and PHP, especially functions like openssl_pkey_get_public and openssl_pkey_get_private not willing to initialise from the provided public and private key strings, then you just need a few tweaks to get things running, because the [...]]]></description>
			<content:encoded><![CDATA[<p><strong><span style="color: #ff0000;">PHP Warning:  openssl_public_encrypt(): key parameter is not a valid public key in /script.php on line 175</span></strong></p>
<p>If you experienced problems with OpenSSL and PHP, especially functions like openssl_pkey_get_public and openssl_pkey_get_private not willing to initialise from the provided public and private key strings, then you just need a few tweaks to get things running, because the key formats are not compatible. If you are used to using ssh-keygen command line tool to generate your key pair, you will need to manually edit the public key in order to make it php openssl compatible. <span id="more-73"></span>For example your tmp_rsa.pub key looks like this:</p>
<pre>ssh-rsa ABABB3NzaC1yc2EAAAABIwAAAQEAtO9f1rn1plAH5flOotX0NYFjfQH7xt0dukt7v
8Nt3g7GFijXsoc+/+1SNSusHbj4LfBPXgKQJJoaZaCoQIWjBIXXKlODv+z2pSMBvvCPRThSFetqeh
/0pWcdiHPsmPOYpHby7zzwNKPCDyMrVrlC7FsaGmOC+F7FvSGA1PLdYEiOiJV/OmxQ
2HELrmhYPDc0vVPHfOETygNjjqMUuu8QwLvBgk3OUbT1m5NRNHMnpgPOID6+BBumLs
M0t8jOp1/AQG3pQFtlLBNETOMe7nuBPuE5pPhr5HbyV+9FUGI2FiYlNl7G+d8VlibR2wZkGHsa
p6mmzmJi64x4gNDdil+QDa== xyz@computer
</pre>
<p>That&#8217;s good for shell but not good enough for PHP openssl_pkey_get_public() function. You will need to manually edit it, and in the end it should look like this:</p>
<pre>-----BEGIN PUBLIC KEY-----
ABABB3NzaC1yc2EAAAABIwAAAQEAtO9f1rn1plAH5flOotX0NYFjfQH7xt0dukt7v
8Nt3g7GFijXsoc+/+1SNSusHbj4LfBPXgKQJJoaZaCoQIWjBIXXKlODv+z2pSMBvvC
PRThSFetqeh/0pWcdiHPsmPOYpHby7zzwNKPCDyMrVrlC7FsaGmOC+F7FvSGA1
PLdYEiOiJV/OmxQ2HELrmhYPDc0vVPHfOETygNjjqMUuu8QwLvBgk3OUbT1m5N
RNHMnpgPOID6+BBumLsM0t8jOp1/AQG3pQFtlLBNETOMe7nuBPuE5pPhr5HbyV
+9FUGI2FiYlNl7G+d8VlibR2wZkGHsap6mmzmJi64x4gNDdil+QDa==
-----END PUBLIC KEY-----
</pre>
<p>Don&#8217;t ask me why, it simply won&#8217;t work (or at least in my case didn&#8217;t work) if you don&#8217;t do it. It will still be complaining about some start line:</p>
<pre>error:0906D06C:PEM routines:PEM_read_bio:no start line
</pre>
<p>But that is obviously a trivial error, which you can ignore. I haven&#8217;t figured out yet how to avoid it.</p>
<p><strong>Another thing -</strong><br />
make sure you check for ssl errors after each ssl function. Here is a simple fn which can be helpful in your openSSL wrapper class:</p>
<pre>//! Checks for recent OpenSSL errors, and logs them.
//! \return true if no errors found, otherwise false.
public static function check_ssl_error()
{
  $ret = true;
  while ($msg = openssl_error_string())
  {
    // --- todo : log the error in your log file
    $ret = false;
  }
  return $ret;
}
</pre>
<p>So in the end you will init your openssl wrapper like this:</p>
<pre>$public_key_str = file_get_contents("./public.key");
$private_key_str = file_get_contents("./private.key");

MY_ASSERT($public_key_str, "Public key not found.");
MY_ASSERT($private_key_str, "Private key not found.");

$this-&gt;public_key = openssl_pkey_get_public($public_key_str);
OpenSSLWrapper::check_ssl_error();
OpenSSLWrapper::check_ssl_error();
$this-&gt;private_key = openssl_pkey_get_private($private_key_str);
OpenSSLWrapper::check_ssl_error();
</pre>
]]></content:encoded>
			<wfw:commentRss>http://dev.modmancer.com/index.php/2010/07/07/php-and-openssl-key-format/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

