1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /* 6 * Copyright 2005 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 /* 22 * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved. 23 */ 24 /* 25 * $Id: DOMTransform.java,v 1.2 2008/07/24 15:20:32 mullan Exp $ 26 */ 27 package org.jcp.xml.dsig.internal.dom; 28 29 import java.io.OutputStream; 30 import java.security.InvalidAlgorithmParameterException; 31 import java.security.NoSuchAlgorithmException; 32 import java.security.Provider; 33 import java.security.spec.AlgorithmParameterSpec; 34 35 import org.w3c.dom.Document; 36 import org.w3c.dom.Element; 37 import org.w3c.dom.Node; 38 import org.w3c.dom.NodeList; 39 40 import javax.xml.crypto.*; 41 import javax.xml.crypto.dsig.*; 42 import javax.xml.crypto.dom.DOMCryptoContext; 43 import javax.xml.crypto.dsig.dom.DOMSignContext; 44 import javax.xml.crypto.dsig.spec.TransformParameterSpec; 45 46 /** 47 * DOM-based abstract implementation of Transform. 48 * 49 * @author Sean Mullan 50 */ 51 public class DOMTransform extends DOMStructure implements Transform { 52 53 protected TransformService spi; 54 55 /** 56 * Creates a <code>DOMTransform</code>. 57 * 58 * @param spi the TransformService 59 */ 60 public DOMTransform(TransformService spi) { 61 this.spi = spi; 62 } 63 64 /** 65 * Creates a <code>DOMTransform</code> from an element. This constructor 66 * invokes the abstract {@link #unmarshalParams unmarshalParams} method to 67 * unmarshal any algorithm-specific input parameters. 68 * 69 * @param transElem a Transform element 70 */ 71 public DOMTransform(Element transElem, XMLCryptoContext context, 72 Provider provider) throws MarshalException { 73 String algorithm = DOMUtils.getAttributeValue(transElem, "Algorithm"); 74 try { 75 spi = TransformService.getInstance(algorithm, "DOM"); 76 } catch (NoSuchAlgorithmException e1) { 77 try { 78 spi = TransformService.getInstance(algorithm, "DOM", provider); 79 } catch (NoSuchAlgorithmException e2) { 80 throw new MarshalException(e2); 81 } 82 } 83 try { 84 spi.init(new javax.xml.crypto.dom.DOMStructure(transElem), context); 85 } catch (InvalidAlgorithmParameterException iape) { 86 throw new MarshalException(iape); 87 } 88 } 89 90 public final AlgorithmParameterSpec getParameterSpec() { 91 return spi.getParameterSpec(); 92 } 93 94 public final String getAlgorithm() { 95 return spi.getAlgorithm(); 96 } 97 98 /** 99 * This method invokes the abstract {@link #marshalParams marshalParams} 100 * method to marshal any algorithm-specific parameters. 101 */ 102 public void marshal(Node parent, String dsPrefix, DOMCryptoContext context) 103 throws MarshalException { 104 Document ownerDoc = DOMUtils.getOwnerDocument(parent); 105 106 Element transformElem = null; 107 if (parent.getLocalName().equals("Transforms")) { 108 transformElem = DOMUtils.createElement 109 (ownerDoc, "Transform", XMLSignature.XMLNS, dsPrefix); 110 } else { 111 transformElem = DOMUtils.createElement 112 (ownerDoc, "CanonicalizationMethod", XMLSignature.XMLNS, dsPrefix); 113 } 114 DOMUtils.setAttribute(transformElem, "Algorithm", getAlgorithm()); 115 116 spi.marshalParams 117 (new javax.xml.crypto.dom.DOMStructure(transformElem), context); 118 119 parent.appendChild(transformElem); 120 } 121 122 /** 123 * Transforms the specified data using the underlying transform algorithm. 124 * 125 * @param data the data to be transformed 126 * @param sc the <code>XMLCryptoContext</code> containing 127 * additional context (may be <code>null</code> if not applicable) 128 * @return the transformed data 129 * @throws NullPointerException if <code>data</code> is <code>null</code> 130 * @throws XMLSignatureException if an unexpected error occurs while 131 * executing the transform 132 */ 133 public Data transform(Data data, XMLCryptoContext xc) 134 throws TransformException { 135 return spi.transform(data, xc); 136 } 137 138 /** 139 * Transforms the specified data using the underlying transform algorithm. 140 * 141 * @param data the data to be transformed 142 * @param sc the <code>XMLCryptoContext</code> containing 143 * additional context (may be <code>null</code> if not applicable) 144 * @param os the <code>OutputStream</code> that should be used to write 145 * the transformed data to 146 * @return the transformed data 147 * @throws NullPointerException if <code>data</code> is <code>null</code> 148 * @throws XMLSignatureException if an unexpected error occurs while 149 * executing the transform 150 */ 151 public Data transform(Data data, XMLCryptoContext xc, OutputStream os) 152 throws TransformException { 153 return spi.transform(data, xc, os); 154 } 155 156 public boolean equals(Object o) { 157 if (this == o) { 158 return true; 159 } 160 161 if (!(o instanceof Transform)) { 162 return false; 163 } 164 Transform otransform = (Transform) o; 165 166 return (getAlgorithm().equals(otransform.getAlgorithm()) && 167 DOMUtils.paramsEqual 168 (getParameterSpec(), otransform.getParameterSpec())); 169 } 170 171 /** 172 * Transforms the specified data using the underlying transform algorithm. 173 * This method invokes the {@link #marshal marshal} method and passes it 174 * the specified <code>DOMSignContext</code> before transforming the data. 175 * 176 * @param data the data to be transformed 177 * @param sc the <code>XMLCryptoContext</code> containing 178 * additional context (may be <code>null</code> if not applicable) 179 * @param context the marshalling context 180 * @return the transformed data 181 * @throws MarshalException if an exception occurs while marshalling 182 * @throws NullPointerException if <code>data</code> or <code>context</code> 183 * is <code>null</code> 184 * @throws XMLSignatureException if an unexpected error occurs while 185 * executing the transform 186 */ 187 Data transform(Data data, XMLCryptoContext xc, DOMSignContext context) 188 throws MarshalException, TransformException { 189 marshal(context.getParent(), 190 DOMUtils.getSignaturePrefix(context), context); 191 return transform(data, xc); 192 } 193 } | 1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /** 6 * Licensed to the Apache Software Foundation (ASF) under one 7 * or more contributor license agreements. See the NOTICE file 8 * distributed with this work for additional information 9 * regarding copyright ownership. The ASF licenses this file 10 * to you under the Apache License, Version 2.0 (the 11 * "License"); you may not use this file except in compliance 12 * with the License. You may obtain a copy of the License at 13 * 14 * http://www.apache.org/licenses/LICENSE-2.0 15 * 16 * Unless required by applicable law or agreed to in writing, 17 * software distributed under the License is distributed on an 18 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 19 * KIND, either express or implied. See the License for the 20 * specific language governing permissions and limitations 21 * under the License. 22 */ 23 /* 24 * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved. 25 */ 26 /* 27 * $Id: DOMTransform.java 1333415 2012-05-03 12:03:51Z coheigea $ 28 */ 29 package org.jcp.xml.dsig.internal.dom; 30 31 import java.io.OutputStream; 32 import java.security.InvalidAlgorithmParameterException; 33 import java.security.NoSuchAlgorithmException; 34 import java.security.Provider; 35 import java.security.spec.AlgorithmParameterSpec; 36 37 import org.w3c.dom.Document; 38 import org.w3c.dom.Element; 39 import org.w3c.dom.Node; 40 41 import javax.xml.crypto.*; 42 import javax.xml.crypto.dsig.*; 43 import javax.xml.crypto.dom.DOMCryptoContext; 44 import javax.xml.crypto.dsig.dom.DOMSignContext; 45 46 /** 47 * DOM-based abstract implementation of Transform. 48 * 49 * @author Sean Mullan 50 */ 51 public class DOMTransform extends DOMStructure implements Transform { 52 53 protected TransformService spi; 54 55 /** 56 * Creates a <code>DOMTransform</code>. 57 * 58 * @param spi the TransformService 59 */ 60 public DOMTransform(TransformService spi) { 61 this.spi = spi; 62 } 63 64 /** 65 * Creates a <code>DOMTransform</code> from an element. This constructor 66 * invokes the abstract {@link #unmarshalParams unmarshalParams} method to 67 * unmarshal any algorithm-specific input parameters. 68 * 69 * @param transElem a Transform element 70 */ 71 public DOMTransform(Element transElem, XMLCryptoContext context, 72 Provider provider) 73 throws MarshalException 74 { 75 String algorithm = DOMUtils.getAttributeValue(transElem, "Algorithm"); 76 77 if (provider == null) { 78 try { 79 spi = TransformService.getInstance(algorithm, "DOM"); 80 } catch (NoSuchAlgorithmException e1) { 81 throw new MarshalException(e1); 82 } 83 } else { 84 try { 85 spi = TransformService.getInstance(algorithm, "DOM", provider); 86 } catch (NoSuchAlgorithmException nsae) { 87 try { 88 spi = TransformService.getInstance(algorithm, "DOM"); 89 } catch (NoSuchAlgorithmException e2) { 90 throw new MarshalException(e2); 91 } 92 } 93 } 94 try { 95 spi.init(new javax.xml.crypto.dom.DOMStructure(transElem), context); 96 } catch (InvalidAlgorithmParameterException iape) { 97 throw new MarshalException(iape); 98 } 99 } 100 101 public final AlgorithmParameterSpec getParameterSpec() { 102 return spi.getParameterSpec(); 103 } 104 105 public final String getAlgorithm() { 106 return spi.getAlgorithm(); 107 } 108 109 /** 110 * This method invokes the abstract {@link #marshalParams marshalParams} 111 * method to marshal any algorithm-specific parameters. 112 */ 113 public void marshal(Node parent, String dsPrefix, DOMCryptoContext context) 114 throws MarshalException 115 { 116 Document ownerDoc = DOMUtils.getOwnerDocument(parent); 117 118 Element transformElem = null; 119 if (parent.getLocalName().equals("Transforms")) { 120 transformElem = DOMUtils.createElement(ownerDoc, "Transform", 121 XMLSignature.XMLNS, 122 dsPrefix); 123 } else { 124 transformElem = DOMUtils.createElement(ownerDoc, 125 "CanonicalizationMethod", 126 XMLSignature.XMLNS, 127 dsPrefix); 128 } 129 DOMUtils.setAttribute(transformElem, "Algorithm", getAlgorithm()); 130 131 spi.marshalParams(new javax.xml.crypto.dom.DOMStructure(transformElem), 132 context); 133 134 parent.appendChild(transformElem); 135 } 136 137 /** 138 * Transforms the specified data using the underlying transform algorithm. 139 * 140 * @param data the data to be transformed 141 * @param sc the <code>XMLCryptoContext</code> containing 142 * additional context (may be <code>null</code> if not applicable) 143 * @return the transformed data 144 * @throws NullPointerException if <code>data</code> is <code>null</code> 145 * @throws XMLSignatureException if an unexpected error occurs while 146 * executing the transform 147 */ 148 public Data transform(Data data, XMLCryptoContext xc) 149 throws TransformException 150 { 151 return spi.transform(data, xc); 152 } 153 154 /** 155 * Transforms the specified data using the underlying transform algorithm. 156 * 157 * @param data the data to be transformed 158 * @param sc the <code>XMLCryptoContext</code> containing 159 * additional context (may be <code>null</code> if not applicable) 160 * @param os the <code>OutputStream</code> that should be used to write 161 * the transformed data to 162 * @return the transformed data 163 * @throws NullPointerException if <code>data</code> is <code>null</code> 164 * @throws XMLSignatureException if an unexpected error occurs while 165 * executing the transform 166 */ 167 public Data transform(Data data, XMLCryptoContext xc, OutputStream os) 168 throws TransformException 169 { 170 return spi.transform(data, xc, os); 171 } 172 173 @Override 174 public boolean equals(Object o) { 175 if (this == o) { 176 return true; 177 } 178 179 if (!(o instanceof Transform)) { 180 return false; 181 } 182 Transform otransform = (Transform)o; 183 184 return (getAlgorithm().equals(otransform.getAlgorithm()) && 185 DOMUtils.paramsEqual(getParameterSpec(), 186 otransform.getParameterSpec())); 187 } 188 189 @Override 190 public int hashCode() { 191 int result = 17; 192 result = 31 * result + getAlgorithm().hashCode(); 193 AlgorithmParameterSpec spec = getParameterSpec(); 194 if (spec != null) { 195 result = 31 * result + spec.hashCode(); 196 } 197 198 return result; 199 } 200 201 /** 202 * Transforms the specified data using the underlying transform algorithm. 203 * This method invokes the {@link #marshal marshal} method and passes it 204 * the specified <code>DOMSignContext</code> before transforming the data. 205 * 206 * @param data the data to be transformed 207 * @param sc the <code>XMLCryptoContext</code> containing 208 * additional context (may be <code>null</code> if not applicable) 209 * @param context the marshalling context 210 * @return the transformed data 211 * @throws MarshalException if an exception occurs while marshalling 212 * @throws NullPointerException if <code>data</code> or <code>context</code> 213 * is <code>null</code> 214 * @throws XMLSignatureException if an unexpected error occurs while 215 * executing the transform 216 */ 217 Data transform(Data data, XMLCryptoContext xc, DOMSignContext context) 218 throws MarshalException, TransformException 219 { 220 marshal(context.getParent(), 221 DOMUtils.getSignaturePrefix(context), context); 222 return transform(data, xc); 223 } 224 } |