Ejemplo n.º 1
0
        /// <summary>
        /// called when we reach the end of entry in one of the read() methods.
        /// </summary>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private void processEntry(sun.security.util.ManifestEntryVerifier mev) throws IOException
        private void ProcessEntry(ManifestEntryVerifier mev)
        {
            if (!ParsingBlockOrSF)
            {
                JarEntry je = mev.Entry;
                if ((je != null) && (je.Signers == null))
                {
                    je.Signers = mev.verify(VerifiedSigners, SigFileSigners);
                    je.Certs   = MapSignersToCertArray(je.Signers);
                }
            }
            else
            {
                try
                {
                    ParsingBlockOrSF = false;

                    if (Debug != null)
                    {
                        Debug.println("processEntry: processing block");
                    }

                    String uname = mev.Entry.Name.ToUpperCase(Locale.ENGLISH);

                    if (uname.EndsWith(".SF"))
                    {
                        String  key   = uname.Substring(0, uname.Length() - 3);
                        sbyte[] bytes = Baos.ToByteArray();
                        // add to sigFileData in case future blocks need it
                        SigFileData.Put(key, bytes);
                        // check pending blocks, we can now process
                        // anyone waiting for this .SF file
                        Iterator <SignatureFileVerifier> it = PendingBlocks.Iterator();
                        while (it.HasNext())
                        {
                            SignatureFileVerifier sfv = it.Next();
                            if (sfv.needSignatureFile(key))
                            {
                                if (Debug != null)
                                {
                                    Debug.println("processEntry: processing pending block");
                                }

                                sfv.SignatureFile = bytes;
                                sfv.process(SigFileSigners, ManifestDigests_Renamed);
                            }
                        }
                        return;
                    }

                    // now we are parsing a signature block file

                    String key = uname.Substring(0, uname.LastIndexOf("."));

                    if (SignerCache == null)
                    {
                        SignerCache = new List <>();
                    }

                    if (ManDig == null)
                    {
                        lock (ManifestRawBytes)
                        {
                            if (ManDig == null)
                            {
                                ManDig           = new ManifestDigester(ManifestRawBytes);
                                ManifestRawBytes = null;
                            }
                        }
                    }

                    SignatureFileVerifier sfv = new SignatureFileVerifier(SignerCache, ManDig, uname, Baos.ToByteArray());

                    if (sfv.needSignatureFileBytes())
                    {
                        // see if we have already parsed an external .SF file
                        sbyte[] bytes = SigFileData.Get(key);

                        if (bytes == null)
                        {
                            // put this block on queue for later processing
                            // since we don't have the .SF bytes yet
                            // (uname, block);
                            if (Debug != null)
                            {
                                Debug.println("adding pending block");
                            }
                            PendingBlocks.Add(sfv);
                            return;
                        }
                        else
                        {
                            sfv.SignatureFile = bytes;
                        }
                    }
                    sfv.process(SigFileSigners, ManifestDigests_Renamed);
                }
                catch (IOException ioe)
                {
                    // e.g. sun.security.pkcs.ParsingException
                    if (Debug != null)
                    {
                        Debug.println("processEntry caught: " + ioe);
                    }
                    // ignore and treat as unsigned
                }
                catch (SignatureException se)
                {
                    if (Debug != null)
                    {
                        Debug.println("processEntry caught: " + se);
                    }
                    // ignore and treat as unsigned
                }
                catch (NoSuchAlgorithmException nsae)
                {
                    if (Debug != null)
                    {
                        Debug.println("processEntry caught: " + nsae);
                    }
                    // ignore and treat as unsigned
                }
                catch (CertificateException ce)
                {
                    if (Debug != null)
                    {
                        Debug.println("processEntry caught: " + ce);
                    }
                    // ignore and treat as unsigned
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This method scans to see which entry we're parsing and
        /// keeps various state information depending on what type of
        /// file is being parsed.
        /// </summary>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void beginEntry(JarEntry je, sun.security.util.ManifestEntryVerifier mev) throws IOException
        public virtual void BeginEntry(JarEntry je, ManifestEntryVerifier mev)
        {
            if (je == null)
            {
                return;
            }

            if (Debug != null)
            {
                Debug.println("beginEntry " + je.Name);
            }

            String name = je.Name;

            /*
             * Assumptions:
             * 1. The manifest should be the first entry in the META-INF directory.
             * 2. The .SF/.DSA/.EC files follow the manifest, before any normal entries
             * 3. Any of the following will throw a SecurityException:
             *    a. digest mismatch between a manifest section and
             *       the SF section.
             *    b. digest mismatch between the actual jar entry and the manifest
             */

            if (ParsingMeta)
            {
                String uname = name.ToUpperCase(Locale.ENGLISH);
                if ((uname.StartsWith("META-INF/") || uname.StartsWith("/META-INF/")))
                {
                    if (je.Directory)
                    {
                        mev.setEntry(null, je);
                        return;
                    }

                    if (uname.Equals(JarFile.MANIFEST_NAME) || uname.Equals(JarIndex.INDEX_NAME))
                    {
                        return;
                    }

                    if (SignatureFileVerifier.isBlockOrSF(uname))
                    {
                        /* We parse only DSA, RSA or EC PKCS7 blocks. */
                        ParsingBlockOrSF = true;
                        Baos.Reset();
                        mev.setEntry(null, je);
                        return;
                    }

                    // If a META-INF entry is not MF or block or SF, they should
                    // be normal entries. According to 2 above, no more block or
                    // SF will appear. Let's doneWithMeta.
                }
            }

            if (ParsingMeta)
            {
                DoneWithMeta();
            }

            if (je.Directory)
            {
                mev.setEntry(null, je);
                return;
            }

            // be liberal in what you accept. If the name starts with ./, remove
            // it as we internally canonicalize it with out the ./.
            if (name.StartsWith("./"))
            {
                name = name.Substring(2);
            }

            // be liberal in what you accept. If the name starts with /, remove
            // it as we internally canonicalize it with out the /.
            if (name.StartsWith("/"))
            {
                name = name.Substring(1);
            }

            // only set the jev object for entries that have a signature
            // (either verified or not)
            if (SigFileSigners.Get(name) != null || VerifiedSigners.Get(name) != null)
            {
                mev.setEntry(name, je);
                return;
            }

            // don't compute the digest for this entry
            mev.setEntry(null, je);

            return;
        }