Example #1
0
        public void Read(Stream stream)
        {
            var reader = new StreamReader(stream);

            MainSection.Read(reader);

            while (!reader.EndOfStream)
            {
                var section = new ManifestSection();
                section.Read(reader);
                AdditionalSections[section.Value] = section;
            }
        }
Example #2
0
        private byte[] CreateSignatureFile(ZipFile outputJar, Manifest manifest, HashAlgorithmInfo hashAlgorithmInfo)
        {
            try
            {
                Log.Trace("Creating CERT.SF");
                var signatureFile = new Manifest();
                signatureFile.MainSection.Name  = "Signature-Version";
                signatureFile.MainSection.Value = "1.0";
                signatureFile.MainSection.Add(new ManifestEntry(hashAlgorithmInfo.DigestName + "-Manifest-Main-Attributes", manifest.MainSection.Digest));
                signatureFile.MainSection.Add(new ManifestEntry(hashAlgorithmInfo.DigestName + "-Manifest", manifest.Digest));
                signatureFile.MainSection.Add(new ManifestEntry("Created-By", CreatedBy));

                foreach (var additionalSection in manifest.AdditionalSections)
                {
                    Log.Trace($"creating entry for ${additionalSection.Value.Value}");
                    var signatureSection = new ManifestSection
                    {
                        Name  = additionalSection.Value.Name,
                        Value = additionalSection.Value.Value
                    };
                    signatureSection.Add(new ManifestEntry(hashAlgorithmInfo.DigestName, additionalSection.Value.Digest));
                    signatureFile.AdditionalSections[signatureSection.Value] = signatureSection;
                }

                var signatureData = new MemoryStream();
                signatureFile.Write(signatureData, hashAlgorithmInfo);

                var signatureDataRaw = signatureData.ToArray();
                outputJar.Add(new StreamDataSource(new MemoryStream(signatureDataRaw)), SignatureName);

                Log.Trace("CERT.SF created");

                return(signatureDataRaw);
            }
            catch (Exception e)
            {
                Log.Error(e, "CERT.SF creation failed");
                throw;
            }
        }
Example #3
0
        private Manifest CreateSignedManifest(ZipFile inputJar, ZipFile outputJar, HashAlgorithmInfo hashAlgorithmInfo)
        {
            Log.Trace("Creating MANIFEST.MF");
            try
            {
                var newManifest      = new Manifest();
                var existingManifest = inputJar.GetEntry(ManifestName);
                if (existingManifest != null)
                {
                    Log.Trace("Found existing manifest, importing it");
                    using (var s = inputJar.GetInputStream(existingManifest))
                    {
                        newManifest.Read(s);
                    }
                }
                else
                {
                    Log.Trace("No manifest found in jar, create a new one");
                    newManifest.MainSection.Name  = "Manifest-Version";
                    newManifest.MainSection.Value = "1.0";
                    newManifest.MainSection.Add(new ManifestEntry("Created-By", CreatedBy));
                }

                // compute file digests for manifest
                foreach (var entry in inputJar.OfType <ZipEntry>())
                {
                    if (IsSignatureRelated(entry))
                    {
                        Log.Trace($"creating manifest entry for {entry.Name}");

                        ManifestSection section;
                        if (!newManifest.AdditionalSections.TryGetValue(entry.Name, out section))
                        {
                            section = new ManifestSection
                            {
                                Name  = "Name",
                                Value = entry.Name
                            };
                            newManifest.AdditionalSections[section.Value] = section;
                        }
                        else
                        {
                            // remove existing digest
                            section.RemoveAll(e => e.Key.EndsWith("-Digest"));
                        }


                        section.Add(new ManifestEntry(hashAlgorithmInfo.DigestName, HashEntry(inputJar, entry, hashAlgorithmInfo)));
                    }
                }

                var manifestData = new MemoryStream();
                newManifest.Write(manifestData, hashAlgorithmInfo);
                manifestData.Seek(0, SeekOrigin.Begin);
                outputJar.Add(new StreamDataSource(manifestData), ManifestName);

                Log.Trace("manifest created");

                return(newManifest);
            }
            catch (Exception e)
            {
                Log.Error(e, "Manifest creation failed");
                throw;
            }
        }
Example #4
0
 public Manifest()
 {
     MainSection        = new ManifestSection();
     AdditionalSections = new Dictionary <string, ManifestSection>();
 }