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.InputStream;
27
import java.io.ObjectInputStream;
30
import javax.crypto.Cipher;
31
import javax.crypto.CipherInputStream;
33
import org.jboss.remoting.marshal.UnMarshaller;
34
import org.jboss.remoting.marshal.VersionedUnMarshaller;
35
import org.jboss.remoting.marshal.serializable.SerializableUnMarshaller;
36
import org.jboss.remoting.serialization.SerializationManager;
37
import org.jboss.remoting.serialization.SerializationStreamFactory;
41
* <code>EncryptingMarshaller</code> and <code>EncryptingUnMarshaller</code> are a general
42
* purpose encrypting marshaller / decompressing unmarshaller pair based on
43
* Java's crypto stream facilities.
45
* <code>EncryptingUnMarshaller</code> is subclassed from <code>SerializableUnMarshaller</code>,
46
* and by default it uses <code>super.read()</code> to deserialize an object, once the object has been
47
* decrypted. Optionally, it can wrap any other unmarshaller and use that instead of
48
* <code>SerializableUnMarshaller</code> to unmarshall an encrypted input stream. For example,
50
* <center><code>new EncryptingUnMarshaller(new HTTPUnMarshaller())</code></center
52
* will create an umarshaller that
53
* uses an <code>HTTPUnMarshaller</code> to restore an unencrypted input stream.
55
* @author Anil.Saldhana@jboss.org
56
* @version $Revision: 3584 $
59
public class EncryptingUnMarshaller extends SerializableUnMarshaller
61
/** The serialVersionUID */
62
private static final long serialVersionUID = 1L;
64
public final static String DATATYPE = "encrypt";
66
private UnMarshaller wrappedUnMarshaller;
68
private String cipherAlgorithm = EncryptionManager.DEFAULT_CIPHER_ALGORITHM;
70
private Cipher cipher = EncryptionManager.getCipher(Cipher.DECRYPT_MODE, cipherAlgorithm);
74
* Create a new EncryptingUnMarshaller.
76
public EncryptingUnMarshaller()
82
* Create a new EncryptingUnMarshaller.
84
* @param unMarshaller unmarshaller to be used to restore
85
* unencrypted byte stream to original object
87
public EncryptingUnMarshaller(UnMarshaller unMarshaller)
89
wrappedUnMarshaller = unMarshaller;
93
* Set the Cipher Algorithm to use
95
* @see EncryptionManager#DEFAULT_CIPHER_ALGORITHM
97
public void setCipherAlgorithm(String algo)
99
this.cipherAlgorithm = algo;
100
cipher = EncryptionManager.getCipher(Cipher.DECRYPT_MODE, this.cipherAlgorithm);
103
public InputStream getMarshallingStream(InputStream inputStream) throws IOException
109
* Restores a encrypted, marshalled form of an object to its original state.
111
* @param inputStream <code>InputStream</code> from which marshalled form is to be retrieved
112
* @param metadata can be any transport specific metadata (such as headers from http transport).
113
* This can be null, depending on if transport supports metadata.
114
* @param version wire format version
115
* @return restored object
116
* @throws IOException if there is a problem reading from <code>inputStream</code>
117
* @throws ClassNotFoundException if there is a problem finding a class needed for unmarshalling
119
public Object read(InputStream inputStream, Map metadata, int version) throws IOException, ClassNotFoundException
122
throw new IllegalStateException("Cipher is null for algo="+ this.cipherAlgorithm);
123
CipherInputStream cis = new CipherInputStream(inputStream,cipher);
124
SerializationManager sm = SerializationStreamFactory.getManagerInstance(getSerializationType());
125
ObjectInputStream ois = sm.createRegularInput(cis);
128
if(wrappedUnMarshaller != null)
130
if (wrappedUnMarshaller instanceof VersionedUnMarshaller)
131
return ((VersionedUnMarshaller)wrappedUnMarshaller).read(ois, metadata, version);
133
obj = wrappedUnMarshaller.read(ois, metadata);
137
obj = super.read(ois, metadata, version);
143
* Returns a new <code>EncryptingUnMarshaller</code>
145
* @return a new <code>EncryptingUnMarshaller</code>
146
* @throws CloneNotSupportedException In practice no exceptions are thrown.
148
public UnMarshaller cloneUnMarshaller() throws CloneNotSupportedException
150
EncryptingUnMarshaller um = new EncryptingUnMarshaller(wrappedUnMarshaller);
151
um.setCipherAlgorithm(this.cipherAlgorithm);