2
* JBoss, Home of Professional Open Source
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
* by the @authors tag. See the copyright.txt in the distribution for a
5
* full listing of individual contributors.
7
* This is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU Lesser General Public License as
9
* published by the Free Software Foundation; either version 2.1 of
10
* the License, or (at your option) any later version.
12
* This software is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this software; if not, write to the Free
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23
package org.jboss.remoting.marshal.encryption;
25
import java.io.IOException;
26
import java.io.ObjectOutputStream;
27
import java.io.OutputStream;
28
import java.security.Key;
30
import javax.crypto.Cipher;
31
import javax.crypto.CipherOutputStream;
33
import org.jboss.remoting.marshal.Marshaller;
34
import org.jboss.remoting.marshal.VersionedMarshaller;
35
import org.jboss.remoting.marshal.serializable.SerializableMarshaller;
36
import org.jboss.remoting.serialization.SerializationManager;
37
import org.jboss.remoting.serialization.SerializationStreamFactory;
41
* <code>EncryptingMarshaller</code> and <code>EncryptingMarshaller</code> are a general
42
* purpose encryption based marshaller / decompressing unmarshaller pair
43
* based on Java's Cipher facilities.
45
* <code>EncryptingMarshaller</code> is subclassed from <code>SerializableMarshaller</code>,
46
* and by default it uses <code>super.write()</code> to marshall an object, which is then
47
* encrypted. Optionally, it can wrap any other marshaller and use that instead of
48
* <code>SerializableMarshaller</code> to marshall an object before it is encrypted.
51
* <center><code>new EncryptingMarshaller(new HTTPMarshaller())</code></center>
53
* will create a marshaller that encrypts the output of an <code>HTTPMarshaller</code>.
55
* @author <a href="mailto:anil.saldhana@jboss.com">Anil Saldhana</a>
58
public class EncryptingMarshaller extends SerializableMarshaller
60
/** The serialVersionUID */
61
private static final long serialVersionUID = 1L;
63
public final static String DATATYPE = "encrypt";
65
private Marshaller wrappedMarshaller;
67
private String cipherAlgorithm = EncryptionManager.DEFAULT_CIPHER_ALGORITHM;
69
private Cipher cipher = EncryptionManager.getCipher(Cipher.ENCRYPT_MODE, cipherAlgorithm);
72
* Create a new EncryptingMarshaller.
74
public EncryptingMarshaller()
80
* Create a new EncryptingMarshaller.
82
* @param algo Cipher Algorithm
84
* @see #setCipherAlgorithm(String)
86
public EncryptingMarshaller(String algo, Key key)
88
cipher = EncryptionManager.getCipher(Cipher.ENCRYPT_MODE, algo, key);
93
* Create a new EncryptingMarshaller.
95
* @param marshaller A <code>Marshaller</code> which is used to turn objects into byte streams.
97
public EncryptingMarshaller(Marshaller marshaller)
99
wrappedMarshaller = marshaller;
103
* Set the Cipher Algorithm to use
105
* @see EncryptionManager#DEFAULT_CIPHER_ALGORITHM
107
public void setCipherAlgorithm(String algo)
109
this.cipherAlgorithm = algo;
110
cipher = EncryptionManager.getCipher(Cipher.ENCRYPT_MODE, this.cipherAlgorithm);
113
public OutputStream getMarshallingStream(OutputStream outputStream) throws IOException
119
* Writes encrypted, marshalled form of <code>dataObject</code> to <code>output</code>.
121
* @param dataObject arbitrary object to be marshalled
122
* @param output <code>OutputStream</code> to which <code>output</code> is to be marshalled
123
* @param version wire format version
125
public void write(Object dataObject, OutputStream output, int version) throws IOException
128
throw new IllegalStateException("Cipher is null for algo="+ this.cipherAlgorithm);
131
//EOS intercepts the close() call and does not close the stream
132
EncryptionOutputStream eos = new EncryptionOutputStream(output);
134
CipherOutputStream cos = new CipherOutputStream(eos, cipher);
136
SerializationManager sm = SerializationStreamFactory.getManagerInstance(getSerializationType());
137
ObjectOutputStream oos = sm.createOutput(cos);
139
if(wrappedMarshaller != null)
141
if (wrappedMarshaller instanceof VersionedMarshaller)
142
((VersionedMarshaller) wrappedMarshaller).write(dataObject, oos, version);
144
wrappedMarshaller.write(dataObject, oos);
148
super.write(dataObject, oos, version);
152
//Vagaries of CipherOutputStream which needs a close() to flush at the end
153
cos.close(); //Tests fail without this statement - oos.close() should do it
154
oos.close(); //There is a need to close cos
158
* Returns a <code>EncryptingMarshaller</code>.
160
* @return a <code>EncryptingMarshaller</code>.
161
* @throws CloneNotSupportedException In practice no exceptions are thrown
163
public Marshaller cloneMarshaller() throws CloneNotSupportedException
165
EncryptingMarshaller em = new EncryptingMarshaller(wrappedMarshaller);
166
em.setCipherAlgorithm(this.cipherAlgorithm);