<?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>Tom&#039;s Blog &#187; security</title>
	<atom:link href="http://www.encapsulated.org/blog/tag/security/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.encapsulated.org/blog</link>
	<description>An analog guy in a digital world</description>
	<lastBuildDate>Tue, 15 Dec 2009 20:46:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Security with Zend_AMF and Flex &#8211; Part 1: Theory</title>
		<link>http://www.encapsulated.org/blog/2009/04/05/security-with-zend_amf-and-flex-part-1-theory/</link>
		<comments>http://www.encapsulated.org/blog/2009/04/05/security-with-zend_amf-and-flex-part-1-theory/#comments</comments>
		<pubDate>Sun, 05 Apr 2009 18:24:46 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[chap]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://www.encapsulated.org/blog/?p=24</guid>
		<description><![CDATA[In a series of two posts, I will explain how to secure the communication between a Flex client and PHP server architecture. The first part will explain how I envision that security, and in the second part I will show snippets of PHP code for the practical implementation.
I&#8217;m currently working together with my friend and [...]]]></description>
			<content:encoded><![CDATA[<p>In a series of two posts, I will explain how to secure the communication between a Flex client and PHP server architecture. The first part will explain how I envision that security, and in the second part I will show snippets of PHP code for the practical implementation.</p>
<p>I&#8217;m currently working together with my friend and colleague <a title="VICR" href="http://www.vicr.be">Vic</a> on a client-server application that involves <a title="Flex" href="http://www.adobe.com/products/flex/">Flex</a> on the front-side, and PHP (<a href="http://zendframework.com/">Zend Framework</a>) on the back. Since I&#8217;m the PHP guy, I&#8217;m in charge of creating the API for his Flex application. For the moment, the project will only be accessed locally from the client&#8217;s network. But there is a possibility that in a later stage, it might open up to the general public. One of my main concerns was how we could make every API call as secure as possible. This without making it too complicated, or involve too many service calls that might slow everything down.<br />
<span id="more-24"></span></p>
<p><strong>First login&#8230;</strong></p>
<p>The first aspect of securing the application, is authentication of users. The application is accessible to multiple users, and those are grouped in various groups. Each group has different privileges. To identify every user, they are given usernames and passwords. My biggest concern with the login process, is that we didn&#8217;t have access to a secure connection via SSL. This means that every communication between the client and server software can be monitored by network sniffers. Flex uses the <a href="http://en.wikipedia.org/wiki/Action_Message_Format">AMF protocol</a> to communicate with Zend_AMF. Even though this is a binary stream, the specifications are open to everyone. So it is possible to intercept communications, and read everything out.</p>
<p>I wanted to make sure that during the login process, the password was not exposed to the outside world via the communications line. Because once the password and username are intercepted, then it would be easy to start making fake API calls to the server. The solution I had in mind, is called CHAP &#8211; <a title="CHAP protocol" href="http://en.wikipedia.org/wiki/Challenge-handshake_authentication_protocol">Challenge-Handshake Authentication Protocol</a>. Here&#8217;s a diagram about how this protocol works.</p>
<p><img class="aligncenter size-full wp-image-26" src="http://www.encapsulated.org/blog/wp-content/uploads/2009/04/diagram1.png" alt="CHAP diagram" width="350" height="217" />The main purpose of using CHAP, is to authenticate users, without having to transmit the password between the client and the server. To accomplish this, we have to replace the password with something else, but still allowing the server to recognize the correct user, and log him in. I call this password replacer the &#8220;signature&#8221;.</p>
<p>In the first phase of the diagram, the client requests a &#8220;challenge&#8221; from the server. A challenge is a key that will be used by both the client and the server, to create the signature. During the first phase, the client asks the server what key will be used to encrypt the password. It is important that both the client and the server use the exact same challenge.</p>
<p>During the second phase, an authentication request is being transmitted. Instead of using the username and password, we use the username and signature. The final signature is calculated like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$signature</span> <span style="color: #339933;">=</span> <span style="color: #990000;">MD5</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">MD5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$challenge</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The client calculates this signature, and sends it to the server. The server then looks up the given username in the list of possible users. The corresponding password is then used by the server, to also calculate a signature, with the earlier given challenge. The outcome of that comparison is given during the third and last phase of the CHAP authentication.</p>
<p>This is not a very difficult protocol to implement, but according to me, it still provides a reasonable amount of security during the login process.</p>
<p><strong>&#8230; and then the rest of the calls.</strong></p>
<p>Now that a user is logged in, the most important stuff still has to happen: interactivity with the server by calling API methods. Once the user is authenticated, he remains authenticated by using the browser session. So now we have to keep in mind that other people might try to steal the session, and invoke undesired API calls. During each call, the server needs to be sure that it was still the same client making the call, and not some <a title="Man-in-the-middle attack" href="http://en.wikipedia.org/wiki/Man-in-the-middle_attack">man-in-the-middle</a>.</p>
<p>For this, I decided to elaborate on the CHAP protocol, used for authentication. How about we could keep the system, where the client signs its calls with a signature. For obvious reasons, it is not possible to recycle the signature from the login procedure. But I wanted to force the client to generate a new signature for each API request. There are still two variables to generate the signature: the password and the challenge.</p>
<p>It was not a practical option to ask the user&#8217;s password for each and every API call. Instead, once the user was authenticated, I had the Flex application remember the password in its memory. This way, it could be recycled to generate new signatures.</p>
<p>The only problem was the challenge. A first option was to have the client request a challenge from the server. But this would mean that for each regular API call, there would be an additional call for a challenge. That would double the amount of API calls. I was afraid that in the long run, this would generate too much calls for the server. That&#8217;s why I chose a second option: have the client generate the challenge and transmit it alongside the signature. The server would then use this challenge, and the password from the session, to calculate the signature. If they matched, then the call was permitted to be executed.</p>
<p>As far as I could see, this wouldn&#8217;t impose a bigger security risk. Of course, a man-in-the-middle could generate fake challenges. But he still wouldn&#8217;t be able to generate fake signatures. For that, he still needs the correct password. Once he has that, then well&#8230; all security checks fail. The responsibility of keeping the password secret lies entirely at the client now.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.encapsulated.org/blog/2009/04/05/security-with-zend_amf-and-flex-part-1-theory/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
