{"id":39,"date":"2016-06-21T20:28:46","date_gmt":"2016-06-21T18:28:46","guid":{"rendered":"http:\/\/www.petersplanet.nl\/?p=39"},"modified":"2016-07-09T14:38:06","modified_gmt":"2016-07-09T12:38:06","slug":"how-to-bypass-the-ssl-security-check-in-jython","status":"publish","type":"post","link":"https:\/\/www.petersplanet.nl\/index.php\/2016\/06\/21\/how-to-bypass-the-ssl-security-check-in-jython\/","title":{"rendered":"How to bypass the SSL security check in Jython"},"content":{"rendered":"<p>In the past to bypass the SSL security check in Jython I have used the example published at <a href=\"http:\/\/jython.xhaus.com\/installing-an-all-trusting-security-provider-on-java-and-jython\/\">jython.xhaus.com<\/a>.\u00a0Unfortunately this doesn&#8217;t work anymore with Java 8 and Jython 2.7.0. Luckily I found a solution for Java at\u00a0<a href=\"http:\/\/log.rowanto.com\/java-8-turning-off-ssl-certificate-check\/\">log.rowanto.com<\/a>. Now I only had to transform this into a solution for Jython. This can be seen below.\u00a0<!--more--><br \/>\nDisclaimer: bypassing the SSL security check is not what you normally should do. Use it at your own risk.<\/p>\n<p>Put the MyProvider code into a file MyProvider.java. Compile it, create a jar-file of it and put it into your Jython classpath. Then use it in your Jython program as showed below. For this I have used the Eclipse IDE with the Jython PyDev plugin.<\/p>\n<pre>import java.net.Socket;\r\nimport java.security.KeyStore;\r\nimport java.security.Provider;\r\nimport java.security.cert.CertificateException;\r\nimport java.security.cert.X509Certificate;\r\nimport javax.net.ssl.ManagerFactoryParameters;\r\nimport javax.net.ssl.SSLEngine;\r\nimport javax.net.ssl.TrustManager;\r\nimport javax.net.ssl.TrustManagerFactorySpi;\r\nimport javax.net.ssl.X509ExtendedTrustManager;\r\n\r\npublic class MyProvider extends Provider {\r\n    public MyProvider() {\r\n        super(\"MyProvider\", 1.0, \"Trust certificates\");\r\n        put(\"TrustManagerFactory.TrustAllCertificates\", MyTrustManagerFactory.class.getName());\r\n    }\r\n\r\n    public static class MyTrustManagerFactory extends TrustManagerFactorySpi {\r\n        public MyTrustManagerFactory() {}\r\n\t    protected void engineInit( KeyStore keystore ) {}\r\n\t    protected void engineInit(ManagerFactoryParameters mgrparams ) {}\r\n\t    protected TrustManager[] engineGetTrustManagers() {\r\n\t    \treturn new TrustManager[] {\r\n\t    \t    new X509ExtendedTrustManager() {\r\n\t    \t\t    \t        \r\n\t    \t    \t@Override\r\n\t    \t\tpublic void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {}\r\n\t    \t\t    \t        \r\n\t    \t\t@Override\r\n\t    \t\tpublic void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {}\r\n\t    \t\t    \t        \r\n\t    \t\t@Override    \t\t    \t        \r\n\t    \t\tpublic X509Certificate[] getAcceptedIssuers() {\r\n\t    \t\t    return null;\r\n\t    \t\t}\r\n\t    \t\t    \t        \r\n\t    \t\t@Override\r\n\t    \t\tpublic void checkClientTrusted(X509Certificate[] x509Certificates, String s, Socket socket) throws CertificateException {}\r\n\t    \t\t    \t        \r\n\t    \t\t@Override\r\n\t    \t\tpublic void checkServerTrusted(X509Certificate[] x509Certificates, String s, Socket socket) throws CertificateException {}\r\n\t    \t\t    \t        \r\n\t    \t\t@Override\r\n\t    \t\tpublic void checkClientTrusted(X509Certificate[] x509Certificates, String s, SSLEngine sslEngine) throws CertificateException {}\r\n\t    \t\t    \t        \r\n\t    \t\t@Override\r\n\t    \t\tpublic void checkServerTrusted(X509Certificate[] x509Certificates, String s, SSLEngine sslEngine) throws CertificateException {}\r\n\t    \t    }\t    \t\t\t\t    \t\t\t\r\n\t    \t};\r\n\t    }\r\n    }\r\n}\r\n<\/pre>\n<pre>import java.security.Security\r\nimport MyProvider\r\n\r\n# Install the all-trusting trust manager\r\njava.security.Security.addProvider(MyProvider())\r\njava.security.Security.setProperty(\"ssl.TrustManagerFactory.algorithm\", \"TrustAllCertificates\")\r\n\r\n# Your code\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In the past to bypass the SSL security check in Jython I have used the example published at jython.xhaus.com.\u00a0Unfortunately this doesn&#8217;t work anymore with Java 8 and Jython 2.7.0. Luckily I found a solution for Java at\u00a0log.rowanto.com. Now I only had to transform this into a solution for Jython. This can be seen below.\u00a0<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[10],"class_list":["post-39","post","type-post","status-publish","format-standard","hentry","category-programming","tag-jython"],"_links":{"self":[{"href":"https:\/\/www.petersplanet.nl\/index.php\/wp-json\/wp\/v2\/posts\/39","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.petersplanet.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.petersplanet.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.petersplanet.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.petersplanet.nl\/index.php\/wp-json\/wp\/v2\/comments?post=39"}],"version-history":[{"count":8,"href":"https:\/\/www.petersplanet.nl\/index.php\/wp-json\/wp\/v2\/posts\/39\/revisions"}],"predecessor-version":[{"id":47,"href":"https:\/\/www.petersplanet.nl\/index.php\/wp-json\/wp\/v2\/posts\/39\/revisions\/47"}],"wp:attachment":[{"href":"https:\/\/www.petersplanet.nl\/index.php\/wp-json\/wp\/v2\/media?parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.petersplanet.nl\/index.php\/wp-json\/wp\/v2\/categories?post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.petersplanet.nl\/index.php\/wp-json\/wp\/v2\/tags?post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}