Пример #1
0
        public async Task ExecuteAsync(string[] args)
        {
            _logger.WriteStart(_dateTimeProvider.Snapshot);

            if (args.Length != 1)
            {
                throw new ArgumentException("The SigTestFileCreator requires one file as a command-line argument.");
            }

            if (Environment.UserInteractive && !WindowsIdentityQueries.CurrentUserIsAdministrator())
            {
                _logger.WriteNoElevatedPrivs();
            }

            var fullPath      = Path.GetFullPath(args[0].Trim());
            var fileDirectory = Path.GetDirectoryName(fullPath);
            var fileName      = Path.GetFileNameWithoutExtension(fullPath);

            var fileContents = File.ReadAllBytes(fullPath);

            var signedFileContents = await BuildEksOutputAsync(fileContents);

            _logger.WriteSavingResultfile();
            using (var outputFile = File.Create($"{fileDirectory}\\{fileName}-signed.zip", 1024, FileOptions.None))
            {
                outputFile.Write(signedFileContents);
            }

            _logger.WriteFinished(fileDirectory);
        }
        public async Task Execute(string[] args)
        {
            _Logger.LogDebug("Key presence Test started ({time})", _DateTimeProvider.Snapshot);

            if (args.Length > 1)
            {
                throw new ArgumentException("The tester was started with more than one argument: ", String.Join(";", args));
            }
            else if (args.Length == 1)
            {
                string CleanedInput             = args[0].Trim();
                string FilePathWithoutExtension = CleanedInput.Substring(0, CleanedInput.LastIndexOf('.'));

                _fileInputLocation     = CleanedInput;
                _eksFileOutputLocation = FilePathWithoutExtension + "-eks" + ".Zip";
            }

            if (Environment.UserInteractive && !WindowsIdentityQueries.CurrentUserIsAdministrator())
            {
                _Logger.LogError("The test was started WITHOUT elevated privileges - errors may occur when signing content.");
            }

            LoadFile(_fileInputLocation);
            var eksZipOutput = await BuildEksOutput();

            _Logger.LogDebug("Saving EKS-engine resultfile.");
            ExportOutput(_eksFileOutputLocation, eksZipOutput);

            _Logger.LogDebug("Key presence test complete.\nResults can be found in: {_fileOutputLocation}", _eksFileOutputLocation);
        }
Пример #3
0
        public async Task ExecuteAsync(string[] args)
        {
            _Logger.WriteStart(_DateTimeProvider.Snapshot);

            if (args.Length > 1)
            {
                throw new ArgumentException("The tester was started with more than one argument: ", String.Join(";", args));
            }
            else if (args.Length == 1)
            {
                var CleanedInput             = args[0].Trim();
                var FilePathWithoutExtension = CleanedInput.Substring(0, CleanedInput.LastIndexOf('.'));

                _fileInputLocation     = CleanedInput;
                _eksFileOutputLocation = FilePathWithoutExtension + "-eks" + ".Zip";
            }

            if (Environment.UserInteractive && !WindowsIdentityQueries.CurrentUserIsAdministrator())
            {
                _Logger.WriteNoElevatedPrivs();
            }

            LoadFile(_fileInputLocation);
            var eksZipOutput = await BuildEksOutputAsync();

            _Logger.WriteSavingResultfile();
            ExportOutput(_eksFileOutputLocation, eksZipOutput);

            _Logger.WriteFinished(_eksFileOutputLocation);
        }
Пример #4
0
        public async Task <EksEngineResult> ExecuteAsync()
        {
            if (_fired)
            {
                throw new InvalidOperationException("One use only.");
            }

            _fired = true;

            var stopwatch = new Stopwatch();

            stopwatch.Start();

            _logger.WriteStart(_jobName);

            if (Environment.UserInteractive && !WindowsIdentityQueries.CurrentUserIsAdministrator())
            {
                _logger.WriteNoElevatedPrivs(_jobName);
            }

            _eksEngineResult.Started = _dateTimeProvider.Snapshot; //Align with the logged job name.

            await ClearJobTablesAsync();

            var snapshotResult = await _snapshotter.ExecuteAsync(_eksEngineResult.Started);

            _eksEngineResult.InputCount                = snapshotResult.TekInputCount;
            _eksEngineResult.FilteredInputCount        = snapshotResult.FilteredTekInputCount;
            _eksEngineResult.SnapshotSeconds           = snapshotResult.SnapshotSeconds;
            _eksEngineResult.TransmissionRiskNoneCount = await GetTransmissionRiskNoneCountAsync();

            if (snapshotResult.TekInputCount != 0)
            {
                await StuffAsync();
                await BuildOutputAsync();
                await CommitResultsAsync();
            }

            _eksEngineResult.TotalSeconds = stopwatch.Elapsed.TotalSeconds;
            _eksEngineResult.EksInfo      = _eksResults.ToArray();

            _logger.WriteReconciliationMatchUsable(_eksEngineResult.ReconcileOutputCount);
            _logger.WriteReconciliationMatchCount(_eksEngineResult.ReconcileEksSumCount);

            _logger.WriteFinished(_jobName);

            return(_eksEngineResult);
        }
        public async Task <EksEngineResult> Execute()
        {
            if (_Fired)
            {
                throw new InvalidOperationException("One use only.");
            }

            _Fired = true;

            var stopwatch = new Stopwatch();

            stopwatch.Start();

            _Logger.LogInformation("Started - JobName:{JobName}", _JobName);

            if (Environment.UserInteractive && !WindowsIdentityQueries.CurrentUserIsAdministrator())
            {
                _Logger.LogWarning("{JobName} started WITHOUT elevated privileges - errors may occur when signing content.", _JobName);
            }

            _EksEngineResult.Started = _DateTimeProvider.Snapshot; //Align with the logged job name.

            await ClearJobTables();

            var snapshotResult = await _Snapshotter.Execute(_EksEngineResult.Started);

            _EksEngineResult.InputCount                = snapshotResult.TekInputCount;
            _EksEngineResult.SnapshotSeconds           = snapshotResult.SnapshotSeconds;
            _EksEngineResult.TransmissionRiskNoneCount = await GetTransmissionRiskNoneCount();

            if (snapshotResult.TekInputCount != 0)
            {
                await Stuff();
                await BuildOutput();
                await CommitResults();
            }

            _EksEngineResult.TotalSeconds = stopwatch.Elapsed.TotalSeconds;
            _EksEngineResult.EksInfo      = _EksResults.ToArray();

            _Logger.LogInformation("Reconciliation - Teks in EKSs matches usable input and stuffing - Delta:{ReconcileOutputCount}", _EksEngineResult.ReconcileOutputCount);
            _Logger.LogInformation("Reconciliation - Teks in EKSs matches output count - Delta:{ReconcileEksSumCount}", _EksEngineResult.ReconcileEksSumCount);

            _Logger.LogInformation("{JobName} complete.", _JobName);

            return(_EksEngineResult);
        }
Пример #6
0
        public byte[] GetSignature(byte[] content)
        {
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }

            var certificate = _CertificateProvider.GetCertificate();

            if (!certificate.HasPrivateKey)
            {
                throw new InvalidOperationException($"Certificate does not have a private key - Subject:{certificate.Subject} Thumbprint:{certificate.Thumbprint}.");
            }

            var contentInfo = new ContentInfo(content);
            var signedCms   = new SignedCms(contentInfo, true);

            var signer = new CmsSigner(SubjectIdentifierType.IssuerAndSerialNumber, certificate)
            {
                IncludeOption = X509IncludeOption.EndCertOnly
            };

            try
            {
                signedCms.ComputeSignature(signer);
            }
            catch (Exception e)
            {
                //NB. Cannot catch the internal exception type (cross-platform design of .NET Core)
                if (e.GetType().Name == "WindowsCryptographicException" && e.Message == "Keyset does not exist" && !WindowsIdentityQueries.CurrentUserIsAdministrator())
                {
                    throw new InvalidOperationException("Failed to sign with certificate when current user does not have UAC elevated permissions.", e);
                }

                throw;
            }

            return(signedCms.Encode());
        }