]> source.dussan.org Git - poi.git/commitdiff
starting tsp tests / removed mockito stuff again
authorAndreas Beeker <kiwiwings@apache.org>
Sun, 17 Aug 2014 19:53:34 +0000 (19:53 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Sun, 17 Aug 2014 19:53:34 +0000 (19:53 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/xml_signature@1618515 13f79535-47bb-0310-9956-ffa450edef68

.classpath
build.xml
src/ooxml/java/org/apache/poi/poifs/crypt/dsig/services/TSPTimeStampService.java
src/ooxml/testcases/org/apache/poi/poifs/crypt/TestSignatureInfo.java

index a95bc314e124b977b7c040007522ef3d002d9cd5..2445269b1918850a627b55686f63aec83eb921ad 100644 (file)
@@ -24,8 +24,6 @@
        <classpathentry kind="lib" path="lib/junit-4.11.jar"/>\r
        <classpathentry kind="lib" path="ooxml-lib/ooxml-schemas-1.1.jar" sourcepath="ooxml-lib/ooxml-schemas-src-1.1.jar"/>\r
        <classpathentry kind="lib" path="ooxml-lib/ooxml-encryption-1.2.jar" sourcepath="ooxml-lib/ooxml-encryption-src-1.2.jar"/>\r
-       <classpathentry kind="lib" path="lib/com.springsource.org.objenesis-1.0.0.jar"/>\r
        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>\r
-       <classpathentry kind="lib" path="lib/mockito-core-1.9.5.jar" sourcepath="E:/tmp/mavenRepo/org/mockito/mockito-core/1.9.5/mockito-core-1.9.5-sources.jar"/>\r
        <classpathentry kind="output" path="build/eclipse"/>\r
 </classpath>\r
index 0e374e51fa528708c062fc1d759077146703c727..a83ccaea6d5f5ad5885f4961d4923bff1db00747 100644 (file)
--- a/build.xml
+++ b/build.xml
@@ -151,10 +151,6 @@ under the License.
        <property name="test.bouncycastle-prov.url" value="${repository.m2}/maven2/org/bouncycastle/bcprov-ext-jdk15on/1.51/bcprov-ext-jdk15on-1.51.jar"/>
        <property name="test.bouncycastle-pkix.jar" location="${main.lib}/bcpkix-jdk15on-151.jar"/>
        <property name="test.bouncycastle-pkix.url" value="${repository.m2}/maven2/org/bouncycastle/bcpkix-jdk15on/1.51/bcpkix-jdk15on-151.jar"/>
-       <property name="test.mockito.jar" location="${main.lib}/mockito-core-1.9.5.jar"/>
-       <property name="test.mockito.url" value="${repository.m2}/maven2/org/mockito/mockito-core/1.9.5/mockito-core-1.9.5.jar"/>
-       <property name="test.objenesis.jar" location="${main.lib}/com.springsource.org.objenesis-1.0.0.jar"/>
-       <property name="test.objenesis.url" value="http://repository.springsource.com/ivy/bundles/external/org.objenesis/com.springsource.org.objenesis/1.0.0/com.springsource.org.objenesis-1.0.0.jar"/>
 
        <!-- jars in the lib-ooxml directory, see the fetch-ooxml-jars target-->
     <property name="ooxml.xmlbeans23.jar" location="${ooxml.lib}/xmlbeans-2.3.0.jar"/>
@@ -267,8 +263,6 @@ under the License.
         <pathelement location="${ooxml.output.dir}"/>
         <pathelement location="${ooxml.output.test.dir}"/>
         <pathelement location="${main.output.test.dir}"/>
-       <pathelement location="${test.mockito.jar}"/>
-       <pathelement location="${test.objenesis.jar}"/>
     </path>
 
     <path id="ooxml-lite.classpath">
@@ -443,14 +437,6 @@ under the License.
             <param name="sourcefile" value="${rat.url}"/>
             <param name="destfile" value="${rat.jar}"/>
         </antcall>
-        <antcall target="downloadfile">
-            <param name="sourcefile" value="${test.mockito.url}"/>
-            <param name="destfile" value="${test.mockito.jar}"/>
-        </antcall>
-        <antcall target="downloadfile">
-            <param name="sourcefile" value="${test.objenesis.url}"/>
-            <param name="destfile" value="${test.objenesis.jar}"/>
-        </antcall>
         <antcall target="downloadfile">
             <param name="sourcefile" value="${test.bouncycastle-prov.url}"/>
             <param name="destfile" value="${test.bouncycastle-prov.jar}"/>
index d0ba961eb9c89943b7f33ece922fb7be2aa819c8..100c0f7f3f27aa90c19c95c5bf00587e4f2c34c4 100644 (file)
@@ -24,6 +24,9 @@
 \r
 package org.apache.poi.poifs.crypt.dsig.services;\r
 \r
+import static org.apache.poi.poifs.crypt.dsig.HorribleProxy.createProxy;\r
+import static org.apache.poi.poifs.crypt.dsig.HorribleProxy.newProxy;\r
+\r
 import java.io.ByteArrayInputStream;\r
 import java.io.ByteArrayOutputStream;\r
 import java.io.OutputStream;\r
@@ -48,6 +51,7 @@ import javax.xml.bind.DatatypeConverter;
 \r
 import org.apache.commons.codec.binary.Hex;\r
 import org.apache.poi.poifs.crypt.CryptoFunctions;\r
+import org.apache.poi.poifs.crypt.HashAlgorithm;\r
 import org.apache.poi.poifs.crypt.dsig.HorribleProxies.ASN1InputStreamIf;\r
 import org.apache.poi.poifs.crypt.dsig.HorribleProxies.ASN1OctetStringIf;\r
 import org.apache.poi.poifs.crypt.dsig.HorribleProxies.AuthorityKeyIdentifierIf;\r
@@ -64,7 +68,6 @@ import org.apache.poi.poifs.crypt.dsig.HorribleProxies.TimeStampRequestIf;
 import org.apache.poi.poifs.crypt.dsig.HorribleProxies.TimeStampResponseIf;\r
 import org.apache.poi.poifs.crypt.dsig.HorribleProxies.TimeStampTokenIf;\r
 import org.apache.poi.poifs.crypt.dsig.HorribleProxies.X509CertificateHolderIf;\r
-import org.apache.poi.poifs.crypt.dsig.HorribleProxy;\r
 import org.apache.poi.util.IOUtils;\r
 import org.apache.poi.util.POILogFactory;\r
 import org.apache.poi.util.POILogger;\r
@@ -83,7 +86,7 @@ public class TSPTimeStampService implements TimeStampService {
         CryptoFunctions.registerBouncyCastle();\r
     }\r
 \r
-    public static final String DEFAULT_USER_AGENT = "eID Applet Service TSP Client";\r
+    public static final String DEFAULT_USER_AGENT = "POI XmlSign Service TSP Client";\r
 \r
     private final String tspServiceUrl;\r
 \r
@@ -101,7 +104,7 @@ public class TSPTimeStampService implements TimeStampService {
 \r
     private int proxyPort;\r
 \r
-    private String digestAlgo;\r
+    private HashAlgorithm digestAlgo;\r
 \r
     private String digestAlgoOid;\r
 \r
@@ -143,9 +146,8 @@ public class TSPTimeStampService implements TimeStampService {
         } else {\r
             this.userAgent = DEFAULT_USER_AGENT;\r
         }\r
-\r
-        this.digestAlgo = "SHA-1";\r
-        this.digestAlgoOid = "1.3.14.3.2.26";\r
+        \r
+        setDigestAlgo(HashAlgorithm.sha1);\r
     }\r
 \r
     /**\r
@@ -183,16 +185,21 @@ public class TSPTimeStampService implements TimeStampService {
      * \r
      * @param digestAlgo\r
      */\r
-    public void setDigestAlgo(String digestAlgo) {\r
-        if ("SHA-1".equals(digestAlgo)) {\r
-            this.digestAlgoOid = "1.3.14.3.2.26";\r
-        } else if ("SHA-256".equals(digestAlgo)) {\r
-            this.digestAlgoOid = "2.16.840.1.101.3.4.2.1";\r
-        } else if ("SHA-384".equals(digestAlgo)) {\r
-            this.digestAlgoOid = "2.16.840.1.101.3.4.2.2";\r
-        } else if ("SHA-512".equals(digestAlgo)) {\r
-            this.digestAlgoOid = "2.16.840.1.101.3.4.2.3";\r
-        } else {\r
+    public void setDigestAlgo(HashAlgorithm digestAlgo) {\r
+        switch (digestAlgo) {\r
+        case sha1:\r
+            digestAlgoOid = "1.3.14.3.2.26";\r
+            break;\r
+        case sha256:\r
+            digestAlgoOid = "2.16.840.1.101.3.4.2.1";\r
+            break;\r
+        case sha384:\r
+            digestAlgoOid = "2.16.840.1.101.3.4.2.2";\r
+            break;\r
+        case sha512:\r
+            digestAlgoOid = "2.16.840.1.101.3.4.2.3";\r
+            break;\r
+        default:\r
             throw new IllegalArgumentException("unsupported digest algo: " + digestAlgo);\r
         }\r
 \r
@@ -222,13 +229,12 @@ public class TSPTimeStampService implements TimeStampService {
     public byte[] timeStamp(byte[] data, RevocationData revocationData)\r
             throws Exception {\r
         // digest the message\r
-        MessageDigest messageDigest = MessageDigest\r
-                .getInstance(this.digestAlgo);\r
+        MessageDigest messageDigest = CryptoFunctions.getMessageDigest(this.digestAlgo);\r
         byte[] digest = messageDigest.digest(data);\r
 \r
         // generate the TSP request\r
         BigInteger nonce = new BigInteger(128, new SecureRandom());\r
-        TimeStampRequestGeneratorIf requestGenerator = HorribleProxy.newProxy(TimeStampRequestGeneratorIf.class);\r
+        TimeStampRequestGeneratorIf requestGenerator = newProxy(TimeStampRequestGeneratorIf.class);\r
         requestGenerator.setCertReq(true);\r
         if (null != this.requestPolicy) {\r
             requestGenerator.setReqPolicy(this.requestPolicy);\r
@@ -250,7 +256,8 @@ public class TSPTimeStampService implements TimeStampService {
 \r
         huc.setDoOutput(true); // also sets method to POST.\r
         huc.setRequestProperty("User-Agent", this.userAgent);\r
-        huc.setRequestProperty("Content-Type", "application/timestamp-query;charset=ISO-8859-1");\r
+        // "application/timestamp-query;charset=ISO-8859-1"\r
+        huc.setRequestProperty("Content-Type", "application/timestamp-request");\r
         \r
         OutputStream hucOut = huc.getOutputStream();\r
         hucOut.write(encodedRequest);\r
@@ -274,7 +281,8 @@ public class TSPTimeStampService implements TimeStampService {
         IOUtils.copy(huc.getInputStream(), bos);\r
         LOG.log(POILogger.DEBUG, "response content: ", bos.toString());\r
         \r
-        if (!contentType.startsWith("application/timestamp-reply")) {\r
+        // "application/timestamp-reply"\r
+        if (!contentType.startsWith("application/timestamp-response")) {\r
             throw new RuntimeException("invalid Content-Type: " + contentType);\r
         }\r
         \r
@@ -283,7 +291,7 @@ public class TSPTimeStampService implements TimeStampService {
         }\r
 \r
         // TSP response parsing and validation\r
-        TimeStampResponseIf timeStampResponse = HorribleProxy.newProxy(TimeStampResponseIf.class, bos.toByteArray());\r
+        TimeStampResponseIf timeStampResponse = newProxy(TimeStampResponseIf.class, bos.toByteArray());\r
         timeStampResponse.validate(request);\r
 \r
         if (0 != timeStampResponse.getStatus()) {\r
@@ -346,10 +354,10 @@ public class TSPTimeStampService implements TimeStampService {
         } while (null != certificate);\r
 \r
         // verify TSP signer signature\r
-        X509CertificateHolderIf holder = HorribleProxy.newProxy(X509CertificateHolderIf.class, tspCertificateChain.get(0).getEncoded());\r
-        DefaultDigestAlgorithmIdentifierFinderIf finder = HorribleProxy.newProxy(DefaultDigestAlgorithmIdentifierFinderIf.class);\r
-        BcDigestCalculatorProviderIf calculator = HorribleProxy.newProxy(BcDigestCalculatorProviderIf.class);\r
-        BcRSASignerInfoVerifierBuilderIf verifierBuilder = HorribleProxy.newProxy(BcRSASignerInfoVerifierBuilderIf.class, finder, calculator);\r
+        X509CertificateHolderIf holder = newProxy(X509CertificateHolderIf.class, tspCertificateChain.get(0).getEncoded());\r
+        DefaultDigestAlgorithmIdentifierFinderIf finder = newProxy(DefaultDigestAlgorithmIdentifierFinderIf.class);\r
+        BcDigestCalculatorProviderIf calculator = newProxy(BcDigestCalculatorProviderIf.class);\r
+        BcRSASignerInfoVerifierBuilderIf verifierBuilder = newProxy(BcRSASignerInfoVerifierBuilderIf.class, finder, calculator);\r
         SignerInformationVerifierIf verifier = verifierBuilder.build(holder);\r
         \r
         timeStampToken.validate(verifier);\r
@@ -369,10 +377,10 @@ public class TSPTimeStampService implements TimeStampService {
         byte[] extvalue = cert.getExtensionValue("2.5.29.14");\r
         if (extvalue == null) return null;\r
 \r
-        ASN1InputStreamIf keyCntStream = HorribleProxy.newProxy(ASN1InputStreamIf.class, new ByteArrayInputStream(extvalue));\r
-        ASN1OctetStringIf cntStr = HorribleProxy.createProxy(ASN1OctetStringIf.class, "getInstance", keyCntStream.readObject$Object());\r
-        ASN1InputStreamIf keyIdStream = HorribleProxy.newProxy(ASN1InputStreamIf.class, new ByteArrayInputStream(cntStr.getOctets()));\r
-        SubjectKeyIdentifierIf keyId = HorribleProxy.createProxy(SubjectKeyIdentifierIf.class, "getInstance", keyIdStream.readObject$Object());\r
+        ASN1InputStreamIf keyCntStream = newProxy(ASN1InputStreamIf.class, new ByteArrayInputStream(extvalue));\r
+        ASN1OctetStringIf cntStr = createProxy(ASN1OctetStringIf.class, "getInstance", keyCntStream.readObject$Object());\r
+        ASN1InputStreamIf keyIdStream = newProxy(ASN1InputStreamIf.class, new ByteArrayInputStream(cntStr.getOctets()));\r
+        SubjectKeyIdentifierIf keyId = createProxy(SubjectKeyIdentifierIf.class, "getInstance", keyIdStream.readObject$Object());\r
 \r
         return keyId.getKeyIdentifier();\r
     }\r
@@ -382,10 +390,10 @@ public class TSPTimeStampService implements TimeStampService {
         byte[] extvalue = cert.getExtensionValue("2.5.29.35");\r
         if (extvalue == null) return null;\r
 \r
-        ASN1InputStreamIf keyCntStream = HorribleProxy.newProxy(ASN1InputStreamIf.class, new ByteArrayInputStream(extvalue));\r
+        ASN1InputStreamIf keyCntStream = newProxy(ASN1InputStreamIf.class, new ByteArrayInputStream(extvalue));\r
         DEROctetStringIf cntStr = keyCntStream.readObject$DERString();\r
-        ASN1InputStreamIf keyIdStream = HorribleProxy.newProxy(ASN1InputStreamIf.class, new ByteArrayInputStream(cntStr.getOctets()));\r
-        AuthorityKeyIdentifierIf keyId = HorribleProxy.newProxy(AuthorityKeyIdentifierIf.class, keyIdStream.readObject$Sequence());\r
+        ASN1InputStreamIf keyIdStream = newProxy(ASN1InputStreamIf.class, new ByteArrayInputStream(cntStr.getOctets()));\r
+        AuthorityKeyIdentifierIf keyId = newProxy(AuthorityKeyIdentifierIf.class, keyIdStream.readObject$Sequence());\r
         \r
         return keyId.getKeyIdentifier();\r
     }\r
index 05dad0b1efa81ca2dfd1e285d94c8d3c2534a5b7..b329474e5ae96f8e73a24a44278b363a21125d9d 100644 (file)
@@ -26,12 +26,6 @@ package org.apache.poi.poifs.crypt;
 import static org.junit.Assert.assertEquals;\r
 import static org.junit.Assert.assertNotNull;\r
 import static org.junit.Assert.assertTrue;\r
-import static org.mockito.Matchers.any;\r
-import static org.mockito.Matchers.eq;\r
-import static org.mockito.Mockito.mock;\r
-import static org.mockito.Mockito.times;\r
-import static org.mockito.Mockito.verify;\r
-import static org.mockito.Mockito.when;\r
 \r
 import java.io.File;\r
 import java.io.FileInputStream;\r
@@ -73,7 +67,9 @@ import org.apache.poi.poifs.crypt.dsig.facets.XAdESSignatureFacet;
 import org.apache.poi.poifs.crypt.dsig.facets.XAdESXLSignatureFacet;\r
 import org.apache.poi.poifs.crypt.dsig.services.RevocationData;\r
 import org.apache.poi.poifs.crypt.dsig.services.RevocationDataService;\r
+import org.apache.poi.poifs.crypt.dsig.services.TSPTimeStampService;\r
 import org.apache.poi.poifs.crypt.dsig.services.TimeStampService;\r
+import org.apache.poi.poifs.crypt.dsig.services.TimeStampServiceValidator;\r
 import org.apache.poi.poifs.crypt.dsig.services.XmlSignatureService;\r
 import org.apache.poi.poifs.crypt.dsig.spi.DigestInfo;\r
 import org.apache.poi.util.IOUtils;\r
@@ -84,8 +80,6 @@ import org.etsi.uri.x01903.v13.DigestAlgAndValueType;
 import org.etsi.uri.x01903.v13.QualifyingPropertiesType;\r
 import org.junit.BeforeClass;\r
 import org.junit.Test;\r
-import org.mockito.invocation.InvocationOnMock;\r
-import org.mockito.stubbing.Answer;\r
 import org.w3.x2000.x09.xmldsig.SignatureDocument;\r
 \r
 public class TestSignatureInfo {\r
@@ -210,23 +204,38 @@ public class TestSignatureInfo {
         String testFile = "hello-world-unsigned.xlsx";\r
         OPCPackage pkg = OPCPackage.open(copy(testdata.getFile(testFile)), PackageAccess.READ_WRITE);\r
 \r
+        initKeyPair("Test", "CN=Test");\r
+        \r
         // setup\r
         EnvelopedSignatureFacet envelopedSignatureFacet = new EnvelopedSignatureFacet();\r
         KeyInfoSignatureFacet keyInfoSignatureFacet = new KeyInfoSignatureFacet(true, false, false);\r
         SignaturePolicyService signaturePolicyService = null;\r
         XAdESSignatureFacet xadesSignatureFacet = new XAdESSignatureFacet(null, null, signaturePolicyService);\r
+        final X509CRL crl = PkiTestUtils.generateCrl(x509, keyPair.getPrivate());\r
 \r
+//        TimeStampService timeStampService = new TimeStampService(){\r
+//            public byte[] timeStamp(byte[] data, RevocationData revocationData) throws Exception {\r
+//                revocationData.addCRL(crl);\r
+//                return "time-stamp-token".getBytes();                \r
+//            }\r
+//        };\r
+\r
+        // http://timestamping.edelweb.fr/service/tsp\r
+        // http://tsa.belgium.be/connect\r
+        String tspServiceUrl = "http://timestamping.edelweb.fr/service/tsp";\r
+        TimeStampServiceValidator tspValidator = new TimeStampServiceValidator() {\r
+            @Override\r
+            public void validate(List<X509Certificate> certificateChain,\r
+            RevocationData revocationData) throws Exception {\r
+                for (X509Certificate certificate : certificateChain) {\r
+                    LOG.log(POILogger.DEBUG, "certificate: " + certificate.getSubjectX500Principal());\r
+                    LOG.log(POILogger.DEBUG, "validity: " + certificate.getNotBefore() + " - " + certificate.getNotAfter());\r
+                }\r
+            }\r
+        };\r
         \r
-        TimeStampService mockTimeStampService = mock(TimeStampService.class);\r
-        RevocationDataService mockRevocationDataService = mock(RevocationDataService.class);\r
-\r
-        XAdESXLSignatureFacet xadesXLSignatureFacet = new XAdESXLSignatureFacet(\r
-                mockTimeStampService, mockRevocationDataService);\r
-        XmlSignatureService testedInstance = new XmlSignatureService(HashAlgorithm.sha1, pkg);\r
-        testedInstance.addSignatureFacet(envelopedSignatureFacet, keyInfoSignatureFacet,\r
-                xadesSignatureFacet, xadesXLSignatureFacet);\r
+        TimeStampService timeStampService = new TSPTimeStampService(tspServiceUrl, tspValidator);\r
         \r
-        initKeyPair("Test", "CN=Test");\r
         List<X509Certificate> certificateChain = new ArrayList<X509Certificate>();\r
         /*\r
          * We need at least 2 certificates for the XAdES-C complete certificate\r
@@ -235,25 +244,25 @@ public class TestSignatureInfo {
         certificateChain.add(x509);\r
         certificateChain.add(x509);\r
         \r
-        RevocationData revocationData = new RevocationData();\r
-        final X509CRL crl = PkiTestUtils.generateCrl(x509, keyPair.getPrivate());\r
+        final RevocationData revocationData = new RevocationData();\r
         revocationData.addCRL(crl);\r
         OCSPRespIf ocspResp = PkiTestUtils.createOcspResp(x509, false,\r
                 x509, x509, keyPair.getPrivate(), "SHA1withRSA", cal.getTimeInMillis());\r
         revocationData.addOCSP(ocspResp.getEncoded());\r
+\r
+        RevocationDataService revocationDataService = new RevocationDataService(){\r
+            public RevocationData getRevocationData(List<X509Certificate> certificateChain) {\r
+                return revocationData;\r
+            }\r
+        };\r
+\r
+        XAdESXLSignatureFacet xadesXLSignatureFacet = new XAdESXLSignatureFacet(\r
+                timeStampService, revocationDataService);\r
+        XmlSignatureService testedInstance = new XmlSignatureService(HashAlgorithm.sha1, pkg);\r
+        testedInstance.addSignatureFacet(envelopedSignatureFacet, keyInfoSignatureFacet,\r
+                xadesSignatureFacet, xadesXLSignatureFacet);\r
         \r
-        when(mockTimeStampService.timeStamp(any(byte[].class), any(RevocationData.class)))\r
-        .thenAnswer(new Answer<byte[]>(){\r
-            public byte[] answer(InvocationOnMock invocation) throws Throwable {\r
-                Object[] arguments = invocation.getArguments();\r
-                RevocationData revocationData = (RevocationData) arguments[1];\r
-                revocationData.addCRL(crl);\r
-                return "time-stamp-token".getBytes();\r
-            }            \r
-        });\r
         \r
-        when(mockRevocationDataService.getRevocationData(eq(certificateChain)))\r
-        .thenReturn(revocationData);\r
         \r
         // operate\r
         DigestInfo digestInfo = testedInstance.preSign(null, certificateChain, null, null, null);\r
@@ -279,10 +288,6 @@ public class TestSignatureInfo {
         // Operate: postSign\r
         testedInstance.postSign(signatureValue, certificateChain);\r
         \r
-        // verify\r
-        verify(mockTimeStampService, times(2)).timeStamp(any(byte[].class), any(RevocationData.class));\r
-        verify(mockRevocationDataService).getRevocationData(certificateChain);\r
-        \r
         DOMValidateContext domValidateContext = new DOMValidateContext(\r
                 KeySelector.singletonKeySelector(keyPair.getPublic()),\r
                 testedInstance.getSignatureDocument().getDomNode());\r
@@ -389,4 +394,5 @@ public class TestSignatureInfo {
         fos.close();\r
         return tmpFile;\r
     }\r
+\r
 }\r