Enable client authentication on the server side by calling
- * {@link SSLEngine#setNeedClientAuth(boolean)} before creating
- * {@link SslHandler}.
- *
When initializing an {@link SSLContext} on the client side,
- * specify the {@link KeyManager} that contains the client certificate as
- * the first argument of {@link SSLContext#init(KeyManager[], TrustManager[], SecureRandom)}.
- *
When initializing an {@link SSLContext} on the server side,
- * specify the proper {@link TrustManager} as the second argument of
- * {@link SSLContext#init(KeyManager[], TrustManager[], SecureRandom)}
- * to validate the client certificate.
- *
- */
-public final class SecureChatSslContextFactory {
-
- private static final String PROTOCOL = "TLS";
- private static final SSLContext SERVER_CONTEXT;
- private static final SSLContext CLIENT_CONTEXT;
-
- static {
- String algorithm = SystemPropertyUtil.get("ssl.KeyManagerFactory.algorithm");
- if (algorithm == null) {
- algorithm = "SunX509";
- }
-
- SSLContext serverContext;
- SSLContext clientContext;
- try {
- KeyStore ks = KeyStore.getInstance("JKS");
- ks.load(SecureChatKeyStore.asInputStream(),
- SecureChatKeyStore.getKeyStorePassword());
-
- // Set up key manager factory to use our key store
- KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
- kmf.init(ks, SecureChatKeyStore.getCertificatePassword());
-
- // Initialize the SSLContext to work with our key managers.
- serverContext = SSLContext.getInstance(PROTOCOL);
- serverContext.init(kmf.getKeyManagers(), null, null);
- } catch (Exception e) {
- throw new Error(
- "Failed to initialize the server-side SSLContext", e);
- }
-
- try {
- clientContext = SSLContext.getInstance(PROTOCOL);
- clientContext.init(null, SecureChatTrustManagerFactory.getTrustManagers(), null);
- } catch (Exception e) {
- throw new Error(
- "Failed to initialize the client-side SSLContext", e);
- }
-
- SERVER_CONTEXT = serverContext;
- CLIENT_CONTEXT = clientContext;
- }
-
- public static SSLContext getServerContext() {
- return SERVER_CONTEXT;
- }
-
- public static SSLContext getClientContext() {
- return CLIENT_CONTEXT;
- }
-
- private SecureChatSslContextFactory() {
- // Unused
- }
-}
diff --git a/example/src/main/java/io/netty/example/securechat/SecureChatTrustManagerFactory.java b/example/src/main/java/io/netty/example/securechat/SecureChatTrustManagerFactory.java
deleted file mode 100644
index 28dafea70f..0000000000
--- a/example/src/main/java/io/netty/example/securechat/SecureChatTrustManagerFactory.java
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
- * Copyright 2012 The Netty Project
- *
- * The Netty Project 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 io.netty.example.securechat;
-
-import javax.net.ssl.ManagerFactoryParameters;
-import javax.net.ssl.TrustManager;
-import javax.net.ssl.TrustManagerFactorySpi;
-import javax.net.ssl.X509TrustManager;
-import java.security.InvalidAlgorithmParameterException;
-import java.security.KeyStore;
-import java.security.KeyStoreException;
-import java.security.cert.X509Certificate;
-
-/**
- * Bogus {@link TrustManagerFactorySpi} which accepts any certificate
- * even if it is invalid.
- */
-public class SecureChatTrustManagerFactory extends TrustManagerFactorySpi {
-
- private static final TrustManager DUMMY_TRUST_MANAGER = new X509TrustManager() {
- @Override
- public X509Certificate[] getAcceptedIssuers() {
- return new X509Certificate[0];
- }
-
- @Override
- public void checkClientTrusted(X509Certificate[] chain, String authType) {
- // Always trust - it is an example.
- // You should do something in the real world.
- // You will reach here only if you enabled client certificate auth,
- // as described in SecureChatSslContextFactory.
- System.err.println(
- "UNKNOWN CLIENT CERTIFICATE: " + chain[0].getSubjectDN());
- }
-
- @Override
- public void checkServerTrusted(X509Certificate[] chain, String authType) {
- // Always trust - it is an example.
- // You should do something in the real world.
- System.err.println(
- "UNKNOWN SERVER CERTIFICATE: " + chain[0].getSubjectDN());
- }
- };
-
- public static TrustManager[] getTrustManagers() {
- return new TrustManager[] { DUMMY_TRUST_MANAGER };
- }
-
- @Override
- protected TrustManager[] engineGetTrustManagers() {
- return getTrustManagers();
- }
-
- @Override
- protected void engineInit(KeyStore keystore) throws KeyStoreException {
- // Unused
- }
-
- @Override
- protected void engineInit(ManagerFactoryParameters managerFactoryParameters)
- throws InvalidAlgorithmParameterException {
- // Unused
- }
-}
diff --git a/example/src/main/java/io/netty/example/spdy/client/SpdyClient.java b/example/src/main/java/io/netty/example/spdy/client/SpdyClient.java
index e334cd4673..f5fa16f513 100644
--- a/example/src/main/java/io/netty/example/spdy/client/SpdyClient.java
+++ b/example/src/main/java/io/netty/example/spdy/client/SpdyClient.java
@@ -27,7 +27,11 @@ import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpVersion;
+import io.netty.handler.ssl.SslContext;
+import io.netty.handler.ssl.SslProvider;
+import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
+import javax.net.ssl.SSLException;
import java.net.InetSocketAddress;
import java.util.concurrent.BlockingQueue;
@@ -48,13 +52,15 @@ import static java.util.concurrent.TimeUnit.*;
*/
public class SpdyClient {
+ private final SslContext sslCtx;
private final String host;
private final int port;
private final HttpResponseClientHandler httpResponseHandler;
private Channel channel;
private EventLoopGroup workerGroup;
- public SpdyClient(String host, int port) {
+ public SpdyClient(String host, int port) throws SSLException {
+ sslCtx = SslContext.newClientContext(SslProvider.JDK, InsecureTrustManagerFactory.INSTANCE);
this.host = host;
this.port = port;
httpResponseHandler = new HttpResponseClientHandler();
@@ -73,7 +79,7 @@ public class SpdyClient {
b.channel(NioSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE, true);
b.remoteAddress(new InetSocketAddress(host, port));
- b.handler(new SpdyClientInitializer(httpResponseHandler));
+ b.handler(new SpdyClientInitializer(sslCtx, httpResponseHandler));
// Start the client.
channel = b.connect().syncUninterruptibly().channel();
diff --git a/example/src/main/java/io/netty/example/spdy/client/SpdyClientInitializer.java b/example/src/main/java/io/netty/example/spdy/client/SpdyClientInitializer.java
index ffc6d75fd5..63400c472d 100644
--- a/example/src/main/java/io/netty/example/spdy/client/SpdyClientInitializer.java
+++ b/example/src/main/java/io/netty/example/spdy/client/SpdyClientInitializer.java
@@ -18,11 +18,11 @@ package io.netty.example.spdy.client;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
-import io.netty.example.securechat.SecureChatSslContextFactory;
import io.netty.handler.codec.spdy.SpdyFrameCodec;
import io.netty.handler.codec.spdy.SpdyHttpDecoder;
import io.netty.handler.codec.spdy.SpdyHttpEncoder;
import io.netty.handler.codec.spdy.SpdySessionHandler;
+import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslHandler;
import org.eclipse.jetty.npn.NextProtoNego;
@@ -33,9 +33,11 @@ import static io.netty.util.internal.logging.InternalLogLevel.*;
public class SpdyClientInitializer extends ChannelInitializer {
+ private final SslContext sslCtx;
private final HttpResponseClientHandler httpResponseHandler;
- public SpdyClientInitializer(HttpResponseClientHandler httpResponseHandler) {
+ public SpdyClientInitializer(SslContext sslCtx, HttpResponseClientHandler httpResponseHandler) {
+ this.sslCtx = sslCtx;
this.httpResponseHandler = httpResponseHandler;
}
@@ -43,14 +45,14 @@ public class SpdyClientInitializer extends ChannelInitializer {
@Override
public void initChannel(SocketChannel ch) throws Exception {
- SSLEngine engine = SecureChatSslContextFactory.getClientContext().createSSLEngine();
- engine.setUseClientMode(true);
+ SslHandler sslHandler = sslCtx.newHandler(ch.alloc());
+ SSLEngine engine = sslHandler.engine();
NextProtoNego.put(engine, new SpdyClientProvider());
NextProtoNego.debug = true;
ChannelPipeline pipeline = ch.pipeline();
- pipeline.addLast("ssl", new SslHandler(engine));
+ pipeline.addLast("ssl", sslHandler);
pipeline.addLast("spdyFrameCodec", new SpdyFrameCodec(SPDY_3_1));
pipeline.addLast("spdyFrameLogger", new SpdyFrameLogger(INFO));
pipeline.addLast("spdySessionHandler", new SpdySessionHandler(SPDY_3_1, false));
diff --git a/example/src/main/java/io/netty/example/spdy/server/SpdyServer.java b/example/src/main/java/io/netty/example/spdy/server/SpdyServer.java
index 8e6c177ad7..a384e3da54 100644
--- a/example/src/main/java/io/netty/example/spdy/server/SpdyServer.java
+++ b/example/src/main/java/io/netty/example/spdy/server/SpdyServer.java
@@ -21,6 +21,8 @@ import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
+import io.netty.handler.ssl.SslContext;
+import io.netty.handler.ssl.util.SelfSignedCertificate;
/**
* A SPDY Server that responds to a GET request with a Hello World.
@@ -42,9 +44,11 @@ import io.netty.channel.socket.nio.NioServerSocketChannel;
*/
public class SpdyServer {
+ private final SslContext sslCtx;
private final int port;
- public SpdyServer(int port) {
+ public SpdyServer(SslContext sslCtx, int port) {
+ this.sslCtx = sslCtx;
this.port = port;
}
@@ -56,7 +60,7 @@ public class SpdyServer {
ServerBootstrap b = new ServerBootstrap();
b.option(ChannelOption.SO_BACKLOG, 1024);
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
- .childHandler(new SpdyServerInitializer());
+ .childHandler(new SpdyServerInitializer(sslCtx));
Channel ch = b.bind(port).sync().channel();
ch.closeFuture().sync();
@@ -79,7 +83,10 @@ public class SpdyServer {
System.out.println("Open your SPDY enabled browser and navigate to https://localhost:" + port + '/');
System.out.println("If using Chrome browser, check your SPDY sessions at chrome://net-internals/#spdy");
- new SpdyServer(port).run();
+ // Configure SSL.
+ SelfSignedCertificate ssc = new SelfSignedCertificate();
+ SslContext sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
+ new SpdyServer(sslCtx, port).run();
}
private static void checkForNpnSupport() {
diff --git a/example/src/main/java/io/netty/example/spdy/server/SpdyServerInitializer.java b/example/src/main/java/io/netty/example/spdy/server/SpdyServerInitializer.java
index 33b0dbed01..9c5ee4ccdb 100644
--- a/example/src/main/java/io/netty/example/spdy/server/SpdyServerInitializer.java
+++ b/example/src/main/java/io/netty/example/spdy/server/SpdyServerInitializer.java
@@ -18,7 +18,7 @@ package io.netty.example.spdy.server;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
-import io.netty.example.securechat.SecureChatSslContextFactory;
+import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslHandler;
import org.eclipse.jetty.npn.NextProtoNego;
@@ -28,12 +28,19 @@ import javax.net.ssl.SSLEngine;
* Sets up the Netty pipeline
*/
public class SpdyServerInitializer extends ChannelInitializer {
+
+ private final SslContext sslCtx;
+
+ public SpdyServerInitializer(SslContext sslCtx) {
+ this.sslCtx = sslCtx;
+ }
+
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
- SSLEngine engine = SecureChatSslContextFactory.getServerContext().createSSLEngine();
- engine.setUseClientMode(false);
+ SslHandler sslHandler = sslCtx.newHandler(ch.alloc());
+ SSLEngine engine = sslHandler.engine();
p.addLast("ssl", new SslHandler(engine));
// Setup NextProtoNego with our server provider
diff --git a/handler/pom.xml b/handler/pom.xml
index beb620dbc7..eb25980f2b 100644
--- a/handler/pom.xml
+++ b/handler/pom.xml
@@ -44,6 +44,12 @@
netty-codec${project.version}
+
+ ${project.groupId}
+ netty-tcnative
+ ${os.detected.classifier}
+ true
+ org.bouncycastlebcpkix-jdk15on
diff --git a/handler/src/main/java/io/netty/handler/ssl/ApplicationProtocolSelector.java b/handler/src/main/java/io/netty/handler/ssl/ApplicationProtocolSelector.java
new file mode 100644
index 0000000000..4a69861db7
--- /dev/null
+++ b/handler/src/main/java/io/netty/handler/ssl/ApplicationProtocolSelector.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2014 The Netty Project
+ *
+ * The Netty Project 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 io.netty.handler.ssl;
+
+import java.util.List;
+
+/**
+ * Selects an application layer protocol in TLS NPN
+ * (Next Protocol Negotiation) or ALPN
+ * (Application Layer Protocol Negotiation).
+ */
+public interface ApplicationProtocolSelector {
+ /**
+ * Invoked to select a protocol from the list of specified application layer protocols.
+ *
+ * @param protocols the list of application layer protocols sent by the server.
+ * The list is empty if the server supports neither NPN nor ALPM.
+ * @return the selected protocol. {@code null} if no protocol was selected.
+ */
+ String selectProtocol(List protocols) throws Exception;
+}
diff --git a/handler/src/main/java/io/netty/handler/ssl/JdkSslClientContext.java b/handler/src/main/java/io/netty/handler/ssl/JdkSslClientContext.java
new file mode 100644
index 0000000000..bd4647cb62
--- /dev/null
+++ b/handler/src/main/java/io/netty/handler/ssl/JdkSslClientContext.java
@@ -0,0 +1,174 @@
+/*
+ * Copyright 2014 The Netty Project
+ *
+ * The Netty Project 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 io.netty.handler.ssl;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufInputStream;
+
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLException;
+import javax.net.ssl.SSLSessionContext;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.TrustManagerFactory;
+import javax.security.auth.x500.X500Principal;
+import java.io.File;
+import java.security.KeyStore;
+import java.security.cert.CertificateFactory;
+import java.security.cert.X509Certificate;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * A client-side {@link SslContext} which uses JDK's SSL/TLS implementation.
+ */
+public final class JdkSslClientContext extends JdkSslContext {
+
+ private final SSLContext ctx;
+
+ /**
+ * Creates a new instance.
+ */
+ public JdkSslClientContext() throws SSLException {
+ this(null, null, null, null, 0, 0);
+ }
+
+ /**
+ * Creates a new instance.
+ *
+ * @param certChainFile an X.509 certificate chain file in PEM format.
+ * {@code null} to use the system default
+ */
+ public JdkSslClientContext(File certChainFile) throws SSLException {
+ this(certChainFile, null);
+ }
+
+ /**
+ * Creates a new instance.
+ *
+ * @param trustManagerFactory the {@link TrustManagerFactory} that provides the {@link TrustManager}s
+ * that verifies the certificates sent from servers.
+ * {@code null} to use the default.
+ */
+ public JdkSslClientContext(TrustManagerFactory trustManagerFactory) throws SSLException {
+ this(null, trustManagerFactory);
+ }
+
+ /**
+ * Creates a new instance.
+ *
+ * @param certChainFile an X.509 certificate chain file in PEM format.
+ * {@code null} to use the system default
+ * @param trustManagerFactory the {@link TrustManagerFactory} that provides the {@link TrustManager}s
+ * that verifies the certificates sent from servers.
+ * {@code null} to use the default.
+ */
+ public JdkSslClientContext(File certChainFile, TrustManagerFactory trustManagerFactory) throws SSLException {
+ this(certChainFile, trustManagerFactory, null, null, 0, 0);
+ }
+
+ /**
+ * Creates a new instance.
+ *
+ * @param certChainFile an X.509 certificate chain file in PEM format.
+ * {@code null} to use the system default
+ * @param trustManagerFactory the {@link TrustManagerFactory} that provides the {@link TrustManager}s
+ * that verifies the certificates sent from servers.
+ * {@code null} to use the default.
+ * @param ciphers the cipher suites to enable, in the order of preference.
+ * {@code null} to use the default cipher suites.
+ * @param nextProtocolSelector the {@link ApplicationProtocolSelector} that chooses one of the application layer
+ * protocols returned by a TLS server.
+ * {@code null} to disable TLS NPN/ALPN extension.
+ * @param sessionCacheSize the size of the cache used for storing SSL session objects.
+ * {@code 0} to use the default value.
+ * @param sessionTimeout the timeout for the cached SSL session objects, in seconds.
+ * {@code 0} to use the default value.
+ */
+ public JdkSslClientContext(
+ File certChainFile, TrustManagerFactory trustManagerFactory,
+ Iterable ciphers, ApplicationProtocolSelector nextProtocolSelector,
+ long sessionCacheSize, long sessionTimeout) throws SSLException {
+
+ super(ciphers);
+
+ if (nextProtocolSelector != null) {
+ throw new SSLException("NPN/ALPN unsupported: " + nextProtocolSelector);
+ }
+
+ try {
+ if (certChainFile == null) {
+ ctx = SSLContext.getInstance(PROTOCOL);
+ if (trustManagerFactory == null) {
+ ctx.init(null, null, null);
+ } else {
+ trustManagerFactory.init((KeyStore) null);
+ ctx.init(null, trustManagerFactory.getTrustManagers(), null);
+ }
+ } else {
+ KeyStore ks = KeyStore.getInstance("JKS");
+ ks.load(null, null);
+ CertificateFactory cf = CertificateFactory.getInstance("X.509");
+
+ for (ByteBuf buf: PemReader.readCertificates(certChainFile)) {
+ X509Certificate cert = (X509Certificate) cf.generateCertificate(new ByteBufInputStream(buf));
+ X500Principal principal = cert.getSubjectX500Principal();
+ ks.setCertificateEntry(principal.getName("RFC2253"), cert);
+ }
+
+ // Set up trust manager factory to use our key store.
+ if (trustManagerFactory == null) {
+ trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
+ }
+ trustManagerFactory.init(ks);
+
+ // Initialize the SSLContext to work with the trust managers.
+ ctx = SSLContext.getInstance(PROTOCOL);
+ ctx.init(null, trustManagerFactory.getTrustManagers(), null);
+ }
+
+ SSLSessionContext sessCtx = ctx.getClientSessionContext();
+ if (sessionCacheSize > 0) {
+ sessCtx.setSessionCacheSize((int) Math.min(sessionCacheSize, Integer.MAX_VALUE));
+ }
+ if (sessionTimeout > 0) {
+ sessCtx.setSessionTimeout((int) Math.min(sessionTimeout, Integer.MAX_VALUE));
+ }
+ } catch (Exception e) {
+ throw new SSLException("failed to initialize the server-side SSL context", e);
+ }
+ }
+
+ @Override
+ public boolean isClient() {
+ return true;
+ }
+
+ @Override
+ public ApplicationProtocolSelector nextProtocolSelector() {
+ return null;
+ }
+
+ @Override
+ public List nextProtocols() {
+ return Collections.emptyList();
+ }
+
+ @Override
+ public SSLContext context() {
+ return ctx;
+ }
+}
diff --git a/handler/src/main/java/io/netty/handler/ssl/JdkSslContext.java b/handler/src/main/java/io/netty/handler/ssl/JdkSslContext.java
new file mode 100644
index 0000000000..4e5825cb9b
--- /dev/null
+++ b/handler/src/main/java/io/netty/handler/ssl/JdkSslContext.java
@@ -0,0 +1,178 @@
+/*
+ * Copyright 2014 The Netty Project
+ *
+ * The Netty Project 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 io.netty.handler.ssl;
+
+import io.netty.buffer.ByteBufAllocator;
+import io.netty.util.internal.logging.InternalLogger;
+import io.netty.util.internal.logging.InternalLoggerFactory;
+
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLEngine;
+import javax.net.ssl.SSLSessionContext;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * An {@link SslContext} which uses JDK's SSL/TLS implementation.
+ */
+public abstract class JdkSslContext extends SslContext {
+
+ private static final InternalLogger logger = InternalLoggerFactory.getInstance(JdkSslContext.class);
+
+ static final String PROTOCOL = "TLS";
+ static final String[] PROTOCOLS;
+ static final List DEFAULT_CIPHERS;
+
+ static {
+ SSLContext context;
+ try {
+ context = SSLContext.getInstance(PROTOCOL);
+ context.init(null, null, null);
+ } catch (Exception e) {
+ throw new Error("failed to initialize the default SSL context", e);
+ }
+
+ SSLEngine engine = context.createSSLEngine();
+
+ // Choose the sensible default list of protocols.
+ String[] supportedProtocols = engine.getSupportedProtocols();
+ List protocols = new ArrayList();
+ addIfSupported(
+ supportedProtocols, protocols,
+ "TLSv1.2", "TLSv1.1", "TLSv1", "SSLv3");
+
+ if (!protocols.isEmpty()) {
+ PROTOCOLS = protocols.toArray(new String[protocols.size()]);
+ } else {
+ PROTOCOLS = engine.getEnabledProtocols();
+ }
+
+ // Choose the sensible default list of cipher suites.
+ String[] supportedCiphers = engine.getSupportedCipherSuites();
+ List ciphers = new ArrayList();
+ addIfSupported(
+ supportedCiphers, ciphers,
+ // XXX: Make sure to sync this list with OpenSslEngineFactory.
+ "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", // since JDK 8
+ "TLS_ECDHE_RSA_WITH_RC4_128_SHA",
+ "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
+ "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",
+ "TLS_RSA_WITH_AES_128_GCM_SHA256", // since JDK 8
+ "SSL_RSA_WITH_RC4_128_SHA",
+ "SSL_RSA_WITH_RC4_128_MD5",
+ "TLS_RSA_WITH_AES_128_CBC_SHA",
+ "TLS_RSA_WITH_AES_256_CBC_SHA",
+ "SSL_RSA_WITH_DES_CBC_SHA");
+
+ if (!ciphers.isEmpty()) {
+ DEFAULT_CIPHERS = Collections.unmodifiableList(ciphers);
+ } else {
+ // Use the default from JDK as fallback.
+ DEFAULT_CIPHERS = Collections.unmodifiableList(Arrays.asList(engine.getEnabledCipherSuites()));
+ }
+
+ if (logger.isDebugEnabled()) {
+ logger.debug("Default protocols (JDK): {} ", Arrays.asList(PROTOCOLS));
+ logger.debug("Default cipher suites (JDK): {}", DEFAULT_CIPHERS);
+ }
+ }
+
+ private static void addIfSupported(String[] supported, List enabled, String... names) {
+ for (String n: names) {
+ for (String s: supported) {
+ if (n.equals(s)) {
+ enabled.add(s);
+ break;
+ }
+ }
+ }
+ }
+
+ private final String[] cipherSuites;
+ private final List unmodifiableCipherSuites;
+
+ JdkSslContext(Iterable ciphers) {
+ cipherSuites = toCipherSuiteArray(ciphers);
+ unmodifiableCipherSuites = Collections.unmodifiableList(Arrays.asList(cipherSuites));
+ }
+
+ /**
+ * Returns the JDK {@link SSLContext} object held by this context.
+ */
+ public abstract SSLContext context();
+
+ /**
+ * Returns the JDK {@link SSLSessionContext} object held by this context.
+ */
+ public final SSLSessionContext sessionContext() {
+ if (isServer()) {
+ return context().getServerSessionContext();
+ } else {
+ return context().getClientSessionContext();
+ }
+ }
+
+ @Override
+ public final List cipherSuites() {
+ return unmodifiableCipherSuites;
+ }
+
+ @Override
+ public final long sessionCacheSize() {
+ return sessionContext().getSessionCacheSize();
+ }
+
+ @Override
+ public final long sessionTimeout() {
+ return sessionContext().getSessionTimeout();
+ }
+
+ @Override
+ public final SSLEngine newEngine(ByteBufAllocator alloc) {
+ SSLEngine engine = context().createSSLEngine();
+ engine.setEnabledCipherSuites(cipherSuites);
+ engine.setEnabledProtocols(PROTOCOLS);
+ engine.setUseClientMode(isClient());
+ return engine;
+ }
+
+ @Override
+ public final SSLEngine newEngine(ByteBufAllocator alloc, String peerHost, int peerPort) {
+ SSLEngine engine = context().createSSLEngine(peerHost, peerPort);
+ engine.setEnabledCipherSuites(cipherSuites);
+ engine.setEnabledProtocols(PROTOCOLS);
+ engine.setUseClientMode(isClient());
+ return engine;
+ }
+
+ private static String[] toCipherSuiteArray(Iterable ciphers) {
+ if (ciphers == null) {
+ return DEFAULT_CIPHERS.toArray(new String[DEFAULT_CIPHERS.size()]);
+ } else {
+ List newCiphers = new ArrayList();
+ for (String c: ciphers) {
+ if (c == null) {
+ break;
+ }
+ newCiphers.add(c);
+ }
+ return newCiphers.toArray(new String[newCiphers.size()]);
+ }
+ }
+}
diff --git a/handler/src/main/java/io/netty/handler/ssl/JdkSslServerContext.java b/handler/src/main/java/io/netty/handler/ssl/JdkSslServerContext.java
new file mode 100644
index 0000000000..236d334374
--- /dev/null
+++ b/handler/src/main/java/io/netty/handler/ssl/JdkSslServerContext.java
@@ -0,0 +1,176 @@
+/*
+ * Copyright 2014 The Netty Project
+ *
+ * The Netty Project 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 io.netty.handler.ssl;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufInputStream;
+
+import javax.net.ssl.KeyManagerFactory;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLException;
+import javax.net.ssl.SSLSessionContext;
+import java.io.File;
+import java.security.KeyFactory;
+import java.security.KeyStore;
+import java.security.PrivateKey;
+import java.security.Security;
+import java.security.cert.Certificate;
+import java.security.cert.CertificateFactory;
+import java.security.spec.InvalidKeySpecException;
+import java.security.spec.PKCS8EncodedKeySpec;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * A server-side {@link SslContext} which uses JDK's SSL/TLS implementation.
+ */
+public final class JdkSslServerContext extends JdkSslContext {
+
+ private final SSLContext ctx;
+
+ /**
+ * Creates a new instance.
+ *
+ * @param certChainFile an X.509 certificate chain file in PEM format
+ * @param keyFile a PKCS#8 private key file in PEM format
+ */
+ public JdkSslServerContext(File certChainFile, File keyFile) throws SSLException {
+ this(certChainFile, keyFile, null);
+ }
+
+ /**
+ * Creates a new instance.
+ *
+ * @param certChainFile an X.509 certificate chain file in PEM format
+ * @param keyFile a PKCS#8 private key file in PEM format
+ * @param keyPassword the password of the {@code keyFile}.
+ * {@code null} if it's not password-protected.
+ */
+ public JdkSslServerContext(File certChainFile, File keyFile, String keyPassword) throws SSLException {
+ this(certChainFile, keyFile, keyPassword, null, null, 0, 0);
+ }
+
+ /**
+ * Creates a new instance.
+ *
+ * @param certChainFile an X.509 certificate chain file in PEM format
+ * @param keyFile a PKCS#8 private key file in PEM format
+ * @param keyPassword the password of the {@code keyFile}.
+ * {@code null} if it's not password-protected.
+ * @param ciphers the cipher suites to enable, in the order of preference.
+ * {@code null} to use the default cipher suites.
+ * @param nextProtocols the application layer protocols to accept, in the order of preference.
+ * {@code null} to disable TLS NPN/ALPN extension.
+ * @param sessionCacheSize the size of the cache used for storing SSL session objects.
+ * {@code 0} to use the default value.
+ * @param sessionTimeout the timeout for the cached SSL session objects, in seconds.
+ * {@code 0} to use the default value.
+ */
+ public JdkSslServerContext(
+ File certChainFile, File keyFile, String keyPassword,
+ Iterable ciphers, Iterable nextProtocols,
+ long sessionCacheSize, long sessionTimeout) throws SSLException {
+
+ super(ciphers);
+
+ if (certChainFile == null) {
+ throw new NullPointerException("certChainFile");
+ }
+ if (keyFile == null) {
+ throw new NullPointerException("keyFile");
+ }
+
+ if (keyPassword == null) {
+ keyPassword = "";
+ }
+
+ if (nextProtocols != null && nextProtocols.iterator().hasNext()) {
+ throw new SSLException("NPN/ALPN unsupported: " + nextProtocols);
+ }
+
+ String algorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm");
+ if (algorithm == null) {
+ algorithm = "SunX509";
+ }
+
+ try {
+ KeyStore ks = KeyStore.getInstance("JKS");
+ ks.load(null, null);
+ CertificateFactory cf = CertificateFactory.getInstance("X.509");
+ KeyFactory rsaKF = KeyFactory.getInstance("RSA");
+ KeyFactory dsaKF = KeyFactory.getInstance("DSA");
+
+ ByteBuf encodedKeyBuf = PemReader.readPrivateKey(keyFile);
+ byte[] encodedKey = new byte[encodedKeyBuf.readableBytes()];
+ encodedKeyBuf.readBytes(encodedKey);
+ PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(encodedKey);
+
+ PrivateKey key;
+ try {
+ key = rsaKF.generatePrivate(encodedKeySpec);
+ } catch (InvalidKeySpecException ignore) {
+ key = dsaKF.generatePrivate(encodedKeySpec);
+ }
+
+ List certChain = new ArrayList();
+ for (ByteBuf buf: PemReader.readCertificates(certChainFile)) {
+ certChain.add(cf.generateCertificate(new ByteBufInputStream(buf)));
+ }
+
+ ks.setKeyEntry("key", key, keyPassword.toCharArray(), certChain.toArray(new Certificate[certChain.size()]));
+
+ // Set up key manager factory to use our key store
+ KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
+ kmf.init(ks, keyPassword.toCharArray());
+
+ // Initialize the SSLContext to work with our key managers.
+ ctx = SSLContext.getInstance(PROTOCOL);
+ ctx.init(kmf.getKeyManagers(), null, null);
+
+ SSLSessionContext sessCtx = ctx.getServerSessionContext();
+ if (sessionCacheSize > 0) {
+ sessCtx.setSessionCacheSize((int) Math.min(sessionCacheSize, Integer.MAX_VALUE));
+ }
+ if (sessionTimeout > 0) {
+ sessCtx.setSessionTimeout((int) Math.min(sessionTimeout, Integer.MAX_VALUE));
+ }
+ } catch (Exception e) {
+ throw new SSLException("failed to initialize the server-side SSL context", e);
+ }
+ }
+
+ @Override
+ public boolean isClient() {
+ return false;
+ }
+
+ @Override
+ public ApplicationProtocolSelector nextProtocolSelector() {
+ return null;
+ }
+
+ @Override
+ public List nextProtocols() {
+ return Collections.emptyList();
+ }
+
+ @Override
+ public SSLContext context() {
+ return ctx;
+ }
+}
diff --git a/handler/src/main/java/io/netty/handler/ssl/OpenSsl.java b/handler/src/main/java/io/netty/handler/ssl/OpenSsl.java
new file mode 100644
index 0000000000..206c27a38f
--- /dev/null
+++ b/handler/src/main/java/io/netty/handler/ssl/OpenSsl.java
@@ -0,0 +1,84 @@
+/*
+ * Copyright 2014 The Netty Project
+ *
+ * The Netty Project 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 io.netty.handler.ssl;
+
+import io.netty.util.internal.NativeLibraryLoader;
+import io.netty.util.internal.logging.InternalLogger;
+import io.netty.util.internal.logging.InternalLoggerFactory;
+import org.apache.tomcat.jni.Library;
+import org.apache.tomcat.jni.SSL;
+
+/**
+ * Tells if {@code netty-tcnative} and its OpenSSL support
+ * are available.
+ */
+public final class OpenSsl {
+
+ private static final InternalLogger logger = InternalLoggerFactory.getInstance(OpenSsl.class);
+ private static final Throwable UNAVAILABILITY_CAUSE;
+
+ static final String IGNORABLE_ERROR_PREFIX = "error:00000000:";
+
+ static {
+ Throwable cause = null;
+ try {
+ NativeLibraryLoader.load("netty-tcnative", SSL.class.getClassLoader());
+ Library.initialize("provided");
+ SSL.initialize(null);
+ } catch (Throwable t) {
+ cause = t;
+ logger.debug(
+ "Failed to load netty-tcnative; " +
+ OpenSslEngine.class.getSimpleName() + " will be unavailable.", t);
+ }
+ UNAVAILABILITY_CAUSE = cause;
+ }
+
+ /**
+ * Returns {@code true} if and only if
+ * {@code netty-tcnative} and its OpenSSL support
+ * are available.
+ */
+ public static boolean isAvailable() {
+ return UNAVAILABILITY_CAUSE == null;
+ }
+
+ /**
+ * Ensure that {@code netty-tcnative} and
+ * its OpenSSL support are available.
+ *
+ * @throws UnsatisfiedLinkError if unavailable
+ */
+ public static void ensureAvailability() {
+ if (UNAVAILABILITY_CAUSE != null) {
+ throw (Error) new UnsatisfiedLinkError(
+ "failed to load the required native library").initCause(UNAVAILABILITY_CAUSE);
+ }
+ }
+
+ /**
+ * Returns the cause of unavailability of
+ * {@code netty-tcnative} and its OpenSSL support.
+ *
+ * @return the cause if unavailable. {@code null} if available.
+ */
+ public static Throwable unavailabilityCause() {
+ return UNAVAILABILITY_CAUSE;
+ }
+
+ private OpenSsl() { }
+}
diff --git a/handler/src/main/java/io/netty/handler/ssl/OpenSslEngine.java b/handler/src/main/java/io/netty/handler/ssl/OpenSslEngine.java
new file mode 100644
index 0000000000..c0b1e400e2
--- /dev/null
+++ b/handler/src/main/java/io/netty/handler/ssl/OpenSslEngine.java
@@ -0,0 +1,867 @@
+/*
+ * Copyright 2014 The Netty Project
+ *
+ * The Netty Project 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 io.netty.handler.ssl;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufAllocator;
+import io.netty.util.internal.EmptyArrays;
+import io.netty.util.internal.logging.InternalLogger;
+import io.netty.util.internal.logging.InternalLoggerFactory;
+import org.apache.tomcat.jni.Buffer;
+import org.apache.tomcat.jni.SSL;
+
+import javax.net.ssl.SSLEngine;
+import javax.net.ssl.SSLEngineResult;
+import javax.net.ssl.SSLException;
+import javax.net.ssl.SSLSession;
+import javax.net.ssl.SSLSessionContext;
+import javax.security.cert.X509Certificate;
+import java.nio.ByteBuffer;
+import java.nio.ReadOnlyBufferException;
+import java.security.Principal;
+import java.security.cert.Certificate;
+import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
+
+import static javax.net.ssl.SSLEngineResult.HandshakeStatus.*;
+import static javax.net.ssl.SSLEngineResult.Status.*;
+
+/**
+ * Implements a {@link SSLEngine} using
+ * OpenSSL BIO abstractions.
+ */
+public final class OpenSslEngine extends SSLEngine {
+
+ private static final InternalLogger logger = InternalLoggerFactory.getInstance(OpenSslEngine.class);
+
+ private static final Certificate[] EMPTY_CERTIFICATES = new Certificate[0];
+ private static final X509Certificate[] EMPTY_X509_CERTIFICATES = new X509Certificate[0];
+
+ private static final SSLException ENGINE_CLOSED = new SSLException("engine closed");
+ private static final SSLException RENEGOTIATION_UNSUPPORTED = new SSLException("renegotiation unsupported");
+ private static final SSLException ENCRYPTED_PACKET_OVERSIZED = new SSLException("encrypted packet oversized");
+
+ static {
+ ENGINE_CLOSED.setStackTrace(EmptyArrays.EMPTY_STACK_TRACE);
+ RENEGOTIATION_UNSUPPORTED.setStackTrace(EmptyArrays.EMPTY_STACK_TRACE);
+ ENCRYPTED_PACKET_OVERSIZED.setStackTrace(EmptyArrays.EMPTY_STACK_TRACE);
+ }
+
+ private static final int MAX_PLAINTEXT_LENGTH = 16 * 1024; // 2^14
+ private static final int MAX_COMPRESSED_LENGTH = MAX_PLAINTEXT_LENGTH + 1024;
+ private static final int MAX_CIPHERTEXT_LENGTH = MAX_COMPRESSED_LENGTH + 1024;
+
+ // Header (5) + Data (2^14) + Compression (1024) + Encryption (1024) + MAC (20) + Padding (256)
+ static final int MAX_ENCRYPTED_PACKET_LENGTH = MAX_CIPHERTEXT_LENGTH + 5 + 20 + 256;
+
+ private static final AtomicIntegerFieldUpdater DESTROYED_UPDATER =
+ AtomicIntegerFieldUpdater.newUpdater(OpenSslEngine.class, "destroyed");
+
+ // OpenSSL state
+ private long ssl;
+ private long networkBIO;
+
+ /**
+ * 0 - not accepted, 1 - accepted implicitly via wrap()/unwrap(), 2 - accepted explicitly via beginHandshake() call
+ */
+ private int accepted;
+ private boolean handshakeFinished;
+ private boolean receivedShutdown;
+ @SuppressWarnings("UnusedDeclaration")
+ private volatile int destroyed;
+
+ private String cipher;
+ private String protocol;
+
+ // SSL Engine status variables
+ private boolean isInboundDone;
+ private boolean isOutboundDone;
+ private boolean engineClosed;
+
+ private int lastPrimingReadResult;
+
+ private final ByteBufAllocator alloc;
+ private SSLSession session;
+
+ /**
+ * Creates a new instance
+ *
+ * @param sslCtx an OpenSSL {@code SSL_CTX} object
+ * @param alloc the {@link ByteBufAllocator} that will be used by this engine
+ */
+ public OpenSslEngine(long sslCtx, ByteBufAllocator alloc) {
+ OpenSsl.ensureAvailability();
+ if (sslCtx == 0) {
+ throw new NullPointerException("sslContext");
+ }
+ if (alloc == null) {
+ throw new NullPointerException("alloc");
+ }
+
+ this.alloc = alloc;
+ ssl = SSL.newSSL(sslCtx, true);
+ networkBIO = SSL.makeNetworkBIO(ssl);
+ }
+
+ /**
+ * Destroys this engine.
+ */
+ public synchronized void shutdown() {
+ if (DESTROYED_UPDATER.compareAndSet(this, 0, 1)) {
+ SSL.freeSSL(ssl);
+ SSL.freeBIO(networkBIO);
+ ssl = networkBIO = 0;
+
+ // internal errors can cause shutdown without marking the engine closed
+ isInboundDone = isOutboundDone = engineClosed = true;
+ }
+ }
+
+ /**
+ * Write plaintext data to the OpenSSL internal BIO
+ *
+ * Calling this function with src.remaining == 0 is undefined.
+ */
+ private int writePlaintextData(final ByteBuffer src) {
+ final int pos = src.position();
+ final int limit = src.limit();
+ final int len = Math.min(limit - pos, MAX_PLAINTEXT_LENGTH);
+ final int sslWrote;
+
+ if (src.isDirect()) {
+ final long addr = Buffer.address(src) + pos;
+ sslWrote = SSL.writeToSSL(ssl, addr, len);
+ if (sslWrote > 0) {
+ src.position(pos + sslWrote);
+ return sslWrote;
+ }
+ } else {
+ ByteBuf buf = alloc.directBuffer(len);
+ try {
+ final long addr;
+ if (buf.hasMemoryAddress()) {
+ addr = buf.memoryAddress();
+ } else {
+ addr = Buffer.address(buf.nioBuffer());
+ }
+
+ src.limit(pos + len);
+
+ buf.setBytes(0, src);
+ src.limit(limit);
+
+ sslWrote = SSL.writeToSSL(ssl, addr, len);
+ if (sslWrote > 0) {
+ src.position(pos + sslWrote);
+ return sslWrote;
+ } else {
+ src.position(pos);
+ }
+ } finally {
+ buf.release();
+ }
+ }
+
+ throw new IllegalStateException("SSL.writeToSSL() returned a non-positive value: " + sslWrote);
+ }
+
+ /**
+ * Write encrypted data to the OpenSSL network BIO
+ */
+ private int writeEncryptedData(final ByteBuffer src) {
+ final int pos = src.position();
+ final int len = src.remaining();
+ if (src.isDirect()) {
+ final long addr = Buffer.address(src) + pos;
+ final int netWrote = SSL.writeToBIO(networkBIO, addr, len);
+ if (netWrote >= 0) {
+ src.position(pos + netWrote);
+ lastPrimingReadResult = SSL.readFromSSL(ssl, addr, 0); // priming read
+ return netWrote;
+ }
+ } else {
+ final ByteBuf buf = alloc.directBuffer(len);
+ try {
+ final long addr;
+ if (buf.hasMemoryAddress()) {
+ addr = buf.memoryAddress();
+ } else {
+ addr = Buffer.address(buf.nioBuffer());
+ }
+
+ buf.setBytes(0, src);
+
+ final int netWrote = SSL.writeToBIO(networkBIO, addr, len);
+ if (netWrote >= 0) {
+ src.position(pos + netWrote);
+ lastPrimingReadResult = SSL.readFromSSL(ssl, addr, 0); // priming read
+ return netWrote;
+ } else {
+ src.position(pos);
+ }
+ } finally {
+ buf.release();
+ }
+ }
+
+ return 0;
+ }
+
+ /**
+ * Read plaintext data from the OpenSSL internal BIO
+ */
+ private int readPlaintextData(final ByteBuffer dst) {
+ if (dst.isDirect()) {
+ final int pos = dst.position();
+ final long addr = Buffer.address(dst) + pos;
+ final int len = dst.limit() - pos;
+ final int sslRead = SSL.readFromSSL(ssl, addr, len);
+ if (sslRead > 0) {
+ dst.position(pos + sslRead);
+ return sslRead;
+ }
+ } else {
+ final int pos = dst.position();
+ final int limit = dst.limit();
+ final int len = Math.min(MAX_ENCRYPTED_PACKET_LENGTH, limit - pos);
+ final ByteBuf buf = alloc.directBuffer(len);
+ try {
+ final long addr;
+ if (buf.hasMemoryAddress()) {
+ addr = buf.memoryAddress();
+ } else {
+ addr = Buffer.address(buf.nioBuffer());
+ }
+
+ final int sslRead = SSL.readFromSSL(ssl, addr, len);
+ if (sslRead > 0) {
+ dst.limit(pos + sslRead);
+ buf.getBytes(0, dst);
+ dst.limit(limit);
+ return sslRead;
+ }
+ } finally {
+ buf.release();
+ }
+ }
+
+ return 0;
+ }
+
+ /**
+ * Read encrypted data from the OpenSSL network BIO
+ */
+ private int readEncryptedData(final ByteBuffer dst, final int pending) {
+ if (dst.isDirect() && dst.remaining() >= pending) {
+ final int pos = dst.position();
+ final long addr = Buffer.address(dst) + pos;
+ final int bioRead = SSL.readFromBIO(networkBIO, addr, pending);
+ if (bioRead > 0) {
+ dst.position(pos + bioRead);
+ return bioRead;
+ }
+ } else {
+ final ByteBuf buf = alloc.directBuffer(pending);
+ try {
+ final long addr;
+ if (buf.hasMemoryAddress()) {
+ addr = buf.memoryAddress();
+ } else {
+ addr = Buffer.address(buf.nioBuffer());
+ }
+
+ final int bioRead = SSL.readFromBIO(networkBIO, addr, pending);
+ if (bioRead > 0) {
+ int oldLimit = dst.limit();
+ dst.limit(dst.position() + bioRead);
+ buf.getBytes(0, dst);
+ dst.limit(oldLimit);
+ return bioRead;
+ }
+ } finally {
+ buf.release();
+ }
+ }
+
+ return 0;
+ }
+
+ @Override
+ public synchronized SSLEngineResult wrap(
+ final ByteBuffer[] srcs, final int offset, final int length, final ByteBuffer dst) throws SSLException {
+
+ // Check to make sure the engine has not been closed
+ if (destroyed != 0) {
+ return new SSLEngineResult(CLOSED, NOT_HANDSHAKING, 0, 0);
+ }
+
+ // Throw required runtime exceptions
+ if (srcs == null) {
+ throw new NullPointerException("srcs");
+ }
+ if (dst == null) {
+ throw new NullPointerException("dst");
+ }
+
+ if (offset >= srcs.length || offset + length > srcs.length) {
+ throw new IndexOutOfBoundsException(
+ "offset: " + offset + ", length: " + length +
+ " (expected: offset <= offset + length <= srcs.length (" + srcs.length + "))");
+ }
+
+ if (dst.isReadOnly()) {
+ throw new ReadOnlyBufferException();
+ }
+
+ // Prepare OpenSSL to work in server mode and receive handshake
+ if (accepted == 0) {
+ beginHandshakeImplicitly();
+ }
+
+ // In handshake or close_notify stages, check if call to wrap was made
+ // without regard to the handshake status.
+ SSLEngineResult.HandshakeStatus handshakeStatus = getHandshakeStatus();
+ if ((!handshakeFinished || engineClosed) && handshakeStatus == NEED_UNWRAP) {
+ return new SSLEngineResult(getEngineStatus(), NEED_UNWRAP, 0, 0);
+ }
+
+ int bytesProduced = 0;
+ int pendingNet;
+
+ // Check for pending data in the network BIO
+ pendingNet = SSL.pendingWrittenBytesInBIO(networkBIO);
+ if (pendingNet > 0) {
+ // Do we have enough room in dst to write encrypted data?
+ int capacity = dst.remaining();
+ if (capacity < pendingNet) {
+ return new SSLEngineResult(BUFFER_OVERFLOW, handshakeStatus, 0, bytesProduced);
+ }
+
+ // Write the pending data from the network BIO into the dst buffer
+ try {
+ bytesProduced += readEncryptedData(dst, pendingNet);
+ } catch (Exception e) {
+ throw new SSLException(e);
+ }
+
+ // If isOuboundDone is set, then the data from the network BIO
+ // was the close_notify message -- we are not required to wait
+ // for the receipt the peer's close_notify message -- shutdown.
+ if (isOutboundDone) {
+ shutdown();
+ }
+
+ return new SSLEngineResult(getEngineStatus(), getHandshakeStatus(), 0, bytesProduced);
+ }
+
+ // There was no pending data in the network BIO -- encrypt any application data
+ int bytesConsumed = 0;
+ for (int i = offset; i < length; ++ i) {
+ final ByteBuffer src = srcs[i];
+ while (src.hasRemaining()) {
+
+ // Write plaintext application data to the SSL engine
+ try {
+ bytesConsumed += writePlaintextData(src);
+ } catch (Exception e) {
+ throw new SSLException(e);
+ }
+
+ // Check to see if the engine wrote data into the network BIO
+ pendingNet = SSL.pendingWrittenBytesInBIO(networkBIO);
+ if (pendingNet > 0) {
+ // Do we have enough room in dst to write encrypted data?
+ int capacity = dst.remaining();
+ if (capacity < pendingNet) {
+ return new SSLEngineResult(BUFFER_OVERFLOW, getHandshakeStatus(), bytesConsumed, bytesProduced);
+ }
+
+ // Write the pending data from the network BIO into the dst buffer
+ try {
+ bytesProduced += readEncryptedData(dst, pendingNet);
+ } catch (Exception e) {
+ throw new SSLException(e);
+ }
+
+ return new SSLEngineResult(getEngineStatus(), getHandshakeStatus(), bytesConsumed, bytesProduced);
+ }
+ }
+ }
+
+ return new SSLEngineResult(getEngineStatus(), getHandshakeStatus(), bytesConsumed, bytesProduced);
+ }
+
+ @Override
+ public synchronized SSLEngineResult unwrap(
+ final ByteBuffer src, final ByteBuffer[] dsts, final int offset, final int length) throws SSLException {
+
+ // Check to make sure the engine has not been closed
+ if (destroyed != 0) {
+ return new SSLEngineResult(CLOSED, NOT_HANDSHAKING, 0, 0);
+ }
+
+ // Throw requried runtime exceptions
+ if (src == null) {
+ throw new NullPointerException("src");
+ }
+ if (dsts == null) {
+ throw new NullPointerException("dsts");
+ }
+ if (offset >= dsts.length || offset + length > dsts.length) {
+ throw new IndexOutOfBoundsException(
+ "offset: " + offset + ", length: " + length +
+ " (expected: offset <= offset + length <= dsts.length (" + dsts.length + "))");
+ }
+
+ int capacity = 0;
+ final int endOffset = offset + length;
+ for (int i = offset; i < endOffset; i ++) {
+ ByteBuffer dst = dsts[i];
+ if (dst == null) {
+ throw new IllegalArgumentException();
+ }
+ if (dst.isReadOnly()) {
+ throw new ReadOnlyBufferException();
+ }
+ capacity += dst.remaining();
+ }
+
+ // Prepare OpenSSL to work in server mode and receive handshake
+ if (accepted == 0) {
+ beginHandshakeImplicitly();
+ }
+
+ // In handshake or close_notify stages, check if call to unwrap was made
+ // without regard to the handshake status.
+ SSLEngineResult.HandshakeStatus handshakeStatus = getHandshakeStatus();
+ if ((!handshakeFinished || engineClosed) && handshakeStatus == NEED_WRAP) {
+ return new SSLEngineResult(getEngineStatus(), NEED_WRAP, 0, 0);
+ }
+
+ // protect against protocol overflow attack vector
+ if (src.remaining() > MAX_ENCRYPTED_PACKET_LENGTH) {
+ isInboundDone = true;
+ isOutboundDone = true;
+ engineClosed = true;
+ shutdown();
+ throw ENCRYPTED_PACKET_OVERSIZED;
+ }
+
+ // Write encrypted data to network BIO
+ int bytesConsumed = 0;
+ lastPrimingReadResult = 0;
+ try {
+ bytesConsumed += writeEncryptedData(src);
+ } catch (Exception e) {
+ throw new SSLException(e);
+ }
+
+ // Check for OpenSSL errors caused by the priming read
+ String error = SSL.getLastError();
+ if (error != null && !error.startsWith(OpenSsl.IGNORABLE_ERROR_PREFIX)) {
+ if (logger.isInfoEnabled()) {
+ logger.info(
+ "SSL_read failed: primingReadResult: " + lastPrimingReadResult +
+ "; OpenSSL error: '" + error + '\'');
+ }
+
+ // There was an internal error -- shutdown
+ shutdown();
+ throw new SSLException(error);
+ }
+
+ // There won't be any application data until we're done handshaking
+ int pendingApp = SSL.isInInit(ssl) == 0 ? SSL.pendingReadableBytesInSSL(ssl) : 0;
+
+ // Do we have enough room in dsts to write decrypted data?
+ if (capacity < pendingApp) {
+ return new SSLEngineResult(BUFFER_OVERFLOW, getHandshakeStatus(), bytesConsumed, 0);
+ }
+
+ // Write decrypted data to dsts buffers
+ int bytesProduced = 0;
+ int idx = offset;
+ while (idx < endOffset) {
+ ByteBuffer dst = dsts[idx];
+ if (!dst.hasRemaining()) {
+ idx ++;
+ continue;
+ }
+
+ if (pendingApp <= 0) {
+ break;
+ }
+
+ int bytesRead;
+ try {
+ bytesRead = readPlaintextData(dst);
+ } catch (Exception e) {
+ throw new SSLException(e);
+ }
+
+ if (bytesRead == 0) {
+ break;
+ }
+
+ bytesProduced += bytesRead;
+ pendingApp -= bytesRead;
+
+ if (!dst.hasRemaining()) {
+ idx ++;
+ }
+ }
+
+ // Check to see if we received a close_notify message from the peer
+ if (!receivedShutdown && (SSL.getShutdown(ssl) & SSL.SSL_RECEIVED_SHUTDOWN) == SSL.SSL_RECEIVED_SHUTDOWN) {
+ receivedShutdown = true;
+ closeOutbound();
+ closeInbound();
+ }
+
+ return new SSLEngineResult(getEngineStatus(), getHandshakeStatus(), bytesConsumed, bytesProduced);
+ }
+
+ @Override
+ public Runnable getDelegatedTask() {
+ // Currently, we do not delegate SSL computation tasks
+ // TODO: in the future, possibly create tasks to do encrypt / decrypt async
+
+ return null;
+ }
+
+ @Override
+ public synchronized void closeInbound() throws SSLException {
+ if (isInboundDone) {
+ return;
+ }
+
+ isInboundDone = true;
+ engineClosed = true;
+
+ if (accepted != 0) {
+ if (!receivedShutdown) {
+ shutdown();
+ throw new SSLException(
+ "Inbound closed before receiving peer's close_notify: possible truncation attack?");
+ }
+ } else {
+ // engine closing before initial handshake
+ shutdown();
+ }
+ }
+
+ @Override
+ public synchronized boolean isInboundDone() {
+ return isInboundDone || engineClosed;
+ }
+
+ @Override
+ public synchronized void closeOutbound() {
+ if (isOutboundDone) {
+ return;
+ }
+
+ isOutboundDone = true;
+ engineClosed = true;
+
+ if (accepted != 0 && destroyed == 0) {
+ int mode = SSL.getShutdown(ssl);
+ if ((mode & SSL.SSL_SENT_SHUTDOWN) != SSL.SSL_SENT_SHUTDOWN) {
+ SSL.shutdownSSL(ssl);
+ }
+ } else {
+ // engine closing before initial handshake
+ shutdown();
+ }
+ }
+
+ @Override
+ public synchronized boolean isOutboundDone() {
+ return isOutboundDone;
+ }
+
+ @Override
+ public String[] getSupportedCipherSuites() {
+ return EmptyArrays.EMPTY_STRINGS;
+ }
+
+ @Override
+ public String[] getEnabledCipherSuites() {
+ return EmptyArrays.EMPTY_STRINGS;
+ }
+
+ @Override
+ public void setEnabledCipherSuites(String[] strings) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public String[] getSupportedProtocols() {
+ return EmptyArrays.EMPTY_STRINGS;
+ }
+
+ @Override
+ public String[] getEnabledProtocols() {
+ return EmptyArrays.EMPTY_STRINGS;
+ }
+
+ @Override
+ public void setEnabledProtocols(String[] strings) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public SSLSession getSession() {
+ SSLSession session = this.session;
+ if (session == null) {
+ this.session = session = new SSLSession() {
+ @Override
+ public byte[] getId() {
+ return String.valueOf(ssl).getBytes();
+ }
+
+ @Override
+ public SSLSessionContext getSessionContext() {
+ return null;
+ }
+
+ @Override
+ public long getCreationTime() {
+ return 0;
+ }
+
+ @Override
+ public long getLastAccessedTime() {
+ return 0;
+ }
+
+ @Override
+ public void invalidate() {
+ }
+
+ @Override
+ public boolean isValid() {
+ return false;
+ }
+
+ @Override
+ public void putValue(String s, Object o) {
+ }
+
+ @Override
+ public Object getValue(String s) {
+ return null;
+ }
+
+ @Override
+ public void removeValue(String s) {
+ }
+
+ @Override
+ public String[] getValueNames() {
+ return EmptyArrays.EMPTY_STRINGS;
+ }
+
+ @Override
+ public Certificate[] getPeerCertificates() {
+ return EMPTY_CERTIFICATES;
+ }
+
+ @Override
+ public Certificate[] getLocalCertificates() {
+ return EMPTY_CERTIFICATES;
+ }
+
+ @Override
+ public X509Certificate[] getPeerCertificateChain() {
+ return EMPTY_X509_CERTIFICATES;
+ }
+
+ @Override
+ public Principal getPeerPrincipal() {
+ return null;
+ }
+
+ @Override
+ public Principal getLocalPrincipal() {
+ return null;
+ }
+
+ @Override
+ public String getCipherSuite() {
+ return cipher;
+ }
+
+ @Override
+ public String getProtocol() {
+ return protocol;
+ }
+
+ @Override
+ public String getPeerHost() {
+ return null;
+ }
+
+ @Override
+ public int getPeerPort() {
+ return 0;
+ }
+
+ @Override
+ public int getPacketBufferSize() {
+ return MAX_ENCRYPTED_PACKET_LENGTH;
+ }
+
+ @Override
+ public int getApplicationBufferSize() {
+ return MAX_PLAINTEXT_LENGTH;
+ }
+ };
+ }
+
+ return session;
+ }
+
+ @Override
+ public synchronized void beginHandshake() throws SSLException {
+ if (engineClosed) {
+ throw ENGINE_CLOSED;
+ }
+
+ switch (accepted) {
+ case 0:
+ SSL.doHandshake(ssl);
+ accepted = 2;
+ break;
+ case 1:
+ // A user did not start handshake by calling this method by him/herself,
+ // but handshake has been started already by wrap() or unwrap() implicitly.
+ // Because it's the user's first time to call this method, it is unfair to
+ // raise an exception. From the user's standpoint, he or she never asked
+ // for renegotiation.
+
+ accepted = 2; // Next time this method is invoked by the user, we should raise an exception.
+ break;
+ case 2:
+ throw RENEGOTIATION_UNSUPPORTED;
+ default:
+ throw new Error();
+ }
+ }
+
+ private synchronized void beginHandshakeImplicitly() throws SSLException {
+ if (engineClosed) {
+ throw ENGINE_CLOSED;
+ }
+
+ if (accepted == 0) {
+ SSL.doHandshake(ssl);
+ accepted = 1;
+ }
+ }
+
+ private SSLEngineResult.Status getEngineStatus() {
+ return engineClosed? CLOSED : OK;
+ }
+
+ @Override
+ public synchronized SSLEngineResult.HandshakeStatus getHandshakeStatus() {
+ if (accepted == 0 || destroyed != 0) {
+ return NOT_HANDSHAKING;
+ }
+
+ // Check if we are in the initial handshake phase
+ if (!handshakeFinished) {
+ // There is pending data in the network BIO -- call wrap
+ if (SSL.pendingWrittenBytesInBIO(networkBIO) != 0) {
+ return NEED_WRAP;
+ }
+
+ // No pending data to be sent to the peer
+ // Check to see if we have finished handshaking
+ if (SSL.isInInit(ssl) == 0) {
+ handshakeFinished = true;
+ cipher = SSL.getCipherForSSL(ssl);
+ protocol = SSL.getNextProtoNegotiated(ssl);
+ return FINISHED;
+ }
+
+ // No pending data and still handshaking
+ // Must be waiting on the peer to send more data
+ return NEED_UNWRAP;
+ }
+
+ // Check if we are in the shutdown phase
+ if (engineClosed) {
+ // Waiting to send the close_notify message
+ if (SSL.pendingWrittenBytesInBIO(networkBIO) != 0) {
+ return NEED_WRAP;
+ }
+
+ // Must be waiting to receive the close_notify message
+ return NEED_UNWRAP;
+ }
+
+ return NOT_HANDSHAKING;
+ }
+
+ @Override
+ public void setUseClientMode(boolean clientMode) {
+ if (clientMode) {
+ throw new UnsupportedOperationException();
+ }
+ }
+
+ @Override
+ public boolean getUseClientMode() {
+ return false;
+ }
+
+ @Override
+ public void setNeedClientAuth(boolean b) {
+ if (b) {
+ throw new UnsupportedOperationException();
+ }
+ }
+
+ @Override
+ public boolean getNeedClientAuth() {
+ return false;
+ }
+
+ @Override
+ public void setWantClientAuth(boolean b) {
+ if (b) {
+ throw new UnsupportedOperationException();
+ }
+ }
+
+ @Override
+ public boolean getWantClientAuth() {
+ return false;
+ }
+
+ @Override
+ public void setEnableSessionCreation(boolean b) {
+ if (b) {
+ throw new UnsupportedOperationException();
+ }
+ }
+
+ @Override
+ public boolean getEnableSessionCreation() {
+ return false;
+ }
+}
diff --git a/handler/src/main/java/io/netty/handler/ssl/OpenSslServerContext.java b/handler/src/main/java/io/netty/handler/ssl/OpenSslServerContext.java
new file mode 100644
index 0000000000..04142440b6
--- /dev/null
+++ b/handler/src/main/java/io/netty/handler/ssl/OpenSslServerContext.java
@@ -0,0 +1,349 @@
+/*
+ * Copyright 2014 The Netty Project
+ *
+ * The Netty Project 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 io.netty.handler.ssl;
+
+import io.netty.buffer.ByteBufAllocator;
+import io.netty.util.internal.logging.InternalLogger;
+import io.netty.util.internal.logging.InternalLoggerFactory;
+import org.apache.tomcat.jni.Pool;
+import org.apache.tomcat.jni.SSL;
+import org.apache.tomcat.jni.SSLContext;
+
+import javax.net.ssl.SSLEngine;
+import javax.net.ssl.SSLException;
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * A server-side {@link SslContext} which uses OpenSSL's SSL/TLS implementation.
+ */
+public final class OpenSslServerContext extends SslContext {
+
+ private static final InternalLogger logger = InternalLoggerFactory.getInstance(OpenSslServerContext.class);
+ private static final List DEFAULT_CIPHERS;
+
+ static {
+ List ciphers = new ArrayList();
+ // XXX: Make sure to sync this list with JdkSslEngineFactory.
+ Collections.addAll(
+ ciphers,
+ "ECDHE-RSA-AES128-GCM-SHA256",
+ "ECDHE-RSA-RC4-SHA",
+ "ECDHE-RSA-AES128-SHA",
+ "ECDHE-RSA-AES256-SHA",
+ "AES128-GCM-SHA256",
+ "RC4-SHA",
+ "RC4-MD5",
+ "AES128-SHA",
+ "AES256-SHA",
+ "DES-CBC3-SHA");
+ DEFAULT_CIPHERS = Collections.unmodifiableList(ciphers);
+
+ if (logger.isDebugEnabled()) {
+ logger.debug("Default cipher suite (OpenSSL): " + ciphers);
+ }
+ }
+
+ private final long aprPool;
+
+ private final List ciphers = new ArrayList();
+ private final List unmodifiableCiphers = Collections.unmodifiableList(ciphers);
+ private final long sessionCacheSize;
+ private final long sessionTimeout;
+ private final List nextProtocols = new ArrayList();
+ private final List unmodifiableNextProtocols = Collections.unmodifiableList(nextProtocols);
+
+ /** The OpenSSL SSL_CTX object */
+ private final long ctx;
+ private final OpenSslSessionStats stats;
+
+ /**
+ * Creates a new instance.
+ *
+ * @param certChainFile an X.509 certificate chain file in PEM format
+ * @param keyFile a PKCS#8 private key file in PEM format
+ */
+ public OpenSslServerContext(File certChainFile, File keyFile) throws SSLException {
+ this(certChainFile, keyFile, null);
+ }
+
+ /**
+ * Creates a new instance.
+ *
+ * @param certChainFile an X.509 certificate chain file in PEM format
+ * @param keyFile a PKCS#8 private key file in PEM format
+ * @param keyPassword the password of the {@code keyFile}.
+ * {@code null} if it's not password-protected.
+ */
+ public OpenSslServerContext(File certChainFile, File keyFile, String keyPassword) throws SSLException {
+ this(certChainFile, keyFile, keyPassword, null, null, 0, 0);
+ }
+
+ /**
+ * Creates a new instance.
+ *
+ * @param certChainFile an X.509 certificate chain file in PEM format
+ * @param keyFile a PKCS#8 private key file in PEM format
+ * @param keyPassword the password of the {@code keyFile}.
+ * {@code null} if it's not password-protected.
+ * @param ciphers the cipher suites to enable, in the order of preference.
+ * {@code null} to use the default cipher suites.
+ * @param nextProtocols the application layer protocols to accept, in the order of preference.
+ * {@code null} to disable TLS NPN/ALPN extension.
+ * @param sessionCacheSize the size of the cache used for storing SSL session objects.
+ * {@code 0} to use the default value.
+ * @param sessionTimeout the timeout for the cached SSL session objects, in seconds.
+ * {@code 0} to use the default value.
+ */
+ public OpenSslServerContext(
+ File certChainFile, File keyFile, String keyPassword,
+ Iterable ciphers, Iterable nextProtocols,
+ long sessionCacheSize, long sessionTimeout) throws SSLException {
+
+ OpenSsl.ensureAvailability();
+
+ if (certChainFile == null) {
+ throw new NullPointerException("certChainFile");
+ }
+ if (!certChainFile.isFile()) {
+ throw new IllegalArgumentException("certChainFile is not a file: " + certChainFile);
+ }
+ if (keyFile == null) {
+ throw new NullPointerException("keyPath");
+ }
+ if (!keyFile.isFile()) {
+ throw new IllegalArgumentException("keyPath is not a file: " + keyFile);
+ }
+ if (ciphers == null) {
+ ciphers = DEFAULT_CIPHERS;
+ }
+
+ if (keyPassword == null) {
+ keyPassword = "";
+ }
+ if (nextProtocols == null) {
+ nextProtocols = Collections.emptyList();
+ }
+
+ for (String c: ciphers) {
+ if (c == null) {
+ break;
+ }
+ this.ciphers.add(c);
+ }
+
+ for (String p: nextProtocols) {
+ if (p == null) {
+ break;
+ }
+ this.nextProtocols.add(p);
+ }
+
+ // Allocate a new APR pool.
+ aprPool = Pool.create(0);
+
+ // Create a new SSL_CTX and configure it.
+ boolean success = false;
+ try {
+ synchronized (OpenSslServerContext.class) {
+ try {
+ ctx = SSLContext.make(aprPool, SSL.SSL_PROTOCOL_ALL, SSL.SSL_MODE_SERVER);
+ } catch (Exception e) {
+ throw new SSLException("failed to create an SSL_CTX", e);
+ }
+
+ SSLContext.setOptions(ctx, SSL.SSL_OP_ALL);
+ SSLContext.setOptions(ctx, SSL.SSL_OP_NO_SSLv2);
+ SSLContext.setOptions(ctx, SSL.SSL_OP_CIPHER_SERVER_PREFERENCE);
+ SSLContext.setOptions(ctx, SSL.SSL_OP_SINGLE_ECDH_USE);
+ SSLContext.setOptions(ctx, SSL.SSL_OP_SINGLE_DH_USE);
+ SSLContext.setOptions(ctx, SSL.SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION);
+
+ /* List the ciphers that the client is permitted to negotiate. */
+ try {
+ // Convert the cipher list into a colon-separated string.
+ StringBuilder cipherBuf = new StringBuilder();
+ for (String c: this.ciphers) {
+ cipherBuf.append(c);
+ cipherBuf.append(':');
+ }
+ cipherBuf.setLength(cipherBuf.length() - 1);
+
+ SSLContext.setCipherSuite(ctx, cipherBuf.toString());
+ } catch (SSLException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new SSLException("failed to set cipher suite: " + this.ciphers, e);
+ }
+
+ /* Set certificate verification policy. */
+ SSLContext.setVerify(ctx, SSL.SSL_CVERIFY_NONE, 10);
+
+ /* Load the certificate file and private key. */
+ try {
+ if (!SSLContext.setCertificate(
+ ctx, certChainFile.getPath(), keyFile.getPath(), keyPassword, SSL.SSL_AIDX_RSA)) {
+ throw new SSLException("failed to set certificate: " +
+ certChainFile + " and " + keyFile + " (" + SSL.getLastError() + ')');
+ }
+ } catch (SSLException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new SSLException("failed to set certificate: " + certChainFile + " and " + keyFile, e);
+ }
+
+ /* Load the certificate chain. We must skip the first cert since it was loaded above. */
+ if (!SSLContext.setCertificateChainFile(ctx, certChainFile.getPath(), true)) {
+ String error = SSL.getLastError();
+ if (!error.startsWith(OpenSsl.IGNORABLE_ERROR_PREFIX)) {
+ throw new SSLException(
+ "failed to set certificate chain: " + certChainFile + " (" + SSL.getLastError() + ')');
+ }
+ }
+
+ /* Set next protocols for next protocol negotiation extension, if specified */
+ if (!this.nextProtocols.isEmpty()) {
+ // Convert the protocol list into a comma-separated string.
+ StringBuilder nextProtocolBuf = new StringBuilder();
+ for (String p: this.nextProtocols) {
+ nextProtocolBuf.append(p);
+ nextProtocolBuf.append(',');
+ }
+ nextProtocolBuf.setLength(nextProtocolBuf.length() - 1);
+
+ SSLContext.setNextProtos(ctx, nextProtocolBuf.toString());
+ }
+
+ /* Set session cache size, if specified */
+ if (sessionCacheSize > 0) {
+ this.sessionCacheSize = sessionCacheSize;
+ SSLContext.setSessionCacheSize(ctx, sessionCacheSize);
+ } else {
+ // Get the default session cache size using SSLContext.setSessionCacheSize()
+ this.sessionCacheSize = sessionCacheSize = SSLContext.setSessionCacheSize(ctx, 20480);
+ // Revert the session cache size to the default value.
+ SSLContext.setSessionCacheSize(ctx, sessionCacheSize);
+ }
+
+ /* Set session timeout, if specified */
+ if (sessionTimeout > 0) {
+ this.sessionTimeout = sessionTimeout;
+ SSLContext.setSessionCacheTimeout(ctx, sessionTimeout);
+ } else {
+ // Get the default session timeout using SSLContext.setSessionCacheTimeout()
+ this.sessionTimeout = sessionTimeout = SSLContext.setSessionCacheTimeout(ctx, 300);
+ // Revert the session timeout to the default value.
+ SSLContext.setSessionCacheTimeout(ctx, sessionTimeout);
+ }
+ }
+ success = true;
+ } finally {
+ if (!success) {
+ destroyPools();
+ }
+ }
+
+ stats = new OpenSslSessionStats(ctx);
+ }
+
+ @Override
+ public boolean isClient() {
+ return false;
+ }
+
+ @Override
+ public List cipherSuites() {
+ return unmodifiableCiphers;
+ }
+
+ @Override
+ public long sessionCacheSize() {
+ return sessionCacheSize;
+ }
+
+ @Override
+ public long sessionTimeout() {
+ return sessionTimeout;
+ }
+
+ @Override
+ public ApplicationProtocolSelector nextProtocolSelector() {
+ return null;
+ }
+
+ @Override
+ public List nextProtocols() {
+ return unmodifiableNextProtocols;
+ }
+
+ /**
+ * Returns the {@code SSL_CTX} object of this context.
+ */
+ public long context() {
+ return ctx;
+ }
+
+ /**
+ * Returns the stats of this context.
+ */
+ public OpenSslSessionStats stats() {
+ return stats;
+ }
+
+ /**
+ * Returns a new server-side {@link javax.net.ssl.SSLEngine} with the current configuration.
+ */
+ @Override
+ public SSLEngine newEngine(ByteBufAllocator alloc) {
+ return new OpenSslEngine(ctx, alloc);
+ }
+
+ @Override
+ public SSLEngine newEngine(ByteBufAllocator alloc, String peerHost, int peerPort) {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * Sets the SSL session ticket keys of this context.
+ */
+ public void setTicketKeys(byte[] keys) {
+ if (keys != null) {
+ throw new NullPointerException("keys");
+ }
+ SSLContext.setSessionTicketKeys(ctx, keys);
+ }
+
+ @Override
+ @SuppressWarnings("FinalizeDeclaration")
+ protected void finalize() throws Throwable {
+ super.finalize();
+ synchronized (OpenSslServerContext.class) {
+ if (ctx != 0) {
+ SSLContext.free(ctx);
+ }
+ }
+
+ destroyPools();
+ }
+
+ private void destroyPools() {
+ if (aprPool != 0) {
+ Pool.destroy(aprPool);
+ }
+ }
+}
diff --git a/handler/src/main/java/io/netty/handler/ssl/OpenSslSessionStats.java b/handler/src/main/java/io/netty/handler/ssl/OpenSslSessionStats.java
new file mode 100644
index 0000000000..2ec514681d
--- /dev/null
+++ b/handler/src/main/java/io/netty/handler/ssl/OpenSslSessionStats.java
@@ -0,0 +1,122 @@
+/*
+ * Copyright 2014 The Netty Project
+ *
+ * The Netty Project 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 io.netty.handler.ssl;
+
+import org.apache.tomcat.jni.SSLContext;
+
+/**
+ * Stats exposed by an OpenSSL session context.
+ *
+ * @see SSL_CTX_sess_number
+ */
+public final class OpenSslSessionStats {
+
+ private final long context;
+
+ OpenSslSessionStats(long context) {
+ this.context = context;
+ }
+
+ /**
+ * Returns the current number of sessions in the internal session cache.
+ */
+ public long number() {
+ return SSLContext.sessionNumber(context);
+ }
+
+ /**
+ * Returns the number of started SSL/TLS handshakes in client mode.
+ */
+ public long connect() {
+ return SSLContext.sessionConnect(context);
+ }
+
+ /**
+ * Returns the number of successfully established SSL/TLS sessions in client mode.
+ */
+ public long connectGood() {
+ return SSLContext.sessionConnectGood(context);
+ }
+
+ /**
+ * Returns the number of start renegotiations in client mode.
+ */
+ public long connectRenegotiate() {
+ return SSLContext.sessionConnectRenegotiate(context);
+ }
+
+ /**
+ * Returns the number of started SSL/TLS handshakes in server mode.
+ */
+ public long accept() {
+ return SSLContext.sessionAccept(context);
+ }
+
+ /**
+ * Returns the number of successfully established SSL/TLS sessions in server mode.
+ */
+ public long acceptGood() {
+ return SSLContext.sessionAcceptGood(context);
+ }
+
+ /**
+ * Returns the number of start renegotiations in server mode.
+ */
+ public long acceptRenegotiate() {
+ return SSLContext.sessionAcceptRenegotiate(context);
+ }
+
+ /**
+ * Returns the number of successfully reused sessions. In client mode, a session set with {@code SSL_set_session}
+ * successfully reused is counted as a hit. In server mode, a session successfully retrieved from internal or
+ * external cache is counted as a hit.
+ */
+ public long hits() {
+ return SSLContext.sessionHits(context);
+ }
+
+ /**
+ * Returns the number of successfully retrieved sessions from the external session cache in server mode.
+ */
+ public long cbHits() {
+ return SSLContext.sessionCbHits(context);
+ }
+
+ /**
+ * Returns the number of sessions proposed by clients that were not found in the internal session cache
+ * in server mode.
+ */
+ public long misses() {
+ return SSLContext.sessionMisses(context);
+ }
+
+ /**
+ * Returns the number of sessions proposed by clients and either found in the internal or external session cache
+ * in server mode, but that were invalid due to timeout. These sessions are not included in the {@link #hits()}
+ * count.
+ */
+ public long timeouts() {
+ return SSLContext.sessionTimeouts(context);
+ }
+
+ /**
+ * Returns the number of sessions that were removed because the maximum session cache size was exceeded.
+ */
+ public long cacheFull() {
+ return SSLContext.sessionCacheFull(context);
+ }
+}
diff --git a/handler/src/main/java/io/netty/handler/ssl/PemReader.java b/handler/src/main/java/io/netty/handler/ssl/PemReader.java
new file mode 100644
index 0000000000..491359fe22
--- /dev/null
+++ b/handler/src/main/java/io/netty/handler/ssl/PemReader.java
@@ -0,0 +1,137 @@
+/*
+ * Copyright 2014 The Netty Project
+ *
+ * The Netty Project 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 io.netty.handler.ssl;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import io.netty.handler.codec.base64.Base64;
+import io.netty.util.CharsetUtil;
+import io.netty.util.internal.logging.InternalLogger;
+import io.netty.util.internal.logging.InternalLoggerFactory;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.security.KeyException;
+import java.security.KeyStore;
+import java.security.cert.CertificateException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Reads a PEM file and converts it into a list of DERs so that they are imported into a {@link KeyStore} easily.
+ */
+final class PemReader {
+
+ private static final InternalLogger logger = InternalLoggerFactory.getInstance(PemReader.class);
+
+ private static final Pattern CERT_PATTERN = Pattern.compile(
+ "-+BEGIN\\s+.*CERTIFICATE[^-]*-+(?:\\s|\\r|\\n)+" + // Header
+ "([a-z0-9+/=\\r\\n]+)" + // Base64 text
+ "-+END\\s+.*CERTIFICATE[^-]*-+", // Footer
+ Pattern.CASE_INSENSITIVE);
+ private static final Pattern KEY_PATTERN = Pattern.compile(
+ "-+BEGIN\\s+.*PRIVATE\\s+KEY[^-]*-+(?:\\s|\\r|\\n)+" + // Header
+ "([a-z0-9+/=\\r\\n]+)" + // Base64 text
+ "-+END\\s+.*PRIVATE\\s+KEY[^-]*-+", // Footer
+ Pattern.CASE_INSENSITIVE);
+
+ static ByteBuf[] readCertificates(File file) throws CertificateException {
+ String content;
+ try {
+ content = readContent(file);
+ } catch (IOException e) {
+ throw new CertificateException("failed to read a file: " + file, e);
+ }
+
+ List certs = new ArrayList();
+ Matcher m = CERT_PATTERN.matcher(content);
+ int start = 0;
+ for (;;) {
+ if (!m.find(start)) {
+ break;
+ }
+
+ certs.add(Base64.decode(Unpooled.copiedBuffer(m.group(1), CharsetUtil.US_ASCII)));
+ start = m.end();
+ }
+
+ if (certs.isEmpty()) {
+ throw new CertificateException("found no certificates: " + file);
+ }
+
+ return certs.toArray(new ByteBuf[certs.size()]);
+ }
+
+ static ByteBuf readPrivateKey(File file) throws KeyException {
+ String content;
+ try {
+ content = readContent(file);
+ } catch (IOException e) {
+ throw new KeyException("failed to read a file: " + file, e);
+ }
+
+ Matcher m = KEY_PATTERN.matcher(content);
+ if (!m.find()) {
+ throw new KeyException("found no private key: " + file);
+ }
+
+ return Base64.decode(Unpooled.copiedBuffer(m.group(1), CharsetUtil.US_ASCII));
+ }
+
+ private static String readContent(File file) throws IOException {
+ InputStream in = new FileInputStream(file);
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ try {
+ byte[] buf = new byte[8192];
+ for (;;) {
+ int ret = in.read(buf);
+ if (ret < 0) {
+ break;
+ }
+ out.write(buf, 0, ret);
+ }
+ return out.toString(CharsetUtil.US_ASCII.name());
+ } finally {
+ safeClose(in);
+ safeClose(out);
+ }
+ }
+
+ private static void safeClose(InputStream in) {
+ try {
+ in.close();
+ } catch (IOException e) {
+ logger.warn("Failed to close a stream.", e);
+ }
+ }
+
+ private static void safeClose(OutputStream out) {
+ try {
+ out.close();
+ } catch (IOException e) {
+ logger.warn("Failed to close a stream.", e);
+ }
+ }
+
+ private PemReader() { }
+}
diff --git a/handler/src/main/java/io/netty/handler/ssl/SslContext.java b/handler/src/main/java/io/netty/handler/ssl/SslContext.java
new file mode 100644
index 0000000000..8da2a14757
--- /dev/null
+++ b/handler/src/main/java/io/netty/handler/ssl/SslContext.java
@@ -0,0 +1,463 @@
+/*
+ * Copyright 2014 The Netty Project
+ *
+ * The Netty Project 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 io.netty.handler.ssl;
+
+import io.netty.buffer.ByteBufAllocator;
+import io.netty.channel.ChannelInitializer;
+import io.netty.channel.ChannelPipeline;
+
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLEngine;
+import javax.net.ssl.SSLException;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.TrustManagerFactory;
+import java.io.File;
+import java.util.List;
+
+/**
+ * A secure socket protocol implementation which acts as a factory for {@link SSLEngine} and {@link SslHandler}.
+ * Internally, it is implemented via JDK's {@link SSLContext} or OpenSSL's {@code SSL_CTX}.
+ *
+ *
+ */
+public abstract class SslContext {
+
+ /**
+ * Returns the default server-side implementation provider currently in use.
+ *
+ * @return {@link SslProvider#OPENSSL} if OpenSSL is available. {@link SslProvider#JDK} otherwise.
+ */
+ public static SslProvider defaultServerProvider() {
+ if (OpenSsl.isAvailable()) {
+ return SslProvider.OPENSSL;
+ } else {
+ return SslProvider.JDK;
+ }
+ }
+
+ /**
+ * Returns the default client-side implementation provider currently in use.
+ *
+ * @return {@link SslProvider#JDK}, because it is the only implementation at the moment
+ */
+ public static SslProvider defaultClientProvider() {
+ return SslProvider.JDK;
+ }
+
+ /**
+ * Creates a new server-side {@link SslContext}.
+ *
+ * @param certChainFile an X.509 certificate chain file in PEM format
+ * @param keyFile a PKCS#8 private key file in PEM format
+ * @return a new server-side {@link SslContext}
+ */
+ public static SslContext newServerContext(File certChainFile, File keyFile) throws SSLException {
+ return newServerContext(null, certChainFile, keyFile, null, null, null, 0, 0);
+ }
+
+ /**
+ * Creates a new server-side {@link SslContext}.
+ *
+ * @param certChainFile an X.509 certificate chain file in PEM format
+ * @param keyFile a PKCS#8 private key file in PEM format
+ * @param keyPassword the password of the {@code keyFile}.
+ * {@code null} if it's not password-protected.
+ * @return a new server-side {@link SslContext}
+ */
+ public static SslContext newServerContext(
+ File certChainFile, File keyFile, String keyPassword) throws SSLException {
+ return newServerContext(null, certChainFile, keyFile, keyPassword, null, null, 0, 0);
+ }
+
+ /**
+ * Creates a new server-side {@link SslContext}.
+ *
+ * @param certChainFile an X.509 certificate chain file in PEM format
+ * @param keyFile a PKCS#8 private key file in PEM format
+ * @param keyPassword the password of the {@code keyFile}.
+ * {@code null} if it's not password-protected.
+ * @param ciphers the cipher suites to enable, in the order of preference.
+ * {@code null} to use the default cipher suites.
+ * @param nextProtocols the application layer protocols to accept, in the order of preference.
+ * {@code null} to disable TLS NPN/ALPN extension.
+ * @param sessionCacheSize the size of the cache used for storing SSL session objects.
+ * {@code 0} to use the default value.
+ * @param sessionTimeout the timeout for the cached SSL session objects, in seconds.
+ * {@code 0} to use the default value.
+ * @return a new server-side {@link SslContext}
+ */
+ public static SslContext newServerContext(
+ File certChainFile, File keyFile, String keyPassword,
+ Iterable ciphers, Iterable nextProtocols,
+ long sessionCacheSize, long sessionTimeout) throws SSLException {
+ return newServerContext(
+ null, certChainFile, keyFile, keyPassword,
+ ciphers, nextProtocols, sessionCacheSize, sessionTimeout);
+ }
+
+ /**
+ * Creates a new server-side {@link SslContext}.
+ *
+ * @param provider the {@link SslContext} implementation to use.
+ * {@code null} to use the current default one.
+ * @param certChainFile an X.509 certificate chain file in PEM format
+ * @param keyFile a PKCS#8 private key file in PEM format
+ * @return a new server-side {@link SslContext}
+ */
+ public static SslContext newServerContext(
+ SslProvider provider, File certChainFile, File keyFile) throws SSLException {
+ return newServerContext(provider, certChainFile, keyFile, null, null, null, 0, 0);
+ }
+
+ /**
+ * Creates a new server-side {@link SslContext}.
+ *
+ * @param provider the {@link SslContext} implementation to use.
+ * {@code null} to use the current default one.
+ * @param certChainFile an X.509 certificate chain file in PEM format
+ * @param keyFile a PKCS#8 private key file in PEM format
+ * @param keyPassword the password of the {@code keyFile}.
+ * {@code null} if it's not password-protected.
+ * @return a new server-side {@link SslContext}
+ */
+ public static SslContext newServerContext(
+ SslProvider provider, File certChainFile, File keyFile, String keyPassword) throws SSLException {
+ return newServerContext(provider, certChainFile, keyFile, keyPassword, null, null, 0, 0);
+ }
+
+ /**
+ * Creates a new server-side {@link SslContext}.
+ *
+ * @param provider the {@link SslContext} implementation to use.
+ * {@code null} to use the current default one.
+ * @param certChainFile an X.509 certificate chain file in PEM format
+ * @param keyFile a PKCS#8 private key file in PEM format
+ * @param keyPassword the password of the {@code keyFile}.
+ * {@code null} if it's not password-protected.
+ * @param ciphers the cipher suites to enable, in the order of preference.
+ * {@code null} to use the default cipher suites.
+ * @param nextProtocols the application layer protocols to accept, in the order of preference.
+ * {@code null} to disable TLS NPN/ALPN extension.
+ * @param sessionCacheSize the size of the cache used for storing SSL session objects.
+ * {@code 0} to use the default value.
+ * @param sessionTimeout the timeout for the cached SSL session objects, in seconds.
+ * {@code 0} to use the default value.
+ * @return a new server-side {@link SslContext}
+ */
+ public static SslContext newServerContext(
+ SslProvider provider,
+ File certChainFile, File keyFile, String keyPassword,
+ Iterable ciphers, Iterable nextProtocols,
+ long sessionCacheSize, long sessionTimeout) throws SSLException {
+
+ if (provider == null) {
+ provider = OpenSsl.isAvailable()? SslProvider.OPENSSL : SslProvider.JDK;
+ }
+
+ switch (provider) {
+ case JDK:
+ return new JdkSslServerContext(
+ certChainFile, keyFile, keyPassword,
+ ciphers, nextProtocols, sessionCacheSize, sessionTimeout);
+ case OPENSSL:
+ return new OpenSslServerContext(
+ certChainFile, keyFile, keyPassword,
+ ciphers, nextProtocols, sessionCacheSize, sessionTimeout);
+ default:
+ throw new Error(provider.toString());
+ }
+ }
+
+ /**
+ * Creates a new client-side {@link SslContext}.
+ *
+ * @return a new client-side {@link SslContext}
+ */
+ public static SslContext newClientContext() throws SSLException {
+ return newClientContext(null, null, null, null, null, 0, 0);
+ }
+
+ /**
+ * Creates a new client-side {@link SslContext}.
+ *
+ * @param certChainFile an X.509 certificate chain file in PEM format
+ *
+ * @return a new client-side {@link SslContext}
+ */
+ public static SslContext newClientContext(File certChainFile) throws SSLException {
+ return newClientContext(null, certChainFile, null, null, null, 0, 0);
+ }
+
+ /**
+ * Creates a new client-side {@link SslContext}.
+ *
+ * @param trustManagerFactory the {@link TrustManagerFactory} that provides the {@link TrustManager}s
+ * that verifies the certificates sent from servers.
+ * {@code null} to use the default.
+ *
+ * @return a new client-side {@link SslContext}
+ */
+ public static SslContext newClientContext(TrustManagerFactory trustManagerFactory) throws SSLException {
+ return newClientContext(null, null, trustManagerFactory, null, null, 0, 0);
+ }
+
+ /**
+ * Creates a new client-side {@link SslContext}.
+ *
+ * @param certChainFile an X.509 certificate chain file in PEM format.
+ * {@code null} to use the system default
+ * @param trustManagerFactory the {@link TrustManagerFactory} that provides the {@link TrustManager}s
+ * that verifies the certificates sent from servers.
+ * {@code null} to use the default.
+ *
+ * @return a new client-side {@link SslContext}
+ */
+ public static SslContext newClientContext(
+ File certChainFile, TrustManagerFactory trustManagerFactory) throws SSLException {
+ return newClientContext(null, certChainFile, trustManagerFactory, null, null, 0, 0);
+ }
+
+ /**
+ * Creates a new client-side {@link SslContext}.
+ *
+ * @param certChainFile an X.509 certificate chain file in PEM format.
+ * {@code null} to use the system default
+ * @param trustManagerFactory the {@link TrustManagerFactory} that provides the {@link TrustManager}s
+ * that verifies the certificates sent from servers.
+ * {@code null} to use the default.
+ * @param ciphers the cipher suites to enable, in the order of preference.
+ * {@code null} to use the default cipher suites.
+ * @param nextProtocolSelector the {@link ApplicationProtocolSelector} that chooses one of the application layer
+ * protocols returned by a TLS server.
+ * {@code null} to disable TLS NPN/ALPN extension.
+ * @param sessionCacheSize the size of the cache used for storing SSL session objects.
+ * {@code 0} to use the default value.
+ * @param sessionTimeout the timeout for the cached SSL session objects, in seconds.
+ * {@code 0} to use the default value.
+ *
+ * @return a new client-side {@link SslContext}
+ */
+ public static SslContext newClientContext(
+ File certChainFile, TrustManagerFactory trustManagerFactory,
+ Iterable ciphers, ApplicationProtocolSelector nextProtocolSelector,
+ long sessionCacheSize, long sessionTimeout) throws SSLException {
+ return newClientContext(
+ null, certChainFile, trustManagerFactory,
+ ciphers, nextProtocolSelector, sessionCacheSize, sessionTimeout);
+ }
+
+ /**
+ * Creates a new client-side {@link SslContext}.
+ *
+ * @param provider the {@link SslContext} implementation to use.
+ * {@code null} to use the current default one.
+ *
+ * @return a new client-side {@link SslContext}
+ */
+ public static SslContext newClientContext(SslProvider provider) throws SSLException {
+ return newClientContext(provider, null, null, null, null, 0, 0);
+ }
+
+ /**
+ * Creates a new client-side {@link SslContext}.
+ *
+ * @param provider the {@link SslContext} implementation to use.
+ * {@code null} to use the current default one.
+ * @param certChainFile an X.509 certificate chain file in PEM format.
+ * {@code null} to use the system default
+ *
+ * @return a new client-side {@link SslContext}
+ */
+ public static SslContext newClientContext(SslProvider provider, File certChainFile) throws SSLException {
+ return newClientContext(provider, certChainFile, null, null, null, 0, 0);
+ }
+
+ /**
+ * Creates a new client-side {@link SslContext}.
+ *
+ * @param provider the {@link SslContext} implementation to use.
+ * {@code null} to use the current default one.
+ * @param trustManagerFactory the {@link TrustManagerFactory} that provides the {@link TrustManager}s
+ * that verifies the certificates sent from servers.
+ * {@code null} to use the default.
+ *
+ * @return a new client-side {@link SslContext}
+ */
+ public static SslContext newClientContext(
+ SslProvider provider, TrustManagerFactory trustManagerFactory) throws SSLException {
+ return newClientContext(provider, null, trustManagerFactory, null, null, 0, 0);
+ }
+
+ /**
+ * Creates a new client-side {@link SslContext}.
+ *
+ * @param provider the {@link SslContext} implementation to use.
+ * {@code null} to use the current default one.
+ * @param certChainFile an X.509 certificate chain file in PEM format.
+ * {@code null} to use the system default
+ * @param trustManagerFactory the {@link TrustManagerFactory} that provides the {@link TrustManager}s
+ * that verifies the certificates sent from servers.
+ * {@code null} to use the default.
+ *
+ * @return a new client-side {@link SslContext}
+ */
+ public static SslContext newClientContext(
+ SslProvider provider, File certChainFile, TrustManagerFactory trustManagerFactory) throws SSLException {
+ return newClientContext(provider, certChainFile, trustManagerFactory, null, null, 0, 0);
+ }
+
+ /**
+ * Creates a new client-side {@link SslContext}.
+ *
+ * @param provider the {@link SslContext} implementation to use.
+ * {@code null} to use the current default one.
+ * @param certChainFile an X.509 certificate chain file in PEM format.
+ * {@code null} to use the system default
+ * @param trustManagerFactory the {@link TrustManagerFactory} that provides the {@link TrustManager}s
+ * that verifies the certificates sent from servers.
+ * {@code null} to use the default.
+ * @param ciphers the cipher suites to enable, in the order of preference.
+ * {@code null} to use the default cipher suites.
+ * @param nextProtocolSelector the {@link ApplicationProtocolSelector} that chooses one of the application layer
+ * protocols returned by a TLS server.
+ * {@code null} to disable TLS NPN/ALPN extension.
+ * @param sessionCacheSize the size of the cache used for storing SSL session objects.
+ * {@code 0} to use the default value.
+ * @param sessionTimeout the timeout for the cached SSL session objects, in seconds.
+ * {@code 0} to use the default value.
+ *
+ * @return a new client-side {@link SslContext}
+ */
+ public static SslContext newClientContext(
+ SslProvider provider,
+ File certChainFile, TrustManagerFactory trustManagerFactory,
+ Iterable ciphers, ApplicationProtocolSelector nextProtocolSelector,
+ long sessionCacheSize, long sessionTimeout) throws SSLException {
+
+ if (provider != null && provider != SslProvider.JDK) {
+ throw new SSLException("client context unsupported for: " + provider);
+ }
+
+ return new JdkSslClientContext(
+ certChainFile, trustManagerFactory,
+ ciphers, nextProtocolSelector, sessionCacheSize, sessionTimeout);
+ }
+
+ SslContext() { }
+
+ /**
+ * Returns {@code true} if and only if this context is for server-side.
+ */
+ public final boolean isServer() {
+ return !isClient();
+ }
+
+ /**
+ * Returns the {@code true} if and only if this context is for client-side.
+ */
+ public abstract boolean isClient();
+
+ /**
+ * Returns the list of enabled cipher suites, in the order of preference.
+ */
+ public abstract List cipherSuites();
+
+ /**
+ * Returns the size of the cache used for storing SSL session objects.
+ */
+ public abstract long sessionCacheSize();
+
+ /**
+ * Returns the timeout for the cached SSL session objects, in seconds.
+ */
+ public abstract long sessionTimeout();
+
+ /**
+ * Returns the client-side {@link ApplicationProtocolSelector} for the TLS NPN/ALPN extension.
+ *
+ * @return the client-side {@link ApplicationProtocolSelector}.
+ * {@code null} if NPN/ALPN extension has been disabled.
+ */
+ public abstract ApplicationProtocolSelector nextProtocolSelector();
+
+ /**
+ * Returns the list of server-side application layer protocols for the TLS NPN/ALPN extension,
+ * in the order of preference.
+ *
+ * @return the list of server-side application layer protocols.
+ * {@code null} if NPN/ALPN extension has been disabled.
+ */
+ public abstract List nextProtocols();
+
+ /**
+ * Creates a new {@link SSLEngine}.
+ *
+ * @return a new {@link SSLEngine}
+ */
+ public abstract SSLEngine newEngine(ByteBufAllocator alloc);
+
+ /**
+ * Creates a new {@link SSLEngine} using advisory peer information.
+ *
+ * @param peerHost the non-authoritative name of the host
+ * @param peerPort the non-authoritative port
+ *
+ * @return a new {@link SSLEngine}
+ */
+ public abstract SSLEngine newEngine(ByteBufAllocator alloc, String peerHost, int peerPort);
+
+ /**
+ * Creates a new {@link SslHandler}.
+ *
+ * @return a new {@link SslHandler}
+ */
+ public final SslHandler newHandler(ByteBufAllocator alloc) {
+ return newHandler(newEngine(alloc));
+ }
+
+ /**
+ * Creates a new {@link SslHandler} with advisory peer information.
+ *
+ * @param peerHost the non-authoritative name of the host
+ * @param peerPort the non-authoritative port
+ *
+ * @return a new {@link SslHandler}
+ */
+ public final SslHandler newHandler(ByteBufAllocator alloc, String peerHost, int peerPort) {
+ return newHandler(newEngine(alloc, peerHost, peerPort));
+ }
+
+ private static SslHandler newHandler(SSLEngine engine) {
+ return new SslHandler(engine);
+ }
+}
diff --git a/handler/src/main/java/io/netty/handler/ssl/SslHandler.java b/handler/src/main/java/io/netty/handler/ssl/SslHandler.java
index 33920e3419..f5d4124e0e 100644
--- a/handler/src/main/java/io/netty/handler/ssl/SslHandler.java
+++ b/handler/src/main/java/io/netty/handler/ssl/SslHandler.java
@@ -848,7 +848,7 @@ public class SslHandler extends ByteToMessageDecoder {
packet.limit(packet.position() + recordLengths[i]);
try {
unwrapSingle(ctx, packet, totalLength);
- assert !packet.hasRemaining();
+ assert !packet.hasRemaining() || engine.isInboundDone();
} finally {
ByteBuf decodeOut = this.decodeOut;
if (decodeOut != null && decodeOut.isReadable()) {
@@ -986,6 +986,9 @@ public class SslHandler extends ByteToMessageDecoder {
*/
private void setHandshakeSuccess() {
if (handshakePromise.trySuccess(ctx.channel())) {
+ if (logger.isDebugEnabled()) {
+ logger.debug(ctx.channel() + " HANDSHAKEN: " + engine.getSession().getCipherSuite());
+ }
ctx.fireUserEventTriggered(SslHandshakeCompletionEvent.SUCCESS);
}
}
@@ -1050,7 +1053,7 @@ public class SslHandler extends ByteToMessageDecoder {
public void handlerAdded(final ChannelHandlerContext ctx) throws Exception {
this.ctx = ctx;
- if (ctx.channel().isActive()) {
+ if (ctx.channel().isActive() && engine.getUseClientMode()) {
// channelActive() event has been fired already, which means this.channelActive() will
// not be invoked. We have to initialize here instead.
handshake();
diff --git a/handler/src/main/java/io/netty/handler/ssl/SslProvider.java b/handler/src/main/java/io/netty/handler/ssl/SslProvider.java
new file mode 100644
index 0000000000..3d4f08bfa9
--- /dev/null
+++ b/handler/src/main/java/io/netty/handler/ssl/SslProvider.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2014 The Netty Project
+ *
+ * The Netty Project 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 io.netty.handler.ssl;
+
+/**
+ * An enumeration of SSL/TLS protocol providers.
+ */
+public enum SslProvider {
+ /**
+ * JDK's default implementation.
+ */
+ JDK,
+ /**
+ * OpenSSL-based implementation.
+ */
+ OPENSSL
+}
diff --git a/handler/src/main/java/io/netty/handler/ssl/util/BouncyCastleSelfSignedCertGenerator.java b/handler/src/main/java/io/netty/handler/ssl/util/BouncyCastleSelfSignedCertGenerator.java
new file mode 100644
index 0000000000..88a7c9dbab
--- /dev/null
+++ b/handler/src/main/java/io/netty/handler/ssl/util/BouncyCastleSelfSignedCertGenerator.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2014 The Netty Project
+ *
+ * The Netty Project 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 io.netty.handler.ssl.util;
+
+import org.bouncycastle.asn1.x500.X500Name;
+import org.bouncycastle.cert.X509CertificateHolder;
+import org.bouncycastle.cert.X509v3CertificateBuilder;
+import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
+import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder;
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
+import org.bouncycastle.operator.ContentSigner;
+import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
+
+import java.math.BigInteger;
+import java.security.KeyPair;
+import java.security.PrivateKey;
+import java.security.Provider;
+import java.security.SecureRandom;
+import java.security.cert.X509Certificate;
+
+import static io.netty.handler.ssl.util.SelfSignedCertificate.*;
+
+/**
+ * Generates a self-signed certificate using Bouncy Castle.
+ */
+final class BouncyCastleSelfSignedCertGenerator {
+
+ private static final Provider PROVIDER = new BouncyCastleProvider();
+
+ static String[] generate(String fqdn, KeyPair keypair, SecureRandom random) throws Exception {
+ PrivateKey key = keypair.getPrivate();
+
+ // Prepare the information required for generating an X.509 certificate.
+ X500Name owner = new X500Name("CN=" + fqdn);
+ X509v3CertificateBuilder builder = new JcaX509v3CertificateBuilder(
+ owner, new BigInteger(64, random), NOT_BEFORE, NOT_AFTER, owner, keypair.getPublic());
+
+ ContentSigner signer = new JcaContentSignerBuilder("SHA256WithRSAEncryption").build(key);
+ X509CertificateHolder certHolder = builder.build(signer);
+ X509Certificate cert = new JcaX509CertificateConverter().setProvider(PROVIDER).getCertificate(certHolder);
+ cert.verify(keypair.getPublic());
+
+ return newSelfSignedCertificate(fqdn, key, cert);
+ }
+
+ private BouncyCastleSelfSignedCertGenerator() { }
+}
diff --git a/handler/src/main/java/io/netty/handler/ssl/util/FingerprintTrustManagerFactory.java b/handler/src/main/java/io/netty/handler/ssl/util/FingerprintTrustManagerFactory.java
new file mode 100644
index 0000000000..218ae1a48b
--- /dev/null
+++ b/handler/src/main/java/io/netty/handler/ssl/util/FingerprintTrustManagerFactory.java
@@ -0,0 +1,207 @@
+/*
+ * Copyright 2014 The Netty Project
+ *
+ * The Netty Project 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 io.netty.handler.ssl.util;
+
+import io.netty.buffer.ByteBufUtil;
+import io.netty.buffer.Unpooled;
+import io.netty.util.internal.EmptyArrays;
+
+import javax.net.ssl.ManagerFactoryParameters;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.TrustManagerFactory;
+import javax.net.ssl.X509TrustManager;
+import java.security.KeyStore;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.security.cert.CertificateEncodingException;
+import java.security.cert.CertificateException;
+import java.security.cert.X509Certificate;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.regex.Pattern;
+
+/**
+ * An {@link TrustManagerFactory} that trusts an X.509 certificate whose SHA1 checksum matches.
+ *
+ * NOTE:
+ * Never use this {@link TrustManagerFactory} in production unless you are not sure what you are exactly doing with it.
+ *
+ * The SHA1 checksum of an X.509 certificate is calculated from its DER encoded format. You can get the fingerprint of
+ * an X.509 certificate using the {@code openssl} command. For example:
+ *
+ */
+public final class FingerprintTrustManagerFactory extends SimpleTrustManagerFactory {
+
+ private static final Pattern FINGERPRINT_PATTERN = Pattern.compile("^[0-9a-fA-F:]+$");
+ private static final Pattern FINGERPRINT_STRIP_PATTERN = Pattern.compile(":");
+ private static final int SHA1_BYTE_LEN = 20;
+ private static final int SHA1_HEX_LEN = SHA1_BYTE_LEN * 2;
+
+ private static final ThreadLocal tlmd = new ThreadLocal() {
+ @Override
+ protected MessageDigest initialValue() {
+ try {
+ return MessageDigest.getInstance("SHA1");
+ } catch (NoSuchAlgorithmException e) {
+ // All Java implementation must have SHA1 digest algorithm.
+ throw new Error(e);
+ }
+ }
+ };
+
+ private final TrustManager tm = new X509TrustManager() {
+
+ @Override
+ public void checkClientTrusted(X509Certificate[] chain, String s) throws CertificateException {
+ checkTrusted("client", chain);
+ }
+
+ @Override
+ public void checkServerTrusted(X509Certificate[] chain, String s) throws CertificateException {
+ checkTrusted("server", chain);
+ }
+
+ private void checkTrusted(String type, X509Certificate[] chain) throws CertificateException {
+ X509Certificate cert = chain[0];
+ byte[] fingerprint = fingerprint(cert);
+ boolean found = false;
+ for (byte[] allowedFingerprint: fingerprints) {
+ if (Arrays.equals(fingerprint, allowedFingerprint)) {
+ found = true;
+ break;
+ }
+ }
+
+ if (!found) {
+ throw new CertificateException(
+ type + " certificate with unknown fingerprint: " + cert.getSubjectDN());
+ }
+ }
+
+ private byte[] fingerprint(X509Certificate cert) throws CertificateEncodingException {
+ MessageDigest md = tlmd.get();
+ md.reset();
+ return md.digest(cert.getEncoded());
+ }
+
+ @Override
+ public X509Certificate[] getAcceptedIssuers() {
+ return EmptyArrays.EMPTY_X509_CERTIFICATES;
+ }
+ };
+
+ private final byte[][] fingerprints;
+
+ /**
+ * Creates a new instance.
+ *
+ * @param fingerprints a list of SHA1 fingerprints in heaxdecimal form
+ */
+ public FingerprintTrustManagerFactory(Iterable fingerprints) {
+ this(toFingerprintArray(fingerprints));
+ }
+
+ /**
+ * Creates a new instance.
+ *
+ * @param fingerprints a list of SHA1 fingerprints in heaxdecimal form
+ */
+ public FingerprintTrustManagerFactory(String... fingerprints) {
+ this(toFingerprintArray(Arrays.asList(fingerprints)));
+ }
+
+ /**
+ * Creates a new instance.
+ *
+ * @param fingerprints a list of SHA1 fingerprints
+ */
+ public FingerprintTrustManagerFactory(byte[]... fingerprints) {
+ if (fingerprints == null) {
+ throw new NullPointerException("fingerprints");
+ }
+
+ List list = new ArrayList();
+ for (byte[] f: fingerprints) {
+ if (f == null) {
+ break;
+ }
+ if (f.length != SHA1_BYTE_LEN) {
+ throw new IllegalArgumentException("malformed fingerprint: " +
+ ByteBufUtil.hexDump(Unpooled.wrappedBuffer(f)) + " (expected: SHA1)");
+ }
+ list.add(f.clone());
+ }
+
+ this.fingerprints = list.toArray(new byte[list.size()][]);
+ }
+
+ private static byte[][] toFingerprintArray(Iterable fingerprints) {
+ if (fingerprints == null) {
+ throw new NullPointerException("fingerprints");
+ }
+
+ List list = new ArrayList();
+ for (String f: fingerprints) {
+ if (f == null) {
+ break;
+ }
+
+ if (!FINGERPRINT_PATTERN.matcher(f).matches()) {
+ throw new IllegalArgumentException("malformed fingerprint: " + f);
+ }
+ f = FINGERPRINT_STRIP_PATTERN.matcher(f).replaceAll("");
+ if (f.length() != SHA1_HEX_LEN) {
+ throw new IllegalArgumentException("malformed fingerprint: " + f + " (expected: SHA1)");
+ }
+
+ byte[] farr = new byte[SHA1_BYTE_LEN];
+ for (int i = 0; i < farr.length; i ++) {
+ int strIdx = i << 1;
+ farr[i] = (byte) Integer.parseInt(f.substring(strIdx, strIdx + 2), 16);
+ }
+ }
+
+ return list.toArray(new byte[list.size()][]);
+ }
+
+ @Override
+ protected void engineInit(KeyStore keyStore) throws Exception { }
+
+ @Override
+ protected void engineInit(ManagerFactoryParameters managerFactoryParameters) throws Exception { }
+
+ @Override
+ protected TrustManager[] engineGetTrustManagers() {
+ return new TrustManager[] { tm };
+ }
+}
diff --git a/handler/src/main/java/io/netty/handler/ssl/util/InsecureTrustManagerFactory.java b/handler/src/main/java/io/netty/handler/ssl/util/InsecureTrustManagerFactory.java
new file mode 100644
index 0000000000..04799758fd
--- /dev/null
+++ b/handler/src/main/java/io/netty/handler/ssl/util/InsecureTrustManagerFactory.java
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2014 The Netty Project
+ *
+ * The Netty Project 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 io.netty.handler.ssl.util;
+
+import io.netty.util.internal.EmptyArrays;
+import io.netty.util.internal.logging.InternalLogger;
+import io.netty.util.internal.logging.InternalLoggerFactory;
+
+import javax.net.ssl.ManagerFactoryParameters;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.TrustManagerFactory;
+import javax.net.ssl.X509TrustManager;
+import java.security.KeyStore;
+import java.security.cert.X509Certificate;
+
+/**
+ * An insecure {@link javax.net.ssl.TrustManagerFactory} that trusts all X.509 certificates without any verification.
+ *
+ * NOTE:
+ * Never use this {@link javax.net.ssl.TrustManagerFactory} in production.
+ * It is purely for testing purposes, and thus it is very insecure.
+ *
+ */
+public final class InsecureTrustManagerFactory extends SimpleTrustManagerFactory {
+
+ private static final InternalLogger logger = InternalLoggerFactory.getInstance(InsecureTrustManagerFactory.class);
+
+ public static final TrustManagerFactory INSTANCE = new InsecureTrustManagerFactory();
+
+ private static final TrustManager tm = new X509TrustManager() {
+ @Override
+ public void checkClientTrusted(X509Certificate[] chain, String s) {
+ logger.debug("Accepting a client certificate: " + chain[0].getSubjectDN());
+ }
+
+ @Override
+ public void checkServerTrusted(X509Certificate[] chain, String s) {
+ logger.debug("Accepting a server certificate: " + chain[0].getSubjectDN());
+ }
+
+ @Override
+ public X509Certificate[] getAcceptedIssuers() {
+ return EmptyArrays.EMPTY_X509_CERTIFICATES;
+ }
+ };
+
+ private InsecureTrustManagerFactory() { }
+
+ @Override
+ protected void engineInit(KeyStore keyStore) throws Exception { }
+
+ @Override
+ protected void engineInit(ManagerFactoryParameters managerFactoryParameters) throws Exception { }
+
+ @Override
+ protected TrustManager[] engineGetTrustManagers() {
+ return new TrustManager[] { tm };
+ }
+}
diff --git a/handler/src/main/java/io/netty/handler/ssl/util/OpenJdkSelfSignedCertGenerator.java b/handler/src/main/java/io/netty/handler/ssl/util/OpenJdkSelfSignedCertGenerator.java
new file mode 100644
index 0000000000..691f50b0cd
--- /dev/null
+++ b/handler/src/main/java/io/netty/handler/ssl/util/OpenJdkSelfSignedCertGenerator.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2014 The Netty Project
+ *
+ * The Netty Project 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 io.netty.handler.ssl.util;
+
+import sun.security.x509.AlgorithmId;
+import sun.security.x509.CertificateAlgorithmId;
+import sun.security.x509.CertificateIssuerName;
+import sun.security.x509.CertificateSerialNumber;
+import sun.security.x509.CertificateSubjectName;
+import sun.security.x509.CertificateValidity;
+import sun.security.x509.CertificateVersion;
+import sun.security.x509.CertificateX509Key;
+import sun.security.x509.X500Name;
+import sun.security.x509.X509CertImpl;
+import sun.security.x509.X509CertInfo;
+
+import java.math.BigInteger;
+import java.security.KeyPair;
+import java.security.PrivateKey;
+import java.security.SecureRandom;
+
+import static io.netty.handler.ssl.util.SelfSignedCertificate.*;
+
+/**
+ * Generates a self-signed certificate using {@code sun.security.x509} package provided by OpenJDK.
+ */
+final class OpenJdkSelfSignedCertGenerator {
+
+ static String[] generate(String fqdn, KeyPair keypair, SecureRandom random) throws Exception {
+ PrivateKey key = keypair.getPrivate();
+
+ // Prepare the information required for generating an X.509 certificate.
+ X509CertInfo info = new X509CertInfo();
+ X500Name owner = new X500Name("CN=" + fqdn);
+ info.set(X509CertInfo.VERSION, new CertificateVersion(CertificateVersion.V3));
+ info.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(new BigInteger(64, random)));
+ info.set(X509CertInfo.SUBJECT, new CertificateSubjectName(owner));
+ info.set(X509CertInfo.ISSUER, new CertificateIssuerName(owner));
+ info.set(X509CertInfo.VALIDITY, new CertificateValidity(NOT_BEFORE, NOT_AFTER));
+ info.set(X509CertInfo.KEY, new CertificateX509Key(keypair.getPublic()));
+ info.set(X509CertInfo.ALGORITHM_ID,
+ new CertificateAlgorithmId(new AlgorithmId(AlgorithmId.sha1WithRSAEncryption_oid)));
+
+ // Sign the cert to identify the algorithm that's used.
+ X509CertImpl cert = new X509CertImpl(info);
+ cert.sign(key, "SHA1withRSA");
+
+ // Update the algorithm and sign again.
+ info.set(CertificateAlgorithmId.NAME + '.' + CertificateAlgorithmId.ALGORITHM, cert.get(X509CertImpl.SIG_ALG));
+ cert = new X509CertImpl(info);
+ cert.sign(key, "SHA1withRSA");
+ cert.verify(keypair.getPublic());
+
+ return newSelfSignedCertificate(fqdn, key, cert);
+ }
+
+ private OpenJdkSelfSignedCertGenerator() { }
+}
diff --git a/handler/src/main/java/io/netty/handler/ssl/util/SelfSignedCertificate.java b/handler/src/main/java/io/netty/handler/ssl/util/SelfSignedCertificate.java
new file mode 100644
index 0000000000..54257a7697
--- /dev/null
+++ b/handler/src/main/java/io/netty/handler/ssl/util/SelfSignedCertificate.java
@@ -0,0 +1,207 @@
+/*
+ * Copyright 2014 The Netty Project
+ *
+ * The Netty Project 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 io.netty.handler.ssl.util;
+
+import io.netty.buffer.Unpooled;
+import io.netty.handler.codec.base64.Base64;
+import io.netty.util.CharsetUtil;
+import io.netty.util.internal.logging.InternalLogger;
+import io.netty.util.internal.logging.InternalLoggerFactory;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.security.KeyPair;
+import java.security.KeyPairGenerator;
+import java.security.NoSuchAlgorithmException;
+import java.security.PrivateKey;
+import java.security.SecureRandom;
+import java.security.cert.CertificateEncodingException;
+import java.security.cert.CertificateException;
+import java.security.cert.X509Certificate;
+import java.util.Date;
+
+/**
+ * Generates a temporary self-signed certificate for testing purposes.
+ *
+ * NOTE:
+ * Never use the certificate and private key generated by this class in production.
+ * It is purely for testing purposes, and thus it is very insecure.
+ * It even uses an insecure pseudo-random generator for faster generation internally.
+ *
+ * A X.509 certificate file and a RSA private key file are generated in a system's temporary directory using
+ * {@link java.io.File#createTempFile(String, String)}, and they are deleted when the JVM exits using
+ * {@link java.io.File#deleteOnExit()}.
+ *
+ * At first, this method tries to use OpenJDK's X.509 implementation (the {@code sun.security.x509} package).
+ * If it fails, it tries to use Bouncy Castle as a fallback.
+ *
+ */
+public final class SelfSignedCertificate {
+
+ private static final InternalLogger logger = InternalLoggerFactory.getInstance(SelfSignedCertificate.class);
+
+ /** Current time minus 1 year, just in case software clock goes back due to time synchronization */
+ static final Date NOT_BEFORE = new Date(System.currentTimeMillis() - 86400000L * 365);
+ /** The maximum possible value in X.509 specification: 9999-12-31 23:59:59 */
+ static final Date NOT_AFTER = new Date(253402300799000L);
+
+ private final File certificate;
+ private final File privateKey;
+
+ /**
+ * Creates a new instance.
+ */
+ public SelfSignedCertificate() throws CertificateException {
+ this("example.com");
+ }
+
+ /**
+ * Creates a new instance.
+ *
+ * @param fqdn a fully qualified domain name
+ */
+ public SelfSignedCertificate(String fqdn) throws CertificateException {
+ // Bypass entrophy collection by using insecure random generator.
+ // We just want to generate it without any delay because it's for testing purposes only.
+ this(fqdn, ThreadLocalInsecureRandom.current(), 1024);
+ }
+
+ /**
+ * Creates a new instance.
+ *
+ * @param fqdn a fully qualified domain name
+ * @param random the {@link java.security.SecureRandom} to use
+ * @param bits the number of bits of the generated private key
+ */
+ public SelfSignedCertificate(String fqdn, SecureRandom random, int bits) throws CertificateException {
+ // Generate an RSA key pair.
+ final KeyPair keypair;
+ try {
+ KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
+ keyGen.initialize(bits, random);
+ keypair = keyGen.generateKeyPair();
+ } catch (NoSuchAlgorithmException e) {
+ // Should not reach here because every Java implementation must have RSA key pair generator.
+ throw new Error(e);
+ }
+
+ String[] paths;
+ try {
+ // Try the OpenJDK's proprietary implementation.
+ paths = OpenJdkSelfSignedCertGenerator.generate(fqdn, keypair, random);
+ } catch (Throwable t) {
+ logger.debug("Failed to generate a self-signed X.509 certificate using sun.security.x509:", t);
+ try {
+ // Try Bouncy Castle if the current JVM didn't have sun.security.x509.
+ paths = BouncyCastleSelfSignedCertGenerator.generate(fqdn, keypair, random);
+ } catch (Throwable t2) {
+ logger.debug("Failed to generate a self-signed X.509 certificate using Bouncy Castle:", t2);
+ throw new CertificateException(
+ "No provider succeeded to generate a self-signed certificate. " +
+ "See debug log for the root cause.");
+ }
+ }
+
+ certificate = new File(paths[0]);
+ privateKey = new File(paths[1]);
+ }
+
+ /**
+ * Returns the generated X.509 certificate file in PEM format.
+ */
+ public File certificate() {
+ return certificate;
+ }
+
+ /**
+ * Returns the generated RSA private key file in PEM format.
+ */
+ public File privateKey() {
+ return privateKey;
+ }
+
+ /**
+ * Deletes the generated X.509 certificate file and RSA private key file.
+ */
+ public void delete() {
+ safeDelete(certificate);
+ safeDelete(privateKey);
+ }
+
+ static String[] newSelfSignedCertificate(
+ String fqdn, PrivateKey key, X509Certificate cert) throws IOException, CertificateEncodingException {
+
+ // Encode the private key into a file.
+ String keyText = "-----BEGIN PRIVATE KEY-----\n" +
+ Base64.encode(Unpooled.wrappedBuffer(key.getEncoded()), true).toString(CharsetUtil.US_ASCII) +
+ "\n-----END PRIVATE KEY-----\n";
+
+ File keyFile = File.createTempFile("keyutil_" + fqdn + '_', ".key");
+ keyFile.deleteOnExit();
+
+ OutputStream keyOut = new FileOutputStream(keyFile);
+ try {
+ keyOut.write(keyText.getBytes(CharsetUtil.US_ASCII));
+ keyOut.close();
+ keyOut = null;
+ } finally {
+ if (keyOut != null) {
+ safeClose(keyFile, keyOut);
+ safeDelete(keyFile);
+ }
+ }
+
+ // Encode the certificate into a CRT file.
+ String certText = "-----BEGIN CERTIFICATE-----\n" +
+ Base64.encode(Unpooled.wrappedBuffer(cert.getEncoded()), true).toString(CharsetUtil.US_ASCII) +
+ "\n-----END CERTIFICATE-----\n";
+
+ File certFile = File.createTempFile("keyutil_" + fqdn + '_', ".crt");
+ certFile.deleteOnExit();
+
+ OutputStream certOut = new FileOutputStream(certFile);
+ try {
+ certOut.write(certText.getBytes(CharsetUtil.US_ASCII));
+ certOut.close();
+ certOut = null;
+ } finally {
+ if (certOut != null) {
+ safeClose(certFile, certOut);
+ safeDelete(certFile);
+ safeDelete(keyFile);
+ }
+ }
+
+ return new String[] { certFile.getPath(), keyFile.getPath() };
+ }
+
+ private static void safeDelete(File certFile) {
+ if (!certFile.delete()) {
+ logger.warn("Failed to delete a file: " + certFile);
+ }
+ }
+
+ private static void safeClose(File keyFile, OutputStream keyOut) {
+ try {
+ keyOut.close();
+ } catch (IOException e) {
+ logger.warn("Failed to close a file: " + keyFile, e);
+ }
+ }
+}
diff --git a/handler/src/main/java/io/netty/handler/ssl/util/SimpleTrustManagerFactory.java b/handler/src/main/java/io/netty/handler/ssl/util/SimpleTrustManagerFactory.java
new file mode 100644
index 0000000000..b05e506ec9
--- /dev/null
+++ b/handler/src/main/java/io/netty/handler/ssl/util/SimpleTrustManagerFactory.java
@@ -0,0 +1,132 @@
+/*
+ * Copyright 2014 The Netty Project
+ *
+ * The Netty Project 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 io.netty.handler.ssl.util;
+
+import javax.net.ssl.ManagerFactoryParameters;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.TrustManagerFactory;
+import javax.net.ssl.TrustManagerFactorySpi;
+import java.security.InvalidAlgorithmParameterException;
+import java.security.KeyStore;
+import java.security.KeyStoreException;
+import java.security.Provider;
+
+/**
+ * Helps to implement a custom {@link TrustManagerFactory}.
+ */
+public abstract class SimpleTrustManagerFactory extends TrustManagerFactory {
+
+ private static final Provider PROVIDER = new Provider("", 0.0, "") {
+ private static final long serialVersionUID = -2680540247105807895L;
+ };
+
+ /**
+ * {@link SimpleTrustManagerFactorySpi} must have a reference to {@link SimpleTrustManagerFactory}
+ * to delegate its callbacks back to {@link SimpleTrustManagerFactory}. However, it is impossible to do so,
+ * because {@link TrustManagerFactory} requires {@link TrustManagerFactorySpi} at construction time and
+ * does not provide a way to access it later.
+ *
+ * To work around this issue, we use an ugly hack which uses a {@link ThreadLocal}.
+ */
+ private static final ThreadLocal CURRENT_SPI =
+ new ThreadLocal() {
+ @Override
+ protected SimpleTrustManagerFactorySpi initialValue() {
+ return new SimpleTrustManagerFactorySpi();
+ }
+ };
+
+ /**
+ * Creates a new instance.
+ */
+ protected SimpleTrustManagerFactory() {
+ this("");
+ }
+
+ /**
+ * Creates a new instance.
+ *
+ * @param name the name of this {@link TrustManagerFactory}
+ */
+ protected SimpleTrustManagerFactory(String name) {
+ super(CURRENT_SPI.get(), PROVIDER, name);
+ CURRENT_SPI.get().init(this);
+ CURRENT_SPI.remove();
+
+ if (name == null) {
+ throw new NullPointerException("name");
+ }
+ }
+
+ /**
+ * Initializes this factory with a source of certificate authorities and related trust material.
+ *
+ * @see TrustManagerFactorySpi#engineInit(KeyStore)
+ */
+ protected abstract void engineInit(KeyStore keyStore) throws Exception;
+
+ /**
+ * Initializes this factory with a source of provider-specific key material.
+ *
+ * @see TrustManagerFactorySpi#engineInit(ManagerFactoryParameters)
+ */
+ protected abstract void engineInit(ManagerFactoryParameters managerFactoryParameters) throws Exception;
+
+ /**
+ * Returns one trust manager for each type of trust material.
+ *
+ * @see TrustManagerFactorySpi#engineGetTrustManagers()
+ */
+ protected abstract TrustManager[] engineGetTrustManagers();
+
+ static final class SimpleTrustManagerFactorySpi extends TrustManagerFactorySpi {
+
+ private SimpleTrustManagerFactory parent;
+
+ void init(SimpleTrustManagerFactory parent) {
+ this.parent = parent;
+ }
+
+ @Override
+ protected void engineInit(KeyStore keyStore) throws KeyStoreException {
+ try {
+ parent.engineInit(keyStore);
+ } catch (KeyStoreException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new KeyStoreException(e);
+ }
+ }
+
+ @Override
+ protected void engineInit(
+ ManagerFactoryParameters managerFactoryParameters) throws InvalidAlgorithmParameterException {
+ try {
+ parent.engineInit(managerFactoryParameters);
+ } catch (InvalidAlgorithmParameterException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new InvalidAlgorithmParameterException(e);
+ }
+ }
+
+ @Override
+ protected TrustManager[] engineGetTrustManagers() {
+ return parent.engineGetTrustManagers();
+ }
+ }
+}
diff --git a/handler/src/main/java/io/netty/handler/ssl/util/ThreadLocalInsecureRandom.java b/handler/src/main/java/io/netty/handler/ssl/util/ThreadLocalInsecureRandom.java
new file mode 100644
index 0000000000..c69f886cd5
--- /dev/null
+++ b/handler/src/main/java/io/netty/handler/ssl/util/ThreadLocalInsecureRandom.java
@@ -0,0 +1,100 @@
+/*
+ * Copyright 2014 The Netty Project
+ *
+ * The Netty Project 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 io.netty.handler.ssl.util;
+
+import io.netty.util.internal.ThreadLocalRandom;
+
+import java.security.SecureRandom;
+import java.util.Random;
+
+/**
+ * Insecure {@link java.security.SecureRandom} which relies on {@link ThreadLocalRandom} for random number generation.
+ */
+final class ThreadLocalInsecureRandom extends SecureRandom {
+
+ private static final long serialVersionUID = -8209473337192526191L;
+
+ private static final SecureRandom INSTANCE = new ThreadLocalInsecureRandom();
+
+ static SecureRandom current() {
+ return INSTANCE;
+ }
+
+ private ThreadLocalInsecureRandom() { }
+
+ @Override
+ public String getAlgorithm() {
+ return "insecure";
+ }
+
+ @Override
+ public void setSeed(byte[] seed) { }
+
+ @Override
+ public void setSeed(long seed) { }
+
+ @Override
+ public void nextBytes(byte[] bytes) {
+ random().nextBytes(bytes);
+ }
+
+ @Override
+ public byte[] generateSeed(int numBytes) {
+ byte[] seed = new byte[numBytes];
+ random().nextBytes(seed);
+ return seed;
+ }
+
+ @Override
+ public int nextInt() {
+ return random().nextInt();
+ }
+
+ @Override
+ public int nextInt(int n) {
+ return random().nextInt(n);
+ }
+
+ @Override
+ public boolean nextBoolean() {
+ return random().nextBoolean();
+ }
+
+ @Override
+ public long nextLong() {
+ return random().nextLong();
+ }
+
+ @Override
+ public float nextFloat() {
+ return random().nextFloat();
+ }
+
+ @Override
+ public double nextDouble() {
+ return random().nextDouble();
+ }
+
+ @Override
+ public double nextGaussian() {
+ return random().nextGaussian();
+ }
+
+ private static Random random() {
+ return ThreadLocalRandom.current();
+ }
+}
diff --git a/handler/src/main/java/io/netty/handler/ssl/util/package-info.java b/handler/src/main/java/io/netty/handler/ssl/util/package-info.java
new file mode 100644
index 0000000000..fbdf16d7d2
--- /dev/null
+++ b/handler/src/main/java/io/netty/handler/ssl/util/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2012 The Netty Project
+ *
+ * The Netty Project 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.
+ */
+
+/**
+ * Utility classes that helps easier development of TLS/SSL applications.
+ */
+package io.netty.handler.ssl.util;
diff --git a/pom.xml b/pom.xml
index 7ff7ac1f13..debaa5d01e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -253,6 +253,16 @@
2.5.0
+
+
+ ${project.groupId}
+ netty-tcnative
+ 1.1.30.Fork1
+ ${os.detected.classifier}
+ compile
+ true
+
+
@@ -108,222 +105,52 @@
-
-
-
- maven-dependency-plugin
-
-
- copy
- generate-resources
-
- copy
-
-
-
-
- org.mortbay.jetty.npn
- npn-boot
- ${npn.version}
- jar
- false
- ${project.build.directory}/npn
-
-
-
-
-
-
-
-
-
spdy-server
-
-
-
- org.codehaus.mojo
- exec-maven-plugin
-
- ${java.home}/bin/java
-
- -Xbootclasspath/p:${project.build.directory}/npn/npn-boot-${npn.version}.jar
- -classpath
-
- io.netty.example.spdy.server.SpdyServer
-
- runtime
-
-
-
-
+
+ io.netty.example.spdy.server.SpdyServer
+ spdy-client
-
-
-
- org.codehaus.mojo
- exec-maven-plugin
-
- ${java.home}/bin/java
-
- -Xbootclasspath/p:${project.build.directory}/npn/npn-boot-${npn.version}.jar
- -classpath
-
- io.netty.example.spdy.client.SpdyClient
-
- runtime
-
-
-
-
+
+ io.netty.example.spdy.client.SpdyClient
+ http2-server
-
-
-
- org.codehaus.mojo
- exec-maven-plugin
-
- ${java.home}/bin/java
-
- -Xbootclasspath/p:${project.build.directory}/npn/npn-boot-${npn.version}.jar
- -classpath
-
- io.netty.example.http2.server.Http2Server
-
- runtime
-
-
-
-
+
+ io.netty.example.http2.server.Http2Server
+ http2-client
-
-
-
- org.codehaus.mojo
- exec-maven-plugin
-
- ${java.home}/bin/java
-
- -Xbootclasspath/p:${project.build.directory}/npn/npn-boot-${npn.version}.jar
- -classpath
-
- io.netty.example.http2.client.Http2Client
-
- runtime
-
-
-
-
-
-
-
-
- 7u9
-
-
- java.version
- 1.7.0_9
-
-
- 1.1.3.v20130313
-
-
-
- 7u10
-
-
- java.version
- 1.7.0_10
-
-
-
- 1.1.3.v20130313
-
-
-
- 7u11
-
-
- java.version
- 1.7.0_11
-
-
-
- 1.1.3.v20130313
-
-
-
- 7u13
-
-
- java.version
- 1.7.0_13
-
-
-
- 1.1.4.v20130313
-
-
-
- 7u15
-
-
- java.version
- 1.7.0_15
-
-
-
- 1.1.5.v20130313
-
-
-
- 7u17
-
-
- java.version
- 1.7.0_17
-
-
-
- 1.1.5.v20130313
-
-
-
- 7u21
-
-
- java.version
- 1.7.0_21
-
-
-
- 1.1.5.v20130313
-
-
-
- 7u25
-
-
- java.version
- 1.7.0_25
-
-
-
- 1.1.5.v20130313
+ io.netty.example.http2.client.Http2Client
+
+
+
+
+ org.codehaus.mojo
+ exec-maven-plugin
+
+ ${java.home}/bin/java
+
+ ${argLine.common}
+ ${argLine.leak}
+ ${argLine.coverage}
+ -classpath %classpath
+ ${exampleClass}
+
+ runtime
+
+
+
+
diff --git a/example/src/main/java/io/netty/example/spdy/server/SpdyServer.java b/example/src/main/java/io/netty/example/spdy/server/SpdyServer.java
index a384e3da54..21e2d8ebe5 100644
--- a/example/src/main/java/io/netty/example/spdy/server/SpdyServer.java
+++ b/example/src/main/java/io/netty/example/spdy/server/SpdyServer.java
@@ -22,6 +22,7 @@ import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.ssl.SslContext;
+import io.netty.handler.ssl.SslProvider;
import io.netty.handler.ssl.util.SelfSignedCertificate;
/**
@@ -85,7 +86,7 @@ public class SpdyServer {
// Configure SSL.
SelfSignedCertificate ssc = new SelfSignedCertificate();
- SslContext sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
+ SslContext sslCtx = SslContext.newServerContext(SslProvider.JDK, ssc.certificate(), ssc.privateKey());
new SpdyServer(sslCtx, port).run();
}
diff --git a/example/src/main/java/io/netty/example/spdy/server/SpdyServerInitializer.java b/example/src/main/java/io/netty/example/spdy/server/SpdyServerInitializer.java
index 9c5ee4ccdb..9f3779a3cd 100644
--- a/example/src/main/java/io/netty/example/spdy/server/SpdyServerInitializer.java
+++ b/example/src/main/java/io/netty/example/spdy/server/SpdyServerInitializer.java
@@ -41,7 +41,7 @@ public class SpdyServerInitializer extends ChannelInitializer {
SslHandler sslHandler = sslCtx.newHandler(ch.alloc());
SSLEngine engine = sslHandler.engine();
- p.addLast("ssl", new SslHandler(engine));
+ p.addLast("ssl", sslHandler);
// Setup NextProtoNego with our server provider
NextProtoNego.put(engine, new SpdyServerProvider());
diff --git a/pom.xml b/pom.xml
index c6ab75b957..230307185d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -71,24 +71,13 @@
leak
-
- -server
- -dsa -da -ea:io.netty...
- -XX:+AggressiveOpts
- -XX:+TieredCompilation
- -XX:+UseBiasedLocking
- -XX:+UseFastAccessorMethods
- -XX:+OptimizeStringConcat
- -XX:+HeapDumpOnOutOfMemoryError
- -Dio.netty.leakDetectionLevel=3
- -verbose:gc
-
+ -Dio.netty.leakDetectionLevel=paranoidcoverage
- ${jacoco.argLine}
+ ${jacoco.argLine}
@@ -179,15 +168,154 @@
+
+
+
+ npn-7u9
+
+
+ java.version
+ 1.7.0_9
+
+
+
+ 1.1.3.v20130313
+
+
+
+ npn-7u10
+
+
+ java.version
+ 1.7.0_10
+
+
+
+ 1.1.3.v20130313
+
+
+
+ npn-7u11
+
+
+ java.version
+ 1.7.0_11
+
+
+
+ 1.1.3.v20130313
+
+
+
+ npn-7u13
+
+
+ java.version
+ 1.7.0_13
+
+
+
+ 1.1.4.v20130313
+
+
+
+ npn-7u15
+
+
+ java.version
+ 1.7.0_15
+
+
+
+ 1.1.5.v20130313
+
+
+
+ npn-7u17
+
+
+ java.version
+ 1.7.0_17
+
+
+
+ 1.1.5.v20130313
+
+
+
+ npn-7u21
+
+
+ java.version
+ 1.7.0_21
+
+
+
+ 1.1.5.v20130313
+
+
+
+ npn-7u25
+
+
+ java.version
+ 1.7.0_25
+
+
+
+ 1.1.5.v20130313
+
+
+
+ npn-7u40
+
+
+ java.version
+ 1.7.0_40
+
+
+
+ 1.1.6.v20130911
+
+
+
+ npn-7u45
+
+
+ java.version
+ 1.7.0_45
+
+
+
+ 1.1.6.v20130911
+
+
+
+ npn-7u51
+
+
+ java.version
+ 1.7.0_51
+
+
+
+ 1.1.6.v20130911
+
+ UTF-8UTF-81.3.18.GA
-
-
- -server
+ 1.1.7.v20140316
+ ${settings.localRepository}/org/mortbay/jetty/npn/npn-boot/${jetty.npn.version}/npn-boot-${jetty.npn.version}.jar
+
+ -Xbootclasspath/p:${jetty.npn.path}
+ -server
-dsa -da -ea:io.netty...
-XX:+AggressiveOpts
-XX:+TieredCompilation
@@ -196,7 +324,9 @@
-XX:+OptimizeStringConcat
-XX:+HeapDumpOnOutOfMemoryError
-verbose:gc
-
+
+ -D_
+ -D_
@@ -239,12 +369,17 @@
true
-
+
org.eclipse.jetty.npnnpn-api1.1.0.v20120525
+
+ org.mortbay.jetty.npn
+ npn-boot
+ ${jetty.npn.version}
+
@@ -568,6 +703,24 @@
+
+
+ maven-dependency-plugin
+
+
+ get-npn-boot
+ validate
+
+ get
+
+
+ org.mortbay.jetty.npn
+ npn-boot
+ ${jetty.npn.version}
+
+
+
+ maven-surefire-plugin
@@ -580,7 +733,7 @@
**/TestUtil*random
- ${test.jvm.argLine.coverage} ${test.jvm.argLine}
+ ${argLine.common} ${argLine.leak} ${argLine.coverage}
@@ -908,7 +1061,7 @@
org.codehaus.mojoexec-maven-plugin
- 1.2.1
+ 1.3org.fusesource.hawtjni
diff --git a/run-example.sh b/run-example.sh
new file mode 100755
index 0000000000..9bf40f64cb
--- /dev/null
+++ b/run-example.sh
@@ -0,0 +1,15 @@
+#!/bin/bash -e
+cd "`dirname "$0"`"/example
+if [[ $# -ne 1 ]]; then
+ echo "Usage: $0 " >&2
+ echo >&2
+ echo "Available examples:" >&2
+ grep -E '^ [-a-z0-9]*' pom.xml | sed -e 's#\(^.*\|.*$\)##g' | sed -e 's#^# #' >&2
+ exit 1
+fi
+
+EXAMPLE_NAME="$1"
+
+echo "[INFO] Running: $EXAMPLE_NAME"
+mvn -X -P "$EXAMPLE_NAME" compile exec:exec
+
From 59f3d550fec32cc9ddcc04a90679a437738c362d Mon Sep 17 00:00:00 2001
From: Trustin Lee
Date: Tue, 20 May 2014 20:04:41 +0900
Subject: [PATCH 17/26] Fix a problem where all classes are compiled again
Motivation:
Due to a known problem[1] of maven-compiler-plugin, our build always
compiles everything from scratch, which is waste of time.
Modifications:
Exclude package-info.java from the source list.
Result:
Much shorter build time.
[1]: https://jira.codehaus.org/browse/MCOMPILER-205
---
pom.xml | 3 +++
1 file changed, 3 insertions(+)
diff --git a/pom.xml b/pom.xml
index 230307185d..761cfbad7b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -625,6 +625,9 @@
-->
256m1024m
+
+ **/package-info.java
+
From 66d969b453307b05569880fbbb8ba1af57fab3d3 Mon Sep 17 00:00:00 2001
From: Trustin Lee
Date: Tue, 20 May 2014 22:37:55 +0900
Subject: [PATCH 18/26] Fix a build problem with JDK 8
Motivation:
Build fails with JDK 8 because npn-boot does not work with JDK 8
Modifications:
Do not specify bootclasspath when on JDK 8
Result:
Build is green again.
---
example/pom.xml | 1 +
pom.xml | 8 +++++---
2 files changed, 6 insertions(+), 3 deletions(-)
diff --git a/example/pom.xml b/example/pom.xml
index c44e9e3b15..833ebbe7de 100644
--- a/example/pom.xml
+++ b/example/pom.xml
@@ -142,6 +142,7 @@
${java.home}/bin/java
${argLine.common}
+ ${argLine.bootcp}
${argLine.leak}
${argLine.coverage}
-classpath %classpath
diff --git a/pom.xml b/pom.xml
index 761cfbad7b..14cd9f4464 100644
--- a/pom.xml
+++ b/pom.xml
@@ -100,14 +100,16 @@
-
jdk8[1.8,)
+
false
+
+ -D_
@@ -314,7 +316,6 @@
1.1.7.v20140316${settings.localRepository}/org/mortbay/jetty/npn/npn-boot/${jetty.npn.version}/npn-boot-${jetty.npn.version}.jar
- -Xbootclasspath/p:${jetty.npn.path}
-server
-dsa -da -ea:io.netty...
-XX:+AggressiveOpts
@@ -325,6 +326,7 @@
-XX:+HeapDumpOnOutOfMemoryError
-verbose:gc
+ -Xbootclasspath/p:${jetty.npn.path}-D_-D_
@@ -736,7 +738,7 @@
**/TestUtil*random
- ${argLine.common} ${argLine.leak} ${argLine.coverage}
+ ${argLine.common} ${argLine.bootcp} ${argLine.leak} ${argLine.coverage}
From 72ccf83861f30d32ab56173c344ad19fbd30cebb Mon Sep 17 00:00:00 2001
From: Trustin Lee
Date: Tue, 20 May 2014 23:24:34 +0900
Subject: [PATCH 19/26] Clean up the execution mechanism of examples
Motivation:
- There's no way to pass an argument to an example.
- Assigning a Maven profile for each example is an overkill.
It makes the pom.xml crowded.
Modifications:
- Remove example profiles from example/pom.xml
- Keep the list of examples in run-example.sh
- run-example.sh passes all options to exec-maven-plugin.
For example, we can now do this:
./run-example.sh -Dssl -Dport=443 http-server
Result:
- It's much easier to add a new example and provide an easy way to
launch it.
- We can still pass an arbitrary argument to the example being launched.
(I'll update all examples to make them get their options from system
properties rather than from args[].
---
example/pom.xml | 29 +----------------------------
run-example.sh | 45 +++++++++++++++++++++++++++++++++++++--------
2 files changed, 38 insertions(+), 36 deletions(-)
diff --git a/example/pom.xml b/example/pom.xml
index 833ebbe7de..d09c478776 100644
--- a/example/pom.xml
+++ b/example/pom.xml
@@ -105,34 +105,6 @@
-
-
- spdy-server
-
- io.netty.example.spdy.server.SpdyServer
-
-
-
- spdy-client
-
- io.netty.example.spdy.client.SpdyClient
-
-
-
-
- http2-server
-
- io.netty.example.http2.server.Http2Server
-
-
-
- http2-client
-
- io.netty.example.http2.client.Http2Client
-
-
-
-
@@ -146,6 +118,7 @@
${argLine.leak}
${argLine.coverage}
-classpath %classpath
+ ${argLine.example}
${exampleClass}
runtime
diff --git a/run-example.sh b/run-example.sh
index 9bf40f64cb..35fb34da7a 100755
--- a/run-example.sh
+++ b/run-example.sh
@@ -1,15 +1,44 @@
#!/bin/bash -e
-cd "`dirname "$0"`"/example
-if [[ $# -ne 1 ]]; then
- echo "Usage: $0 " >&2
+declare -A EXAMPLE_MAP=(
+ ['spdy-server']='io.netty.example.spdy.server.SpdyServer'
+ ['spdy-client']='io.netty.example.spdy.client.SpdyClient'
+ ['http2-server']='io.netty.example.http2.server.Http2Server'
+ ['http2-client']='io.netty.example.http2.client.Http2Client'
+)
+
+EXAMPLE=''
+EXAMPLE_CLASS=''
+EXAMPLE_ARGS=''
+I=0
+
+while [[ $# -gt 0 ]]; do
+ ARG="$1"
+ shift
+ if [[ "$ARG" =~ (^-.+) ]]; then
+ if [[ -z "$EXAMPLE_ARGS" ]]; then
+ EXAMPLE_ARGS="$ARG"
+ else
+ EXAMPLE_ARGS="$EXAMPLE_ARGS $ARG"
+ fi
+ else
+ EXAMPLE="$ARG"
+ EXAMPLE_CLASS="${EXAMPLE_MAP["$EXAMPLE"]}"
+ break
+ fi
+done
+
+if [[ -z "$EXAMPLE" ]] || [[ -z "$EXAMPLE_CLASS" ]] || [[ $# -ne 0 ]]; then
+ echo " Usage: $0 [-D[=] ...] " >&2
+ echo "Example: $0 -Dport=8443 -Dssl http-server" >&2
echo >&2
echo "Available examples:" >&2
- grep -E '^ [-a-z0-9]*' pom.xml | sed -e 's#\(^.*\|.*$\)##g' | sed -e 's#^# #' >&2
+ for E in "${!EXAMPLE_MAP[@]}"; do
+ echo " $E"
+ done | sort >&2
exit 1
fi
-EXAMPLE_NAME="$1"
-
-echo "[INFO] Running: $EXAMPLE_NAME"
-mvn -X -P "$EXAMPLE_NAME" compile exec:exec
+cd "`dirname "$0"`"/example
+echo "[INFO] Running: $EXAMPLE ($EXAMPLE_CLASS $EXAMPLE_ARGS)"
+exec mvn -nsu compile exec:exec -DargLine.example="$EXAMPLE_ARGS" -DexampleClass="$EXAMPLE_CLASS"
From 50ae950203545622bd214c9f2bfd5e88956abad9 Mon Sep 17 00:00:00 2001
From: Trustin Lee
Date: Tue, 20 May 2014 23:38:53 +0900
Subject: [PATCH 20/26] Improve run-example.sh
- More usage example
- Newlines for prettier output
---
run-example.sh | 3 +++
1 file changed, 3 insertions(+)
diff --git a/run-example.sh b/run-example.sh
index 35fb34da7a..82c529296c 100755
--- a/run-example.sh
+++ b/run-example.sh
@@ -30,11 +30,14 @@ done
if [[ -z "$EXAMPLE" ]] || [[ -z "$EXAMPLE_CLASS" ]] || [[ $# -ne 0 ]]; then
echo " Usage: $0 [-D[=] ...] " >&2
echo "Example: $0 -Dport=8443 -Dssl http-server" >&2
+ echo " $0 -Dhost=127.0.0.1 -Dport=8009 echo-client" >&2
echo >&2
echo "Available examples:" >&2
+ echo >&2
for E in "${!EXAMPLE_MAP[@]}"; do
echo " $E"
done | sort >&2
+ echo >&2
exit 1
fi
From 579973e35a4c39b4347ce60fd0b0159ad67d1125 Mon Sep 17 00:00:00 2001
From: Trustin Lee
Date: Wed, 21 May 2014 12:54:36 +0900
Subject: [PATCH 21/26] Fix resource leak in DefaultHttp2FrameIOTest
---
.../codec/http2/DefaultHttp2FrameIOTest.java | 53 +++++++++++++++----
1 file changed, 44 insertions(+), 9 deletions(-)
diff --git a/codec-http2/src/test/java/io/netty/handler/codec/http2/DefaultHttp2FrameIOTest.java b/codec-http2/src/test/java/io/netty/handler/codec/http2/DefaultHttp2FrameIOTest.java
index 9912716a04..639ccc2cb1 100644
--- a/codec-http2/src/test/java/io/netty/handler/codec/http2/DefaultHttp2FrameIOTest.java
+++ b/codec-http2/src/test/java/io/netty/handler/codec/http2/DefaultHttp2FrameIOTest.java
@@ -15,12 +15,6 @@
package io.netty.handler.codec.http2;
-import static io.netty.handler.codec.http2.Http2CodecUtil.MAX_UNSIGNED_INT;
-import static io.netty.handler.codec.http2.Http2CodecUtil.MAX_UNSIGNED_SHORT;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Matchers.isNull;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.Unpooled;
@@ -28,7 +22,6 @@ import io.netty.buffer.UnpooledByteBufAllocator;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.util.CharsetUtil;
-
import io.netty.util.ReferenceCountUtil;
import org.junit.Before;
import org.junit.Test;
@@ -36,6 +29,11 @@ import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
+import static io.netty.handler.codec.http2.Http2CodecUtil.*;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Matchers.isNull;
+import static org.mockito.Mockito.*;
+
/**
* Integration tests for {@link DefaultHttp2FrameReader} and {@link DefaultHttp2FrameWriter}.
*/
@@ -70,51 +68,63 @@ public class DefaultHttp2FrameIOTest {
public void emptyDataShouldRoundtrip() throws Exception {
ByteBuf data = Unpooled.EMPTY_BUFFER;
writer.writeData(ctx, promise, 1000, data, 0, false, false, false);
+
ByteBuf frame = captureWrite();
reader.readFrame(ctx, frame, observer);
verify(observer).onDataRead(eq(ctx), eq(1000), eq(data), eq(0), eq(false), eq(false), eq(false));
+ frame.release();
}
@Test
public void dataShouldRoundtrip() throws Exception {
ByteBuf data = dummyData();
writer.writeData(ctx, promise, 1000, data.retain().duplicate(), 0, false, false, false);
+
ByteBuf frame = captureWrite();
reader.readFrame(ctx, frame, observer);
verify(observer).onDataRead(eq(ctx), eq(1000), eq(data), eq(0), eq(false), eq(false), eq(false));
+ frame.release();
}
@Test
public void dataWithPaddingShouldRoundtrip() throws Exception {
ByteBuf data = dummyData();
writer.writeData(ctx, promise, 1, data.retain().duplicate(), 256, true, true, true);
+
ByteBuf frame = captureWrite();
reader.readFrame(ctx, frame, observer);
verify(observer).onDataRead(eq(ctx), eq(1), eq(data), eq(256), eq(true), eq(true), eq(true));
+ frame.release();
}
@Test
public void priorityShouldRoundtrip() throws Exception {
writer.writePriority(ctx, promise, 1, 2, (short) 255, true);
+
ByteBuf frame = captureWrite();
reader.readFrame(ctx, frame, observer);
verify(observer).onPriorityRead(eq(ctx), eq(1), eq(2), eq((short) 255), eq(true));
+ frame.release();
}
@Test
public void rstStreamShouldRoundtrip() throws Exception {
writer.writeRstStream(ctx, promise, 1, MAX_UNSIGNED_INT);
+
ByteBuf frame = captureWrite();
reader.readFrame(ctx, frame, observer);
verify(observer).onRstStreamRead(eq(ctx), eq(1), eq(MAX_UNSIGNED_INT));
+ frame.release();
}
@Test
public void emptySettingsShouldRoundtrip() throws Exception {
writer.writeSettings(ctx, promise, new Http2Settings());
+
ByteBuf frame = captureWrite();
reader.readFrame(ctx, frame, observer);
verify(observer).onSettingsRead(eq(ctx), eq(new Http2Settings()));
+ frame.release();
}
@Test
@@ -127,35 +137,43 @@ public class DefaultHttp2FrameIOTest {
settings.allowCompressedData(false);
writer.writeSettings(ctx, promise, settings);
+
ByteBuf frame = captureWrite();
reader.readFrame(ctx, frame, observer);
verify(observer).onSettingsRead(eq(ctx), eq(settings));
+ frame.release();
}
@Test
public void settingsAckShouldRoundtrip() throws Exception {
writer.writeSettingsAck(ctx, promise);
+
ByteBuf frame = captureWrite();
reader.readFrame(ctx, frame, observer);
verify(observer).onSettingsAckRead(eq(ctx));
+ frame.release();
}
@Test
public void pingShouldRoundtrip() throws Exception {
ByteBuf data = dummyData();
writer.writePing(ctx, promise, false, data.retain().duplicate());
+
ByteBuf frame = captureWrite();
reader.readFrame(ctx, frame, observer);
verify(observer).onPingRead(eq(ctx), eq(data));
+ frame.release();
}
@Test
public void pingAckShouldRoundtrip() throws Exception {
ByteBuf data = dummyData();
writer.writePing(ctx, promise, true, data.retain().duplicate());
+
ByteBuf frame = captureWrite();
reader.readFrame(ctx, frame, observer);
verify(observer).onPingAckRead(eq(ctx), eq(data));
+ frame.release();
}
@Test
@@ -165,6 +183,7 @@ public class DefaultHttp2FrameIOTest {
ByteBuf frame = captureWrite();
reader.readFrame(ctx, frame, observer);
verify(observer).onGoAwayRead(eq(ctx), eq(1), eq(MAX_UNSIGNED_INT), eq(data));
+ frame.release();
}
@Test
@@ -173,16 +192,17 @@ public class DefaultHttp2FrameIOTest {
ByteBuf frame = captureWrite();
reader.readFrame(ctx, frame, observer);
verify(observer).onWindowUpdateRead(eq(ctx), eq(1), eq(Integer.MAX_VALUE));
+ frame.release();
}
@Test
public void altSvcShouldRoundtrip() throws Exception {
- writer.writeAltSvc(ctx, promise, 1, MAX_UNSIGNED_INT, MAX_UNSIGNED_SHORT, dummyData(), "host",
- "origin");
+ writer.writeAltSvc(ctx, promise, 1, MAX_UNSIGNED_INT, MAX_UNSIGNED_SHORT, dummyData(), "host", "origin");
ByteBuf frame = captureWrite();
reader.readFrame(ctx, frame, observer);
verify(observer).onAltSvcRead(eq(ctx), eq(1), eq(MAX_UNSIGNED_INT), eq(MAX_UNSIGNED_SHORT),
eq(dummyData()), eq("host"), eq("origin"));
+ frame.release();
}
@Test
@@ -192,6 +212,7 @@ public class DefaultHttp2FrameIOTest {
reader.readFrame(ctx, frame, observer);
verify(observer).onAltSvcRead(eq(ctx), eq(1), eq(MAX_UNSIGNED_INT), eq(MAX_UNSIGNED_SHORT),
eq(dummyData()), eq("host"), isNull(String.class));
+ frame.release();
}
@Test
@@ -200,6 +221,7 @@ public class DefaultHttp2FrameIOTest {
ByteBuf frame = captureWrite();
reader.readFrame(ctx, frame, observer);
verify(observer).onBlockedRead(eq(ctx), eq(1));
+ frame.release();
}
@Test
@@ -209,6 +231,7 @@ public class DefaultHttp2FrameIOTest {
ByteBuf frame = captureWrite();
reader.readFrame(ctx, frame, observer);
verify(observer).onHeadersRead(eq(ctx), eq(1), eq(headers), eq(0), eq(true), eq(true));
+ frame.release();
}
@Test
@@ -218,6 +241,7 @@ public class DefaultHttp2FrameIOTest {
ByteBuf frame = captureWrite();
reader.readFrame(ctx, frame, observer);
verify(observer).onHeadersRead(eq(ctx), eq(1), eq(headers), eq(256), eq(true), eq(true));
+ frame.release();
}
@Test
@@ -227,6 +251,7 @@ public class DefaultHttp2FrameIOTest {
ByteBuf frame = captureWrite();
reader.readFrame(ctx, frame, observer);
verify(observer).onHeadersRead(eq(ctx), eq(1), eq(headers), eq(0), eq(true), eq(true));
+ frame.release();
}
@Test
@@ -236,6 +261,7 @@ public class DefaultHttp2FrameIOTest {
ByteBuf frame = captureWrite();
reader.readFrame(ctx, frame, observer);
verify(observer).onHeadersRead(eq(ctx), eq(1), eq(headers), eq(256), eq(true), eq(true));
+ frame.release();
}
@Test
@@ -246,6 +272,7 @@ public class DefaultHttp2FrameIOTest {
reader.readFrame(ctx, frame, observer);
verify(observer).onHeadersRead(eq(ctx), eq(1), eq(headers), eq(2), eq((short) 3), eq(true), eq(0),
eq(true), eq(true));
+ frame.release();
}
@Test
@@ -256,6 +283,7 @@ public class DefaultHttp2FrameIOTest {
reader.readFrame(ctx, frame, observer);
verify(observer).onHeadersRead(eq(ctx), eq(1), eq(headers), eq(2), eq((short) 3), eq(true), eq(256),
eq(true), eq(true));
+ frame.release();
}
@Test
@@ -266,6 +294,7 @@ public class DefaultHttp2FrameIOTest {
reader.readFrame(ctx, frame, observer);
verify(observer).onHeadersRead(eq(ctx), eq(1), eq(headers), eq(2), eq((short) 3), eq(true), eq(0),
eq(true), eq(true));
+ frame.release();
}
@Test
@@ -276,6 +305,7 @@ public class DefaultHttp2FrameIOTest {
reader.readFrame(ctx, frame, observer);
verify(observer).onHeadersRead(eq(ctx), eq(1), eq(headers), eq(2), eq((short) 3), eq(true), eq(256),
eq(true), eq(true));
+ frame.release();
}
@Test
@@ -285,6 +315,7 @@ public class DefaultHttp2FrameIOTest {
ByteBuf frame = captureWrite();
reader.readFrame(ctx, frame, observer);
verify(observer).onPushPromiseRead(eq(ctx), eq(1), eq(2), eq(headers), eq(0));
+ frame.release();
}
@Test
@@ -294,6 +325,7 @@ public class DefaultHttp2FrameIOTest {
ByteBuf frame = captureWrite();
reader.readFrame(ctx, frame, observer);
verify(observer).onPushPromiseRead(eq(ctx), eq(1), eq(2), eq(headers), eq(0));
+ frame.release();
}
@Test
@@ -303,6 +335,7 @@ public class DefaultHttp2FrameIOTest {
ByteBuf frame = captureWrite();
reader.readFrame(ctx, frame, observer);
verify(observer).onPushPromiseRead(eq(ctx), eq(1), eq(2), eq(headers), eq(256));
+ frame.release();
}
@Test
@@ -312,6 +345,7 @@ public class DefaultHttp2FrameIOTest {
ByteBuf frame = captureWrite();
reader.readFrame(ctx, frame, observer);
verify(observer).onPushPromiseRead(eq(ctx), eq(1), eq(2), eq(headers), eq(0));
+ frame.release();
}
@Test
@@ -321,6 +355,7 @@ public class DefaultHttp2FrameIOTest {
ByteBuf frame = captureWrite();
reader.readFrame(ctx, frame, observer);
verify(observer).onPushPromiseRead(eq(ctx), eq(1), eq(2), eq(headers), eq(256));
+ frame.release();
}
private ByteBuf captureWrite() {
From 3051c1ee0f3d61584f857e4e20b64d4918397753 Mon Sep 17 00:00:00 2001
From: Trustin Lee
Date: Wed, 21 May 2014 13:35:28 +0900
Subject: [PATCH 22/26] Bash 3 compatibility
Motivation:
Mac OS X ships Bash 3, and it does not have an associative array
(declare -A).
Modifications:
Do not use an associative array.
Result:
Can run examples on Mac OS X using run-example.sh
---
run-example.sh | 33 ++++++++++++++++++---------------
1 file changed, 18 insertions(+), 15 deletions(-)
diff --git a/run-example.sh b/run-example.sh
index 82c529296c..f8ed1857f5 100755
--- a/run-example.sh
+++ b/run-example.sh
@@ -1,28 +1,31 @@
#!/bin/bash -e
-declare -A EXAMPLE_MAP=(
- ['spdy-server']='io.netty.example.spdy.server.SpdyServer'
- ['spdy-client']='io.netty.example.spdy.client.SpdyClient'
- ['http2-server']='io.netty.example.http2.server.Http2Server'
- ['http2-client']='io.netty.example.http2.client.Http2Client'
+EXAMPLE_MAP=(
+ 'spdy-server:io.netty.example.spdy.server.SpdyServer'
+ 'spdy-client:io.netty.example.spdy.client.SpdyClient'
+ 'http2-server:io.netty.example.http2.server.Http2Server'
+ 'http2-client:io.netty.example.http2.client.Http2Client'
)
EXAMPLE=''
EXAMPLE_CLASS=''
-EXAMPLE_ARGS=''
+EXAMPLE_ARGS='-D_'
I=0
while [[ $# -gt 0 ]]; do
ARG="$1"
shift
if [[ "$ARG" =~ (^-.+) ]]; then
- if [[ -z "$EXAMPLE_ARGS" ]]; then
- EXAMPLE_ARGS="$ARG"
- else
- EXAMPLE_ARGS="$EXAMPLE_ARGS $ARG"
- fi
+ EXAMPLE_ARGS="$EXAMPLE_ARGS $ARG"
else
EXAMPLE="$ARG"
- EXAMPLE_CLASS="${EXAMPLE_MAP["$EXAMPLE"]}"
+ for E in "${EXAMPLE_MAP[@]}"; do
+ KEY="${E%%:*}"
+ VAL="${E##*:}"
+ if [[ "$EXAMPLE" == "$KEY" ]]; then
+ EXAMPLE_CLASS="$VAL"
+ break
+ fi
+ done
break
fi
done
@@ -34,8 +37,8 @@ if [[ -z "$EXAMPLE" ]] || [[ -z "$EXAMPLE_CLASS" ]] || [[ $# -ne 0 ]]; then
echo >&2
echo "Available examples:" >&2
echo >&2
- for E in "${!EXAMPLE_MAP[@]}"; do
- echo " $E"
+ for E in "${EXAMPLE_MAP[@]}"; do
+ echo " ${E%%:*}"
done | sort >&2
echo >&2
exit 1
@@ -43,5 +46,5 @@ fi
cd "`dirname "$0"`"/example
echo "[INFO] Running: $EXAMPLE ($EXAMPLE_CLASS $EXAMPLE_ARGS)"
-exec mvn -nsu compile exec:exec -DargLine.example="$EXAMPLE_ARGS" -DexampleClass="$EXAMPLE_CLASS"
+exec mvn -X -nsu compile exec:exec -DargLine.example="$EXAMPLE_ARGS" -DexampleClass="$EXAMPLE_CLASS"
From e167ec51eb29036cae9fa1551f319e166c10eba8 Mon Sep 17 00:00:00 2001
From: Trustin Lee
Date: Wed, 21 May 2014 17:21:18 +0900
Subject: [PATCH 23/26] Add unified NextProtoNego extension support to
SslContext
Motivation:
- OpenSslEngine and JDK SSLEngine (+ Jetty NPN) have different APIs to
support NextProtoNego extension.
- It is impossible to configure NPN with SslContext when the provider
type is JDK.
Modification:
- Implement NextProtoNego extension by overriding the behavior of
SSLSession.getProtocol() for both OpenSSLEngine and JDK SSLEngine.
- SSLEngine.getProtocol() returns a string delimited by a colon (':')
where the first component is the transport protosol (e.g. TLSv1.2)
and the second component is the name of the application protocol
- Remove the direct reference of Jetty NPN classes from the examples
- Add SslContext.newApplicationProtocolSelector
Result:
- A user can now use both JDK SSLEngine and OpenSslEngine for NPN-based
protocols such as HTTP2 and SPDY
---
.../example/http2/client/Http2Client.java | 11 +-
.../http2/client/Http2ClientInitializer.java | 15 +-
.../http2/client/Http2ClientProvider.java | 58 ----
.../http2/server/Http2OrHttpHandler.java | 7 +-
.../example/http2/server/Http2Server.java | 26 +-
.../http2/server/Http2ServerInitializer.java | 18 +-
.../http2/server/Http2ServerProvider.java | 66 ----
.../netty/example/spdy/client/SpdyClient.java | 10 +-
.../spdy/client/SpdyClientInitializer.java | 12 +-
.../spdy/client/SpdyClientProvider.java | 58 ----
.../spdy/server/SpdyOrHttpHandler.java | 5 +-
.../netty/example/spdy/server/SpdyServer.java | 27 +-
.../spdy/server/SpdyServerHandler.java | 3 +
.../spdy/server/SpdyServerInitializer.java | 14 +-
.../spdy/server/SpdyServerProvider.java | 63 ----
handler/pom.xml | 11 +
.../handler/ssl/JdkSslClientContext.java | 17 +-
.../io/netty/handler/ssl/JdkSslContext.java | 6 +-
.../handler/ssl/JdkSslServerContext.java | 29 +-
.../netty/handler/ssl/JettyNpnSslEngine.java | 286 ++++++++++++++++++
.../netty/handler/ssl/JettyNpnSslSession.java | 167 ++++++++++
.../io/netty/handler/ssl/OpenSslEngine.java | 20 +-
.../handler/ssl/OpenSslServerContext.java | 6 +-
.../java/io/netty/handler/ssl/SslContext.java | 78 +++++
pom.xml | 3 +
25 files changed, 658 insertions(+), 358 deletions(-)
delete mode 100644 example/src/main/java/io/netty/example/http2/client/Http2ClientProvider.java
delete mode 100644 example/src/main/java/io/netty/example/http2/server/Http2ServerProvider.java
delete mode 100644 example/src/main/java/io/netty/example/spdy/client/SpdyClientProvider.java
delete mode 100644 example/src/main/java/io/netty/example/spdy/server/SpdyServerProvider.java
create mode 100644 handler/src/main/java/io/netty/handler/ssl/JettyNpnSslEngine.java
create mode 100644 handler/src/main/java/io/netty/handler/ssl/JettyNpnSslSession.java
diff --git a/example/src/main/java/io/netty/example/http2/client/Http2Client.java b/example/src/main/java/io/netty/example/http2/client/Http2Client.java
index 609a54491f..8560bbe9a4 100644
--- a/example/src/main/java/io/netty/example/http2/client/Http2Client.java
+++ b/example/src/main/java/io/netty/example/http2/client/Http2Client.java
@@ -22,11 +22,11 @@ import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
-import io.netty.example.http2.server.Http2Server;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http2.DefaultHttp2Headers;
import io.netty.handler.codec.http2.Http2Exception;
import io.netty.handler.codec.http2.Http2Headers;
+import io.netty.handler.codec.http2.Http2OrHttpChooser.SelectedProtocol;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
@@ -50,7 +50,13 @@ public class Http2Client {
private EventLoopGroup workerGroup;
public Http2Client(String host, int port) throws SSLException {
- sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
+ sslCtx = SslContext.newClientContext(
+ null, InsecureTrustManagerFactory.INSTANCE, null,
+ SslContext.newApplicationProtocolSelector(
+ SelectedProtocol.HTTP_2.protocolName(),
+ SelectedProtocol.HTTP_1_1.protocolName()),
+ 0, 0);
+
this.host = host;
this.port = port;
http2ConnectionHandler = new Http2ClientConnectionHandler();
@@ -108,7 +114,6 @@ public class Http2Client {
}
public static void main(String[] args) throws Exception {
- Http2Server.checkForNpnSupport();
int port;
if (args.length > 0) {
port = Integer.parseInt(args[0]);
diff --git a/example/src/main/java/io/netty/example/http2/client/Http2ClientInitializer.java b/example/src/main/java/io/netty/example/http2/client/Http2ClientInitializer.java
index 4b20f6c362..d189aabb95 100644
--- a/example/src/main/java/io/netty/example/http2/client/Http2ClientInitializer.java
+++ b/example/src/main/java/io/netty/example/http2/client/Http2ClientInitializer.java
@@ -15,14 +15,9 @@
package io.netty.example.http2.client;
import io.netty.channel.ChannelInitializer;
-import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http2.AbstractHttp2ConnectionHandler;
import io.netty.handler.ssl.SslContext;
-import io.netty.handler.ssl.SslHandler;
-import org.eclipse.jetty.npn.NextProtoNego;
-
-import javax.net.ssl.SSLEngine;
/**
* Configures the client pipeline to support HTTP/2 frames.
@@ -39,14 +34,6 @@ public class Http2ClientInitializer extends ChannelInitializer {
@Override
public void initChannel(SocketChannel ch) throws Exception {
- SslHandler sslHandler = sslCtx.newHandler(ch.alloc());
- SSLEngine engine = sslHandler.engine();
- NextProtoNego.put(engine, new Http2ClientProvider());
- NextProtoNego.debug = true;
-
- ChannelPipeline pipeline = ch.pipeline();
-
- pipeline.addLast("ssl", new SslHandler(engine));
- pipeline.addLast("http2ConnectionHandler", connectionHandler);
+ ch.pipeline().addLast(sslCtx.newHandler(ch.alloc()), connectionHandler);
}
}
diff --git a/example/src/main/java/io/netty/example/http2/client/Http2ClientProvider.java b/example/src/main/java/io/netty/example/http2/client/Http2ClientProvider.java
deleted file mode 100644
index c9150de520..0000000000
--- a/example/src/main/java/io/netty/example/http2/client/Http2ClientProvider.java
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * Copyright 2014 The Netty Project
- *
- * The Netty Project 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 io.netty.example.http2.client;
-
-import org.eclipse.jetty.npn.NextProtoNego;
-
-import java.util.List;
-
-import static io.netty.handler.codec.http2.Http2OrHttpChooser.SelectedProtocol.*;
-
-/**
- * The Jetty project provides an implementation of the Transport Layer Security (TLS) extension for Next Protocol
- * Negotiation (NPN) for OpenJDK 7 or greater. NPN allows the application layer to negotiate which protocol to use
- * over the secure connection.
- *
- * This NPN service provider negotiates using HTTP2.
- *
- * To enable NPN support, start the JVM with: {@code java -Xbootclasspath/p: ...}. The
- * "path_to_npn_boot_jar" is the path on the file system for the NPN Boot Jar file which can be downloaded from Maven
- * at coordinates org.mortbay.jetty.npn:npn-boot. Different versions applies to different OpenJDK versions.
- *
- * @see Jetty documentation
- */
-public class Http2ClientProvider implements NextProtoNego.ClientProvider {
-
- private String selectedProtocol;
-
- @Override
- public String selectProtocol(List protocols) {
- if (protocols.contains(HTTP_2.protocolName())) {
- selectedProtocol = HTTP_2.protocolName();
- }
- return selectedProtocol;
- }
-
- @Override
- public boolean supports() {
- return true;
- }
-
- @Override
- public void unsupported() {
- selectedProtocol = HTTP_1_1.protocolName();
- }
-}
diff --git a/example/src/main/java/io/netty/example/http2/server/Http2OrHttpHandler.java b/example/src/main/java/io/netty/example/http2/server/Http2OrHttpHandler.java
index 1be6270e59..13160c44b2 100644
--- a/example/src/main/java/io/netty/example/http2/server/Http2OrHttpHandler.java
+++ b/example/src/main/java/io/netty/example/http2/server/Http2OrHttpHandler.java
@@ -17,10 +17,7 @@ package io.netty.example.http2.server;
import io.netty.channel.ChannelHandler;
import io.netty.handler.codec.http2.Http2OrHttpChooser;
-import org.eclipse.jetty.npn.NextProtoNego;
-
import javax.net.ssl.SSLEngine;
-
import java.util.logging.Logger;
/**
@@ -41,8 +38,8 @@ public class Http2OrHttpHandler extends Http2OrHttpChooser {
@Override
protected SelectedProtocol getProtocol(SSLEngine engine) {
- Http2ServerProvider provider = (Http2ServerProvider) NextProtoNego.get(engine);
- SelectedProtocol selectedProtocol = provider.getSelectedProtocol();
+ String[] protocol = engine.getSession().getProtocol().split(":");
+ SelectedProtocol selectedProtocol = SelectedProtocol.protocol(protocol[1]);
logger.info("Selected Protocol is " + selectedProtocol);
return selectedProtocol;
diff --git a/example/src/main/java/io/netty/example/http2/server/Http2Server.java b/example/src/main/java/io/netty/example/http2/server/Http2Server.java
index 42d5146d62..0604d190e5 100644
--- a/example/src/main/java/io/netty/example/http2/server/Http2Server.java
+++ b/example/src/main/java/io/netty/example/http2/server/Http2Server.java
@@ -22,10 +22,12 @@ import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
+import io.netty.handler.codec.http2.Http2OrHttpChooser.SelectedProtocol;
import io.netty.handler.ssl.SslContext;
-import io.netty.handler.ssl.SslProvider;
import io.netty.handler.ssl.util.SelfSignedCertificate;
+import java.util.Arrays;
+
/**
* A HTTP/2 Server that responds to requests with a Hello World.
*
@@ -60,7 +62,6 @@ public class Http2Server {
}
public static void main(String[] args) throws Exception {
- checkForNpnSupport();
int port;
if (args.length > 0) {
port = Integer.parseInt(args[0]);
@@ -72,22 +73,13 @@ public class Http2Server {
// Configure SSL context.
SelfSignedCertificate ssc = new SelfSignedCertificate();
- SslContext sslCtx = SslContext.newServerContext(SslProvider.JDK, ssc.certificate(), ssc.privateKey());
+ SslContext sslCtx = SslContext.newServerContext(
+ ssc.certificate(), ssc.privateKey(), null, null,
+ Arrays.asList(
+ SelectedProtocol.HTTP_2.protocolName(),
+ SelectedProtocol.HTTP_1_1.protocolName()),
+ 0, 0);
new Http2Server(sslCtx, port).run();
}
-
- public static void checkForNpnSupport() {
- try {
- Class.forName("sun.security.ssl.NextProtoNegoExtension");
- } catch (ClassNotFoundException ignored) {
- System.err.println();
- System.err.println("Could not locate Next Protocol Negotiation (NPN) implementation.");
- System.err.println("The NPN jar should have been made available when building the examples with maven.");
- System.err.println("Please check that your JDK is among those supported by Jetty-NPN:");
- System.err.println("http://wiki.eclipse.org/Jetty/Feature/NPN#Versions");
- System.err.println();
- throw new IllegalStateException("Could not locate NPN implementation. See console err for details.");
- }
- }
}
diff --git a/example/src/main/java/io/netty/example/http2/server/Http2ServerInitializer.java b/example/src/main/java/io/netty/example/http2/server/Http2ServerInitializer.java
index 19d2dfb94f..00f3f59529 100644
--- a/example/src/main/java/io/netty/example/http2/server/Http2ServerInitializer.java
+++ b/example/src/main/java/io/netty/example/http2/server/Http2ServerInitializer.java
@@ -17,13 +17,8 @@
package io.netty.example.http2.server;
import io.netty.channel.ChannelInitializer;
-import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.ssl.SslContext;
-import io.netty.handler.ssl.SslHandler;
-import org.eclipse.jetty.npn.NextProtoNego;
-
-import javax.net.ssl.SSLEngine;
/**
* Sets up the Netty pipeline
@@ -38,17 +33,6 @@ public class Http2ServerInitializer extends ChannelInitializer {
@Override
public void initChannel(SocketChannel ch) throws Exception {
- ChannelPipeline p = ch.pipeline();
-
- SslHandler handler = sslCtx.newHandler(ch.alloc());
- SSLEngine engine = handler.engine();
- p.addLast("ssl", new SslHandler(engine));
-
- // Setup NextProtoNego with our server provider
- NextProtoNego.put(engine, new Http2ServerProvider());
- NextProtoNego.debug = true;
-
- // Negotiates with the browser if HTTP2 or HTTP is going to be used
- p.addLast("handler", new Http2OrHttpHandler());
+ ch.pipeline().addLast(sslCtx.newHandler(ch.alloc()), new Http2OrHttpHandler());
}
}
diff --git a/example/src/main/java/io/netty/example/http2/server/Http2ServerProvider.java b/example/src/main/java/io/netty/example/http2/server/Http2ServerProvider.java
deleted file mode 100644
index 9000ae4638..0000000000
--- a/example/src/main/java/io/netty/example/http2/server/Http2ServerProvider.java
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- * Copyright 2014 The Netty Project
- *
- * The Netty Project 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 io.netty.example.http2.server;
-
-import io.netty.handler.codec.http2.Http2OrHttpChooser;
-
-import org.eclipse.jetty.npn.NextProtoNego.ServerProvider;
-
-import java.util.Arrays;
-import java.util.List;
-
-import static io.netty.handler.codec.http2.Http2OrHttpChooser.SelectedProtocol.*;
-
-/**
- * The Jetty project provides an implementation of the Transport Layer Security (TLS) extension for Next
- * Protocol Negotiation (NPN) for OpenJDK 7 or greater. NPN allows the application layer to negotiate which
- * protocol to use over the secure connection.
- *
- * This NPN service provider negotiates using HTTP_2.
- *
- * To enable NPN support, start the JVM with: {@code java -Xbootclasspath/p: ...}. The
- * "path_to_npn_boot_jar" is the path on the file system for the NPN Boot Jar file which can be downloaded from
- * Maven at coordinates org.mortbay.jetty.npn:npn-boot. Different versions applies to different OpenJDK versions.
- *
- * @see Jetty documentation
- */
-public class Http2ServerProvider implements ServerProvider {
-
- private String selectedProtocol;
-
- @Override
- public void unsupported() {
- // if unsupported, default to http/1.1
- selectedProtocol = HTTP_1_1.protocolName();
- }
-
- @Override
- public List protocols() {
- return Arrays.asList(HTTP_2.protocolName(), HTTP_1_1.protocolName());
- }
-
- @Override
- public void protocolSelected(String protocol) {
- selectedProtocol = protocol;
- }
-
- public Http2OrHttpChooser.SelectedProtocol getSelectedProtocol() {
- if (selectedProtocol == null) {
- return UNKNOWN;
- }
- return protocol(selectedProtocol);
- }
-}
diff --git a/example/src/main/java/io/netty/example/spdy/client/SpdyClient.java b/example/src/main/java/io/netty/example/spdy/client/SpdyClient.java
index f5fa16f513..fbae5d12f1 100644
--- a/example/src/main/java/io/netty/example/spdy/client/SpdyClient.java
+++ b/example/src/main/java/io/netty/example/spdy/client/SpdyClient.java
@@ -27,8 +27,8 @@ import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpVersion;
+import io.netty.handler.codec.spdy.SpdyOrHttpChooser.SelectedProtocol;
import io.netty.handler.ssl.SslContext;
-import io.netty.handler.ssl.SslProvider;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import javax.net.ssl.SSLException;
@@ -60,7 +60,13 @@ public class SpdyClient {
private EventLoopGroup workerGroup;
public SpdyClient(String host, int port) throws SSLException {
- sslCtx = SslContext.newClientContext(SslProvider.JDK, InsecureTrustManagerFactory.INSTANCE);
+ sslCtx = SslContext.newClientContext(
+ null, InsecureTrustManagerFactory.INSTANCE, null,
+ SslContext.newApplicationProtocolSelector(
+ SelectedProtocol.SPDY_3_1.protocolName(),
+ SelectedProtocol.HTTP_1_1.protocolName()),
+ 0, 0);
+
this.host = host;
this.port = port;
httpResponseHandler = new HttpResponseClientHandler();
diff --git a/example/src/main/java/io/netty/example/spdy/client/SpdyClientInitializer.java b/example/src/main/java/io/netty/example/spdy/client/SpdyClientInitializer.java
index 63400c472d..f8fc08bce4 100644
--- a/example/src/main/java/io/netty/example/spdy/client/SpdyClientInitializer.java
+++ b/example/src/main/java/io/netty/example/spdy/client/SpdyClientInitializer.java
@@ -23,10 +23,6 @@ import io.netty.handler.codec.spdy.SpdyHttpDecoder;
import io.netty.handler.codec.spdy.SpdyHttpEncoder;
import io.netty.handler.codec.spdy.SpdySessionHandler;
import io.netty.handler.ssl.SslContext;
-import io.netty.handler.ssl.SslHandler;
-import org.eclipse.jetty.npn.NextProtoNego;
-
-import javax.net.ssl.SSLEngine;
import static io.netty.handler.codec.spdy.SpdyVersion.*;
import static io.netty.util.internal.logging.InternalLogLevel.*;
@@ -45,14 +41,8 @@ public class SpdyClientInitializer extends ChannelInitializer {
@Override
public void initChannel(SocketChannel ch) throws Exception {
- SslHandler sslHandler = sslCtx.newHandler(ch.alloc());
- SSLEngine engine = sslHandler.engine();
- NextProtoNego.put(engine, new SpdyClientProvider());
- NextProtoNego.debug = true;
-
ChannelPipeline pipeline = ch.pipeline();
-
- pipeline.addLast("ssl", sslHandler);
+ pipeline.addLast("ssl", sslCtx.newHandler(ch.alloc()));
pipeline.addLast("spdyFrameCodec", new SpdyFrameCodec(SPDY_3_1));
pipeline.addLast("spdyFrameLogger", new SpdyFrameLogger(INFO));
pipeline.addLast("spdySessionHandler", new SpdySessionHandler(SPDY_3_1, false));
diff --git a/example/src/main/java/io/netty/example/spdy/client/SpdyClientProvider.java b/example/src/main/java/io/netty/example/spdy/client/SpdyClientProvider.java
deleted file mode 100644
index c4adedf39b..0000000000
--- a/example/src/main/java/io/netty/example/spdy/client/SpdyClientProvider.java
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * Copyright 2014 The Netty Project
- *
- * The Netty Project 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 io.netty.example.spdy.client;
-
-import org.eclipse.jetty.npn.NextProtoNego.ClientProvider;
-
-import java.util.List;
-
-import static io.netty.handler.codec.spdy.SpdyOrHttpChooser.SelectedProtocol.*;
-
-/**
- * The Jetty project provides an implementation of the Transport Layer Security (TLS) extension for Next Protocol
- * Negotiation (NPN) for OpenJDK 7 or greater. NPN allows the application layer to negotiate which protocol to use
- * over the secure connection.
- *
- * This NPN service provider negotiates using SPDY.
- *
- * To enable NPN support, start the JVM with: {@code java -Xbootclasspath/p: ...}. The
- * "path_to_npn_boot_jar" is the path on the file system for the NPN Boot Jar file which can be downloaded from Maven
- * at coordinates org.mortbay.jetty.npn:npn-boot. Different versions applies to different OpenJDK versions.
- *
- * @see Jetty documentation
- */
-public class SpdyClientProvider implements ClientProvider {
-
- private String selectedProtocol;
-
- @Override
- public String selectProtocol(List protocols) {
- if (protocols.contains(SPDY_3_1.protocolName())) {
- return SPDY_3_1.protocolName();
- }
- return selectedProtocol;
- }
-
- @Override
- public boolean supports() {
- return true;
- }
-
- @Override
- public void unsupported() {
- selectedProtocol = HTTP_1_1.protocolName();
- }
-}
diff --git a/example/src/main/java/io/netty/example/spdy/server/SpdyOrHttpHandler.java b/example/src/main/java/io/netty/example/spdy/server/SpdyOrHttpHandler.java
index a65218f0fd..eda57e3f87 100644
--- a/example/src/main/java/io/netty/example/spdy/server/SpdyOrHttpHandler.java
+++ b/example/src/main/java/io/netty/example/spdy/server/SpdyOrHttpHandler.java
@@ -17,7 +17,6 @@ package io.netty.example.spdy.server;
import io.netty.channel.ChannelHandler;
import io.netty.handler.codec.spdy.SpdyOrHttpChooser;
-import org.eclipse.jetty.npn.NextProtoNego;
import javax.net.ssl.SSLEngine;
import java.util.logging.Logger;
@@ -41,8 +40,8 @@ public class SpdyOrHttpHandler extends SpdyOrHttpChooser {
@Override
protected SelectedProtocol getProtocol(SSLEngine engine) {
- SpdyServerProvider provider = (SpdyServerProvider) NextProtoNego.get(engine);
- SelectedProtocol selectedProtocol = provider.getSelectedProtocol();
+ String[] protocol = engine.getSession().getProtocol().split(":");
+ SelectedProtocol selectedProtocol = SelectedProtocol.protocol(protocol[1]);
logger.info("Selected Protocol is " + selectedProtocol);
return selectedProtocol;
diff --git a/example/src/main/java/io/netty/example/spdy/server/SpdyServer.java b/example/src/main/java/io/netty/example/spdy/server/SpdyServer.java
index 21e2d8ebe5..a281dd5ddb 100644
--- a/example/src/main/java/io/netty/example/spdy/server/SpdyServer.java
+++ b/example/src/main/java/io/netty/example/spdy/server/SpdyServer.java
@@ -21,10 +21,12 @@ import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
+import io.netty.handler.codec.spdy.SpdyOrHttpChooser.SelectedProtocol;
import io.netty.handler.ssl.SslContext;
-import io.netty.handler.ssl.SslProvider;
import io.netty.handler.ssl.util.SelfSignedCertificate;
+import java.util.Arrays;
+
/**
* A SPDY Server that responds to a GET request with a Hello World.
*
@@ -72,7 +74,6 @@ public class SpdyServer {
}
public static void main(String[] args) throws Exception {
- checkForNpnSupport();
int port;
if (args.length > 0) {
port = Integer.parseInt(args[0]);
@@ -86,21 +87,13 @@ public class SpdyServer {
// Configure SSL.
SelfSignedCertificate ssc = new SelfSignedCertificate();
- SslContext sslCtx = SslContext.newServerContext(SslProvider.JDK, ssc.certificate(), ssc.privateKey());
+ SslContext sslCtx = SslContext.newServerContext(
+ ssc.certificate(), ssc.privateKey(), null, null,
+ Arrays.asList(
+ SelectedProtocol.SPDY_3_1.protocolName(),
+ SelectedProtocol.HTTP_1_1.protocolName()),
+ 0, 0);
+
new SpdyServer(sslCtx, port).run();
}
-
- private static void checkForNpnSupport() {
- try {
- Class.forName("sun.security.ssl.NextProtoNegoExtension");
- } catch (ClassNotFoundException ignored) {
- System.err.println();
- System.err.println("Could not locate Next Protocol Negotiation (NPN) implementation.");
- System.err.println("The NPN jar should have been made available when building the examples with maven.");
- System.err.println("Please check that your JDK is among those supported by Jetty-NPN:");
- System.err.println("http://wiki.eclipse.org/Jetty/Feature/NPN#Versions");
- System.err.println();
- throw new IllegalStateException("Could not locate NPN implementation. See console err for details.");
- }
- }
}
diff --git a/example/src/main/java/io/netty/example/spdy/server/SpdyServerHandler.java b/example/src/main/java/io/netty/example/spdy/server/SpdyServerHandler.java
index b1af42ebc9..6d04fc1a8a 100644
--- a/example/src/main/java/io/netty/example/spdy/server/SpdyServerHandler.java
+++ b/example/src/main/java/io/netty/example/spdy/server/SpdyServerHandler.java
@@ -23,6 +23,7 @@ import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpRequest;
+import io.netty.handler.ssl.SslHandler;
import io.netty.util.CharsetUtil;
import java.util.Date;
@@ -43,6 +44,8 @@ public class SpdyServerHandler extends SimpleChannelInboundHandler