<?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>Dimuthu's Blog &#187; security</title>
	<atom:link href="http://www.dimuthu.org/catagory/security/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dimuthu.org</link>
	<description>Waiting for your comments</description>
	<lastBuildDate>Wed, 21 Dec 2011 05:39:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<image>
  <link>http://www.dimuthu.org</link>
  <url>http://www.dimuthu.org/favicon.ico</url>
  <title>Dimuthu's Blog</title>
</image>
		<item>
		<title>Security Considerations in Firefox When Accessing Different Domains</title>
		<link>http://www.dimuthu.org/blog/2008/12/22/security-considerations-in-firefox-when-accessing-different-domains/</link>
		<comments>http://www.dimuthu.org/blog/2008/12/22/security-considerations-in-firefox-when-accessing-different-domains/#comments</comments>
		<pubDate>Mon, 22 Dec 2008 18:14:57 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[firefox]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[domains]]></category>
		<category><![CDATA[external domains]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=843</guid>
		<description><![CDATA[If you are a web developer you may have experienced that there are many situations that you need to access remote domains for data sources. For an example if you are building a weather mashup, you may like to connect to some weather forecasting services like  http://www.weather.gov or http://weather.cnn.com/weather/forecast.jsp. Mostly these services are very simple, [...]]]></description>
			<content:encoded><![CDATA[<p>If you are a web developer you may have experienced that there are many situations that you need to access remote domains for data sources. For an example if you are building a weather mashup, you may like to connect to some weather forecasting services like  <a href="http://www.weather.gov">http://www.weather.gov</a> or <a href="http://weather.cnn.com/weather/forecast.jsp">http://weather.cnn.com/weather/forecast.jsp</a>. Mostly these services are very simple, so you can build these services from Javascript  itself. (FYI you can use the blog post, I wrote sometime back, <a href="http://www.dimuthu.org/blog/2008/12/11/calling-simple-web-services-from-javascript/">Calling Simple Web Services From Javascript</a>.) But browsers doesn&#8217;t make it this much straight forward.</p>
<p>For an example if you try running the following code, which basically do a simple AJAX call to an external domain,</p>
<pre class="javascript">    <span style="font-style: italic; color: #009900;">// some external domain</span>
    <span style="font-weight: bold; color: #003366;">var</span> url = <span style="color: #3366cc;">"http://test.dimuthu.org"</span>;

    <span style="font-style: italic; color: #009900;">// doing the ajax call</span>
    <span style="font-weight: bold; color: #003366;">var</span> req = <span style="font-weight: bold; color: #003366;">new</span> XMLHttpRequest<span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;

    req.<span style="color: #000066;">open</span><span style="color: #66cc66;">(</span><span style="color: #3366cc;">"GET"</span>, url, <span style="font-weight: bold; color: #003366;">true</span><span style="color: #66cc66;">)</span>;
    req.<span style="color: #006600;">onreadystatechange</span> = <span style="font-weight: bold; color: #003366;">function</span> <span style="color: #66cc66;">(</span>e<span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
        <span style="font-weight: bold; color: #000066;">if</span> <span style="color: #66cc66;">(</span>req.<span style="color: #006600;">readyState</span> == <span style="color: #cc0000;">4</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
            <span style="font-weight: bold; color: #000066;">if</span><span style="color: #66cc66;">(</span>req.<span style="color: #000066;">status</span> == <span style="color: #cc0000;">200</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
                <span style="color: #000066;">alert</span><span style="color: #66cc66;">(</span>req.<span style="color: #006600;">responseText</span><span style="color: #66cc66;">)</span>;
            <span style="color: #66cc66;">}</span>
        <span style="color: #66cc66;">}</span>
    <span style="color: #66cc66;">}</span>

    req.<span style="color: #006600;">send</span><span style="color: #66cc66;">(</span><span style="font-weight: bold; color: #003366;">null</span><span style="color: #66cc66;">)</span>;</pre>
<p>You will get a security exception from the Firefox (opera too gives a similar exception).</p>
<pre><span style="color: #ff0000;">uncaught exception: Access to restricted URI denied (NS_ERROR_DOM_BAD_URI)</span></pre>
<p>In order to avoid this, you have do some special work.</p>
<ol>
<li>You need to add the following code before doing any AJAX request to external domains, This will give the script special privileges to access any domain through XMLHttpRequest object.
<pre class="javascript">    <span style="font-weight: bold; color: #000066;">try</span> <span style="color: #66cc66;">{</span>
        netscape.<span style="color: #006600;">security</span>.<span style="color: #006600;">PrivilegeManager</span>.<span style="color: #006600;">enablePrivilege</span><span style="color: #66cc66;">(</span><span style="color: #3366cc;">"UniversalBrowserRead"</span><span style="color: #66cc66;">)</span>;
    <span style="color: #66cc66;">}</span> <span style="font-weight: bold; color: #000066;">catch</span> <span style="color: #66cc66;">(</span>e<span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
        <span style="color: #000066;">alert</span><span style="color: #66cc66;">(</span><span style="color: #3366cc;">"Permission UniversalBrowserRead denied."</span><span style="color: #66cc66;">)</span>;
    <span style="color: #66cc66;">}</span></pre>
</li>
<li> If your script always jump to the exception, you have to configure your browser to allow the above setting. You can do this by going to the &#8220;about:config&#8221; page in Firefox (Just type the &#8220;about:config&#8221; in the url field and hit Enter) where it shows a list of configurations, there you need to set &#8220;signed.applets.codebase_principal_support&#8221; field to &#8220;true&#8221;. By default this field is set to false in Firefox 3.0</li>
</ol>
<p>After you completed above 2 steps, the page will show you an warning message saying that it is asking more privileges, in which the client have to click the &#8220;allow&#8221; button to continue.</p>
<p>This procedure is not much difficult to setup, but still it will be really painful for an average user, so it is better you avoid this as much as possible in your code.</p>
<p>The main reason this special setup is arranged in Firefox (and most of the other browsers) is attackers can run malicious scripts in some page which you trust, (for an example from one of your email message) and send your private data to some other domain that you don&#8217;t know and don&#8217;t trust.</p>
<p>Apart from XMLHttpRequest another famous way of accessing different domains from a web page is using framesets or iframes. using this technique, You can show an external web page inside yours as it is one part of that.</p>
<p>Before Firefox 3.0 and IE 7.0 you were able to change that external page (appearance or the content) according to your need when it is shown in a frame or iframe. This was possible to do by manipulating the DOM of that external page. But with Firefox 3.0 and IE 7.0 it is impossible. That is you still you can show an external page inside your web page, but you can&#8217;t change anything of it even it shows inside your page. Because it doesn&#8217;t allow you to access the DOM of that external page. See this issue is discussed in details at here, <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=397828">https://bugzilla.mozilla.org/show_bug.cgi?id=397828</a></p>
<p>With this improvement, you can&#8217;t call the <a href="http://developer.mozilla.org/en/docs/DOM:window.document">window.document</a> if the page of the window is from external domain.</p>
<p>The reason to this limitation is apparent, if you ever thought of modifying external pages and put it in your web page, you will be able feel many security holes in there. You can show some web based email login page in one of iframe, and fool some users. If that web based email application is not changed by the iframe container, it won&#8217;t be a problem, but how it is changed to submit your username, password to the parent site by updating the submit event (onclick attribute) of the DOM of that external page.</p>
<p>In fact Firefox and most of the browsers are trying to protect your from all these security attacks by restricting lot of functionalities of the browsers. They are doing what they can do it in the client side, but you don&#8217;t know what exactly happens in the server side since it is always a black box. The all the restriction mentioned above (i.e. accessing remote services, changing and showing an external web page) can be done in very simple PHP or .NET code in server side. So it is right that you should use the right tools, but more important thing is you are aware of these attacks and you selectively browse web while avoiding them</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/12/22/security-considerations-in-firefox-when-accessing-different-domains/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Sending Encrypted Binary Messages With PHP Web Services</title>
		<link>http://www.dimuthu.org/blog/2008/12/14/sending-encrypted-binary-messages-with-php-web-services/</link>
		<comments>http://www.dimuthu.org/blog/2008/12/14/sending-encrypted-binary-messages-with-php-web-services/#comments</comments>
		<pubDate>Sun, 14 Dec 2008 17:51:04 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[wsf/php]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[Attachment]]></category>
		<category><![CDATA[base64]]></category>
		<category><![CDATA[encryption]]></category>
		<category><![CDATA[MIME]]></category>
		<category><![CDATA[mtom]]></category>
		<category><![CDATA[policy]]></category>
		<category><![CDATA[secuirty token]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=797</guid>
		<description><![CDATA[Web services has made the communication between heterogeneous environments (say PHP with .NET  or Java) a reality. It has defines standards for communicate not only with texts but also with binaries. And more importantly you can keep these communication confidential using encrypted messages according to your requirement. In this post, we will look at how [...]]]></description>
			<content:encoded><![CDATA[<p>Web services has made the communication between heterogeneous environments (say PHP with .NET  or Java) a reality. It has defines standards for communicate not only with texts but also with binaries. And more importantly you can keep these communication confidential using encrypted messages according to your requirement. In this post, we will look at how we can implement such a system with PHP in one side.</p>
<p>In web services we can send/receive binary messages in two basic forms.</p>
<ol>
<li>Setting the binary inside the SOAP message. &#8211; Binary should be converted to base64 to make sure the SOAP body contains only texts. Since base64 converted data span longer than the binary data, we call this form as non-optimized way of sending binaries.</li>
<li>Setting the binary outside the SOAP message &#8211; Binary would be sent as a MIME part in the message. And some element inside SOAP body keeps a reference to the binary using the MIME id. MTOM is a standard for referencing the MIME from inside the SOAP body. Since the binary is encoded, this will keep the message optimum with the binaries.</li>
</ol>
<p>In <a href="http://wso2.org/projects/wsf/php">WSF/PHP</a> you can use any of these methods as you prefer. Lets forget about the encryption for now. We will check how we can send binaries in both of the above mentioned forms.</p>
<pre class="php"><span style="font-style: italic; color: #808080;">// first the request xml. Note tht xop:Include element that is referring the attachment with the id "myid1".</span>
<span style="color: #0000ff;">$reqPayloadString</span> = &lt;&lt;&lt;XML
&lt;ns1:upload xmlns:ns1=<span style="color: #ff0000;">"http://wso2.org/wsfphp/samples/mtom"</span>&gt;
               &lt;ns1:fileName&gt;test.jpg&lt;/ns1:fileName&gt;
               &lt;ns1:image xmlmime:contentType=<span style="color: #ff0000;">"image/jpeg"</span> xmlns:xmlmime=<span style="color: #ff0000;">"http://www.w3.org/2004/06/xmlmime"</span>&gt;
                  &lt;xop:<span style="color: #b1b100;">Include</span> xmlns:xop=<span style="color: #ff0000;">"http://www.w3.org/2004/08/xop/include"</span> href=<span style="color: #ff0000;">"cid:myid1"</span>&gt;&lt;/xop:Include&gt;
               &lt;/ns1:image&gt;
&lt;/ns1:upload&gt;
XML;

try <span style="color: #66cc66;">{</span>
    <span style="color: #0000ff;">$f</span> = <a href="http://www.php.net/file_get_contents"><span style="color: #000066;">file_get_contents</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"my_binary_file.jpg"</span><span style="color: #66cc66;">)</span>;

    <span style="font-style: italic; color: #808080;">// here in the attachments option we define the binaries</span>
    <span style="font-style: italic; color: #808080;">// corresponding to the id defined in the above XML</span>
    <span style="color: #0000ff;">$reqMessage</span> = <span style="font-weight: bold; color: #000000;">new</span> WSMessage<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$reqPayloadString</span>,
                                <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"to"</span> =&gt; <span style="color: #ff0000;">"http://localhost/simple_upload_service.php"</span>,
                                      <span style="color: #ff0000;">"action"</span> =&gt; <span style="color: #ff0000;">"http://wso2.org/upload"</span>,
                                      <span style="color: #ff0000;">"attachments"</span> =&gt; <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"myid1"</span> =&gt; <span style="color: #0000ff;">$f</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

    <span style="font-style: italic; color: #808080;">// creating the WSClient</span>
    <span style="font-style: italic; color: #808080;">// here the option useMTOM will decide whether the</span>
    <span style="font-style: italic; color: #808080;">// attachment is set MTOM or base64</span>
    <span style="color: #0000ff;">$client</span> = <span style="font-weight: bold; color: #000000;">new</span> WSClient<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"useWSA"</span> =&gt; <span style="font-weight: bold; color: #000000;">TRUE</span>,
                                 <span style="color: #ff0000;">"useMTOM"</span> =&gt; <span style="font-weight: bold; color: #000000;">TRUE</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

    <span style="font-style: italic; color: #808080;">// sending the message and retrieving the response</span>
    <span style="color: #0000ff;">$resMessage</span> = <span style="color: #0000ff;">$client</span>-&gt;<span style="color: #006600;">request</span><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$reqMessage</span><span style="color: #66cc66;">)</span>;

    <a href="http://www.php.net/printf"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"Response = %s <span style="font-weight: bold; color: #000099;">\\</span>n"</span>, <span style="color: #0000ff;">$resMessage</span>-&gt;<span style="color: #006600;">str</span><span style="color: #66cc66;">)</span>;

<span style="color: #66cc66;">}</span> catch <span style="color: #66cc66;">(</span>Exception <span style="color: #0000ff;">$e</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>

    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$e</span> instanceof WSFault<span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
        <a href="http://www.php.net/printf"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"Soap Fault: %s<span style="font-weight: bold; color: #000099;">\\</span>n"</span>, <span style="color: #0000ff;">$e</span>-&gt;<span style="color: #006600;">Reason</span><span style="color: #66cc66;">)</span>;
    <span style="color: #66cc66;">}</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">{</span>
        <a href="http://www.php.net/printf"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"Message = %s<span style="font-weight: bold; color: #000099;">\\</span>n"</span>,<span style="color: #0000ff;">$e</span>-&gt;<span style="color: #006600;">getMessage</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;
    <span style="color: #66cc66;">}</span>
<span style="color: #66cc66;">}</span></pre>
<p>As mentioned in the inline comment we can choose the preferred form of sending binary using the &#8220;useMTOM&#8221; option. if it is true, the binary is set as a MTOM, (referencing from the body) or if it is set false, the binary will be set as a base64 binary within the SOAP body.<br />
To encrypt the message you only need to write few additional lines. First you define your policy that you need to encrypt this message using a WSPolicy object. Then the security token including the service public key and your private key. You need to give these two option as a constructor argument in WSClient. Here is that little additional code you need to write to add the encryption.</p>
<pre class="php">    <span style="font-style: italic; color: #808080;">// loading the keys</span>
    <span style="color: #0000ff;">$rec_cert</span> = ws_get_cert_from_file<span style="color: #66cc66;">(</span><span style="color: #ff0000;">"receiving_server.cert"</span><span style="color: #66cc66;">)</span>;
    <span style="color: #0000ff;">$pvt_key</span> = ws_get_key_from_file<span style="color: #66cc66;">(</span><span style="color: #ff0000;">"my_private_key.pem"</span><span style="color: #66cc66;">)</span>;

    <span style="font-style: italic; color: #808080;">// here we defines the policies and create WSPolicy object</span>
    <span style="color: #0000ff;">$sec_array</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"encrypt"</span> =&gt; <span style="font-weight: bold; color: #000000;">TRUE</span>,
                       <span style="color: #ff0000;">"algorithmSuite"</span> =&gt; <span style="color: #ff0000;">"Basic256Rsa15"</span>,
                       <span style="color: #ff0000;">"securityTokenReference"</span> =&gt; <span style="color: #ff0000;">"IssuerSerial"</span><span style="color: #66cc66;">)</span>;

    <span style="color: #0000ff;">$policy</span> = <span style="font-weight: bold; color: #000000;">new</span> WSPolicy<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"security"</span> =&gt; <span style="color: #0000ff;">$sec_array</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

    <span style="font-style: italic; color: #808080;">// defining Security Tokens</span>
    <span style="color: #0000ff;">$sec_token</span> = <span style="font-weight: bold; color: #000000;">new</span> WSSecurityToken<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"privateKey"</span> =&gt; <span style="color: #0000ff;">$pvt_key</span>,
                                           <span style="color: #ff0000;">"receiverCertificate"</span> =&gt; <span style="color: #0000ff;">$rec_cert</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

    <span style="font-style: italic; color: #808080;">// modifing WSClient with adding WSPolicy and WSSecurityToken object</span>
    <span style="color: #0000ff;">$client</span> = <span style="font-weight: bold; color: #000000;">new</span> WSClient<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"useWSA"</span> =&gt; <span style="font-weight: bold; color: #000000;">TRUE</span>,
                                 <span style="color: #ff0000;">"useMTOM"</span> =&gt; <span style="font-weight: bold; color: #000000;">TRUE</span>,
                                 <span style="color: #ff0000;">"policy"</span> =&gt; <span style="color: #0000ff;">$policy</span>,
                                 <span style="color: #ff0000;">"securityToken"</span> =&gt; <span style="color: #0000ff;">$sec_token</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;</pre>
<p>You can implement the receiving side of the message similar to the sending side that we just described above. The most important thing is it doesn&#8217;t need to be written in PHP. It can be a Java code or .NET code.If you already have web services that use encrypted binary messaging, the above php code can be use out of the box to communicate with it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/12/14/sending-encrypted-binary-messages-with-php-web-services/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>WS-SecurityPolicy With PHP</title>
		<link>http://www.dimuthu.org/blog/2008/11/19/ws-securitypolicy-with-php/</link>
		<comments>http://www.dimuthu.org/blog/2008/11/19/ws-securitypolicy-with-php/#comments</comments>
		<pubDate>Wed, 19 Nov 2008 16:20:43 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[DataServices]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[WSDL]]></category>
		<category><![CDATA[wsf/php]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xml schema]]></category>
		<category><![CDATA[policy]]></category>
		<category><![CDATA[ws-policy]]></category>
		<category><![CDATA[ws-security]]></category>
		<category><![CDATA[ws-security policy]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=647</guid>
		<description><![CDATA[WS-SecurityPolicy specification defines standards for defining security policies for your web service. WSF/PHP allows you to declare your security policies according to these standards. You can take one of following approaches to associate policies to your web service or client. PHP Array to represent your policies Policy file compliant with WS-Security Policy. Declaring policies inline [...]]]></description>
			<content:encoded><![CDATA[<p>WS-SecurityPolicy specification defines standards for defining security policies for your web service.<a href="http://wso2.org/projects/wsf/php"> WSF/PHP</a> allows you to declare your security policies according to these standards.</p>
<p>You can take one of following approaches to associate policies to your web service or client.</p>
<ul>
<li>PHP Array to represent your policies</li>
<li>Policy file compliant with WS-Security Policy.</li>
<li>Declaring policies inline with the WSDL.</li>
</ul>
<p><strong>Declaring Policies with a PHP Array</strong></p>
<p>This is a WSF/PHP specific API to declare policies for a web service. You don&#8217;t need to learn WS-Security Policy to write policies with this approach. You can set whether you want to use encryption, signing or usernameToken in a PHP array and create a WSPolicy object using it.</p>
<pre class="php"><span style="font-style: italic; color: #808080;">// here is the security array to declare your policies in simple manner</span><span style="color: #0000ff;">
$sec_array</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"encrypt"</span> =&gt; <span style="font-weight: bold; color: #000000;">TRUE</span>,
 <span style="color: #ff0000;">"algorithmSuite"</span> =&gt; <span style="color: #ff0000;">"Basic256Rsa15"</span>,
 <span style="color: #ff0000;">"securityTokenReference"</span> =&gt; <span style="color: #ff0000;">"IssuerSerial"</span><span style="color: #66cc66;">)</span>;

<span style="font-style: italic; color: #808080;">// creating WSPolicy instance using the policy array</span><span style="color: #0000ff;">
$policy</span> = <span style="font-weight: bold; color: #000000;">new</span> WSPolicy<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"security"</span>=&gt; <span style="color: #0000ff;">$sec_array</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;</pre>
<p>You can use this policy object to create a service along with a WSSecurityToken which contain the user parameters like the server private key and the client certificate.</p>
<pre class="php"><span style="color: #0000ff;">$sec_token</span> = <span style="font-weight: bold; color: #000000;">new</span> WSSecurityToken<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span>
 <span style="color: #ff0000;">"privateKey"</span> =&gt; <span style="color: #0000ff;">$server_pvt_key</span>,
 <span style="color: #ff0000;">"receiverCertificate"</span> =&gt; <span style="color: #0000ff;">$client_pub_key</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

<span style="color: #0000ff;">$svr</span> = <span style="font-weight: bold; color: #000000;">new</span> WSService<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"actions"</span> =&gt; <span style="color: #0000ff;">$actions</span>,
 <span style="color: #ff0000;">"operations"</span> =&gt; <span style="color: #0000ff;">$operations</span>,
 <span style="color: #ff0000;">"policy"</span> =&gt; <span style="color: #0000ff;">$policy</span>,
 <span style="color: #ff0000;">"securityToken"</span> =&gt; <span style="color: #0000ff;">$sec_token</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>; <span style="font-style: italic; color: #808080;">// here is the policy object you just created</span><span style="color: #0000ff;">

$svr</span>-&gt;<span style="color: #006600;">reply</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;</pre>
<p>You can invoke this service just by writing a simple web service client. There also you need to provide the policies declared in the service, so the client can build his request to validate with server policies. You will be using a similar WSPolicy object to set these policies at the client side too, as show in the below code segment.</p>
<pre class="php"> <span style="color: #0000ff;">$sec_token</span> = <span style="font-weight: bold; color: #000000;">new</span> WSSecurityToken<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span>
 <span style="color: #ff0000;">  "privateKey"</span> =&gt; <span style="color: #0000ff;">$pvt_key</span>,
 <span style="color: #ff0000;">  "receiverCertificate"</span> =&gt; <span style="color: #0000ff;">$rec_cert</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

 <span style="color: #0000ff;">$client</span> = <span style="font-weight: bold; color: #000000;">new</span> WSClient<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"useWSA"</span> =&gt; <span style="font-weight: bold; color: #000000;">TRUE</span>,
    <span style="color: #ff0000;">"policy"</span> =&gt; <span style="color: #0000ff;">$policy</span>, <span style="font-style: italic; color: #808080;">/* the policy object */</span>
    <span style="color: #ff0000;">"securityToken"</span> =&gt; <span style="color: #0000ff;">$sec_token</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

 <span style="color: #0000ff;">$resMessage</span> = <span style="color: #0000ff;">$client</span>-&gt;<span style="color: #006600;">request</span><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$reqMessage</span><span style="color: #66cc66;">)</span>;</pre>
<p><strong>Declaring Policies with a Policy File</strong></p>
<p>You can set your policies in the server or client side using a policy file compliant with WS-Security Policy specification. You have to take this approach if your policy requirements are too complicated, like you want to sign only some parts of the message or you want to encrypt some soap headers.</p>
<p>Similar to the above method, here too you will use the WSPolicy object to set your policies. But unlike the above where you give the policies as a PHP array , here you can just give the policy file as an argument to the WSPolicy constructor.</p>
<pre class="php"><span style="font-style: italic; color: #808080;">// creating the WSPolicy instance from a policy file</span><span style="color: #0000ff;">
$policy_xml</span> = <a href="http://www.php.net/file_get_contents"><span style="color: #000066;">file_get_contents</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"policy.xml"</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$policy</span> = <span style="font-weight: bold; color: #000000;">new</span> WSPolicy<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$policy_xml</span><span style="color: #66cc66;">)</span>;</pre>
<p><a href="http://labs.wso2.org/wsf/php/source_page_old.php?src=solutions%2Fsamples%2Fsecurity%2Fsigning%2Fpolicy_file_based%2Fpolicy.xml">Here </a>is an example of a complete policy file written according to the WS-Security Policy standards. And you can find a quick guide on WS-Security Policy from the article  <a href="http://wso2.org/library/3132">Understanding WS-Security Policy Language</a> written by <a href="http://nandana83.blogspot.com/">Nandana</a>, a key leader of Apache Rampart project.</p>
<p><strong>Declaring Policies inline in a WSDL</strong></p>
<p>We use WSDL to describe our web services. WSDL has the information about the service endpoint, the transport protocols (e.g. http), messaging protocols (e.g. SOAP) and the message schemas and many others about the service. You can attach your policies inside a WSDL.</p>
<p><a href="http://labs.wso2.org/wsf/php/source_page_old.php?src=solutions%2FCalendar%2FCalendar.wsdl">Here</a> is an example of a WSDL with inline policies. The difference in this approach is you can set your policies separately for each messages or each operations or each endpoints of your service. The following segment of a WSDL shows how you refer to different policies which are declared in the early part of the WSDL.</p>
<pre class="xml">     <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;wsdl:binding</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"CalendarSOAP12Binding"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"ns1:CalendarPortType"</span><span style="font-weight: bold; color: black;">&gt;
</span></span>       <span style="color: #009900;"><span style="font-style: italic; color: #808080;">&lt;!-- Endpoint policies are declared here.
          these are common to all messages transferring
          through this protocols (i.e. SOAP12, http)--&gt;</span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;wsp:PolicyReference</span> <span style="color: #000066;">URI</span>=<span style="color: #ff0000;">"#transport_binding_policy"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">  &lt;soap12:binding</span> <span style="color: #000066;">transport</span>=<span style="color: #ff0000;">"http://schemas.xmlsoap.org/soap/http"</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">"document"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;wsdl:operation</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"login"</span><span style="font-weight: bold; color: black;">&gt;
          </span></span> <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;soap12:operation</span> <span style="color: #000066;">soapAction</span>=<span style="color: #ff0000;">"urn:login"</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">"document"</span><span style="font-weight: bold; color: black;">/&gt;
          </span></span> <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;wsdl:input<span style="font-weight: bold; color: black;">&gt;
             </span></span></span> <span style="color: #009900;"><span style="font-style: italic; color: #808080;">&lt;!-- policy specific to the 'login' operation --&gt;</span></span>
              <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;wsp:PolicyReference</span> <span style="color: #000066;">URI</span>=<span style="color: #ff0000;">"#username_token_policy"</span><span style="font-weight: bold; color: black;">/&gt;
             </span></span> <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;soap12:body</span> <span style="color: #000066;">use</span>=<span style="color: #ff0000;">"literal"</span><span style="font-weight: bold; color: black;">/&gt;
          </span></span> <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/wsdl:input<span style="font-weight: bold; color: black;">&gt;</span></span></span>
           <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;wsdl:output<span style="font-weight: bold; color: black;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;soap12:body</span> <span style="color: #000066;">use</span>=<span style="color: #ff0000;">"literal"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
           <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/wsdl:output<span style="font-weight: bold; color: black;">&gt;
         </span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/wsdl:operation<span style="font-weight: bold; color: black;">&gt;
        </span></span></span> <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;wsdl:operation</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"register"</span><span style="font-weight: bold; color: black;">&gt;
           </span></span> <span style="color: #009900;"><span style="font-style: italic; color: #808080;">&lt;!-- no specific policies are set for the 'register' operation</span></span><span style="color: #009900;"><span style="font-style: italic; color: #808080;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">
          </span></span> <span style="color: #009900;"><span style="font-weight: bold; color: black;"> &lt;soap12:operation</span> <span style="color: #000066;">soapAction</span>=<span style="color: #ff0000;">"urn:register"</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">"document"</span><span style="font-weight: bold; color: black;">/&gt;
         </span></span> </span></span>  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;wsdl:input<span style="font-weight: bold; color: black;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;soap12:body</span> <span style="color: #000066;">use</span>=<span style="color: #ff0000;">"literal"</span><span style="font-weight: bold; color: black;">/&gt;
          </span></span>  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/wsdl:input<span style="font-weight: bold; color: black;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;wsdl:output<span style="font-weight: bold; color: black;">&gt;</span></span></span>
               <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;soap12:body</span> <span style="color: #000066;">use</span>=<span style="color: #ff0000;">"literal"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/wsdl:output<span style="font-weight: bold; color: black;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/wsdl:operation<span style="font-weight: bold; color: black;">&gt;</span></span></span>
           ....
       <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/wsdl:binding<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>This is the binding section of a WSDL where we bind messaging protocol and transport protocols with a service endpoint. Here we have &#8220;login&#8221; and &#8220;register&#8221; operations. Note that we are referring to &#8220;transport_binding_policy&#8221; from the parent level of each operation elements. That means these policies are common to all the operation in that binding. And inside the &#8220;login&#8221; operation we are referring to &#8220;username_token_policy&#8221;, so in order to invoke this operation, you have to send username token headers. And &#8220;register&#8221; doesn&#8217;t require any operation specific policies allowing users to register without any prior authentications.</p>
<p>You can select any of the above mentioned approach to define policies of your web service or to invoke a web service that support WS-Policy. If your policy requirements are simple, it will be easy to use the array based approach. If your policy requirements are complex or you have a good understanding of WS-Policy  and WS-Security Policy you can rely on the policy file based approach or defining policy inline with WSDL. And the former 2 methods will give you a nice separation of the logic code and security configurations. The selection is yours:)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/11/19/ws-securitypolicy-with-php/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Signing SOAP Headers In PHP Web Services</title>
		<link>http://www.dimuthu.org/blog/2008/11/18/signing-soap-headers-in-php-web-services/</link>
		<comments>http://www.dimuthu.org/blog/2008/11/18/signing-soap-headers-in-php-web-services/#comments</comments>
		<pubDate>Tue, 18 Nov 2008 16:27:13 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[wsf/php]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[message id]]></category>
		<category><![CDATA[sign]]></category>
		<category><![CDATA[signature]]></category>
		<category><![CDATA[ws-addressing]]></category>
		<category><![CDATA[ws-security]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=638</guid>
		<description><![CDATA[Non-Repudiation and Integrity are two main security issues addressed by signing a message. If you are writing a web service or a service consumer in PHP you can use the WSF/PHP toolkit to sign messages. Here is how you can sign a SOAP request message. // loading the keys $my_cert = ws_get_cert_from_file("client_certificate.cert"); $my_key = ws_get_key_from_file("client_private_key.pem"); [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Non-repudiation">Non-Repudiation</a> and Integrity are two main security issues addressed by signing a message. If you are writing a web service or a service consumer in PHP you can use the <a href="http://wso2.org/projects/wsf/php">WSF/PHP toolkit</a> to sign messages.</p>
<p>Here is how you can sign a SOAP request message.</p>
<pre class="php"><span style="font-style: italic; color: #808080;">// loading the keys</span>
<span style="color: #0000ff;">$my_cert</span> = ws_get_cert_from_file<span style="color: #66cc66;">(</span><span style="color: #ff0000;">"client_certificate.cert"</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$my_key</span> = ws_get_key_from_file<span style="color: #66cc66;">(</span><span style="color: #ff0000;">"client_private_key.pem"</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$rec_cert</span> = ws_get_cert_from_file<span style="color: #66cc66;">(</span><span style="color: #ff0000;">"server_certificate.cert"</span><span style="color: #66cc66;">)</span>;

<span style="font-style: italic; color: #808080;">// preparing the policy array</span>
<span style="color: #0000ff;">$sec_array</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"sign"</span>=&gt;TRUE,
                   <span style="color: #ff0000;">"algorithmSuite"</span> =&gt; <span style="color: #ff0000;">"Basic256Rsa15"</span>,
                   <span style="color: #ff0000;">"securityTokenReference"</span> =&gt; <span style="color: #ff0000;">"IssuerSerial"</span><span style="color: #66cc66;">)</span>;

<span style="color: #0000ff;">$policy</span> = <span style="font-weight: bold; color: #000000;">new</span> WSPolicy<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"security"</span>=&gt;<span style="color: #0000ff;">$sec_array</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

<span style="font-style: italic; color: #808080;">// preparing the security_token</span>
<span style="color: #0000ff;">$sec_token</span> = <span style="font-weight: bold; color: #000000;">new</span> WSSecurityToken<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"privateKey"</span> =&gt; <span style="color: #0000ff;">$my_key</span>,
                                       <span style="color: #ff0000;">"certificate"</span> =&gt; <span style="color: #0000ff;">$my_cert</span>,
                                       <span style="color: #ff0000;">"receiverCertificate"</span> =&gt; <span style="color: #0000ff;">$rec_cert</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

<span style="font-style: italic; color: #808080;">// create the client using the policy and sec token + option ws-addressing</span>
<span style="color: #0000ff;">$client</span> = <span style="font-weight: bold; color: #000000;">new</span> WSClient<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"useWSA"</span> =&gt; <span style="font-weight: bold; color: #000000;">TRUE</span>,
                              <span style="color: #ff0000;">"policy"</span> =&gt; <span style="color: #0000ff;">$policy</span>,
                              <span style="color: #ff0000;">"securityToken"</span> =&gt; <span style="color: #0000ff;">$sec_token</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

<span style="font-style: italic; color: #808080;">// invoke the request				</span>
<span style="color: #0000ff;">$resMessage</span> = <span style="color: #0000ff;">$client</span>-&gt;<span style="color: #006600;">request</span><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$reqMessage</span><span style="color: #66cc66;">)</span>;</pre>
<p>Although I&#8217;ve not shown in the above code, you should always wrap the requesting code inside a try/catch block specially when there are security tokens in your message. Because whenever security test is failed the server will send a SOAP fault, and the client is expected to handle that.</p>
<p>Similarity you can write a service that accept only signing messages with the same policy and security tokens. But this time your security token contains the private key and the certificate of the server. And for service you create WSService instance (similarity to  WSClient) and feed these options.</p>
<pre class="php"><span style="font-style: italic; color: #808080;">// loading the keys and create the security token</span>
<span style="color: #0000ff;">$cert</span> = ws_get_cert_from_file<span style="color: #66cc66;">(</span><span style="color: #ff0000;">"server_certificate.cert"</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$pvt_key</span> = ws_get_key_from_file<span style="color: #66cc66;">(</span><span style="color: #ff0000;">"server_private_key.pem"</span><span style="color: #66cc66;">)</span>

<span style="color: #0000ff;">$sec_token</span> = <span style="font-weight: bold; color: #000000;">new</span> WSSecurityToken<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"privateKey"</span> =&gt; <span style="color: #0000ff;">$pvt_key</span>,
                                       <span style="color: #ff0000;">"certificate"</span> =&gt; <span style="color: #0000ff;">$cert</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

<span style="font-style: italic; color: #808080;">// policy is declared similar to earlier example</span>

<span style="color: #0000ff;">$sec_array</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"sign"</span> =&gt; <span style="font-weight: bold; color: #000000;">TRUE</span>,
                    <span style="color: #ff0000;">"algorithmSuite"</span> =&gt; <span style="color: #ff0000;">"Basic256Rsa15"</span>,
                    <span style="color: #ff0000;">"securityTokenReference"</span> =&gt; <span style="color: #ff0000;">"KeyIdentifier"</span><span style="color: #66cc66;">)</span>;

<span style="color: #0000ff;">$policy</span> = <span style="font-weight: bold; color: #000000;">new</span> WSPolicy<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"security"</span>=&gt;<span style="color: #0000ff;">$sec_array</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

<span style="font-style: italic; color: #808080;">// create a WSService instance and feed the options</span>
<span style="color: #0000ff;">$svr</span> = <span style="font-weight: bold; color: #000000;">new</span> WSService<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"operations"</span> =&gt; <span style="color: #0000ff;">$operations</span>,
                           <span style="color: #ff0000;">"actions"</span> =&gt; <span style="color: #0000ff;">$actions</span>,
                           <span style="color: #ff0000;">"policy"</span> =&gt; <span style="color: #0000ff;">$policy</span>,
                           <span style="color: #ff0000;">"securityToken"</span> =&gt; <span style="color: #0000ff;">$sec_token</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

<span style="color: #0000ff;">$svr</span>-&gt;<span style="color: #006600;">reply</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;</pre>
<p>With this code you will only be able to sign the payload of a SOAP request. Payload is what stays inside the soap body part which contain the actual data of your business. So in most of the case, what you want is integrity of your business data, so this will satisfy your need.</p>
<p>But there can be situation you want to sign SOAP headers which are outside the SOAP body. SOAP headers contain meta data of the message. For an example when you sign a message, the SOAP headers could carry the signature information and the certificates.</p>
<p>One example of the use of signing SOAP headers is replay detection. (I&#8217;ve written about<a href="http://www.dimuthu.org/blog/2008/11/17/detect-replay-attacks-in-to-your-php-web-service/"> replay detection on a early blog</a>). in SOAP, replays can be detected using &#8216;message id&#8217;s which are transferred through the WS-addressing SOAP headers. But interceptors can change the message ids and send it as a unique message which will eventually cause to fail the replay detection. But if you sign the &#8216;message id&#8217; (i.e. WS-Addressing headers) the intruders will not be able to do the trick since they can not recreate the signature for the changed content.</p>
<p>Anyway you can not set your requirement to sign the soap headers, just using a PHP array as it has done in the above examples. Rather you have to declare it using a policy file compliant to WS-SecurityPolicy specification. I.e instead of using a PHP array to create a WSPolicy instance like this,</p>
<pre class="php"><span style="color: #0000ff;">$sec_array</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"sign"</span>=&gt;TRUE,
                    <span style="color: #ff0000;">"algorithmSuite"</span> =&gt; <span style="color: #ff0000;">"Basic256Rsa15"</span>,
                    <span style="color: #ff0000;">"securityTokenReference"</span> =&gt; <span style="color: #ff0000;">"IssuerSerial"</span><span style="color: #66cc66;">)</span>;

<span style="color: #0000ff;">$policy</span> = <span style="font-weight: bold; color: #000000;">new</span> WSPolicy<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"security"</span>=&gt;<span style="color: #0000ff;">$sec_array</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;</pre>
<p>You can create the WSPolicy instance directly using the policy file,</p>
<pre class="php"><span style="color: #0000ff;">$policy_xml</span> = <a href="http://www.php.net/file_get_contents"><span style="color: #000066;">file_get_contents</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"policy.xml"</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$policy</span> = <span style="font-weight: bold; color: #000000;">new</span> WSPolicy<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$policy_xml</span><span style="color: #66cc66;">)</span>;</pre>
<p>And inside the policy file, you can declare your requirements, policies. Here is how I say I want to sign all my WS-Addressing headers.</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;sp:SignedParts</span> <span style="color: #000066;">xmlns:sp</span>=<span style="color: #ff0000;">"http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;sp:Header</span> <span style="color: #000066;">Namespace</span>=<span style="color: #ff0000;">"http://www.w3.org/2005/08/addressing"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/sp:SignedParts<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>Note that here the namespace &#8220;http://www.w3.org/2005/08/addressing&#8221; is the namespace used in the Addressing headers. So I instruct to sign headers containing that namespace in order to sign the &#8216;message id&#8217; inside the WS-Addressing headers. You can visit a comlete policy file with the declarations of signed part from <a href="http://labs.wso2.org/wsf/php/source_page_old.php?src=solutions%2Fsamples%2Fsecurity%2Fsigning%2Fpolicy_file_based%2Fpolicy.xml">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/11/18/signing-soap-headers-in-php-web-services/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Detect Replay Attacks In to Your PHP Web Service</title>
		<link>http://www.dimuthu.org/blog/2008/11/17/detect-replay-attacks-in-to-your-php-web-service/</link>
		<comments>http://www.dimuthu.org/blog/2008/11/17/detect-replay-attacks-in-to-your-php-web-service/#comments</comments>
		<pubDate>Mon, 17 Nov 2008 17:47:11 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[wsf/php]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[replay detect]]></category>
		<category><![CDATA[replay detection]]></category>
		<category><![CDATA[ws-security]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=631</guid>
		<description><![CDATA[Replay attack is a common kind of attack, the hackers are using to break the security of a web service. If you can intercept one soap message while it is transferring through the wire, you can replay that message to the server again and again. Since the original message may have already encrypted, signed and [...]]]></description>
			<content:encoded><![CDATA[<p>Replay attack is a common kind of attack, the hackers are using to break the security of a web service.</p>
<p>If you can intercept one soap message while it is transferring through the wire, you can replay that message to the server again and again. Since the original message may have already encrypted, signed and contain valid authentication credentials the replaying messages will also be able to pass all the security tests and fool the server and do enormous damages to the business. For an example think of replying a soap message that a client is conducting a payment with a e-commerce service. The service may tend to charge the client multiple times for each request which break the integrity of the business.</p>
<p>So it is no doubt when ever you thinking of designing an enterprise web service application, you should give attention to &#8216;Replay Detection&#8217; more seriously.</p>
<p>One solution is you can handle the replay detection inside the business logic itself. If you do this for the above mentioned e-commerce like services, you will keep all the session ids and make sure only one payment is possible for one session. But this may need some really careful design of the application logic.</p>
<p>The other solution is to let your web service framework to handle the &#8216;Replay Detection&#8217;. That will clearly separate the security aspects from the business aspects of your service. And it will give you more flexibility in configuring your security requirements. And the other advantage is it will detect &#8216;Replay Attacks&#8217; well before hitting the business logic, making the web service perform well.</p>
<p><a href="http://wso2.org/projects/wsf/php">WSF/PHP </a>allows you to detect replay attacks using WS-Addressing and WS-Username token headers. WS-Addressing headers contains a message id which can be considered as unique to a soap message and ws-security headers contains created time of the message which can be used to calculate the age of the message and derive its validity.</p>
<p>WSF/PHP provide web service developer a callback with the &#8216;message id&#8217; and the &#8216;message created time&#8217;  per each message. In the callback you can store this message id and created time in a database, and check them against all the incoming soap messages. If it found duplicate entries, you can consider it as a replay attack.</p>
<p>Here is an example draft of the above scenario written using WSF/PHP.</p>
<pre class="php"><span style="font-style: italic; color: #808080;">/* replay detection callback */</span>
<span style="font-weight: bold; color: #000000;">function</span> replay_detect_callback<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$msg_id</span>, <span style="color: #0000ff;">$time_created</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
    <span style="font-style: italic; color: #808080;">/* Here is the pseudo code of the logic

    query for the $msg_id and $time_created for the database.
    if already exist
      return FALSE;
    else
      Insert message id and time created to the database
    return TRUE */</span>
<span style="color: #66cc66;">}</span>

<span style="color: #0000ff;">$security_token</span> = <span style="font-weight: bold; color: #000000;">new</span> WSSecurityToken<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="font-style: italic; color: #808080;"> </span>
                      <span style="color: #ff0000;">"replayDetectionCallback"</span> =&gt; <span style="color: #ff0000;">"replay_detect_callback"</span>,
                      <span style="color: #ff0000;">"enableReplayDetect"</span> =&gt; <span style="font-weight: bold; color: #000000;">TRUE</span>,<span style="color: #66cc66;">
                      </span><span style="font-style: italic; color: #808080;">/* Other tokens */</span><span style="color: #66cc66;"> </span><span><span style="color: #66cc66;">)</span></span><span style="color: #66cc66;">)</span>;

<span style="color: #0000ff;">$svr</span> = <span style="font-weight: bold; color: #000000;">new</span> WSService<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"securityToken"</span> =&gt; <span style="color: #0000ff;">$security_token</span>,<span style="color: #66cc66;">
                      </span><span style="font-style: italic; color: #808080;">/* Other options*/</span><span style="color: #66cc66;"> </span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

<span style="color: #0000ff;">$svr</span>-&gt;<span style="color: #006600;">reply</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span></pre>
<p>So if you use this code in the service you can happily detect any replays of an old message and avoid possible damages. But remember this security check will work only if the replaying message contain the same message id and the created time as the original one. It is possible that an intruder not only intercept the message, but also change these fields and replay it in to your server. Such replays will not be detected from this code.</p>
<p>The solution is to sign each SOAP requests. If a client sign a message with his private key, the server can confirm that the message is not altered while it is on the wire. So if intruder replay a signed message, either it will fail the replay detection test (if it is replaying without changing the message id and created time) or it will fail validating the signature (if the message id and the created time is altered).</p>
<p>So if you implement a replay detection test with a signature test, you can eliminate all the replay attacks to your service (at least theoretically <img src='http://www.dimuthu.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/11/17/detect-replay-attacks-in-to-your-php-web-service/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>WSF/PHP Samples Explained</title>
		<link>http://www.dimuthu.org/blog/2008/11/07/wsfphp-samples-explained/</link>
		<comments>http://www.dimuthu.org/blog/2008/11/07/wsfphp-samples-explained/#comments</comments>
		<pubDate>Fri, 07 Nov 2008 12:20:02 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[DataServices]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[WSDL]]></category>
		<category><![CDATA[wsf/php]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[beginners]]></category>
		<category><![CDATA[code first]]></category>
		<category><![CDATA[contract first]]></category>
		<category><![CDATA[data services]]></category>
		<category><![CDATA[mtom]]></category>
		<category><![CDATA[reliable messaging]]></category>
		<category><![CDATA[samples]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=589</guid>
		<description><![CDATA[Here is a simple categorization of the WSF/PHP samples. You can access all the wsf/php samples from http://labs.wso2.org/wsf/php/solutions/samples/index.html. Sample Category Example Client Source Code Example Service Source Code Online Demo Beginners echo_client.php echo_service.php Demo REST echo_client_rest.php echo_service_with_rest.php Demo WSDL Mode (Contract First) wsdl_11_client.php wsdl_11_service.php Demo WSDL Generation (Code First) doclit_client.php doclit_service.php Demo MTOM Attachments mtom_download_client.php [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a simple categorization of the <a href="http://wso2.org/projects/wsf/php">WSF/PHP </a>samples. You can access all the wsf/php samples from <a href="http://labs.wso2.org/wsf/php/solutions/samples/index.html">http://labs.wso2.org/wsf/php/solutions/samples/index.html</a>.</p>
<table border="1">
<tbody>
<tr style="background:#dddddd">
<td>Sample Category</td>
<td>Example Client Source Code</td>
<td>Example Service Source Code</td>
<td>Online Demo</td>
</tr>
<tr>
<td>Beginners</td>
<td><a href="http://labs.wso2.org/wsf/php/source_page_old.php?src=solutions%2Fsamples%2Fecho_client.php">echo_client.php</a></td>
<td><a href="http://labs.wso2.org/wsf/php/source_page_old.php?src=solutions%2Fsamples%2Fecho_service.php">echo_service.php</a></td>
<td><a href="http://labs.wso2.org/wsf/php/solutions/samples/echo_client.php">Demo</a></td>
</tr>
<tr>
<td>REST</td>
<td><a href="http://labs.wso2.org/wsf/php/source_page_old.php?src=solutions%2Fsamples%2Fecho_client_rest.php">echo_client_rest.php</a></td>
<td><a href="http://labs.wso2.org/wsf/php/source_page_old.php?src=solutions%2Fsamples%2Fecho_service_with_rest.php">echo_service_with_rest.php</a></td>
<td><a href="http://labs.wso2.org/wsf/php/solutions/samples/echo_client_rest.php">Demo</a></td>
</tr>
<tr>
<td>WSDL Mode (Contract First)</td>
<td><a href="http://labs.wso2.org/wsf/php/source_page_old.php?src=solutions%2Fsamples%2Fwsdl_mode%2Fwsdl_11_client.php">wsdl_11_client.php</a></td>
<td><a href="http://labs.wso2.org/wsf/php/source_page_old.php?src=solutions%2Fsamples%2Fwsdl_mode%2Fwsdl_11_service.php">wsdl_11_service.php</a></td>
<td><a href="http://labs.wso2.org/wsf/php/solutions/samples/wsdl_mode/wsdl_11_client.php">Demo</a></td>
</tr>
<tr>
<td>WSDL Generation (Code First)</td>
<td><a href="http://labs.wso2.org/wsf/php/source_page_old.php?src=solutions%2Fsamples%2Fwsdl_generation%2Fdoclit_client.php">doclit_client.php</a></td>
<td><a href="http://labs.wso2.org/wsf/php/source_page_old.php?src=solutions%2Fsamples%2Fwsdl_generation%2Fdoclit_service.php">doclit_service.php</a></td>
<td><a href="http://labs.wso2.org/wsf/php/solutions/samples/wsdl_generation/doclit_client.php">Demo</a></td>
</tr>
<tr>
<td>MTOM Attachments</td>
<td><a href="http://labs.wso2.org/wsf/php/source_page_old.php?src=solutions%2Fsamples%2Fmtom%2Fmtom_download_client.php">mtom_download_client.php</a></td>
<td><a href="http://labs.wso2.org/wsf/php/source_page_old.php?src=solutions%2Fsamples%2Fmtom%2Fmtom_download_service.php">mtom_download_service.php</a></td>
<td><a href="http://labs.wso2.org/wsf/php/solutions/samples/mtom/mtom_download_client.php">Demo</a></td>
</tr>
<tr>
<td>Security</td>
<td><a href="http://labs.wso2.org/wsf/php/source_page_old.php?src=solutions%2Fsamples%2Fsecurity%2Fencryption%2Fclient.php">encryption_client.php</a></td>
<td><a href="http://labs.wso2.org/wsf/php/source_page_old.php?src=solutions%2Fsamples%2Fsecurity%2Fencryption%2Fservice.php">encryption_service.php</a></td>
<td><a href="http://labs.wso2.org/wsf/php/solutions/samples/security/encryption/client.php">Demo</a></td>
</tr>
<tr>
<td>Reliable Messaging</td>
<td><a href="http://labs.wso2.org/wsf/php/source_page_old.php?src=solutions%2Fsamples%2Freliable%2Fecho_client_rm.php">echo_client_rm.php</a></td>
<td><a href="http://labs.wso2.org/wsf/php/source_page_old.php?src=solutions%2Fsamples%2Freliable%2Fecho_service_rm.php">echo_service_rm.php</a></td>
<td><a href="http://labs.wso2.org/wsf/php/solutions/samples/reliable/echo_client_rm.php">Demo</a></td>
</tr>
<tr>
<td>Data Services</td>
<td><a href="http://labs.wso2.org/wsf/php/source_page_old.php?src=solutions%2Fsamples%2FDataServices%2FCustomerDetailsClient.php">CustomerDetailsClient.php</a><a></a></td>
<td><a href="http://labs.wso2.org/wsf/php/source_page_old.php?src=solutions%2Fsamples%2FDataServices%2FCustomerDetailsService.php">CustomerDetailsService.php</a><a></a></td>
<td><a href="http://labs.wso2.org/wsf/php/solutions/samples/DataServices/CustomerDetailsClient.php">Demo</a></td>
</tr>
</tbody>
</table>
<p>If you have downloaded the  <a href="http://wso2.org/projects/wsf/php">WSF/PHP binaries or souce code</a> package you can find all these samples, inside the &#8216;samples&#8217; directory</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/11/07/wsfphp-samples-explained/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Demo on Providing PHP Web Service with Username Token</title>
		<link>http://www.dimuthu.org/blog/2008/10/16/demo-on-providing-php-web-service-with-username-token/</link>
		<comments>http://www.dimuthu.org/blog/2008/10/16/demo-on-providing-php-web-service-with-username-token/#comments</comments>
		<pubDate>Thu, 16 Oct 2008 17:43:45 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[security]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[WSDL]]></category>
		<category><![CDATA[wsf/php]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[Calendar]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[policies]]></category>
		<category><![CDATA[wsdl mode]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=468</guid>
		<description><![CDATA[WSF/PHP Demo Site contains number of applications that demonstrate the different features of WSO2 WSF/PHP in practice. Calendar Service is one of such application. It demonstrate the use of WSDL Mode for a service with different policies for different operations + the use of Username tokens. You can view the source code of the Calendar [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://labs.wso2.org/wsf/php">WSF/PHP Demo Site</a> contains number of applications that demonstrate the different features of <a href="http://wso2.org/projects/wsf/php">WSO2 WSF/PHP</a> in practice. <a href="http://labs.wso2.org/wsf/php/resource_view.php?url=Calendar">Calendar Service</a> is one of such application. It demonstrate the use of WSDL Mode for a service with different policies for different operations + the use of Username tokens.</p>
<p>You can view the source code of the Calendar Service from <a href="http://labs.wso2.org/wsf/php/source_page_frame.php?src=Calendar">here</a>.</p>
<p>The username token is provided as an arguments to the WSService constructor at the end of <a href="http://labs.wso2.org/wsf/php/source_page_old.php?src=solutions%2FCalendar%2FCalendarService.php&amp;aschild=1">the service script</a>.</p>
<pre class="php"><span style="font-style: italic; color: #808080;">// our security token</span>
<span style="color: #0000ff;">$security_token</span> = <span style="font-weight: bold; color: #000000;">new</span> WSSecurityToken<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"passwordCallback"</span> =&gt; <span style="color: #ff0000;">"get_password"</span>,
                                      <span style="color: #ff0000;">"passwordType"</span> =&gt; <span style="color: #ff0000;">"plain"</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;
<span style="font-style: italic; color: #808080;">// create service in WSDL mode</span>
<span style="color: #0000ff;">$service</span> = <span style="font-weight: bold; color: #000000;">new</span> WSService<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a> <span style="color: #66cc66;">(</span><span style="color: #ff0000;">"wsdl"</span> =&gt;<span style="color: #ff0000;">"Calendar.wsdl"</span>,
        <span style="color: #ff0000;">"actions"</span> =&gt; <span style="color: #0000ff;">$actions</span>,
        <span style="color: #ff0000;">"classmap"</span> =&gt; <span style="color: #0000ff;">$class_map</span>,
        <span style="color: #ff0000;">"securityToken"</span> =&gt; <span style="color: #0000ff;">$security_token</span>,
        <span style="color: #ff0000;">"operations"</span> =&gt; <span style="color: #0000ff;">$operations</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

<span style="font-style: italic; color: #808080;">// process client requests and reply </span>
<span style="color: #0000ff;">$service</span>-&gt;<span style="color: #006600;">reply</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;</pre>
<p>We use a callback function (&#8220;get_password&#8221;) to validate the user and give that function name to the securityToken object constructor. Inside that callback function, we retrieve the password for the user from a database call. Here is the code inside the callback function that is again extracted out from the <a href="http://labs.wso2.org/wsf/php/source_page_old.php?src=solutions%2FCalendar%2FCalendarService.php">calendar service</a>.</p>
<pre class="php"><span style="font-style: italic; color: #808080;">/**
 * call back function.
 * verify the validity of user enterd password with
 * the actual password which is kept in the database.
 */</span>
<span style="color: #0000ff;">$current_username</span> = <span style="color: #ff0000;">""</span>;
<span style="font-weight: bold; color: #000000;">function</span> get_password<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$username</span><span style="color: #66cc66;">)</span>
<span style="color: #66cc66;">{</span>

    <span style="color: #0000ff;">$dbhost</span> = DB_HOST;
    <span style="color: #0000ff;">$dbname</span> = DB_NAME;
    <span style="color: #0000ff;">$dbuname</span> = DB_USERNAME;
    <span style="color: #0000ff;">$dbpass</span> = DB_PASSWORD;
    <span style="color: #0000ff;">$link</span>=<a href="http://www.php.net/mysql_connect"><span style="color: #000066;">mysql_connect</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$dbhost</span>,  <span style="color: #0000ff;">$dbuname</span>,  <span style="color: #0000ff;">$dbpass</span><span style="color: #66cc66;">)</span>;
    <a href="http://www.php.net/mysql_select_db"><span style="color: #000066;">mysql_select_db</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$dbname</span>, <span style="color: #0000ff;">$link</span><span style="color: #66cc66;">)</span>;

    <span style="color: #0000ff;">$sql</span>=<span style="color: #ff0000;">"SELECT password FROM `customer_details` WHERE `user_name` = '"</span>.<span style="color: #0000ff;">$username</span>.<span style="color: #ff0000;">"'"</span>;
    <span style="color: #0000ff;">$result</span>=<a href="http://www.php.net/mysql_query"><span style="color: #000066;">mysql_query</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$sql</span>,<span style="color: #0000ff;">$link</span><span style="color: #66cc66;">)</span>;
    <span style="color: #0000ff;">$password</span>=<a href="http://www.php.net/mysql_fetch_array"><span style="color: #000066;">mysql_fetch_array</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$result</span>, MYSQL_NUM<span style="color: #66cc66;">)</span>;

    <a href="http://www.php.net/global"><span style="color: #000066;">global</span></a> <span style="color: #0000ff;">$current_username</span>;
    <span style="color: #b1b100;">if</span><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$password</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
         <span style="color: #0000ff;">$current_username</span> = <span style="color: #0000ff;">$username</span>;
         <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$password</span><span style="color: #66cc66;">[</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">]</span>;
    <span style="color: #66cc66;">}</span>
    <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">{</span>
         <span style="color: #0000ff;">$current_username</span> = <span style="color: #ff0000;">""</span>;
         <span style="color: #b1b100;">return</span> <span style="font-weight: bold; color: #000000;">NULL</span>;
    <span style="color: #66cc66;">}</span>
<span style="color: #66cc66;">}</span></pre>
<p>So for all the operations which require authentication like login, getEvents, deleteEvents and addEvent, the WSF/PHP engine validate the user before invoking the operation. If the authentication fails, the engine will send a SOAP fault with the fault details. But in this service there is a operation which doesn&#8217;t require authentication. That is the &#8216;register&#8217; operation. Because until the registration complete you can&#8217;t have a username password, so we should not authenticate that &#8216;register&#8217; operation. So we need to provide a different policy for the &#8216;register&#8217; operation.</p>
<p>The policies for each of the operation is declared in the <a href="http://labs.wso2.org/wsf/php/source_page_old.php?src=solutions%2FCalendar%2FCalendar.wsdl&amp;aschild=1">Calender.wsdl</a> itself.  If you look at the WSDL you can see each of the policies required by the operations are declared inside the policy elements as mentioned in WS-Policy Specification. And each of the operation refers the corresponding policy element from the binding section of the WSDL.</p>
<p>You can see how it is done for login (which requires authentication) and the register (which doesn&#8217;t requires authentication) from the code below.</p>
<pre class="xml">        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;wsdl:operation</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"login"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;soap12:operation</span> <span style="color: #000066;">soapAction</span>=<span style="color: #ff0000;">"urn:login"</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">"document"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;wsdl:input<span style="font-weight: bold; color: black;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;wsp:PolicyReference</span> <span style="color: #000066;">URI</span>=<span style="color: #ff0000;">"#username_token_policy"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
                <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;soap12:body</span> <span style="color: #000066;">use</span>=<span style="color: #ff0000;">"literal"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/wsdl:input<span style="font-weight: bold; color: black;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;wsdl:output<span style="font-weight: bold; color: black;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;soap12:body</span> <span style="color: #000066;">use</span>=<span style="color: #ff0000;">"literal"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/wsdl:output<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/wsdl:operation<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;wsdl:operation</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"register"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;soap12:operation</span> <span style="color: #000066;">soapAction</span>=<span style="color: #ff0000;">"urn:register"</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">"document"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;wsdl:input<span style="font-weight: bold; color: black;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;soap12:body</span> <span style="color: #000066;">use</span>=<span style="color: #ff0000;">"literal"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/wsdl:input<span style="font-weight: bold; color: black;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;wsdl:output<span style="font-weight: bold; color: black;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;soap12:body</span> <span style="color: #000066;">use</span>=<span style="color: #ff0000;">"literal"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/wsdl:output<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/wsdl:operation<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/10/16/demo-on-providing-php-web-service-with-username-token/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Test your SSL SOAP Client with an Online Service</title>
		<link>http://www.dimuthu.org/blog/2008/10/07/test-your-ssl-soap-client-with-an-online-service/</link>
		<comments>http://www.dimuthu.org/blog/2008/10/07/test-your-ssl-soap-client-with-an-online-service/#comments</comments>
		<pubDate>Tue, 07 Oct 2008 17:48:00 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[security]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[wsf/php]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[CACert]]></category>
		<category><![CDATA[demo site]]></category>
		<category><![CDATA[endpoint]]></category>
		<category><![CDATA[HTTPS]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[public]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=451</guid>
		<description><![CDATA[WSF/PHP Demo Site services can be accessed via https (Secured HTTP)  transport. For an example you can access the echo service via https from https://2ec2.wso2.org/samples/echo_service.php endpoint. This can be used to identify whether you WSF/PHP instance is built with SSL enabled. (Note that from WSF/PHP 2.0.0 onwards, You have SSL enabled by default both in [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://labs.wso2.org/wsf/php">WSF/PHP Demo Site</a> services can be accessed via https (Secured HTTP)  transport. For an example you can access the echo service via https from <a href="https://2ec2.wso2.org/samples/echo_service.php">https://2ec2.wso2.org/samples/echo_service.php</a> endpoint. This can be used to identify whether you WSF/PHP instance is built with SSL enabled. (Note that from WSF/PHP 2.0.0 onwards, You have SSL enabled by default both in Linux and Windows, so for newer releases you don&#8217;t need explicitly set that when compiling).</p>
<p>Here is a sample client I used to connect to the https service. The only thing new from the traditional echo client is it has specified &#8220;CACert&#8221; option and the URL is pointing to a https service.</p>
<pre class="php"><span style="font-weight: bold; color: #000000;">&lt;?php</span>
<span style="color: #0000ff;">$requestPayloadString</span> = &lt;&lt;&lt;XML
&lt;ns1:echoString xmlns:ns1=<span style="color: #ff0000;">"http://wso2.org/wsfphp/samples"</span>&gt;&lt;text&gt;Hello World!&lt;/text&gt;&lt;/ns1:echoString&gt;
XML;

try <span style="color: #66cc66;">{</span>

    <span style="color: #0000ff;">$client</span> = <span style="font-weight: bold; color: #000000;">new</span> WSClient<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span> <span style="color: #ff0000;">"to"</span> =&gt; <span style="color: #ff0000;">"https://2ec2.wso2.org/samples/echo_service.php"</span>,
                                <span style="color: #ff0000;">"CACert"</span> =&gt; <span style="color: #ff0000;">"./resources/cacert.pem"</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

    <span style="color: #0000ff;">$responseMessage</span> = <span style="color: #0000ff;">$client</span>-&gt;<span style="color: #006600;">request</span><span style="color: #66cc66;">(</span> <span style="color: #0000ff;">$requestPayloadString</span> <span style="color: #66cc66;">)</span>;

    <a href="http://www.php.net/printf"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"Response = %s &lt;br&gt;"</span>, <a href="http://www.php.net/htmlspecialchars"><span style="color: #000066;">htmlspecialchars</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$responseMessage</span>-&gt;<span style="color: #006600;">str</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

<span style="color: #66cc66;">}</span> catch <span style="color: #66cc66;">(</span>Exception <span style="color: #0000ff;">$e</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>

    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$e</span> instanceof WSFault<span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
        <a href="http://www.php.net/printf"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"Soap Fault: %s<span style="font-weight: bold; color: #000099;">\\</span>n"</span>, <span style="color: #0000ff;">$e</span>-&gt;<span style="color: #006600;">Reason</span><span style="color: #66cc66;">)</span>;
    <span style="color: #66cc66;">}</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">{</span>
        <a href="http://www.php.net/printf"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"Message = %s<span style="font-weight: bold; color: #000099;">\\</span>n"</span>,<span style="color: #0000ff;">$e</span>-&gt;<span style="color: #006600;">getMessage</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;
    <span style="color: #66cc66;">}</span>

<span style="color: #66cc66;">}</span>
<span style="font-weight: bold; color: #000000;">?&gt;</span></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/10/07/test-your-ssl-soap-client-with-an-online-service/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Http Authentication for SOAP Messages in PHP &#8211; 2 Minutes Introduction</title>
		<link>http://www.dimuthu.org/blog/2008/09/24/http-authentication-for-sopa-messages-in-php-2-minutes-introduction/</link>
		<comments>http://www.dimuthu.org/blog/2008/09/24/http-authentication-for-sopa-messages-in-php-2-minutes-introduction/#comments</comments>
		<pubDate>Wed, 24 Sep 2008 17:29:28 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[2 minutes guide]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[wsf/php]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[2 minutes]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[basic]]></category>
		<category><![CDATA[HTTP]]></category>
		<category><![CDATA[messages]]></category>
		<category><![CDATA[SOAP]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=380</guid>
		<description><![CDATA[Yesterday&#8217;s blog on &#8220;Using Username token in Authentication&#8221; I explained a standard way of authenting SOAP messages in Application layer (Message level Authentication). Anyway you can authenticate SOAP messages in transport level itself. For an example with HTTP Transport we can use the HTTP Basic Authentication for this purpose. Setting up a client with Authentication [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday&#8217;s blog on <a href="http://www.dimuthu.org/blog/2008/09/23/authenticate-using-username-token-from-php-2-minutes-introduction/">&#8220;Using Username token in Authentication&#8221;</a> I explained a standard way of authenting SOAP messages in Application layer (Message level Authentication). Anyway you can authenticate SOAP messages in transport level itself. For an example with HTTP Transport we can use the HTTP Basic Authentication for this purpose.</p>
<p><strong>Setting up a client with Authentication Information</strong></p>
<p>With <a href="http://wso2.org/projects/wsf/php">WSF/PHP</a> you can give the username, password and the authentication type as options for WSClient constructor.</p>
<pre class="php">	<span style="color: #0000ff;">$client</span> = <span style="font-weight: bold; color: #000000;">new</span> WSClient<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a> <span style="color: #66cc66;">(</span><span style="color: #ff0000;">"to"</span> =&gt; <span style="color: #ff0000;">"http://server/myendpoint"</span>,
		<span style="color: #ff0000;">"httpAuthUsername"</span> =&gt; <span style="color: #ff0000;">"user"</span>,
		<span style="color: #ff0000;">"httpAuthPassword"</span> =&gt; <span style="color: #ff0000;">"user_password"</span>,
		<span style="color: #ff0000;">"httpAuthType"</span> =&gt; <span style="color: #ff0000;">"basic"</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;</pre>
<p><strong>Setting up the Server to Handle the Authentication</strong></p>
<p>Since the Http authentication is handled by the transport level, you have to configure your authentication information in your web server itself. (e.g. Apache or IIS).</p>
<p>If you are using Apache, please use <a href="http://apache.active-venture.com/auth-basic.html">this guide</a> to configure your allowed list to access the server.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/09/24/http-authentication-for-sopa-messages-in-php-2-minutes-introduction/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Authenticate using Username Token from PHP &#8211; 2 Minutes Introduction</title>
		<link>http://www.dimuthu.org/blog/2008/09/23/authenticate-using-username-token-from-php-2-minutes-introduction/</link>
		<comments>http://www.dimuthu.org/blog/2008/09/23/authenticate-using-username-token-from-php-2-minutes-introduction/#comments</comments>
		<pubDate>Tue, 23 Sep 2008 14:58:59 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[2 minutes guide]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[wsf/php]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[2 minutes]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[username token]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=372</guid>
		<description><![CDATA[Username token is a simple token sent inside SOAP message header element with username and password information.  It is used to authenticate SOAP messages in a standardized way. Sending Username Token To send username token with WSF/PHP you can use the generic API designed to implement WS-Security scenarios. First you need to declare the security [...]]]></description>
			<content:encoded><![CDATA[<p>Username token is a simple token sent inside SOAP message header element with username and password information.  It is used to authenticate SOAP messages in a standardized way.</p>
<p><strong>Sending Username Token</strong></p>
<p>To send username token with <a href="http://wso2.org/projects/wsf/php">WSF/PHP</a> you can use the generic API designed to implement WS-Security scenarios.</p>
<ul>
<li>First you need to declare the security policy saying you are using username token. You can do this either with a policy which is complaint with WS-Security Policy standards or using an associative array. Here we use the second approach which is more <em>PHP-Friendly</em>.
<pre class="php"><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"useUsernameToken"</span> =&gt; <span style="font-weight: bold; color: #000000;">TRUE</span><span style="color: #66cc66;">)</span></pre>
</li>
<li> With a WSSecurityToken instance we are giving our user parameters. In this case it is username, password and the password type.
<pre class="php"><span style="color: #0000ff;">$security_token</span> = <span style="font-weight: bold; color: #000000;">new</span> WSSecurityToken<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"user"</span> =&gt; <span style="color: #ff0000;">"my_username"</span>,
                                                <span style="color: #ff0000;">"password"</span> =&gt; <span style="color: #ff0000;">"my_password"</span>,
                                                <span style="color: #ff0000;">"passwordType"</span> =&gt; <span style="color: #ff0000;">"Digest"</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;</pre>
</li>
<li> And create the WSClient object with policy and the security token object you just created + with &#8220;useWSA&#8221; on.  This is to enable the addressing headers in the request message which guide the server to identify the service and the operation.</li>
</ul>
<p>Here is the complete code for the client.</p>
<pre class="php">    <span style="font-style: italic; color: #808080;">// Set up security options</span>
    <span style="color: #0000ff;">$security_options</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"useUsernameToken"</span> =&gt; <span style="font-weight: bold; color: #000000;">TRUE</span> <span style="color: #66cc66;">)</span>;
    <span style="color: #0000ff;">$policy</span> = <span style="font-weight: bold; color: #000000;">new</span> WSPolicy<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"security"</span> =&gt; <span style="color: #0000ff;">$security_options</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;
    <span style="color: #0000ff;">$security_token</span> = <span style="font-weight: bold; color: #000000;">new</span> WSSecurityToken<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"user"</span> =&gt; <span style="color: #ff0000;">"my_username"</span>,
                                                <span style="color: #ff0000;">"password"</span> =&gt; <span style="color: #ff0000;">"my_password"</span>,
                                                <span style="color: #ff0000;">"passwordType"</span> =&gt; <span style="color: #ff0000;">"Digest"</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

    <span style="font-style: italic; color: #808080;">// Create client with options</span>
    <span style="color: #0000ff;">$client</span> = <span style="font-weight: bold; color: #000000;">new</span> WSClient<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"useWSA"</span> =&gt; <span style="font-weight: bold; color: #000000;">TRUE</span>,
                                 <span style="color: #ff0000;">"policy"</span> =&gt; <span style="color: #0000ff;">$policy</span>,
                                 <span style="color: #ff0000;">"securityToken"</span> =&gt; <span style="color: #0000ff;">$security_token</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

    <span style="font-style: italic; color: #808080;">// Send request and capture response</span>
    <span style="color: #0000ff;">$resMessage</span> = <span style="color: #0000ff;">$client</span>-&gt;<span style="color: #006600;">request</span><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$reqMessage</span><span style="color: #66cc66;">)</span>;</pre>
<p><strong>Handling Username Token at Server Side</strong><br />
The same options (&#8220;policy&#8221; and &#8220;securityToken&#8221;) you gave to WSClient, can be given to WSService object as well. But hard coding values for &#8220;username&#8221; and &#8220;password&#8221; in SecurityToken is not much useful at the server side. Because it authenticate only one user. So in order to maintain multiple accounts, you have to have a callback function in php.</p>
<pre class="php"><span style="font-style: italic; color: #808080;">// callback function</span>
<span style="font-weight: bold; color: #000000;">function</span> my_passwd_callback_function<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$username</span><span style="color: #66cc66;">)</span>
<span style="color: #66cc66;">{</span>
    <span style="font-style: italic; color: #808080;">// logic to return the password for the username</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$password</span>
<span style="color: #66cc66;">}</span>

<span style="font-style: italic; color: #808080;">// setting it to the security token</span>
<span style="color: #0000ff;">$sec_token</span> = <span style="font-weight: bold; color: #000000;">new</span> WSSecurityToken<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"passwordCallback"</span> =&gt; <span style="color: #ff0000;">"my_passwd_callback_function"</span>,
                                       <span style="color: #ff0000;">"passwordType"</span> =&gt; <span style="color: #ff0000;">"Digest"</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;</pre>
<p>Here in the function you return the password for the username from a database and that information will be used to authenticate the request message.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/09/23/authenticate-using-username-token-from-php-2-minutes-introduction/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

