Beispiel #1
0
        byte[] ExportPkcs12(string password)
        {
            var pfx = new MX.PKCS12();

            try {
                var attrs      = new Hashtable();
                var localKeyId = new ArrayList();
                localKeyId.Add(new byte[] { 1, 0, 0, 0 });
                attrs.Add(MX.PKCS9.localKeyId, localKeyId);

                if (password != null)
                {
                    pfx.Password = password;
                }
                pfx.AddCertificate(_cert, attrs);
                var privateKey = PrivateKey;
                if (privateKey != null)
                {
                    pfx.AddPkcs8ShroudedKeyBag(privateKey, attrs);
                }
                return(pfx.GetBytes());
            } finally {
                pfx.Password = null;
            }
        }
Beispiel #2
0
        byte[] ExportPkcs12(string password)
        {
            var pfx = new MX.PKCS12();

            try {
                var attrs      = new Hashtable();
                var localKeyId = new ArrayList();
                localKeyId.Add(new byte[] { 1, 0, 0, 0 });
                attrs.Add(MX.PKCS9.localKeyId, localKeyId);
                if (password != null)
                {
                    pfx.Password = password;
                }
                pfx.AddCertificate(new MX.X509Certificate(GetRawCertData()), attrs);
                if (IntermediateCertificates != null)
                {
                    for (int i = 0; i < IntermediateCertificates.Count; i++)
                    {
                        pfx.AddCertificate(new MX.X509Certificate(IntermediateCertificates [i].GetRawCertData()));
                    }
                }
                var privateKey = PrivateKey;
                if (privateKey != null)
                {
                    pfx.AddPkcs8ShroudedKeyBag(privateKey, attrs);
                }
                return(pfx.GetBytes());
            } finally {
                pfx.Password = null;
            }
        }
Beispiel #3
0
        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);
                }
                return(cert);
            }
        }
Beispiel #4
0
 private void ImportPkcs12(byte[] rawData, string password)
 {
     MX.PKCS12 pfx = (password == null) ? new MX.PKCS12(rawData) : new MX.PKCS12(rawData, password);
     if (pfx.Certificates.Count > 0)
     {
         _cert = pfx.Certificates [0];
     }
     else
     {
         _cert = null;
     }
     if (pfx.Keys.Count > 0)
     {
         _cert.RSA = (pfx.Keys [0] as RSA);
         _cert.DSA = (pfx.Keys [0] as DSA);
     }
 }
Beispiel #5
0
        byte[] ExportPkcs12(string password)
        {
            var pfx = new MX.PKCS12();

            try {
                if (password != null)
                {
                    pfx.Password = password;
                }
                pfx.AddCertificate(_cert);
                var privateKey = PrivateKey;
                if (privateKey != null)
                {
                    pfx.AddPkcs8ShroudedKeyBag(privateKey);
                }
                return(pfx.GetBytes());
            } finally {
                pfx.Password = null;
            }
        }
		byte[] ExportPkcs12 (string password)
		{
			var pfx = new MX.PKCS12 ();
			try {
				var attrs = new Hashtable ();
				var localKeyId = new ArrayList ();
				localKeyId.Add (new byte[] { 1, 0, 0, 0 });
				attrs.Add (MX.PKCS9.localKeyId, localKeyId);
				if (password != null)
					pfx.Password = password;
				pfx.AddCertificate (new MX.X509Certificate (GetRawCertData ()), attrs);
				if (IntermediateCertificates != null) {
					for (int i = 0; i < IntermediateCertificates.Count; i++)
						pfx.AddCertificate (new MX.X509Certificate (IntermediateCertificates [i].GetRawCertData ()));
				}
				var privateKey = PrivateKey;
				if (privateKey != null)
					pfx.AddPkcs8ShroudedKeyBag (privateKey, attrs);
				return pfx.GetBytes ();
			} finally {
				pfx.Password = null;
			}
		}
		byte[] ExportPkcs12 (string password)
		{
			var pfx = new MX.PKCS12 ();
			try {
				var attrs = new Hashtable ();
				var localKeyId = new ArrayList ();
				localKeyId.Add (new byte[] { 1, 0, 0, 0 });
				attrs.Add (MX.PKCS9.localKeyId, localKeyId);

				if (password != null)
					pfx.Password = password;
				pfx.AddCertificate (_cert, attrs);
				var privateKey = PrivateKey;
				if (privateKey != null)
					pfx.AddPkcs8ShroudedKeyBag (privateKey, attrs);
				return pfx.GetBytes ();
			} finally {
				pfx.Password = null;
			}
		}
		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);
				}
				return cert;
			}
		}
Beispiel #9
0
		byte[] ExportPkcs12 (string password)
		{
			var pfx = new MX.PKCS12 ();
			try {
				if (password != null)
					pfx.Password = password;
				pfx.AddCertificate (_cert);
				var privateKey = PrivateKey;
				if (privateKey != null)
					pfx.AddPkcs8ShroudedKeyBag (privateKey);
				return pfx.GetBytes ();
			} finally {
				pfx.Password = null;
			}
		}