<?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>Jython Journeys &#187; Security</title>
	<atom:link href="http://jython.xhaus.com/tag/security/feed/" rel="self" type="application/rss+xml" />
	<link>http://jython.xhaus.com</link>
	<description>Notes about my work with jython and python</description>
	<lastBuildDate>Sat, 05 Mar 2011 20:40:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Installing an all-trusting security provider on java and jython.</title>
		<link>http://jython.xhaus.com/installing-an-all-trusting-security-provider-on-java-and-jython/</link>
		<comments>http://jython.xhaus.com/installing-an-all-trusting-security-provider-on-java-and-jython/#comments</comments>
		<pubDate>Fri, 29 Aug 2008 16:38:00 +0000</pubDate>
		<dc:creator>alan.kennedy</dc:creator>
				<category><![CDATA[jython]]></category>
		<category><![CDATA[networking]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://jython.xhaus.com/?p=4</guid>
		<description><![CDATA[Back in 2007, I updated the jython socket module to support client-side ssl sockets. This post will describe how to configure jython so that it behaves like cpython, in relation to acceptance of SSL certificates.

The support was very basic, because it is an implementation of the existing core cpython ssl socket support, which is really [...]]]></description>
			<content:encoded><![CDATA[<p>Back in 2007, <a title="Jython Changeset 3227" href="http://fisheye3.atlassian.com/changelog/jython?cs=3227" target="_self">I updated the jython socket module to support client-side ssl sockets</a>. This post will describe how to configure jython so that it behaves like cpython, in relation to acceptance of SSL certificates.</p>
<p><span id="more-4"></span></p>
<p>The support was very basic, because it is an implementation of the existing <a title="Link to cpython SSL socket documentation" href="http://www.python.org/doc/lib/ssl-objects.html">core cpython ssl socket support</a>, which is really basic, merely allowing a client to make an SSL connection to a server. Cpython has no server-side SSL support in the standard library (although multiple 3rd party modules do support it), and it has no support for certificate management or verification. So basically the cpython ssl support is really only useful for testing; without certificate verification, it&#8217;s not really usable in scenarios where authentication actually matters, e.g. E-commerce.</p>
<p>Because the jython socket module is layered on top of java socket libraries, it automatically inherits java behaviour in relation to certificate validation; <strong>java by default always validates certificates</strong>.</p>
<p>Which is great, if you want certificate validation; it means that you can use the jython ssl support when counterparty authentication actually matters, because it will always verify that the server certificate is valid, and will refuse to form a connection if the certificate is not valid.</p>
<p>However, this is problematic for jython users who want to use the jython ssl support like they would use the cpython ssl support: for testing. In testing phases, it&#8217;s very common for users to create and test with their own <a title="Self signed certificates" href="http://en.wikipedia.org/wiki/Self-signed_certificate">self-signed certificates</a>; installing real production certificates may be difficult for a number of operational reasons.</p>
<p>As described above, this is not a problem for cpython users. But jython users have to go an extra step in order to be able to use self-signed certificates. There are two choices</p>
<ol>
<li>Add the self signed certificate to the JVM configuration. To do that, read this <a title="Installing and Configuring SSL Support " href="http://java.sun.com/j2ee/1.4/docs/tutorial/doc/Security6.html">Sun article on Installing and Configuring SSL Support</a>.</li>
<li>Install a custom java Security Provider which simply trusts all certificates. <strong>WARNING: Installing a security provider that accepts all certificates leaves you open to all forms of impersonation and other identity-verification failures; only use it for testing purposes!</strong> To install a custom security provider requires writing some java. I&#8217;ll explain it below.</li>
</ol>
<p>In order to change the Java trust decision process, you have to create an <a title="Link to X509TrustManager documentation" href="http://java.sun.com/j2se/1.5.0/docs/api/javax/net/ssl/X509TrustManager.html">X509TrustManager</a>. Java TrustManagers are based on an exception model; the trust manager is called to determine if a given client, server or issuer is trusted. If not, the TrustManager should raise a <a title="Link to CertificateException documentation" href="http://java.sun.com/j2se/1.5.0/docs/api/java/security/cert/CertificateException.html">CertificateException</a>. Which means that if a TrustManager wishes to trust everyone, it simply should not raise exceptions when a trust request is made.</p>
<p>But you can&#8217;t just create and install a TrustManager; TrustManagers are created by a <a title="Link to TrustManagerFactory documentation" href="http://java.sun.com/j2se/1.5.0/docs/api/javax/net/ssl/TrustManagerFactory.html">TrustManagerFactory</a>, so you have to create and install one of those. And in order to do that, you have to create and install a <a title="Link to java.security.Provider documentation" href="http://java.sun.com/j2se/1.5.0/docs/api/java/security/Provider.html">java.security.Provider</a>. Sounds complex? </p>
<p>Well, thankfully it&#8217;s not that complex, as you can see in the code sample from this article on the ZXTC KnowledgeHub: <a title="Link to article containing source code to install a new java.security.Provider" href="http://knowledgehub.zeus.com/articles/2006/01/03/using_the_control_api_with_java">Using the Control API with Java.</a> I have reproduced that code here.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">&nbsp;
<span style="color: #666666; font-style: italic;">// Example java.security.Provider implementation</span>
<span style="color: #666666; font-style: italic;">// that trusts ALL SSL certificates</span>
<span style="color: #666666; font-style: italic;">// Regardless of whether they are valid or not</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Store this code in a file called MyProvider.java</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.security.Security</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.security.KeyStore</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.security.Provider</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.security.cert.X509Certificate</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.net.ssl.ManagerFactoryParameters</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.net.ssl.TrustManager</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.net.ssl.TrustManagerFactorySpi</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.net.ssl.X509TrustManager</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyProvider <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Provider</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> MyProvider<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MyProvider&quot;</span>, <span style="color: #cc66cc;">1.0</span>, <span style="color: #0000ff;">&quot;Trust certificates&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    put<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;TrustManagerFactory.TrustAllCertificates&quot;</span>, MyTrustManagerFactory.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> MyTrustManagerFactory <span style="color: #000000; font-weight: bold;">extends</span> TrustManagerFactorySpi
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> MyTrustManagerFactory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> engineInit<span style="color: #009900;">&#40;</span> <span style="color: #003399;">KeyStore</span> keystore <span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> engineInit<span style="color: #009900;">&#40;</span>ManagerFactoryParameters mgrparams <span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> TrustManager<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> engineGetTrustManagers<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> TrustManager<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">new</span> MyX509TrustManager<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> MyX509TrustManager <span style="color: #000000; font-weight: bold;">implements</span> X509TrustManager
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> checkClientTrusted<span style="color: #009900;">&#40;</span><span style="color: #003399;">X509Certificate</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> chain, <span style="color: #003399;">String</span> authType<span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> checkServerTrusted<span style="color: #009900;">&#40;</span><span style="color: #003399;">X509Certificate</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> chain, <span style="color: #003399;">String</span> authType<span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">X509Certificate</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> getAcceptedIssuers<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This class should be stored in a file called MyProvider.java, and compiled with javac to generate a MyProvider.class file, which you should place in the classpath.</p>
<p>Once that&#8217;s complete, execute the following piece of jython</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">java.<span style="color: black;">security</span>.<span style="color: black;">Security</span>.<span style="color: black;">addProvider</span><span style="color: black;">&#40;</span>MyProvider<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
java.<span style="color: black;">security</span>.<span style="color: black;">Security</span>.<span style="color: black;">setProperty</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;ssl.TrustManagerFactory.algorithm&quot;</span>, <span style="color: #483d8b;">&quot;TrustAllCertificates&quot;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span></pre></div></div>

<p>And your code should now start accepting all SSL certificates, regardless of whether they are valid or not.</p>
<p>This issue came up on jython-users, where a jython user was wondering why, after he had installed his own TrustManagerFactory, he could access an HTTPS url on a server with an invalid certificate, without problems, when using <a title="Link to java.net.URL exception" href="http://java.sun.com/j2se/1.5.0/docs/api/java/net/URL.html">java.net.URL</a>.</p>
<p>But when he tried to use jython&#8217;s <a title="Link to urllib2 documentation" href="http://docs.python.org/lib/module-urllib2.html">urllib2</a>, it failed with a <a title="Link to CertPathValidatorException documentation" href="http://java.sun.com/j2se/1.5.0/docs/api/java/security/cert/CertPathValidatorException.html">java.security.cert.CertPathValidatorException</a>. The reason for that is that the jython urllib2 module does not use java.net.URL; it uses the jython socket module directly. So over-riding the default TrustManagerFactory for the <a title="Link to javax.net.ssl.HttpsURLConnection documentation" href="http://java.sun.com/j2se/1.5.0/docs/api/javax/net/ssl/HttpsURLConnection.html">javax.net.ssl.HttpsURLConnection</a> class has no effect on anything but instances of the HttpsURLConnection class. Which happens to be the class that java.net.URL uses to make HTTPS connections.</p>
<p>Read the jython-users post <a title="Link to jython-users post about invalid certs." href="http://www.nabble.com/https---invalid-certs-td19198649.html">HTTPS and invalid certs</a> for more details.</p>
<p>Lastly, this information is reproduced on the <a title="Link to jython wiki documentation for the socket module." href="http://wiki.python.org/jython/NewSocketModule">Jython Socket Module Wiki page</a>, which I try to keep up-to-date as much as possible.</p>
]]></content:encoded>
			<wfw:commentRss>http://jython.xhaus.com/installing-an-all-trusting-security-provider-on-java-and-jython/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

