Skip to content

Latest commit

 

History

History
1019 lines (841 loc) · 44.2 KB

README.adoc

File metadata and controls

1019 lines (841 loc) · 44.2 KB

helloworld-mutual-ssl: WildFly Mutual TLS(two-way) Configuration Example

The helloworld-mutual-ssl quickstart is a basic example that demonstrates mutual TLS configuration in WildFly

What is it?

Mutual TLS provides the same security as TLS, with the addition of authentication and non-repudiation of the client authentication, using digital signatures. When mutual authentication is used, the server requests the client to provide a certificate in addition to the server certificate issued to the client. Mutual authentication requires an extra round trip each time for client certificate exchange. In addition, the client must obtain and maintain a digital certificate.

The out of the box configuration for WildFly has one-way TLS enabled by default. This quickstart shows how to configure WildFly with mutual (two-way) TLS authentication.

Before you run this example, you must create the client certificate and configure the server to use two-way TLS.

System Requirements

The application this project produces is designed to be run on WildFly Application Server 31 or later.

All you need to build this project is Java 11.0 (Java SDK 11) or later and Maven 3.6.0 or later. See Configure Maven to Build and Deploy the Quickstarts to make sure you are configured correctly for testing the quickstarts.

Use of the WILDFLY_HOME and QUICKSTART_HOME Variables

In the following instructions, replace WILDFLY_HOME with the actual path to your WildFly installation. The installation path is described in detail here: Use of WILDFLY_HOME and JBOSS_HOME Variables.

When you see the replaceable variable QUICKSTART_HOME, replace it with the path to the root directory of all of the quickstarts.

Set Up the Client Keystore Using Java Keytool

  1. Open a terminal and navigate to the WildFly server configuration directory:

    $ cd WILDFLY_HOME/standalone/configuration/
  2. Create the client certificate, which is used to authenticate against the server when accessing a resource through TLS.

    $>keytool -genkey -keystore client.keystore -storepass secret -validity 365 -keyalg RSA -keysize 2048 -storetype pkcs12
    
    What is your first and last name?
        [Unknown]:  quickstartUser
    What is the name of your organizational unit?
        [Unknown]:  Sales
    What is the name of your organization?
        [Unknown]:  My Company
    What is the name of your City or Locality?
        [Unknown]:  Sao Paulo
    What is the name of your State or Province?
        [Unknown]:  Sao Paulo
    What is the two-letter country code for this unit?
        [Unknown]:  BR
    Is CN=quickstartUser, OU=Sales, O=My Company, L=Sao Paulo, ST=Sao Paulo, C=BR correct?
        [no]:  yes
  3. Export the client certificate and create a truststore by importing this certificate:

    $>keytool -exportcert -keystore client.keystore  -storetype pkcs12 -storepass secret -keypass secret -file client.crt
    $>keytool -import -file client.crt -alias quickstartUser -keystore client.truststore -storepass secret
    
    Owner: CN=quickstartUser, OU=Sales, O=My Company, L=Sao Paulo, ST=Sao Paulo, C=BR
    Issuer: CN=quickstartUser, OU=Sales, O=My Company, L=Sao Paulo, ST=Sao Paulo, C=BR
    Serial number: 7fd95ce4
    Valid from: Mon Jul 24 16:14:03 BRT 2017 until: Tue Jul 24 16:14:03 BRT 2018
    Certificate fingerprints:
         MD5:  87:41:C5:CC:E6:79:91:F0:9D:90:AD:9E:DD:57:81:80
         SHA1: 55:35:CA:B0:DC:DD:4F:E6:B8:9F:45:4B:4B:98:93:B5:3B:7C:55:84
         SHA256: 0A:FC:93:B6:25:5A:74:42:B8:A1:C6:5F:69:88:72:7F:27:A9:81:B0:17:0C:F1:AF:3D:DE:B7:E5:F1:69:66:4B
         Signature algorithm name: SHA256withRSA
         Version: 3
    
    Extensions:
    
    #1: ObjectId: 2.5.29.14 Criticality=false
    SubjectKeyIdentifier [
    KeyIdentifier [
    0000: 95 84 BE C6 32 BB 2B 13   4C 7F 5D D4 C4 C8 22 12  ....2.+.L.]...".
    0010: CB 09 39 09                                        ..9.
    ]
    ]
    
    Trust this certificate? [no]:  yes
    Certificate was added to keystore
  4. Export client certificate to pkcs12 format

    $>keytool -importkeystore -srckeystore client.keystore -srcstorepass secret -destkeystore clientCert.p12 -srcstoretype PKCS12 -deststoretype PKCS12 -deststorepass secret
  5. The certificate and keystore are now properly configured.

Back Up the WildFly Standalone Server Configuration

Before you begin, back up your server configuration file.

  1. If it is running, stop the WildFly server.

  2. Back up the WILDFLY_HOME/standalone/configuration/standalone.xml file.

After you have completed testing this quickstart, you can replace this file to restore the server to its original configuration.

Start the WildFly Standalone Server

  1. Open a terminal and navigate to the root of the WildFly directory.

  2. Start the WildFly server with the default profile by typing the following command.

    $ WILDFLY_HOME/bin/standalone.sh 
    Note
    For Windows, use the WILDFLY_HOME\bin\standalone.bat script.

Configure the Server

You configure the SSL context by running JBoss CLI commands. For your convenience, this quickstart batches the commands into a configure-ssl.cli script provided in the root directory of this quickstart.

  1. Before you begin, make sure you do the following:

  2. Review the configure-ssl.cli file in the root of this quickstart directory. Comments in the script describe the purpose of each block of commands.

  3. Open a new terminal, navigate to the root directory of this quickstart, and run the following command, replacing WILDFLY_HOME with the path to your server:

    $ WILDFLY_HOME/bin/jboss-cli.sh --connect --file=configure-ssl.cli
    Note
    For Windows, use the WILDFLY_HOME\bin\jboss-cli.bat script.

    You should see the following result when you run the script:

    The batch executed successfully
    process-state: reload-required
  4. Stop the WildFly server.

Review the Modified Server Configuration

After stopping the server, open the WILDFLY_HOME/standalone/configuration/standalone.xml file and review the changes.

  1. The following keystore element was added to the elytron subsystem:

    <key-store name="qsTrustStore">
        <credential-reference clear-text="secret"/>
        <implementation type="JKS"/>
        <file path="client.truststore" relative-to="jboss.server.config.dir"/>
    </key-store>
  2. The following trust-manager was added to the elytron subsystem:

    <trust-managers>
        <trust-manager name="qsTrustManager" key-store="qsTrustStore"/>
    </trust-managers>
  3. The default ssl-context was updated to reference the trust-manager to enable two-way TLS:

    <server-ssl-contexts>
        <server-ssl-context name="applicationSSC" need-client-auth="true" key-manager="applicationKM" trust-manager="qsTrustManager"/>
    </server-ssl-contexts>

    Note that the https-listener in the undertow subsystem references the applicationSSC server-ssl-context by default.

Test the Server TLS Configuration

To test the TLS configuration, access: https://localhost:8443

If it is configured correctly, you should be asked to trust the server certificate.

Import the Client Certificate into Your Browser

Before you access the application, you must import the clientCert.p12, which holds the client certificate, into your browser.

Import the Client Certificate into Google Chrome

  1. Click the Chrome menu icon (3 dots) in the upper right on the browser toolbar and choose Settings. This takes you to link:`chrome://settings/.

  2. Click on Privacy and security and then on Security.

  3. Scroll down to the Advanced section and on the Manage certificates screen, select the Your Certificates tab and click on the Import button.

  4. Select the clientCert.p12 file. You will be prompted to enter the password: secret.

  5. The client certificate is now installed in the Google Chrome browser.

Import the Client Certificate into Mozilla Firefox

  1. Click the Edit menu item on the browser menu and choose Settings.

  2. A new window will open. Click on Privacy & Security and scroll down to the Certificates section.

  3. Click the View Certificates button.

  4. A new window will open. Select the Your Certificates tab and click the Import button.

  5. Select the clientCert.p12 file. You will be prompted to enter the password: secret.

  6. The certificate is now installed in the Mozilla Firefox browser.

Build and Deploy the Quickstart

  1. Make sure you start the WildFly server as described above.

  2. Open a terminal and navigate to the root directory of this quickstart.

  3. Type the following command to build the quickstart.

    $ mvn clean package
  4. Type the following command to deploy the quickstart.

    $ mvn wildfly:deploy

This deploys the helloworld-mutual-ssl/target/helloworld-mutual-ssl.war to the running instance of the server.

You should see a message in the server log indicating that the archive deployed successfully.

Access the Application

The application will be running at the following URL: https://localhost:8443/helloworld-mutual-ssl/HelloWorld.

A page displaying the client certificate should be visible:

Hello World ! WildFly mutual TLS is configured and client certificate is verified !!

Client Certificate Pem: MIIDhTCCAm2gAwIBAgIEf9lc5DANBgkqhkiG9w0BAQsFADBzMQswCQYDVQQGEwJCUjESMBAGA1UECBMJU2FvIFBhd
WxvMRIwEAYDVQQHEwlTYW8gUGF1bG8xEzARBgNVBAoTCk15IENvbXBhbnkxDjAMBgNVBAsTBVNhbGVzMRcwFQYDVQQDEw5xdWlja3N0YXJ0VXNlcj
AeFw0xNzA3MjQxOTE0MDNaFw0xODA3MjQxOTE0MDNaMHMxCzAJBgNVBAYTAkJSMRIwEAYDVQQIEwlTYW8gUGF1bG8xEjAQBgNVBAcTCVNhbyBQYXV
sbzETMBEGA1UEChMKTXkgQ29tcGFueTEOMAwGA1UECxMFU2FsZXMxFzAVBgNVBAMTDnF1aWNrc3RhcnRVc2VyMIIBIjANBgkqhkiG9w0BAQEFAAOC
AQ8AMIIBCgKCAQEAnHwflE8K/ArTPbTeZZEFK+1jtpg9grPSD62GIz/awoIDr6Rf9vCBTpAg4lom62A0BNZDEJKdab/ExNOOBRY+/pOnYlXZTYlDp
dQQap0E7UP5EfHNZsafgpfILCop2LdTuUbcV7tLKBsthJLJ0ZCoG5QJFble+OPxEbissOvIqHfvUJZi34k9ULteLJc330g0uTuDrLgtoFQ0cbHa4F
CQ86o85EuRPpFeW6EBA3iYE/tKHSYsK7QSajefX6jZjXoZiUflw97SAGL43ZtvNbrKRywEfsVqDpDurjBg2HI+YahuDz5R1QWTSyTHWMZzcyJYqxj
XiSf0oK1cUahn6m5t1wIDAQABoyEwHzAdBgNVHQ4EFgQUlYS+xjK7KxNMf13UxMgiEssJOQkwDQYJKoZIhvcNAQELBQADggEBADkp+R6kSNXJNfih
qbDRp3uFtNMG6OgaYsfC7RtNLMdrhvoLlU7uWzxVCFuifvNlWVRiADBHDCRQU2uNRFW35GQSfHQyok4KoBuKlfBtQ+Xu7c8R0JzxN/rPJPXoCbShz
DHo1uoz5/dzXZz0EjjWCPJk+LVEhEvH0GcWAp3x3irpNU4hRZLd0XomY0Z4NnUt7VMBNYDOxVxgT9qcLnEaEpIfYULubLLCFHwAga2YgsKzZYLuwM
aEWK4zhPVFynfnMaOxI67FC2QzhfzERyKqHj47WuwN0xWbS/1gBypS2nUwvItyxaEQG2X5uQY8j8QoY9wcMzIIkP2Mk14gJGHUnA8=

Undeploy the Quickstart

When you are finished testing the quickstart, follow these steps to undeploy the archive.

  1. Make sure you start the WildFly server as described above.

  2. Open a terminal and navigate to the root directory of this quickstart.

  3. Type this command to undeploy the archive:

    $ mvn wildfly:undeploy

Restore the WildFly Standalone Server Configuration

You can restore the original server configuration using either of the following methods.

Restore the WildFly Standalone Server Configuration by Running the JBoss CLI Script

  1. Start the WildFly server as described above.

  2. Open a new terminal, navigate to the root directory of this quickstart, and run the following command, replacing WILDFLY_HOME with the path to your server:

    $ WILDFLY_HOME/bin/jboss-cli.sh --connect --file=restore-configuration.cli
    Note
    For Windows, use the WILDFLY_HOME\bin\jboss-cli.bat script.

This script reverts the changes made to the undertow subsystem and it also removes the ssl-context, key-manager, trust-manager and key-store`s from the `elytron subsystem. You should see the following result when you run the script:

The batch executed successfully
process-state: reload-required

Restore the WildFly Standalone Server Configuration Manually

When you have completed testing the quickstart, you can restore the original server configuration by manually restoring the backup copy the configuration file.

  1. If it is running, stop the WildFly server.

  2. Replace the WILDFLY_HOME/standalone/configuration/standalone.xml file with the backup copy of the file.

Remove the keystores and certificates created for this quickstart

  1. Open a terminal and navigate to the WildFly server configuration directory:

    $ cd WILDFLY_HOME/standalone/configuration/
  2. Remove the clientCert.p12, client.crt, client.keystore, and client.truststore files that were generated for this quickstart.

Remove the Client Certificate from Your Browser

After you are done with this quickstart, remember to remove the certificate that was imported into your browser.

Remove the Client Certificate from Google Chrome

  1. Click the Chrome menu icon (3 dots) in the upper right on the browser toolbar and choose Settings. This takes you to chrome://settings/.

  2. Click on Privacy and security and then on Security.

  3. Scroll down to the Advanced section and on the Manage certificates screen, select the Your Certificates tab and click on the arrow to the right of the certificate to be removed.

  4. The certificate is expanded, displaying the quickstartUser entry. Click on the icon (3 dots) to the right of it and then select Delete.

  5. Confirm the deletion in the dialog box. The certificate has now been removed from the Google Chrome browser.

Remove the Client Certificate from Mozilla Firefox

  1. Click the Edit menu item on the browser menu and choose Preferences.

  2. A new window will open. Click on Privacy & Security and scroll down to the Certificates section.

  3. Click the View Certificates button.

  4. A new window will open. Select the Your Certificates tab.

  5. Select the quickstartUser certificate and click the Delete button.

  6. The certificate has now been removed from the Mozilla Firefox browser.

Run the Quickstart in Red Hat CodeReady Studio or Eclipse

You can also start the server and deploy the quickstarts or run the Arquillian tests in Red Hat CodeReady Studio or from Eclipse using JBoss tools. For general information about how to import a quickstart, add a WildFly server, and build and deploy a quickstart, see Use Red Hat CodeReady Studio or Eclipse to Run the Quickstarts.

Debug the Application

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.

$ mvn dependency:sources
$ mvn dependency:resolve -Dclassifier=javadoc