J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * reserved comment block |
| 3 | * DO NOT REMOVE OR ALTER! |
| 4 | */ |
| 5 | /* |
| 6 | * Copyright 1999-2004 The Apache Software Foundation. |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | * you may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
| 19 | * |
| 20 | */ |
| 21 | package com.sun.org.apache.xml.internal.security.transforms.implementations; |
| 22 | |
| 23 | |
| 24 | |
| 25 | import com.sun.org.apache.xml.internal.security.signature.NodeFilter; |
| 26 | import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput; |
| 27 | import com.sun.org.apache.xml.internal.security.transforms.TransformSpi; |
| 28 | import com.sun.org.apache.xml.internal.security.transforms.TransformationException; |
| 29 | import com.sun.org.apache.xml.internal.security.transforms.Transforms; |
| 30 | import com.sun.org.apache.xml.internal.security.utils.Constants; |
| 31 | import com.sun.org.apache.xml.internal.security.utils.XMLUtils; |
| 32 | import org.w3c.dom.Element; |
| 33 | import org.w3c.dom.Node; |
| 34 | |
| 35 | |
| 36 | /** |
| 37 | * Implements the <CODE>http://www.w3.org/2000/09/xmldsig#enveloped-signature</CODE> |
| 38 | * transform. |
| 39 | * |
| 40 | * @author Christian Geuer-Pollmann |
| 41 | */ |
| 42 | public class TransformEnvelopedSignature extends TransformSpi { |
| 43 | |
| 44 | /** Field implementedTransformURI */ |
| 45 | public static final String implementedTransformURI = |
| 46 | Transforms.TRANSFORM_ENVELOPED_SIGNATURE; |
| 47 | |
| 48 | /** |
| 49 | * Method engineGetURI |
| 50 | * |
| 51 | * @inheritDoc |
| 52 | */ |
| 53 | protected String engineGetURI() { |
| 54 | return implementedTransformURI; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @inheritDoc |
| 59 | */ |
| 60 | protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input) |
| 61 | throws TransformationException { |
| 62 | |
| 63 | |
| 64 | |
| 65 | /** |
| 66 | * If the actual input is an octet stream, then the application MUST |
| 67 | * convert the octet stream to an XPath node-set suitable for use by |
| 68 | * Canonical XML with Comments. (A subsequent application of the |
| 69 | * REQUIRED Canonical XML algorithm would strip away these comments.) |
| 70 | * |
| 71 | * ... |
| 72 | * |
| 73 | * The evaluation of this expression includes all of the document's nodes |
| 74 | * (including comments) in the node-set representing the octet stream. |
| 75 | */ |
| 76 | |
| 77 | /* |
| 78 | if (input.isOctetStream()) { |
| 79 | input.setNodesetXPath(Canonicalizer.XPATH_C14N_WITH_COMMENTS); |
| 80 | } |
| 81 | */ |
| 82 | |
| 83 | Element transformElement = this._transformObject.getElement(); |
| 84 | Node signatureElement = transformElement; |
| 85 | |
| 86 | |
| 87 | signatureElement = searchSignatureElement(signatureElement); |
| 88 | input.setExcludeNode(signatureElement); |
| 89 | input.addNodeFilter(new EnvelopedNodeFilter(signatureElement)); |
| 90 | return input; |
| 91 | |
| 92 | // |
| 93 | |
| 94 | |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * @param signatureElement |
| 99 | * @return the node that is the signature |
| 100 | * @throws TransformationException |
| 101 | */ |
| 102 | private static Node searchSignatureElement(Node signatureElement) throws TransformationException { |
| 103 | boolean found=false; |
| 104 | |
| 105 | while (true) { |
| 106 | if ((signatureElement == null) |
| 107 | || (signatureElement.getNodeType() == Node.DOCUMENT_NODE)) { |
| 108 | break; |
| 109 | } |
| 110 | Element el=(Element)signatureElement; |
| 111 | if (el.getNamespaceURI().equals(Constants.SignatureSpecNS) |
| 112 | && |
| 113 | el.getLocalName().equals(Constants._TAG_SIGNATURE)) { |
| 114 | found = true; |
| 115 | break; |
| 116 | } |
| 117 | |
| 118 | signatureElement = signatureElement.getParentNode(); |
| 119 | } |
| 120 | |
| 121 | if (!found) { |
| 122 | throw new TransformationException( |
| 123 | "envelopedSignatureTransformNotInSignatureElement"); |
| 124 | } |
| 125 | return signatureElement; |
| 126 | } |
| 127 | class EnvelopedNodeFilter implements NodeFilter { |
| 128 | Node exclude; |
| 129 | EnvelopedNodeFilter(Node n) { |
| 130 | exclude=n; |
| 131 | } |
| 132 | /** |
| 133 | * @see com.sun.org.apache.xml.internal.security.signature.NodeFilter#isNodeInclude(org.w3c.dom.Node) |
| 134 | */ |
| 135 | public boolean isNodeInclude(Node n) { |
| 136 | // TODO Optimize me. |
| 137 | return !XMLUtils.isDescendantOrSelf(exclude,n); |
| 138 | } |
| 139 | |
| 140 | } |
| 141 | } |