Exemplo n.º 1
1
        private void btn_savepfx_Click(object sender, RoutedEventArgs e)
        {
            string passwd = txt_Pfxpasswd.Password;
            string caserver = txt_CAServer.Text;
            string dir = Directory.GetParent(Assembly.GetExecutingAssembly().Location).ToString();

            if (Certs.Count == 0)
            {
                MessageBox.Show("No Request(s) To Save");
                return;
            }

            foreach (Certificates c in Certs)
            {
                if (c.Status != "File Created!" && c.Status == "certificate issued")
                {

                CX509Enrollment objEnroll = new CX509EnrollmentClass();
                var objCertRequest = new CCertRequest();

                var iDisposition = objCertRequest.RetrievePending(Convert.ToInt32(c.ID), caserver);

                if (Convert.ToInt32(iDisposition) == 3)
                {
                    var cert = objCertRequest.GetCertificate(CR_OUT_BASE64 | CR_OUT_CHAIN);

                    objEnroll.Initialize(X509CertificateEnrollmentContext.ContextUser);
                    objEnroll.InstallResponse(
                        InstallResponseRestrictionFlags.AllowUntrustedRoot,
                        cert,
                        EncodingType.XCN_CRYPT_STRING_BASE64,
                        null
                    );

                    c.Status = "File Created!";

                    var fil = objEnroll.CreatePFX(passwd, PFXExportOptions.PFXExportChainWithRoot, EncodingType.XCN_CRYPT_STRING_BASE64);
                    System.IO.File.WriteAllText(dir + @"\" + c.FQDN + ".pfx", fil);
                }

            }

            }
        }
Exemplo n.º 2
0
        public X509Certificate2 ImportResponse(string pResponse)
        {
            CX509Enrollment objEnroll = new CX509EnrollmentClass();

            try
            {
                var strCert = pResponse;

                // Install the certificate
                objEnroll.Initialize(X509CertificateEnrollmentContext.ContextUser);
                objEnroll.InstallResponse(
                    InstallResponseRestrictionFlags.AllowUntrustedRoot,
                    strCert,
                    EncodingType.XCN_CRYPT_STRING_BASE64HEADER,
                    null
                    );
                var x509Cert = new X509Certificate2(Encoding.ASCII.GetBytes(pResponse));
                return(x509Cert);
            }
            catch (Exception ex)
            {
                EventLogHelper.LogEvent(ex.Message + "\n\r" + ex.StackTrace);
                return(null);
            }
        }
 /// <summary>
 /// The install certificate response.
 /// </summary>
 /// <param name="response">
 /// The response.
 /// </param>
 /// <returns>
 /// The <see cref="bool"/>.
 /// </returns>
 public bool InstallCertificateResponse(string response)
 {
     this.LastError.Clear();
     try
     {
         var objEnroll = new CX509EnrollmentClass();
         objEnroll.Initialize(X509CertificateEnrollmentContext.ContextUser);
         objEnroll.InstallResponse(InstallResponseRestrictionFlags.AllowUntrustedRoot, response, EncodingType.XCN_CRYPT_STRING_BASE64, null);
         return(true);
     }
     catch (Exception ex)
     {
         this.LastError.Add(ex.Message);
         return(false);
     }
 }
Exemplo n.º 4
0
        public SSLCertificate InstallCertificate(SSLCertificate cert, WebSite website)
        {
            CX509Enrollment response = new CX509EnrollmentClass();

            try
            {
                response.Initialize(X509CertificateEnrollmentContext.ContextMachine);
                response.InstallResponse(
                    InstallResponseRestrictionFlags.AllowUntrustedRoot,
                    cert.Certificate, EncodingType.XCN_CRYPT_STRING_BASE64HEADER,
                    null
                    );

                SSLCertificate servercert = (from c in GetServerCertificates()
                                             where c.FriendlyName == cert.FriendlyName
                                             select c).Single();

                cert.SerialNumber      = servercert.SerialNumber;
                cert.ValidFrom         = servercert.ValidFrom;
                cert.ExpiryDate        = servercert.ExpiryDate;
                cert.Hash              = servercert.Hash;
                cert.DistinguishedName = servercert.DistinguishedName;

                if (cert.IsRenewal && CheckCertificate(website))
                {
                    DeleteCertificate(GetCurrentSiteCertificate(website), website);
                }

                AddBinding(cert, website);
            }
            catch (Exception ex)
            {
                Log.WriteError("Error adding SSL certificate", ex);
                cert.Success = false;
            }
            return(cert);
        }
Exemplo n.º 5
0
		public SSLCertificate InstallCertificate(SSLCertificate cert, WebSite website)
		{
			CX509Enrollment response = new CX509EnrollmentClass();
			try
			{

				response.Initialize(X509CertificateEnrollmentContext.ContextMachine);
				response.InstallResponse(
					InstallResponseRestrictionFlags.AllowUntrustedRoot,
					cert.Certificate, EncodingType.XCN_CRYPT_STRING_BASE64HEADER,
					null
				);

				SSLCertificate servercert = (from c in GetServerCertificates()
											 where c.FriendlyName == cert.FriendlyName
											 select c).Single();

				cert.SerialNumber = servercert.SerialNumber;
				cert.ValidFrom = servercert.ValidFrom;
				cert.ExpiryDate = servercert.ExpiryDate;
				cert.Hash = servercert.Hash;
				cert.DistinguishedName = servercert.DistinguishedName;

				if (cert.IsRenewal && CheckCertificate(website))
				{
					DeleteCertificate(GetCurrentSiteCertificate(website), website);
				}

				AddBinding(cert, website);

			}
			catch (Exception ex)
			{


				Log.WriteError("Error adding SSL certificate", ex);
				cert.Success = false;
			}
			return cert;
		}