J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | /* |
| 26 | * $Id: DOMExcC14NMethod.java,v 1.28 2005/09/23 20:20:41 mullan Exp $ |
| 27 | */ |
| 28 | package org.jcp.xml.dsig.internal.dom; |
| 29 | |
| 30 | import javax.xml.crypto.*; |
| 31 | import javax.xml.crypto.dsig.*; |
| 32 | import javax.xml.crypto.dsig.spec.C14NMethodParameterSpec; |
| 33 | import javax.xml.crypto.dsig.spec.ExcC14NParameterSpec; |
| 34 | import javax.xml.crypto.dsig.spec.TransformParameterSpec; |
| 35 | |
| 36 | import java.security.InvalidAlgorithmParameterException; |
| 37 | import java.security.spec.AlgorithmParameterSpec; |
| 38 | import java.util.*; |
| 39 | import org.w3c.dom.Element; |
| 40 | |
| 41 | import com.sun.org.apache.xml.internal.security.c14n.Canonicalizer; |
| 42 | import com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException; |
| 43 | |
| 44 | /** |
| 45 | * DOM-based implementation of CanonicalizationMethod for Exclusive |
| 46 | * Canonical XML algorithm (with or without comments). |
| 47 | * Uses Apache XML-Sec Canonicalizer. |
| 48 | * |
| 49 | * @author Sean Mullan |
| 50 | */ |
| 51 | public final class DOMExcC14NMethod extends ApacheCanonicalizer { |
| 52 | |
| 53 | public void init(TransformParameterSpec params) |
| 54 | throws InvalidAlgorithmParameterException { |
| 55 | if (params != null) { |
| 56 | if (!(params instanceof ExcC14NParameterSpec)) { |
| 57 | throw new InvalidAlgorithmParameterException |
| 58 | ("params must be of type ExcC14NParameterSpec"); |
| 59 | } |
| 60 | this.params = (C14NMethodParameterSpec) params; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | public void init(XMLStructure parent, XMLCryptoContext context) |
| 65 | throws InvalidAlgorithmParameterException { |
| 66 | super.init(parent, context); |
| 67 | Element paramsElem = DOMUtils.getFirstChildElement(transformElem); |
| 68 | if (paramsElem == null) { |
| 69 | this.params = null; |
| 70 | this.inclusiveNamespaces = null; |
| 71 | return; |
| 72 | } |
| 73 | unmarshalParams(paramsElem); |
| 74 | } |
| 75 | |
| 76 | private void unmarshalParams(Element paramsElem) { |
| 77 | String prefixListAttr = paramsElem.getAttributeNS(null, "PrefixList"); |
| 78 | this.inclusiveNamespaces = prefixListAttr; |
| 79 | int begin = 0; |
| 80 | int end = prefixListAttr.indexOf(' '); |
| 81 | List prefixList = new ArrayList(); |
| 82 | while (end != -1) { |
| 83 | prefixList.add(prefixListAttr.substring(begin, end)); |
| 84 | begin = end + 1; |
| 85 | end = prefixListAttr.indexOf(' ', begin); |
| 86 | } |
| 87 | if (begin <= prefixListAttr.length()) { |
| 88 | prefixList.add(prefixListAttr.substring(begin)); |
| 89 | } |
| 90 | this.params = new ExcC14NParameterSpec(prefixList); |
| 91 | } |
| 92 | |
| 93 | public void marshalParams(XMLStructure parent, XMLCryptoContext context) |
| 94 | throws MarshalException { |
| 95 | |
| 96 | super.marshalParams(parent, context); |
| 97 | AlgorithmParameterSpec spec = getParameterSpec(); |
| 98 | if (spec == null) { |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | String prefix = |
| 103 | DOMUtils.getNSPrefix(context, CanonicalizationMethod.EXCLUSIVE); |
| 104 | Element excElem = DOMUtils.createElement |
| 105 | (ownerDoc, "InclusiveNamespaces", |
| 106 | CanonicalizationMethod.EXCLUSIVE, prefix); |
| 107 | if (prefix == null) { |
| 108 | excElem.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", |
| 109 | CanonicalizationMethod.EXCLUSIVE); |
| 110 | } else { |
| 111 | excElem.setAttributeNS("http://www.w3.org/2000/xmlns/", |
| 112 | "xmlns:" + prefix, CanonicalizationMethod.EXCLUSIVE); |
| 113 | } |
| 114 | |
| 115 | ExcC14NParameterSpec params = (ExcC14NParameterSpec) spec; |
| 116 | StringBuffer prefixListAttr = new StringBuffer(""); |
| 117 | List prefixList = params.getPrefixList(); |
| 118 | for (int i = 0, size = prefixList.size(); i < size; i++) { |
| 119 | prefixListAttr.append((String) prefixList.get(i)); |
| 120 | if (i < size - 1) { |
| 121 | prefixListAttr.append(" "); |
| 122 | } |
| 123 | } |
| 124 | DOMUtils.setAttribute(excElem, "PrefixList", prefixListAttr.toString()); |
| 125 | this.inclusiveNamespaces = prefixListAttr.toString(); |
| 126 | transformElem.appendChild(excElem); |
| 127 | } |
| 128 | |
| 129 | public String getParamsNSURI() { |
| 130 | return CanonicalizationMethod.EXCLUSIVE; |
| 131 | } |
| 132 | |
| 133 | public Data transform(Data data, XMLCryptoContext xc) |
| 134 | throws TransformException { |
| 135 | |
| 136 | // ignore comments if dereferencing same-document URI that require |
| 137 | // you to omit comments, even if the Transform says otherwise - |
| 138 | // this is to be compliant with section 4.3.3.3 of W3C Rec. |
| 139 | if (data instanceof DOMSubTreeData) { |
| 140 | DOMSubTreeData subTree = (DOMSubTreeData) data; |
| 141 | if (subTree.excludeComments()) { |
| 142 | try { |
| 143 | apacheCanonicalizer = Canonicalizer.getInstance |
| 144 | (CanonicalizationMethod.EXCLUSIVE); |
| 145 | } catch (InvalidCanonicalizerException ice) { |
| 146 | throw new TransformException |
| 147 | ("Couldn't find Canonicalizer for: " + |
| 148 | CanonicalizationMethod.EXCLUSIVE + ": " + |
| 149 | ice.getMessage(), ice); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | return canonicalize(data, xc); |
| 155 | } |
| 156 | } |