Https Connection Android

后端 未结 15 1705
萌比男神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: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.

提交回复
热议问题