Ejemplo n.º 1
0
        public async static Task <X509Certificate2> GetSSLCertificate(StoreName storeName, string subject)
        {
            var x509Certificates = await SSLCertificateServices.GetSSLCertificates(storeName);

            X509Certificate2 x509Certificate = x509Certificates.Where(c => c.HasPrivateKey && c.Subject == subject).OrderByDescending(c => c.NotAfter).FirstOrDefault();

            return(x509Certificate);
        }
Ejemplo n.º 2
0
        public async static Task <bool> TryFindAndBindLatestSSLCertToPort(int portNumber, string subject, StoreName storeName = StoreName.My, Action <string> OnInfoLog = null, Action <string> OnErrorLog = null, bool RemoveAnyPreviousBinding = true, string IP = "0.0.0.0")
        {
            if (string.IsNullOrWhiteSpace(subject))
            {
                OnErrorLog?.Invoke("Unable to bind SSL Cert to Port as no certificate subject was specified.");
                return(false);
            }
            //List<X509Certificate2> x509Certificates = new List<X509Certificate2>();
            var certs = await SSLCertificateServices.GetSSLCertificates(storeName);

            X509Certificate2 x509Certificate = certs.Where(c => c.HasPrivateKey && c.Subject == subject).OrderByDescending(c => c.NotAfter).FirstOrDefault();

            if (x509Certificate == null)
            {
                OnErrorLog?.Invoke("Unable to find SSL Certificate with subject '" + subject + "' in certificte store '" + storeName.ToString() + "'");
                return(false);
            }

            string applicationId = null;
            var    asm           = Assembly.GetEntryAssembly();

            if (asm == null)
            {
                applicationId = Guid.NewGuid().ToString();
            }
            else
            {
                try
                {
                    applicationId = ((GuidAttribute)Assembly.GetEntryAssembly().GetCustomAttributes(typeof(GuidAttribute), true)[0]).Value;
                }
                catch
                { }
            }
            if (applicationId == null)
            {
                applicationId = Guid.NewGuid().ToString();
            }


            if (!SSLCertificateServices.SSLCertBinded(x509Certificate.Thumbprint, portNumber))
            {
                //Remove any Previously Binded SSL Sert at PORT
                if (RemoveAnyPreviousBinding)
                {
                    RemoveSSLCertFromPort(IP, portNumber, (log) => OnInfoLog?.Invoke(log));
                }

                try
                {
                    string BindCommand = "netsh http add sslcert ipport=" + IP + ":" + portNumber + " certhash=" + x509Certificate.Thumbprint + " appid={" + applicationId + "}";
                    OnInfoLog?.Invoke("Binding SSL Certificate '" + subject + "' to " + IP + ":" + portNumber); // + " via Command=" + BindCommand)
                    string BindResultText = ExecuteCommand(BindCommand).RemoveAllNewLines().Trim(' ');
                    OnInfoLog?.Invoke(BindResultText);
                }
                catch (Exception ex)
                {
                    OnErrorLog?.Invoke("Unable to bind generate SSL Certificate to Port " + portNumber + "\r\n" + ex.ToString());
                    return(false);
                }
            }
            return(true);
        }