PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/helloworld-client-ssl/README.md

https://gitlab.com/avinashmx/WildFly
Markdown | 302 lines | 195 code | 107 blank | 0 comment | 0 complexity | 7b46f4efd8f1ab698017441e36a44d36 MD5 | raw file
  1. helloworld-client-ssl: Wildfly mutual SSL(two-way) configuration example
  2. =======================================================================
  3. Author: Giriraj Sharma
  4. Level: Intermediate
  5. Technologies: Mutual SSL, Undertow, Wildfly
  6. Summary: Basic example that demonstrates client mutual SSL authentication in wildlfy.
  7. What is it?
  8. -----------
  9. This example demonstrates the configuration of *client mutual SSL authentication* in *JBoss Enterprise Application Platform 6* or *WildFly*.
  10. Mutual SSL provides the same security as SSL, with the addition of authentication and non-repudiation of the client authentication, using digital signatures. When mutual authentication is used the server would request the client to provide a certificate in addition to the server certificate issued to the client. Mutual authentication requires an extra round trip time for client certificate exchange. In addition the client must buy and maintain a digital certificate.
  11. This quickstart shows how to configure wildfly to enable TLS/SSL configuration for the new wildfly web-subsystem Undertow and enable mutual (two-way) SSL authentication for clients.
  12. Before we run this example, we must create certificates and configure the server to use SSL, https listener and require client verification.
  13. System requirements
  14. -------------------
  15. All you need to build this project is Java 6.0 (Java SDK 1.6) or better, Maven 3.0 or better.
  16. The application this project produces is designed to be run on JBoss Enterprise Application Platform 6 or WildFly.
  17. Configure Maven
  18. ---------------
  19. If you have not yet done so, you must [Configure Maven](http://www.jboss.org/jdf/quickstarts/jboss-as-quickstart/#configure_maven) before testing the quickstarts.
  20. ## Setup CA, server and client keys using openSSL
  21. Certificate Authority, server and client keys can be generated either viua traditional openSSL tool or via cross-paltform java keytool.
  22. ### Setup CA
  23. First of all we need to set up the Certificate Authority (CA) to issue certificate.
  24. 1. First download OpenSSL and install it.
  25. 2. Set up the directory structure and files required by OpenSSL.
  26. 3. Create a directory ~\OpenSSL\workspace and place the openssl.conf file in the workplace.
  27. mkdir -p OpenSSL/workspace
  28. workspace cd OpenSSL/workspace
  29. workspace mkdir Keys CSR Certificates
  30. workspace touch serial.txt database.txt
  31. 4. Generate a key for your Root CA. Execute the below OpenSSL command at workspace where you have openssl configuration file.
  32. openssl genrsa -des3 -out Keys/RootCA.key 2048
  33. 5. This will ask for passphrase for the key, please provide the passphrase and remember it. This will be used later.
  34. 6. The next step is to create a self-signed certificate for our CA, this certificate will be used to sign and issue other certificates.
  35. openssl req -config openssl.conf -new -x509 -days 360 -key Keys/RootCA.key -out Certificates/RootCA.crt
  36. 7. You will be asked to provide the following information:-
  37. Country Name (2 letter code) :US
  38. State or Province Name (full name) :Carolina
  39. Locality Name (eg, city) :Raleigh
  40. Organization Name (eg, company) :Sample Inc
  41. Organizational Unit Name (eg, section) :Web
  42. Common Name (eg, your websites domain name) :sample.com
  43. Email Address :sample@sample.com
  44. 8. Export root CA certificate into a keystore
  45. keytool -export -alias server -keystore RootCA.keystore -rfc -file Certificates/RootCA.crt -keypass keypassword -storepass keypassword
  46. 9. Export root CA certificate into a truststore
  47. keytool -import -file Certificates/RootCA.crt -keystore RootCA.truststore -keypass keypassword -storepass keypassword
  48. Now we can see our CAs certificate in the Certificates folder and is ready to sign the certificates.
  49. The server/client certificate pair can be used when an application is trying to access a web service which is configured to authenticate the client application using the client ssl certificates. We can follow steps below to create server and client certificate using OpenSSL
  50. ### Create the server and client certificate
  51. 1. Create private key for the server.
  52. openssl genrsa -des3 -out Keys/server.key 2048
  53. 2. Create CSR for the server.
  54. openssl req -config openssl.cnf -new -key Keys/server.key -out CSR/server.csr
  55. 3. Create server certificate.
  56. openssl ca -config openssl.cnf -days 360 -in CSR/server.csr -out Certificates/server.crt -keyfile Keys/RootCA.key -cert Certificates/RootCA.crt -policy policy_anything
  57. 4. Export server certificate into a keystore
  58. keytool -export -alias server -keystore server.keystore -rfc -file Certificates/server.crt -keypass keypassword -storepass keypassword
  59. 5. Create private key for the client.
  60. openssl genrsa -des3 -out Keys/client.key 2048
  61. 6. Create CSR for the client.
  62. openssl req -config openssl.cnf -new -key Keys/client.key -out CSR/client.csr
  63. 7. Create client certificate.
  64. openssl ca -config openssl.cnf -days 360 -in CSR/client.csr -out Certificates/client.crt -keyfile Keys/RootCA.key -cert Certificates/RootCA.crt
  65. 8. Finally export the client certificate to pkcs format.
  66. openssl pkcs12 -export -in Certificates/client.crt -inkey Keys/client.key -certfile Certificates/RootCA.crt -out Certificates/clientCert.p12
  67. ## Setup CA, server and client keys using Java Keytool
  68. ### Create the server and client certificate
  69. 1. Open a command line and navigate to the JBoss server `configuration` directory:
  70. For Linux: JBOSS_HOME/standalone/configuration
  71. For Windows: JBOSS_HOME\standalone\configuration
  72. 2. Create a certificate for your server using the following command:
  73. keytool -genkey -keyalg RSA -keystore server.keystore -storepass keypassword -validity 365
  74. You'll be prompted for some additional information, such as your name, organizational unit, and location. Enter any values you prefer.
  75. 3. Create the client certificate, which is used to authenticate against the server when accessing a resource through SSL.
  76. keytool -genkey -keystore client.keystore -storepass keypassword -validity 365 -keyalg RSA -keysize 2048 -storetype pkcs12
  77. 4. Export the client certificate and create a truststore by importing this certificate:
  78. keytool -exportcert -keystore client.keystore -storetype pkcs12 -storepass keypassword -keypass keypassword -file client.crt
  79. keytool -import -file client.crt -keystore client.truststore
  80. 5. Export client certificate to pkcs12 format
  81. keytool -importkeystore -srckeystore client.keystore -destkeystore clientCert.p12 -srcstoretype PKCS12 -deststoretype PKCS12 -deststorepass keypassword
  82. 6. The certificates and keystores are now properly configured.
  83. Configure Wildlfy for mutual client SSL authentication
  84. ------------------------------------------------------
  85. 1. Open a command line and navigate to the JBoss server `configuration` directory:
  86. For Linux: JBOSS_HOME/standalone/configuration
  87. For Windows: JBOSS_HOME\standalone\configuration
  88. 2. Copy `RootCA.trustsore` and `server.keystore` (or `server.keystore` and `client.truststore`) into the JBoss server `configuration` directory.
  89. ### Configure The Additional WildFly Security Realm
  90. The next step is to configure the new keystore as a server identity for ssl in the WildFly security-realms section of the standalone.xml (if you're using -ha or other versions, edit those). Make sure to backup the file: `JBOSS_HOME/standalone/configuration/standalone.xml`
  91. In case keys and certificates have been generated using openSSL
  92. `keystore path` can be configured either via `RootCA.keystore` or `server.keystore`.
  93. <management>
  94. <security-realms>
  95. <security-realm name="UndertowRealm">
  96. <server-identities>
  97. <ssl>
  98. <keystore path="RootCA.keystore" relative-to="jboss.server.config.dir" keystore-password="keypassword" key-password="keypassword"/>
  99. </ssl>
  100. </server-identities>
  101. </security-realm>
  102. <authentication>
  103. <truststore path="RootCA.truststore" relative-to="jboss.server.config.dir" keystore-password="keypassword"/>
  104. <local default-user="$local" skip-group-loading="true"/>
  105. <properties path="mgmt-users.properties" relative-to="jboss.server.config.dir"/>
  106. </authentication>
  107. ...
  108. </management>
  109. else In case keys and certificates have been generated using java keytool
  110. <management>
  111. <security-realms>
  112. <security-realm name="UndertowRealm">
  113. <server-identities>
  114. <ssl>
  115. <keystore path="server.keystore" relative-to="jboss.server.config.dir" keystore-password="keypassword" key-password="keypassword"/>
  116. </ssl>
  117. </server-identities>
  118. </security-realm>
  119. <authentication>
  120. <truststore path="client.truststore" relative-to="jboss.server.config.dir" keystore-password="keypassword"/>
  121. <local default-user="$local" skip-group-loading="true"/>
  122. <properties path="mgmt-users.properties" relative-to="jboss.server.config.dir"/>
  123. </authentication>
  124. ...
  125. </management>
  126. ###Configure Undertow Subsystem for SSL
  127. If you're running with the default-server, add the https-listener to the undertow subsystem:
  128. <subsystem xmlns="urn:jboss:domain:undertow:2.0">
  129. <server name="default-server">
  130. <https-listener name="https" socket-binding="https" security-realm="UndertowRealm" verify-client="REQUIRED"/>
  131. ...
  132. ...
  133. </server>
  134. ...
  135. ...
  136. </subsystem>
  137. That's it, now we are ready to connect to the ssl port of our instance `https://localhost:8443/`. Note, that we get the privacy error as the server certificate is self signed. If we need to use a fully signed certificate, we mostly get a PEM file from the Certificate Authority. In such a case, we need to import the PEM into the keystore and truststore.
  138. Test the Server SSL Configuration
  139. ---------------------------------
  140. To test the SSL configuration, access: `<https://localhost:8443>`
  141. If it is configured correctly, you should be asked to trust the server certificate.
  142. Import the Certificate into Your Browser
  143. ---------------------------------------
  144. Before you access the application, you must import the *clientCert.p12*, which holds the client certificate, into your browser.
  145. #### Import the Certificate into Google Chrome
  146. 1. Click the Chrome menu icon (3 horizontal bars) in the upper right on the browser toolbar and choose 'Settings'. This takes you to <chrome://settings/>.
  147. 2. At the bottom of the page, click on the 'Show advanced settings...' link.
  148. 3. Find the section 'HTTPS/SSL' and click on the 'Manage certificates...' button.
  149. 4. In the 'Certificate manager' dialog box, choose the 'Your Certificates' tab and click the 'Import' button.
  150. 5. Select the `clientCert.p12` file. You will be prompted to enter the password: `keypassword`.
  151. 6. The certificate is now installed in the Google Chrome browser.
  152. #### Import the Certificate into Mozilla Firefox
  153. 1. Click the 'Edit' menu item on the browser menu and choose 'Preferences'.
  154. 2. A new window will open. Select the 'Advanced' icon and after that the 'Certificates' tab.
  155. 3. On the 'Certificates' tab, mark the option 'Ask me every time' and click the 'View Certificates' button.
  156. 4. A new window will open. Select the 'Your Certificates' tab and click the 'Import' button.
  157. 5. Select the `clientCert.p12` file. You will be prompted to enter the password: `keypassword`.
  158. 6. The certificate is now installed in the Mozilla Firefox browser.
  159. Start JBoss Enterprise Application Platform 6 or WildFly with the Web Profile
  160. ------------------------------------------------------------------------------
  161. 1. Open a command line and navigate to the root of the JBoss server directory.
  162. 2. The following shows the command line to start the server with the web profile:
  163. For Linux: JBOSS_HOME/bin/standalone.sh
  164. For Windows: JBOSS_HOME\bin\standalone.bat
  165. Build and Deploy the Quickstart
  166. -------------------------
  167. _NOTE: The following build command assumes you have configured your Maven user settings. If you have not, you must include Maven setting arguments on the command line. See [Build and Deploy the Quickstarts](../README.md#build-and-deploy-the-quickstarts) for complete instructions and additional options._
  168. 1. Make sure you have started the Wildfly Server as described above.
  169. 2. Open a command line and navigate to the root directory of one of the quickstart.
  170. 3. Type this command to build and deploy the archive:
  171. For EAP 6: mvn clean package jboss-as:deploy
  172. For WildFly: mvn -Pwildfly clean package wildfly:deploy
  173. 4. This will deploy `target/wildfly-helloworld-client-ssl.war` to the running instance of the server.
  174. Access the application
  175. ---------------------
  176. The application will be running at the following URL: `<https://localhost:8443/wildfly-helloworld-client-ssl>`.
  177. Undeploy the Archive
  178. --------------------
  179. 1. Make sure you have started the JBoss Server as described above.
  180. 2. Open a command line and navigate to the root directory of this quickstart.
  181. 3. When you are finished testing, type this command to undeploy the archive:
  182. For EAP 6: mvn jboss-as:undeploy
  183. For WildFly: mvn -Pwildfly wildfly:undeploy
  184. Remove the SSL Configuration
  185. ----------------------------
  186. 1. If the server is running, stop the JBoss Enterprise Application Platform 6 or WildFly Server.
  187. 2. Replace the `WILDFLY_HOME/standalone/configuration/standalone.xml` file with the back-up copy of the file.
  188. Run the Quickstart in JBoss Developer Studio or Eclipse
  189. -------------------------------------
  190. You can also start the server and deploy the quickstarts from Eclipse using JBoss tools.
  191. Debug the Application
  192. ------------------------------------
  193. If you want to debug the source code or look at the Javadocs of any library in the project, run either of the following commands to pull them into your local repository. The IDE should then detect them.
  194. mvn dependency:sources
  195. mvn dependency:resolve -Dclassifier=javadoc