Search in sources :

Example 31 with OPCPackage

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

the class XSSFWorkbook method newPackage.

/**
     * Create a new SpreadsheetML package and setup the default minimal content
     */
protected static OPCPackage newPackage(XSSFWorkbookType workbookType) {
    try {
        OPCPackage pkg = OPCPackage.create(new ByteArrayOutputStream());
        // Main part
        PackagePartName corePartName = PackagingURIHelper.createPartName(XSSFRelation.WORKBOOK.getDefaultFileName());
        // Create main part relationship
        pkg.addRelationship(corePartName, TargetMode.INTERNAL, PackageRelationshipTypes.CORE_DOCUMENT);
        // Create main document part
        pkg.createPart(corePartName, workbookType.getContentType());
        pkg.getPackageProperties().setCreatorProperty(DOCUMENT_CREATOR);
        return pkg;
    } catch (Exception e) {
        throw new POIXMLException(e);
    }
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) ByteArrayOutputStream(java.io.ByteArrayOutputStream) POIXMLException(org.apache.poi.POIXMLException) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) OpenXML4JException(org.apache.poi.openxml4j.exceptions.OpenXML4JException) NoSuchElementException(java.util.NoSuchElementException) POIXMLException(org.apache.poi.POIXMLException) XLSBUnsupportedException(org.apache.poi.xssf.XLSBUnsupportedException) IOException(java.io.IOException) XmlException(org.apache.xmlbeans.XmlException)

Example 32 with OPCPackage

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

the class TestSignatureInfo method getSignerUnsigned.

@Test
public void getSignerUnsigned() throws Exception {
    String[] testFiles = { "hello-world-unsigned.docx", "hello-world-unsigned.pptx", "hello-world-unsigned.xlsx", "hello-world-office-2010-technical-preview-unsigned.docx" };
    for (String testFile : testFiles) {
        OPCPackage pkg = OPCPackage.open(testdata.getFile(testFile), PackageAccess.READ);
        SignatureConfig sic = new SignatureConfig();
        sic.setOpcPackage(pkg);
        SignatureInfo si = new SignatureInfo();
        si.setSignatureConfig(sic);
        List<X509Certificate> result = new ArrayList<X509Certificate>();
        for (SignaturePart sp : si.getSignatureParts()) {
            if (sp.validate()) {
                result.add(sp.getSigner());
            }
        }
        pkg.revert();
        pkg.close();
        assertNotNull(result);
        assertTrue(result.isEmpty());
    }
}
Also used : SignatureInfo(org.apache.poi.poifs.crypt.dsig.SignatureInfo) SignatureConfig(org.apache.poi.poifs.crypt.dsig.SignatureConfig) ArrayList(java.util.ArrayList) SignaturePart(org.apache.poi.poifs.crypt.dsig.SignatureInfo.SignaturePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) X509Certificate(java.security.cert.X509Certificate) Test(org.junit.Test)

Example 33 with OPCPackage

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

the class TestSignatureInfo method testCertChain.

@Test
public void testCertChain() throws Exception {
    KeyStore keystore = KeyStore.getInstance("PKCS12");
    String password = "test";
    InputStream is = testdata.openResourceAsStream("chaintest.pfx");
    keystore.load(is, password.toCharArray());
    is.close();
    Key key = keystore.getKey("poitest", password.toCharArray());
    Certificate[] chainList = keystore.getCertificateChain("poitest");
    List<X509Certificate> certChain = new ArrayList<X509Certificate>();
    for (Certificate c : chainList) {
        certChain.add((X509Certificate) c);
    }
    x509 = certChain.get(0);
    keyPair = new KeyPair(x509.getPublicKey(), (PrivateKey) key);
    String testFile = "hello-world-unsigned.xlsx";
    OPCPackage pkg = OPCPackage.open(copy(testdata.getFile(testFile)), PackageAccess.READ_WRITE);
    SignatureConfig signatureConfig = new SignatureConfig();
    signatureConfig.setKey(keyPair.getPrivate());
    signatureConfig.setSigningCertificateChain(certChain);
    Calendar oldCal = LocaleUtil.getLocaleCalendar(2007, 7, 1);
    signatureConfig.setExecutionTime(oldCal.getTime());
    signatureConfig.setDigestAlgo(HashAlgorithm.sha1);
    signatureConfig.setOpcPackage(pkg);
    SignatureInfo si = new SignatureInfo();
    si.setSignatureConfig(signatureConfig);
    si.confirmSignature();
    for (SignaturePart sp : si.getSignatureParts()) {
        assertTrue("Could not validate", sp.validate());
        X509Certificate signer = sp.getSigner();
        assertNotNull("signer undefined?!", signer);
        List<X509Certificate> certChainRes = sp.getCertChain();
        assertEquals(3, certChainRes.size());
    }
    pkg.close();
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SignatureConfig(org.apache.poi.poifs.crypt.dsig.SignatureConfig) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) SignatureInfo(org.apache.poi.poifs.crypt.dsig.SignatureInfo) SignaturePart(org.apache.poi.poifs.crypt.dsig.SignatureInfo.SignaturePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Key(java.security.Key) PrivateKey(java.security.PrivateKey) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate) Test(org.junit.Test)

Example 34 with OPCPackage

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

the class TestSignatureInfo method testNonSha1.

@Test
public void testNonSha1() throws Exception {
    String testFile = "hello-world-unsigned.xlsx";
    initKeyPair("Test", "CN=Test");
    SignatureConfig signatureConfig = new SignatureConfig();
    signatureConfig.setKey(keyPair.getPrivate());
    signatureConfig.setSigningCertificateChain(Collections.singletonList(x509));
    HashAlgorithm[] testAlgo = { HashAlgorithm.sha224, HashAlgorithm.sha256, HashAlgorithm.sha384, HashAlgorithm.sha512, HashAlgorithm.ripemd160 };
    for (HashAlgorithm ha : testAlgo) {
        OPCPackage pkg = null;
        try {
            signatureConfig.setDigestAlgo(ha);
            pkg = OPCPackage.open(copy(testdata.getFile(testFile)), PackageAccess.READ_WRITE);
            signatureConfig.setOpcPackage(pkg);
            SignatureInfo si = new SignatureInfo();
            si.setSignatureConfig(signatureConfig);
            si.confirmSignature();
            boolean b = si.verifySignature();
            assertTrue("Signature not correctly calculated for " + ha, b);
        } finally {
            if (pkg != null) {
                pkg.close();
            }
        }
    }
}
Also used : SignatureInfo(org.apache.poi.poifs.crypt.dsig.SignatureInfo) SignatureConfig(org.apache.poi.poifs.crypt.dsig.SignatureConfig) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Example 35 with OPCPackage

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

the class TestSignatureInfo method getMultiSigners.

@Test
public void getMultiSigners() throws Exception {
    String testFile = "hello-world-signed-twice.docx";
    OPCPackage pkg = OPCPackage.open(testdata.getFile(testFile), PackageAccess.READ);
    try {
        SignatureConfig sic = new SignatureConfig();
        sic.setOpcPackage(pkg);
        SignatureInfo si = new SignatureInfo();
        si.setSignatureConfig(sic);
        List<X509Certificate> result = new ArrayList<X509Certificate>();
        for (SignaturePart sp : si.getSignatureParts()) {
            if (sp.validate()) {
                result.add(sp.getSigner());
            }
        }
        assertNotNull(result);
        assertEquals("test-file: " + testFile, 2, result.size());
        X509Certificate signer1 = result.get(0);
        X509Certificate signer2 = result.get(1);
        LOG.log(POILogger.DEBUG, "signer 1: " + signer1.getSubjectX500Principal());
        LOG.log(POILogger.DEBUG, "signer 2: " + signer2.getSubjectX500Principal());
        boolean b = si.verifySignature();
        assertTrue("test-file: " + testFile, b);
        pkg.revert();
    } finally {
        pkg.close();
    }
}
Also used : SignatureInfo(org.apache.poi.poifs.crypt.dsig.SignatureInfo) SignatureConfig(org.apache.poi.poifs.crypt.dsig.SignatureConfig) ArrayList(java.util.ArrayList) SignaturePart(org.apache.poi.poifs.crypt.dsig.SignatureInfo.SignaturePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) X509Certificate(java.security.cert.X509Certificate) 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