October 22, 2017

ActiveMQ and the SSL Transport

In a previous article I’ve shown how to configure ActiveMQ for High Availability in a “shared storage master/slave” scenario. Now let’s see how you can communicate with ActiveMQ using the SSL transport. ActiveMQ uses the Java Secure Socket Extension (JSSE) to implement its SSL functionality so you must include SSL certificates for successful SSL communication. In this article, I will show an example of an SSL trusted client/server connection using either the Java SSL System Properties (directly in a Maven profile) or the Spring beans definition. Here the roadmap.

ROADMAP

1. Creating your own SSL certificates (keystore and truststore files)
2. Enabling the SSL transport for ActiveMQ with trusted clients
3. Setup clients using Java SSL System Properties in a Maven profile
4. Setup clients using Spring

1.  Creating your own SSL certificates (keystore and trustedstore files)

I have summed up the process of creating and sharing self-signed certificates using the command-line “keytool” distributed with Java.

 

2. Enabling the SSL transport for ActiveMQ with trusted clients

To configure the ActiveMQ broker to use the SSL transport, the first thing to do is to replace the default keystore and truststore files in the conf directory with the ones that were previously created.

Now change the <transportConnectors> element in the ${ACTIVEMQ_HOME}/conf/activemq.xml file and specify the “needClientAuth=true” parameter which instructs the broker to check connecting client certificates and allow access only to those that are found in the truststore. Edit the file  ${ACTIVEMQ_HOME}/conf/activemq.xml as below.

If no error occurs, you can see the following message at the ActiveMQ bootstrap.

 activemq-and-ssl-transport-screen01

 3. Setup clients using Java SSL System Properties in a Maven profile

The typical URI syntax for the SSL protocol is ssl://hostname:port?key=value. If you have configured a Master/Slave ActiveMQ instance for High Availability you must use a failover connection string like this failover:(ssl://amq01:61617,ssl://amq02:61617)?randomize=false. In my example I have used a ResouceBundle to externalize some properties like the broker connection string.

– activemq-samples/ssl-transport/src/main/resources/configuration.properties

– activemq-samples/ssl-transport/src/main/java/activemq/samples/ssltransport/Producer.java

When using JSSE, in order to successfully connect to the broker via SSL, you must provide  the keystore, the keystore password, and the truststore as system properties. You can set the JAVA_OPTS enviorment variable using the following java system properties.

To run clients with the Java SSL System Properties, I have used in this example the “exec-maven-plugin” in a dedicated Maven profile. Here is an example for the Producer.

– activemq-samples/ssl-transport/pom.xml

Compile the project source and run the maven “exec:java” goal

If no errors occur, you can see the SSL connection established message in the output console

activemq-and-ssl-transport-screen02

 4. Setup clients using Spring

If you want to use Spring for your clients, you can define your own JMS SSL Connection Factory using the org.apache.activemq.ActiveMQSslConnectionFactory object.

–  activemq-samples/ssl-transport/src/main/resources/SpringBeans-Producer.xml 

Related posts

18 Comments

  1. mailk

    Hi,

    I followed the step 1 and 2 to configure ssl in activemq. I can see the ssl started. While trying to access the broker with admin port like https://host:8161/admin getting error:

    Unable to make a secure connection to the server. This may be a problem with the server or it may be requiring a client authentication certificate that you don’t have.
    Error code: ERR_SSL_PROTOCOL_ERROR

    Sametime I can see below error in the log

    Transport Connection to: tcp://172.18.108.153:55482 failed: javax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake | org.apache.activemq.broker.TransportConnection.Transport | ActiveMQ Transport: ssl:///172.18.108.153:55482
    2014-05-01 01:45:14,510 | ERROR | Could not accept connection from tcp://172.18.108.153:55482: javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake | org.apache.activemq.broker.TransportConnector | ActiveMQ BrokerService[localhost] Task-1
    2014-05-01 01:45:14,535 | ERROR | Could not accept connection from tcp://172.18.108.153:55483: javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake | org.apache.activemq.broker.TransportConnector | ActiveMQ BrokerService[localhost] Task-2
    2014-05-01 01:45:14,941 | WARN | Transport Connection to: tcp://172.18.108.153:55484 failed: javax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLHandshakeException: null cert chain | org.apache.activemq.broker.TransportConnection.Transport | ActiveMQ Transport: ssl:///172.18.108.153:55484
    2014-05-01 01:45:14,943 | ERROR | Could not accept connection from tcp://172.18.108.153:55484: javax.net.ssl.SSLHandshakeException: null cert chain | org.apache.activemq.broker.TransportConnector | ActiveMQ BrokerService[localhost]

    Reply
    1. Giuseppe Urso

      Hi mailk,

      thanks for your comment.By default, the admin web panel of Activemq should be running on HTTP (not https), you can access it using a url like this:
      http://your_host:8161/admin

      The configuration I exposed in this article enables the ssl protocol for the ActiveMQConnectionFactory so the Broker can manage messages using a secure ssl transport connector.
      About the errors in the log, it seems there is a problem with the java SSLHandshake when a client tries to connect to the broker. You can deeply investigate the ssl handshake adding the following option when you run the client:

      -Djavax.net.debug=ssl,handshake,record

      Giuseppe

  2. Giuseppe Urso

    Thanks Thomas!
    In my work experience I’ve been involved with IBM WebSphere Business Integration 5.0 and the MQ-Series. But I’ve to admit that ActiveMQ represents a valid and reliable open-source alternative.

    Reply
  3. Cihat Imamoglu

    Thanks for the great article!

    -Djavax.net.ssl.trustStorePassword=${ACTIVEMQ_HOME}/conf/123456

    I believe you meant -Djavax.net.ssl.trustStorePassword=123456

    Reply
    1. Giuseppe Urso

      Thank you!

      You say right, it was a typo! The javax.net.ssl.trustStorePassword property must be set with the truststore’s password.

      Thanks for reporting I updated the post 😉

      Thank again Cihat
      Giuseppe

  4. Zach Jacobs

    I’m not familiar with Java so I’m not sure where you’re getting your examples starting in Step 3. Is this a project that you have cloned from somewhere?

    ie activemq-samples/ssl-transport/src/main/java/activemq/samples/ssltransport/Producer.java

    Reply
    1. Giuseppe Urso

      Hi Zach,

      thanks for you comment. This is because this post comes from my collection of sample projects now hosted on my github repositories, you can check it here:
      https://github.com/giuseppeurso-eu/activemq

      In this article I use a conventional name “activemq-samples” for the base directory in order to not confuse the reader. The focus of this post should be the ActiveMQ SSL configuration and the metodology to quick setup a maven-based client project. Starting from the Step 3, I provide an example of naming of source files in order to show a possible maven project layout.
      Giuseppe

  5. Renukaradhya

    Hi,
    I tried below steps and after starting activeMQ,its failing to start:
    1. Creating your own SSL certificates (keystore and trustedstore files)
    2. Enabling the SSL transport for ActiveMQ with trusted clients – i copied two files manually to conf folder of activeMQ

    My confihurations:


    | Loading message broker from: xbean:activemq.xml
    jvm 1 | INFO | Refreshing org.apache.activemq.xbean.XBeanBrokerFactory$1@53bfbef7: startup date [Sat May 21 20:33:30 GMT+05:30 2016]; root of context hierarchy
    jvm 1 | WARN | Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.apache.activemq.xbean.XBeanBrokerService#0' defined in class path resource [activemq.xml]: Cannot create inner bean '(inner bean)#1d45d7cf' of type [org.apache.activemq.spring.SpringSslContext] while setting bean property 'sslContext'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1d45d7cf' defined in class path resource [activemq.xml]: Invocation of init method failed; nested exception is java.security.UnrecoverableKeyException: Cannot recover key
    jvm 1 | ERROR | Failed to load: class path resource [activemq.xml], reason: Error creating bean with name 'org.apache.activemq.xbean.XBeanBrokerService#0' defined in class path resource [activemq.xml]: Cannot create inner bean '(inner bean)#1d45d7cf' of type [org.apache.activemq.spring.SpringSslContext] while setting bean property 'sslContext'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1d45d7cf' defined in class path resource [activemq.xml]: Invocation of init method failed; nested exception is java.security.UnrecoverableKeyException: Cannot recover key
    jvm 1 |

    Reply
    1. Giuseppe Urso

      Hi Renukaradhya,

      it seems something in your keystores is incorrect. Pay attention to the log snippet:

      "... nested exception is java.security.UnrecoverableKeyException: Cannot recover key..."

      This could mean something like this:
      – you had a bad password;
      – your keystore is corrupted somehow.
      Try to check the integrity of your keystore and password with keytool. Take a look at the other useful commands I’ve posted in the article and verify the key password is correct.

      Giuseppe

  6. Jay

    Hi Giuseppe,
    I have a JMS client which used to connect to a broker which did not have SSL configured and everything was working.
    Recently SSL has been enabled on the broker.
    When I asked the administrator for the certificate, he said that
    “we’re not doing mutual client authentication, so the client doesn’t need to have a certificate, it just has to know how to do an SSL handshake”
    Do you know if it is possible to connect to the broker over SSL without a certificate ?

    Thanks
    Jay

    Reply
    1. Giuseppe

      Hi Jay,

      it seems your administrator has not enabled the SSL/TLS two-way authentication (mutual authentication), this means the client only needs to import the broker certificate as a Trusted Certificate.
      First of all, make sure the broker’s SSL/TLS certificate is imported into the trust store of your CLIENT java virtual machine (you can download the broker certificate by using a browser or openssl command):

      ## Copy and paste the BEGIN/END part into a file broker.cert
      > openssl s_client -connect Broker_Host:SSL_Port
      ## Import broker cert into a trust store
      > keytool -import -alias broker-server -keystore client.ts -file broker.cert

      After importing the broker certificate into the truststore, you only need to run your JMS client specifying the truststore property (take a look at the Step 3 in the article):

      -Djavax.net.ssl.trustStore=your_path/client.ts

      Giuseppe

Leave a Reply

Your email address will not be published.