TheProjecter / sardine

Automatically exported from code.google.com/p/sardine
0 stars 0 forks source link

HTTPS support #15

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
https schema is not registered. It's not possible to acces WEBDAV resources
over HTTPS

Original issue reported on code.google.com by christia...@gmail.com on 19 Jan 2010 at 5:02

GoogleCodeExporter commented 9 years ago
This issue was closed by revision r57.

Original comment by latch...@gmail.com on 19 Jan 2010 at 5:23

GoogleCodeExporter commented 9 years ago
ok, i think i fixed this, could you please check it out from svn and try it 
before i do a 
release?

Original comment by latch...@gmail.com on 19 Jan 2010 at 5:24

GoogleCodeExporter commented 9 years ago

Original comment by latch...@gmail.com on 20 Jan 2010 at 1:02

GoogleCodeExporter commented 9 years ago
Thanks for the fix. Unfortunately it is a little more complicated: the server
certificate has to be accepted. I have a working solution with a keystore, but 
it is
not generic. Maybe i have time to fix it today an send you a diff.

Original comment by christia...@gmail.com on 20 Jan 2010 at 6:46

GoogleCodeExporter commented 9 years ago
ok, what i may do is expose the httpcore internals a bit more so that this can 
be 
modified outside of sardine. if you get a chance for a patch, then that would 
be great.

Original comment by latch...@gmail.com on 20 Jan 2010 at 7:15

GoogleCodeExporter commented 9 years ago
not fixed yet

Original comment by latch...@gmail.com on 20 Jan 2010 at 7:15

GoogleCodeExporter commented 9 years ago
Here is a patch:
I added a third parameter to the begin methode: a opened keystore with the
certificate which should be accepted. The problem of loading and opening the 
keystor
is in the resposibility of the caller.

btw: i am using sardine to connect to a scalix groupware server. I have 
different
issues i'm working on (PROPFIND allprop not implemended in scalix, date paring
problems). When i found out more, i will report the other issues. I like 
sardine!

--- original/Factory.java       2010-01-07 16:40:00.000000000 +0100
+++ patched/Factory.java        2010-01-20 16:22:28.000000000 +0100
@@ -1,5 +1,7 @@
 package com.googlecode.sardine;

+import java.security.KeyStore;
+
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Unmarshaller;
@@ -62,8 +64,9 @@ public class Factory
        }

        /** */
-       public Sardine begin(String username, String password)
+       public Sardine begin(String username, String password, KeyStore 
truststore)
        {
-               return new SardineImpl(this, username, password);
+               return new SardineImpl(this, username, password, truststore);
        }
+
 }
diff -rupN original/SardineFactory.java patched/SardineFactory.java
--- original/SardineFactory.java        2010-01-07 16:40:00.000000000 +0100
+++ patched/SardineFactory.java 2010-01-20 16:22:10.000000000 +0100
@@ -1,5 +1,6 @@
 package com.googlecode.sardine;

+import java.security.KeyStore;
 import java.util.List;

 import javax.xml.bind.JAXBContext;
@@ -44,10 +45,20 @@ public class SardineFactory
         */
        public static Sardine begin(String username, String password)
        {
-               return Factory.instance().begin(username, password);
+               return Factory.instance().begin(username, password, null);
        }

        /**
+        * Pass in a HTTP Auth username/password for being used with all
+        * connections and a keystore with trusted server certificates for SSL
connections
+        */
+       public static Sardine begin(String username, String password, KeyStore
truststore)
+       {
+               return Factory.instance().begin(username, password, truststore);
+       }
+
+
+       /**
         * for testing
         */
        public static void main(String[] args) throws Exception
diff -rupN original/SardineImpl.java patched/SardineImpl.java
--- original/SardineImpl.java   2010-01-07 16:40:00.000000000 +0100
+++ patched/SardineImpl.java    2010-01-20 16:24:16.000000000 +0100
@@ -1,8 +1,16 @@
 package com.googlecode.sardine;

+import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.security.KeyManagementException;
+import java.security.KeyStore;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
+import java.security.UnrecoverableKeyException;
+import java.security.cert.CertificateException;
 import java.util.ArrayList;
+import java.util.Enumeration;
 import java.util.List;

 import org.apache.http.HttpResponse;
@@ -20,6 +28,7 @@ import org.apache.http.conn.params.ConnM
 import org.apache.http.conn.scheme.PlainSocketFactory;
 import org.apache.http.conn.scheme.Scheme;
 import org.apache.http.conn.scheme.SchemeRegistry;
+import org.apache.http.conn.ssl.SSLSocketFactory;
 import org.apache.http.entity.ByteArrayEntity;
 import org.apache.http.entity.InputStreamEntity;
 import org.apache.http.impl.client.DefaultHttpClient;
@@ -58,11 +67,11 @@ public class SardineImpl implements Sard
        /** */
        public SardineImpl(Factory factory)
        {
-               this(factory, null, null);
+               this(factory, null, null, null);
        }

        /** */
-       public SardineImpl(Factory factory, String username, String password)
+       public SardineImpl(Factory factory, String username, String password,
KeyStore truststore)
        {
                this.factory = factory;

@@ -73,7 +82,23 @@ public class SardineImpl implements Sard
                SchemeRegistry schemeRegistry = new SchemeRegistry();
                schemeRegistry.register(
                        new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
-
+               if (truststore != null){
+                       SSLSocketFactory sslSocketFactory=null;
+                               try {
+                                       sslSocketFactory = new
SSLSocketFactory(truststore);
+                               } catch (KeyManagementException ex) {
+
+                               } catch (UnrecoverableKeyException ex) {
+
+                               } catch (NoSuchAlgorithmException ex) {
+
+                               } catch (KeyStoreException ex) {
+
+                               }
+
+                      
sslSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIE
R);
+                       schemeRegistry.register(new Scheme("https", 
sslSocketFactory,
443));
+               }
                ClientConnectionManager cm = new ThreadSafeClientConnManager(params,
schemeRegistry);
                this.client = new DefaultHttpClient(cm, params);

@@ -82,7 +107,6 @@ public class SardineImpl implements Sard
                        new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
                        new UsernamePasswordCredentials(username, password));
        }
-
        /*
         * (non-Javadoc)
         * @see com.googlecode.sardine.Sardine#getResources(java.lang.String)

Original comment by christia...@gmail.com on 20 Jan 2010 at 3:40

GoogleCodeExporter commented 9 years ago
This issue was closed by revision r63.

Original comment by latch...@gmail.com on 21 Jan 2010 at 10:53