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.

TestHxxFEncryption.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.tests;
  16. import static org.apache.poi.POIDataSamples.getDocumentInstance;
  17. import static org.apache.poi.POIDataSamples.getSlideShowInstance;
  18. import static org.apache.poi.POIDataSamples.getSpreadSheetInstance;
  19. import static org.junit.Assert.assertEquals;
  20. import static org.junit.Assert.assertNotNull;
  21. import static org.junit.Assert.assertTrue;
  22. import static org.junit.Assert.fail;
  23. import java.io.ByteArrayInputStream;
  24. import java.io.ByteArrayOutputStream;
  25. import java.io.File;
  26. import java.io.IOException;
  27. import java.nio.charset.StandardCharsets;
  28. import java.util.Arrays;
  29. import java.util.Collection;
  30. import org.apache.poi.POIDataSamples;
  31. import org.apache.poi.POIDocument;
  32. import org.apache.poi.extractor.ExtractorFactory;
  33. import org.apache.poi.extractor.POITextExtractor;
  34. import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
  35. import org.apache.poi.poifs.crypt.EncryptionInfo;
  36. import org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIEncryptionHeader;
  37. import org.apache.poi.poifs.storage.RawDataUtil;
  38. import org.junit.Test;
  39. import org.junit.runner.RunWith;
  40. import org.junit.runners.Parameterized;
  41. import org.junit.runners.Parameterized.Parameter;
  42. import org.junit.runners.Parameterized.Parameters;
  43. @RunWith(Parameterized.class)
  44. public class TestHxxFEncryption {
  45. @Parameter(value = 0)
  46. public POIDataSamples sampleDir;
  47. @Parameter(value = 1)
  48. public String file;
  49. @Parameter(value = 2)
  50. public String password;
  51. @Parameter(value = 3)
  52. public String expected;
  53. @Parameters(name="{1}")
  54. public static Collection<Object[]> data() throws IOException {
  55. final String base64 =
  56. "H4sIAAAAAAAAAF1Uu24bMRDs/RULVwkgCUhSpHaZwkDgpHJH8fZ0G/Nx4ZI6y13yG/mRfIb9R5mlZFlIpdPtcnZmdnjPf57/vvx6+f3h6obuv3"+
  57. "ylbY5bEiVHe1fEpUp5pOgkrK0iabehm7FyoZi1ks8xcvHiQu8h5bLnorTlnUvkJ/YPOHKsLVInAqCs91KakuaxLq4w3g00SgCo9Xou1UnCmSBe"+
  58. "MhpRY6qHmXVFteQfQJ5yUaaOw4qXwgPVjPGAqhNH5bBHAfTmwqqoSkLdFT/J3nC0eZBRk7yiu5s7yoU+r+9l3tDtm5A3jgt6AQxNOY2ya+U4sK"+
  59. "XZ+YczbpfSVVuzFOuunKraqIVD2ND3yVXauT3TNthR/O3IJAM7gzTOGeIcXZvj14ahotW8wSognlMu0Yyp/Fi7O6s+CK6haUUjtPCji7MVcgqH"+
  60. "jh+42tqeqPDMroJ/lBAE4AZbJbJu6Fu35ej42Tw9mYeTwVXoBKJiPeFV94q2rZJAyNEPo/qOdWYLBpq3B2JX8GDZeJ14mZf3tOQWBmpd9yQ7kI"+
  61. "DCY/jmkj1oGOicFy62r9vutC5uJsVEMFgmAXXfYcC6BRBKNHCybALFJolnrDcPXNLl+K60Vctt09YZT7YgbeOICGJ/ZgC2JztOnm1JhX3eJXni"+
  62. "U5Bqhezzlu334vD/Ajr3yDGXw5G9IZ6aLmLfQafY42N3J7cjj1LaXOHihSrcC5ThmuYIB5FX5AU8tKlnNG9Dn1EnsdD4KcnPhsSNPRiXtz461b"+
  63. "VZw8Pm6vn0afh4fvr0D5P/+cMuBAAA";
  64. final String x = new String(RawDataUtil.decompress(base64), StandardCharsets.UTF_8);
  65. return Arrays.asList(
  66. // binary rc4
  67. new Object[]{ getDocumentInstance(), "password_tika_binaryrc4.doc", "tika", "This is an encrypted Word 2007 File." },
  68. // cryptoapi
  69. new Object[]{ getDocumentInstance(), "password_password_cryptoapi.doc", "password", "This is a test" },
  70. // binary rc4
  71. new Object[]{ getSpreadSheetInstance(), "password.xls", "password", x },
  72. // cryptoapi
  73. new Object[]{ getSpreadSheetInstance(), "35897-type4.xls", "freedom", "Sheet1\nhello there!" },
  74. // cryptoapi (PPT only supports cryptoapi...)
  75. new Object[]{ getSlideShowInstance(), "cryptoapi-proc2356.ppt", "crypto", "Dominic Salemno" }
  76. );
  77. }
  78. @Test
  79. public void extract() throws IOException {
  80. File f = sampleDir.getFile(file);
  81. Biff8EncryptionKey.setCurrentUserPassword(password);
  82. try (POITextExtractor te = ExtractorFactory.createExtractor(f)) {
  83. String actual = te.getText().trim();
  84. assertEquals(expected, actual);
  85. } finally {
  86. Biff8EncryptionKey.setCurrentUserPassword(null);
  87. }
  88. }
  89. @Test
  90. public void changePassword() throws IOException {
  91. newPassword("test");
  92. }
  93. @Test
  94. public void removePassword() throws IOException {
  95. newPassword(null);
  96. }
  97. private void newPassword(String newPass) throws IOException {
  98. File f = sampleDir.getFile(file);
  99. Biff8EncryptionKey.setCurrentUserPassword(password);
  100. try (POITextExtractor te1 = ExtractorFactory.createExtractor(f)) {
  101. Biff8EncryptionKey.setCurrentUserPassword(newPass);
  102. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  103. try (POIDocument doc = (POIDocument) te1.getDocument()) {
  104. doc.write(bos);
  105. }
  106. ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
  107. try (POITextExtractor te2 = ExtractorFactory.createExtractor(bis)) {
  108. String actual = te2.getText().trim();
  109. assertEquals(expected, actual);
  110. }
  111. } finally {
  112. Biff8EncryptionKey.setCurrentUserPassword(null);
  113. }
  114. }
  115. /** changing the encryption mode and key size in poor mans style - see comments below */
  116. @Test
  117. public void changeEncryption() throws IOException {
  118. File f = sampleDir.getFile(file);
  119. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  120. Biff8EncryptionKey.setCurrentUserPassword(password);
  121. try (POITextExtractor te1 = ExtractorFactory.createExtractor(f)) {
  122. // first remove encryption
  123. Biff8EncryptionKey.setCurrentUserPassword(null);
  124. try (POIDocument doc = (POIDocument) te1.getDocument()) {
  125. doc.write(bos);
  126. }
  127. // then use default setting, which is cryptoapi
  128. String newPass = "newPass";
  129. try (POITextExtractor te2 = ExtractorFactory.createExtractor(new ByteArrayInputStream(bos.toByteArray()))) {
  130. Biff8EncryptionKey.setCurrentUserPassword(newPass);
  131. try (POIDocument doc = (POIDocument) te2.getDocument()) {
  132. bos.reset();
  133. doc.write(bos);
  134. }
  135. }
  136. // and finally update cryptoapi setting
  137. try (POITextExtractor te3 = ExtractorFactory.createExtractor(new ByteArrayInputStream(bos.toByteArray()));
  138. POIDocument doc = (POIDocument) te3.getDocument()) {
  139. // need to cache data (i.e. read all data) before changing the key size
  140. Class<?> clazz = doc.getClass();
  141. if ("HSLFSlideShow".equals(clazz.getSimpleName())) {
  142. try {
  143. clazz.getDeclaredMethod("getPictureData").invoke(doc);
  144. } catch (ReflectiveOperationException e) {
  145. fail("either scratchpad jar is included and this should work or the clazz should be != HSLFSlideShowImpl");
  146. }
  147. doc.getDocumentSummaryInformation();
  148. }
  149. EncryptionInfo ei = doc.getEncryptionInfo();
  150. assertNotNull(ei);
  151. assertTrue(ei.getHeader() instanceof CryptoAPIEncryptionHeader);
  152. assertEquals(0x28, ei.getHeader().getKeySize());
  153. ei.getHeader().setKeySize(0x78);
  154. bos.reset();
  155. doc.write(bos);
  156. }
  157. // check the setting
  158. try (POITextExtractor te4 = ExtractorFactory.createExtractor(new ByteArrayInputStream(bos.toByteArray()));
  159. POIDocument doc = (POIDocument) te4.getDocument()) {
  160. EncryptionInfo ei = doc.getEncryptionInfo();
  161. assertNotNull(ei);
  162. assertTrue(ei.getHeader() instanceof CryptoAPIEncryptionHeader);
  163. assertEquals(0x78, ei.getHeader().getKeySize());
  164. }
  165. } finally {
  166. Biff8EncryptionKey.setCurrentUserPassword(null);
  167. }
  168. }
  169. }