예제 #1
0
        private Cleaner.ElementMeta CreateSafeElement(iText.StyledXmlParser.Jsoup.Nodes.Element sourceEl)
        {
            String     sourceTag = sourceEl.TagName();
            Attributes destAttrs = new Attributes();

            iText.StyledXmlParser.Jsoup.Nodes.Element dest = new iText.StyledXmlParser.Jsoup.Nodes.Element(iText.StyledXmlParser.Jsoup.Parser.Tag
                                                                                                           .ValueOf(sourceTag), sourceEl.BaseUri(), destAttrs);
            int        numDiscarded = 0;
            Attributes sourceAttrs  = sourceEl.Attributes();

            foreach (iText.StyledXmlParser.Jsoup.Nodes.Attribute sourceAttr in sourceAttrs)
            {
                if (whitelist.IsSafeAttribute(sourceTag, sourceEl, sourceAttr))
                {
                    destAttrs.Put(sourceAttr);
                }
                else
                {
                    numDiscarded++;
                }
            }
            Attributes enforcedAttrs = whitelist.GetEnforcedAttributes(sourceTag);

            destAttrs.AddAll(enforcedAttrs);
            return(new Cleaner.ElementMeta(dest, numDiscarded));
        }
예제 #2
0
        /** Add the SHA1 of every file to the manifest, creating it if necessary. */
        private static Manifest addDigestsToManifest(JarFile jar)
        {
            Manifest   input  = jar.Manifest;
            Manifest   output = new Manifest();
            Attributes main   = output.MainAttributes;

            if (input != null)
            {
                main.AddAll(input.MainAttributes);
            }
            else
            {
                main.Add("Manifest-Version", "1.0");
                main.Add("Created-By", "1.0 (Android SignApk)");
            }

            byte[] buffer = new byte[4096];
            int    num;

            IEnumerable <JarEntry> jes;

            if (input == null)
            {
                jes = jar.OrderBy(j => j.Name);
            }
            else
            {
                var entries       = jar.ToDictionary(j => j.Name);
                var sortedEntries = new List <JarEntry>();
                foreach (var entry in input.Entries)
                {
                    sortedEntries.Add(entries[entry.Key]);
                }
                jes = sortedEntries;
            }

            foreach (JarEntry entry in jes)
            {
                HashAlgorithm md   = HashAlgorithm.Create("SHA1");
                String        name = entry.Name;
                if (!entry.IsDirectory && !name.Equals(JarFile.MANIFEST_NAME) &&
                    !name.Equals(CERT_SF_NAME) && !name.Equals(CERT_RSA_NAME) &&
                    !name.Equals(OTACERT_NAME) &&
                    (stripPattern == null ||
                     !stripPattern.IsMatch(name)))
                {
                    Stream data = jar.GetInputStream(entry);
                    while ((num = data.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        md.TransformBlock(buffer, 0, num, null, 0);
                    }
                    md.TransformFinalBlock(buffer, 0, 0);

                    Attributes attr = null;
                    if (input != null)
                    {
                        attr = input.GetAttributes(name);
                    }
                    attr = attr != null ? new Attributes(attr) : new Attributes();
                    attr.Add("SHA1-Digest", Convert.ToBase64String(md.Hash));
                    output.Entries.Add(name, attr);
                }
            }

            return(output);
        }