コード例 #1
0
        private CertificateBundle InputPfx(string sourcePath)
        {
            CertificateBundle bundle   = null;
            string            password = null;

            while (bundle == null)
            {
                bundle = CertificateBundle.LoadFromPfxFile(sourcePath, password);
                if (bundle == null)
                {
                    PasswordPrompt pp = new PasswordPrompt("Enter the password", "The .pfx file requires a password:");
                    pp.ShowDialog(this);
                    if (pp.OkWasClicked)
                    {
                        password = pp.EnteredPassword;
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
            return(bundle);
        }
コード例 #2
0
        private void MakeCertificate(object Argument)
        {
            try
            {
                MakeCertArgs args = (MakeCertArgs)Argument;

                // Verify that the files do not already exist
                string safeFileName = SafeFileName(args.domains[0]);
                if (args.saveCerAndKey)
                {
                    if (File.Exists(safeFileName + ".cer"))
                    {
                        ShowFileExistsError(safeFileName + ".cer");
                        return;
                    }
                    else if (File.Exists(safeFileName + ".key"))
                    {
                        ShowFileExistsError(safeFileName + ".key");
                        return;
                    }
                }
                else
                {
                    if (File.Exists(safeFileName + ".pfx"))
                    {
                        ShowFileExistsError(safeFileName + ".pfx");
                        return;
                    }
                }

                CertificateBundle certBundle;
                if (args.issuer == "*Self-Signed*")
                {
                    certBundle = CertMaker.GetCertificateSignedBySelf(args.domains, args.keyStrength, args.validFrom, args.validTo);
                }
                else
                {
                    CertificateBundle issuerBundle = null;
                    if (args.issuer.EndsWith(".pfx", StringComparison.OrdinalIgnoreCase))
                    {
                        string password = null;
                        while (issuerBundle == null)
                        {
                            issuerBundle = CertificateBundle.LoadFromPfxFile(args.issuer, password);
                            if (issuerBundle == null)
                            {
                                PasswordPrompt pp = new PasswordPrompt("CA file is protected", "The .pfx file requires a password:"******".key", StringComparison.OrdinalIgnoreCase) ? args.issuer.Remove(args.issuer.Length - 4) + ".cer" : null;
                        issuerBundle = CertificateBundle.LoadFromCerAndKeyFiles(cerFile, args.issuer);
                    }

                    certBundle = CertMaker.GetCertificateSignedByCA(args.domains, args.keyStrength, args.validFrom, args.validTo, issuerBundle);
                }

                if (args.saveCerAndKey)
                {
                    File.WriteAllBytes(safeFileName + ".cer", certBundle.GetPublicCertAsCerFile());
                    File.WriteAllBytes(safeFileName + ".key", certBundle.GetPrivateKeyAsKeyFile());
                }
                else
                {
                    if (args.password == "")
                    {
                        args.password = null;
                    }
                    File.WriteAllBytes(safeFileName + ".pfx", certBundle.GetPfx(true, args.password));
                }
            }
            catch (ThreadAbortException) { }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                StopProgress();
            }
        }