You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2007/03/11 22:48:23 UTC

svn commit: r517023 - in /jakarta/httpcomponents/httpcore/trunk/module-niossl/src: examples/org/apache/http/examples/nio/ main/java/org/apache/http/impl/nio/reactor/

Author: olegk
Date: Sun Mar 11 14:48:22 2007
New Revision: 517023

URL: http://svn.apache.org/viewvc?view=rev&rev=517023
Log:
HTTPCORE-55: Added SSL session handler interface that can be used to customize some aspects of SSL sessions such as initialization of the SSL engine and verification of the remote host name

Added:
    jakarta/httpcomponents/httpcore/trunk/module-niossl/src/main/java/org/apache/http/impl/nio/reactor/SSLIOSessionHandler.java   (with props)
    jakarta/httpcomponents/httpcore/trunk/module-niossl/src/main/java/org/apache/http/impl/nio/reactor/SSLMode.java   (with props)
Modified:
    jakarta/httpcomponents/httpcore/trunk/module-niossl/src/examples/org/apache/http/examples/nio/NHttpSSLServer.java
    jakarta/httpcomponents/httpcore/trunk/module-niossl/src/main/java/org/apache/http/impl/nio/reactor/SSLClientIOEventDispatch.java
    jakarta/httpcomponents/httpcore/trunk/module-niossl/src/main/java/org/apache/http/impl/nio/reactor/SSLIOSession.java
    jakarta/httpcomponents/httpcore/trunk/module-niossl/src/main/java/org/apache/http/impl/nio/reactor/SSLServerIOEventDispatch.java

Modified: jakarta/httpcomponents/httpcore/trunk/module-niossl/src/examples/org/apache/http/examples/nio/NHttpSSLServer.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-niossl/src/examples/org/apache/http/examples/nio/NHttpSSLServer.java?view=diff&rev=517023&r1=517022&r2=517023
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-niossl/src/examples/org/apache/http/examples/nio/NHttpSSLServer.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-niossl/src/examples/org/apache/http/examples/nio/NHttpSSLServer.java Sun Mar 11 14:48:22 2007
@@ -128,7 +128,8 @@
         
         IOEventDispatch ioEventDispatch = new SSLServerIOEventDispatch(
                 handler, 
-                sslcontext, 
+                sslcontext,
+                null,
                 params);
         
         ListeningIOReactor ioReactor = new DefaultListeningIOReactor(2, params);

Modified: jakarta/httpcomponents/httpcore/trunk/module-niossl/src/main/java/org/apache/http/impl/nio/reactor/SSLClientIOEventDispatch.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-niossl/src/main/java/org/apache/http/impl/nio/reactor/SSLClientIOEventDispatch.java?view=diff&rev=517023&r1=517022&r2=517023
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-niossl/src/main/java/org/apache/http/impl/nio/reactor/SSLClientIOEventDispatch.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-niossl/src/main/java/org/apache/http/impl/nio/reactor/SSLClientIOEventDispatch.java Sun Mar 11 14:48:22 2007
@@ -51,10 +51,12 @@
     private final NHttpClientHandler handler;
     private final HttpParams params;
     private final SSLContext sslcontext;
+    private final SSLIOSessionHandler sslHandler;
     
     public SSLClientIOEventDispatch(
             final NHttpClientHandler handler,
             final SSLContext sslcontext,
+            final SSLIOSessionHandler sslHandler,
             final HttpParams params) {
         super();
         if (handler == null) {
@@ -69,13 +71,22 @@
         this.handler = handler;
         this.params = params;
         this.sslcontext = sslcontext;
+        this.sslHandler = sslHandler;
+    }
+    
+    public SSLClientIOEventDispatch(
+            final NHttpClientHandler handler,
+            final SSLContext sslcontext,
+            final HttpParams params) {
+        this(handler, sslcontext, null, params);
     }
     
     public void connected(final IOSession session) {
 
         SSLIOSession sslSession = new SSLIOSession(
                 session, 
-                this.sslcontext); 
+                this.sslcontext,
+                this.sslHandler); 
         
         DefaultNHttpClientConnection conn = new DefaultNHttpClientConnection(
                 sslSession, 
@@ -89,7 +100,7 @@
         this.handler.connected(conn, attachment);
 
         try {
-            sslSession.initialize(true);
+            sslSession.initialize(SSLMode.CLIENT, this.params);
         } catch (SSLException ex) {
             this.handler.exception(conn, ex);
             sslSession.shutdown();

Modified: jakarta/httpcomponents/httpcore/trunk/module-niossl/src/main/java/org/apache/http/impl/nio/reactor/SSLIOSession.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-niossl/src/main/java/org/apache/http/impl/nio/reactor/SSLIOSession.java?view=diff&rev=517023&r1=517022&r2=517023
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-niossl/src/main/java/org/apache/http/impl/nio/reactor/SSLIOSession.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-niossl/src/main/java/org/apache/http/impl/nio/reactor/SSLIOSession.java Sun Mar 11 14:48:22 2007
@@ -47,6 +47,7 @@
 import org.apache.http.nio.reactor.EventMask;
 import org.apache.http.nio.reactor.IOSession;
 import org.apache.http.nio.reactor.SessionBufferStatus;
+import org.apache.http.params.HttpParams;
 
 /**
  * A decorator class intended to transparently extend an {@link IOSession} 
@@ -63,6 +64,7 @@
     private final ByteBuffer inPlain;
     private final ByteBuffer outPlain;
     private final InternalByteChannel channel;
+    private final SSLIOSessionHandler handler;
     
     private int appEventMask;
     private SessionBufferStatus appBufferStatus;
@@ -71,7 +73,8 @@
     
     public SSLIOSession(
             final IOSession session, 
-            final SSLContext sslContext) {
+            final SSLContext sslContext, 
+            final SSLIOSessionHandler handler) {
         super();
         if (session == null) {
             throw new IllegalArgumentException("IO session may not be null");
@@ -82,6 +85,7 @@
         this.session = session;
         this.appEventMask = session.getEventMask();
         this.channel = new InternalByteChannel();
+        this.handler = handler;
         
         // Override the status buffer interface
         this.session.setBufferStatus(this);
@@ -106,10 +110,31 @@
         this.outPlain = ByteBuffer.allocateDirect(appBuffersize);
     }
     
-    public synchronized void initialize(boolean clientMode) throws SSLException {
-        this.sslEngine.setUseClientMode(clientMode);
+    public synchronized void initialize(
+            final SSLMode mode, 
+            final HttpParams params) throws SSLException {
+        if (params == null) {
+            throw new IllegalArgumentException("HTTP parameters may not be null");
+        }
+        switch (mode) {
+        case CLIENT:
+            this.sslEngine.setUseClientMode(true);
+            break;
+        case SERVER:
+            this.sslEngine.setUseClientMode(false);
+            break;
+        }
+        if (this.handler != null) {
+            this.handler.initalize(this.sslEngine, params);
+        }
         this.sslEngine.beginHandshake();
         doHandshake();
+
+        if (this.handler != null) {
+            this.handler.verify(
+                    this.session.getRemoteAddress(), 
+                    this.sslEngine.getSession());
+        }
     }
     
     private void doHandshake() throws SSLException {
@@ -419,5 +444,5 @@
         }
         
     }
-    
+
 }

Added: jakarta/httpcomponents/httpcore/trunk/module-niossl/src/main/java/org/apache/http/impl/nio/reactor/SSLIOSessionHandler.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-niossl/src/main/java/org/apache/http/impl/nio/reactor/SSLIOSessionHandler.java?view=auto&rev=517023
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-niossl/src/main/java/org/apache/http/impl/nio/reactor/SSLIOSessionHandler.java (added)
+++ jakarta/httpcomponents/httpcore/trunk/module-niossl/src/main/java/org/apache/http/impl/nio/reactor/SSLIOSessionHandler.java Sun Mar 11 14:48:22 2007
@@ -0,0 +1,50 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.impl.nio.reactor;
+
+import java.net.SocketAddress;
+
+import javax.net.ssl.SSLEngine;
+import javax.net.ssl.SSLException;
+import javax.net.ssl.SSLSession;
+
+import org.apache.http.params.HttpParams;
+
+public interface SSLIOSessionHandler {
+
+    void initalize(SSLEngine sslengine, HttpParams params)
+        throws SSLException;
+
+    void verify(SocketAddress remoteAddress, SSLSession session)
+        throws SSLException;
+    
+}

Propchange: jakarta/httpcomponents/httpcore/trunk/module-niossl/src/main/java/org/apache/http/impl/nio/reactor/SSLIOSessionHandler.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpcore/trunk/module-niossl/src/main/java/org/apache/http/impl/nio/reactor/SSLIOSessionHandler.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/httpcomponents/httpcore/trunk/module-niossl/src/main/java/org/apache/http/impl/nio/reactor/SSLMode.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-niossl/src/main/java/org/apache/http/impl/nio/reactor/SSLMode.java?view=auto&rev=517023
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-niossl/src/main/java/org/apache/http/impl/nio/reactor/SSLMode.java (added)
+++ jakarta/httpcomponents/httpcore/trunk/module-niossl/src/main/java/org/apache/http/impl/nio/reactor/SSLMode.java Sun Mar 11 14:48:22 2007
@@ -0,0 +1,39 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.impl.nio.reactor;
+
+public enum SSLMode {
+
+    CLIENT,
+    SERVER
+    
+}

Propchange: jakarta/httpcomponents/httpcore/trunk/module-niossl/src/main/java/org/apache/http/impl/nio/reactor/SSLMode.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpcore/trunk/module-niossl/src/main/java/org/apache/http/impl/nio/reactor/SSLMode.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jakarta/httpcomponents/httpcore/trunk/module-niossl/src/main/java/org/apache/http/impl/nio/reactor/SSLServerIOEventDispatch.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-niossl/src/main/java/org/apache/http/impl/nio/reactor/SSLServerIOEventDispatch.java?view=diff&rev=517023&r1=517022&r2=517023
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-niossl/src/main/java/org/apache/http/impl/nio/reactor/SSLServerIOEventDispatch.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-niossl/src/main/java/org/apache/http/impl/nio/reactor/SSLServerIOEventDispatch.java Sun Mar 11 14:48:22 2007
@@ -50,11 +50,13 @@
    
     private final NHttpServiceHandler handler;
     private final SSLContext sslcontext;
+    private final SSLIOSessionHandler sslHandler;
     private final HttpParams params;
     
     public SSLServerIOEventDispatch(
             final NHttpServiceHandler handler,
             final SSLContext sslcontext,
+            final SSLIOSessionHandler sslHandler,
             final HttpParams params) {
         super();
         if (handler == null) {
@@ -69,13 +71,22 @@
         this.handler = handler;
         this.params = params;
         this.sslcontext = sslcontext;
+        this.sslHandler = sslHandler;
+    }
+    
+    public SSLServerIOEventDispatch(
+            final NHttpServiceHandler handler,
+            final SSLContext sslcontext,
+            final HttpParams params) {
+        this(handler, sslcontext, null, params);
     }
     
     public void connected(final IOSession session) {
 
         SSLIOSession sslSession = new SSLIOSession(
                 session, 
-                this.sslcontext); 
+                this.sslcontext,
+                this.sslHandler); 
         
         DefaultNHttpServerConnection conn = new DefaultNHttpServerConnection(
                 sslSession, 
@@ -88,7 +99,7 @@
         this.handler.connected(conn);
 
         try {
-            sslSession.initialize(false);
+            sslSession.initialize(SSLMode.SERVER, this.params);
         } catch (SSLException ex) {
             this.handler.exception(conn, ex);
             sslSession.shutdown();