Exemple #1
0
        /// <summary>
        /// Get source agrument if not present
        /// </summary>
        /// <param name="args">Command line argments</param>
        /// <returns>Arguments</returns>
        private static bool GetSourceIfNotPresent(ref string[] args)
        {
            try
            {
                if (CertificateHelper.LoadFromResource(DEFAULT_CERTIFICATE_RESOURCE_NAME) != null)
                {
                    return(true);
                }

                if (args.Length == 1)
                {
                    return(true);
                }
                else if (args.Length < 1)
                {
                    var name = GetDnsNameFromExecutableFileName();

                    if (name != null)
                    {
                        args    = new string[1];
                        args[0] = name;
                        return(true);
                    }

                    var inputHelperForm = new InputHelperForm();
                    if (inputHelperForm.ShowDialog() == DialogResult.OK)
                    {
                        args    = new string[1];
                        args[0] = inputHelperForm.toolStripTextBox1.Text;
                        return(true);
                    }
                }
                else
                {
                    throw new ArgumentOutOfRangeException(nameof(args) + ".Length", args.Length, "> 1");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), nameof(GetSourceIfNotPresent) + " Error ", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return(false);
        }
Exemple #2
0
        /// <summary>
        /// Load certificate from file, http server or resource
        /// </summary>
        /// <param name="args">Command line arguments</param>
        /// <param name="certificate">Certificate</param>
        /// <returns>Load is success?</returns>
        private static bool LoadCertificate(string[] args, out X509Certificate2 certificate)
        {
            certificate = null;

            try
            {
                certificate = CertificateHelper.LoadFromResource(DEFAULT_CERTIFICATE_RESOURCE_NAME);
                if (certificate != null)
                {
                    return(true);
                }


                if (args.Length == 1)
                {
                    string name = args[0];

                    if (File.Exists(name))
                    {
                        certificate = CertificateHelper.LoadFromFile(name);
                        return(true);
                    }
                    else
                    {
                        certificate = CertificateHelper.LoadFromTcpHost(name);
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), nameof(LoadCertificate) + " Error ", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return(false);
        }
Exemple #3
0
        /// <summary>
        /// Install certificate to Root Store, Local Machine
        /// </summary>
        /// <param name="certificate">Certificate</param>
        /// <param name="canonicalName">Canonical name of the certificate or hostname</param>
        /// <returns>Install is success?</returns>
        private static bool InstallCertificate(X509Certificate2 certificate, string[] args, out string canonicalName)
        {
            canonicalName = null;
            try
            {
                canonicalName = certificate.GetNameInfo(X509NameType.SimpleName, false);

                CertificateHelper.Install(certificate);

                // If its wildcard return hostname
                if (canonicalName.StartsWith("*"))
                {
                    canonicalName = args[0];
                }

                return(true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), nameof(InstallCertificate) + " Error ", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return(false);
        }