Android HTTPS-Verbindung mit Client-Authentifizierung

witschi

Mitglied
Hallo Leute,

ich bin gerade verzweifelt dabei zu versuchen eine HTTPS-Verbindung zu meinem Server herzustellen. Dabei soll eine Client-Authentifizierung zum Einsatz kommen. Ich habe mein Zertifikat in einer *.p12-Datei und habe damit auch erfolgreich geschafft, eine Verbindung herzustellen:

Java:
File p12File = new File("zertifikat.p12");
SSLContext sslctx = createContextFromPKCS12(p12File,"password".toCharArray());

...

static private SSLContext createContextFromPKCS12(File file, char[] passwd)
        throws Exception
    {
        SSLContext sslctx = SSLContext.getInstance("TLS");

        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        keyStore.load(new FileInputStream(file), passwd);
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");
        kmf.init(keyStore, passwd);

        TrustManagerFactory tmf = createTrustManager(keyStore);

        sslctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
        return sslctx;
    }

...

    //  der trusted Keystore muss JKS-formatiert sein. Eine leerer Keystore
    //  wird mit den Zertifikaten aus dem privaten KeyStore gefüllt
    static private TrustManagerFactory createTrustManager(KeyStore ksKeys)
    throws Exception
    {
        KeyStore ksTrust = KeyStore.getInstance("BKS");
        ksTrust.load(null,null);
        for (Enumeration<String> enu = ksKeys.aliases(); enu.hasMoreElements();)
        {
            String s = (String) enu.nextElement();
			Certificate[] acertificate = ksKeys.getCertificateChain(s);
            if (acertificate == null)
                continue;
            for (int i = 0; i < acertificate.length; i++)
            {
                if ( (acertificate[i] instanceof X509Certificate))
                {
                    X509Certificate cert = (X509Certificate)acertificate[i];
                    String name = cert.getSubjectDN().toString();
                    int from = name.indexOf("CN=") + 3;
                    int to = name.indexOf(",", from);
                    ksTrust.setCertificateEntry(name.substring(from, to), cert);
                 }
            }
        }
        TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
        tmf.init(ksTrust);
        return tmf;
    }

Nun möchte ich aber das Zertifikat über die Einstellungen in den KeyChain des Geräts laden, was auch geklappt. Nun bekomme ich es aber nicht mehr hin, den SSLContext so zu initialisieren, dass der Handshake erfolgreich ist.

Folgendes schlägt leider fehl:

Java:
PrivateKey privateKey = KeyChain.getPrivateKey(E2dActivity.this, alias);
X509Certificate[] certificateChain = KeyChain.getCertificateChain(E2dActivity.this, alias);

Log.d(TAG, certificateChain.toString());

sslctx = SSLContext.getInstance("TLS");

KeyStore keyStore = KeyStore.getInstance("PKCS12");
// keyStore.load(new FileInputStream(file), passwd);
KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");
// kmf.init(keyStore, passwd);


KeyStore ksTrust = KeyStore.getInstance("BKS");
ksTrust.load(null, null);
for (int i = 0; i < certificateChain.length; i++) {
	if ((certificateChain[i] instanceof X509Certificate)) {
		X509Certificate cert = (X509Certificate) certificateChain[i];
		String name = cert.getSubjectDN().toString();
		int from = name.indexOf("CN=") + 3;
		int to = name.indexOf(",", from);
		ksTrust.setCertificateEntry(name.substring(from, to), cert);
	}
}

TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
tmf.init(ksTrust);

sslctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

Hier liegt vermutlich der Hund begraben, ich begreife aber einfach nicht, wie ich dem SSLContext beibringe, mein Zeritifkat aus dem KeyChain zu verwenden...

Ich hoffe mir kann jemand helfen... :(
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
U Android Https-Verbindung Android & Cross-Platform Mobile Apps 2
S Android Probleme beim Verbinden mit einer HTTPS Seite Android & Cross-Platform Mobile Apps 4
A https tutorial Android & Cross-Platform Mobile Apps 11
missy72 Kotlin SSH Verbindung mit JSch Android & Cross-Platform Mobile Apps 5
J Android VPN Verbindung herstellen? Android & Cross-Platform Mobile Apps 4
B Android TCP-Verbindung zum Server über welche Prozess auslagerung nutzen? Android & Cross-Platform Mobile Apps 1
M Problem bei Werteübergabe, MSQL verbindung Android & Cross-Platform Mobile Apps 3
H WIFI, Bluetooth und NFC Verbindung überwachen Android & Cross-Platform Mobile Apps 1
H Android 3G TCP Socket Verbindung zum PC durch NAT Android & Cross-Platform Mobile Apps 8
T Android MSSQL Verbindung herstellen - Android Studio Android & Cross-Platform Mobile Apps 2
M Android Server-Client-Verbindung in Android-App mit Sockets aufbauen Android & Cross-Platform Mobile Apps 5
R Socket Verbindung AsycTask Android & Cross-Platform Mobile Apps 5
F Android USB Verbindung zu Windows Programm Android & Cross-Platform Mobile Apps 3
K Java ME Bluetooth verbindung parameter Android & Cross-Platform Mobile Apps 3
N Java ME Server-Client Verbindung über Wifi Android & Cross-Platform Mobile Apps 6
A Problem mit HTTP- Verbindung Android & Cross-Platform Mobile Apps 4
N Handy -PC Verbindung Android & Cross-Platform Mobile Apps 2
N Blutooz-Verbindung ... ich schaffs nicht Android & Cross-Platform Mobile Apps 5
F Server - Client Verbindung mit Java ME Android & Cross-Platform Mobile Apps 3
G Bluetooth Verbindung zwischen Handy und PC Android & Cross-Platform Mobile Apps 5
G Bluetooth Verbindung Android & Cross-Platform Mobile Apps 2
O Bluetooth Verbindung zwischen 2 Handys Android & Cross-Platform Mobile Apps 5
K HTTP-Verbindung mit J2ME.... Android & Cross-Platform Mobile Apps 2
M Android TCP Client Android & Cross-Platform Mobile Apps 4
ruutaiokwu Android Selbst entwickelter SMTP-Client läuft auf PC, nicht aber auf Android Android & Cross-Platform Mobile Apps 9
M Android Simpler TLS/SSL Client Android & Cross-Platform Mobile Apps 11
R Android Mail Client öffnen Android & Cross-Platform Mobile Apps 4
Exdroid Android Broadcast client Android & Cross-Platform Mobile Apps 1
N Android Client-Server-Kommunikation: push oder poll? Android & Cross-Platform Mobile Apps 10

Ähnliche Java Themen

Neue Themen


Oben