KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
28 import java.util.Set JavaDoc;
29 import javax.crypto.Cipher;
30 import javax.crypto.SealedObject;
31 import javax.crypto.SecretKey;
32 import javax.crypto.spec.IvParameterSpec;
33 import javax.security.auth.Subject JavaDoc;
34
35 import org.jboss.logging.Logger;
36
37 import org.jboss.invocation.Invocation;
38 import org.jboss.proxy.Interceptor;
39 import org.jboss.security.SecurityAssociation;
40 import org.jboss.security.srp.SRPParameters;
41
42 /** A client side interceptor that encrypts
43
44 @author Scott.Stark@jboss.org
45 @version $Revision: 58115 $
46 */

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