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.

TestOOXMLSignatureVerifier.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. /*
  16. * Based on the eID Applet Project code.
  17. * Original Copyright (C) 2008-2009 FedICT.
  18. */
  19. package org.apache.poi.ooxml.signature.service.signer;
  20. import java.io.InputStream;
  21. import java.net.URL;
  22. import java.security.cert.X509Certificate;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import java.util.logging.Level;
  26. import junit.framework.TestCase;
  27. import org.apache.commons.logging.Log;
  28. import org.apache.commons.logging.LogFactory;
  29. import org.apache.poi.POIXMLDocument;
  30. import org.apache.poi.ooxml.signature.service.signer.ooxml.OOXMLProvider;
  31. import org.apache.poi.ooxml.signature.service.signer.ooxml.OOXMLSignatureVerifier;
  32. import org.apache.poi.openxml4j.opc.OPCPackage;
  33. import org.apache.poi.openxml4j.opc.PackagePart;
  34. import org.apache.poi.openxml4j.opc.signature.PackageDigitalSignatureManager;
  35. public class TestOOXMLSignatureVerifier extends TestCase {
  36. private static final Log LOG = LogFactory.getLog(TestOOXMLSignatureVerifier.class);
  37. static {
  38. OOXMLProvider.install();
  39. }
  40. public void testIsOOXMLDocument() throws Exception {
  41. // setup
  42. URL url = TestOOXMLSignatureVerifier.class.getResource("/hello-world-unsigned.docx");
  43. // operate
  44. boolean result = OOXMLSignatureVerifier.isOOXML(url);
  45. // verify
  46. assertTrue(result);
  47. }
  48. public void testPOI() throws Exception {
  49. // setup
  50. InputStream inputStream = TestOOXMLSignatureVerifier.class.getResourceAsStream("/hello-world-unsigned.docx");
  51. // operate
  52. boolean result = POIXMLDocument.hasOOXMLHeader(inputStream);
  53. // verify
  54. assertTrue(result);
  55. }
  56. public void testOPC() throws Exception {
  57. // setup
  58. InputStream inputStream = TestOOXMLSignatureVerifier.class.getResourceAsStream("/hello-world-signed.docx");
  59. // operate
  60. OPCPackage opcPackage = OPCPackage.open(inputStream);
  61. ArrayList<PackagePart> parts = opcPackage.getParts();
  62. for (PackagePart part : parts) {
  63. LOG.debug("part name: " + part.getPartName().getName());
  64. LOG.debug("part content type: " + part.getContentType());
  65. }
  66. ArrayList<PackagePart> signatureParts = opcPackage.getPartsByContentType("application/vnd.openxmlformats-package.digital-signature-xmlsignature+xml");
  67. assertFalse(signatureParts.isEmpty());
  68. PackagePart signaturePart = signatureParts.get(0);
  69. LOG.debug("signature part class type: " + signaturePart.getClass().getName());
  70. PackageDigitalSignatureManager packageDigitalSignatureManager = new PackageDigitalSignatureManager();
  71. // yeah... POI implementation still missing
  72. }
  73. public void testGetSignerUnsigned() throws Exception {
  74. // setup
  75. URL url = TestOOXMLSignatureVerifier.class.getResource("/hello-world-unsigned.docx");
  76. // operate
  77. List<X509Certificate> result = OOXMLSignatureVerifier.getSigners(url);
  78. // verify
  79. assertNotNull(result);
  80. assertTrue(result.isEmpty());
  81. }
  82. public void testGetSignerOffice2010Unsigned() throws Exception {
  83. // setup
  84. URL url = TestOOXMLSignatureVerifier.class.getResource("/hello-world-office-2010-technical-preview-unsigned.docx");
  85. // operate
  86. List<X509Certificate> result = OOXMLSignatureVerifier.getSigners(url);
  87. // verify
  88. assertNotNull(result);
  89. assertTrue(result.isEmpty());
  90. }
  91. public void testGetSignerUnsignedPowerpoint() throws Exception {
  92. // setup
  93. URL url = TestOOXMLSignatureVerifier.class.getResource("/hello-world-unsigned.pptx");
  94. // operate
  95. List<X509Certificate> result = OOXMLSignatureVerifier.getSigners(url);
  96. // verify
  97. assertNotNull(result);
  98. assertTrue(result.isEmpty());
  99. }
  100. public void testGetSignerUnsignedExcel() throws Exception {
  101. // setup
  102. URL url = TestOOXMLSignatureVerifier.class.getResource("/hello-world-unsigned.xlsx");
  103. // operate
  104. List<X509Certificate> result = OOXMLSignatureVerifier.getSigners(url);
  105. // verify
  106. assertNotNull(result);
  107. assertTrue(result.isEmpty());
  108. }
  109. public void testGetSigner() throws Exception {
  110. // setup
  111. URL url = TestOOXMLSignatureVerifier.class.getResource("/hello-world-signed.docx");
  112. // operate
  113. List<X509Certificate> result = OOXMLSignatureVerifier.getSigners(url);
  114. // verify
  115. assertNotNull(result);
  116. assertEquals(1, result.size());
  117. X509Certificate signer = result.get(0);
  118. LOG.debug("signer: " + signer.getSubjectX500Principal());
  119. }
  120. public void testOffice2010TechnicalPreview() throws Exception {
  121. // setup
  122. URL url = TestOOXMLSignatureVerifier.class.getResource("/hello-world-office-2010-technical-preview.docx");
  123. // operate
  124. List<X509Certificate> result = OOXMLSignatureVerifier.getSigners(url);
  125. // verify
  126. assertNotNull(result);
  127. assertEquals(1, result.size());
  128. X509Certificate signer = result.get(0);
  129. LOG.debug("signer: " + signer.getSubjectX500Principal());
  130. }
  131. public void testGetSignerPowerpoint() throws Exception {
  132. // setup
  133. URL url = TestOOXMLSignatureVerifier.class.getResource("/hello-world-signed.pptx");
  134. // operate
  135. List<X509Certificate> result = OOXMLSignatureVerifier.getSigners(url);
  136. // verify
  137. assertNotNull(result);
  138. assertEquals(1, result.size());
  139. X509Certificate signer = result.get(0);
  140. LOG.debug("signer: " + signer.getSubjectX500Principal());
  141. }
  142. public void testGetSignerExcel() throws Exception {
  143. // setup
  144. URL url = TestOOXMLSignatureVerifier.class.getResource("/hello-world-signed.xlsx");
  145. // operate
  146. List<X509Certificate> result = OOXMLSignatureVerifier.getSigners(url);
  147. // verify
  148. assertNotNull(result);
  149. assertEquals(1, result.size());
  150. X509Certificate signer = result.get(0);
  151. LOG.debug("signer: " + signer.getSubjectX500Principal());
  152. }
  153. public void testGetSigners() throws Exception {
  154. // setup
  155. URL url = TestOOXMLSignatureVerifier.class.getResource("/hello-world-signed-twice.docx");
  156. // operate
  157. List<X509Certificate> result = OOXMLSignatureVerifier.getSigners(url);
  158. // verify
  159. assertNotNull(result);
  160. assertEquals(2, result.size());
  161. X509Certificate signer1 = result.get(0);
  162. X509Certificate signer2 = result.get(1);
  163. LOG.debug("signer 1: " + signer1.getSubjectX500Principal());
  164. LOG.debug("signer 2: " + signer2.getSubjectX500Principal());
  165. }
  166. public void testVerifySignature() throws Exception {
  167. java.util.logging.Logger logger = java.util.logging.Logger.getLogger("org.jcp.xml.dsig.internal.dom");
  168. logger.log(Level.FINE, "test");
  169. URL url = TestOOXMLSignatureVerifier.class.getResource("/hello-world-signed.docx");
  170. boolean validity = OOXMLSignatureVerifier.verifySignature(url);
  171. assertTrue(validity);
  172. }
  173. public void testTamperedFile() throws Exception {
  174. java.util.logging.Logger logger = java.util.logging.Logger.getLogger("org.jcp.xml.dsig.internal.dom");
  175. logger.log(Level.FINE, "test");
  176. URL url = TestOOXMLSignatureVerifier.class.getResource("/invalidsig.docx");
  177. boolean validity = OOXMLSignatureVerifier.verifySignature(url);
  178. assertFalse(validity);
  179. }
  180. }