0N/A/*
661N/A * reserved comment block
661N/A * DO NOT REMOVE OR ALTER!
0N/A */
0N/A/*
661N/A * Copyright 2005 The Apache Software Foundation.
661N/A *
661N/A * Licensed under the Apache License, Version 2.0 (the "License");
661N/A * you may not use this file except in compliance with the License.
661N/A * You may obtain a copy of the License at
661N/A *
661N/A * http://www.apache.org/licenses/LICENSE-2.0
661N/A *
661N/A * Unless required by applicable law or agreed to in writing, software
661N/A * distributed under the License is distributed on an "AS IS" BASIS,
661N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
661N/A * See the License for the specific language governing permissions and
661N/A * limitations under the License.
661N/A *
661N/A */
661N/A/*
2362N/A * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
661N/A */
661N/A/*
661N/A * $Id: ApacheCanonicalizer.java,v 1.2 2008/07/24 15:20:32 mullan Exp $
0N/A */
0N/Apackage org.jcp.xml.dsig.internal.dom;
0N/A
0N/Aimport java.io.ByteArrayInputStream;
0N/Aimport java.io.ByteArrayOutputStream;
0N/Aimport java.io.IOException;
0N/Aimport java.io.OutputStream;
0N/Aimport java.security.spec.AlgorithmParameterSpec;
0N/Aimport java.security.InvalidAlgorithmParameterException;
0N/Aimport java.util.Set;
0N/Aimport java.util.logging.Logger;
0N/Aimport java.util.logging.Level;
0N/Aimport javax.xml.crypto.*;
0N/Aimport javax.xml.crypto.dom.DOMCryptoContext;
0N/Aimport javax.xml.crypto.dsig.TransformException;
0N/Aimport javax.xml.crypto.dsig.TransformService;
0N/Aimport javax.xml.crypto.dsig.XMLSignatureException;
0N/Aimport javax.xml.crypto.dsig.spec.C14NMethodParameterSpec;
0N/A
0N/Aimport com.sun.org.apache.xml.internal.security.c14n.Canonicalizer;
0N/Aimport com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException;
0N/Aimport com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
0N/Aimport com.sun.org.apache.xml.internal.security.transforms.Transform;
0N/Aimport org.w3c.dom.Document;
0N/Aimport org.w3c.dom.Element;
0N/Aimport org.w3c.dom.NodeList;
0N/A
0N/Apublic abstract class ApacheCanonicalizer extends TransformService {
0N/A
661N/A static {
661N/A com.sun.org.apache.xml.internal.security.Init.init();
661N/A }
661N/A
0N/A private static Logger log = Logger.getLogger("org.jcp.xml.dsig.internal.dom");
0N/A protected Canonicalizer apacheCanonicalizer;
0N/A private Transform apacheTransform;
0N/A protected String inclusiveNamespaces;
0N/A protected C14NMethodParameterSpec params;
0N/A protected Document ownerDoc;
0N/A protected Element transformElem;
0N/A
0N/A public final AlgorithmParameterSpec getParameterSpec() {
0N/A return params;
0N/A }
0N/A
0N/A public void init(XMLStructure parent, XMLCryptoContext context)
0N/A throws InvalidAlgorithmParameterException {
0N/A if (context != null && !(context instanceof DOMCryptoContext)) {
0N/A throw new ClassCastException
0N/A ("context must be of type DOMCryptoContext");
0N/A }
0N/A transformElem = (Element)
0N/A ((javax.xml.crypto.dom.DOMStructure) parent).getNode();
0N/A ownerDoc = DOMUtils.getOwnerDocument(transformElem);
0N/A }
0N/A
0N/A public void marshalParams(XMLStructure parent, XMLCryptoContext context)
0N/A throws MarshalException {
0N/A if (context != null && !(context instanceof DOMCryptoContext)) {
0N/A throw new ClassCastException
0N/A ("context must be of type DOMCryptoContext");
0N/A }
0N/A transformElem = (Element)
0N/A ((javax.xml.crypto.dom.DOMStructure) parent).getNode();
0N/A ownerDoc = DOMUtils.getOwnerDocument(transformElem);
0N/A }
0N/A
0N/A public Data canonicalize(Data data, XMLCryptoContext xc)
0N/A throws TransformException {
0N/A return canonicalize(data, xc, null);
0N/A }
0N/A
0N/A public Data canonicalize(Data data, XMLCryptoContext xc, OutputStream os)
0N/A throws TransformException {
0N/A
0N/A if (apacheCanonicalizer == null) {
0N/A try {
0N/A apacheCanonicalizer = Canonicalizer.getInstance(getAlgorithm());
0N/A if (log.isLoggable(Level.FINE)) {
0N/A log.log(Level.FINE, "Created canonicalizer for algorithm: "
0N/A + getAlgorithm());
0N/A }
0N/A } catch (InvalidCanonicalizerException ice) {
0N/A throw new TransformException
0N/A ("Couldn't find Canonicalizer for: " + getAlgorithm() +
0N/A ": " + ice.getMessage(), ice);
0N/A }
0N/A }
0N/A
0N/A if (os != null) {
0N/A apacheCanonicalizer.setWriter(os);
0N/A } else {
0N/A apacheCanonicalizer.setWriter(new ByteArrayOutputStream());
0N/A }
0N/A
0N/A try {
0N/A Set nodeSet = null;
0N/A if (data instanceof ApacheData) {
0N/A XMLSignatureInput in =
0N/A ((ApacheData) data).getXMLSignatureInput();
0N/A if (in.isElement()) {
0N/A if (inclusiveNamespaces != null) {
0N/A return new OctetStreamData(new ByteArrayInputStream
0N/A (apacheCanonicalizer.canonicalizeSubtree
0N/A (in.getSubNode(), inclusiveNamespaces)));
0N/A } else {
0N/A return new OctetStreamData(new ByteArrayInputStream
0N/A (apacheCanonicalizer.canonicalizeSubtree
0N/A (in.getSubNode())));
0N/A }
0N/A } else if (in.isNodeSet()) {
0N/A nodeSet = in.getNodeSet();
0N/A } else {
0N/A return new OctetStreamData(new ByteArrayInputStream(
0N/A apacheCanonicalizer.canonicalize(
0N/A Utils.readBytesFromStream(in.getOctetStream()))));
0N/A }
0N/A } else if (data instanceof DOMSubTreeData) {
0N/A DOMSubTreeData subTree = (DOMSubTreeData) data;
0N/A if (inclusiveNamespaces != null) {
0N/A return new OctetStreamData(new ByteArrayInputStream
0N/A (apacheCanonicalizer.canonicalizeSubtree
0N/A (subTree.getRoot(), inclusiveNamespaces)));
0N/A } else {
0N/A return new OctetStreamData(new ByteArrayInputStream
0N/A (apacheCanonicalizer.canonicalizeSubtree
0N/A (subTree.getRoot())));
0N/A }
0N/A } else if (data instanceof NodeSetData) {
0N/A NodeSetData nsd = (NodeSetData) data;
0N/A // convert Iterator to Set
0N/A nodeSet = Utils.toNodeSet(nsd.iterator());
0N/A if (log.isLoggable(Level.FINE)) {
0N/A log.log(Level.FINE, "Canonicalizing " + nodeSet.size()
0N/A + " nodes");
0N/A }
0N/A } else {
0N/A return new OctetStreamData(new ByteArrayInputStream(
0N/A apacheCanonicalizer.canonicalize(
0N/A Utils.readBytesFromStream(
0N/A ((OctetStreamData)data).getOctetStream()))));
0N/A }
0N/A if (inclusiveNamespaces != null) {
0N/A return new OctetStreamData(new ByteArrayInputStream(
0N/A apacheCanonicalizer.canonicalizeXPathNodeSet
0N/A (nodeSet, inclusiveNamespaces)));
0N/A } else {
0N/A return new OctetStreamData(new ByteArrayInputStream(
0N/A apacheCanonicalizer.canonicalizeXPathNodeSet(nodeSet)));
0N/A }
0N/A } catch (Exception e) {
0N/A throw new TransformException(e);
0N/A }
0N/A }
0N/A
0N/A public Data transform(Data data, XMLCryptoContext xc, OutputStream os)
0N/A throws TransformException {
0N/A if (data == null) {
0N/A throw new NullPointerException("data must not be null");
0N/A }
0N/A if (os == null) {
0N/A throw new NullPointerException("output stream must not be null");
0N/A }
0N/A
0N/A if (ownerDoc == null) {
0N/A throw new TransformException("transform must be marshalled");
0N/A }
0N/A
0N/A if (apacheTransform == null) {
0N/A try {
6159N/A apacheTransform = new Transform
0N/A (ownerDoc, getAlgorithm(), transformElem.getChildNodes());
0N/A apacheTransform.setElement(transformElem, xc.getBaseURI());
0N/A if (log.isLoggable(Level.FINE)) {
0N/A log.log(Level.FINE, "Created transform for algorithm: "
0N/A + getAlgorithm());
0N/A }
0N/A } catch (Exception ex) {
0N/A throw new TransformException
0N/A ("Couldn't find Transform for: " + getAlgorithm(), ex);
0N/A }
0N/A }
0N/A
0N/A XMLSignatureInput in;
0N/A if (data instanceof ApacheData) {
0N/A if (log.isLoggable(Level.FINE)) {
0N/A log.log(Level.FINE, "ApacheData = true");
0N/A }
0N/A in = ((ApacheData) data).getXMLSignatureInput();
0N/A } else if (data instanceof NodeSetData) {
0N/A if (log.isLoggable(Level.FINE)) {
0N/A log.log(Level.FINE, "isNodeSet() = true");
0N/A }
0N/A if (data instanceof DOMSubTreeData) {
0N/A DOMSubTreeData subTree = (DOMSubTreeData) data;
0N/A in = new XMLSignatureInput(subTree.getRoot());
0N/A in.setExcludeComments(subTree.excludeComments());
0N/A } else {
0N/A Set nodeSet =
0N/A Utils.toNodeSet(((NodeSetData) data).iterator());
0N/A in = new XMLSignatureInput(nodeSet);
0N/A }
0N/A } else {
0N/A if (log.isLoggable(Level.FINE)) {
0N/A log.log(Level.FINE, "isNodeSet() = false");
0N/A }
0N/A try {
0N/A in = new XMLSignatureInput
0N/A (((OctetStreamData)data).getOctetStream());
0N/A } catch (Exception ex) {
0N/A throw new TransformException(ex);
0N/A }
0N/A }
0N/A
0N/A try {
661N/A in = apacheTransform.performTransform(in, os);
661N/A if (!in.isNodeSet() && !in.isElement()) {
661N/A return null;
0N/A }
0N/A if (in.isOctetStream()) {
0N/A return new ApacheOctetStreamData(in);
0N/A } else {
0N/A return new ApacheNodeSetData(in);
0N/A }
0N/A } catch (Exception ex) {
0N/A throw new TransformException(ex);
0N/A }
0N/A }
0N/A
0N/A public final boolean isFeatureSupported(String feature) {
0N/A if (feature == null) {
0N/A throw new NullPointerException();
0N/A } else {
0N/A return false;
0N/A }
0N/A }
0N/A}