Exemple #1
0
        /* TLS hanshake callback */

        /*  Extract a public key from certificate and append to a header specific to the key type
         *  Returns nil if the key type in the certificate can not be recognized/extracted
         */
        byte[] PublicKeyWithHeader(X509Certificate2 cert)
        {
            /* We need to use java native code to allow ECC public key extraction which is
             * not implemented in C#/Xamarin
             */
            byte[] rawData = cert.GetRawCertData();
            Java.Security.Cert.CertificateFactory certFactory = Java.Security.Cert.CertificateFactory.GetInstance("X.509");
            System.IO.Stream inputBytes = new System.IO.MemoryStream(rawData);
            try
            {
                var javaCert = certFactory.GenerateCertificate(inputBytes);
                Java.Security.IPublicKey publicKey = javaCert.PublicKey;

                byte[] encoded = publicKey.GetEncoded();
                if (encoded == null)
                {
                    // Return null and let caller throw an exception with hostname
                    return(null);
                }
                return(encoded);
            }
            catch (Java.Security.Cert.CertificateException e)
            {
                Console.WriteLine(TAG + "Unable to generate certificate: " + e.Message);
                return(null);
            }
        }
 static Java.Security.Cert.X509Certificate ConvertCertificate(Java.Security.Cert.CertificateFactory factory, byte[] certificateData)
 {
     return(factory.GenerateCertificate(new System.IO.MemoryStream(certificateData))
            .JavaCast <Java.Security.Cert.X509Certificate>() !);
 }