Search in sources :

Example 96 with OPCPackage

use of org.apache.poi.openxml4j.opc.OPCPackage in project poi by apache.

the class TestOPCComplianceCoreProperties method testCorePropertiesPart.

@Test
public void testCorePropertiesPart() {
    OPCPackage pkg;
    try {
        InputStream is = OpenXML4JTestDataSamples.openComplianceSampleStream("OPCCompliance_CoreProperties_OnlyOneCorePropertiesPart.docx");
        pkg = OPCPackage.open(is);
    } catch (InvalidFormatException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    pkg.revert();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) Test(org.junit.Test)

Example 97 with OPCPackage

use of org.apache.poi.openxml4j.opc.OPCPackage in project poi by apache.

the class TestSignatureInfo method testManipulation.

@Test
public void testManipulation() throws Exception {
    // sign & validate
    String testFile = "hello-world-unsigned.xlsx";
    @SuppressWarnings("resource") OPCPackage pkg = OPCPackage.open(copy(testdata.getFile(testFile)), PackageAccess.READ_WRITE);
    sign(pkg, "Test", "CN=Test", 1);
    // manipulate
    XSSFWorkbook wb = new XSSFWorkbook(pkg);
    wb.setSheetName(0, "manipulated");
    // ... I don't know, why commit is protected ...
    POITestCase.callMethod(XSSFWorkbook.class, wb, Void.class, "commit", new Class[0], new Object[0]);
    // todo: test a manipulation on a package part, which is not signed
    // ... maybe in combination with #56164 
    // validate
    SignatureConfig sic = new SignatureConfig();
    sic.setOpcPackage(pkg);
    SignatureInfo si = new SignatureInfo();
    si.setSignatureConfig(sic);
    boolean b = si.verifySignature();
    assertFalse("signature should be broken", b);
    wb.close();
}
Also used : SignatureInfo(org.apache.poi.poifs.crypt.dsig.SignatureInfo) SignatureConfig(org.apache.poi.poifs.crypt.dsig.SignatureConfig) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Example 98 with OPCPackage

use of org.apache.poi.openxml4j.opc.OPCPackage in project poi by apache.

the class TestSignatureInfo method bug58630.

@Test
public void bug58630() throws Exception {
    // test deletion of sheet 0 and signing
    File tpl = copy(testdata.getFile("bug58630.xlsx"));
    SXSSFWorkbook wb1 = new SXSSFWorkbook((XSSFWorkbook) WorkbookFactory.create(tpl), 10);
    wb1.setCompressTempFiles(true);
    wb1.removeSheetAt(0);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    wb1.write(os);
    wb1.close();
    OPCPackage pkg = OPCPackage.open(new ByteArrayInputStream(os.toByteArray()));
    initKeyPair("Test", "CN=Test");
    SignatureConfig signatureConfig = new SignatureConfig();
    signatureConfig.setKey(keyPair.getPrivate());
    signatureConfig.setSigningCertificateChain(Collections.singletonList(x509));
    signatureConfig.setOpcPackage(pkg);
    SignatureInfo si = new SignatureInfo();
    si.setSignatureConfig(signatureConfig);
    si.confirmSignature();
    assertTrue("invalid signature", si.verifySignature());
    pkg.close();
}
Also used : SignatureInfo(org.apache.poi.poifs.crypt.dsig.SignatureInfo) ByteArrayInputStream(java.io.ByteArrayInputStream) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) SignatureConfig(org.apache.poi.poifs.crypt.dsig.SignatureConfig) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Example 99 with OPCPackage

use of org.apache.poi.openxml4j.opc.OPCPackage in project poi by apache.

the class TestSignatureInfo method testMultiSign.

@Test
public void testMultiSign() throws Exception {
    initKeyPair("KeyA", "CN=KeyA");
    //KeyPair keyPairA = keyPair;
    //X509Certificate x509A = x509;
    initKeyPair("KeyB", "CN=KeyB");
    //KeyPair keyPairB = keyPair;
    //X509Certificate x509B = x509;
    File tpl = copy(testdata.getFile("bug58630.xlsx"));
    OPCPackage pkg = OPCPackage.open(tpl);
    try {
        //SignatureConfig signatureConfig = new SignatureConfig();
        assertNotNull(pkg);
    } finally {
        pkg.close();
    }
}
Also used : File(java.io.File) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Example 100 with OPCPackage

use of org.apache.poi.openxml4j.opc.OPCPackage in project poi by apache.

the class TestSignatureInfo method testSignSpreadsheet.

@Test
public void testSignSpreadsheet() throws Exception {
    String testFile = "hello-world-unsigned.xlsx";
    OPCPackage pkg = OPCPackage.open(copy(testdata.getFile(testFile)), PackageAccess.READ_WRITE);
    sign(pkg, "Test", "CN=Test", 1);
    pkg.close();
}
Also used : OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Aggregations

OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)116 Test (org.junit.Test)54 InputStream (java.io.InputStream)29 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)25 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)21 File (java.io.File)19 IOException (java.io.IOException)17 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)16 ByteArrayInputStream (java.io.ByteArrayInputStream)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)13 SignatureConfig (org.apache.poi.poifs.crypt.dsig.SignatureConfig)10 SignatureInfo (org.apache.poi.poifs.crypt.dsig.SignatureInfo)10 XSSFReader (org.apache.poi.xssf.eventusermodel.XSSFReader)10 OutputStream (java.io.OutputStream)9 ArrayList (java.util.ArrayList)9 FileOutputStream (java.io.FileOutputStream)8 InvalidOperationException (org.apache.poi.openxml4j.exceptions.InvalidOperationException)8 XmlException (org.apache.xmlbeans.XmlException)8 FileInputStream (java.io.FileInputStream)7