-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ELY-2112] Automatic registration of client side / JVM wide default S…
…SLContext
- Loading branch information
Showing
11 changed files
with
321 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
auth/client/src/main/java/org/wildfly/security/auth/client/DefaultSSLContextSpi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.wildfly.security.auth.client; | ||
|
||
import org.wildfly.client.config.ConfigXMLParseException; | ||
|
||
import javax.net.ssl.KeyManager; | ||
import javax.net.ssl.SSLContext; | ||
import javax.net.ssl.SSLContextSpi; | ||
import javax.net.ssl.SSLEngine; | ||
import javax.net.ssl.SSLServerSocketFactory; | ||
import javax.net.ssl.SSLSessionContext; | ||
import javax.net.ssl.SSLSocketFactory; | ||
import javax.net.ssl.TrustManager; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.security.AccessController; | ||
import java.security.GeneralSecurityException; | ||
import java.security.PrivilegedAction; | ||
import java.security.SecureRandom; | ||
|
||
public class DefaultSSLContextSpi extends SSLContextSpi { | ||
|
||
private SSLContext configuredDefaultClientSSLContext; | ||
|
||
public DefaultSSLContextSpi() throws GeneralSecurityException { | ||
this(AuthenticationContext.captureCurrent()); | ||
} | ||
|
||
public DefaultSSLContextSpi(String configPath) throws GeneralSecurityException, URISyntaxException, ConfigXMLParseException { | ||
this(ElytronXmlParser.parseAuthenticationClientConfiguration(new URI(configPath)).create()); | ||
} | ||
|
||
public DefaultSSLContextSpi(AuthenticationContext authenticationContext) throws GeneralSecurityException { | ||
AuthenticationContextConfigurationClient AUTH_CONTEXT_CLIENT = AccessController.doPrivileged((PrivilegedAction<AuthenticationContextConfigurationClient>) AuthenticationContextConfigurationClient::new); | ||
this.configuredDefaultClientSSLContext = AUTH_CONTEXT_CLIENT.getSSLContext(authenticationContext); | ||
} | ||
|
||
@Override | ||
protected void engineInit(KeyManager[] keyManagers, TrustManager[] trustManagers, SecureRandom secureRandom) { | ||
// ignore | ||
} | ||
|
||
@Override | ||
protected SSLSocketFactory engineGetSocketFactory() { | ||
return this.configuredDefaultClientSSLContext.getSocketFactory(); | ||
} | ||
|
||
@Override | ||
protected SSLServerSocketFactory engineGetServerSocketFactory() { | ||
return this.configuredDefaultClientSSLContext.getServerSocketFactory(); | ||
} | ||
|
||
@Override | ||
protected SSLEngine engineCreateSSLEngine() { | ||
return this.configuredDefaultClientSSLContext.createSSLEngine(); | ||
} | ||
|
||
@Override | ||
protected SSLEngine engineCreateSSLEngine(String s, int i) { | ||
return this.configuredDefaultClientSSLContext.createSSLEngine(s, i); | ||
} | ||
|
||
@Override | ||
protected SSLSessionContext engineGetServerSessionContext() { | ||
return this.configuredDefaultClientSSLContext.getServerSessionContext(); | ||
} | ||
|
||
@Override | ||
protected SSLSessionContext engineGetClientSessionContext() { | ||
return this.configuredDefaultClientSSLContext.getClientSessionContext(); | ||
} | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
...rc/main/java/org/wildfly/security/auth/client/ElytronClientDefaultSSLContextProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package org.wildfly.security.auth.client; | ||
|
||
import org.wildfly.client.config.ConfigXMLParseException; | ||
import org.wildfly.security.auth.client._private.ElytronMessages; | ||
|
||
import java.net.URISyntaxException; | ||
import java.security.GeneralSecurityException; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.Provider; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public final class ElytronClientDefaultSSLContextProvider extends Provider { | ||
|
||
public ElytronClientDefaultSSLContextProvider() { | ||
this(null); | ||
} | ||
|
||
public ElytronClientDefaultSSLContextProvider(String configPath) { | ||
super("ElytronClientDefaultSSLContextProvider", 1.0, "Elytron client provider for default SSLContext"); | ||
putService(new ClientSSLContextProviderService(this, "SSLContext", "Default", "org.wildfly.security.auth.client.DefaultSSLContextSpi", null, null, configPath)); | ||
} | ||
|
||
private static final class ClientSSLContextProviderService extends Provider.Service { | ||
String configPath; | ||
// this is Integer because we need to count the number of times entered | ||
// entered.get()==2 means we requested this provider second time, creating a loop, so we throw an sslContextForSecurityProviderCreatesInfiniteLoop exception | ||
// AuthenticationContextConfigurationClient receives sslContextForSecurityProviderCreatesInfiniteLoop exception during obtaining of default SSL context and will therefore request default SSL context from other providers | ||
// after default SSL context from other provider is returned, we must check the entered variable again and throw an exception to inform users that this provider was unsuccessful because of invalid configuration | ||
private final ThreadLocal<Integer> entered = new ThreadLocal<>(); | ||
|
||
ClientSSLContextProviderService(Provider provider, String type, String algorithm, String className, List<String> aliases, | ||
Map<String, String> attributes, String configPath) { | ||
super(provider, type, algorithm, className, aliases, attributes); | ||
this.configPath = configPath; | ||
} | ||
|
||
@Override | ||
public Object newInstance(Object ignored) throws NoSuchAlgorithmException { | ||
Integer enteredCountTmp = entered.get(); | ||
entered.set(enteredCountTmp == null ? 1 : enteredCountTmp + 1); | ||
if (entered.get() >= 2) { | ||
// we do not do clean up entered variable here because it is needed for the second check and possible throwing of second exception below | ||
throw ElytronMessages.log.sslContextForSecurityProviderCreatesInfiniteLoop(); | ||
} | ||
|
||
DefaultSSLContextSpi sslContext; | ||
try { | ||
if (configPath == null) { | ||
sslContext = new DefaultSSLContextSpi(AuthenticationContext.captureCurrent()); | ||
} else { | ||
sslContext = new DefaultSSLContextSpi(this.configPath); | ||
} | ||
// if we had an exception previously, then default ssl context was still returned from other security provider | ||
// which is why we need to check entered variable again | ||
if (entered.get() >= 2) { | ||
throw ElytronMessages.log.sslContextForSecurityProviderCreatesInfiniteLoop(); | ||
} | ||
} catch (GeneralSecurityException | URISyntaxException | ConfigXMLParseException e) { | ||
throw new NoSuchAlgorithmException(e); | ||
} finally { | ||
entered.remove(); | ||
} | ||
return sslContext; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
.../wildfly/security/auth/client/DefaultSSLContextFromFileWorksAndHasPrecedenceTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.wildfly.security.auth.client; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import javax.net.ssl.SSLContext; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.Security; | ||
|
||
public class DefaultSSLContextFromFileWorksAndHasPrecedenceTestCase { | ||
private static final String CONFIG_FILE = "file:./src/test/resources/org/wildfly/security/auth/client/test-wildfly-config-client-default-sslcontext.xml"; | ||
|
||
@Test | ||
public void testDefaultSSLContextFromFileWorksAndHasPrecedence() { | ||
Security.insertProviderAt(new ElytronClientDefaultSSLContextProvider(CONFIG_FILE), 1); | ||
Assert.assertNotNull(Security.getProvider("ElytronClientDefaultSSLContextProvider")); | ||
AuthenticationContext.empty().run(() -> { // provider will not use current auth context but will use authentication context from the file passed to provider. File passed to provider has precedence | ||
SSLContext cip = null; | ||
try { | ||
cip = SSLContext.getDefault(); | ||
} catch (NoSuchAlgorithmException e) { | ||
Assert.fail(); | ||
} | ||
Assert.assertNotNull(cip); | ||
Assert.assertEquals(cip.getProvider().getName(), ElytronClientDefaultSSLContextProvider.class.getSimpleName()); | ||
Assert.assertNotNull(cip.getSocketFactory()); | ||
}); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...security/auth/client/DefaultSSLContextProviderCanDelegateWhenConfigIsMissingTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.wildfly.security.auth.client; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import javax.net.ssl.SSLContext; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.Security; | ||
|
||
public class DefaultSSLContextProviderCanDelegateWhenConfigIsMissingTestCase { | ||
|
||
@Test | ||
public void testDefaultSSLContextProviderWillDelegateIfNoConfigured() { | ||
Security.insertProviderAt(new ElytronClientDefaultSSLContextProvider(), 1); | ||
Assert.assertEquals(ElytronClientDefaultSSLContextProvider.class.getSimpleName(), Security.getProvider("ElytronClientDefaultSSLContextProvider").getName()); | ||
AuthenticationContext.empty().run(() -> { | ||
SSLContext cip = null; | ||
try { | ||
cip = SSLContext.getDefault(); | ||
} catch (NoSuchAlgorithmException e) { | ||
Assert.fail(); | ||
} | ||
Assert.assertNotNull(cip); | ||
Assert.assertNotEquals(cip.getProvider().getName(), ElytronClientDefaultSSLContextProvider.class.getSimpleName()); // different provider was used since no default SSL context configured in Elytron client | ||
Assert.assertNotNull(cip.getSocketFactory()); | ||
}); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...t/java/org/wildfly/security/auth/client/DefaultSSLContextProviderDoesNotLoopTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.wildfly.security.auth.client; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.wildfly.client.config.ConfigXMLParseException; | ||
|
||
import javax.net.ssl.SSLContext; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.security.GeneralSecurityException; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.Security; | ||
|
||
public class DefaultSSLContextProviderDoesNotLoopTestCase { | ||
private static final String CONFIG_FILE = "file:./src/test/resources/org/wildfly/security/auth/client/test-wildfly-config-default-ssl-context-invalid-looping.xml"; | ||
|
||
@Test | ||
public void testDefaultSSLContextProviderDoesNotLoopTestCase() throws GeneralSecurityException, URISyntaxException, ConfigXMLParseException { | ||
Security.insertProviderAt(new ElytronClientDefaultSSLContextProvider(), 1); | ||
AuthenticationContext authenticationContext = ElytronXmlParser.parseAuthenticationClientConfiguration(new URI(CONFIG_FILE)).create(); | ||
authenticationContext.run(() -> { | ||
SSLContext cip = null; | ||
try { | ||
cip = SSLContext.getDefault(); | ||
} catch (NoSuchAlgorithmException e) { | ||
Assert.fail(); | ||
} | ||
Assert.assertNotNull(cip); | ||
Assert.assertNotEquals(cip.getProvider().getName(), ElytronClientDefaultSSLContextProvider.class.getSimpleName()); // diff provider was used since elytron provider is looping | ||
Assert.assertNotNull(cip.getSocketFactory()); | ||
}); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...wildfly/security/auth/client/DefaultSSLContextProviderFromCurrentAuthContextTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.wildfly.security.auth.client; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.wildfly.client.config.ConfigXMLParseException; | ||
|
||
import javax.net.ssl.SSLContext; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.security.GeneralSecurityException; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.Security; | ||
|
||
public class DefaultSSLContextProviderFromCurrentAuthContextTestCase { | ||
private static final String CONFIG_FILE = "file:./src/test/resources/org/wildfly/security/auth/client/test-wildfly-config-client-default-sslcontext.xml"; | ||
|
||
@Test | ||
public void testDefaultSSLContextTakenFromCurrentContext() throws GeneralSecurityException, URISyntaxException, ConfigXMLParseException { | ||
Security.insertProviderAt(new ElytronClientDefaultSSLContextProvider(), 1); | ||
AuthenticationContext authenticationContext = ElytronXmlParser.parseAuthenticationClientConfiguration(new URI(CONFIG_FILE)).create(); | ||
authenticationContext.run(() -> { | ||
SSLContext cip = null; | ||
try { | ||
cip = SSLContext.getDefault(); | ||
} catch (NoSuchAlgorithmException e) { | ||
Assert.fail(); | ||
} | ||
Assert.assertNotNull(cip); | ||
Assert.assertEquals(cip.getProvider().getName(), ElytronClientDefaultSSLContextProvider.class.getSimpleName()); | ||
Assert.assertNotNull(cip.getSocketFactory()); | ||
}); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...ources/org/wildfly/security/auth/client/test-wildfly-config-client-default-sslcontext.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<configuration> | ||
<authentication-client xmlns="urn:elytron:client:1.5"> | ||
<key-stores> | ||
<key-store name="keystore1" type="JKS"> | ||
<file name="src/test/resources/client.keystore"/> | ||
<key-store-clear-password password="password"/> | ||
</key-store> | ||
</key-stores> | ||
<ssl-contexts> | ||
<default-ssl-context name="other-provider-default-sslcontext"/> | ||
<ssl-context name="default-context"> | ||
<providers> | ||
<global/> | ||
</providers> | ||
<key-store-ssl-certificate key-store-name="keystore1"> | ||
<key-store-clear-password password="password"/> | ||
</key-store-ssl-certificate> | ||
</ssl-context> | ||
</ssl-contexts> | ||
<ssl-context-rules> | ||
<rule use-ssl-context="other-provider-default-sslcontext"> | ||
<match-port number="12345"/> | ||
</rule> | ||
<rule use-ssl-context="default-context"> | ||
</rule> | ||
</ssl-context-rules> | ||
</authentication-client> | ||
</configuration> |
13 changes: 13 additions & 0 deletions
13
.../wildfly/security/auth/client/test-wildfly-config-default-ssl-context-invalid-looping.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<configuration> | ||
<authentication-client xmlns="urn:elytron:client:1.5"> | ||
<ssl-contexts> | ||
<default-ssl-context name="looping-default-sslcontext"/> | ||
</ssl-contexts> | ||
<ssl-context-rules> | ||
<rule use-ssl-context="looping-default-sslcontext"> | ||
</rule> | ||
</ssl-context-rules> | ||
</authentication-client> | ||
</configuration> |