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: * ACTranform.java
025: *
026: * Created on March 16, 2005, 2:14 PM
027: */
028:
029: package com.sun.xml.wss.impl.transform;
030:
031: import com.sun.xml.wss.impl.misc.UnsyncByteArrayOutputStream;
032: import com.sun.xml.wss.logging.LogDomainConstants;
033: import java.util.logging.Level;
034: import java.util.logging.Logger;
035: import javax.xml.crypto.dsig.TransformService;
036:
037: import com.sun.xml.wss.impl.c14n.Canonicalizer;
038: import com.sun.xml.wss.impl.c14n.CanonicalizerFactory;
039: import com.sun.xml.wss.impl.c14n.MimeHeaderCanonicalizer;
040: import com.sun.xml.wss.impl.dsig.AttachmentData;
041: import java.io.ByteArrayInputStream;
042: import java.io.ByteArrayOutputStream;
043: import java.io.InputStream;
044: import java.io.OutputStream;
045: import java.util.Iterator;
046:
047: import javax.xml.crypto.Data;
048: import javax.xml.crypto.OctetStreamData;
049: import javax.xml.soap.AttachmentPart;
050:
051: /**
052: *
053: * @author K.Venugopal@sun.com
054: */
055: public class ACTransform extends TransformService {
056: private static Logger logger = Logger.getLogger(
057: LogDomainConstants.IMPL_SIGNATURE_DOMAIN,
058: LogDomainConstants.IMPL_SIGNATURE_DOMAIN_BUNDLE);
059:
060: /** Creates a new instance of ACTranform */
061: public ACTransform() {
062: }
063:
064: public void init(
065: javax.xml.crypto.dsig.spec.TransformParameterSpec transformParameterSpec)
066: throws java.security.InvalidAlgorithmParameterException {
067: }
068:
069: public void init(javax.xml.crypto.XMLStructure xMLStructure,
070: javax.xml.crypto.XMLCryptoContext xMLCryptoContext)
071: throws java.security.InvalidAlgorithmParameterException {
072: }
073:
074: public java.security.spec.AlgorithmParameterSpec getParameterSpec() {
075: return null;
076: }
077:
078: public void marshalParams(
079: javax.xml.crypto.XMLStructure xMLStructure,
080: javax.xml.crypto.XMLCryptoContext xMLCryptoContext)
081: throws javax.xml.crypto.MarshalException {
082: }
083:
084: /*
085: private Data canonicalize(OctetStreamData octetData,javax.xml.crypto.XMLCryptoContext xMLCryptoContext) throws Exception {
086: Vector mimeHeaders = (Vector)xMLCryptoContext.getProperty(MessageConstants.ATTACHMENT_MIME_HEADERS);
087: InputStream os = octetData.getOctetStream();
088: //Revisit ::
089: // rf. RFC822
090: MimeHeaderCanonicalizer mHCanonicalizer = CanonicalizerFactory.getMimeHeaderCanonicalizer("US-ASCII");
091: byte[] outputHeaderBytes = mHCanonicalizer._canonicalize(mimeHeaders);
092: Canonicalizer canonicalizer = CanonicalizerFactory.getCanonicalizer(octetData.getMimeType());
093: InputStream is = canonicalizer.canonicalize(os);
094: ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
095: byteStream.write(outputHeaderBytes);
096: int len=0;
097: byte [] data= null;
098: try{
099: len = is.read(data);
100: } catch (IOException e) {
101: // log me
102: throw new XWSSecurityException(e);
103: }
104:
105: while(len > 0){
106: try {
107: byteStream.write(data);
108: len = is.read(data);
109: } catch (IOException e) {
110: // log me
111: throw new XWSSecurityException(e);
112: }
113: }
114: return new OctetStreamData(new ByteArrayInputStream(byteStream.toByteArray()));
115: }
116: */
117:
118: private Data canonicalize(AttachmentData attachmentData,
119: OutputStream outputStream)
120: throws javax.xml.crypto.dsig.TransformException {
121: try {
122: AttachmentPart attachment = attachmentData
123: .getAttachmentPart();
124: Iterator mimeHeaders = attachment.getAllMimeHeaders();
125: //Revisit ::
126: // rf. RFC822
127: MimeHeaderCanonicalizer mHCanonicalizer = CanonicalizerFactory
128: .getMimeHeaderCanonicalizer("US-ASCII");
129: byte[] outputHeaderBytes = mHCanonicalizer
130: ._canonicalize(mimeHeaders);
131: OutputStream byteStream = new UnsyncByteArrayOutputStream();
132: attachment.getDataHandler().writeTo(byteStream);
133: ByteArrayInputStream byteInputStream = new ByteArrayInputStream(
134: ((ByteArrayOutputStream) byteStream).toByteArray());
135: byteStream.close();
136: if (outputStream == null) {
137: byteStream = new ByteArrayOutputStream();
138: } else {
139: byteStream = outputStream;
140: }
141: byteStream.write(outputHeaderBytes);
142: Canonicalizer canonicalizer = CanonicalizerFactory
143: .getCanonicalizer(attachment.getContentType());
144: InputStream is = canonicalizer.canonicalize(
145: byteInputStream, byteStream);
146: if (is != null)
147: return new OctetStreamData(is);
148: return null;
149: } catch (javax.xml.crypto.dsig.TransformException te) {
150: logger.log(Level.SEVERE, "WSS1319.aco.transform.error", te);
151: throw te;
152: } catch (Exception ex) {
153: logger.log(Level.SEVERE, "WSS1319.aco.transform.error", ex);
154: throw new javax.xml.crypto.dsig.TransformException(ex
155: .getMessage());
156: }
157: }
158:
159: public boolean isFeatureSupported(String str) {
160: return false;
161: }
162:
163: public javax.xml.crypto.Data transform(javax.xml.crypto.Data data,
164: javax.xml.crypto.XMLCryptoContext xMLCryptoContext)
165: throws javax.xml.crypto.dsig.TransformException {
166: if (data instanceof AttachmentData) {
167: try {
168: return canonicalize((AttachmentData) data, null);
169: } catch (javax.xml.crypto.dsig.TransformException tex) {
170: logger.log(Level.SEVERE, "WSS1319.aco.transform.error",
171: tex);
172: throw tex;
173: } catch (Exception ex) {
174: logger.log(Level.SEVERE, "WSS1319.aco.transform.error",
175: ex);
176: throw new RuntimeException(ex);
177: }
178: } else {
179: //TODO::
180: throw new UnsupportedOperationException();
181: }
182: }
183:
184: public javax.xml.crypto.Data transform(javax.xml.crypto.Data data,
185: javax.xml.crypto.XMLCryptoContext xMLCryptoContext,
186: java.io.OutputStream outputStream)
187: throws javax.xml.crypto.dsig.TransformException {
188: if (data instanceof AttachmentData) {
189: return canonicalize((AttachmentData) data, outputStream);
190: } else {
191: //TODO::
192: throw new UnsupportedOperationException();
193: }
194: }
195:
196: }
|