-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #360 from Hakky54/make-sslcontext-customizable
Make SSLContext customizable and consistent with SSLSocket, SSLSocketFactory, SSLServerSocketFactory and SSLEngine
- Loading branch information
Showing
6 changed files
with
260 additions
and
13 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
33 changes: 33 additions & 0 deletions
33
sslcontext-kickstart/src/main/java/nl/altindag/ssl/sslcontext/FenixSSLContext.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 @@ | ||
/* | ||
* Copyright 2019 Thunderberry. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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 nl.altindag.ssl.sslcontext; | ||
|
||
import javax.net.ssl.SSLContext; | ||
import javax.net.ssl.SSLParameters; | ||
|
||
/** | ||
* <strong>NOTE:</strong> | ||
* Please don't use this class directly as it is part of the internal API. Class name and methods can be changed any time. | ||
* | ||
* @author Hakan Altindag | ||
*/ | ||
public class FenixSSLContext extends SSLContext { | ||
|
||
public FenixSSLContext(SSLContext baseSslContext, SSLParameters baseSslParameters) { | ||
super(new FenixSSLContextSpi(baseSslContext, baseSslParameters), baseSslContext.getProvider(), baseSslContext.getProtocol()); | ||
} | ||
|
||
} |
108 changes: 108 additions & 0 deletions
108
sslcontext-kickstart/src/main/java/nl/altindag/ssl/sslcontext/FenixSSLContextSpi.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,108 @@ | ||
/* | ||
* Copyright 2019 Thunderberry. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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 nl.altindag.ssl.sslcontext; | ||
|
||
import nl.altindag.ssl.util.SSLParametersUtils; | ||
import nl.altindag.ssl.util.SSLSocketUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.net.ssl.KeyManager; | ||
import javax.net.ssl.SSLContext; | ||
import javax.net.ssl.SSLContextSpi; | ||
import javax.net.ssl.SSLEngine; | ||
import javax.net.ssl.SSLParameters; | ||
import javax.net.ssl.SSLServerSocketFactory; | ||
import javax.net.ssl.SSLSessionContext; | ||
import javax.net.ssl.SSLSocketFactory; | ||
import javax.net.ssl.TrustManager; | ||
import java.security.SecureRandom; | ||
|
||
import static java.util.Objects.nonNull; | ||
|
||
/** | ||
* @author Hakan Altindag | ||
*/ | ||
class FenixSSLContextSpi extends SSLContextSpi { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(FenixSSLContextSpi.class); | ||
|
||
private final SSLContext sslContext; | ||
private final SSLParameters sslParameters; | ||
|
||
FenixSSLContextSpi(SSLContext sslContext, SSLParameters sslParameters) { | ||
this.sslContext = sslContext; | ||
this.sslParameters = sslParameters; | ||
} | ||
|
||
@Override | ||
protected void engineInit(KeyManager[] km, TrustManager[] tm, SecureRandom sr) { | ||
LOGGER.debug("The provided parameters are being ignored as the SSLContext has already been initialized"); | ||
} | ||
|
||
@Override | ||
protected SSLSocketFactory engineGetSocketFactory() { | ||
return SSLSocketUtils.createSslSocketFactory(sslContext, engineGetDefaultSSLParameters()); | ||
} | ||
|
||
@Override | ||
protected SSLServerSocketFactory engineGetServerSocketFactory() { | ||
return SSLSocketUtils.createSslServerSocketFactory(sslContext, engineGetDefaultSSLParameters()); | ||
} | ||
|
||
@Override | ||
protected SSLEngine engineCreateSSLEngine() { | ||
return getSSLEngine(null, 0); | ||
} | ||
|
||
@Override | ||
protected SSLEngine engineCreateSSLEngine(String host, int port) { | ||
return getSSLEngine(host, port); | ||
} | ||
|
||
private SSLEngine getSSLEngine(String peerHost, int peerPort) { | ||
SSLEngine sslEngine; | ||
if (nonNull(peerHost)) { | ||
sslEngine = sslContext.createSSLEngine(peerHost, peerPort); | ||
} else { | ||
sslEngine = sslContext.createSSLEngine(); | ||
} | ||
|
||
sslEngine.setSSLParameters(engineGetDefaultSSLParameters()); | ||
return sslEngine; | ||
} | ||
|
||
@Override | ||
protected SSLSessionContext engineGetServerSessionContext() { | ||
return sslContext.getServerSessionContext(); | ||
} | ||
|
||
@Override | ||
protected SSLSessionContext engineGetClientSessionContext() { | ||
return sslContext.getClientSessionContext(); | ||
} | ||
|
||
@Override | ||
protected SSLParameters engineGetDefaultSSLParameters() { | ||
return SSLParametersUtils.copy(sslParameters); | ||
} | ||
|
||
@Override | ||
protected SSLParameters engineGetSupportedSSLParameters() { | ||
return SSLParametersUtils.copy(sslContext.getSupportedSSLParameters()); | ||
} | ||
|
||
} |
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
39 changes: 39 additions & 0 deletions
39
sslcontext-kickstart/src/test/java/nl/altindag/ssl/sslcontext/FenixSSLContextSpiShould.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,39 @@ | ||
/* | ||
* Copyright 2019 Thunderberry. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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 nl.altindag.ssl.sslcontext; | ||
|
||
import nl.altindag.log.LogCaptor; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* @author Hakan Altindag | ||
*/ | ||
class FenixSSLContextSpiShould { | ||
|
||
@Test | ||
void ignoreProvidedParametersAndDebugLogWhenEngineInitIsBeingCalled() { | ||
LogCaptor logCaptor = LogCaptor.forClass(FenixSSLContextSpi.class); | ||
|
||
FenixSSLContextSpi sslContextSpi = new FenixSSLContextSpi(null, null); | ||
sslContextSpi.engineInit(null, null, null); | ||
|
||
assertThat(logCaptor.getDebugLogs()) | ||
.containsExactly("The provided parameters are being ignored as the SSLContext has already been initialized"); | ||
} | ||
|
||
} |