Https Connection Android

后端 未结 15 1724
萌比男神i
萌比男神i 2020-11-22 04:43

I am doing a https post and I\'m getting an exception of ssl exception Not trusted server certificate. If i do normal http it is working perfectly fine. Do I have to accept

相关标签:
15条回答
  • 2020-11-22 05:37

    While trying to answer this question I found a better tutorial. With it you don't have to compromise the certificate check.

    http://blog.crazybob.org/2010/02/android-trusting-ssl-certificates.html

    *I did not write this but thanks to Bob Lee for the work

    0 讨论(0)
  • 2020-11-22 05:37

    You can also look at my blog article, very similar to crazybobs.

    This solution also doesn't compromise certificate checking and explains how to add the trusted certs in your own keystore.

    http://blog.antoine.li/index.php/2010/10/android-trusting-ssl-certificates/

    0 讨论(0)
  • 2020-11-22 05:38

    This is a known problem with Android 2.x. I was struggling with this problem for a week until I came across the following question, which not only gives a good background of the problem but also provides a working and effective solution devoid of any security holes.

    'No peer certificate' error in Android 2.3 but NOT in 4

    0 讨论(0)
  • 2020-11-22 05:38

    Sources that helped me get to work with my self signed certificate on my AWS Apache server and connect with HttpsURLConnection from android device:

    SSL on aws instance - amazon tutorial on ssl
    Android Security with HTTPS and SSL - creating your own trust manager on client for accepting your certificate
    Creating self signed certificate - easy script for creating your certificates

    Then I did the following:

    1. Made sure the server supports https (sudo yum install -y mod24_ssl)
    2. Put this script in a file create_my_certs.sh:
    #!/bin/bash
    FQDN=$1
    
    # make directories to work from
    mkdir -p server/ client/ all/
    
    # Create your very own Root Certificate Authority
    openssl genrsa \
      -out all/my-private-root-ca.privkey.pem \
      2048
    
    # Self-sign your Root Certificate Authority
    # Since this is private, the details can be as bogus as you like
    openssl req \
      -x509 \
      -new \
      -nodes \
      -key all/my-private-root-ca.privkey.pem \
      -days 1024 \
      -out all/my-private-root-ca.cert.pem \
      -subj "/C=US/ST=Utah/L=Provo/O=ACME Signing Authority Inc/CN=example.com"
    
    # Create a Device Certificate for each domain,
    # such as example.com, *.example.com, awesome.example.com
    # NOTE: You MUST match CN to the domain name or ip address you want to use
    openssl genrsa \
      -out all/privkey.pem \
      2048
    
    # Create a request from your Device, which your Root CA will sign
    openssl req -new \
      -key all/privkey.pem \
      -out all/csr.pem \
      -subj "/C=US/ST=Utah/L=Provo/O=ACME Tech Inc/CN=${FQDN}"
    
    # Sign the request from Device with your Root CA
    openssl x509 \
      -req -in all/csr.pem \
      -CA all/my-private-root-ca.cert.pem \
      -CAkey all/my-private-root-ca.privkey.pem \
      -CAcreateserial \
      -out all/cert.pem \
      -days 500
    
    # Put things in their proper place
    rsync -a all/{privkey,cert}.pem server/
    cat all/cert.pem > server/fullchain.pem         # we have no intermediates in this case
    rsync -a all/my-private-root-ca.cert.pem server/
    rsync -a all/my-private-root-ca.cert.pem client/
    
    1. Run bash create_my_certs.sh yourdomain.com
    2. Place the certificates in their proper place on the server (you can find configuration in /etc/httpd/conf.d/ssl.conf). All these should be set:
      SSLCertificateFile
      SSLCertificateKeyFile
      SSLCertificateChainFile
      SSLCACertificateFile

    3. Restart httpd using sudo service httpd restart and make sure httpd started:
      Stopping httpd: [ OK ]
      Starting httpd: [ OK ]

    4. Copy my-private-root-ca.cert to your android project assets folder

    5. Create your trust manager:

      SSLContext SSLContext;

      CertificateFactory cf = CertificateFactory.getInstance("X.509"); InputStream caInput = context.getAssets().open("my-private-root-ca.cert.pem"); Certificate ca; try { ca = cf.generateCertificate(caInput); } finally { caInput.close(); }

        // Create a KeyStore containing our trusted CAs
        String keyStoreType = KeyStore.getDefaultType();
        KeyStore keyStore = KeyStore.getInstance(keyStoreType);
        keyStore.load(null, null);
        keyStore.setCertificateEntry("ca", ca);
      
        // Create a TrustManager that trusts the CAs in our KeyStore
        String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
        tmf.init(keyStore);
      
        // Create an SSLContext that uses our TrustManager
        SSLContext = SSLContext.getInstance("TLS");
        SSSLContext.init(null, tmf.getTrustManagers(), null);
      
    6. And make the connection using HttpsURLConnection:

      HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); connection.setSSLSocketFactory(SSLContext.getSocketFactory());

    7. Thats it, try your https connection.

    0 讨论(0)
  • 2020-11-22 05:39

    For some reason the solution mentioned for httpClient above didn't worked for me. At the end I was able to make it work by correctly overriding the method when implementing the custom SSLSocketFactory class.

    @Override
    public Socket createSocket(Socket socket, String host, int port, boolean autoClose) 
                                  throws IOException, UnknownHostException 
        {
        return sslFactory.createSocket(socket, host, port, autoClose);
    }
    
    @Override
    public Socket createSocket() throws IOException {
        return sslFactory.createSocket();
    }
    

    This is how it worked perfectly for me. You can see the full custom class and implementing on the following thread: http://blog.syedgakbar.com/2012/07/21/android-https-and-not-trusted-server-certificate-error/

    0 讨论(0)
  • 2020-11-22 05:44

    http://madurangasblogs.blogspot.in/2013/08/avoiding-javaxnetsslsslpeerunverifiedex.html

    Courtesy Maduranga

    When developing an application that uses https, your test server doesn't have a valid SSL certificate. Or sometimes the web site is using a self-signed certificate or the web site is using free SSL certificate. So if you try to connect to the server using Apache HttpClient, you will get a exception telling that the "peer not authenticated". Though it is not a good practice to trust all the certificates in a production software, you may have to do so according to the situation. This solution resolves the exception caused by "peer not authenticated".

    But before we go to the solution, I must warn you that this is not a good idea for a production application. This will violate the purpose of using a security certificate. So unless you have a good reason or if you are sure that this will not cause any problem, don't use this solution.

    Normally you create a HttpClient like this.

    HttpClient httpclient = new DefaultHttpClient();

    But you have to change the way you create the HttpClient.

    First you have to create a class extending org.apache.http.conn.ssl.SSLSocketFactory.

    import org.apache.http.conn.ssl.SSLSocketFactory;
    import java.io.IOException;
    import java.net.Socket;
    import java.net.UnknownHostException;
    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.security.cert.X509Certificate;
    
    import javax.net.ssl.SSLContext;
    import javax.net.ssl.TrustManager;
    import javax.net.ssl.X509TrustManager;
    
    public class MySSLSocketFactory extends SSLSocketFactory {
             SSLContext sslContext = SSLContext.getInstance("TLS");
    
        public MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
            super(truststore);
    
            TrustManager tm = new X509TrustManager() {
                public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                }
    
                public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                }
    
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };
    
            sslContext.init(null, new TrustManager[] { tm }, null);
        }
    
        @Override
        public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
            return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
        }
    
        @Override
        public Socket createSocket() throws IOException {
            return sslContext.getSocketFactory().createSocket();
        }
    }
    

    Then create a method like this.

    public HttpClient getNewHttpClient() {
         try {
             KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
             trustStore.load(null, null);
    
             SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
             sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    
             HttpParams params = new BasicHttpParams();
             HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
             HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
    
             SchemeRegistry registry = new SchemeRegistry();
             registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
             registry.register(new Scheme("https", sf, 443));
    
             ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
    
             return new DefaultHttpClient(ccm, params);
         } catch (Exception e) {
             return new DefaultHttpClient();
         }
    }
    

    Then you can create the HttpClient.

    HttpClient httpclient = getNewHttpClient();

    If you are trying to send a post request to a login page the rest of the code would be like this.

    private URI url = new URI("url of the action of the form");
    HttpPost httppost =  new HttpPost(url);
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
    nameValuePairs.add(new BasicNameValuePair("username", "user"));  
    nameValuePairs.add(new BasicNameValuePair("password", "password"));
    try {
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        InputStream is = entity.getContent();
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    

    You get the html page to the InputStream. Then you can do whatever you want with the returned html page.

    But here you will face a problem. If you want to manage a session using cookies, you will not be able to do it with this method. If you want to get the cookies, you will have to do it via a browser. Then only you will receive cookies.

    0 讨论(0)
提交回复
热议问题