void ImportPkcs12 (byte[] data, string password) { using (var pkcs12 = new MonoBtlsPkcs12 ()) { if (string.IsNullOrEmpty (password)) { try { // Support both unencrypted PKCS#12.. pkcs12.Import (data, null); } catch { // ..and PKCS#12 encrypted with an empty password pkcs12.Import (data, string.Empty); } } else { pkcs12.Import (data, password); } x509 = pkcs12.GetCertificate (0); if (pkcs12.HasPrivateKey) nativePrivateKey = pkcs12.GetPrivateKey (); if (pkcs12.Count > 1) { intermediateCerts = new X509CertificateImplCollection (); for (int i = 0; i < pkcs12.Count; i++) { using (var ic = pkcs12.GetCertificate (i)) { if (MonoBtlsX509.Compare (ic, x509) == 0) continue; var impl = new X509CertificateImplBtls (ic, true); intermediateCerts.Add (impl, true); } } } } }
MX.X509Certificate ImportPkcs12(byte[] rawData, string password) { MX.PKCS12 pfx = null; if (string.IsNullOrEmpty(password)) { try { // Support both unencrypted PKCS#12.. pfx = new MX.PKCS12(rawData, (string)null); } catch { // ..and PKCS#12 encrypted with an empty password pfx = new MX.PKCS12(rawData, string.Empty); } } else { pfx = new MX.PKCS12(rawData, password); } if (pfx.Certificates.Count == 0) { // no certificate was found return(null); } else if (pfx.Keys.Count == 0) { // no key were found - pick the first certificate return(pfx.Certificates [0]); } else { // find the certificate that match the first key MX.X509Certificate cert = null; var keypair = (pfx.Keys [0] as AsymmetricAlgorithm); string pubkey = keypair.ToXmlString(false); foreach (var c in pfx.Certificates) { if (((c.RSA != null) && (pubkey == c.RSA.ToXmlString(false))) || ((c.DSA != null) && (pubkey == c.DSA.ToXmlString(false)))) { cert = c; break; } } if (cert == null) { cert = pfx.Certificates [0]; // no match, pick first certificate without keys } else { cert.RSA = (keypair as RSA); cert.DSA = (keypair as DSA); } if (pfx.Certificates.Count > 1) { intermediateCerts = new X509CertificateImplCollection(); foreach (var c in pfx.Certificates) { if (c == cert) { continue; } var impl = new X509Certificate2ImplMono(c); intermediateCerts.Add(impl, true); } } return(cert); } }
private MX.X509Certificate ImportPkcs12 (byte[] rawData, string password) { MX.PKCS12 pfx = null; if (string.IsNullOrEmpty (password)) { try { // Support both unencrypted PKCS#12.. pfx = new MX.PKCS12 (rawData, (string)null); } catch { // ..and PKCS#12 encrypted with an empty password pfx = new MX.PKCS12 (rawData, string.Empty); } } else { pfx = new MX.PKCS12 (rawData, password); } if (pfx.Certificates.Count == 0) { // no certificate was found return null; } else if (pfx.Keys.Count == 0) { // no key were found - pick the first certificate return pfx.Certificates [0]; } else { // find the certificate that match the first key MX.X509Certificate cert = null; var keypair = (pfx.Keys [0] as AsymmetricAlgorithm); string pubkey = keypair.ToXmlString (false); foreach (var c in pfx.Certificates) { if (((c.RSA != null) && (pubkey == c.RSA.ToXmlString (false))) || ((c.DSA != null) && (pubkey == c.DSA.ToXmlString (false)))) { cert = c; break; } } if (cert == null) { cert = pfx.Certificates [0]; // no match, pick first certificate without keys } else { cert.RSA = (keypair as RSA); cert.DSA = (keypair as DSA); } if (pfx.Certificates.Count > 1) { intermediateCerts = new X509CertificateImplCollection (); foreach (var c in pfx.Certificates) { if (c == cert) continue; var impl = new X509Certificate2ImplMono (c); intermediateCerts.Add (impl, true); } } return cert; } }