View | Details | Raw Unified | Return to bug 53475
Collapse All | Expand All

(-)src/java/org/apache/poi/poifs/crypt/AgileDecryptor.java (-1 / +7 lines)
Lines 194-199 Link Here
194
194
195
    private Cipher getCipher(SecretKey key, byte[] vec)
195
    private Cipher getCipher(SecretKey key, byte[] vec)
196
        throws GeneralSecurityException {
196
        throws GeneralSecurityException {
197
        
197
        String name = null;
198
        String name = null;
198
        String chain = null;
199
        String chain = null;
199
200
Lines 209-214 Link Here
209
             throw new EncryptedDocumentException("Unsupported algorithm");
210
             throw new EncryptedDocumentException("Unsupported algorithm");
210
        }
211
        }
211
212
213
        // check for JCE policies files
214
        if (Cipher.getMaxAllowedKeyLength(name) < _info.getHeader().getKeySize()) {
215
            throw new EncryptedDocumentException("export restriction in place - please install JCE Unlimited Strength Jurisdiction Policy files");
216
        }
217
        
212
        switch (verifier.getCipherMode()) {
218
        switch (verifier.getCipherMode()) {
213
          case EncryptionHeader.MODE_CBC: 
219
          case EncryptionHeader.MODE_CBC: 
214
              chain = "CBC"; 
220
              chain = "CBC"; 
Lines 219-225 Link Here
219
          default: 
225
          default: 
220
              throw new EncryptedDocumentException("Unsupported chain mode");
226
              throw new EncryptedDocumentException("Unsupported chain mode");
221
        }
227
        }
222
228
        
223
        Cipher cipher = Cipher.getInstance(name + "/" + chain + "/NoPadding");
229
        Cipher cipher = Cipher.getInstance(name + "/" + chain + "/NoPadding");
224
        IvParameterSpec iv = new IvParameterSpec(vec);
230
        IvParameterSpec iv = new IvParameterSpec(vec);
225
        cipher.init(Cipher.DECRYPT_MODE, key, iv);
231
        cipher.init(Cipher.DECRYPT_MODE, key, iv);
(-)src/ooxml/testcases/org/apache/poi/xwpf/AllXWPFTests.java (-22 / +19 lines)
Lines 17-25 Link Here
17
17
18
package org.apache.poi.xwpf;
18
package org.apache.poi.xwpf;
19
19
20
import junit.framework.Test;
21
import junit.framework.TestSuite;
22
23
import org.apache.poi.xwpf.extractor.TestXWPFWordExtractor;
20
import org.apache.poi.xwpf.extractor.TestXWPFWordExtractor;
24
import org.apache.poi.xwpf.model.TestXWPFHeaderFooterPolicy;
21
import org.apache.poi.xwpf.model.TestXWPFHeaderFooterPolicy;
25
import org.apache.poi.xwpf.usermodel.TestXWPFDocument;
22
import org.apache.poi.xwpf.usermodel.TestXWPFDocument;
Lines 31-60 Link Here
31
import org.apache.poi.xwpf.usermodel.TestXWPFRun;
28
import org.apache.poi.xwpf.usermodel.TestXWPFRun;
32
import org.apache.poi.xwpf.usermodel.TestXWPFStyles;
29
import org.apache.poi.xwpf.usermodel.TestXWPFStyles;
33
import org.apache.poi.xwpf.usermodel.TestXWPFTable;
30
import org.apache.poi.xwpf.usermodel.TestXWPFTable;
31
import org.junit.runner.RunWith;
32
import org.junit.runners.Suite;
34
33
35
/**
34
/**
36
 * Collects all tests for <tt>org.apache.poi.xwpf</tt> and sub-packages.
35
 * Collects all tests for <tt>org.apache.poi.xwpf</tt> and sub-packages.
37
 *
36
 *
38
 * @author Josh Micich
37
 * @author Josh Micich
39
 */
38
 */
39
@RunWith(Suite.class)
40
@Suite.SuiteClasses({
41
    TestXWPFBugs.class,
42
    TestXWPFDocument.class,
43
    TestXWPFWordExtractor.class,
44
    TestXWPFHeaderFooterPolicy.class,
45
    TestXWPFHeader.class,
46
    TestXWPFHeadings.class,
47
    TestXWPFParagraph.class,
48
    TestXWPFRun.class,
49
    TestXWPFTable.class,
50
    TestXWPFStyles.class,
51
    TestXWPFPictureData.class,
52
    TestXWPFNumbering.class,
53
    TestAllExtendedProperties.class,
54
    TestPackageCorePropertiesGetKeywords.class
55
})
40
public final class AllXWPFTests {
56
public final class AllXWPFTests {
41
42
   public static Test suite() {
43
       TestSuite result = new TestSuite(AllXWPFTests.class.getName());
44
       result.addTestSuite(TestXWPFBugs.class);
45
       result.addTestSuite(TestXWPFDocument.class);
46
       result.addTestSuite(TestXWPFWordExtractor.class);
47
       result.addTestSuite(TestXWPFHeaderFooterPolicy.class);
48
       result.addTestSuite(TestXWPFHeader.class);
49
       result.addTestSuite(TestXWPFHeadings.class);
50
       result.addTestSuite(TestXWPFParagraph.class);
51
       result.addTestSuite(TestXWPFRun.class);
52
       result.addTestSuite(TestXWPFTable.class);
53
       result.addTestSuite(TestXWPFStyles.class);
54
       result.addTestSuite(TestXWPFPictureData.class);
55
       result.addTestSuite(TestXWPFNumbering.class);
56
       result.addTestSuite(TestAllExtendedProperties.class);
57
       result.addTestSuite(TestPackageCorePropertiesGetKeywords.class);
58
       return result;
59
   }
60
}
57
}
(-)src/ooxml/testcases/org/apache/poi/xwpf/TestXWPFBugs.java (-53 / +52 lines)
Lines 1-12 Link Here
1
package org.apache.poi.xwpf;
1
package org.apache.poi.xwpf;
2
2
3
import static org.junit.Assert.assertEquals;
4
import static org.junit.Assert.assertNotNull;
5
import static org.junit.Assert.assertTrue;
6
3
import java.io.File;
7
import java.io.File;
4
import java.io.InputStream;
8
import java.io.InputStream;
5
9
6
import junit.framework.TestCase;
10
import javax.crypto.Cipher;
7
11
8
import org.apache.poi.POIDataSamples;
12
import org.apache.poi.POIDataSamples;
9
import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
10
import org.apache.poi.openxml4j.opc.OPCPackage;
13
import org.apache.poi.openxml4j.opc.OPCPackage;
11
import org.apache.poi.poifs.crypt.Decryptor;
14
import org.apache.poi.poifs.crypt.Decryptor;
12
import org.apache.poi.poifs.crypt.EncryptionHeader;
15
import org.apache.poi.poifs.crypt.EncryptionHeader;
Lines 14-88 Link Here
14
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
17
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
15
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
18
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
16
import org.apache.poi.xwpf.usermodel.XWPFDocument;
19
import org.apache.poi.xwpf.usermodel.XWPFDocument;
20
import org.junit.Assume;
21
import org.junit.Test;
17
22
18
public class TestXWPFBugs extends TestCase {
23
public class TestXWPFBugs {
19
    /**
24
    /**
20
     * A word document that's encrypted with non-standard
25
     * A word document that's encrypted with non-standard
21
     *  Encryption options, and no cspname section. See bug 53475
26
     *  Encryption options, and no cspname section. See bug 53475
22
     */
27
     */
28
    @Test
23
    public void test53475NoCSPName() throws Exception {
29
    public void test53475NoCSPName() throws Exception {
24
        try {
30
        File file = POIDataSamples.getDocumentInstance().getFile("bug53475-password-is-solrcell.docx");
25
            Biff8EncryptionKey.setCurrentUserPassword("solrcell");
31
        NPOIFSFileSystem filesystem = new NPOIFSFileSystem(file, true);
26
            File file = POIDataSamples.getDocumentInstance().getFile("bug53475-password-is-solrcell.docx");
27
            NPOIFSFileSystem filesystem = new NPOIFSFileSystem(file, true);
28
32
29
            // Check the encryption details
33
        // Check the encryption details
30
            EncryptionInfo info = new EncryptionInfo(filesystem);
34
        EncryptionInfo info = new EncryptionInfo(filesystem);
31
            assertEquals(128, info.getHeader().getKeySize());
35
        assertEquals(128, info.getHeader().getKeySize());
32
            assertEquals(EncryptionHeader.ALGORITHM_AES_128, info.getHeader().getAlgorithm());
36
        assertEquals(EncryptionHeader.ALGORITHM_AES_128, info.getHeader().getAlgorithm());
33
            assertEquals(EncryptionHeader.HASH_SHA1, info.getHeader().getHashAlgorithm());
37
        assertEquals(EncryptionHeader.HASH_SHA1, info.getHeader().getHashAlgorithm());
34
38
35
            // Check it can be decoded
39
        // Check it can be decoded
36
            Decryptor d = Decryptor.getInstance(info);     
40
        Decryptor d = Decryptor.getInstance(info);     
37
            assertTrue("Unable to process: document is encrypted", d.verifyPassword("solrcell"));
41
        assertTrue("Unable to process: document is encrypted", d.verifyPassword("solrcell"));
38
42
39
            // Check we can read the word document in that
43
        // Check we can read the word document in that
40
            InputStream dataStream = d.getDataStream(filesystem);
44
        InputStream dataStream = d.getDataStream(filesystem);
41
            OPCPackage opc = OPCPackage.open(dataStream);
45
        OPCPackage opc = OPCPackage.open(dataStream);
42
            XWPFDocument doc = new XWPFDocument(opc);
46
        XWPFDocument doc = new XWPFDocument(opc);
43
            XWPFWordExtractor ex = new XWPFWordExtractor(doc);
47
        XWPFWordExtractor ex = new XWPFWordExtractor(doc);
44
            String text = ex.getText();
48
        String text = ex.getText();
45
            assertNotNull(text);
49
        assertNotNull(text);
46
            assertEquals("This is password protected Word document.", text.trim());
50
        assertEquals("This is password protected Word document.", text.trim());
47
            ex.close();
51
        ex.close();
48
        } finally {
49
            Biff8EncryptionKey.setCurrentUserPassword(null);
50
        }
51
    }
52
    }
52
53
53
    /**
54
    /**
54
     * A word document with aes-256, i.e. aes is always 128 bit (= 128 bit block size),
55
     * A word document with aes-256, i.e. aes is always 128 bit (= 128 bit block size),
55
     * but the key can be 128/192/256 bits
56
     * but the key can be 128/192/256 bits
56
     */
57
     */
58
    @Test
57
    public void test53475_aes256() throws Exception {
59
    public void test53475_aes256() throws Exception {
58
        try {
60
        int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES");
59
            Biff8EncryptionKey.setCurrentUserPassword("pass");
61
        Assume.assumeTrue("Please install JCE Unlimited Strength Jurisdiction Policy files for AES 256", maxKeyLen == 2147483647);
60
            File file = POIDataSamples.getDocumentInstance().getFile("bug53475-password-is-pass.docx");
62
        File file = POIDataSamples.getDocumentInstance().getFile("bug53475-password-is-pass.docx");
61
            NPOIFSFileSystem filesystem = new NPOIFSFileSystem(file, true);
63
        NPOIFSFileSystem filesystem = new NPOIFSFileSystem(file, true);
62
64
63
            // Check the encryption details
65
        // Check the encryption details
64
            EncryptionInfo info = new EncryptionInfo(filesystem);
66
        EncryptionInfo info = new EncryptionInfo(filesystem);
65
            assertEquals(16, info.getHeader().getBlockSize());
67
        assertEquals(16, info.getHeader().getBlockSize());
66
            assertEquals(256, info.getHeader().getKeySize());
68
        assertEquals(256, info.getHeader().getKeySize());
67
            assertEquals(EncryptionHeader.ALGORITHM_AES_256, info.getHeader().getAlgorithm());
69
        assertEquals(EncryptionHeader.ALGORITHM_AES_256, info.getHeader().getAlgorithm());
68
            assertEquals(EncryptionHeader.HASH_SHA1, info.getHeader().getHashAlgorithm());
70
        assertEquals(EncryptionHeader.HASH_SHA1, info.getHeader().getHashAlgorithm());
69
71
70
            // Check it can be decoded
72
        // Check it can be decoded
71
            Decryptor d = Decryptor.getInstance(info);     
73
        Decryptor d = Decryptor.getInstance(info);     
72
            assertTrue("Unable to process: document is encrypted", d.verifyPassword("pass"));
74
        assertTrue("Unable to process: document is encrypted", d.verifyPassword("pass"));
73
75
74
            // Check we can read the word document in that
76
        // Check we can read the word document in that
75
            InputStream dataStream = d.getDataStream(filesystem);
77
        InputStream dataStream = d.getDataStream(filesystem);
76
            OPCPackage opc = OPCPackage.open(dataStream);
78
        OPCPackage opc = OPCPackage.open(dataStream);
77
            XWPFDocument doc = new XWPFDocument(opc);
79
        XWPFDocument doc = new XWPFDocument(opc);
78
            XWPFWordExtractor ex = new XWPFWordExtractor(doc);
80
        XWPFWordExtractor ex = new XWPFWordExtractor(doc);
79
            String text = ex.getText();
81
        String text = ex.getText();
80
            assertNotNull(text);
82
        assertNotNull(text);
81
            // I know ... a stupid typo, maybe next time ...
83
        // I know ... a stupid typo, maybe next time ...
82
            assertEquals("The is a password protected document.", text.trim());
84
        assertEquals("The is a password protected document.", text.trim());
83
            ex.close();
85
        ex.close();
84
        } finally {
85
            Biff8EncryptionKey.setCurrentUserPassword(null);
86
        }
87
    }
86
    }
88
}
87
}

Return to bug 53475