<?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>4sak3n Design &#187; PHP</title>
	<atom:link href="http://4sk.us/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://4sk.us</link>
	<description>The trials and tribulations of a programmer.</description>
	<lastBuildDate>Mon, 07 May 2012 20:13:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Secure Plain Text Authentication in PHP using SCRAM</title>
		<link>http://4sk.us/2011/10/secure-plain-text-authentication-in-php-using-scram/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=secure-plain-text-authentication-in-php-using-scram</link>
		<comments>http://4sk.us/2011/10/secure-plain-text-authentication-in-php-using-scram/#comments</comments>
		<pubDate>Thu, 20 Oct 2011 08:27:40 +0000</pubDate>
		<dc:creator>Justin Jahn</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[Crypto JS]]></category>
		<category><![CDATA[Dojo]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[login]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SCRAM]]></category>

		<guid isPermaLink="false">http://4sk.us/?p=28</guid>
		<description><![CDATA[In today&#8217;s world, a secure authentication mechanism for web sites is an absolute necessity.  Hackers and script kiddies love to hijack accounts in any way possible.  In some cases even entire databases hackers are compromising entire databases, which, is the last thing a developer wants to be held responsible for. Today, I will be showing [...]]]></description>
			<content:encoded><![CDATA[<p>In today&#8217;s world, a secure authentication mechanism for web sites is an absolute necessity.  Hackers and script kiddies love to hijack accounts in any way possible.  In some cases even entire databases hackers are compromising entire databases, which, is the last thing a developer wants to be held responsible for.</p>
<p>Today, I will be showing you a mechanism to not only secure the passwords being held in your databases, but a way to secure the authentication process itself from prying eyes.  Enter SCRAM.  While it&#8217;s impossible to completely prevent a man in the middle access attack, utilizing SCRAM will certainly make it exponentially more difficult.  For a site that isn&#8217;t running e-commerce, I believe this is a much more cost-effective solution to an SSL certificate.</p>
<p><span id="more-28"></span></p>
<h2>SCRAM</h2>
<p>SCRAM is a <a title="Scram Whitepaper" href="http://www.isode.com/whitepapers/scram.html" target="_blank">relatively new</a> protocol for authentication written under <a title="SCRAM RFC" href="http://tools.ietf.org/html/rfc5802" target="_blank">RFC 5802</a> that works very well with web technologies as they are today.  It is algorithm agnostic, protocol agnostic (for the most part), and straightforward to implement.  The basic concept of SCRAM is that the client and server never send enough information for a hacker to simply decrypt (or use rainbow tables) the password.  Instead, a &#8216;client proof&#8217; is generated that the server uses to determine authentication.  If you wish to know more about it, feel free to read the links above.</p>
<h3>Differences</h3>
<p>There are some key differences in the SCRAM-SHA256-JSON approach we will be utilizing today:</p>
<ol>
<li>The protocol is HTTP, so some bits of the protocol are no longer necessary/possible.</li>
<li>In lieu of an AuthMessage, a JSON object is sent back and forth.  This is possible because the RFC defines the order of each part included in an AuthMessage.</li>
<li>The &#8216;third phase&#8217;, where client verifies server authentication has been left out to reduce client-side overhead.</li>
<li>A smaller iteration was chosen to cut the impact on performance caused by JavaScript code.</li>
<li>Input passwords will be hashed before salting.</li>
</ol>
<p>I tried to stay as true to the protocol as possible, however some things just didn&#8217;t make sense in a web environment where connections do not persist and encryption transparent to the code itself.  None of the authentication steps have changed, just the way they are sent.</p>
<h2>The Code</h2>
<p>This tutorial is broken up into two major sections, the server-side and client side.  I will talk about what needs to happen on each step and present you with the well commented and completed source code.</p>
<h3>Overview</h3>
<p>I think it will be necessary to outline what we are actually trying to do in each section.   The SCRAM authentication process works like this:</p>
<pre class="brush: text; gutter: false">1. Client sends username and ClientNonce.
    a. The client stores its own request string in memory for later use.
2. Server sends salt, ClientNonceServerNonce, and iterations.
    a. The server stores the client request and its own in SESSION
       for later use.
    b. The client stores the server request in memory for later use.
    c. The client uses the iterations response to determine the strength
       of the hash.
3. Client performs the calculations necessary to form a ClientProof,
   sending it and a ClientNonceServerNonce to the server.
    a. The server verifies the ClientNonceServerNonce with the one
       stored in SESSION.
    b. The server performs the calculations necessary to get a
       ClientSignature.
    c. The server obtains the ClientKey by performing an XOR on
       ClientSignature and ClientProof.
    d. The server hashes the SaltedPassword using the iterations
       provided in step 2.
    e. If the passwords match, an HTTP 200 response is sent with
       a URL to use on page refresh.  If the passwords don't
       match, an HTTP 401 response is sent.</pre>
<h3>Technologies Used</h3>
<p>You should not need to install additional PHP extensions, however, <a title="PHP Manual on Mcrypt" href="http://php.net/manual/en/book.mcrypt.php" target="_blank">mcrypt</a> is highly recommended if you don&#8217;t already have it.  You might also want to pickup <a title="PHP Manual on Multibyte Strings" href="http://php.net/manual/en/book.mbstring.php" target="_blank">mbstring</a> (if you don&#8217;t already have it).  A minimum PHP version of 5.2 is required to run the provided code.  It can certainly run on older versions with some effort, but PHP 5.3 is becoming the norm, so get with the program!</p>
<p>The client side will use the <a title="Dojo Toolkit Main Page" href="http://dojotoolkit.org/" target="_blank">Dojo Toolkit&#8217;s</a> core libraries for element manipulation and prototyping.  There isn&#8217;t much Dojo-specific code in the provided file, so it will be easy to remove if you so choose.  On the other hand, <a title="Crypto JS on Google Code" href="http://code.google.com/p/crypto-js/" target="_blank">Crypto-JS</a> is definitely engrained within the client side code.  Crypto-JS can be custom compiled using Google&#8217;s <a title="Closure Compiler on Google Code" href="http://code.google.com/closure/compiler/" target="_blank">Closure Compiler</a> if need be, which makes it an extremely small library to include.</p>
]]></content:encoded>
			<wfw:commentRss>http://4sk.us/2011/10/secure-plain-text-authentication-in-php-using-scram/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

