https mit certifikaten

  • Antworten:12
Ralf Kruppa
  • Forum-Beiträge: 26

20.10.2017, 08:15:15 via Website

Hallo,

aufgrund der letztlich gefundenen Sicherheitslücke im WLAN möchte ich meine Kommunikation von HTTP auf HTTPS mit Zertifikaten umstellen.
Den Server habe ich erfolgreich umgestellt und kann nun nach der installation der Zertifikate alles über https erreichen.
Nun muss ich meine APP auch umstellen und da habe ich so meine Probleme.

Ich habe folgendes zum testen umgesetzt in HTTP:

public class MainActivity extends AppCompatActivity {
private static String LOG_TAG = "MainActivity";
TextView TxtResult;
Button PostBtn, GetBtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    GetBtn = (Button) findViewById(R.id.get_btn);
    PostBtn = (Button) findViewById(R.id.post_btn);

    GetBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new MakeNetworkCall().execute("htp://xxxxxx.eu/" +
                    "mobile.php?get=1", "Get");
        }

    });

    PostBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new MakeNetworkCall().execute("htp://xxxxxx.eu/" +
                    "tutorial/http.php?post=1", "Post");
        }
    });


}

InputStream ByGetMethod(String ServerURL) {

    InputStream DataInputStream = null;
    try {

        URL url = new URL(ServerURL);
        HttpURLConnection cc = (HttpURLConnection)
                url.openConnection();
        //set timeout for reading InputStream
        cc.setReadTimeout(5000);
        // set timeout for connection
        cc.setConnectTimeout(5000);
        //set HTTP method to GET
        cc.setRequestMethod("GET");
        //set it to true as we are connecting for input
        cc.setDoInput(true);

        //reading HTTP response code
        int response = cc.getResponseCode();

        //if response code is 200 / OK then read Inputstream
        if (response == HttpURLConnection.HTTP_OK) {
            DataInputStream = cc.getInputStream();
        }

    } catch (Exception e) {
        Log.e(LOG_TAG, "Error in GetData", e);
    }
    return DataInputStream;

}

InputStream ByPostMethod(String ServerURL) {

    InputStream DataInputStream = null;
    try {

        //Post parameters
        String PostParam = "first_name=android&last_name=pala";

        //Preparing
        URL url = new URL(ServerURL);

        HttpURLConnection cc = (HttpURLConnection)
                url.openConnection();
        //set timeout for reading InputStream
        cc.setReadTimeout(5000);
        // set timeout for connection
        cc.setConnectTimeout(5000);
        //set HTTP method to POST
        cc.setRequestMethod("POST");
        //set it to true as we are connecting for input
        cc.setDoInput(true);
        //opens the communication link
        cc.connect();

        //Writing data (bytes) to the data output stream
        DataOutputStream dos = new DataOutputStream(cc.getOutputStream());
        dos.writeBytes(PostParam);
        //flushes data output stream.
        dos.flush();
        dos.close();

        //Getting HTTP response code
        int response = cc.getResponseCode();

        //if response code is 200 / OK then read Inputstream
        //HttpURLConnection.HTTP_OK is equal to 200
        if(response == HttpURLConnection.HTTP_OK) {
            DataInputStream = cc.getInputStream();
        }

    } catch (Exception e) {
        Log.e(LOG_TAG, "Error in GetData", e);
    }
    return DataInputStream;

}

String ConvertStreamToString(InputStream stream) {

    InputStreamReader isr = new InputStreamReader(stream);
    BufferedReader reader = new BufferedReader(isr);
    StringBuilder response = new StringBuilder();

    String line = null;
    try {

        while ((line = reader.readLine()) != null) {
            response.append(line);
        }

    } catch (IOException e) {
        Log.e(LOG_TAG, "Error in ConvertStreamToString", e);
    } catch (Exception e) {
        Log.e(LOG_TAG, "Error in ConvertStreamToString", e);
    } finally {

        try {
            stream.close();

        } catch (IOException e) {
            Log.e(LOG_TAG, "Error in ConvertStreamToString", e);

        } catch (Exception e) {
            Log.e(LOG_TAG, "Error in ConvertStreamToString", e);
        }
    }
    return response.toString();


}

public void DisplayMessage(String a) {

    TxtResult = (TextView) findViewById(R.id.response);
    TxtResult.setText(a);
}

private class MakeNetworkCall extends AsyncTask<String, Void, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        DisplayMessage("Please Wait ...");
    }

    @Override
    protected String doInBackground(String... arg) {

        InputStream is = null;
        String URL = arg[0];
        Log.d(LOG_TAG, "URL: " + URL);
        String res = "";


        if (arg[1].equals("Post")) {

            is = ByPostMethod(URL);

        } else {

            is = ByGetMethod(URL);
        }
        if (is != null) {
            res = ConvertStreamToString(is);
        } else {
            res = "Something went wrong";
        }
        return res;
    }

    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        DisplayMessage(result);
        Log.d(LOG_TAG, "Result: " + result);
    }
}

}

Wie kann ich dieses nun so ändern das es mit HTTPS und Zertifikaten läuft ?

Danke für die unterstützung.

Gruß Ralf

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

20.10.2017, 11:40:37 via App

Wie wäre es denn die Url einfach auf https abzuändern. Den Rest sollte Android selber handeln zumindest wenn dein Serverzertifikat von einer offiziellen Zertifizierungsstelle ist.

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Ralf Kruppa
  • Forum-Beiträge: 26

20.10.2017, 12:32:40 via Website

Hallo, das habe ioch versucht leider ohne Erfolg.
Das Zertifikat ist selber ertstellt. Ich habe eine eigene PKI erstellt mit Root Zertifikat und entsprechende Client-Zertifikatenlle .
Ist das so ein großer unterschied ?
Muss ich um das hinzubekommen auch das Root Zertifikat als Authentifiezierungsquelle mit installieren ?

Gruß Ralf

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

20.10.2017, 12:48:05 via App

Je nachdem für welche Benutzer deine App ist. Nur für dich selbst: Das Zertifikat auf den Gerät installieren, das sollte reichen.

Ist die App im Store solltest du in deiner HttpUrlConnection nicht vertrauenswürdige Zertifikate zulassen, dann geht das auch. Ansonsten musst du dein Zertifikat von offizieller Stelle holen.

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Ralf Kruppa
  • Forum-Beiträge: 26

20.10.2017, 17:50:26 via Website

Die APP ist nicht öffentlich sondern nur für mich selbst.

Wie mache ich denn nicht vertrauenswürdige Zertifikate zulässig bei httpurlconnection ?

Antworten
Ralf Kruppa
  • Forum-Beiträge: 26

20.10.2017, 19:07:25 via Website

Hallo, ok ich habe es mit eingebaut aber an welcher Stelle füge ich nun die Funktion "trustAllCertificates()" ein und wie ?

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

20.10.2017, 20:34:18 via App

Am Anfang in der OnCreate einmal aufrufen sollte reichen.

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Ralf Kruppa
  • Forum-Beiträge: 26

20.10.2017, 20:48:03 via Website

Hallo, nein das klappt leider auch nicht. Ich denke das das Personen Zertifikat irgendwo angegeben werden muß. Das akzeptieren der Zertifikatsquelle alleine reicht scheinbar nicht aus.

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

20.10.2017, 20:49:51 via App

Sollte es aber. Was sagt denn LogCat dazu?

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Ralf Kruppa
  • Forum-Beiträge: 26

20.10.2017, 21:03:30 via Website

LogCat ?
Wie kann ich das Log erstellen oder einsehen ?
Sorry hatte noch nicht davon gehört.

Antworten
Ralf Kruppa
  • Forum-Beiträge: 26

20.10.2017, 22:46:15 via Website

Im CatLog steht folgendes:
Suppressed: javax.net.ssl.SSLHandshakeException: Handshake failed
... 22 more
Suppressed: javax.net.ssl.SSLHandshakeException: Handshake failed
... 22 more
Suppressed: javax.net.ssl.SSLHandshakeException: Handshake failed
... 22 more
Caused by: javax.net.ssl.SSLProtocolException: SSL handshake terminated: ssl=0x9f33cc00: Failure in SSL library, usually a protocol error
error:100c5410:SSL routines:ssl3_read_bytes:SSLV3_ALERT_HANDSHAKE_FAILURE (external/boringssl/src/ssl/s3_pkt.c:972 0xa95bc680:0x00000001)
at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:353)
... 21 more
Caused by: javax.net.ssl.SSLProtocolException: SSL handshake terminated: ssl=0x9f33cc00: Failure in SSL library, usually a protocol error
error:100c543e:SSL routines:ssl3_read_bytes:TLSV1_ALERT_INAPPROPRIATE_FALLBACK (external/boringssl/src/ssl/s3_pkt.c:972 0xa95bc680:0x00000001)
at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:353)
... 21 more
Caused by: javax.net.ssl.SSLProtocolException: SSL handshake terminated: ssl=0x9f33cc00: Failure in SSL library, usually a protocol error
error:100c543e:SSL routines:ssl3_read_bytes:TLSV1_ALERT_INAPPROPRIATE_FALLBACK (external/boringssl/src/ssl/s3_pkt.c:972 0xa95bc680:0x00000001)
at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:353)
... 21 more
Caused by: javax.net.ssl.SSLProtocolException: SSL handshake terminated: ssl=0x9f33cc00: Failure in SSL library, usually a protocol error
error:100c5410:SSL routines:ssl3_read_bytes:SSLV3_ALERT_HANDSHAKE_FAILURE (external/boringssl/src/ssl/s3_pkt.c:972 0xa95bc800:0x00000001)
error:100c009f:SSL routines:ssl3_get_server_hello:HANDSHAKE_FAILURE_ON_CLIENT_HELLO (external/boringssl/src/ssl/s3_clnt.c:771 0xa93747f7:0x00000000)
at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Nati
10-20 22:44:44.237 4511-4511/? E/Zygote: v2

Antworten
Ralf Kruppa
  • Forum-Beiträge: 26

23.10.2017, 12:34:31 via Website

Hat jemand eine lösung für die Fehlermeldung ?

Trust anchor for certification path not found.

Antworten