| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
package org.apache.qpid.proton.systemtests.engine; |
| 20 |
|
| 21 |
import static org.apache.qpid.proton.engine.EndpointState.ACTIVE; |
| 22 |
import static org.apache.qpid.proton.engine.EndpointState.UNINITIALIZED; |
| 23 |
import static org.junit.Assert.assertEquals; |
| 24 |
import static org.junit.Assert.assertNull; |
| 25 |
import static org.junit.Assert.assertTrue; |
| 26 |
import static org.junit.Assert.fail; |
| 27 |
|
| 28 |
import java.io.File; |
| 29 |
import java.io.FileInputStream; |
| 30 |
import java.io.InputStream; |
| 31 |
import java.security.KeyStore; |
| 32 |
import java.security.SecureRandom; |
| 33 |
|
| 34 |
import javax.net.ssl.KeyManagerFactory; |
| 35 |
import javax.net.ssl.SSLContext; |
| 36 |
import javax.net.ssl.SSLException; |
| 37 |
import javax.net.ssl.SSLHandshakeException; |
| 38 |
import javax.net.ssl.TrustManagerFactory; |
| 39 |
|
| 40 |
import org.apache.qpid.proton.Proton; |
| 41 |
import org.apache.qpid.proton.amqp.transport.ErrorCondition; |
| 42 |
import org.apache.qpid.proton.engine.Connection; |
| 43 |
import org.apache.qpid.proton.engine.Endpoint; |
| 44 |
import org.apache.qpid.proton.engine.EndpointState; |
| 45 |
import org.apache.qpid.proton.engine.SslDomain; |
| 46 |
import org.apache.qpid.proton.engine.SslDomain.Mode; |
| 47 |
import org.apache.qpid.proton.engine.SslDomain.VerifyMode; |
| 48 |
import org.apache.qpid.proton.engine.SslPeerDetails; |
| 49 |
import org.apache.qpid.proton.engine.Transport; |
| 50 |
import org.apache.qpid.proton.engine.TransportException; |
| 51 |
import org.junit.Test; |
| 52 |
|
| 53 |
public class SslTest |
| 54 |
{ |
| 55 |
private static final String SERVER_JKS_KEYSTORE = "src/test/resources/server-jks.keystore"; |
| 56 |
private static final String SERVER_JKS_TRUSTSTORE = "src/test/resources/server-jks.truststore"; |
| 57 |
private static final String CLIENT_JKS_KEYSTORE = "src/test/resources/client-jks.keystore"; |
| 58 |
private static final String CLIENT_JKS_TRUSTSTORE = "src/test/resources/client-jks.truststore"; |
| 59 |
private static final String PASSWORD = "password"; |
| 60 |
|
| 61 |
private static final String SERVER_2_JKS_KEYSTORE = "src/test/resources/server2-jks.keystore"; |
| 62 |
private static final String CA_CERTS = "src/test/resources/ca-certs.crt"; |
| 63 |
|
| 64 |
private static final String SERVER_CONTAINER = "serverContainer"; |
| 65 |
private static final String CLIENT_CONTAINER = "clientContainer"; |
| 66 |
|
| 67 |
private final Transport _clientTransport = Proton.transport(); |
| 68 |
private final Transport _serverTransport = Proton.transport(); |
| 69 |
|
| 70 |
private final TransportPumper _pumper = new TransportPumper(_clientTransport, _serverTransport); |
| 71 |
|
| 72 |
private final Connection _clientConnection = Proton.connection(); |
| 73 |
private final Connection _serverConnection = Proton.connection(); |
| 74 |
|
| 75 |
@Test |
| 76 |
public void testFailureToInitSslDomainThrowsISE() throws Exception { |
| 77 |
SslDomain sslDomain = SslDomain.Factory.create(); |
| 78 |
|
| 79 |
try { |
| 80 |
_clientTransport.ssl(sslDomain, null); |
| 81 |
fail("Expected an exception to be thrown"); |
| 82 |
} catch (IllegalStateException ise) { |
| 83 |
// Expected |
| 84 |
assertTrue(ise.getMessage().contains("Client/server mode must be configured")); |
| 85 |
} |
| 86 |
|
| 87 |
try { |
| 88 |
_serverTransport.ssl(sslDomain); |
| 89 |
fail("Expected an exception to be thrown"); |
| 90 |
} catch (IllegalStateException ise) { |
| 91 |
// Expected |
| 92 |
assertTrue(ise.getMessage().contains("Client/server mode must be configured")); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
@Test |
| 97 |
public void testOpenConnectionsWithProvidedSslContext() throws Exception |
| 98 |
{ |
| 99 |
SslDomain clientSslDomain = SslDomain.Factory.create(); |
| 100 |
clientSslDomain.init(Mode.CLIENT); |
| 101 |
clientSslDomain.setPeerAuthentication(VerifyMode.VERIFY_PEER); |
| 102 |
SSLContext clientSslContext = createSslContext(CLIENT_JKS_KEYSTORE, PASSWORD, CLIENT_JKS_TRUSTSTORE, PASSWORD); |
| 103 |
clientSslDomain.setSslContext(clientSslContext); |
| 104 |
_clientTransport.ssl(clientSslDomain); |
| 105 |
|
| 106 |
SslDomain serverSslDomain = SslDomain.Factory.create(); |
| 107 |
serverSslDomain.init(Mode.SERVER); |
| 108 |
serverSslDomain.setPeerAuthentication(VerifyMode.VERIFY_PEER); |
| 109 |
SSLContext serverSslContext = createSslContext(SERVER_JKS_KEYSTORE, PASSWORD, SERVER_JKS_TRUSTSTORE, PASSWORD); |
| 110 |
serverSslDomain.setSslContext(serverSslContext); |
| 111 |
_serverTransport.ssl(serverSslDomain); |
| 112 |
|
| 113 |
_clientConnection.setContainer(CLIENT_CONTAINER); |
| 114 |
_serverConnection.setContainer(SERVER_CONTAINER); |
| 115 |
|
| 116 |
_clientTransport.bind(_clientConnection); |
| 117 |
_serverTransport.bind(_serverConnection); |
| 118 |
|
| 119 |
assertConditions(_clientTransport); |
| 120 |
assertConditions(_serverTransport); |
| 121 |
|
| 122 |
_clientConnection.open(); |
| 123 |
|
| 124 |
assertEndpointState(_clientConnection, ACTIVE, UNINITIALIZED); |
| 125 |
assertEndpointState(_serverConnection, UNINITIALIZED, UNINITIALIZED); |
| 126 |
|
| 127 |
assertConditions(_clientTransport); |
| 128 |
assertConditions(_serverTransport); |
| 129 |
|
| 130 |
_pumper.pumpAll(); |
| 131 |
|
| 132 |
assertEndpointState(_clientConnection, ACTIVE, UNINITIALIZED); |
| 133 |
assertEndpointState(_serverConnection, UNINITIALIZED, ACTIVE); |
| 134 |
|
| 135 |
assertConditions(_clientTransport); |
| 136 |
assertConditions(_serverTransport); |
| 137 |
|
| 138 |
_serverConnection.open(); |
| 139 |
|
| 140 |
assertEndpointState(_clientConnection, ACTIVE, UNINITIALIZED); |
| 141 |
assertEndpointState(_serverConnection, ACTIVE, ACTIVE); |
| 142 |
|
| 143 |
assertConditions(_clientTransport); |
| 144 |
assertConditions(_serverTransport); |
| 145 |
|
| 146 |
_pumper.pumpAll(); |
| 147 |
|
| 148 |
assertEndpointState(_clientConnection, ACTIVE, ACTIVE); |
| 149 |
assertEndpointState(_serverConnection, ACTIVE, ACTIVE); |
| 150 |
|
| 151 |
assertConditions(_clientTransport); |
| 152 |
assertConditions(_serverTransport); |
| 153 |
} |
| 154 |
|
| 155 |
@Test |
| 156 |
public void testHostnameVerificationSuccess() throws Exception { |
| 157 |
doHostnameVerificationTestImpl(true, true); |
| 158 |
} |
| 159 |
|
| 160 |
@Test |
| 161 |
public void testHostnameVerificationFailure() throws Exception { |
| 162 |
doHostnameVerificationTestImpl(false, true); |
| 163 |
} |
| 164 |
|
| 165 |
@Test |
| 166 |
public void testHostnameVerificationSkipped() throws Exception { |
| 167 |
doHostnameVerificationTestImpl(false, false); |
| 168 |
} |
| 169 |
|
| 170 |
private void doHostnameVerificationTestImpl(boolean useMatchingServerName, boolean useHostnameVerification) throws Exception { |
| 171 |
final String serverPeerName = useMatchingServerName ? "localhost" : "anotherserverhost"; |
| 172 |
final VerifyMode clientVerifyMode = useHostnameVerification ? VerifyMode.VERIFY_PEER_NAME : VerifyMode.VERIFY_PEER; |
| 173 |
|
| 174 |
SslDomain clientSslDomain = SslDomain.Factory.create(); |
| 175 |
clientSslDomain.init(Mode.CLIENT); |
| 176 |
clientSslDomain.setPeerAuthentication(clientVerifyMode); |
| 177 |
|
| 178 |
SSLContext clientSslContext = createSslContext(CLIENT_JKS_KEYSTORE, PASSWORD, CLIENT_JKS_TRUSTSTORE, PASSWORD); |
| 179 |
clientSslDomain.setSslContext(clientSslContext); |
| 180 |
SslPeerDetails sslPeerDetails = SslPeerDetails.Factory.create(serverPeerName, 1234); |
| 181 |
_clientTransport.ssl(clientSslDomain, sslPeerDetails); |
| 182 |
|
| 183 |
SslDomain serverSslDomain = SslDomain.Factory.create(); |
| 184 |
serverSslDomain.init(Mode.SERVER); |
| 185 |
serverSslDomain.setPeerAuthentication(VerifyMode.VERIFY_PEER_NAME); |
| 186 |
SSLContext serverSslContext = createSslContext(SERVER_JKS_KEYSTORE, PASSWORD, SERVER_JKS_TRUSTSTORE, PASSWORD); |
| 187 |
serverSslDomain.setSslContext(serverSslContext); |
| 188 |
_serverTransport.ssl(serverSslDomain, SslPeerDetails.Factory.create("client", 4567)); |
| 189 |
|
| 190 |
_clientConnection.setContainer(CLIENT_CONTAINER); |
| 191 |
_serverConnection.setContainer(SERVER_CONTAINER); |
| 192 |
|
| 193 |
_clientTransport.bind(_clientConnection); |
| 194 |
_serverTransport.bind(_serverConnection); |
| 195 |
|
| 196 |
assertConditions(_clientTransport); |
| 197 |
assertConditions(_serverTransport); |
| 198 |
|
| 199 |
_clientConnection.open(); |
| 200 |
|
| 201 |
assertEndpointState(_clientConnection, ACTIVE, UNINITIALIZED); |
| 202 |
assertEndpointState(_serverConnection, UNINITIALIZED, UNINITIALIZED); |
| 203 |
|
| 204 |
assertConditions(_clientTransport); |
| 205 |
assertConditions(_serverTransport); |
| 206 |
|
| 207 |
if(useHostnameVerification && !useMatchingServerName) { |
| 208 |
// Verify the expected failures and resulting transport closures |
| 209 |
pumpWithFailingNegotiation(); |
| 210 |
|
| 211 |
assertEquals(Transport.END_OF_STREAM, _clientTransport.pending()); |
| 212 |
assertEquals(Transport.END_OF_STREAM, _clientTransport.capacity()); |
| 213 |
|
| 214 |
assertEquals(Transport.END_OF_STREAM, _serverTransport.pending()); |
| 215 |
assertEquals(Transport.END_OF_STREAM, _serverTransport.capacity()); |
| 216 |
return; |
| 217 |
} else { |
| 218 |
// Verify the connections succeed |
| 219 |
_pumper.pumpAll(); |
| 220 |
|
| 221 |
assertEndpointState(_clientConnection, ACTIVE, UNINITIALIZED); |
| 222 |
assertEndpointState(_serverConnection, UNINITIALIZED, ACTIVE); |
| 223 |
|
| 224 |
assertConditions(_clientTransport); |
| 225 |
assertConditions(_serverTransport); |
| 226 |
|
| 227 |
_serverConnection.open(); |
| 228 |
|
| 229 |
assertEndpointState(_clientConnection, ACTIVE, UNINITIALIZED); |
| 230 |
assertEndpointState(_serverConnection, ACTIVE, ACTIVE); |
| 231 |
|
| 232 |
assertConditions(_clientTransport); |
| 233 |
assertConditions(_serverTransport); |
| 234 |
|
| 235 |
_pumper.pumpAll(); |
| 236 |
|
| 237 |
assertEndpointState(_clientConnection, ACTIVE, ACTIVE); |
| 238 |
assertEndpointState(_serverConnection, ACTIVE, ACTIVE); |
| 239 |
|
| 240 |
assertConditions(_clientTransport); |
| 241 |
assertConditions(_serverTransport); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
@Test |
| 246 |
public void testOmittingPeerDetailsThrowsIAEWhenRequired() throws Exception { |
| 247 |
doOmitPeerDetailsThrowsIAEWhenRequiredTestImpl(true); |
| 248 |
doOmitPeerDetailsThrowsIAEWhenRequiredTestImpl(false); |
| 249 |
} |
| 250 |
|
| 251 |
private void doOmitPeerDetailsThrowsIAEWhenRequiredTestImpl(boolean explicitlySetVerifyMode) { |
| 252 |
SslDomain clientSslDomain = SslDomain.Factory.create(); |
| 253 |
clientSslDomain.init(Mode.CLIENT); |
| 254 |
|
| 255 |
if (explicitlySetVerifyMode) { |
| 256 |
clientSslDomain.setPeerAuthentication(VerifyMode.VERIFY_PEER_NAME); |
| 257 |
} |
| 258 |
|
| 259 |
try { |
| 260 |
_clientTransport.ssl(clientSslDomain, null); |
| 261 |
fail("Expected an exception to be thrown"); |
| 262 |
} catch (IllegalArgumentException ise) { |
| 263 |
// Expected |
| 264 |
} |
| 265 |
|
| 266 |
try { |
| 267 |
_clientTransport.ssl(clientSslDomain); |
| 268 |
fail("Expected an exception to be thrown"); |
| 269 |
} catch (IllegalArgumentException ise) { |
| 270 |
// Expected |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
private void pumpWithFailingNegotiation() throws Exception { |
| 275 |
try { |
| 276 |
_pumper.pumpAll(); |
| 277 |
fail("Expected an exception"); |
| 278 |
} catch (TransportException te) { |
| 279 |
assertTrue(te.getCause().getCause() instanceof SSLHandshakeException); |
| 280 |
} |
| 281 |
|
| 282 |
try { |
| 283 |
_pumper.pumpAll(); |
| 284 |
fail("Expected an exception"); |
| 285 |
} catch (TransportException te) { |
| 286 |
assertTrue(te.getCause().getCause() instanceof SSLException); |
| 287 |
} |
| 288 |
|
| 289 |
_pumper.pumpAll(); |
| 290 |
} |
| 291 |
|
| 292 |
private void assertConditions(Transport transport) |
| 293 |
{ |
| 294 |
ErrorCondition remoteCondition = transport.getRemoteCondition(); |
| 295 |
if (remoteCondition != null) |
| 296 |
{ |
| 297 |
assertNull(remoteCondition.getCondition()); |
| 298 |
} |
| 299 |
|
| 300 |
ErrorCondition condition = transport.getCondition(); |
| 301 |
if (condition != null) |
| 302 |
{ |
| 303 |
assertNull(condition.getCondition()); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
private SSLContext createSslContext(String keyStoreLocation, String keyStorePassword, |
| 308 |
String trustStoreLocation, String trustStorePassword) throws Exception |
| 309 |
{ |
| 310 |
SSLContext context = SSLContext.getInstance("TLS"); |
| 311 |
|
| 312 |
KeyManagerFactory keyFact = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); |
| 313 |
TrustManagerFactory trustFact = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| 314 |
|
| 315 |
KeyStore keyStore = KeyStore.getInstance("JKS"); |
| 316 |
try (InputStream in = new FileInputStream(new File(keyStoreLocation));) |
| 317 |
{ |
| 318 |
keyStore.load(in, keyStorePassword.toCharArray()); |
| 319 |
} |
| 320 |
keyFact.init(keyStore, keyStorePassword.toCharArray()); |
| 321 |
|
| 322 |
KeyStore trustStore = KeyStore.getInstance("JKS"); |
| 323 |
try (InputStream in = new FileInputStream(new File(trustStoreLocation));) |
| 324 |
{ |
| 325 |
trustStore.load(in, trustStorePassword.toCharArray()); |
| 326 |
} |
| 327 |
trustFact.init(trustStore); |
| 328 |
|
| 329 |
context.init(keyFact.getKeyManagers(), trustFact.getTrustManagers(), new SecureRandom()); |
| 330 |
|
| 331 |
return context; |
| 332 |
} |
| 333 |
|
| 334 |
private void assertEndpointState(Endpoint endpoint, EndpointState localState, EndpointState remoteState) |
| 335 |
{ |
| 336 |
assertEquals("Unexpected local state", localState, endpoint.getLocalState()); |
| 337 |
assertEquals("Unexpected remote state", remoteState, endpoint.getRemoteState()); |
| 338 |
} |
| 339 |
|
| 340 |
@Test |
| 341 |
public void testMultiplePemTrustCertificates() throws Exception |
| 342 |
{ |
| 343 |
doMultiplePemTrustCertificatesTestImpl(SERVER_JKS_KEYSTORE); |
| 344 |
doMultiplePemTrustCertificatesTestImpl(SERVER_2_JKS_KEYSTORE); |
| 345 |
} |
| 346 |
|
| 347 |
private void doMultiplePemTrustCertificatesTestImpl(String serverKeystore) throws Exception { |
| 348 |
Transport clientTransport = Proton.transport(); |
| 349 |
Transport serverTransport = Proton.transport(); |
| 350 |
|
| 351 |
TransportPumper pumper = new TransportPumper(clientTransport, serverTransport); |
| 352 |
|
| 353 |
Connection clientConnection = Proton.connection(); |
| 354 |
Connection serverConnection = Proton.connection(); |
| 355 |
|
| 356 |
SslDomain clientSslDomain = SslDomain.Factory.create(); |
| 357 |
clientSslDomain.init(Mode.CLIENT); |
| 358 |
clientSslDomain.setPeerAuthentication(VerifyMode.VERIFY_PEER); |
| 359 |
clientSslDomain.setTrustedCaDb(CA_CERTS); |
| 360 |
clientTransport.ssl(clientSslDomain); |
| 361 |
|
| 362 |
SslDomain serverSslDomain = SslDomain.Factory.create(); |
| 363 |
serverSslDomain.init(Mode.SERVER); |
| 364 |
SSLContext serverSslContext = createSslContext(serverKeystore, PASSWORD, SERVER_JKS_TRUSTSTORE, PASSWORD); |
| 365 |
serverSslDomain.setSslContext(serverSslContext); |
| 366 |
serverTransport.ssl(serverSslDomain); |
| 367 |
|
| 368 |
clientConnection.setContainer(CLIENT_CONTAINER); |
| 369 |
serverConnection.setContainer(SERVER_CONTAINER); |
| 370 |
|
| 371 |
clientTransport.bind(clientConnection); |
| 372 |
serverTransport.bind(serverConnection); |
| 373 |
|
| 374 |
assertConditions(clientTransport); |
| 375 |
assertConditions(serverTransport); |
| 376 |
|
| 377 |
clientConnection.open(); |
| 378 |
|
| 379 |
assertEndpointState(clientConnection, ACTIVE, UNINITIALIZED); |
| 380 |
assertEndpointState(serverConnection, UNINITIALIZED, UNINITIALIZED); |
| 381 |
|
| 382 |
assertConditions(clientTransport); |
| 383 |
assertConditions(serverTransport); |
| 384 |
|
| 385 |
pumper.pumpAll(); |
| 386 |
|
| 387 |
assertEndpointState(clientConnection, ACTIVE, UNINITIALIZED); |
| 388 |
assertEndpointState(serverConnection, UNINITIALIZED, ACTIVE); |
| 389 |
|
| 390 |
assertConditions(clientTransport); |
| 391 |
assertConditions(serverTransport); |
| 392 |
|
| 393 |
serverConnection.open(); |
| 394 |
|
| 395 |
assertEndpointState(clientConnection, ACTIVE, UNINITIALIZED); |
| 396 |
assertEndpointState(serverConnection, ACTIVE, ACTIVE); |
| 397 |
|
| 398 |
assertConditions(clientTransport); |
| 399 |
assertConditions(serverTransport); |
| 400 |
|
| 401 |
pumper.pumpAll(); |
| 402 |
|
| 403 |
assertEndpointState(clientConnection, ACTIVE, ACTIVE); |
| 404 |
assertEndpointState(serverConnection, ACTIVE, ACTIVE); |
| 405 |
|
| 406 |
assertConditions(clientTransport); |
| 407 |
assertConditions(serverTransport); |
| 408 |
} |
| 409 |
} |