Windows Benutzer

Heute habe ich im Fitness (ja, ich gehöre auch zu denen) das Gespräch zweier Frauen mitbekommen. Sie standen vor einem Fitness Gerät und die eine erzählte ganz entzückt: Oh, jetzt habe ich Windows 7 auf meinem Computer. Karl hat’s installiert. Er ist ja so geschickt. Weisst du jetzt habe ich dieses neue Hintergrundbild. Es besitzt eine avantgardistische Quadratur mit einer feinen Zeichnung eines Grashalmes im Vordergrund. Karl konnte das einfach aus vielen mit-installierten Bildern auswählen. Ihre interessiert zuhörende Freundin meinte daraufhin: Ja, ich habe auch das neue Windows 7 installiert bekommen von meinem Mann. Er hat mir auch gezeigt wie ich das Windows-Aussehen verändern kann. Er weiss so viel über Computer. Ich bin froh dass er mir immer hilft. Mein neues Hintergrundbild ist eine klassische Hausfassade, griechischer Baustil, fein gezeichnet mit einem traumhaft sphärischen Einfluss der Umgebung. Frei mitgeliefert von Microsoft.

Liebe Leser, die weiteren 10 Minuten dieser hochstehenden Diskussion über Farben, Kunst der geometrischen Formen und sensationellen neuen Betriebsystemen mit neuer, nie da gewesener Technologie – möchten sie nicht unbedingt hören.

Android: Trusting SSL certificates

Two weeks ago I got the task to establish TLS secured connections via certificates to a service endpoint.
I thought it’s not a big deal, because the endpoint already uses an EV certificate from a trusted CA (SwissSign) in Switzerland. Therefore I shouldn’t have to worry that the certificate would be considered as untrusted so I don’t have to import it to the trusted certs in the  Java  key store etc.
FAIL! I’ve got a security exception, cert is not trusted. Same problem when I visit the website with the browser. Ok, that’s bad, SwissSign is not such a big player like thawte, so, it needs some time till it will be added to the android trusted CA list. But, when I visit thawte.com, their cert is also not trusted by android. WTF?
Windows Phone and iPhone trust my SwissSign CA and don’t complain.

So, let’s ask google, stackoverflow and the blogosphere. Found a lot of solutions how to disable certificate checking entirely.
Yeah, great, this will solve my problem, my connection will be “secure” and everyone will be able to intercept my connection and inject his own certificate. But I finally found the solution with help from other sites and some testing and debugging.

The Solution

The following main steps are required to achieve a secured connection from trusted Certification Authorities.

  1. Grab all required certificates (root and any intermediate CA’s)
  2. Create a keystore with keytool and the BouncyCastle provider and import the certs
  3. Load the keystore in your android app and use it for the secured connections
    • Don’t use the standard java.net.ssl.HttpsURLConnection for the secure connection. Use the Apache HttpClient (Version 4 atm) library, which is already built-in in android. It’s built on top of the java connection libraries and is, in my opinion, faster, better modularized and easier to understand.

Step 1: Grab the certs

You have to obtain all certificates that build a chain from the endpoint certificate the whole way up to the Root CA. This means, any (if present) Intermediate CA certs and also the Root CA cert. You don’t need to obtain the endpoint certificate.
You can obtain those certs from the chain (if provided) included in the endpoint certificate or from the official site of the issuer (in my case SwissSign).

Ensure that you save the obtained certificates in the Base64 encoded X.509 format. The content should look similar to this:

[code lang="plain"]
-----BEGIN CERTIFICATE-----
MIIGqTC.....
-----END CERTIFICATE-----
[/code]

Step 2: Create the keystore

Download the BouncyCastle Provider and store it to a known location.
Also ensure that you can invoke the keytool command (usually located under the bin folder of your JRE installation).

Now import the obtained certs (don’t import the endpoint cert) into a BouncyCastle formatted keystore.
I didn’t tested it, but I think the order of importing the certificates is important. This means, import the lowermost Intermediate CA certificate first and then all the way up to the Root CA certificate.

With the following command a new keystore (if not already present) with the password mysecret will be created and the Intermediate CA certificate will be imported. I also defined the BouncyCastle provider, where it can be found on my file system and the keystore format. Execute this command for each certificate in the chain.

[code lang="plain"]
keytool -importcert -v -trustcacerts -file "path_to_cert/interm_ca.cer" -alias IntermediateCA -keystore "res/raw/myKeystore.bks" -provider org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath "path_to_bouncycastle/bcprov-jdk16-145.jar" -storetype BKS -storepass mysecret
[/code]

Verify if the certificates were imported correctly into the keystore:

[code lang="plain"]
keytool -list -keystore "res/raw/myKeystore.bks" -provider org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath "path_to_bouncycastle/bcprov-jdk16-145.jar" -storetype BKS -storepass mysecret
[/code]

Should output the whole chain:

[code lang="plain"]
RootCA, 22.10.2010, trustedCertEntry, Thumbprint (MD5): 24:77:D9:A8:91:D1:3B:FA:88:2D:C2:FF:F8:CD:33:93
IntermediateCA, 22.10.2010, trustedCertEntry, Thumbprint (MD5): 98:0F:C3:F8:39:F7:D8:05:07:02:0D:E3:14:5B:29:43
[/code]

Now you can copy the keystore as a raw resource in your android app under res/raw/

Step 3: Use the keystore in your app

First of all we have to create a custom Apache HttpClient that uses our keystore for HTTPS connections:

[java]
public class MyHttpClient extends DefaultHttpClient {

final Context context;

public MyHttpClient(Context context) {
this.context = context;
}

@Override
protected ClientConnectionManager createClientConnectionManager() {
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
// Register for port 443 our SSLSocketFactory with our keystore
// to the ConnectionManager
registry.register(new Scheme("https", newSslSocketFactory(), 443));
return new SingleClientConnManager(getParams(), registry);
}

private SSLSocketFactory newSslSocketFactory() {
try {
// Get an instance of the Bouncy Castle KeyStore format
KeyStore trusted = KeyStore.getInstance("BKS");
// Get the raw resource, which contains the keystore with
// your trusted certificates (root and any intermediate certs)
InputStream in = context.getResources().openRawResource(R.raw.mykeystore);
try {
// Initialize the keystore with the provided trusted certificates
// Also provide the password of the keystore
trusted.load(in, "mysecret".toCharArray());
} finally {
in.close();
}
// Pass the keystore to the SSLSocketFactory. The factory is responsible
// for the verification of the server certificate.
SSLSocketFactory sf = new SSLSocketFactory(trusted);
// Hostname verification from certificate
// http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e506
sf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
return sf;
} catch (Exception e) {
throw new AssertionError(e);
}
}
}
[/java]

We have created our custom HttpClient, now we can just use it for secure connections. For example when we make a GET call to a REST resource.

[java]
// Instantiate the custom HttpClient
DefaultHttpClient client = new MyHttpClient(getApplicationContext());
HttpGet get = new HttpGet("https://www.mydomain.ch/rest/contacts/23");
// Execute the GET call and obtain the response
HttpResponse getResponse = client.execute(get);
HttpEntity responseEntity = getResponse.getEntity();
[/java]

That’s it. Took me long to figure it out, hope this helps and saves you that time.

I really hope that the android platform will implement a better mechanism in future releases for defining which Certification Authorities should be trusted or not or just expand their own trusted CA list. If they don’t, I can’t believe they will get good acceptance from the business sector. Ok, you can control which certificates you want to trust in your app, but you still can’t add thawte as a trusted CA in the android keystore and your browser will always complain about an untrusted CA. The only way I know to eliminate this problem is to root your phone (very user friendly) and add your CA manually to the android keystore.

Feel free to comment.

No Facebook @HSLU?

Wir alle kennen das Problem. Im F-Stock vom Gebäude 3 gibt es massive WLAN Probleme. IT-Services hat nun begonnen systematisch Messungen zu machen und dem Problem auf den Grund zu gehen. Es hat sich gezeigt, dass die vielen Studierenden (im PREN Modul können es bis 250 sein) und das on-line Verhalten dieser zu den schlechten Antwortzeiten im Wireless Netz auf dem F-Stock führen (gleiches gilt für andere, hoch frequentierte Räume). IT-Services wird nun einen Massnahmenkatalog ausarbeiten. An erster Stelle werden technische Verbesserungen stehen. Was passiert aber wenn diese Massnahmen nicht die gewünschte Verbesserung erzielen?

Detaillierte Messungen haben ein erbärmliches Verhalten einiger wenigen Studierenden ans Tageslicht gefördert. Es sind jene Studierenden die in einer Gruppe arbeiten und die anderen für sich arbeiten lassen. In der Zwischenzeit sind sie nämlich am on-line Gamen und nehmen an Tauschbörsen für Film und Musik teil. Die Messungen zeigen selbstverständlich die Namen und MAC Adressen der Übeltäter. Aus Datenschutzgründen dürfen diese nicht publiziert werden. Ich persönlich wurde die Namen an dieser Stelle veröffentlichen. Die Schelte und Busse würde ich gerne bezahlen. Es würde immerhin dazu führen, dass diese coolen Typen von ihren Kommilitonen und Kommilitoninnen verantwortlich gemacht werden für ein Szenario das wir so nie gewünscht haben: Das Netz wird geschlossen für alles was nichts mit dem Unterricht zu tun hat! “Super Arbeit” sage ich jenem oder jener Studierenden der es fertigbringt, während dem Unterricht, innerhalb von 2,5 Stunden 5 GB Datenverkehr zu generieren. Ich wünsche mir, dass alle jene die in den Unterricht kommen um an einem on-line Spiel teilzunehmen, von ihren Mitstudierenden mit Verachtung bestraft werden. Diese sind dafür verantwortlich wenn wir ins Kommunikations-Steinzeitalter zurückfallen.

Also liebe HSLU Studenten und Studentinnen, sorgt dafür dass die schwarzen Schafe uncool sind. So bleibt unser Netz wie es sein sollte: “unzensiert und frei”

2010-10-13 | Posted in: /etc | Comments Closed