forked from apache/activemq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActiveMQSslConnectionFactoryTest.java
More file actions
456 lines (388 loc) · 18 KB
/
ActiveMQSslConnectionFactoryTest.java
File metadata and controls
456 lines (388 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.Socket;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.security.UnrecoverableKeyException;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.broker.SslBrokerService;
import org.apache.activemq.transport.Transport;
import org.apache.activemq.transport.TransportFilter;
import org.apache.activemq.transport.tcp.SslTransport;
import org.apache.activemq.transport.tcp.TcpTransport;
import org.apache.activemq.test.annotations.ParallelTest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertArrayEquals;
import org.junit.experimental.categories.Category;
@Category(ParallelTest.class)
public class ActiveMQSslConnectionFactoryTest extends CombinationTestSupport {
private static final Log LOG = LogFactory.getLog(ActiveMQSslConnectionFactoryTest.class);
public static final String KEYSTORE_TYPE = "jks";
public static final String PASSWORD = "password";
public static final String SERVER_KEYSTORE = "src/test/resources/server.keystore";
public static final String TRUST_KEYSTORE = "src/test/resources/client.keystore";
private ActiveMQConnection connection;
private BrokerService broker;
private String actualBrokerUri;
@Override
protected void tearDown() throws Exception {
// Try our best to close any previously opend connection.
try {
connection.close();
} catch (Throwable ignore) {
}
// Try our best to stop any previously started broker.
try {
broker.stop();
} catch (Throwable ignore) {
}
}
public void testCreateTcpConnectionUsingKnownPort() throws Exception {
// Control case: check that the factory can create an ordinary (non-ssl) connection.
broker = createBroker("tcp://localhost:0?wireFormat.tcpNoDelayEnabled=true");
// This should create the connection.
ActiveMQSslConnectionFactory cf = getFactory(actualBrokerUri);
connection = (ActiveMQConnection)cf.createConnection();
assertNotNull(connection);
connection.start();
connection.stop();
brokerStop();
}
public void testCreateTcpConnectionWithSocketParameters() throws Exception {
// Control case: check that the factory can create an ordinary (non-ssl) connection.
String tcpUri = "tcp://localhost:0";
broker = createBroker(tcpUri);
// Socket parameters must be set on the CLIENT URI, not the server URI.
// The broker's publishable URI doesn't include socket.* parameters as those are server-side configs.
String clientUri = actualBrokerUri + "?socket.OOBInline=true&socket.keepAlive=true&tcpNoDelay=true";
ActiveMQSslConnectionFactory cf = getFactory(clientUri);
connection = (ActiveMQConnection)cf.createConnection();
assertNotNull(connection);
Socket socket = getSocketFromConnection(connection);
assertTrue(socket.getOOBInline());
assertTrue(socket.getKeepAlive());
assertTrue(socket.getTcpNoDelay());
connection.start();
connection.stop();
brokerStop();
}
public void testCreateFailoverTcpConnectionUsingKnownPort() throws Exception {
// Control case: check that the factory can create an ordinary (non-ssl) connection.
broker = createBroker("tcp://localhost:0?wireFormat.tcpNoDelayEnabled=true");
// This should create the connection using the actual bound URI.
ActiveMQSslConnectionFactory cf = getFactory("failover:(" + actualBrokerUri + "?wireFormat.tcpNoDelayEnabled=true)");
connection = (ActiveMQConnection)cf.createConnection();
assertNotNull(connection);
connection.start();
connection.stop();
brokerStop();
}
public void testCreateSslConnection() throws Exception {
// Create SSL/TLS connection with trusted cert from truststore.
String sslUri = "ssl://localhost:0";
broker = createSslBroker(sslUri);
assertNotNull(broker);
// This should create the connection using the actual bound URI.
ActiveMQSslConnectionFactory cf = getFactory(actualBrokerUri);
cf.setTrustStore("server.keystore");
cf.setTrustStorePassword("password");
connection = (ActiveMQConnection)cf.createConnection();
LOG.info("Created client connection");
assertNotNull(connection);
connection.start();
connection.stop();
brokerStop();
}
public void testCreateSslConnectionWithSocketParameters() throws Exception {
// Create SSL/TLS connection with trusted cert from truststore.
String sslUri = "ssl://localhost:0?socket.enabledProtocols=TLSv1.3&socket.enableSessionCreation=true&socket.needClientAuth=true";
broker = createSslBroker(sslUri);
assertNotNull(broker);
// This should create the connection using the actual bound URI.
ActiveMQSslConnectionFactory cf = getFactory(actualBrokerUri + "?socket.enabledProtocols=TLSv1.3&socket.enableSessionCreation=true&socket.needClientAuth=true");
cf.setTrustStore("server.keystore");
cf.setTrustStorePassword("password");
connection = (ActiveMQConnection)cf.createConnection();
assertNotNull(connection);
SSLSocket socket = (SSLSocket) getSocketFromConnection(connection);
String[] expectedProtocols = {"TLSv1.3"};
assertArrayEquals(expectedProtocols, socket.getEnabledProtocols());
assertTrue(socket.getEnableSessionCreation());
assertTrue(socket.getNeedClientAuth());
connection.start();
connection.stop();
brokerStop();
}
public void testCreateSslConnectionKeyStore() throws Exception {
// Create SSL/TLS connection with trusted cert from truststore.
String sslUri = "ssl://localhost:0";
broker = createSslBroker(sslUri);
assertNotNull(broker);
// This should create the connection using the actual bound URI.
ActiveMQSslConnectionFactory cf = getFactory(actualBrokerUri);
cf.setKeyStore("server.keystore");
cf.setKeyStorePassword("password");
cf.setTrustStore("server.keystore");
cf.setTrustStorePassword("password");
connection = (ActiveMQConnection)cf.createConnection();
LOG.info("Created client connection");
assertNotNull(connection);
connection.start();
connection.stop();
brokerStop();
}
public void testFailoverSslConnection() throws Exception {
// Create SSL/TLS connection with trusted cert from truststore.
String sslUri = "ssl://localhost:0";
broker = createSslBroker(sslUri);
assertNotNull(broker);
// This should create the connection using the actual bound URI.
ActiveMQSslConnectionFactory cf = getFactory("failover:(" + actualBrokerUri + ")?maxReconnectAttempts=4");
cf.setTrustStore("server.keystore");
cf.setTrustStorePassword("password");
connection = (ActiveMQConnection)cf.createConnection();
LOG.info("Created client connection");
assertNotNull(connection);
connection.start();
connection.stop();
brokerStop();
}
public void testFailoverSslConnectionWithKeyAndTrustManagers() throws Exception {
String sslUri = "ssl://localhost:0";
broker = createSslBroker(sslUri);
assertNotNull(broker);
ActiveMQSslConnectionFactory cf = getFactory("failover:(" + actualBrokerUri + ")?maxReconnectAttempts=4");
cf.setKeyAndTrustManagers(getKeyManager(), getTrustManager(), new SecureRandom());
connection = (ActiveMQConnection)cf.createConnection();
LOG.info("Created client connection");
assertNotNull(connection);
connection.start();
connection.stop();
brokerStop();
}
public void testNegativeCreateSslConnectionWithWrongTrustStorePassword() throws Exception {
// Create SSL/TLS connection with trusted cert from truststore.
String sslUri = "ssl://localhost:0";
broker = createSslBroker(sslUri);
assertNotNull(broker);
// This should FAIL to connect, due to wrong password.
ActiveMQSslConnectionFactory cf = getFactory(actualBrokerUri);
cf.setTrustStore("server.keystore");
cf.setTrustStorePassword("wrongPassword");
try {
connection = (ActiveMQConnection)cf.createConnection();
}
catch (jakarta.jms.JMSException ignore) {
//Make sure it's an UnrecoverableKeyException
assertTrue(getRootCause(ignore) instanceof UnrecoverableKeyException);
// Expected exception
LOG.info("Expected java.io.Exception [" + ignore + "]");
}
assertNull(connection);
brokerStop();
}
public void testCreateSslConnectionWithNullTrustStorePassword() throws Exception {
// Create SSL/TLS connection with trusted cert from truststore.
String sslUri = "ssl://localhost:0";
broker = createSslBroker(sslUri);
assertNotNull(broker);
// This should create the connection.
ActiveMQSslConnectionFactory cf = getFactory(actualBrokerUri);
cf.setTrustStore("server.keystore");
//don't set a truststore password so it's null, this caused an NPE
//before AMQ-8550. truststore password is used to protect the integrity
//but isn't needed to read the truststore
connection = (ActiveMQConnection)cf.createConnection();
LOG.info("Created client connection");
assertNotNull(connection);
connection.start();
connection.stop();
brokerStop();
}
public void testNegativeCreateSslConnectionWithWrongKeyStorePassword() throws Exception {
// Create SSL/TLS connection with keystore and trusted cert from truststore.
String sslUri = "ssl://localhost:0";
broker = createSslBroker(sslUri);
assertNotNull(broker);
// This should FAIL to connect, due to wrong keystore password.
ActiveMQSslConnectionFactory cf = getFactory(actualBrokerUri);
cf.setKeyStore("server.keystore");
cf.setKeyStorePassword("badPassword");
cf.setTrustStore("server.keystore");
cf.setTrustStorePassword("password");
try {
connection = (ActiveMQConnection)cf.createConnection();
}
catch (jakarta.jms.JMSException ignore) {
//Make sure it's an UnrecoverableKeyException
assertTrue(getRootCause(ignore) instanceof UnrecoverableKeyException);
// Expected exception
LOG.info("Expected java.io.Exception [" + ignore + "]");
}
assertNull(connection);
brokerStop();
}
public void testNegativeCreateSslConnectionWithNullKeyStorePassword() throws Exception {
// Create SSL/TLS connection with keystore and trusted cert from truststore.
String sslUri = "ssl://localhost:0";
broker = createSslBroker(sslUri);
assertNotNull(broker);
// This should FAIL to connect, due to null password for keystore.
//Before AMQ-8550 this would fail with a NPE
ActiveMQSslConnectionFactory cf = getFactory(actualBrokerUri);
cf.setKeyStore("server.keystore");
//don't set keystore password so it's null
cf.setTrustStore("server.keystore");
cf.setTrustStorePassword("password");
try {
connection = (ActiveMQConnection)cf.createConnection();
}
catch (jakarta.jms.JMSException ignore) {
//Make sure it's an UnrecoverableKeyException and not NullPointerException
assertTrue(getRootCause(ignore) instanceof UnrecoverableKeyException);
// Expected exception
LOG.info("Expected java.io.Exception [" + ignore + "]");
}
assertNull(connection);
brokerStop();
}
public void testNegativeCreateSslConnectionWithWrongCert() throws Exception {
// Create SSL/TLS connection with trusted cert from truststore.
String sslUri = "ssl://localhost:0";
broker = createSslBroker(sslUri);
assertNotNull(broker);
// This should FAIL to connect, due to wrong password.
ActiveMQSslConnectionFactory cf = getFactory(actualBrokerUri);
cf.setTrustStore("dummy.keystore");
cf.setTrustStorePassword("password");
try {
connection = (ActiveMQConnection)cf.createConnection();
}
catch (jakarta.jms.JMSException ignore) {
// Expected exception
LOG.info("Expected SSLHandshakeException [" + ignore + "]");
}
assertNull(connection);
brokerStop();
}
protected BrokerService createBroker(String uri) throws Exception {
// Start up a broker with a tcp connector.
BrokerService service = new BrokerService();
service.setPersistent(false);
service.setUseJmx(false);
service.addConnector(uri);
service.start();
// Get the actual bound URI if ephemeral port was used
actualBrokerUri = service.getTransportConnectors().get(0).getPublishableConnectString();
return service;
}
protected BrokerService createSslBroker(String uri) throws Exception {
// http://java.sun.com/javase/javaseforbusiness/docs/TLSReadme.html
// work around: javax.net.ssl.SSLHandshakeException: renegotiation is not allowed
//System.setProperty("sun.security.ssl.allowUnsafeRenegotiation", "true");
SslBrokerService service = new SslBrokerService();
service.setPersistent(false);
KeyManager[] km = getKeyManager();
TrustManager[] tm = getTrustManager();
service.addSslConnector(uri, km, tm, null);
service.start();
// Get the actual bound URI if ephemeral port was used
actualBrokerUri = service.getTransportConnectors().get(0).getPublishableConnectString();
return service;
}
protected void brokerStop() throws Exception {
broker.stop();
}
protected ActiveMQSslConnectionFactory getFactory(String uri) {
return new ActiveMQSslConnectionFactory(uri);
}
public static TrustManager[] getTrustManager() throws Exception {
TrustManager[] trustStoreManagers = null;
KeyStore trustedCertStore = KeyStore.getInstance(ActiveMQSslConnectionFactoryTest.KEYSTORE_TYPE);
trustedCertStore.load(new FileInputStream(ActiveMQSslConnectionFactoryTest.TRUST_KEYSTORE), null);
TrustManagerFactory tmf =
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(trustedCertStore);
trustStoreManagers = tmf.getTrustManagers();
return trustStoreManagers;
}
public static KeyManager[] getKeyManager() throws Exception {
KeyManagerFactory kmf =
KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
KeyStore ks = KeyStore.getInstance(ActiveMQSslConnectionFactoryTest.KEYSTORE_TYPE);
KeyManager[] keystoreManagers = null;
byte[] sslCert = loadClientCredential(ActiveMQSslConnectionFactoryTest.SERVER_KEYSTORE);
if (sslCert != null && sslCert.length > 0) {
ByteArrayInputStream bin = new ByteArrayInputStream(sslCert);
ks.load(bin, ActiveMQSslConnectionFactoryTest.PASSWORD.toCharArray());
kmf.init(ks, ActiveMQSslConnectionFactoryTest.PASSWORD.toCharArray());
keystoreManagers = kmf.getKeyManagers();
}
return keystoreManagers;
}
private static byte[] loadClientCredential(String fileName) throws IOException {
if (fileName == null) {
return null;
}
FileInputStream in = new FileInputStream(fileName);
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[512];
int i = in.read(buf);
while (i > 0) {
out.write(buf, 0, i);
i = in.read(buf);
}
in.close();
return out.toByteArray();
}
private static Throwable getRootCause(Throwable throwable) {
Throwable cause = throwable.getCause();
Throwable rootCause = null;
while (cause != null) {
rootCause = cause;
cause = cause.getCause();
}
return rootCause;
}
private Socket getSocketFromConnection(ActiveMQConnection connection) throws Exception {
Transport transport = connection.getTransport();
while(!(transport instanceof TcpTransport)) {
transport = ((TransportFilter) transport).getNext();
}
Class<?> transportClass = transport.getClass();
if (transport instanceof SslTransport) {
transportClass = transportClass.getSuperclass();
}
Field socket = transportClass.getDeclaredField("socket");
socket.setAccessible(true);
return (Socket) socket.get(transport);
}
}