1

In meiner Android-Anwendung verbinde ich über https. Ich verwende ein selbstsigniertes Zertifikat, um eine Verbindung herzustellen. Es wird auf Geräten unter api Ebene arbeiten 24 (vor android Nougat) .Aber auf Android Nougat es die SSL-Handshake Ausnahme auslöst:SSL-Handshake Ausnahme beim Verbinden über https mit selbst signierten Zertifikat in Android Nougat

javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Vertrauensanker für Zertifizierungspfad nicht gefunden. Diese

ist, wie ich https Verbindung über: -

SSLContext context = null; 
    try 
    { 
     KeyStore keyStore = KeyStore.getInstance("PKCS12"); 
     // Get the raw resource, which contains the keystore with 
     // your trusted certificates (root and any intermediate certs) 
     InputStream input = new BufferedInputStream(context.getAssets().open(pkcsFilename)); 
     try { 
      // Initialize the keystore with the provided trusted certificates 
      // Also provide the password of the keystore 
      keyStore.load(input, password.toCharArray()); 
     } finally { 
      input.close(); 
     } 

     KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); 
     keyFactory.init(keyStore, "".toCharArray()); 

     // Load CAs from an InputStream 
     // (could be from a resource or ByteArrayInputStream or ...) 
     CertificateFactory cf = CertificateFactory.getInstance("X.509"); 
     Certificate ca = null; 
     input = new BufferedInputStream(context.getAssets().open(certificateFilename)); 
     try 
     { 
      ca = cf.generateCertificate(input); 
     } 
     finally 
     { 
      input.close(); 
     } 
     KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); 
     trustStore.load(null, null); 
     trustStore.setCertificateEntry("server", ca); 

     // Create a TrustManager that trusts the CAs in our KeyStore 
     TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); 
     tmf.init(trustStore); 

     // Create an SSLContext that uses our TrustManager 
     context = SSLContext.getInstance("TLS"); 
     context.init(keyFactory.getKeyManagers(), tmf.getTrustManagers(), null); 

     HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection(); 

ich folgende link versucht, aber es hilft nicht.

Dies ist meine Netzwerkkonfigurationsdatei. Ich habe es in meiner AndroidManifest.xml-Datei hinzugefügt.

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

Bitte helfen Sie mir, wie das zu lösen.

Antwort

0

habe ich es arbeiten über Eigentum zu verwenden versuchen. Während der SSL Context context.init(keyFactory.getKeyManagers(), tmf.getTrustManagers(), null);

I modified it as : 

context.init(keyFactory.getKeyManagers(), new TrustManager[] { tm }, null); 
TrustManager tm = new X509TrustManager() { 
       public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { 
       } 

       public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { 
        for (int j=0; j<chain.length; j++) 
        { 
         chain[j].checkValidity(); 
         try { 
          chain[j].verify(ca.getPublicKey()); 
         } catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchProviderException | 
           SignatureException e) { 
          e.printStackTrace(); 
          throw new CertificateException(e.getMessage()); 
         } 
        } 
       } 

       public X509Certificate[] getAcceptedIssuers() { 
        return null; 
       } 
      }; 

Initialisierung i das Zertifikat mit dem Serverzertifikat überprüfen. Jetzt geht es .

Verwandte Themen