Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move to Netty SslContext API, close #1023
- Loading branch information
Showing
10 changed files
with
203 additions
and
55 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
29 changes: 29 additions & 0 deletions
29
client/src/main/java/org/asynchttpclient/SslEngineFactory.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 @@ | ||
/* | ||
* Copyright (c) 2014 AsyncHttpClient Project. All rights reserved. | ||
* | ||
* This program is licensed to you under the Apache License Version 2.0, | ||
* and you may not use this file except in compliance with the Apache License Version 2.0. | ||
* You may obtain a copy of the Apache License Version 2.0 at | ||
* http://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the Apache License Version 2.0 is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. | ||
*/ | ||
package org.asynchttpclient; | ||
|
||
import javax.net.ssl.SSLEngine; | ||
|
||
public interface SslEngineFactory { | ||
|
||
/** | ||
* Creates new {@link SSLEngine}. | ||
* | ||
* @param config the client config | ||
* @param peerHost the peer hostname | ||
* @param peerPort the peer port | ||
* @return new engine | ||
*/ | ||
SSLEngine newSslEngine(AsyncHttpClientConfig config, String peerHost, int peerPort); | ||
} |
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
57 changes: 57 additions & 0 deletions
57
client/src/main/java/org/asynchttpclient/netty/ssl/DefaultSslEngineFactory.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,57 @@ | ||
/* | ||
* Copyright (c) 2015 AsyncHttpClient Project. All rights reserved. | ||
* | ||
* This program is licensed to you under the Apache License Version 2.0, | ||
* and you may not use this file except in compliance with the Apache License Version 2.0. | ||
* You may obtain a copy of the Apache License Version 2.0 at | ||
* http://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the Apache License Version 2.0 is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. | ||
*/ | ||
package org.asynchttpclient.netty.ssl; | ||
|
||
import io.netty.buffer.ByteBufAllocator; | ||
import io.netty.handler.ssl.SslContext; | ||
import io.netty.handler.ssl.SslContextBuilder; | ||
import io.netty.handler.ssl.SslProvider; | ||
import io.netty.handler.ssl.util.InsecureTrustManagerFactory; | ||
|
||
import javax.net.ssl.SSLEngine; | ||
import javax.net.ssl.SSLException; | ||
|
||
import org.asynchttpclient.AsyncHttpClientConfig; | ||
|
||
public class DefaultSslEngineFactory extends SslEngineFactoryBase { | ||
|
||
private final SslContext sslContext; | ||
|
||
public DefaultSslEngineFactory(AsyncHttpClientConfig config) throws SSLException { | ||
this.sslContext = getSslContext(config); | ||
} | ||
|
||
private SslContext getSslContext(AsyncHttpClientConfig config) throws SSLException { | ||
if (config.getSslContext() != null) | ||
return config.getSslContext(); | ||
|
||
SslContextBuilder sslContextBuilder = SslContextBuilder.forClient()// | ||
.sslProvider(config.isUseOpenSsl() ? SslProvider.OPENSSL : SslProvider.JDK)// | ||
.sessionCacheSize(config.getSslSessionCacheSize())// | ||
.sessionTimeout(config.getSslSessionTimeout()); | ||
|
||
if (config.isAcceptAnyCertificate()) | ||
sslContextBuilder.trustManager(InsecureTrustManagerFactory.INSTANCE); | ||
|
||
return sslContextBuilder.build(); | ||
} | ||
|
||
@Override | ||
public SSLEngine newSslEngine(AsyncHttpClientConfig config, String peerHost, int peerPort) { | ||
// FIXME should be using ctx allocator | ||
SSLEngine sslEngine = sslContext.newEngine(ByteBufAllocator.DEFAULT, peerHost, peerPort); | ||
configureSslEngine(sslEngine, config); | ||
return sslEngine; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
client/src/main/java/org/asynchttpclient/netty/ssl/JsseSslEngineFactory.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,36 @@ | ||
/* | ||
* Copyright (c) 2015 AsyncHttpClient Project. All rights reserved. | ||
* | ||
* This program is licensed to you under the Apache License Version 2.0, | ||
* and you may not use this file except in compliance with the Apache License Version 2.0. | ||
* You may obtain a copy of the Apache License Version 2.0 at | ||
* http://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the Apache License Version 2.0 is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. | ||
*/ | ||
package org.asynchttpclient.netty.ssl; | ||
|
||
import javax.net.ssl.SSLContext; | ||
import javax.net.ssl.SSLEngine; | ||
|
||
import org.asynchttpclient.AsyncHttpClientConfig; | ||
|
||
public class JsseSslEngineFactory extends SslEngineFactoryBase { | ||
|
||
private final SSLContext sslContext; | ||
|
||
public JsseSslEngineFactory(SSLContext sslContext) { | ||
this.sslContext = sslContext; | ||
} | ||
|
||
@Override | ||
public SSLEngine newSslEngine(AsyncHttpClientConfig config, String peerHost, int peerPort) { | ||
SSLEngine sslEngine = sslContext.createSSLEngine(peerHost, peerPort); | ||
configureSslEngine(sslEngine, config); | ||
return sslEngine; | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
client/src/main/java/org/asynchttpclient/netty/ssl/SslEngineFactoryBase.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,40 @@ | ||
/* | ||
* Copyright (c) 2015 AsyncHttpClient Project. All rights reserved. | ||
* | ||
* This program is licensed to you under the Apache License Version 2.0, | ||
* and you may not use this file except in compliance with the Apache License Version 2.0. | ||
* You may obtain a copy of the Apache License Version 2.0 at | ||
* http://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the Apache License Version 2.0 is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. | ||
*/ | ||
package org.asynchttpclient.netty.ssl; | ||
|
||
import static org.asynchttpclient.util.MiscUtils.isNonEmpty; | ||
|
||
import javax.net.ssl.SSLEngine; | ||
import javax.net.ssl.SSLParameters; | ||
|
||
import org.asynchttpclient.AsyncHttpClientConfig; | ||
import org.asynchttpclient.SslEngineFactory; | ||
|
||
public abstract class SslEngineFactoryBase implements SslEngineFactory { | ||
|
||
protected void configureSslEngine(SSLEngine sslEngine, AsyncHttpClientConfig config) { | ||
sslEngine.setUseClientMode(true); | ||
if (!config.isAcceptAnyCertificate()) { | ||
SSLParameters params = sslEngine.getSSLParameters(); | ||
params.setEndpointIdentificationAlgorithm("HTTPS"); | ||
sslEngine.setSSLParameters(params); | ||
} | ||
|
||
if (isNonEmpty(config.getEnabledProtocols())) | ||
sslEngine.setEnabledProtocols(config.getEnabledProtocols()); | ||
|
||
if (isNonEmpty(config.getEnabledCipherSuites())) | ||
sslEngine.setEnabledCipherSuites(config.getEnabledCipherSuites()); | ||
} | ||
} |
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
Oops, something went wrong.