:) I got the same impression from the second URL. From what you say I gather
the P/Invoke is basically a no-go, so I think I will have to say goodbye to
HttpWebRequest.

I've tried to use HttpsUrlConnection
(http://androidapi.xamarin.com/?link=T%3aJavax.Net.Ssl.HttpsURLConnection)
instead, but unfortunately, I am getting very similar results.

The code:

    X509Certificate[] certificateChain = KeyChain.GetCertificateChain(this,
alias);
    IPrivateKey privateKey = KeyChain.GetPrivateKey(this, alias);
    
    KeyStore clientCertificateKeyStore = KeyStore.GetInstance("PKCS12");
    clientCertificateKeyStore.Load(null, null);
    clientCertificateKeyStore.SetKeyEntry(alias, privateKey, null,
certificateChain);
    KeyManagerFactory clientKeysFactory =
KeyManagerFactory.GetInstance("X509");
    clientKeysFactory.Init(clientCertificateKeyStore, null);
    
    // this paragraph is probably irrelevant, just including it here for
completeness
    KeyStore serverKeysStore = KeyStore.GetInstance("AndroidCAStore");
    serverKeysStore.Load(null, null);
    TrustManagerFactory serverTrustManagerFactory =
TrustManagerFactory.GetInstance(TrustManagerFactory.DefaultAlgorithm);
    serverTrustManagerFactory.Init(serverKeysStore);
    
    SSLContext context = SSLContext.GetInstance("TLS");
    context.Init(clientKeysFactory.GetKeyManagers(),
serverTrustManagerFactory.GetTrustManagers(), new SecureRandom());

    URL url = new URL("url");
    HttpsURLConnection urlConnection =
(HttpsURLConnection)url.OpenConnection();
    urlConnection.SSLSocketFactory = context.SocketFactory;
    string contents = new
StreamReader(urlConnection.InputStream).ReadToEnd();

The last line throws the following mysterious exception:

    {System.NullReferenceException: Object reference not set to an instance
of an object
      at Android.Runtime.JNIEnv.CallObjectMethod (IntPtr jobject, IntPtr
jmethod) [0x00000] in    
/Users/builder/data/lanes/monodroid-mac-monodroid-4.4-   
series/6418373f/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:129 
      at Java.Net.URLConnection.get_InputStream () [0x0003e] in
/Users/builder/data/lanes/monodroid-mac-   
monodroid-4.4-series/6418373f/source/monodroid/src/Mono.Android/platforms/android-
   
14/src/generated/Java.Net.URLConnection.cs:689 
      at AndroidApplication8.Activity1.AndroidWayConnection (System.String
alias) [0x000c0] in     C:\Users\anusevn\Documents\Visual Studio
2010\Projects\AndroidApplication8\Activity1.cs:116 }

Then I've noticed the ResponseMessage property of HttpsUrlConnection
contains something more helpful:

    Javax.Net.Ssl.SSLException: Error checking private key: ssl=0x6703a610: 
    error:140A30BE:SSL routines:SSL_check_private_key:no private key
assigned     (external/openssl/ssl/ssl_lib.c:926 0x645ab7de:0x00000000)

Exactly the same code works on Android 4.0. Seems that HttpsUrlConnection
and related factories rely on the ability to get the private key, just as
the HttpWebRequest does, or maybe there is some other underlying issue. This
seems like a Mono for Android bug to me, because it is certainly possible to
use HttpsUrlConnection with native Android code.

Is there *any* way at all to connect to client certificate authenticated
HTTPS site with Mono for Android?



--
View this message in context: 
http://mono-for-android.1047100.n5.nabble.com/KeyChain-API-on-Android-4-1-and-client-certificate-authentication-tp5712844p5712849.html
Sent from the Mono for Android mailing list archive at Nabble.com.
_______________________________________________
Monodroid mailing list
Monodroid@lists.ximian.com

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid

Reply via email to