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: /*
024: * DOMSTRTransform.java
025: *
026: * Created on February 22, 2005, 2:18 PM
027: */
028:
029: package com.sun.xml.wss.impl.transform;
030:
031: import com.sun.xml.wss.impl.XMLUtil;
032: import com.sun.xml.wss.impl.MessageConstants;
033: import com.sun.xml.wss.logging.LogDomainConstants;
034:
035: import java.io.OutputStream;
036:
037: import java.security.InvalidAlgorithmParameterException;
038: import java.security.spec.AlgorithmParameterSpec;
039: import java.util.logging.Level;
040: import java.util.logging.Logger;
041:
042: import javax.xml.crypto.Data;
043: import javax.xml.crypto.MarshalException;
044: import javax.xml.crypto.XMLCryptoContext;
045: import javax.xml.crypto.XMLStructure;
046: import javax.xml.crypto.dom.DOMStructure;
047: import javax.xml.crypto.dsig.CanonicalizationMethod;
048: import javax.xml.crypto.dsig.TransformService;
049: import javax.xml.crypto.dsig.XMLSignature;
050: import javax.xml.crypto.dsig.spec.C14NMethodParameterSpec;
051: import javax.xml.crypto.dsig.spec.TransformParameterSpec;
052: import javax.xml.crypto.dsig.TransformException;
053:
054: import org.w3c.dom.Document;
055: import org.w3c.dom.Element;
056: import org.w3c.dom.Node;
057: import org.w3c.dom.NodeList;
058:
059: /**
060: *
061: * @author K.Venugopal@sun.com
062: * @author Sean Mullan
063: */
064: public class DOMSTRTransform extends TransformService {
065: private STRTransformParameterSpec params;
066: private static Logger logger = Logger.getLogger(
067: LogDomainConstants.IMPL_SIGNATURE_DOMAIN,
068: LogDomainConstants.IMPL_SIGNATURE_DOMAIN_BUNDLE);
069: public static final String WSSE = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
070:
071: public static final String WSU = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd";
072:
073: public void init(TransformParameterSpec params)
074: throws InvalidAlgorithmParameterException {
075: if (params == null) {
076: throw new InvalidAlgorithmParameterException(
077: "params are required");
078: }
079: this .params = (STRTransformParameterSpec) params;
080: }
081:
082: public void init(javax.xml.crypto.XMLStructure params,
083: javax.xml.crypto.XMLCryptoContext xMLCryptoContext)
084: throws java.security.InvalidAlgorithmParameterException {
085: DOMStructure domParams = (DOMStructure) params;
086: try {
087: unmarshalParams(domParams.getNode(), xMLCryptoContext);
088: } catch (MarshalException me) {
089: throw new InvalidAlgorithmParameterException(me
090: .getMessage());
091: }
092: }
093:
094: public java.security.spec.AlgorithmParameterSpec getParameterSpec() {
095: return params;
096: }
097:
098: public void marshalParams(XMLStructure parent,
099: XMLCryptoContext context) throws MarshalException {
100:
101: Node pn = ((DOMStructure) parent).getNode();
102: Document ownerDoc = XMLUtil.getOwnerDocument(pn);
103:
104: String prefix = null;
105: String dsPrefix = null;
106: if (context != null) {
107: prefix = context.getNamespacePrefix(WSSE, "wsse");
108: dsPrefix = context.getNamespacePrefix(XMLSignature.XMLNS,
109: context.getDefaultNamespacePrefix());
110: }
111:
112: Element transformParamElem = XMLUtil.createElement(ownerDoc,
113: "TransformationParameters", WSSE, prefix);
114:
115: CanonicalizationMethod cm = params.getCanonicalizationMethod();
116: Element c14nElem = XMLUtil.createElement(ownerDoc,
117: "CanonicalizationMethod", XMLSignature.XMLNS, dsPrefix);
118: c14nElem.setAttributeNS(null, "Algorithm", cm.getAlgorithm());
119:
120: C14NMethodParameterSpec cs = (C14NMethodParameterSpec) cm
121: .getParameterSpec();
122: if (cs != null) {
123: TransformService cmSpi = null;
124: try {
125: cmSpi = TransformService.getInstance(cm.getAlgorithm(),
126: "DOM");
127: cmSpi.init(cs);
128: cmSpi
129: .marshalParams(new DOMStructure(c14nElem),
130: context);
131: } catch (Exception e) {
132: logger.log(Level.SEVERE,
133: "WSS1321.str_marshal.transform.error", e);
134: throw new MarshalException(e);
135: }
136: }
137:
138: transformParamElem.appendChild(c14nElem);
139: pn.appendChild(transformParamElem);
140: }
141:
142: public javax.xml.crypto.Data transform(javax.xml.crypto.Data data,
143: javax.xml.crypto.XMLCryptoContext xc)
144: throws javax.xml.crypto.dsig.TransformException {
145: java.io.OutputStream outputStream = null;
146: return new STRTransformImpl().transform(data, xc, outputStream);
147: }
148:
149: public javax.xml.crypto.Data transform(javax.xml.crypto.Data data,
150: javax.xml.crypto.XMLCryptoContext xc,
151: java.io.OutputStream outputStream)
152: throws javax.xml.crypto.dsig.TransformException {
153: //throw new UnsupportedOperationException();
154: return new STRTransformImpl().transform(data, xc, outputStream);
155: }
156:
157: public void unmarshalParams(XMLStructure parent,
158: XMLCryptoContext context) throws MarshalException,
159: java.security.InvalidAlgorithmParameterException {
160:
161: Element transformElem = (Element) ((DOMStructure) parent)
162: .getNode();
163: Element tpElem = XMLUtil.getFirstChildElement(transformElem);
164: unmarshalParams(tpElem, context);
165: }
166:
167: private void unmarshalParams(Node tpElem, XMLCryptoContext context)
168: throws MarshalException,
169: java.security.InvalidAlgorithmParameterException {
170:
171: Element c14nElem = null;
172: if (tpElem.getNodeType() == Node.DOCUMENT_NODE) {
173: c14nElem = (Element) ((Document) tpElem).getFirstChild();
174: } else {
175: c14nElem = XMLUtil.getFirstChildElement(tpElem);
176: }
177:
178: if (!"CanonicalizationMethod".equals(c14nElem.getLocalName())) {
179: NodeList nl = c14nElem.getElementsByTagNameNS(
180: MessageConstants.DSIG_NS, "CanonicalizationMethod");
181: if (nl.getLength() > 0)
182: c14nElem = (Element) nl.item(0);
183: }
184: if (c14nElem == null) {
185: throw new java.security.InvalidAlgorithmParameterException(
186: "Cannont find CanonicalizationMethod in TransformationParameters element");
187: }
188: String c14nAlg = c14nElem.getAttributeNodeNS(null, "Algorithm")
189: .getValue();
190: if (logger.isLoggable(Level.FINE)) {
191: logger.log(Level.FINE, "C14 Algo=" + c14nAlg);
192: }
193: C14NMethodParameterSpec cs = null;
194: Element paramsElem = XMLUtil.getFirstChildElement(c14nElem);
195: javax.xml.crypto.dsig.TransformService cmSpi = null;
196: try {
197: cmSpi = javax.xml.crypto.dsig.TransformService.getInstance(
198: c14nAlg, "DOM");
199: if (paramsElem != null) {
200: cmSpi.init(new DOMStructure(paramsElem), context);
201: //cs = (C14NMethodParameterSpec) cmSpi.getParameterSpec();
202: }
203: CanonicalizationMethod cm = new STRC14NMethod(cmSpi);
204: this .params = new STRTransformParameterSpec(cm);
205: } catch (Throwable e) {
206: logger.log(Level.SEVERE, "WSS1320.str_un.transform.error",
207: e);
208: throw new MarshalException(e);
209: }
210:
211: }
212:
213: public boolean isFeatureSupported(String str) {
214: return false;
215: }
216:
217: private static class STRC14NMethod implements
218: CanonicalizationMethod {
219: private javax.xml.crypto.dsig.TransformService cmSpi;
220:
221: STRC14NMethod(javax.xml.crypto.dsig.TransformService cmSpi) {
222: this .cmSpi = cmSpi;
223: }
224:
225: public String getAlgorithm() {
226: return cmSpi.getAlgorithm();
227: }
228:
229: public AlgorithmParameterSpec getParameterSpec() {
230: return cmSpi.getParameterSpec();
231: }
232:
233: public boolean isFeatureSupported(String feature) {
234: return false;
235: }
236:
237: public Data transform(Data data, XMLCryptoContext context)
238: throws TransformException {
239: return cmSpi.transform(data, context);
240: }
241:
242: public Data transform(Data data, XMLCryptoContext context,
243: OutputStream os) throws TransformException {
244: return cmSpi.transform(data, context, os);
245: }
246: }
247:
248: public static class STRTransformParameterSpec implements
249: TransformParameterSpec {
250: private CanonicalizationMethod c14nMethod;
251:
252: public STRTransformParameterSpec(
253: CanonicalizationMethod c14nMethod) {
254: this .c14nMethod = c14nMethod;
255: }
256:
257: public CanonicalizationMethod getCanonicalizationMethod() {
258: return c14nMethod;
259: }
260: }
261: }
|