Exemplo n.º 1
0
        public void Bug57080()
        {
            // the test file Contains a wrong ole entry size, produced by extenxls
            // the fix limits the available size and tries to read all entries
            FileStream       f    = POIDataSamples.GetPOIFSInstance().GetFile("extenxls_pwd123.xlsx");
            NPOIFSFileSystem fs   = new NPOIFSFileSystem(f, true);
            EncryptionInfo   info = new EncryptionInfo(fs);
            Decryptor        d    = Decryptor.GetInstance(info);

            d.VerifyPassword("pwd123");
            MemoryStream   bos = new MemoryStream();
            ZipInputStream zis = new ZipInputStream(d.GetDataStream(fs));
            ZipEntry       ze;

            while ((ze = zis.GetNextEntry()) != null)
            {
                //bos.Reset();
                bos.Seek(0, SeekOrigin.Begin);
                bos.SetLength(0);
                IOUtils.Copy(zis, bos);
                Assert.AreEqual(ze.Size, bos.Length);
            }

            zis.Close();
            fs.Close();
        }
Exemplo n.º 2
0
        public void InPlaceReWrite()
        {
            FileInfo f = TempFile.CreateTempFile("protected_agile", ".docx");
            // File f = new File("protected_agile.docx");
            FileStream fos = f.Create();
            Stream     fis = POIDataSamples.GetPOIFSInstance().OpenResourceAsStream("protected_agile.docx");

            IOUtils.Copy(fis, fos);
            fis.Close();
            fos.Close();

            NPOIFSFileSystem fs = new NPOIFSFileSystem(f, false);

            // decrypt the protected file - in this case it was encrypted with the default password
            EncryptionInfo encInfo = new EncryptionInfo(fs);
            Decryptor      d       = encInfo.Decryptor;
            bool           b       = d.VerifyPassword(Decryptor.DEFAULT_PASSWORD);

            Assert.IsTrue(b);

            // do some strange things with it ;)
            XWPFDocument docx = new XWPFDocument(d.GetDataStream(fs));

            docx.GetParagraphArray(0).InsertNewRun(0).SetText("POI was here! All your base are belong to us!");
            docx.GetParagraphArray(0).InsertNewRun(1).AddBreak();

            // and encrypt it again
            Encryptor e = encInfo.Encryptor;

            e.ConfirmPassword("AYBABTU");
            docx.Write(e.GetDataStream(fs));

            fs.Close();
        }
Exemplo n.º 3
0
        public void TestPasswordVerification()
        {
            POIFSFileSystem fs = new POIFSFileSystem(POIDataSamples.GetPOIFSInstance().OpenResourceAsStream("protect.xlsx"));

            EncryptionInfo info = new EncryptionInfo(fs);

            Decryptor d = Decryptor.GetInstance(info);

            Assert.IsTrue(d.VerifyPassword(Decryptor.DEFAULT_PASSWORD));
        }
Exemplo n.º 4
0
        public void TestDecrypt()
        {
            POIFSFileSystem fs = new POIFSFileSystem(POIDataSamples.GetPOIFSInstance().OpenResourceAsStream("protect.xlsx"));

            EncryptionInfo info = new EncryptionInfo(fs);

            Decryptor d = Decryptor.GetInstance(info);

            d.VerifyPassword(Decryptor.DEFAULT_PASSWORD);

            ZipOk(fs, d);
        }
Exemplo n.º 5
0
        public void TestAgile()
        {
            POIFSFileSystem fs = new POIFSFileSystem(POIDataSamples.GetPOIFSInstance().OpenResourceAsStream("protected_agile.docx"));

            EncryptionInfo info = new EncryptionInfo(fs);

            Assert.IsTrue(info.VersionMajor == 4 && info.VersionMinor == 4);

            Decryptor d = Decryptor.GetInstance(info);

            Assert.IsTrue(d.VerifyPassword(Decryptor.DEFAULT_PASSWORD));

            ZipOk(fs, d);
        }
Exemplo n.º 6
0
        public void TestEncryptionInfoSHA512()
        {
            POIFSFileSystem fs = new POIFSFileSystem(POIDataSamples.GetPOIFSInstance().OpenResourceAsStream("protected_sha512.xlsx"));

            EncryptionInfo info = new EncryptionInfo(fs);

            Assert.AreEqual(4, info.VersionMajor);
            Assert.AreEqual(4, info.VersionMinor);

            Assert.AreEqual(CipherAlgorithm.aes256, info.Header.CipherAlgorithm);
            Assert.AreEqual(HashAlgorithm.sha512, info.Header.HashAlgorithm);
            Assert.AreEqual(256, info.Header.KeySize);
            Assert.AreEqual(64, info.Verifier.EncryptedVerifierHash.Length);
            Assert.AreEqual(CipherProvider.aes, info.Header.CipherProvider);
            //        Assert.AreEqual("Microsoft Enhanced RSA and AES Cryptographic Provider", info.Header.CspName);
        }
Exemplo n.º 7
0
        public void TestEncryptionInfo1()
        {
            POIFSFileSystem fs = new POIFSFileSystem(POIDataSamples.GetPOIFSInstance().OpenResourceAsStream("protect.xlsx"));

            EncryptionInfo info = new EncryptionInfo(fs);

            Assert.AreEqual(3, info.VersionMajor);
            Assert.AreEqual(2, info.VersionMinor);

            Assert.AreEqual(EncryptionHeader.ALGORITHM_AES_128, info.Header.Algorithm);
            Assert.AreEqual(EncryptionHeader.HASH_SHA1, info.Header.HashAlgorithm);
            Assert.AreEqual(128, info.Header.KeySize);
            Assert.AreEqual(EncryptionHeader.PROVIDER_AES, info.Header.ProviderType);
            Assert.AreEqual("Microsoft Enhanced RSA and AES Cryptographic Provider", info.Header.CspName);

            Assert.AreEqual(32, info.Verifier.VerifierHash.Length);
        }
Exemplo n.º 8
0
        public void TestShortLastBlock()
        {
            String[] files = new String[] {
                "ShortLastBlock.qwp", "ShortLastBlock.wps"
            };
            POIDataSamples _samples = POIDataSamples.GetPOIFSInstance();

            for (int i = 0; i < files.Length; i++)
            {
                // Open the file up
                POIFSFileSystem fs = new POIFSFileSystem(
                    _samples.OpenResourceAsStream(files[i])
                    );

                // Write it into a temp output array
                MemoryStream baos = new MemoryStream();
                fs.WriteFileSystem(baos);

                // Check sizes
            }
        }
Exemplo n.º 9
0
        public void TestNotesOLE2Files()
        {
            POIDataSamples _samples = POIDataSamples.GetPOIFSInstance();

            // Open the file up
            POIFSFileSystem fs = new POIFSFileSystem(
                _samples.OpenResourceAsStream("Notes.ole2")
                );

            // Check the contents
            Assert.AreEqual(1, fs.Root.EntryCount);
            fs.Root.Entries.MoveNext();

            //fs.Root.Entries.Current is always null. WHY???
            //Entry entry = fs.Root.Entries.Current;
            Entry entry = fs.Root.GetEntry(0);

            Assert.IsTrue(entry.IsDirectoryEntry);
            Assert.IsTrue(entry is DirectoryEntry);

            // The directory lacks a name!
            DirectoryEntry dir = (DirectoryEntry)entry;

            Assert.AreEqual("", dir.Name);

            // Has two children
            Assert.AreEqual(2, dir.EntryCount);

            // Check them
            IEnumerator <Entry> it = dir.Entries;

            it.MoveNext();
            entry = it.Current;
            Assert.AreEqual(true, entry.IsDocumentEntry);
            Assert.AreEqual("\u0001Ole10Native", entry.Name);
            it.MoveNext();
            entry = it.Current;
            Assert.AreEqual(true, entry.IsDocumentEntry);
            Assert.AreEqual("\u0001CompObj", entry.Name);
        }
Exemplo n.º 10
0
        public void TestFiles()
        {
            FileStream[] files =
            {
                // bug 51891
                POIDataSamples.GetPOIFSInstance().GetFile("multimedia.doc"),
                // tika bug 1072
                POIDataSamples.GetPOIFSInstance().GetFile("20-Force-on-a-current-S00.doc"),
                // other files Containing ole10native records ...
                POIDataSamples.GetDocumentInstance().GetFile("Bug53380_3.doc"),
                POIDataSamples.GetDocumentInstance().GetFile("Bug47731.doc")
            };

            foreach (FileStream f in files)
            {
                NPOIFSFileSystem fs      = new NPOIFSFileSystem(f, true);
                List <Entry>     entries = new List <Entry>();
                FindOle10(entries, fs.Root, "/", "");

                foreach (Entry e in entries)
                {
                    MemoryStream bosExp = new MemoryStream();
                    Stream       is1    = ((DirectoryNode)e.Parent).CreateDocumentInputStream(e);
                    IOUtils.Copy(is1, bosExp);
                    is1.Close();

                    Ole10Native ole = Ole10Native.CreateFromEmbeddedOleObject((DirectoryNode)e.Parent);

                    MemoryStream bosAct = new MemoryStream();
                    ole.WriteOut(bosAct);

                    //assertThat(bosExp.ToByteArray(), EqualTo(bosAct.ToByteArray()));
                    Assert.IsTrue(Arrays.Equals(bosExp.ToArray(), bosAct.ToArray()));
                }

                fs.Close();
            }
        }
Exemplo n.º 11
0
        public void DataLength()
        {
            POIFSFileSystem fs = new POIFSFileSystem(POIDataSamples.GetPOIFSInstance().OpenResourceAsStream("protected_agile.docx"));

            EncryptionInfo info = new EncryptionInfo(fs);

            Decryptor d = Decryptor.GetInstance(info);

            d.VerifyPassword(Decryptor.DEFAULT_PASSWORD);

            Stream is1 = d.GetDataStream(fs);

            long len = d.GetLength();

            Assert.AreEqual(12810, len);

            byte[] buf = new byte[(int)len];

            is1.Read(buf, 0, buf.Length);

            ZipInputStream zin = new ZipInputStream(new MemoryStream(buf));

            while (true)
            {
                ZipEntry entry = zin.GetNextEntry();
                if (entry == null)
                {
                    break;
                }

                while (zin.Available > 0)
                {
                    zin.Skip(zin.Available);
                }
            }
        }