RelationshipTransformService.java

/* ====================================================================
   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.
==================================================================== */

/* ====================================================================
   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
   Copyright (C) 2008-2014 FedICT.
   ================================================================= */

package org.apache.poi.poifs.crypt.dsig.services;

import static org.apache.logging.log4j.util.Unbox.box;
import static org.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
import static org.apache.poi.poifs.crypt.dsig.facets.SignatureFacet.OO_DIGSIG_NS;
import static org.apache.poi.poifs.crypt.dsig.facets.SignatureFacet.XML_NS;

import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidAlgorithmParameterException;
import java.security.Provider;
import java.security.Security;
import java.security.spec.AlgorithmParameterSpec;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import javax.xml.crypto.Data;
import javax.xml.crypto.MarshalException;
import javax.xml.crypto.OctetStreamData;
import javax.xml.crypto.XMLCryptoContext;
import javax.xml.crypto.XMLStructure;
import javax.xml.crypto.dom.DOMStructure;
import javax.xml.crypto.dsig.TransformException;
import javax.xml.crypto.dsig.TransformService;
import javax.xml.crypto.dsig.spec.TransformParameterSpec;

import org.apache.jcp.xml.dsig.internal.dom.ApacheNodeSetData;
import org.apache.logging.log4j.Logger;
import org.apache.poi.logging.PoiLogManager;
import org.apache.poi.openxml4j.opc.PackageNamespaces;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.ooxml.util.DocumentHelper;
import org.apache.poi.util.SuppressForbidden;
import org.apache.xml.security.signature.XMLSignatureNodeInput;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.xpackage.x2006.digitalSignature.CTRelationshipReference;
import org.openxmlformats.schemas.xpackage.x2006.digitalSignature.RelationshipReferenceDocument;
import org.w3.x2000.x09.xmldsig.TransformDocument;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * JSR105 implementation of the RelationshipTransform transformation.
 *
 * <p>
 * Specs: http://openiso.org/Ecma/376/Part2/12.2.4#26
 * </p>
 */
public class RelationshipTransformService extends TransformService {

    public static final String TRANSFORM_URI = "http://schemas.openxmlformats.org/package/2006/RelationshipTransform";

    private final List<String> sourceIds;

    private final List<String> sourceTypes;

    private static final Logger LOG = PoiLogManager.getLogger(RelationshipTransformService.class);

    /**
     * Relationship Transform parameter specification class.
     */
    public static class RelationshipTransformParameterSpec implements TransformParameterSpec {
        List<String> sourceIds = new ArrayList<>();
        List<String> sourceTypes = new ArrayList<>();
        public void addRelationshipReference(String relationshipId) {
                sourceIds.add(relationshipId);
        }
        /**
         * Adds a RelationshipsGroupReference, i.e. signs all relationships of the given type
         *
         * @since 6.1.0
         */
        public void addRelationshipGroupReference(String relationshipType) {
            sourceTypes.add(relationshipType);
        }
        public boolean hasSourceIds() {
            return !sourceIds.isEmpty();
        }
    }

    @SuppressForbidden("new Provider(String,String,String) is not available in Java 8")
    private static final class POIXmlDsigProvider extends Provider {
        static final long serialVersionUID = 1L;
        private static final String NAME = "POIXmlDsigProvider";

        private POIXmlDsigProvider() {
            super(NAME, 1d, NAME);
            put("TransformService." + TRANSFORM_URI, RelationshipTransformService.class.getName());
            put("TransformService." + TRANSFORM_URI + " MechanismType", "DOM");
        }
    }


    public RelationshipTransformService() {
        super();
        LOG.atDebug().log("constructor");
        this.sourceIds = new ArrayList<>();
        this.sourceTypes = new ArrayList<>();
    }

    /**
     * Register the provider for this TransformService
     *
     * @see TransformService
     */
    public static void registerDsigProvider() {
        // the xml signature classes will try to find a special TransformerService,
        // which is of course unknown to JCE before ...
        synchronized (REGISTER_LOCK) {
            if (Security.getProvider(POIXmlDsigProvider.NAME) == null) {
                Security.addProvider(new POIXmlDsigProvider());
            }
        }
    }

    /** A private lock, so other code can't block the registration by locking the class */
    private static final Object REGISTER_LOCK = new Object();


    @Override
    public void init(TransformParameterSpec params) throws InvalidAlgorithmParameterException {
        LOG.atDebug().log("init(params)");
        if (!(params instanceof RelationshipTransformParameterSpec relParams)) {
            throw new InvalidAlgorithmParameterException();
        }
        this.sourceIds.addAll(relParams.sourceIds);
        this.sourceTypes.addAll(relParams.sourceTypes);
    }

    @Override
    public void init(XMLStructure parent, XMLCryptoContext context) throws InvalidAlgorithmParameterException {
        LOG.atDebug().log("init(parent,context)");
        LOG.atDebug().log("parent java type: {}", parent.getClass().getName());
        DOMStructure domParent = (DOMStructure) parent;
        Node parentNode = domParent.getNode();

        try {
            TransformDocument transDoc = TransformDocument.Factory.parse(parentNode, DEFAULT_XML_OPTIONS);
            XmlObject[] xoList = transDoc.getTransform().selectChildren(RelationshipReferenceDocument.type.getDocumentElementName());
            for (XmlObject xo : xoList) {
                String sourceId = ((CTRelationshipReference)xo).getSourceId();
                LOG.atDebug().log("sourceId: {}", sourceId);
                this.sourceIds.add(sourceId);
            }
            // RelationshipsGroupReference selects all relationships of a type (13.2.4.24)
            for (Node n = parentNode.getFirstChild(); n != null; n = n.getNextSibling()) {
                if (n instanceof Element && OO_DIGSIG_NS.equals(n.getNamespaceURI())
                    && "RelationshipsGroupReference".equals(n.getLocalName())) {
                    String sourceType = ((Element)n).getAttribute("SourceType");
                    LOG.atDebug().log("sourceType: {}", sourceType);
                    this.sourceTypes.add(sourceType);
                }
            }
            if (sourceIds.isEmpty() && sourceTypes.isEmpty()) {
                LOG.atWarn().log("no RelationshipReference/@SourceId or RelationshipsGroupReference/@SourceType parameters present");
            }
        } catch (XmlException e) {
            throw new InvalidAlgorithmParameterException(e);
        }
    }

    @Override
    public void marshalParams(XMLStructure parent, XMLCryptoContext context) throws MarshalException {
        LOG.atDebug().log("marshallParams(parent,context)");
        DOMStructure domParent = (DOMStructure) parent;
        Element parentNode = (Element)domParent.getNode();
        Document doc = parentNode.getOwnerDocument();

        for (String sourceId : this.sourceIds) {
            Element el = doc.createElementNS(OO_DIGSIG_NS, "mdssi:RelationshipReference");
            el.setAttributeNS(XML_NS, "xmlns:mdssi", OO_DIGSIG_NS);
            el.setAttribute("SourceId", sourceId);
            parentNode.appendChild(el);
        }
        for (String sourceType : this.sourceTypes) {
            Element el = doc.createElementNS(OO_DIGSIG_NS, "mdssi:RelationshipsGroupReference");
            el.setAttributeNS(XML_NS, "xmlns:mdssi", OO_DIGSIG_NS);
            el.setAttribute("SourceType", sourceType);
            parentNode.appendChild(el);
        }
    }

    public AlgorithmParameterSpec getParameterSpec() {
        LOG.atDebug().log("getParameterSpec");
        return null;
    }

    /**
     * The relationships transform takes the XML document from the Relationships part
     * and converts it to another XML document.
     *
     * @see <a href="https://www.ecma-international.org/activities/Office%20Open%20XML%20Formats/Draft%20ECMA-376%203rd%20edition,%20March%202011/Office%20Open%20XML%20Part%202%20-%20Open%20Packaging%20Conventions.pdf">13.2.4.24 Relationships Transform Algorithm</a>
     */
    public Data transform(Data data, XMLCryptoContext context) throws TransformException {
        LOG.atDebug().log("transform(data,context)");
        LOG.atDebug().log("data java type: {}", data.getClass().getName());
        OctetStreamData octetStreamData = (OctetStreamData) data;
        LOG.atDebug().log("URI: {}", octetStreamData.getURI());
        InputStream octetStream = octetStreamData.getOctetStream();

        Document doc;
        try {
            doc = DocumentHelper.readDocument(octetStream);
        } catch (Exception e) {
            throw new TransformException(e.getMessage(), e);
        }

        // keep only those relationships which id is registered in the sourceIds, or which type is
        // registered in the sourceTypes (RelationshipsGroupReference) - all of them,
        // at any depth: the package model also reads nested elements and resolves a duplicated
        // id to the last one, so dropping a duplicate of a signed id here would leave its target
        // out of the digest. Only elements in the relationships namespace are relationships,
        // as in PackageRelationshipCollection
        Element root = doc.getDocumentElement();
        NodeList nl = root.getElementsByTagNameNS(PackageNamespaces.RELATIONSHIPS, PackageRelationship.RELATIONSHIP_TAG_NAME);
        List<Element> rsList = new ArrayList<>();
        for (int i=0; i<nl.getLength(); i++) {
            Element el = (Element)nl.item(i);
            String id = el.getAttribute("Id");
            if (sourceIds.contains(id) || sourceTypes.contains(el.getAttribute("Type"))) {
                // the default applies to an absent attribute only: the package model reads a
                // present TargetMode with any value other than "Internal" as an external
                // relationship, so an empty one has to be digested as what it is
                if (!el.hasAttribute("TargetMode")) {
                    el.setAttribute("TargetMode", "Internal");
                }
                rsList.add(el);
            }
        }

        while (root.hasChildNodes()) {
            root.removeChild(root.getFirstChild());
        }

        // the transform removes any content of the Relationship elements, e.g. whitespace of a
        // pretty-printed <Relationship ...></Relationship> - Office/.NET digest them as empty elements.
        // A nested relationship which is kept is moved to the root, before or after its parent is emptied
        rsList.sort(Comparator.comparing(el -> el.getAttribute("Id")));
        for (Element el : rsList) {
            while (el.hasChildNodes()) {
                el.removeChild(el.getFirstChild());
            }
            root.appendChild(el);
        }

        LOG.atDebug().log("# Relationship elements: {}", box(rsList.size()));

        return new ApacheNodeSetData(new XMLSignatureNodeInput(root));
    }

    public Data transform(Data data, XMLCryptoContext context, OutputStream os) throws TransformException {
        LOG.atDebug().log("transform(data,context,os)");
        return null;
    }

    public boolean isFeatureSupported(String feature) {
        LOG.atDebug().log("isFeatureSupported(feature)");
        return false;
    }
}