c# get x509 certificate from windows CA by Serial

        public static X509Certificate2 GetCertFromCA(string Thumbprint, string connection)
        {
            // Constants
            const int CV_OUT_BASE64HEADER = 0;
            const int CV_OUT_BINARY = 2;
 
            // Variables
            CERTADMINLib.CCertView certView = null;
            CERTADMINLib.IEnumCERTVIEWROW certViewRow = null;
            CERTADMINLib.IEnumCERTVIEWCOLUMN certViewColumn = null;
 
            try
            {
 
                int iColumnCount = 0;
                object objValue = null;
                StreamWriter objFile = null;
 
                // Connecting to the Certificate Authority
                certView = new CERTADMINLib.CCertView();
                //certView.OpenConnection(strServer + "\\" + strCAName);
                certView.OpenConnection(connection);
 
 
                // Get a column count and place columns into the view
                iColumnCount = certView.GetColumnCount(0);
                certView.SetResultColumnCount(iColumnCount);
 
                // Place each column in the view.
                for (int x = 0; x < iColumnCount; x++)
                {
                    certView.SetResultColumn(x);
                }
 
                // Open the View and reset the row position
                certViewRow = certView.OpenView();
                certViewRow.Reset();
 
                // Enumerate Row and Column Information
                Thumbprint = Thumbprint.Replace(" ", "");
                // Rows (one per cert) 
                for (int x = 0; certViewRow.Next() != -1; x++)
                {
                    // Columns with the info we need
                    certViewColumn = certViewRow.EnumCertViewColumn();
                    bool foundcert = false;
 
                    while (certViewColumn.Next() != -1)
                    {
                        string asdfasd = certViewColumn.GetDisplayName();
                        //Серийный номер
                        //Serial Number
 
                        if (asdfasd == "Serial Number" || asdfasd == "Серийный номер")
                        {
                            objValue = certViewColumn.GetValue(CV_OUT_BINARY);
                            string s = (string)objValue;
                            if (objValue != null)
                            {
 
                                if (Thumbprint.Contains(s))
                                {
                                    foundcert = true;
                                    certViewColumn.Reset();
                                }
                                else
                                    break;
                            }
                        }
                        else if (foundcert)
                            if (asdfasd == "Binary Certificate" || asdfasd == "Двоичный сертификат")
                            {
                                //Binary Certificate
                                //Двоичный сертификат
                                objValue = certViewColumn.GetValue(CV_OUT_BASE64HEADER);
                                if (objValue != null)
                                {
                                    // Write certificate to file
 
                                    objFile = File.CreateText("tempcert.cer");
                                    objFile.Write(objValue.ToString());
                                    objFile.Close();
                                    X509Certificate2 cert = new X509Certificate2("tempcert.cer");
                                    return cert;
                                }
                            }
 
 
                    }
                }
                return null;
            }
            finally
            {
                if (certViewColumn!=null)
                    Marshal.FinalReleaseComObject(certViewColumn);
                if (certViewRow != null)
                    Marshal.FinalReleaseComObject(certViewRow);
                if (certView != null)
                    Marshal.FinalReleaseComObject(certView);
            }
 
        }
 

No comments:

Post a Comment