1 22 package org.jboss.test.security.interceptors; 23 24 import java.io.Serializable ; 25 import java.security.GeneralSecurityException ; 26 import java.security.InvalidAlgorithmParameterException ; 27 import java.util.Arrays ; 28 import java.util.Iterator ; 29 import java.util.Set ; 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 ; 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 48 public class ServerEncryptionInterceptor extends AbstractInterceptor 49 { 50 51 private Cipher decryptCipher; 52 private Cipher encryptCipher; 53 private Container container; 54 55 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 invoke(Invocation mi) throws Exception 71 { 72 if( decryptCipher == null ) 73 { 74 Subject subject = SecurityAssociation.getSubject(); 75 initCipher(subject); 76 } 77 78 log.debug("invoke mi="+mi.getMethod()); 79 Object [] 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 arg = sarg.getObject(decryptCipher); 88 args[a] = arg; 89 log.debug(" Unsealed arg("+a+"): "+arg); 90 } 91 mi.setArguments(args); 93 94 Interceptor next = getNext(); 95 Object value = next.invoke(mi); 96 if( value instanceof Serializable ) 97 { 98 Serializable svalue = (Serializable ) value; 99 value = new SealedObject(svalue, encryptCipher); 100 } 101 return value; 102 } 103 104 private void initCipher(Subject subject) throws GeneralSecurityException 105 { 106 Set credentials = subject.getPrivateCredentials(SecretKey.class); 107 Iterator 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 ("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 ("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 |