2017-01-15 6 views
0

Ich mag würde folgenden Python-Code in eine äquivalenten in Java replizieren:SSL-Verbindung mit Zertifikat

import socket, ssl, pprint 

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
ssl_sock = ssl.wrap_socket(s, 
          ca_certs="server.crt", 
          cert_reqs=ssl.CERT_REQUIRED) 

ssl_sock.connect(('localhost', 10023)) 
ssl_sock.write("boo!") 

if False: 
    data = ssl_sock.read() 
    ssl_sock.close() 

ich mit dem folgenden Code kam (was nicht funktioniert), aber ich habe noch zwei Fragen:

abstract class Connection extends AsyncTask<String, Void, String> { 


    boolean Connect(){ 
     try 
     { 
      SSLSocketFactory factory=(SSLSocketFactory) SSLSocketFactory.getDefault(); 
      SSLSocket mySSLSocket=(SSLSocket) factory.createSocket("localhost", 10023); 

      DataOutputStream myOutPutStream = new DataOutputStream(mySSLSocket.getOutputStream()); 
      myOutPutStream.writeBytes("Hello World!"); 

      myOutPutStream.close(); 
      mySSLSocket.close(); 

      return true; 

     } 
     catch(Exception e) 
     { 
      return false; 
     } 

    } 

} 

Durch Ausführen des Codes ich die folgende Java-Fehler erhalten:

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 
  1. Wo kann ich das Zertifikat in meinem Android-Anwendungsprojekt (Android Studio) speichern?

  2. Wie lautet der entsprechende Code in Java?

    ssl_sock = ssl.wrap_socket (s, ca_certs = "server.crt" cert_reqs = ssl.CERT_REQUIRED)

Antwort

0

Versuchen -Djavax.net.ssl.trustStore=/path/to/keystore, wobei '/ path/to/Schlüsselspeicher' ist die absolute Dateipfad des alternativen Keystores.

Dies muss eine VM-Option für die Ausführung Ihres Codes/Projekts sein.