<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Wil Tan &#187; java</title>
	<atom:link href="http://dready.org/blog/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://dready.org/blog</link>
	<description>musings on internationalized identifiers: domain names, OpenID, TLDs</description>
	<lastBuildDate>Thu, 15 Dec 2011 03:42:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Java SSL Implementation does not detect disconnected socket</title>
		<link>http://dready.org/blog/2008/11/22/java-ssl-implementation-does-not-detect-disconnected-socket/</link>
		<comments>http://dready.org/blog/2008/11/22/java-ssl-implementation-does-not-detect-disconnected-socket/#comments</comments>
		<pubDate>Fri, 21 Nov 2008 16:05:10 +0000</pubDate>
		<dc:creator>wil</dc:creator>
				<category><![CDATA[hacks]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jdk]]></category>
		<category><![CDATA[JVM]]></category>
		<category><![CDATA[networking]]></category>
		<category><![CDATA[socket]]></category>
		<category><![CDATA[ssl]]></category>
		<category><![CDATA[tls]]></category>

		<guid isPermaLink="false">http://dready.org/blog/?p=183</guid>
		<description><![CDATA[To be exact, Sun&#8217;s JSSE SSL/TLS implementation ignores the EOFException that is generated when its underlying socket was closed properly (that is if the server initiates a proper shutdown) and you try to write to it.
This is what you get when debug is turned on (using the -Djavax.net.debug=ssl switch):

Thread, WRITE: TLSv1 Application Data, length = [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>To be exact, Sun&#8217;s JSSE SSL/TLS implementation <strong>ignores</strong> the <code>EOFException</code> that is generated when its underlying socket was closed properly (that is if the server initiates a proper shutdown) and you try to write to it.</p>
<p>This is what you get when debug is turned on (using the <code>-Djavax.net.debug=ssl</code> switch):</p>
<pre>
Thread, WRITE: TLSv1 Application Data, length = 499
Thread, received EOFException: ignoredThread, called closeInternal(false)
Thread, SEND TLSv1 ALERT:  warning, description = close_notify
Thread, WRITE: TLSv1 Alert, length = 18
</pre>
<p>In order to detect the closed connection, you actually need to perform a read on the SSLSocket&#8217;s input stream:</p>
<pre>
Socket sock = factory.createSocket(host, port);
// factory is an SSLSocketFactory() instance
// ...
// ...
// At some point, peer has already shutdown the connection
// ...
// write a byte
OutputStream out = socket.getOutputStream();
out.write(32);
// no exception here
out.flush();
// still no exception
// read something from it
InputStream in = socket.getInputStream();
int n = in.read();
// n becomes -1 here
</pre>
<p>One would expect the <code>write</code> to throw an exception, since the underlying socket was closed. It seems that the <a href="http://hc.apache.org/httpclient-3.x/sslguide.html">Apache HttpClient</a> folks did discover this:</p>
<blockquote><p>
Due to what appears to be a bug in Sun&#8217;s older (below 1.4) implementation of Java Virtual Machines or JSSE there&#8217;s no reliable way of telling if an SSL connection is &#8217;stale&#8217; or not. For example, the HTTP 1.1 specification permits HTTP servers in &#8216;keep-alive&#8217; mode to drop the connection to the client after a given period inactivity without having to notify the client, effectively rendering such connection unusable or &#8217;stale&#8217;. For the HTTP agent written in Java there&#8217;s no reliable way to test if a connection is &#8217;stale&#8217; other than attempting to perform a read on it. However, a read operation on an idle SSL connection on Sun JVM older than 1.4 returns &#8216;end of stream&#8217; instead of an expected read timeout. That effectively makes the connection appear &#8217;stale&#8217; to HttpClient, which leaves it with no other way but to drop the connection and to open a new one, thus defeating HTTP 1.1 keep-alive mechanism and resulting in significant performance degradation (SSL authentication is a highly time consuming operation). The problem appears to have been fixed in Sun&#8217;s Java 1.4 SSL implementation. Sockets which are not using HTTPS are unaffected on any JVM.
</p></blockquote>
<p>However, in many network protocols, there is a fixed command-response sequence so reading from the socket before writing does pose a problem. What the Apache HttpClient folks did was to wrap the socket <code>InputStream</code> in a <code>BufferedInputStream</code>, then mark the stream position, try to read a single byte from it with a timeout of 1ms. If read() returns <code>-1</code> the socket is considered stale, and it resets the stream to the marked position. This is less than ideal, but probably works.</p>
<p>As far as I know, this problem exists on JDK versions 1.4 and 1.5.</p>
<p>There you go, I just wanted to throw it out there for anyone who happens to stumble on the same problem. Personally, I&#8217;ve spent a good afternoon chasing down this problem, from googling to hunting for the JSSE source code (which I gave up finding after discovering that the JDK 1.6 source doesn&#8217;t include it.)</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://dready.org/blog/2008/11/22/java-ssl-implementation-does-not-detect-disconnected-socket/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

