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: * EncryptedData.java
025: *
026: * Created on September 13, 2005, 2:11 PM
027: *
028: * To change this template, choose Tools | Options and locate the template under
029: * the Source Creation and Management node. Right-click the template and choose
030: * Open. You can then make changes to the template in the Source Editor.
031: */
032:
033: package com.sun.xml.wss.core;
034:
035: import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
036: import com.sun.xml.wss.impl.MessageConstants;
037: import com.sun.xml.wss.impl.misc.Base64;
038: import com.sun.xml.wss.impl.misc.ByteArray;
039: import java.io.ByteArrayOutputStream;
040: import java.io.IOException;
041: import java.io.OutputStream;
042:
043: /**
044: * Simple EncryptedData for Sign and Encrypt Usecase.
045: * @author K.Venugopal@sun.com
046: */
047: public class EncryptedDataImpl extends ByteArrayOutputStream {
048: private byte[] iv = null;
049: private byte[] encryptedData = null;
050: private String id = null;
051: private String mimeType = null;
052: private String encoding = null;
053: private String type = null;
054: private KeyInfoHeaderBlock keyInfo = null;
055: private String encAlgo = null;
056: /** Creates a new instance of EncryptedData */
057: private static final byte[] ENCRYPTED_DATA = MessageConstants.ENCRYPTED_DATA_LNAME
058: .getBytes();
059: private static final byte[] ENC_PREFIX = MessageConstants.XENC_PREFIX
060: .getBytes();
061: private static final byte[] ENC_NS = MessageConstants.XENC_NS
062: .getBytes();
063: private static byte[] OPENTAG = "<".getBytes();
064: private static byte[] CLOSETAG = ">".getBytes();
065: private static byte[] ENDTAG = "</".getBytes();
066: private static byte[] CLOSEELEMENT = "/>".getBytes();
067: private static byte[] ENCRYPTION_METHOD = "EncryptionMethod "
068: .getBytes();
069: private static byte[] ALGORITHM = "Algorithm ".getBytes();
070: private static byte[] XMLNS = "xmlns".getBytes();
071: private static byte[] ID = "Id".getBytes();
072:
073: private static byte[] CIPHER_DATA = "CipherData".getBytes();
074: private static byte[] CIPHER_VALUE = "CipherValue".getBytes();
075: private static byte[] TYPE = "Type".getBytes();
076: private static byte[] CONTENT_ONLY = "http://www.w3.org/2001/04/xmlenc#Content"
077: .getBytes();
078: private XMLSerializer xmlSerializer = null;
079:
080: public EncryptedDataImpl() {
081: }
082:
083: public byte[] getIv() {
084: return iv;
085: }
086:
087: public void setIv(byte[] iv) {
088: this .iv = iv;
089: }
090:
091: public byte[] getEncryptedData() {
092: return encryptedData;
093: }
094:
095: public void setEncryptedData(byte[] encryptedData) {
096: this .encryptedData = encryptedData;
097: }
098:
099: public KeyInfoHeaderBlock getKeyInfo() {
100: return keyInfo;
101: }
102:
103: public void setKeyInfo(KeyInfoHeaderBlock keyInfo) {
104: this .keyInfo = keyInfo;
105: }
106:
107: public String getId() {
108: return id;
109: }
110:
111: public void setId(String id) {
112: this .id = id;
113: }
114:
115: public String getMimeType() {
116: return mimeType;
117: }
118:
119: public void setMimeType(String mimeType) {
120: this .mimeType = mimeType;
121: }
122:
123: public String getEncoding() {
124: return encoding;
125: }
126:
127: public void setEncoding(String encoding) {
128: this .encoding = encoding;
129: }
130:
131: public String getType() {
132: return type;
133: }
134:
135: public void setType(String type) {
136: this .type = type;
137: }
138:
139: public void setXMLSerializer(XMLSerializer xmlWriter) {
140: xmlSerializer = xmlWriter;
141: }
142:
143: public void writeTo(OutputStream stream) throws IOException {
144:
145: stream.write(OPENTAG);
146: stream.write(ENC_PREFIX);
147: stream.write(':');
148: stream.write(ENCRYPTED_DATA);
149: stream.write(' ');
150: stream.write(XMLNS);
151: stream.write(':');
152: stream.write(ENC_PREFIX);
153:
154: stream.write('=');
155: stream.write('"');
156: stream.write(ENC_NS);
157: stream.write('"');
158: stream.write(' ');
159: if (getId() != null) {
160: stream.write(ID);
161: stream.write('=');
162: stream.write('"');
163: stream.write(getId().getBytes());
164: stream.write('"');
165: }
166: stream.write(' ');
167: stream.write(TYPE);
168: stream.write('=');
169: stream.write('"');
170: stream.write(CONTENT_ONLY);
171: stream.write('"');
172: stream.write(CLOSETAG);
173: stream.write(OPENTAG);
174: stream.write(ENC_PREFIX);
175: stream.write(':');
176: stream.write(ENCRYPTION_METHOD);
177: stream.write(' ');
178: stream.write(ALGORITHM);
179:
180: stream.write('=');
181: stream.write('"');
182: stream.write(getEncAlgo().getBytes());
183: stream.write('"');
184: stream.write(CLOSEELEMENT);
185: stream.write(OPENTAG);
186: stream.write(ENC_PREFIX);
187: stream.write(':');
188:
189: stream.write(CIPHER_DATA);
190: stream.write(CLOSETAG);
191: try {
192: if (keyInfo != null) {
193: xmlSerializer.setOutputByteStream(stream);
194: xmlSerializer.serialize(keyInfo.getAsSoapElement());
195: // xmlSerializer.reset();
196: }
197: } catch (Exception ex) {
198: throw new RuntimeException(ex);
199: }
200: stream.write(OPENTAG);
201: stream.write(ENC_PREFIX);
202: stream.write(':');
203: stream.write(CIPHER_VALUE);
204: stream.write(CLOSETAG);
205: //Base64.encodeToStream(getEncryptedData(),Base64.BASE64DEFAULTLENGTH,stream);
206: Base64.encodeToStream(new ByteArray(iv, encryptedData),
207: Base64.BASE64DEFAULTLENGTH, stream);
208: stream.write(ENDTAG);
209: stream.write(ENC_PREFIX);
210: stream.write(':');
211: stream.write(CIPHER_VALUE);
212: stream.write(CLOSETAG);
213: stream.write(ENDTAG);
214: stream.write(ENC_PREFIX);
215: stream.write(':');
216: stream.write(CIPHER_DATA);
217: stream.write(CLOSETAG);
218: stream.write(ENDTAG);
219: stream.write(ENC_PREFIX);
220: stream.write(':');
221: stream.write(ENCRYPTED_DATA);
222: stream.write(CLOSETAG);
223:
224: }
225:
226: public String getEncAlgo() {
227: return encAlgo;
228: }
229:
230: public void setEncAlgo(String encAlgo) {
231: this.encAlgo = encAlgo;
232: }
233:
234: }
|