Monday, February 3, 2014

Solving "org.apache.ws.security.WSSecurityException: An unsupported signature or encryption algorithm was used"

While trying out the scenario which I have explained in my previous post Accessing a non secured backend from a secured client with the help of WSO2 ESB, with security scenario 3 onward, you might have come across an issue as below on client side.

org.apache.axis2.AxisFault: Error in encryption
    at org.apache.rampart.handler.RampartSender.invoke(RampartSender.java:76)
    at org.apache.axis2.engine.Phase.invokeHandler(Phase.java:340)
    at org.apache.axis2.engine.Phase.invoke(Phase.java:313)
    at org.apache.axis2.engine.AxisEngine.invoke(AxisEngine.java:261)
    at org.apache.axis2.engine.AxisEngine.send(AxisEngine.java:426)
    at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:430)
    at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:225)
    at org.apache.axis2.client.OperationClient.execute(OperationClient.java:149)
    at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:554)
    at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:530)
    at SecurityClient.runSecurityClient(SecurityClient.java:99)
    at SecurityClient.main(SecurityClient.java:34)
Caused by: org.apache.rampart.RampartException: Error in encryption
    at org.apache.rampart.builder.AsymmetricBindingBuilder.doSignBeforeEncrypt(AsymmetricBindingBuilder.java:612)
    at org.apache.rampart.builder.AsymmetricBindingBuilder.build(AsymmetricBindingBuilder.java:97)
    at org.apache.rampart.MessageBuilder.build(MessageBuilder.java:147)
    at org.apache.rampart.handler.RampartSender.invoke(RampartSender.java:65)
    ... 11 more
Caused by: org.apache.ws.security.WSSecurityException: An unsupported signature or encryption algorithm was used (unsupported key transport encryption algorithm: No such algorithm: http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p); nested exception is:
    java.security.NoSuchAlgorithmException: Cannot find any provider supporting RSA/ECB/OAEPPadding
    at org.apache.ws.security.util.WSSecurityUtil.getCipherInstance(WSSecurityUtil.java:785)
    at org.apache.ws.security.message.WSSecEncryptedKey.prepareInternal(WSSecEncryptedKey.java:205)
    at org.apache.ws.security.message.WSSecEncrypt.prepare(WSSecEncrypt.java:259)
    at org.apache.rampart.builder.AsymmetricBindingBuilder.doSignBeforeEncrypt(AsymmetricBindingBuilder.java:578)
    ... 14 more
Caused by: java.security.NoSuchAlgorithmException: Cannot find any provider supporting RSA/ECB/OAEPPadding
    at javax.crypto.Cipher.getInstance(Cipher.java:524)
    at org.apache.ws.security.util.WSSecurityUtil.getCipherInstance(WSSecurityUtil.java:777)
    ... 17 more
Exception in thread "main" java.lang.NullPointerException
    at SecurityClient.main(SecurityClient.java:38)

The reason for this is that the Bouncycastle jar required to run this scenario, is not found in the class path at the client side.

To overcome this issue, you need to place the relevant bouncycastle jar downloaded from the www.bouncycastle.org.

E.g.:- If you are running your client on JDK1.7, then the jar you need to download is bcprov-jdk15on-150.jar.
A point to note : I tried this scenario pointing the $ESB_HOME/repository/plugins folder to the Eclipse project and pointed to a bouncycastle jar which was at a different location. For some reason, it did not load the jar until I dropped it inside the $ESB_HOME/repository/plugins folder.

NOTE: Sometimes, you will have to clear the Eclipse/IntelliJ Idea cache in order for the classes to pick up the jars properly.

Thursday, January 16, 2014

Accessing a non secured backend from a secured client with the help of WSO2 ESB

Assume that you have a backend that is not secured and you need to invoke this with a secured client. The below configuration is of a Proxy service which talks to a non secured backend. The client used to invoke this proxy service is secured. In this particular scenario, the Proxy service is secured.

Pre-requisites

Download WSO2 ESB 4.8.0 from here. Extract it to a folder of your choice and start the server.
Download the StockPurchasingService.aar from here and deploy it in a application server of your choice.

Testing out the scenario

When the message sent from the client is secured and the backend service is not, you need to ensure that the security headers are removed from the message before it is being sent from ESB to the backend. By using the header mediator as below, we can remove the security headers.




Step 1

Deploy the following proxy service in WSO2 ESB. <?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="secClientNonSecService"
       transports="https,http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <header xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
                 name="wsse:Security"
                 action="remove"/>
      </inSequence>
      <outSequence>
         <send/>
      </outSequence>
      <endpoint>
         <address uri="http://localhost:9773/services/PuchasingService"/>
      </endpoint>
   </target>
   <publishWSDL uri="http://localhost:9773/services/PuchasingService?wsdl"/>
</proxy>

We will test our scenario with security scenario 2 (Non-repudiation). Once the above proxy service is deployed, apply security scenario 2 from the wizard.  View the ?wsdl of the proxy service and verify whether the relevant policy is attached to the proxy service.
 
One the security policy is applied, the complete proxy service configuration will look like what is available here.

Step 2

Create a Java project from your favourite IDE and place the SecurityClient.java & client.properties files in the below structure.

.
└── src
    ├── client.properties
    └── SecurityClient.java
 
Note: Download the resources.zip folder and extract the content to the src level. Then set the paths of following fields of the client.properties file accordingly.
  • clientRepo
  • clientKey
  • securityPolicyLocation
  • trustStore

Step 3

Invoke the SecurityClient.java code and you will get the expected response message as below.



Result :1000
Scenario No :2

Result : >ns:purchaseresponse xmlns:ns="http://service.purchasing.wso2.com"<>ns:return<1000>/ns:return<>/ns:purchaseresponse<

Friday, January 3, 2014

How to solve "PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target" issue of WSO2 Products


Ever come across the error message mentioned in the subject while trying out WSO2 products? Well, if you have, the reason is that cetifacte of the backend that you is not trusted and the certificate of that backend server should be added to the WSO2 product servers client-truststore.jks. Lets try this with a simple example.

Assume you have a simple API with the below configuration pointing to twitter search in WSO2 ESB (You can try this with a latest version of ESB). The configuration will be as follows.

      <api name="TwitterAPI" context="/twitter">
      <resource methods="GET" url-mapping="/*">
         <inSequence>
            <send>
               <endpoint>
                  <address uri="https://twitter.com"/>
               </endpoint>
            </send>
         </inSequence>
         <outSequence>
            <send/>
         </outSequence>
      </resource>
   </api>

Invoke the above API using the below command.

curl -v -X GET -H "Content-type: application/json" http://localhost:8281/twitter/search?q=wso2

It will throw the below exception at the WSO2 ESB server console.


Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:385)
    at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
    at sun.security.validator.Validator.validate(Validator.java:260)
    at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:326)
    at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:283)
    at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:138)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1328)
    ... 17 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:196)
    at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:268)
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:380)
    ... 23 more



Inorder to get rid of this exception and the request to passthrough successfully to the backend, you need to import the public certificate of Twitter to the client-truststore.jks of the WSO2 ESB server instance. Below are the steps you need to follow.

1. Go to https://twitter.com/, click on the lock icon at the address bar, click on the 'Connection' tab, then click on the link 'Certificate Information'. From the 'Certificate Viewer', select the tab 'Details' and click on the 'Export' button and download the certificate (twitter.com) to a preferred location.

2. Once downloaded, issue the below command to import the public certificate of Twitter to the client-truststore.jks.

$ keytool -importcert -file $somepath/twitter.com -keystore $ESB_HOME/repository/resources/security/client-truststore.jks -alias "Twitter"

3. Restart the WSO2 ESB server and invoke the API again and you will get the expected result.

Monday, September 16, 2013

How to solve "WARNING: UNPROTECTED PRIVATE KEY FILE!" error

While trying to connect to a remote server using my private key, I got the below exception.


Warning: Permanently added '122.248.220.36' (ECDSA) to the list of known hosts. @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0664 for 'evanthika.pem' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
bad permissions: ignore key: evanthika.pem
Permission denied (publickey).



To solve this, run the below command from the place that you have you .pem file

chmod 0400 evanthika.pem

Wednesday, December 5, 2012

POX security with WSO2 ESB Proxy services

WSO2 ESB supports POX (Pure Old XML) security now. Let us see how we can test this with the latest version of WSO2 ESB .

Pre-requisites - Download the latest stable release of WSO2 ESB from here.

Step 1 - Start WSO2 ESB server

Step 2 - Create a Proxy Service. (We will use the Echo service shipped with WSO2 ESB here)


 <proxy xmlns="http://ws.apache.org/ns/synapse" name="PoxSecurityProxy" transports="https" statistics="disable" trace="disable" startOnLoad="true">
   <target>
      <outSequence>
         <send/>
      </outSequence>
      <endpoint>
         <address uri="http://localhost:8280/services/echo"/>
      </endpoint>
   </target>
   <publishWSDL uri="http://localhost:8280/services/echo?wsdl"/>
</proxy>



Step 3 - Once the proxy service is deployed, access the dashboard and apply security scenario 1 (UsernameToken)

Step 4 - Select the user group which you expect to be given permission to access the Proxy service


Step 5 - Once security is applied to the service, access the dashboard and you will see only the HTTPS endpoint available


Step 6 - Now let us access this Proxy service. There are two ways which you can access the service.

a) Through a preferred browser

b) By sending a curl command

Through a browser

Copy the above HTTPS Endpoint URL and paste it in the location bar and hit Enter.
https://localhost:8243/services/PoxSecurityProxy/echoString?in=Hi

It would prompt you to specify the user name and the password.
Provide admin as both and you should be given the correct response.

Using Curl
  curl -k --basic -u admin:admin https://localhost:8243/services/PoxSecurityProxy/echoString?in=Hi 

Tuesday, September 25, 2012

5 minuted guide on taking Oracle dumps and restoring them (using exp/imp commands)

To take the a dump, use the below command

$ exp [username]/[password[ file=[anyname].dmp log=[anyname].log owner=[owner_of_db]

E.g.:- exp wso2user/wso2user file=wso2db.dmp log=wso2dberror.log owner=wso2user

To restore the dump which you took, use the below command

$ imp [new user]/[password] file=[dump to be restored].dmp fromuser=[who exported the db] touser=[new user]

E.g.:- imp wso2newuser/wso2newuser file=wso2db.dmp fromuser=wso2user touser=wso2newuser

Note that you need to create a new user using the below commands before issuing the above imp command.

drop user wso2newuser cascade;
Create user wso2newuser identified by wso2newuser account unlock;
grant connect to wso2newuser;
grant create session, dba to wso2newuser;
commit;


Sunday, September 23, 2012

How to resolve "ORA-00018: maximum number of sessions exceeded" issue

While working with WSO2 G-Reg which was connected to an Oracle DB, we came across and issue where the maximum number of connections were exceeded. 

To get over this exception, we had to increase the number of sessions, processors and transactions as below. 

I found this great blog which explains everything in detail. 


 sql> alter system set processes=500 scope=spfile;
 sql> alter system set sessions=555 scope=spfile;
 sql> alter system set transactions=610 scope=spfile;
 sql> shutdown abort
 sql> startup