001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the License). You may not use this file except in
005: * compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * Header Notice in each file and include the License file
014: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
015: * If applicable, add the following below the CDDL Header,
016: * with the fields enclosed by brackets [] replaced by
017: * you own identifying information:
018: * "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
021: */
022:
023: package com.sun.xml.ws.security.opt.crypto.dsig;
024:
025: import org.jvnet.staxex.NamespaceContextEx;
026: import com.sun.xml.wss.XWSSecurityException;
027: import com.sun.xml.ws.security.opt.crypto.JAXBData;
028: import com.sun.xml.ws.security.opt.crypto.StreamWriterData;
029: import com.sun.xml.wss.impl.c14n.StAXEXC14nCanonicalizerImpl;
030: import com.sun.xml.wss.impl.misc.UnsyncByteArrayOutputStream;
031: import com.sun.xml.wss.logging.LogDomainConstants;
032: import java.io.ByteArrayInputStream;
033: import java.io.OutputStream;
034: import java.security.InvalidAlgorithmParameterException;
035: import java.security.spec.AlgorithmParameterSpec;
036: import java.util.Iterator;
037: import java.util.logging.Logger;
038: import javax.xml.crypto.Data;
039: import javax.xml.crypto.MarshalException;
040: import javax.xml.crypto.OctetStreamData;
041: import javax.xml.crypto.XMLCryptoContext;
042: import javax.xml.crypto.XMLStructure;
043: import javax.xml.crypto.dsig.TransformException;
044: import javax.xml.crypto.dsig.TransformService;
045: import javax.xml.crypto.dsig.spec.ExcC14NParameterSpec;
046: import javax.xml.crypto.dsig.spec.TransformParameterSpec;
047: import javax.xml.stream.XMLStreamException;
048: import com.sun.xml.wss.logging.impl.opt.signature.LogStringsMessages;
049: import java.util.logging.Level;
050:
051: /**
052: *
053: * @author K.Venugopal@sun.com
054: */
055: public class Exc14nCanonicalizer extends TransformService {
056:
057: private static final Logger logger = Logger.getLogger(
058: LogDomainConstants.IMPL_OPT_SIGNATURE_DOMAIN,
059: LogDomainConstants.IMPL_OPT_SIGNATURE_DOMAIN_BUNDLE);
060:
061: StAXEXC14nCanonicalizerImpl _canonicalizer = new StAXEXC14nCanonicalizerImpl();
062: UnsyncByteArrayOutputStream baos = new UnsyncByteArrayOutputStream();
063: TransformParameterSpec _transformParameterSpec;
064:
065: /** Creates a new instance of Exc14nCanonicalizer */
066: public Exc14nCanonicalizer() {
067: }
068:
069: public void init(TransformParameterSpec transformParameterSpec)
070: throws InvalidAlgorithmParameterException {
071: _transformParameterSpec = transformParameterSpec;
072: }
073:
074: public void marshalParams(XMLStructure xMLStructure,
075: XMLCryptoContext xMLCryptoContext) throws MarshalException {
076: }
077:
078: public void init(XMLStructure xMLStructure,
079: XMLCryptoContext xMLCryptoContext)
080: throws InvalidAlgorithmParameterException {
081: }
082:
083: public AlgorithmParameterSpec getParameterSpec() {
084: return _transformParameterSpec;
085: }
086:
087: public Data transform(Data data, XMLCryptoContext xMLCryptoContext)
088: throws TransformException {
089: _canonicalizer.setStream(baos);
090: _canonicalizer.reset();
091:
092: if (data instanceof StreamWriterData) {
093: StreamWriterData swd = (StreamWriterData) data;
094: NamespaceContextEx nc = swd.getNamespaceContext();
095: Iterator<NamespaceContextEx.Binding> itr = nc.iterator();
096:
097: while (itr.hasNext()) {
098: final NamespaceContextEx.Binding nd = itr.next();
099: try {
100: _canonicalizer.writeNamespace(nd.getPrefix(), nd
101: .getNamespaceURI());
102: } catch (XMLStreamException ex) {
103: throw new TransformException(ex);
104: }
105: }
106: try {
107: ExcC14NParameterSpec spec = (ExcC14NParameterSpec) _transformParameterSpec;
108: if (spec != null) {
109: _canonicalizer.setInclusivePrefixList(spec
110: .getPrefixList());
111: }
112: swd.write(_canonicalizer);
113: _canonicalizer.flush();
114: } catch (XMLStreamException ex) {
115: logger.log(Level.SEVERE, LogStringsMessages
116: .WSS_1759_TRANSFORM_ERROR(ex.getMessage()), ex);
117: throw new TransformException(ex);
118: }
119:
120: return new OctetStreamData(new ByteArrayInputStream(baos
121: .getBytes(), 0, baos.getLength()));
122: }
123: throw new UnsupportedOperationException("Data type" + data
124: + " not yet supported");
125: }
126:
127: public Data transform(Data data, XMLCryptoContext xMLCryptoContext,
128: OutputStream outputStream) throws TransformException {
129: _canonicalizer.setStream(outputStream);
130: _canonicalizer.reset();
131:
132: if (data instanceof StreamWriterData) {
133: StreamWriterData swd = (StreamWriterData) data;
134: NamespaceContextEx nc = swd.getNamespaceContext();
135: Iterator<NamespaceContextEx.Binding> itr = nc.iterator();
136:
137: while (itr.hasNext()) {
138: final NamespaceContextEx.Binding nd = itr.next();
139: try {
140: _canonicalizer.writeNamespace(nd.getPrefix(), nd
141: .getNamespaceURI());
142: } catch (XMLStreamException ex) {
143: logger.log(Level.SEVERE, LogStringsMessages
144: .WSS_1759_TRANSFORM_ERROR(ex.getMessage()),
145: ex);
146: throw new TransformException(ex);
147: }
148: }
149: try {
150: ExcC14NParameterSpec spec = (ExcC14NParameterSpec) _transformParameterSpec;
151: if (spec != null) {
152: _canonicalizer.setInclusivePrefixList(spec
153: .getPrefixList());
154: }
155: swd.write(_canonicalizer);
156: _canonicalizer.flush();
157: } catch (XMLStreamException ex) {
158: logger.log(Level.SEVERE, LogStringsMessages
159: .WSS_1759_TRANSFORM_ERROR(ex.getMessage()), ex);
160: throw new TransformException(ex);
161: }
162:
163: return null;
164: } else if (data instanceof JAXBData) {
165: JAXBData jd = (JAXBData) data;
166: NamespaceContextEx nc = jd.getNamespaceContext();
167: Iterator<NamespaceContextEx.Binding> itr = nc.iterator();
168:
169: while (itr.hasNext()) {
170: final NamespaceContextEx.Binding nd = itr.next();
171: try {
172: _canonicalizer.writeNamespace(nd.getPrefix(), nd
173: .getNamespaceURI());
174: } catch (XMLStreamException ex) {
175: logger.log(Level.SEVERE, LogStringsMessages
176: .WSS_1759_TRANSFORM_ERROR(ex.getMessage()),
177: ex);
178: throw new TransformException(ex);
179: }
180: }
181:
182: try {
183: ExcC14NParameterSpec spec = (ExcC14NParameterSpec) _transformParameterSpec;
184: if (spec != null) {
185: _canonicalizer.setInclusivePrefixList(spec
186: .getPrefixList());
187: }
188: jd.writeTo(_canonicalizer);
189: _canonicalizer.flush();
190: } catch (XMLStreamException ex) {
191: logger.log(Level.SEVERE, LogStringsMessages
192: .WSS_1759_TRANSFORM_ERROR(ex.getMessage()), ex);
193: throw new TransformException(ex);
194: } catch (XWSSecurityException ex) {
195: throw new TransformException(ex);
196: }
197:
198: return null;
199: }
200: throw new UnsupportedOperationException("Data type" + data
201: + " not yet supported");
202: }
203:
204: public boolean isFeatureSupported(String string) {
205: return true;
206: }
207:
208: }
|