public JsonResult GetFilesStructure(string searchPattern, string signature, string challenge)
        {
            try
            {
                var challengeSession = TempData["challenge"].ToString();
                IList <FileIntegrity> FileIntegrityList = new List <FileIntegrity>();

                if (string.Equals(challengeSession, challenge, StringComparison.OrdinalIgnoreCase))
                {
                    bool isSignatureValid = ECKey.ValidECDSASignature(signature, challengeSession, _configurations.ClientPublicKeyECDSA);
                    if (isSignatureValid)
                    {
                        var      settingsSiteFolder  = Path.Combine(_hostingEnvironment.ContentRootPath, "..");
                        var      fileListIEnumerable = GetFilesDirectory(settingsSiteFolder, searchPattern);
                        string[] fileList            = fileListIEnumerable.ToArray();

                        if (fileList?.Length > 0)
                        {
                            for (var i = 0; i < fileList.Length - 1; i++)
                            {
                                FileIntegrity file = new FileIntegrity();
                                file.Filename = fileList[i];
                                file          = HashFile(file);
                                FileIntegrityList.Add(file);
                            }
                        }
                    }
                }

                var json = new JsonResult(FileIntegrityList);
                return(json);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            return(null);
        }
Пример #2
0
        static void Main(string[] args)
        {
            var commander = new CommandLineManager();

            commander.CommandList.Add("test", Test);
            commander.CommandList.Add("r", Read);
            commander.CommandList.Add("read", Read);

            commander.Parse(args);

            if (commander.IsCommandLineEmpty)
            {
                var integrity = new FileIntegrity();
                integrity.Create(FileIntegrity.FileName);
            }

            //var integrity = new FileIntegrity();
            ////integrity.Create(FileIntegrity.FileName);
            //integrity.Read(FileIntegrity.FileName);

            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Form1());
        }
        private static FileIntegrity IsSigned(FileIntegrity file)
        {
            string filePath = file.Filename;

            if (!File.Exists(filePath))
            {
                Console.WriteLine("File not found");
                file.ErrorMessage = "File not found";
                return(file);
            }

            X509Certificate2 theCertificate;

            try
            {
                X509Certificate theSigner = X509Certificate.CreateFromSignedFile(filePath);
                theCertificate = new X509Certificate2(theSigner);
            }
            catch (Exception ex)
            {
                Console.WriteLine("No digital signature found: " + ex.Message);

                file.ErrorMessage = ("No digital signature found: " + ex.Message);
                return(file);
            }

            bool chainIsValid = false;

            /*
             *
             * This section will check that the certificate is from a trusted authority IE
             * not self-signed.
             *
             */

            var theCertificateChain = new X509Chain();

            theCertificateChain.ChainPolicy.RevocationFlag = X509RevocationFlag.ExcludeRoot;

            /*
             *
             * Using .Online here means that the validation WILL CALL OUT TO THE INTERNET
             * to check the revocation status of the certificate. Change to .Offline if you
             * don't want that to happen.
             */

            theCertificateChain.ChainPolicy.RevocationMode = X509RevocationMode.Online;

            theCertificateChain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan(0, 1, 0);

            theCertificateChain.ChainPolicy.VerificationFlags = X509VerificationFlags.NoFlag;

            chainIsValid = theCertificateChain.Build(theCertificate);

            if (chainIsValid)
            {
                file.PublisherInformation = theCertificate.SubjectName.Name;
                file.ValidFrom            = theCertificate.GetEffectiveDateString();
                file.ValidTo  = theCertificate.GetExpirationDateString();
                file.IssuedBy = theCertificate.GetExpirationDateString();

                Console.WriteLine("Publisher Information : " + theCertificate.SubjectName.Name);
                Console.WriteLine("Valid From: " + theCertificate.GetEffectiveDateString());
                Console.WriteLine("Valid To: " + theCertificate.GetExpirationDateString());
                Console.WriteLine("Issued By: " + theCertificate.Issuer);
            }
            else
            {
                Console.WriteLine("Chain Not Valid (certificate is self-signed)");
                file.ErrorMessage = "Chain Not Valid(certificate is self - signed)";
                return(file);
            }
            return(file);
        }