Generating a Java Keystore using SSL certificate, Private Key and Intermediate Certificate for HTTPS

Working with certificates can be a little confusing at times. This article aims to help give a brief understanding about the steps in creating a java keystore with the private key and a certificate signed by a certificate authority.

The article also discusses about how this changes when working with intermediate certificates and root certificates.

Step 1: Generate Private Key and Certificate Signing Request (CSR)

The openssl command can be used to generate a Private Key and Certificate Signing Request (CSR). A simple google search can reveal many articles on how to generate a Private Key and CSR using openssl based on the requirement, and more information can be gathered from the openssl documentation.

Step 2: Send the CSR to a Certificate Authority (CA) and obtain a Certificate

The generated CSR can be sent to a certificate authority such as DigiCert, VeriSign etc. and they will provide you with a certificate either in a .cer or .crt format.

Step 3: Generate the JKS using the Private Key and the Certificate from the CA

First we need to bundle the Private Key and the Obtained Certificate to form a pkcs12 archive. The following command can be used for this

openssl pkcs12 -export -inkey <GENERATED_PRIVATE_KEY>.key -in <CERTIFICATE_FROM_CA>.cer -out certificate.pkcs12

Next this bundled archive can be used to generate the java keystore with the following command.

keytool -importkeystore -srckeystore certificate.pkcs12 -srcstoretype PKCS12 -destkeystore <NAME_OF_JKS>.jks

Working with Intermediate and Root Certificates

Say you have already obtained a root certificate for <YOUR_DOMAIN>.com. Let’s call it as Now you plan on obtaining a certificate for <YOUR_SUB_DOMAIN>.<YOUR_DOMAIN>.com. Let’s call that as Now you have two separate certificates for these two domains. If you were to generate the JKS using only the certificate, it may not fully work in some scenarios. For this purpose, CA’s provide us with intermediate certificates.

An intermediate certificate is a subordinate certificate issued by the trusted root specifically to issue end-entity server certificates. The result is a certificate chain that begins at the trusted root CA, through the intermediate and ending with the SSL certificate issued to you. Such certificates are called chained root certificates   

So these intermediate certificates chain the root certificate with the certificate provided to you. In this case it chains the certificate with the certificate. The relevant intermediate certificate will be provided to you by the CA when you purchase the SSL certificate.

If this is the case, we can not conduct Step 3 as above. Instead the following two steps need to be conducted.

Step 3a: Concatenate the certificate, the intermediate certificates and the root certificate to form one certificate.txt file


Step 3b: Generate the JKS using the Private Key and the concatenated certificate file

As above, bundle the Private Key and the Concatenated Certificate file to form a pkcs12 archive with the following command,

openssl pkcs12 -export -inkey <GENERATED_PRIVATE_KEY>.key -in certificate.txt -out certificate.pkcs12

NOTE: Here certificate.txt is the concatenated certificate file

Next this bundled archive can be used to generate the java keystore.

keytool -importkeystore -srckeystore certificate.pkcs12 -srcstoretype PKCS12 -destkeystore <NAME_OF_JKS>.jks

Now this generated Java Keystore can be used for securing your application or environment.

Good Luck!


Configuring WSO2 SMSOTP Connector

This topic provides instructions on how to configure the SMSOTP and the Identity Server to integrate using a sample app. This is an updated version of the WSO2 article found here. The current SMSOTP connector can be easily used with nexmo but difficult to integrate with other SMS APIs. I have updated the connector to be easily integrable with most SMS APIs on the market. This is the updated documentation of the connector. 

Building the SMSOTP artifacts

  1. Clone the repository from
  2. Navigate to components/smsotp and build the two artifacts using mvn clean install.

Deploying SMSOTP artifacts

  1. Place the smsotpauthenticationendpoint.war file into the <IS_HOME>/repository/deployment/server/webapps directory.
  2. Place the  org.wso2.carbon.identity.authenticator.SMSOTP-1.0.0.jar file into the <IS_HOME>/repository/components/dropins directory.

Configuring the SMSOTP provider

The SMS provider is the entity that will be used to send SMS. The SMSOTP connector has been configured such that it can be used with most types of SMS APIs. Some use the GET method with the client secret and API Key being encoded in the URL (eg: Nexmo) whereas some may use the POST method while sending the values in the headers and the message and telephone number in the payload (eg: Clickatell). Note that this could change significantly between different SMS providers. The configuration of the Identity Server would also change along with this.

Deploying sample

The next step is to deploy the sample app in order to use it in this scenario.

Once this is done, the next step is to configure the WSO2 Identity Server by adding an identity provider and service provider.

Configuring the identity provider

Now you have to configure WSO2 Identity Server by adding a new identity provider.

  1. Download the WSO2 Identity Server from here and run it.
  2. Log in to the management console as an administrator.
  3. In the Identity section under the Main tab of the management console, click Add under Identity Providers.
  4. Give a suitable name (eg: SMSOTP) as the Identity Provider Name.
  5. Go to SMSOTP Configuration under Federated Authenticators .
  6. Select both checkboxes to Enable SMSOTP Authenticator and make it the Default .
  7. Enter the SMS Url and the HTTP Method used (eg: GET or POST). Include the headers and payload if the API uses any. If the text message and the phone number are passed as parameters in any field, then include them as &msg and &num respectively.

Note: If nexmo is used as the SMS provider, the nexmo API requires the parameters to be encoded in the URL, so the SMS URL would be as follows,*********&api_secret=********&from=NEXMO&to=&num&text=&msg

*The api key and api secret are provided when you register with nexmo.


Note: If clickatell is used as the SMS provider, clickatell uses a POST method with headers and the text message and phone number being sent as the payload. So the fields would be as follows.



HTTP Headers: X-Version: 1,Authorization: bearer ********,Accept: application/json,Content-Type: application/json

HTTP Payload: {“text”:”&msg“,”to”:[“&num“]}

*The auth token is provided when you register with clickatell.

clickatell8. Click Register .

You have now added the identity provider.

Configuring the service provider

The next step is to configure the service provider.

1. Return to the management console.

2. In the Identity section under the Main tab, click Add under Service Providers .

3. Enter in the Service Provider Name text box and click Register .

4. In the Inbound Authentication Configuration section, click Configure under the SAML2 Web SSO Configuration section.


5. Now set the configuration as follows:

6. Select the following check-boxes:

  • Enable Response Signing
  • Enable Single Logout
  • Enable Attribute Profile
  • Include Attributes in the Response Always

7. Click Update to save the changes. Now you will be sent back to the Service Providers page.

8. Go to Claim configuration and select the mobile claim.


9. Go to Local and Outbound Authentication Configuration section.

10. Select the Advanced configuration radio button option

11. Add the basic authentication as first step and SMSOTP authentication as second step


You have now added and configured the service provider.

Configuring User Claim

  1. Select Users and Roles in the IS Management Console
  2. Go to Users → admin →User Profile  and  update the mobile number. ( If nexmo is used, this number must be registered with nexmo in order to send sms).

Testing the sample

1. To  test the sample, go to the following URL: http://localhost:8080/ 

2. Click the link to log in with SAML from WSO2 Identity Server.

3. Basic authentication page will be visible, use your IS username and password.

4. You will get a token to your mobile phone.Type the code to authenticate, You will be  taken to the home page of the app

Federated authentication between two Identity Servers

In this post I shall be explaining how federated authentication can be used to redirect the authentication of a user to another Identity Server.

Download the Identity Server from here, if you have not done so already. The installation guide can be found here.

Checkout the repository of the travelocity SSO sample from the link below, follow these instructions to checkout a folder.

In this tutorial we will be using two Identity Server instances namely an internal IS running on port 9443, and an external IS running on port 9445. Check this post if you need more information on how to run multiple Identity Servers on different ports. The travelocity sample will be deployed on tomcat. The travelocity webapp will redirect the login to the external IS, which again will redirect the authentication to the internal IS. The stepwise scenario is as follows.

  1. User visits the travelocity website and clicks to login through SAML SSO then the user is redirected to the external IS.
  2. The external IS redirects the authentication to the internal IS.
  3. The internal IS login screen is displayed and the user enters the credentials.
  4. The user credentials are checked against the LDAP of the internal IS.
  5. If authenticated, the user is redirected back to the external IS.
  6. The external IS redirects the user back to travelocity website as a logged in user.


Step 1:

Go to the directory you saved the sample in and open src/main/resources/ Check if the the configuration is as below and change the port of the IdPURL to 9445.




Step 2: External IS Configuration

The external IS should have a new Identity Provider configured to federate the users to the internal IS. It should also have a service provider configured to identify the webapp.

  1. Add a new Identity Provider in the internal IS. Give a name and expand Federated Authenticators then SAML2 Web SSO Configuration. Do the configuration as follows. 


Save the configuration.

  1. Add a new Service Provider and click on Configure under Inbound Authentication Configuration -> SAML2 Web SSO Configuration then do the configuration as follows.

45Expand the Local & Outbound Authentication Configuration, then click on the  Federated Authentication radio button then select the identity provider you configured from the drop down.


Save the configuration.

Step 3: Internal IS Configuration

A new service provider needs to be configured in the Internal IS with the external IS as the Assertion Consumer URL.

Add a new Service Provider and click on Configure under Inbound Authentication Configuration -> SAML2 Web SSO Configuration then do the configuration as follows.


Save the configuration.

Step 4: Testing the authentication

Now if you add a user to the internal IS, you should be able to log in with the user even though you have configured the service provider in the external IS.

The basic HTTP header flow is as follows.

http://localhost:8080/ 1. User visits travelocity webpage
http://localhost:8080/ 2. User clicks on the login with SAML hyperlink
https://localhost:9445/samlsso?SAMLRequest=**** 3. User is directed to the external IS
https://localhost:9443/samlsso?SAMLRequest=**** 4. User is directed to the login page of the internal IS from the external IS
https://localhost:9443/samlsso 5. User logs in
https://localhost:9445/commonauth 6. Logged in user is redirected back to external IS
https://localhost:9445/samlsso?sessionDataKey=**** 7. User is given a session data key and is considered a logged in user

Hope this helps, this would prove useful in scenarios where the service provider has to be registered in one IS while the LDAP has been configured to another IS. Do drop a comment if you come across any issues :)

User Provisioning between two WSO2 Identity Servers


In this post I shall be explaining how users can be provisioned between two different Identity Server instances.

User provisioning is a process which simplifies the creation and management of users on multiple systems. With user provisioning, when the user is added to the WSO2 LDAP, the system creates user accounts for the user on many different systems,. This eliminates the need for user accounts to be manually created in these systems. In this tutorial I will demonstrate how this functionality can be done between two WSO2 Identity Server instances. So ultimately when a user is added in one IS, the same user will be provisioned to the other.

Download the Identity Server from here, if you have not done so already. The installation guide can be found here.

In this example I shall be using two IS instances running locally on two different ports 9443 and 9445. If you need more information on how to change the running ports of WSO2 products, check my blog post on “Changing the Ports of WSO2 Servers

For the purposes of this tutorial, one IS shall be referred to as the Internal IS and the other as the External IS. The internal IS in this tutorial is running on port 9443 while the external IS is running on port 9445. Our ultimate objective is: when a user is added to the internal IS the same user shall be provisioned to the external IS.

Start the two Identity Servers and login to their management consoles and do the configurations as below.

Step 1: Configuring the Internal IS

  • Click on Add under Identity Providers on the left pane.
  • Give a name and expand the Outbound Provisioning Connectors and under that the SCIM Provisioning Configuration.
  • Check the Enable Checkbox and give the Username and Password of the External IS.
  • Give the user endpoint of the external IS, this is by default https://<HOST&gt;:<PORT>/wso2/scim/Users. (This value can also be found by expanding the Inbound Provisioning Configuration of the Resident Identity Provider in the External IS)
  • Give the User Store Domain as WSO2
  • Check the Enable User password provisioning to a SCIM domain checkbox.


  • Save the configuration
  • Click on List under Service Providers on the left pane then click on the Resident Service Provider link.
  • Expand Outbound Provisioning Configuration and select the Identity Provider you configured from the drop down menu then click on the plus icon.
  • Then update the configuration.


Step 2: Configuring the External IS

  • Click on List under Service Providers on the left pane then click on the Resident Service Provider link.
  • Expand the Inbound Provisioning Configuration and select the userstore domain to provision users to.
  • Then update the configuration


Step 3: Testing the provisioning

Now the configuration is complete. Hence when you add a user to the internal IS, the user should get provisioned to the external IS. I have added a user named through the management console to the internal IS, the log entries of the internal and external Identity Servers are given below.

Internal IS

[2016-02-05 16:33:42,181]  INFO {org.wso2.carbon.identity.scim.common.impl.ProvisioningClient} –  SCIM – create user operation returned with response code: 201

External IS

[2016-02-05 16:33:42,092]  INFO {org.wso2.carbon.identity.scim.provider.impl.SCIMUserManager} –  User: PRIMARY/ is created through SCIM.

Now if you login to the external IS management console and navigate to List under Users and Roles, you will see that a user called has been added.


Sometimes you may get the following error when adding a user to the internal IS.

ERROR {org.wso2.carbon.user.core.common.AbstractUserStoreManager} –  Error occurred while accessing Java Security Manager Privilege Block

This is caused as a result of the new Identity Provider configuration not being saved properly. In this case, first delete the identity provider entry from the Outbound Provisioning Configuration of the Resident Service Provider of the internal IS and Update it. Then delete the Identity Provider from the list of Identity Providers then follow step 1 again.

Hope this helps, do drop a comment if you come across any issues. Good Luck! :)

Changing the Ports of WSO2 Servers

Almost all of WSO2 products are by default configured to run on port 9443. You will run into many instances where you need to run multiple WSO2 products or multiple instances of the same product in the same environment. In these instances the operating ports of the servers need to be changed.

This can be done mainly in two ways:

Method 1: Setting the port in the carbon.xml

Open the <Product_Home>repository/conf/carbon.xml and change the value between the <Offset> tags. It is by default 0, if you change the value to 2, the server will run on port 9445.

Method 2: Passing the port offset during startup of the server

Running the following command with start the server on port 9445.

./ -DportOffset=2

In certain rare instances the port has been hard coded instead of being extracted from the carbon.xml. If you receive a “”, the best possible way is to run the following command to see if it the port value has been hard coded anywhere and then change them accordingly where needed.

grep -ril “9443”

IDP Initiated SSO vs SP Initiated SSO

When working with SSO with SAML, it is vital that one understands the difference between Identity Provider Initiated Single Sign On and Service Provider Initiated SSO. Before that, it’s important to understand who Identity Providers and Service Providers are and their differences.

Given below are the definitions from the OASIS Organization that created SAML.

“An Identity Provider is a kind of provider that creates, maintains, and manages identity information for principals and provides principal authentication to other service providers within a federation, such as with web browser profiles”

“A Service Provider is a role donned by a system entity where the system entity provides services to principals or other system entities”

In the context of this discussion, an Identity Provider is basically an entity that provides authentication for it’s users, whereas the webapp using this service could be called as the Service Provider.  In the Travelocity Sample in my previous post, is the SP whereas the WSO2 Identity Server is the IDP.

Now the difference between IDP Initiated SSO and SP Initiated SSO is quite simple. In SP Initiated SSO, the Single Sign On process is initiated by the web application. The user first visits the webapp, then the user is redirected to the IDP along with an AuthnRequest generated at the SP. The Travelocity Sample in my previous post is a classic example of this scenario. When the user visits http://localhost:8080/, and when he clicks on the hyperlink to login with SAML, the webapp initiates the SSO process with an AuthnRequest. A sample of the AuthnRequest sent is given below.

<samlp:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
    <samlp:Issuer xmlns:samlp="urn:oasis:names:tc:SAML:2.0:assertion">concurUSD</samlp:Issuer>
    <saml2p:NameIDPolicy xmlns:saml2p="urn:oasis:names:tc:SAML:2.0:protocol"
    <saml2p:RequestedAuthnContext xmlns:saml2p="urn:oasis:names:tc:SAML:2.0:protocol"
        <saml:AuthnContextClassRef xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport</saml:AuthnContextClassRef>

The AuthnRequest contains important information such as the Assertion Consumer URL, the issuer and the NameID Format which is required by the IDP to identify where to redirect the request. Having authenticated the user, the IDP responds with a SAML Response and the process is similar for both types after this.

On the other hand, when using IDP Initiated SSO, the user does not go through the webapp first, but rather is directed to the IDP. So the first thing the user sees is the authentication page of the IDP. This is done by using a query string along with the URL.

The sample URL with the query string used by the WSO2 IS running on localhost port 9443, is given below.


Here the <sp-issuer> is the Issuer value we define when creating a Service Provider in the WSO2 IS Management Console.

So in the same travelocity example, if I want to use IDP Initiated SSO, the first thing that needs to be done is to “Enable IdP Initiated SSO” in the Service Provider configuration in the WSO2 IS. This is done by selecting the Service Provider “” from the list of SPs, then Inbound Authentication Configuration -> SAML2 Web SSO Configuration. Then click on edit, then check the “Enable IdP Initiated SSO” check-box at the very bottom. The save the configuration.

Now instead of having to go through http://localhost:8080/, and having to click on the SAML 2 hyperlink, the user can directly go to the URL given below and he shall be directed to the WSO2 IS authentication page.


Both these methods are equally used in real life and the choice of one over the other, depends heavily on the requirement of the user.

Hope you enjoyed the post, do drop a comment if I’ve missed anything :)

How the Travelocity Sample Works

If you’ve been following the Configuring Single Sign-On with SAML 2.0 on the WSO2 Identity Server Documentation, but can’t seem to figure out how SSO works on it, you’ve arrived at the right place. In this post I’ll be explaining my understanding of how the sample works. This might not be the most accurate description, so please feel free to drop a comment if I’ve mistaken something :)

You can checkout the sample from the SVN repository using the command given below.

svn co

The tree structure is given below,


Here the most important files for the purposes of this tutorial are, the java classes, the properties files, the web.xml and the home and index java servlet pages. The rest can be disregarded since they are not concerned with SSO.

To make this tutorial simple, we’ll be going through each of the components similar to the order in which they are referred in SAML2 based SSO. So once you have done the configuration based on the tutorial and the war file is deployed on tomcat, the first thing that happens when you visit http://localhost:8080/ is that you will be directed to the index page.

Part 1: The index.jsp

Given below is the index page of the travelocity sample.


For this tutorial, we shall only be focused on “login with SAML from WSO2 Identity Server”. If you look at the source code of the index.jsp page, you will see that the hyperlink for the SAML Login is “samlsso


So when you click on the “here” hyperlink, you will get redirected to http://localhost:8080/ but in reality you get directed to the authentication page of the WSO2 Identity Server which is given below.


This is where the next component comes into play. So let’s look at the SSOFilter.

Part 2: The SSOFilter

A filter is a way of performing filtered functionality on a java web application. Typically filters are used to perform some piece of functionality either before or after the primary functionality of the web application. The architecture of the filter is given below.


How the filter is triggered will be discussed in the web.xml (Part 3).

In this scenario, the SSO functionality has been implemented in the form of a filter using the WSO2 library org.wso2.carbon.identity.sso.agent. When the filter is triggered, the request is sent through the SSOFilter. Here a SAML Assertion is generated and the browser is redirected to the WSO2 IS authentication page and once a user is authenticated, the SAML Response is again sent through the filter. During this process the SAML response is validated using the public and private key pair found in the wso2carbon.jks file. Once the user is validated, the browser is redirected to the home.jsp which is the Assertion Consumer URL.

Part 3: The web.xml

The web.xml is the deployment descriptor file of a java web application and is used to determine how URLs map to servlets, which URLs require authentication, and other information. It can be found inside the WEB-INF directory in the webapp folder.

In the web.xml of travelocity, the SSOFilter is defined.


Here the filter class name is given to locate the jar file from the libraries. The jar file was added as a dependency in the pom.xml.

The filter mapping is used to initialize the filter. When a particular url is requested, if it contains any of the url-patterns as given in the filter mapping, the particular filter is initiated. In this scenario, when the url http://localhost:8080/ is requested, since it contains the /samlsso url pattern, the SSOFilter is initiated.

Now once the request is sent through the SSOFilter and the SAML Response is validated, the user is then redirected to the Assertion Consumer URL. In this case the consumer url is http://localhost:8080/ So next let’s look at the home.jsp.

Part 4: The home.jsp


In this example, the default dashboard of the webapp is implemented within the consumer url itself, but in practical situations, the consumer url merely redirects the user to the particular dashboard of the logged in user.

Given below is the source of the home.jsp.


The first few lines of code in the home.jsp are there to retrieve the attributes from the response. the claimedID and openIDAttributes are irrelevant because we are only focused on SAML in this tutorial. The subject is the username of the logged in user. The samlSSOAttributes and accessTokenResponseBean are also saved if needed to be used. At any point if any of the if statements become false, that means that the user is not validated, and redirects the browser back to the index.jsp.

The rest of the home.jsp is HTML and is fairly irrelevant except for the part where it displays the username of the logged in user from the subjectID.


That concludes the SAML process but to fully understand the working of SSO, it’s important to know how the properties file is defined and how it is loaded.

Part 5: The

The file is found inside the resources folder and this file contains all the information about SAML based SSO for the webapp. The file is loaded to the SSOAgentConfigs by the, which will be discussed in the next part.

The information in the properties file such as the issuerID, the Assertion Consumer URL are used by the SSOFilter to identify the Service Provider in the WSO2 Identity Server. This also says if the response and assertion are signed or not, which needs to be configured in the IS.

Other information such as the Keystore Password are used to retrieve the public and private keys from the keystore wso2carbon.jks, to validate the saml response received by the filter.

It is important to understand that the properties file nor the keystore are used by the web application but rather are used by the SSOFilter.

Part 6: The

So how does the SSOFilter know where to find the properties file and the keystore. This is where the is used. The SampleContextEventListener is initialized as a listener in the web.xml as follows.


When the webapp is deployed, the SampleContextEventListener loads the properties file and the keystore to the SSOAgentConfigs in the org.wso2.carbon.identity.sso.agent library. From here it is again retrieved whenever needed.

So that’s a brief explanation as to how the sample works, there’s much more to it, specially the functioanlity of the SSOFilter. Hope I covered everything correctly, do drop a comment if I’ve missed anything.