You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.poifs.crypt;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertTrue;
  18. import java.io.ByteArrayInputStream;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.File;
  21. import java.io.FileInputStream;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.security.GeneralSecurityException;
  25. import java.util.zip.ZipEntry;
  26. import java.util.zip.ZipInputStream;
  27. import org.apache.poi.POIDataSamples;
  28. import org.apache.poi.poifs.filesystem.DirectoryNode;
  29. import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
  30. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  31. import org.apache.poi.util.IOUtils;
  32. import org.apache.poi.xssf.XSSFTestDataSamples;
  33. import org.junit.Test;
  34. /**
  35. * @author Maxim Valyanskiy
  36. * @author Gary King
  37. */
  38. public class TestDecryptor {
  39. @Test
  40. public void passwordVerification() throws IOException, GeneralSecurityException {
  41. POIFSFileSystem fs = new POIFSFileSystem(POIDataSamples.getPOIFSInstance().openResourceAsStream("protect.xlsx"));
  42. EncryptionInfo info = new EncryptionInfo(fs);
  43. Decryptor d = Decryptor.getInstance(info);
  44. assertTrue(d.verifyPassword(Decryptor.DEFAULT_PASSWORD));
  45. }
  46. @Test
  47. public void decrypt() throws IOException, GeneralSecurityException {
  48. POIFSFileSystem fs = new POIFSFileSystem(POIDataSamples.getPOIFSInstance().openResourceAsStream("protect.xlsx"));
  49. EncryptionInfo info = new EncryptionInfo(fs);
  50. Decryptor d = Decryptor.getInstance(info);
  51. d.verifyPassword(Decryptor.DEFAULT_PASSWORD);
  52. zipOk(fs.getRoot(), d);
  53. }
  54. @Test
  55. public void agile() throws IOException, GeneralSecurityException {
  56. POIFSFileSystem fs = new POIFSFileSystem(POIDataSamples.getPOIFSInstance().openResourceAsStream("protected_agile.docx"));
  57. EncryptionInfo info = new EncryptionInfo(fs);
  58. assertTrue(info.getVersionMajor() == 4 && info.getVersionMinor() == 4);
  59. Decryptor d = Decryptor.getInstance(info);
  60. assertTrue(d.verifyPassword(Decryptor.DEFAULT_PASSWORD));
  61. zipOk(fs.getRoot(), d);
  62. }
  63. private void zipOk(DirectoryNode root, Decryptor d) throws IOException, GeneralSecurityException {
  64. ZipInputStream zin = new ZipInputStream(d.getDataStream(root));
  65. while (true) {
  66. ZipEntry entry = zin.getNextEntry();
  67. if (entry==null) break;
  68. // crc32 is checked within zip-stream
  69. if (entry.isDirectory()) continue;
  70. zin.skip(entry.getSize());
  71. byte buf[] = new byte[10];
  72. int readBytes = zin.read(buf);
  73. // zin.available() doesn't work for entries
  74. assertEquals("size failed for "+entry.getName(), -1, readBytes);
  75. }
  76. zin.close();
  77. }
  78. @Test
  79. public void dataLength() throws Exception {
  80. POIFSFileSystem fs = new POIFSFileSystem(POIDataSamples.getPOIFSInstance().openResourceAsStream("protected_agile.docx"));
  81. EncryptionInfo info = new EncryptionInfo(fs);
  82. Decryptor d = Decryptor.getInstance(info);
  83. d.verifyPassword(Decryptor.DEFAULT_PASSWORD);
  84. InputStream is = d.getDataStream(fs);
  85. long len = d.getLength();
  86. assertEquals(12810, len);
  87. byte[] buf = new byte[(int)len];
  88. is.read(buf);
  89. ZipInputStream zin = new ZipInputStream(new ByteArrayInputStream(buf));
  90. while (true) {
  91. ZipEntry entry = zin.getNextEntry();
  92. if (entry==null) {
  93. break;
  94. }
  95. while (zin.available()>0) {
  96. zin.skip(zin.available());
  97. }
  98. }
  99. }
  100. @Test
  101. public void bug57080() throws Exception {
  102. // the test file contains a wrong ole entry size, produced by extenxls
  103. // the fix limits the available size and tries to read all entries
  104. File f = POIDataSamples.getPOIFSInstance().getFile("extenxls_pwd123.xlsx");
  105. NPOIFSFileSystem fs = new NPOIFSFileSystem(f, true);
  106. EncryptionInfo info = new EncryptionInfo(fs);
  107. Decryptor d = Decryptor.getInstance(info);
  108. d.verifyPassword("pwd123");
  109. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  110. ZipInputStream zis = new ZipInputStream(d.getDataStream(fs));
  111. ZipEntry ze;
  112. while ((ze = zis.getNextEntry()) != null) {
  113. bos.reset();
  114. IOUtils.copy(zis, bos);
  115. assertEquals(ze.getSize(), bos.size());
  116. }
  117. zis.close();
  118. fs.close();
  119. }
  120. @Test
  121. public void test58616() throws IOException, GeneralSecurityException {
  122. POIFSFileSystem pfs = new POIFSFileSystem(new FileInputStream(XSSFTestDataSamples.getSampleFile("58616.xlsx")));
  123. EncryptionInfo info = new EncryptionInfo(pfs);
  124. Decryptor dec = Decryptor.getInstance(info);
  125. //dec.verifyPassword(null);
  126. dec.getDataStream(pfs);
  127. }
  128. }