<?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>MagicalTux in Japan &#187; SSL</title>
	<atom:link href="http://blog.magicaltux.net/tag/ssl/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.magicaltux.net</link>
	<description>Geekness brought me to Japan!</description>
	<lastBuildDate>Mon, 26 Jul 2010 21:31:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>SSL server: how to autodetect SSL/non-SSL?</title>
		<link>http://blog.magicaltux.net/2008/12/07/ssl-server-how-to-autodetect-sslnon-ssl/</link>
		<comments>http://blog.magicaltux.net/2008/12/07/ssl-server-how-to-autodetect-sslnon-ssl/#comments</comments>
		<pubDate>Sat, 06 Dec 2008 22:11:57 +0000</pubDate>
		<dc:creator>MagicalTux</dc:creator>
				<category><![CDATA[Geek Attitude]]></category>
		<category><![CDATA[Hacking]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Async]]></category>
		<category><![CDATA[Server]]></category>
		<category><![CDATA[SSL]]></category>

		<guid isPermaLink="false">http://blog.magicaltux.net/?p=188</guid>
		<description><![CDATA[For those of you who already played with Webmin, you probably noticed that connecting without SSL to the webmin interface (typically on port 10000) displays a message telling you how to access the SSL secured interface&#8230; on the same port. Ever wondered how to accept both SSL and non-SSL connections on the same port? Basically, [...]]]></description>
			<content:encoded><![CDATA[<p>For those of you who already played with <a href="http://www.webmin.com/" target="_blank">Webmin</a>, you probably noticed that connecting without SSL to the webmin interface (typically on port 10000) displays a message telling you how to access the SSL secured interface&#8230; on the same port.</p>
<p>Ever wondered how to accept both SSL and non-SSL connections on the same port?</p>
<p>Basically, to display such a message, we need to know if the client talking to us is speaking using SSL, or not. This is easily done by reading a few bytes from the stream, but if you do this, starting the crypto using for example <a href="http://php.net/stream_socket_enable_crypto" target="_blank">stream_socket_enable_crypto()</a> will fail, since OpenSSL won&#8217;t find the full client SSL handshake anymore.</p>
<p>PHP offers us a nice solution to fix this, using <a href="http://php.net/stream_socket_recvfrom" target="_blank">stream_socket_recvfrom()</a>. By passing option <em>STREAM_PEEK</em> to this function, we can take a peek at the data pending in the socket, and try to determine if that&#8217;s SSL or not.</p>
<p>There, we can either try to parse a SSL packet, or instead try to find data we know there should be if the stream is not encrypted.</p>
<p>Doing this for the HTTP protocol is easy. The protocol is text based, and the first word we will get from the client will be something like &#8220;GET&#8221;, &#8220;POST&#8221; or &#8220;HEAD&#8221;. We just check if we got any of those. If we did, we got plain text connection. If we don&#8217;t, it means we are probably facing a real openssl client, and we can try to start negociating the link.</p>
<p>I wrote a little example you can download via SVN at <a href="http://ookoo.org/svn/snip/https_multi_serv/" target="_blank">http://ookoo.org/svn/snip/https_multi_serv/</a>. Just run &#8220;gen_key.sh&#8221; in the ssl directory to get a SSL private key, then run the server with PHP. By default it will listen on port 8000, so direct your browser to localhost:8000 with or without SSL (both will work, this is the point of this server).</p>
<p>Feel free to use the code there, I commented it a bit so it should be somewhat helpful, and I officially release it under public domain (or BSD if &#8220;public domain&#8221; does not legally exists in your country).</p>
<p>By the way it&#8217;s also a nice example of async server using <a href="http://php.net/stream_select" target="_blank">stream_select()</a>.</p>
<p>Of course it&#8217;s not possible to <em>auto-magically</em> determine if the client is talking SSL when he&#8217;s not talking first. You could wait for one or two seconds to see if something comes (ie. an SSL handshake) but it&#8217;s not really something that could be called <em>good practice</em>&#8230; So let&#8217;s just keep this for cases where the client talks first.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.magicaltux.net/2008/12/07/ssl-server-how-to-autodetect-sslnon-ssl/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
