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 2017/05/09 20:03:51 UTC

[22/35] httpcomponents-core git commit: HTTPCORE-268: handle runtime exceptions thrown by SSLEngine (merged from trunk)

HTTPCORE-268: handle runtime exceptions thrown by SSLEngine (merged from trunk)

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpcore/branches/4.1.x@1157115 13f79535-47bb-0310-9956-ffa450edef68


Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/commit/76ed6f59
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/76ed6f59
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/76ed6f59

Branch: refs/heads/4.1.x
Commit: 76ed6f59868ede6b9f3faf1ddb5bdf6be6cb5825
Parents: cc013c3
Author: Oleg Kalnichevski <ol...@apache.org>
Authored: Fri Aug 12 14:13:52 2011 +0000
Committer: Oleg Kalnichevski <ol...@apache.org>
Committed: Fri Aug 12 14:13:52 2011 +0000

----------------------------------------------------------------------
 RELEASE_NOTES.txt                               |  6 +++
 .../http/impl/nio/reactor/SSLIOSession.java     | 53 +++++++++++++++++---
 2 files changed, 52 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/76ed6f59/RELEASE_NOTES.txt
----------------------------------------------------------------------
diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt
index fe0f1f9..f59bd02 100644
--- a/RELEASE_NOTES.txt
+++ b/RELEASE_NOTES.txt
@@ -1,3 +1,9 @@
+Changes since 4.1.3
+-------------------
+
+* [HTTPCORE-268] Handle runtime exceptions thrown by SSLEngine.
+  Contributed by Oleg Kalnichevski <olegk at apache.org>
+
 Release 4.1.3
 -------------------
 

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/76ed6f59/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/SSLIOSession.java
----------------------------------------------------------------------
diff --git a/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/SSLIOSession.java b/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/SSLIOSession.java
index 4cff03c..d7324c9 100644
--- a/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/SSLIOSession.java
+++ b/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/SSLIOSession.java
@@ -142,6 +142,46 @@ public class SSLIOSession implements IOSession, SessionBufferStatus {
         doHandshake();
     }
 
+    // A works-around for exception handling craziness in Sun/Oracle's SSLEngine
+    // implementation.
+    //
+    // sun.security.pkcs11.wrapper.PKCS11Exception is re-thrown as
+    // plain RuntimeException in sun.security.ssl.Handshaker#checkThrown
+    private SSLException convert(final RuntimeException ex) throws SSLException {
+		Throwable cause = ex.getCause();
+		if (cause == null) {
+			cause = ex;
+		}
+		return new SSLException(cause);
+    }
+
+    private SSLEngineResult doWrap(final ByteBuffer src, final ByteBuffer dst) throws SSLException {
+    	try {
+        	return this.sslEngine.wrap(src, dst);
+    	} catch (RuntimeException ex) {
+    		throw convert(ex);
+    	}
+    }
+
+    private SSLEngineResult doUnwrap(final ByteBuffer src, final ByteBuffer dst) throws SSLException {
+    	try {
+        	return this.sslEngine.unwrap(src, dst);
+    	} catch (RuntimeException ex) {
+    		throw convert(ex);
+    	}
+    }
+
+    private void doRunTask() throws SSLException {
+    	try {
+            Runnable r = this.sslEngine.getDelegatedTask();
+            if (r != null) {
+            	r.run();
+            }
+    	} catch (RuntimeException ex) {
+    		throw convert(ex);
+    	}
+    }
+
     private void doHandshake() throws SSLException {
         boolean handshaking = true;
 
@@ -151,7 +191,7 @@ public class SSLIOSession implements IOSession, SessionBufferStatus {
             case NEED_WRAP:
                 // Generate outgoing handshake data
                 this.outPlain.flip();
-                result = this.sslEngine.wrap(this.outPlain, this.outEncrypted);
+                result = doWrap(this.outPlain, this.outEncrypted);
                 this.outPlain.compact();
                 if (result.getStatus() != Status.OK) {
                     handshaking = false;
@@ -160,15 +200,14 @@ public class SSLIOSession implements IOSession, SessionBufferStatus {
             case NEED_UNWRAP:
                 // Process incoming handshake data
                 this.inEncrypted.flip();
-                result = this.sslEngine.unwrap(this.inEncrypted, this.inPlain);
+                result = doUnwrap(this.inEncrypted, this.inPlain);
                 this.inEncrypted.compact();
                 if (result.getStatus() != Status.OK) {
                     handshaking = false;
                 }
                 break;
             case NEED_TASK:
-                Runnable r = this.sslEngine.getDelegatedTask();
-                r.run();
+            	doRunTask();
                 break;
             case NOT_HANDSHAKING:
                 handshaking = false;
@@ -246,7 +285,7 @@ public class SSLIOSession implements IOSession, SessionBufferStatus {
         SSLEngineResult.Status opStatus = Status.OK;
         while (this.inEncrypted.position() > 0 && opStatus == Status.OK) {
             this.inEncrypted.flip();
-            SSLEngineResult result = this.sslEngine.unwrap(this.inEncrypted, this.inPlain);
+            SSLEngineResult result = doUnwrap(this.inEncrypted, this.inPlain);
             this.inEncrypted.compact();
 
             opStatus = result.getStatus();
@@ -302,11 +341,11 @@ public class SSLIOSession implements IOSession, SessionBufferStatus {
         }
         if (this.outPlain.position() > 0) {
             this.outPlain.flip();
-            this.sslEngine.wrap(this.outPlain, this.outEncrypted);
+            doWrap(this.outPlain, this.outEncrypted);
             this.outPlain.compact();
         }
         if (this.outPlain.position() == 0) {
-            SSLEngineResult result = this.sslEngine.wrap(src, this.outEncrypted);
+            SSLEngineResult result = doWrap(src, this.outEncrypted);
             if (result.getStatus() == Status.CLOSED) {
                 this.status = CLOSED;
             }