xamarin.android adding client certificate

前端 未结 2 845
醉梦人生
醉梦人生 2021-01-07 10:12

I\'m trying to send a request to a web api in Xamarin.Android. The api requires a client certificate. I followed the advice in this question: xamarin.ios httpclient clientce

2条回答
  •  青春惊慌失措
    2021-01-07 10:38

    In the post you mentioned probably the managed handler is used. Since this handler currently doesn't support TLS 1.2 you shouldn't use it, but instead really use the AndroidClientHandler (see also Xamarin and TLS 1.2). Unfortunately ClientCertificates is indeed not implemented in AndroidClientHandler.

    If you want to use client certificate with android you can extend the AndroidClientHandler:

    using Java.Security;
    using Java.Security.Cert;
    using Javax.Net.Ssl;
    using Xamarin.Android.Net; 
    using Xamarin.Forms;
    
    public class AndroidHttpsClientHandler : AndroidClientHandler
    {
        private SSLContext sslContext;
    
        public AndroidHttpsClientHandler(byte[] customCA, byte[] keystoreRaw) : base()
        {
            IKeyManager[] keyManagers = null;
            ITrustManager[] trustManagers = null;
    
            // client certificate
            if (keystoreRaw != null)
            {
                using (MemoryStream memoryStream = new MemoryStream(keystoreRaw))
                {
                    KeyStore keyStore = KeyStore.GetInstance("pkcs12");
                    keyStore.Load(memoryStream, clientCertPassword.ToCharArray());
                    KeyManagerFactory kmf = KeyManagerFactory.GetInstance("x509");
                    kmf.Init(keyStore, clientCertPassword.ToCharArray());
                    keyManagers = kmf.GetKeyManagers();
                }
            }
    
            // custom truststore if you have your own ca
            if (customCA != null)
            {
                CertificateFactory certFactory = CertificateFactory.GetInstance("X.509");
    
                using (MemoryStream memoryStream = new MemoryStream(customCA))
                {
                    KeyStore keyStore = KeyStore.GetInstance("pkcs12");
                    keyStore.Load(null, null);
                    keyStore.SetCertificateEntry("MyCA", certFactory.GenerateCertificate(memoryStream));
                    TrustManagerFactory tmf = TrustManagerFactory.GetInstance("x509");
                    tmf.Init(keyStore);
                    trustManagers = tmf.GetTrustManagers();
                }
            }
            sslContext = SSLContext.GetInstance("TLS");
            sslContext.Init(keyManagers, trustManagers, null);
        }
    
        protected override SSLSocketFactory ConfigureCustomSSLSocketFactory(HttpsURLConnection connection)
        {
            SSLSocketFactory socketFactory = sslContext.SocketFactory;
            if (connection != null)
            {
                connection.SSLSocketFactory = socketFactory;
            }
            return socketFactory;
        }
    }
    

提交回复
热议问题