import java.net.URI;
import java.net.URISyntaxException;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.Locale;
+import java.util.Map;
+import java.util.TreeMap;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.InvalidOperationException;
private final static POILogger logger = POILogFactory.getLogger(PackageRelationshipCollection.class);
/**
- * Package relationships ordered by ID.
+ * Package relationships by ID.
*/
- private TreeMap<String, PackageRelationship> relationshipsByID;
+ private final Map<String, PackageRelationship> relationshipsByID = new LinkedHashMap<String, PackageRelationship>();
/**
- * Package relationships ordered by type.
+ * Package relationships by type.
*/
- private TreeMap<String, PackageRelationship> relationshipsByType;
+ private final Map<String, PackageRelationship> relationshipsByType = new LinkedHashMap<String, PackageRelationship>();
/**
* A lookup of internal relationships to avoid
* Constructor.
*/
PackageRelationshipCollection() {
- relationshipsByID = new TreeMap<String, PackageRelationship>();
- relationshipsByType = new TreeMap<String, PackageRelationship>();
}
/**
* @param filter
* Relationship type filter.
*/
- public PackageRelationshipCollection(PackageRelationshipCollection coll,
- String filter) {
- this();
+ public PackageRelationshipCollection(PackageRelationshipCollection coll, String filter) {
for (PackageRelationship rel : coll.relationshipsByID.values()) {
- if (filter == null || rel.getRelationshipType().equals(filter))
+ if (filter == null || rel.getRelationshipType().equals(filter)) {
addRelationship(rel);
+ }
}
}
/**
* Constructor.
*/
- public PackageRelationshipCollection(OPCPackage container)
- throws InvalidFormatException {
+ public PackageRelationshipCollection(OPCPackage container) throws InvalidFormatException {
this(container, null);
}
* @throws InvalidOperationException
* Throws if the specified part is a relationship part.
*/
- public PackageRelationshipCollection(PackagePart part)
- throws InvalidFormatException {
+ public PackageRelationshipCollection(PackagePart part) throws InvalidFormatException {
this(part._container, part);
}
* If an error occurs during the parsing of the relatinships
* part fo the specified part.
*/
- public PackageRelationshipCollection(OPCPackage container, PackagePart part)
- throws InvalidFormatException {
- this();
-
- if (container == null)
+ public PackageRelationshipCollection(OPCPackage container, PackagePart part) throws InvalidFormatException {
+ if (container == null) {
throw new IllegalArgumentException("container needs to be specified");
+ }
// Check if the specified part is not a relationship part
- if (part != null && part.isRelationshipPart())
+ if (part != null && part.isRelationshipPart()) {
throw new IllegalArgumentException("part");
+ }
this.container = container;
this.sourcePart = part;
* @throws InvalidOperationException
* Throws if the specified part is a relationship part.
*/
- private static PackagePartName getRelationshipPartName(PackagePart part)
- throws InvalidOperationException {
+ private static PackagePartName getRelationshipPartName(PackagePart part) throws InvalidOperationException {
PackagePartName partName;
if (part == null) {
partName = PackagingURIHelper.PACKAGE_ROOT_PART_NAME;
* Must be a value between [0-relationships_count-1]
*/
public PackageRelationship getRelationship(int index) {
- if (index < 0 || index > relationshipsByID.values().size())
+ if (index < 0 || index > relationshipsByID.values().size()) {
throw new IllegalArgumentException("index");
+ }
int i = 0;
for (PackageRelationship rel : relationshipsByID.values()) {
- if (index == i++)
+ if (index == i++) {
return rel;
+ }
}
return null;
}
/**
- * Get the numbe rof relationships in the collection.
+ * Get the number of relationships in the collection.
*/
public int size() {
- return relationshipsByID.values().size();
+ return relationshipsByID.size();
}
/**
/* Check OPC Compliance */
// Check Rule M4.1
- if (type.equals(PackageRelationshipTypes.CORE_PROPERTIES))
- if (!fCorePropertiesRelationship)
+ if (type.equals(PackageRelationshipTypes.CORE_PROPERTIES)) {
+ if (!fCorePropertiesRelationship) {
fCorePropertiesRelationship = true;
- else
+ } else {
throw new InvalidFormatException(
"OPC Compliance error [M4.1]: there is more than one core properties relationship in the package !");
+ }
+ }
/* End OPC Compliance */
/**
* Get this collection's iterator.
*/
+ @Override
public Iterator<PackageRelationship> iterator() {
return relationshipsByID.values().iterator();
}
public Iterator<PackageRelationship> iterator(String typeFilter) {
ArrayList<PackageRelationship> retArr = new ArrayList<PackageRelationship>();
for (PackageRelationship rel : relationshipsByID.values()) {
- if (rel.getRelationshipType().equals(typeFilter))
+ if (rel.getRelationshipType().equals(typeFilter)) {
retArr.add(rel);
+ }
}
return retArr.iterator();
}
/* ====================================================================
This product contains an ASLv2 licensed version of the OOXML signer
package from the eID Applet project
- http://code.google.com/p/eid-applet/source/browse/trunk/README.txt
+ http://code.google.com/p/eid-applet/source/browse/trunk/README.txt
Copyright (C) 2008-2014 FedICT.
- ================================================================= */
+ ================================================================= */
package org.apache.poi.poifs.crypt.dsig.facets;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import javax.xml.crypto.dsig.XMLObject;
import javax.xml.crypto.dsig.XMLSignatureException;
+import org.apache.poi.hpsf.ClassID;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.ContentTypes;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.util.LocaleUtil;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
+import org.apache.xmlbeans.XmlCursor;
import org.openxmlformats.schemas.xpackage.x2006.digitalSignature.CTSignatureTime;
import org.openxmlformats.schemas.xpackage.x2006.digitalSignature.SignatureTimeDocument;
import org.w3c.dom.Document;
/**
* Office OpenXML Signature Facet implementation.
- *
- * @author fcorneli
+ *
* @see <a href="http://msdn.microsoft.com/en-us/library/cc313071.aspx">[MS-OFFCRYPTO]: Office Document Cryptography Structure</a>
*/
public class OOXMLSignatureFacet extends SignatureFacet {
private static final POILogger LOG = POILogFactory.getLogger(OOXMLSignatureFacet.class);
+ private static final String ID_PACKAGE_OBJECT = "idPackageObject";
@Override
public void preSign(
List<Reference> manifestReferences = new ArrayList<Reference>();
addManifestReferences(manifestReferences);
Manifest manifest = getSignatureFactory().newManifest(manifestReferences);
-
- String objectId = "idPackageObject"; // really has to be this value.
+
List<XMLStructure> objectContent = new ArrayList<XMLStructure>();
objectContent.add(manifest);
addSignatureTime(document, objectContent);
- XMLObject xo = getSignatureFactory().newXMLObject(objectContent, objectId, null, null);
+ XMLObject xo = getSignatureFactory().newXMLObject(objectContent, ID_PACKAGE_OBJECT, null, null);
objects.add(xo);
- Reference reference = newReference("#" + objectId, null, XML_DIGSIG_NS+"Object", null, null);
+ Reference reference = newReference("#"+ID_PACKAGE_OBJECT, null, XML_DIGSIG_NS+"Object", null, null);
references.add(reference);
}
Set<String> digestedPartNames = new HashSet<String>();
for (PackagePart pp : relsEntryNames) {
- String baseUri = pp.getPartName().getName().replaceFirst("(.*)/_rels/.*", "$1");
+ final String baseUri = pp.getPartName().getName().replaceFirst("(.*)/_rels/.*", "$1");
PackageRelationshipCollection prc;
try {
} catch (InvalidFormatException e) {
throw new XMLSignatureException("Invalid relationship descriptor: "+pp.getPartName().getName(), e);
}
-
+
RelationshipTransformParameterSpec parameterSpec = new RelationshipTransformParameterSpec();
for (PackageRelationship relationship : prc) {
String relationshipType = relationship.getRelationshipType();
-
+
/*
* ECMA-376 Part 2 - 3rd edition
* 13.2.4.16 Manifest Element
continue;
}
- if (!isSignedRelationship(relationshipType)) continue;
+ if (!isSignedRelationship(relationshipType)) {
+ continue;
+ }
parameterSpec.addRelationshipReference(relationship.getId());
- // TODO: find a better way ...
- String partName = relationship.getTargetURI().toString();
- if (!partName.startsWith(baseUri)) {
- partName = baseUri + partName;
- }
- try {
- partName = new URI(partName).normalize().getPath().replace('\\', '/');
- LOG.log(POILogger.DEBUG, "part name: " + partName);
- } catch (URISyntaxException e) {
- throw new XMLSignatureException(e);
+ String partName = normalizePartName(relationship.getTargetURI(), baseUri);
+
+ // We only digest a part once.
+ if (digestedPartNames.contains(partName)) {
+ continue;
}
-
+ digestedPartNames.add(partName);
+
String contentType;
try {
PackagePartName relName = PackagingURIHelper.createPartName(partName);
} catch (InvalidFormatException e) {
throw new XMLSignatureException(e);
}
-
+
if (relationshipType.endsWith("customXml")
&& !(contentType.equals("inkml+xml") || contentType.equals("text/xml"))) {
LOG.log(POILogger.DEBUG, "skipping customXml with content type: " + contentType);
continue;
}
-
- if (!digestedPartNames.contains(partName)) {
- // We only digest a part once.
- String uri = partName + "?ContentType=" + contentType;
- Reference reference = newReference(uri, null, null, null, null);
- manifestReferences.add(reference);
- digestedPartNames.add(partName);
- }
+
+ String uri = partName + "?ContentType=" + contentType;
+ Reference reference = newReference(uri, null, null, null, null);
+ manifestReferences.add(reference);
}
-
+
if (parameterSpec.hasSourceIds()) {
List<Transform> transforms = new ArrayList<Transform>();
transforms.add(newTransform(RelationshipTransformService.TRANSFORM_URI, parameterSpec));
transforms.add(newTransform(CanonicalizationMethod.INCLUSIVE));
- String uri = pp.getPartName().getName()
+ String uri = normalizePartName(pp.getPartName().getURI(), baseUri)
+ "?ContentType=application/vnd.openxmlformats-package.relationships+xml";
Reference reference = newReference(uri, transforms, null, null, null);
manifestReferences.add(reference);
}
}
+
+ Collections.sort(manifestReferences, new Comparator<Reference>() {
+ public int compare(Reference o1, Reference o2) {
+ return o1.getURI().compareTo(o2.getURI());
+ }
+ });
+ }
+
+ /**
+ * Normalize a URI/part name
+ * TODO: find a better way ...
+ */
+ private static String normalizePartName(URI partName, String baseUri) throws XMLSignatureException {
+ String pn = partName.toASCIIString();
+ if (!pn.startsWith(baseUri)) {
+ pn = baseUri + pn;
+ }
+ try {
+ pn = new URI(pn).normalize().getPath().replace('\\', '/');
+ LOG.log(POILogger.DEBUG, "part name: " + pn);
+ } catch (URISyntaxException e) {
+ throw new XMLSignatureException(e);
+ }
+ return pn;
}
List<SignatureProperty> signaturePropertyContent = new ArrayList<SignatureProperty>();
signaturePropertyContent.add(signatureTimeSignatureProperty);
SignatureProperties signatureProperties = getSignatureFactory()
- .newSignatureProperties(signaturePropertyContent,
- "id-signature-time-" + signatureConfig.getExecutionTime());
+ .newSignatureProperties(signaturePropertyContent, null);
+// "id-signature-time-" + signatureConfig.getExecutionTime());
objectContent.add(signatureProperties);
}
SignatureInfoV1Document sigV1 = SignatureInfoV1Document.Factory.newInstance();
CTSignatureInfoV1 ctSigV1 = sigV1.addNewSignatureInfoV1();
- ctSigV1.setManifestHashAlgorithm(signatureConfig.getDigestMethodUri());
+// ctSigV1.setManifestHashAlgorithm(signatureConfig.getDigestMethodUri());
+ XmlCursor cur = ctSigV1.newCursor();
+ cur.toEndToken(); cur.beginElement("SetupID", MS_DIGSIG_NS);
+ cur.toNextToken(); cur.beginElement("SignatureText", MS_DIGSIG_NS);
+ cur.toNextToken(); cur.beginElement("SignatureImage", MS_DIGSIG_NS);
+ cur.toNextToken(); cur.beginElement("SignatureProviderUrl", MS_DIGSIG_NS);
+ cur.dispose();
+ ctSigV1.setSignatureComments("Test");
+ ctSigV1.setWindowsVersion("6.1");
+ ctSigV1.setOfficeVersion("16.0");
+ ctSigV1.setApplicationVersion("16.0");
+ ctSigV1.setMonitors(1);
+ ctSigV1.setHorizontalResolution(3840);
+ ctSigV1.setVerticalResolution(2160);
+ ctSigV1.setColorDepth(32);
+ ctSigV1.setSignatureProviderId(new ClassID().toString());
+ ctSigV1.setSignatureProviderDetails(9);
+ ctSigV1.setSignatureType(1);
+
Element n = (Element)document.importNode(ctSigV1.getDomNode(), true);
n.setAttributeNS(XML_NS, XMLConstants.XMLNS_ATTRIBUTE, MS_DIGSIG_NS);
-
+
List<XMLStructure> signatureInfoContent = new ArrayList<XMLStructure>();
signatureInfoContent.add(new DOMStructure(n));
SignatureProperty signatureInfoSignatureProperty = getSignatureFactory()
protected static boolean isSignedRelationship(String relationshipType) {
LOG.log(POILogger.DEBUG, "relationship type: " + relationshipType);
- for (String signedTypeExtension : signed) {
- if (relationshipType.endsWith(signedTypeExtension)) {
- return true;
- }
- }
- if (relationshipType.endsWith("customXml")) {
- LOG.log(POILogger.DEBUG, "customXml relationship type");
- return true;
- }
- return false;
+ String rt = relationshipType.replaceFirst(".*/relationships/", "");
+ return (signed.contains(rt) || rt.endsWith("customXml"));
}
-
- public static final String[] contentTypes = {
- /*
- * Word
- */
- "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml",
- "application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml",
- "application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml",
- "application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml",
- "application/vnd.openxmlformats-officedocument.theme+xml",
- "application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml",
- "application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml",
-
- /*
- * Word 2010
- */
- "application/vnd.ms-word.stylesWithEffects+xml",
-
- /*
- * Excel
- */
- "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml",
- "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml",
- "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml",
- "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml",
-
- /*
- * Powerpoint
- */
- "application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml",
- "application/vnd.openxmlformats-officedocument.presentationml.slideLayout+xml",
- "application/vnd.openxmlformats-officedocument.presentationml.slideMaster+xml",
- "application/vnd.openxmlformats-officedocument.presentationml.slide+xml",
- "application/vnd.openxmlformats-officedocument.presentationml.tableStyles+xml",
-
- /*
- * Powerpoint 2010
- */
- "application/vnd.openxmlformats-officedocument.presentationml.viewProps+xml",
- "application/vnd.openxmlformats-officedocument.presentationml.presProps+xml"
- };
/**
* Office 2010 list of signed types (extensions).
*/
- public static final String[] signed = {
- "powerPivotData", //
- "activeXControlBinary", //
- "attachedToolbars", //
- "connectorXml", //
- "downRev", //
- "functionPrototypes", //
- "graphicFrameDoc", //
- "groupShapeXml", //
- "ink", //
- "keyMapCustomizations", //
- "legacyDiagramText", //
- "legacyDocTextInfo", //
- "officeDocument", //
- "pictureXml", //
- "shapeXml", //
- "smartTags", //
- "ui/altText", //
- "ui/buttonSize", //
- "ui/controlID", //
- "ui/description", //
- "ui/enabled", //
- "ui/extensibility", //
- "ui/helperText", //
- "ui/imageID", //
- "ui/imageMso", //
- "ui/keyTip", //
- "ui/label", //
- "ui/lcid", //
- "ui/loud", //
- "ui/pressed", //
- "ui/progID", //
- "ui/ribbonID", //
- "ui/showImage", //
- "ui/showLabel", //
- "ui/supertip", //
- "ui/target", //
- "ui/text", //
- "ui/title", //
- "ui/tooltip", //
- "ui/userCustomization", //
- "ui/visible", //
- "userXmlData", //
- "vbaProject", //
- "wordVbaData", //
- "wsSortMap", //
- "xlBinaryIndex", //
- "xlExternalLinkPath/xlAlternateStartup", //
- "xlExternalLinkPath/xlLibrary", //
- "xlExternalLinkPath/xlPathMissing", //
- "xlExternalLinkPath/xlStartup", //
- "xlIntlMacrosheet", //
- "xlMacrosheet", //
- "customData", //
- "diagramDrawing", //
- "hdphoto", //
- "inkXml", //
- "media", //
- "slicer", //
- "slicerCache", //
- "stylesWithEffects", //
- "ui/extensibility", //
- "chartColorStyle", //
- "chartLayout", //
- "chartStyle", //
- "dictionary", //
- "timeline", //
- "timelineCache", //
- "aFChunk", //
- "attachedTemplate", //
- "audio", //
- "calcChain", //
- "chart", //
- "chartsheet", //
- "chartUserShapes", //
- "commentAuthors", //
- "comments", //
- "connections", //
- "control", //
- "customProperty", //
- "customXml", //
- "diagramColors", //
- "diagramData", //
- "diagramLayout", //
- "diagramQuickStyle", //
- "dialogsheet", //
- "drawing", //
- "endnotes", //
- "externalLink", //
- "externalLinkPath", //
- "font", //
- "fontTable", //
- "footer", //
- "footnotes", //
- "glossaryDocument", //
- "handoutMaster", //
- "header", //
- "hyperlink", //
- "image", //
- "mailMergeHeaderSource", //
- "mailMergeRecipientData", //
- "mailMergeSource", //
- "notesMaster", //
- "notesSlide", //
- "numbering", //
- "officeDocument", //
- "oleObject", //
- "package", //
- "pivotCacheDefinition", //
- "pivotCacheRecords", //
- "pivotTable", //
- "presProps", //
- "printerSettings", //
- "queryTable", //
- "recipientData", //
- "settings", //
- "sharedStrings", //
- "sheetMetadata", //
- "slide", //
- "slideLayout", //
- "slideMaster", //
- "slideUpdateInfo", //
- "slideUpdateUrl", //
- "styles", //
- "table", //
- "tableSingleCells", //
- "tableStyles", //
- "tags", //
- "theme", //
- "themeOverride", //
- "transform", //
- "video", //
- "viewProps", //
- "volatileDependencies", //
- "webSettings", //
- "worksheet", //
- "xmlMaps", //
- "ctrlProp", //
- "customData", //
- "diagram", //
- "diagramColorsHeader", //
- "diagramLayoutHeader", //
- "diagramQuickStyleHeader", //
- "documentParts", //
- "slicer", //
- "slicerCache", //
- "vmlDrawing" //
- };
+ private static final Set<String> signed = Collections.unmodifiableSet(new HashSet<String>(Arrays.asList(
+ "activeXControlBinary","aFChunk","attachedTemplate","attachedToolbars","audio","calcChain","chart","chartColorStyle",
+ "chartLayout","chartsheet","chartStyle","chartUserShapes","commentAuthors","comments","connections","connectorXml",
+ "control","ctrlProp","customData","customData","customProperty","customXml","diagram","diagramColors",
+ "diagramColorsHeader","diagramData","diagramDrawing","diagramLayout","diagramLayoutHeader","diagramQuickStyle",
+ "diagramQuickStyleHeader","dialogsheet","dictionary","documentParts","downRev","drawing","endnotes","externalLink",
+ "externalLinkPath","font","fontTable","footer","footnotes","functionPrototypes","glossaryDocument","graphicFrameDoc",
+ "groupShapeXml","handoutMaster","hdphoto","header","hyperlink","image","ink","inkXml","keyMapCustomizations",
+ "legacyDiagramText","legacyDocTextInfo","mailMergeHeaderSource","mailMergeRecipientData","mailMergeSource","media",
+ "notesMaster","notesSlide","numbering","officeDocument","officeDocument","oleObject","package","pictureXml",
+ "pivotCacheDefinition","pivotCacheRecords","pivotTable","powerPivotData","presProps","printerSettings","queryTable",
+ "recipientData","settings","shapeXml","sharedStrings","sheetMetadata","slicer","slicer","slicerCache","slicerCache",
+ "slide","slideLayout","slideMaster","slideUpdateInfo","slideUpdateUrl","smartTags","styles","stylesWithEffects",
+ "table","tableSingleCells","tableStyles","tags","theme","themeOverride","timeline","timelineCache","transform",
+ "ui/altText","ui/buttonSize","ui/controlID","ui/description","ui/enabled","ui/extensibility","ui/extensibility",
+ "ui/helperText","ui/imageID","ui/imageMso","ui/keyTip","ui/label","ui/lcid","ui/loud","ui/pressed","ui/progID",
+ "ui/ribbonID","ui/showImage","ui/showLabel","ui/supertip","ui/target","ui/text","ui/title","ui/tooltip",
+ "ui/userCustomization","ui/visible","userXmlData","vbaProject","video","viewProps","vmlDrawing",
+ "volatileDependencies","webSettings","wordVbaData","worksheet","wsSortMap","xlBinaryIndex",
+ "xlExternalLinkPath/xlAlternateStartup","xlExternalLinkPath/xlLibrary","xlExternalLinkPath/xlPathMissing",
+ "xlExternalLinkPath/xlStartup","xlIntlMacrosheet","xlMacrosheet","xmlMaps"
+ )));
}
\ No newline at end of file