Android N network security configuration to boot at boot Self-signed certificate

In Android N, for SSL certificate I have to add this code (according to the provided Android developer link )

 <?xml version="1.0" encoding="utf-8"?>
 <manifest ... >
<application android:networkSecurityConfig="@xml/network_security_config"
                ... >
    ...
</application>

And the network_security_config.xml file in the xml folder is

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config>
    <domain includeSubdomains="true">example.com</domain>
    <trust-anchors>
        <certificates src="@raw/my_ca"/>
    </trust-anchors>
</domain-config>

This works fine for a single static domain, but my problem is: the server domain will not be applied every time in my application. The second problem is that I download the SSL certificate from my server domain at run time, so every time I can update the certificate file in the raw folder, because we know that we can not write the file in the raw folder at run time.

, Android N .

Edit1: , , .

Edit2: , , 200, "ok" Android Android ( ).

  CertificateFactory cf = CertificateFactory.getInstance("X.509", "BC");

                    InputStream caInput = new BufferedInputStream(new FileInputStream(certFile));
                       X509Certificate ca =(X509Certificate) cf.generateCertificate(caInput);
                    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);
                    TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
                        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                            return new java.security.cert.X509Certificate[]{};
                        }

                        public void checkClientTrusted(X509Certificate[] chain,
                                                       String authType) throws CertificateException {
                        }

                        public void checkServerTrusted(X509Certificate[] chain,
                                                       String authType) throws CertificateException {
                        }
                    }};
                    // Create an SSLContext that uses our TrustManager
                    HttpsURLConnection.setDefaultHostnameVerifier(new NullHostNameVerifier());
                    SSLContext sslcontext = SSLContext.getInstance("TLS");
                    sslcontext.init(null, trustAllCerts, new java.security.SecureRandom());

                    // Tell the URLConnection to use a SocketFactory from our SSLContext
                    url = new URL(wsdlUrl);
                    HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
                    urlConnection.setSSLSocketFactory(sslcontext.getSocketFactory());
                    urlConnection.setConnectTimeout(5000);
                    urlConnection.connect();

                    if (urlConnection.getResponseCode() == 200) {  //Successful response.
                        result = true;
                    } else {
                        result = false;
                    }

200 SOAP- ,

 com.neurospeech.wsclient.SoapFaultException: Server Error 
 at com.neurospeech.wsclient.SoapWebService.postXML(SoapWebService.java:225)
 at com.neurospeech.wsclient.SoapWebService.getSoapResponse(SoapWebService.java:157)
 at com.vxlsoftware.fudmagent.serviceclasses.AndroidServiceAsync.access$1300(AndroidServiceAsync.java:6)
 at com.vxlsoftware.fudmagent.serviceclasses.AndroidServiceAsync$setAndroidClient     HeartbitRequest.executeRequest(AndroidServiceAsync.java:367)
 at com.neurospeech.wsclient.ServiceRequest.run(ServiceRequest.java:20)
 at java.lang.Thread.run(Thread.java:761)

, Android N, SOAP- .

, , , , , , .

+4
1

. , X509TrustManager, , SSLContext, SSLSocketFactory SSLEngine HTTPS. , . , , , MiTM .

. https://developer.android.com/training/articles/security-ssl.html#UnknownCa. , HttpsURLConnection.

+4

Source: https://habr.com/ru/post/1653121/


All Articles