KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > security > interceptors > ServerEncryptionInterceptor


1 /*
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.
6   *
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.
11   *
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.
16   *
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.
21   */

22 package org.jboss.test.security.interceptors;
23
24 import java.io.Serializable JavaDoc;
25 import java.security.GeneralSecurityException JavaDoc;
26 import java.security.InvalidAlgorithmParameterException JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Set JavaDoc;
30 import javax.crypto.Cipher;
31 import javax.crypto.SealedObject;
32 import javax.crypto.SecretKey;
33 import javax.crypto.spec.IvParameterSpec;
34 import javax.security.auth.Subject JavaDoc;
35
36 import org.jboss.ejb.Container;
37 import org.jboss.ejb.Interceptor;
38 import org.jboss.ejb.plugins.AbstractInterceptor;
39 import org.jboss.invocation.Invocation;
40 import org.jboss.security.SecurityAssociation;
41 import org.jboss.security.srp.SRPParameters;
42
43 /** A server side interceptor that encrypts
44
45 @author Scott.Stark@jboss.org
46 @version $Revision: 37406 $
47 */

48 public class ServerEncryptionInterceptor extends AbstractInterceptor
49 {
50    /** The is initialized the first time */
51    private Cipher decryptCipher;
52    private Cipher encryptCipher;
53    private Container container;
54
55    /** Creates a new instance of EncryptionInterceptor */
56    public ServerEncryptionInterceptor()
57    {
58    }
59
60    public void setContainer(Container container)
61    {
62       this.container = container;
63    }
64
65    public Container getContainer()
66    {
67       return container;
68    }
69
70    public Object JavaDoc invoke(Invocation mi) throws Exception JavaDoc
71    {
72       if( decryptCipher == null )
73       {
74          Subject JavaDoc subject = SecurityAssociation.getSubject();
75          initCipher(subject);
76       }
77
78       log.debug("invoke mi="+mi.getMethod());
79       // Check for arguments to decrypt
80
Object JavaDoc[] args = mi.getArguments();
81       int length = args != null ? args.length : 0;
82       for(int a = 0; a < length; a ++)
83       {
84          if( (args[a] instanceof SealedObject) == false )
85             continue;
86          SealedObject sarg = (SealedObject) args[a];
87          Object JavaDoc arg = sarg.getObject(decryptCipher);
88          args[a] = arg;
89          log.debug(" Unsealed arg("+a+"): "+arg);
90       }
91       // We must set the arguments because args[] may be a copy
92
mi.setArguments(args);
93
94       Interceptor next = getNext();
95       Object JavaDoc value = next.invoke(mi);
96       if( value instanceof Serializable JavaDoc )
97       {
98          Serializable JavaDoc svalue = (Serializable JavaDoc) value;
99          value = new SealedObject(svalue, encryptCipher);
100       }
101       return value;
102    }
103
104    private void initCipher(Subject JavaDoc subject) throws GeneralSecurityException JavaDoc
105    {
106       Set JavaDoc credentials = subject.getPrivateCredentials(SecretKey.class);
107       Iterator JavaDoc iter = credentials.iterator();
108       SecretKey key = null;
109       while( iter.hasNext() )
110       {
111          key = (SecretKey) iter.next();
112       }
113       if( key == null )
114          throw new GeneralSecurityException JavaDoc("Failed to find SecretKey in Subject.PrivateCredentials");
115
116       credentials = subject.getPrivateCredentials(SRPParameters.class);
117       iter = credentials.iterator();
118       SRPParameters params = null;
119       while( iter.hasNext() )
120       {
121          params = (SRPParameters) iter.next();
122       }
123       if( params == null )
124          throw new GeneralSecurityException JavaDoc("Failed to find SRPParameters in Subject.PrivateCredentials");
125
126       encryptCipher = Cipher.getInstance(key.getAlgorithm());
127       encryptCipher.init(Cipher.ENCRYPT_MODE, key);
128       decryptCipher = Cipher.getInstance(key.getAlgorithm());
129       decryptCipher.init(Cipher.DECRYPT_MODE, key);
130    }
131 }
132
Popular Tags