removed new dependency on joda

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@825294 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Josh Micich 2009-10-14 21:32:06 +00:00
parent 1833764495
commit ccf7a6d89f
7 changed files with 201 additions and 115 deletions

View File

@ -142,8 +142,6 @@ under the License.
<property name="ooxml.xalan.url" value="${repository.m2}/maven2/xalan/xalan/2.7.1/xalan-2.7.1.jar"/>
<property name="ooxml.xalan-serializer.jar" location="${ooxml.lib}/serializer-2.7.1.jar"/>
<property name="ooxml.xalan-serializer.url" value="${repository.m2}/maven2/xalan/serializer/2.7.1/serializer-2.7.1.jar"/>
<property name="ooxml.joda-time.jar" location="${ooxml.lib}/joda-time-1.6.jar"/>
<property name="ooxml.joda-time.url" value="${repository.m2}/maven2/joda-time/joda-time/1.6/joda-time-1.6.jar"/>
<!-- BouncyCastle is used only for OOXML Digital Signature tests -->
<property name="ooxml.bcprov.jar" location="${ooxml.lib}/bcprov-jdk15-140.jar"/>
<property name="ooxml.bcprov.url" value="${repository.m2}/maven2/bouncycastle/bcprov-jdk15/140/bcprov-jdk15-140.jar"/>
@ -375,7 +373,6 @@ under the License.
<available file="${ooxml.xmlsec.jar}"/>
<available file="${ooxml.xalan.jar}"/>
<available file="${ooxml.xalan-serializer.jar}"/>
<available file="${ooxml.joda-time.jar}"/>
<available file="${ooxml.bcprov.jar}"/>
</and>
<isset property="disconnected"/>
@ -419,10 +416,6 @@ under the License.
<param name="sourcefile" value="${ooxml.xalan-serializer.url}"/>
<param name="destfile" value="${ooxml.xalan-serializer.jar}"/>
</antcall>
<antcall target="downloadfile">
<param name="sourcefile" value="${ooxml.joda-time.url}"/>
<param name="destfile" value="${ooxml.joda-time.jar}"/>
</antcall>
<antcall target="downloadfile">
<param name="sourcefile" value="${ooxml.bcprov.url}"/>
<param name="destfile" value="${ooxml.bcprov.jar}"/>

View File

@ -1,4 +1,3 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
@ -29,8 +28,10 @@ import java.io.InputStream;
import java.net.URL;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
import java.util.TimeZone;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
@ -57,10 +58,6 @@ import org.apache.poi.ooxml.signature.service.signer.NoCloseInputStream;
import org.apache.poi.ooxml.signature.service.signer.SignatureAspect;
import org.apache.xml.security.utils.Constants;
import org.apache.xpath.XPathAPI;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
@ -73,19 +70,14 @@ import org.xml.sax.SAXException;
/**
* Office OpenXML Signature Aspect implementation.
*/
public class OOXMLSignatureAspect implements SignatureAspect {
final class OOXMLSignatureAspect implements SignatureAspect {
private static final Log LOG = LogFactory.getLog(OOXMLSignatureAspect.class);
private final AbstractOOXMLSignatureService signatureService;
private final AbstractOOXMLSignatureService _signatureService;
/**
* Main constructor.
*
* @param ooxmlUrl
*/
public OOXMLSignatureAspect(AbstractOOXMLSignatureService signatureService) {
this.signatureService = signatureService;
_signatureService = signatureService;
}
public void preSign(XMLSignatureFactory signatureFactory, Document document, String signatureId, List<Reference> references, List<XMLObject> objects)
@ -98,7 +90,7 @@ public class OOXMLSignatureAspect implements SignatureAspect {
private void addManifestObject(XMLSignatureFactory signatureFactory, Document document, String signatureId, List<Reference> references,
List<XMLObject> objects) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
Manifest manifest = constructManifest(signatureFactory, document);
Manifest manifest = constructManifest(signatureFactory);
String objectId = "idPackageObject"; // really has to be this value.
List<XMLStructure> objectContent = new LinkedList<XMLStructure>();
objectContent.add(manifest);
@ -112,12 +104,12 @@ public class OOXMLSignatureAspect implements SignatureAspect {
references.add(reference);
}
private Manifest constructManifest(XMLSignatureFactory signatureFactory, Document document) throws NoSuchAlgorithmException,
private Manifest constructManifest(XMLSignatureFactory signatureFactory) throws NoSuchAlgorithmException,
InvalidAlgorithmParameterException {
List<Reference> manifestReferences = new LinkedList<Reference>();
try {
addRelationshipsReferences(signatureFactory, document, manifestReferences);
addRelationshipsReferences(signatureFactory, manifestReferences);
} catch (Exception e) {
throw new RuntimeException("error: " + e.getMessage(), e);
}
@ -145,7 +137,7 @@ public class OOXMLSignatureAspect implements SignatureAspect {
return manifest;
}
private void addSignatureTime(XMLSignatureFactory signatureFactory, Document document, String signatureId, List<XMLStructure> objectContent) {
private static void addSignatureTime(XMLSignatureFactory signatureFactory, Document document, String signatureId, List<XMLStructure> objectContent) {
/*
* SignatureTime
*/
@ -155,9 +147,7 @@ public class OOXMLSignatureAspect implements SignatureAspect {
formatElement.setTextContent("YYYY-MM-DDThh:mm:ssTZD");
signatureTimeElement.appendChild(formatElement);
Element valueElement = document.createElementNS("http://schemas.openxmlformats.org/package/2006/digital-signature", "mdssi:Value");
DateTime dateTime = new DateTime(DateTimeZone.UTC);
DateTimeFormatter fmt = ISODateTimeFormat.dateTimeNoMillis();
String now = fmt.print(dateTime);
String now = formatTimestampAsISO8601(System.currentTimeMillis());
LOG.debug("now: " + now);
valueElement.setTextContent(now);
signatureTimeElement.appendChild(valueElement);
@ -172,6 +162,34 @@ public class OOXMLSignatureAspect implements SignatureAspect {
objectContent.add(signatureProperties);
}
/**
* @return text formatted "YYYY-MM-DDThh:mm:ssTZD"
*/
static String formatTimestampAsISO8601(long ts) {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(ts);
c.setTimeZone(TimeZone.getTimeZone("UTC"));
char[] buf = "yyyy-mm-ddThh:mm:ssZ".toCharArray();
itoa(buf, 0, 4, c.get(Calendar.YEAR));
itoa(buf, 5, 2, c.get(Calendar.MONTH)+1);
itoa(buf, 8, 2, c.get(Calendar.DAY_OF_MONTH));
itoa(buf, 11, 2, c.get(Calendar.HOUR_OF_DAY));
itoa(buf, 14, 2, c.get(Calendar.MINUTE));
itoa(buf, 17, 2, c.get(Calendar.SECOND));
return new String(buf);
}
private static void itoa(char[] buf, int start, int len, int value) {
int acc = value;
int i=start+len-1;
while (i>=start) {
int d = acc % 10;
acc /= 10;
buf[i] = (char) ('0' + d);
i--;
}
}
private void addSignatureInfo(XMLSignatureFactory signatureFactory, Document document, String signatureId, List<Reference> references,
List<XMLObject> objects) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
List<XMLStructure> objectContent = new LinkedList<XMLStructure>();
@ -200,10 +218,10 @@ public class OOXMLSignatureAspect implements SignatureAspect {
references.add(reference);
}
private void addRelationshipsReferences(XMLSignatureFactory signatureFactory, Document document, List<Reference> manifestReferences) throws IOException,
ParserConfigurationException, SAXException, TransformerException, NoSuchAlgorithmException,
private void addRelationshipsReferences(XMLSignatureFactory signatureFactory, List<Reference> manifestReferences) throws IOException,
ParserConfigurationException, SAXException, NoSuchAlgorithmException,
InvalidAlgorithmParameterException {
URL ooxmlUrl = this.signatureService.getOfficeOpenXMLDocumentURL();
URL ooxmlUrl = _signatureService.getOfficeOpenXMLDocumentURL();
InputStream inputStream = ooxmlUrl.openStream();
ZipInputStream zipInputStream = new ZipInputStream(inputStream);
ZipEntry zipEntry;
@ -212,11 +230,11 @@ public class OOXMLSignatureAspect implements SignatureAspect {
continue;
}
Document relsDocument = loadDocumentNoClose(zipInputStream);
addRelationshipsReference(signatureFactory, document, zipEntry.getName(), relsDocument, manifestReferences);
addRelationshipsReference(signatureFactory, zipEntry.getName(), relsDocument, manifestReferences);
}
}
private void addRelationshipsReference(XMLSignatureFactory signatureFactory, Document document, String zipEntryName, Document relsDocument,
private void addRelationshipsReference(XMLSignatureFactory signatureFactory, String zipEntryName, Document relsDocument,
List<Reference> manifestReferences) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
LOG.debug("relationships: " + zipEntryName);
RelationshipTransformParameterSpec parameterSpec = new RelationshipTransformParameterSpec();
@ -267,7 +285,7 @@ public class OOXMLSignatureAspect implements SignatureAspect {
InvalidAlgorithmParameterException {
List<String> documentResourceNames;
try {
documentResourceNames = getResourceNames(this.signatureService.getOfficeOpenXMLDocumentURL(), contentType);
documentResourceNames = getResourceNames(_signatureService.getOfficeOpenXMLDocumentURL(), contentType);
} catch (Exception e) {
throw new RuntimeException(e);
}
@ -318,7 +336,7 @@ public class OOXMLSignatureAspect implements SignatureAspect {
}
protected Document findDocument(String zipEntryName) throws IOException, ParserConfigurationException, SAXException {
URL ooxmlUrl = this.signatureService.getOfficeOpenXMLDocumentURL();
URL ooxmlUrl = _signatureService.getOfficeOpenXMLDocumentURL();
InputStream inputStream = ooxmlUrl.openStream();
ZipInputStream zipInputStream = new ZipInputStream(inputStream);
ZipEntry zipEntry;

View File

@ -0,0 +1,36 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.ooxml.signature.service.signer;
import org.apache.poi.ooxml.signature.service.signer.ooxml.TestOOXMLSignatureAspect;
import junit.framework.Test;
import junit.framework.TestSuite;
public final class AllOOXMLSignatureTests {
public static Test suite() {
TestSuite result = new TestSuite(AllOOXMLSignatureTests.class.getName());
result.addTestSuite(TestAbstractOOXMLSignatureService.class);
result.addTestSuite(TestAbstractXmlSignatureService.class);
result.addTestSuite(TestOOXMLSignatureAspect.class);
result.addTestSuite(TestOOXMLSignatureVerifier.class);
return result;
}
}

View File

@ -1,4 +1,3 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
@ -40,6 +39,8 @@ import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.security.spec.RSAKeyGenParameterSpec;
import java.util.Calendar;
import java.util.Date;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@ -53,6 +54,7 @@ import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.poi.util.HexRead;
import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.DERIA5String;
@ -71,18 +73,18 @@ import org.bouncycastle.asn1.x509.X509Extensions;
import org.bouncycastle.asn1.x509.X509ObjectIdentifiers;
import org.bouncycastle.jce.X509Principal;
import org.bouncycastle.x509.X509V3CertificateGenerator;
import org.joda.time.DateTime;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class PkiTestUtils {
final class PkiTestUtils {
public static final byte[] SHA1_DIGEST_INFO_PREFIX = new byte[] { 0x30, 0x1f, 0x30, 0x07, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x04, 0x14 };
public static final byte[] SHA1_DIGEST_INFO_PREFIX =
HexRead.readFromString( "30 1f 30 07 06 05 2b 0e 03 02 1a 04 14");
private PkiTestUtils() {
super();
// no instances of this class
}
static KeyPair generateKeyPair() throws Exception {
@ -107,17 +109,21 @@ public class PkiTestUtils {
return new AuthorityKeyIdentifier(info);
}
static X509Certificate generateCertificate(PublicKey subjectPublicKey, String subjectDn, DateTime notBefore, DateTime notAfter,
public static X509Certificate generateCertificate(PublicKey subjectPublicKey, String subjectDn,
X509Certificate issuerCertificate, PrivateKey issuerPrivateKey, boolean caFlag, int pathLength, String crlUri,
String ocspUri, KeyUsage keyUsage) throws IOException, InvalidKeyException, IllegalStateException,
NoSuchAlgorithmException, SignatureException, CertificateException {
Date notBefore = makeDate(2010, 1, 1);
Date notAfter = makeDate(2011, 1, 1);
String signatureAlgorithm = "SHA1withRSA";
X509V3CertificateGenerator certificateGenerator = new X509V3CertificateGenerator();
certificateGenerator.reset();
certificateGenerator.setPublicKey(subjectPublicKey);
certificateGenerator.setSignatureAlgorithm(signatureAlgorithm);
certificateGenerator.setNotBefore(notBefore.toDate());
certificateGenerator.setNotAfter(notAfter.toDate());
certificateGenerator.setNotBefore(notBefore);
certificateGenerator.setNotAfter(notAfter);
X509Principal issuerDN;
if (null != issuerCertificate) {
issuerDN = new X509Principal(issuerCertificate.getSubjectX500Principal().toString());
@ -173,6 +179,13 @@ public class PkiTestUtils {
return certificate;
}
private static Date makeDate(int year, int month, int day) {
Calendar c = Calendar.getInstance();
c.set(year, month, day, 0, 0, 0);
c.set(Calendar.MILLISECOND, 0);
return c.getTime();
}
static Document loadDocument(InputStream documentInputStream) throws ParserConfigurationException, SAXException, IOException {
InputSource inputSource = new InputSource(documentInputStream);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();

View File

@ -1,4 +1,3 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
@ -42,13 +41,11 @@ import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.ooxml.signature.service.signer.TemporaryDataStorage;
import org.apache.poi.ooxml.signature.service.signer.ooxml.AbstractOOXMLSignatureService;
import org.apache.poi.ooxml.signature.service.signer.ooxml.OOXMLProvider;
import org.apache.poi.ooxml.signature.service.signer.ooxml.OOXMLSignatureVerifier;
import org.apache.poi.ooxml.signature.service.spi.DigestInfo;
import org.bouncycastle.asn1.x509.KeyUsage;
import org.joda.time.DateTime;
@ -62,35 +59,35 @@ public class TestAbstractOOXMLSignatureService extends TestCase {
private static class OOXMLTestSignatureService extends AbstractOOXMLSignatureService {
private final URL ooxmlUrl;
private final URL _ooxmlUrl;
private final TemporaryTestDataStorage temporaryDataStorage;
private final TemporaryTestDataStorage _temporaryDataStorage;
private final ByteArrayOutputStream signedOOXMLOutputStream;
private final ByteArrayOutputStream _signedOOXMLOutputStream;
public OOXMLTestSignatureService(URL ooxmlUrl) {
this.temporaryDataStorage = new TemporaryTestDataStorage();
this.signedOOXMLOutputStream = new ByteArrayOutputStream();
this.ooxmlUrl = ooxmlUrl;
_temporaryDataStorage = new TemporaryTestDataStorage();
_signedOOXMLOutputStream = new ByteArrayOutputStream();
_ooxmlUrl = ooxmlUrl;
}
@Override
protected URL getOfficeOpenXMLDocumentURL() {
return this.ooxmlUrl;
return _ooxmlUrl;
}
@Override
protected OutputStream getSignedOfficeOpenXMLDocumentOutputStream() {
return this.signedOOXMLOutputStream;
return _signedOOXMLOutputStream;
}
public byte[] getSignedOfficeOpenXMLDocumentData() {
return this.signedOOXMLOutputStream.toByteArray();
return _signedOOXMLOutputStream.toByteArray();
}
@Override
protected TemporaryDataStorage getTemporaryDataStorage() {
return this.temporaryDataStorage;
return _temporaryDataStorage;
}
}
@ -189,9 +186,7 @@ public class TestAbstractOOXMLSignatureService extends TestCase {
byte[] digestInfoValue = ArrayUtils.addAll(PkiTestUtils.SHA1_DIGEST_INFO_PREFIX, digestInfo.digestValue);
byte[] signatureValue = cipher.doFinal(digestInfoValue);
DateTime notBefore = new DateTime();
DateTime notAfter = notBefore.plusYears(1);
X509Certificate certificate = PkiTestUtils.generateCertificate(keyPair.getPublic(), signerDn, notBefore, notAfter, null, keyPair.getPrivate(), true, 0,
X509Certificate certificate = PkiTestUtils.generateCertificate(keyPair.getPublic(), signerDn, null, keyPair.getPrivate(), true, 0,
null, null, new KeyUsage(KeyUsage.nonRepudiation));
// operate: postSign

View File

@ -69,7 +69,6 @@ import org.apache.xpath.XPathAPI;
import org.bouncycastle.asn1.x509.KeyUsage;
import org.jcp.xml.dsig.internal.dom.DOMReference;
import org.jcp.xml.dsig.internal.dom.DOMXMLSignature;
import org.joda.time.DateTime;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
@ -215,9 +214,7 @@ public final class TestAbstractXmlSignatureService extends TestCase {
byte[] digestInfoValue = ArrayUtils.addAll(PkiTestUtils.SHA1_DIGEST_INFO_PREFIX, digestInfo.digestValue);
byte[] signatureValue = cipher.doFinal(digestInfoValue);
DateTime notBefore = new DateTime();
DateTime notAfter = notBefore.plusYears(1);
X509Certificate certificate = PkiTestUtils.generateCertificate(keyPair.getPublic(), "CN=Test", notBefore, notAfter, null, keyPair.getPrivate(), true,
X509Certificate certificate = PkiTestUtils.generateCertificate(keyPair.getPublic(), "CN=Test", null, keyPair.getPrivate(), true,
0, null, null, new KeyUsage(KeyUsage.nonRepudiation));
/*
@ -313,9 +310,7 @@ public final class TestAbstractXmlSignatureService extends TestCase {
byte[] digestInfoValue = ArrayUtils.addAll(PkiTestUtils.SHA1_DIGEST_INFO_PREFIX, digestInfo.digestValue);
byte[] signatureValue = cipher.doFinal(digestInfoValue);
DateTime notBefore = new DateTime();
DateTime notAfter = notBefore.plusYears(1);
X509Certificate certificate = PkiTestUtils.generateCertificate(keyPair.getPublic(), "CN=Test", notBefore, notAfter, null, keyPair.getPrivate(), true,
X509Certificate certificate = PkiTestUtils.generateCertificate(keyPair.getPublic(), "CN=Test", null, keyPair.getPrivate(), true,
0, null, null, new KeyUsage(KeyUsage.nonRepudiation));
/*
@ -395,9 +390,7 @@ public final class TestAbstractXmlSignatureService extends TestCase {
byte[] digestInfoValue = ArrayUtils.addAll(PkiTestUtils.SHA1_DIGEST_INFO_PREFIX, digestInfo.digestValue);
byte[] signatureValue = cipher.doFinal(digestInfoValue);
DateTime notBefore = new DateTime();
DateTime notAfter = notBefore.plusYears(1);
X509Certificate certificate = PkiTestUtils.generateCertificate(keyPair.getPublic(), "CN=Test", notBefore, notAfter, null, keyPair.getPrivate(), true,
X509Certificate certificate = PkiTestUtils.generateCertificate(keyPair.getPublic(), "CN=Test", null, keyPair.getPrivate(), true,
0, null, null, new KeyUsage(KeyUsage.nonRepudiation));
/*
@ -475,9 +468,7 @@ public final class TestAbstractXmlSignatureService extends TestCase {
byte[] digestInfoValue = ArrayUtils.addAll(PkiTestUtils.SHA1_DIGEST_INFO_PREFIX, digestInfo.digestValue);
byte[] signatureValue = cipher.doFinal(digestInfoValue);
DateTime notBefore = new DateTime();
DateTime notAfter = notBefore.plusYears(1);
X509Certificate certificate = PkiTestUtils.generateCertificate(keyPair.getPublic(), "CN=Test", notBefore, notAfter, null, keyPair.getPrivate(), true,
X509Certificate certificate = PkiTestUtils.generateCertificate(keyPair.getPublic(), "CN=Test", null, keyPair.getPrivate(), true,
0, null, null, new KeyUsage(KeyUsage.nonRepudiation));
/*

View File

@ -0,0 +1,40 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.ooxml.signature.service.signer.ooxml;
import java.util.Calendar;
import java.util.TimeZone;
import junit.framework.TestCase;
public final class TestOOXMLSignatureAspect extends TestCase {
private static final TimeZone TIME_ZONE_UTC = TimeZone.getTimeZone("UTC");
public void testFormatTimestampAsISO8601() {
assertEquals("2010-06-05T04:03:02Z", OOXMLSignatureAspect.formatTimestampAsISO8601(makeTimestamp(2010, 6, 5, 4, 3, 2)));
}
private static long makeTimestamp(int year, int month, int day, int hour, int minute, int second) {
Calendar c = Calendar.getInstance();
c.setTimeZone(TIME_ZONE_UTC);
c.set(year, month-1, day, hour, minute, second);
c.set(Calendar.MILLISECOND, 0);
return c.getTimeInMillis();
}
}