Encrypt and Sign your SOAP messages in PHP

When you are developing a Web Service, you have to think about the security aspects of your service seriously. When it comes to security in web services you have two basic choices.

  1. Transport level security – Just SOAP over HTTPS
  2. Message level security – WS-Security

See my previous blog comparing Transport level and Message level security.

If you are satisfied with the security provided by using just ‘SOAP over HTTPS’, you can get the work done by configuring your server (Apache or IIS) to enable ssl. See http://www.onlamp.com/pub/a/onlamp/2008/03/04/step-by-step-configuring-ssl-under-apache.html for an step by step guide for configure SSL in your Apache server.

If you want message level security for your application, just use WS-Security. With WSF/PHP it is even easier to implement than SOAP over HTTPS method, because you can provide the certificates programatically in PHP and no need to do further configuration.

WSF/PHP provides you two classes in line with WSService to implement an API to provide WS-Security.

  1. WSPolicy -Let you provide rules that the engine need to follow in securing the message. E.g.
    $policy = new WSPolicy(array("security"=> array("encrypt" => TRUE,
                        "algorithmSuite" => "Basic256Rsa15",
                        "securityTokenReference" => "IssuerSerial")));

    In fact you can load policies from an xml which adheres to the WS-SecurityPolicy specification.

  2. WSSecurityToken – Keeps the security tokens like certificates, keys, username, passwords which would be used when applying the rules specified in the policy. E.g.
    $sec_token = new WSSecurityToken(array("privateKey" => $pvt_key,
                                           "receiverCertificate" => $pub_key));

You can see the WS-Security in action on live from http://labs.wso2.org/wsf/php/samples/security/ and security demo ource codes.

This entry was posted in security, web services and tagged , , , , , , , , , , . Bookmark the permalink.

3 Responses to Encrypt and Sign your SOAP messages in PHP

  1. Pingback: Recent Links Tagged With "encrypt" - JabberTags

  2. Charles says:

    I was wondering if you had experience or resources with method one, more specifically, connect to a service that’s using a self-signed cert? I’m still digging and I haven’t found a solution…. Using PHP to talk to a WCF webservice (implying IIS7). =/

Leave a Reply

Your email address will not be published. Required fields are marked *