public void RunApplication(string[] args)
        {
            try
            {
                _cmdLine.Parse(args);

                List <ManifestInput> list = new List <ManifestInput>();
                foreach (string filename in _cmdLine.Positional)
                {
                    string[] files = filename.Split(new[] { ';' });
                    foreach (string f in files)
                    {
                        list.Add(ManifestInput.Deserialize(f));
                    }
                }

                if (_cmdLine.Package)
                {
                    if (string.IsNullOrEmpty(_cmdLine.ProductManifest))
                    {
                        throw new ApplicationException("ProductManifest not specified on the command line");
                    }

                    if (string.IsNullOrEmpty(_cmdLine.PackageName))
                    {
                        throw new ApplicationException("PackageName not specified on the command line");
                    }

                    _manifest.PackageManifest = new PackageManifest
                    {
                        Package = new Package()
                    };
                    _manifest.PackageManifest.Package.Manifest = _cmdLine.Manifest;
                    _manifest.PackageManifest.Package.Name     = _cmdLine.PackageName;
                    _manifest.PackageManifest.Package.Product  = new Product
                    {
                        Name = string.Empty
                    };
                    LoadReferencedProduct();
                }
                else
                {
                    _manifest.ProductManifest = new ProductManifest
                    {
                        Product = new Product()
                    };
                    _manifest.ProductManifest.Product.Manifest = _cmdLine.Manifest;
                }

                ProcessFiles(list);

                ProcessConfiguration(list);

                //TODO: get rid of "manifest" constant  Derive from Platform.ManifestDirectory
                string manifestPath = Path.Combine(_cmdLine.DistributionDirectory, "manifest");
                manifestPath = Path.Combine(manifestPath, _cmdLine.Manifest);

                if (File.Exists(manifestPath))
                {
                    File.Delete(manifestPath);
                }

                XmlDocument doc = MacroManifest.Serialize(_manifest);

                // Generate a signing key.
                RSACryptoServiceProvider rsaCsp;
                if (string.IsNullOrEmpty(_cmdLine.Certificate))
                {
                    rsaCsp = new RSACryptoServiceProvider(2048);
                }
                else
                {
                    X509Certificate2 certificate = new X509Certificate2(_cmdLine.Certificate, _cmdLine.Password ?? string.Empty);
                    rsaCsp = (RSACryptoServiceProvider)certificate.PrivateKey;
                }

                ManifestSignature.SignXmlFile(doc, manifestPath, rsaCsp);
                Environment.ExitCode = 0;
            }
            catch (CommandLineException e)
            {
                Platform.Log(LogLevel.Error, e, "Command line exception when generating manifest.");
                Console.WriteLine(e.Message);
                _cmdLine.PrintUsage(Console.Out);
                Environment.ExitCode = -1;
            }
            catch (Exception e)
            {
                Platform.Log(LogLevel.Error, e, "Unexpected exception when generating manifest.");
                string message    = String.Format("Unexpected exception when generating manifest: {0}", e.Message);
                string stackTrace = e.StackTrace;
                Console.WriteLine(message);
                Console.WriteLine(stackTrace);
                Environment.ExitCode = -1;
            }
        }
Пример #2
0
        private void LoadManifestFiles()
        {
            List <string> files = new List <string>();

            FileProcessor.Process(Platform.ManifestDirectory, null,
                                  delegate(string filePath, out bool cancel)
            {
                files.Add(filePath);
                cancel = false;
            }, true);

            List <MacroManifest> manifests = new List <MacroManifest>();

            bool foundManifest = false;

            foreach (string file in files)
            {
                try
                {
                    XmlDocument doc = new XmlDocument();

                    // must open stream manually in case file is read-only
                    using (var fileStream = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        doc.Load(fileStream);
                    }

                    XmlElement modulusNode =
                        (XmlElement)CollectionUtils.FirstElement(doc.GetElementsByTagName("Modulus"));
                    XmlElement exponentNode =
                        (XmlElement)CollectionUtils.FirstElement(doc.GetElementsByTagName("Exponent"));

                    if (!modulusNode.InnerText.Equals("gRuWfDdo9ZCQ9G4hIt6amEgr1DBPDltP0NoBQI/GpooT6hGh6UkFRpaYQMt4ijxbHI5UDGZvAU66Vjt+FUcRVJ4I8AAuhyI4mU2BIzsfpUsKjxJMNdh3fxjaAOjMc4s5pX0CUvCJuZEhY/+PY/aYnjX2a6wes3nGHTx/YJhh42M="))
                    {
                        throw new ApplicationException("Invalid public key in digitial signature.");
                    }
                    if (!exponentNode.InnerText.Equals("AQAB"))
                    {
                        throw new ApplicationException("Invalid public key in digitial signature.");
                    }

                    if (!ManifestSignature.VerifyXmlSignature(doc))
                    {
                        throw new ApplicationException("Manifest digital signature failed verification.");
                    }

                    MacroManifest manifest = MacroManifest.Deserialize(file);

                    if (manifest.ProductManifest != null)
                    {
                        if (string.IsNullOrEmpty(manifest.ProductManifest.Product.Version))
                        {
                            // The Macro.Common.ProductInformation class uses the
                            // Macro.common.dll version if a version is not contained in
                            // the ProductSettings.
                            foreach (ManifestFile manFile in manifest.ProductManifest.Files)
                            {
                                if (manFile.Filename.ToLower().Equals("common\\Macro.common.dll"))
                                {
                                    manifest.ProductManifest.Product.Version = manFile.Version;
                                }
                            }
                        }
                        foundManifest = true;
                        if (!manifest.ProductManifest.Product.Manifest.Equals("Manifest.xml"))
                        {
                            throw new ApplicationException("Product manifest not named Manifest.xml");
                        }

                        _productManifest = manifest;

                        if (!manifest.ProductManifest.Product.Manifest.Equals(Path.GetFileName(file)))
                        {
                            throw new ApplicationException("Manifest name does not match manifest: " +
                                                           Path.GetFileName(file));
                        }
                    }
                    else if (manifest.PackageManifest != null)
                    {
                        if (!manifest.PackageManifest.Package.Manifest.Equals(Path.GetFileName(file)))
                        {
                            throw new ApplicationException("Package Manifest name does not match manifest: " +
                                                           Path.GetFileName(file));
                        }
                    }

                    manifests.Add(manifest);
                }
                catch (Exception e)
                {
                    throw new ApplicationException("Unexpected problem parsing manifest: " + e.Message + " (" + Path.GetFileName(file) + ")");
                }
            }

            if (!foundManifest)
            {
                throw new ApplicationException("Unable to find Manifest.xml");
            }

            _manifests = manifests.ToArray();
        }