001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.security.interceptors;
023:
024: import java.io.Serializable;
025: import java.security.GeneralSecurityException;
026: import java.security.InvalidAlgorithmParameterException;
027: import java.util.Arrays;
028: import java.util.Iterator;
029: import java.util.Set;
030: import javax.crypto.Cipher;
031: import javax.crypto.SealedObject;
032: import javax.crypto.SecretKey;
033: import javax.crypto.spec.IvParameterSpec;
034: import javax.security.auth.Subject;
035:
036: import org.jboss.ejb.Container;
037: import org.jboss.ejb.Interceptor;
038: import org.jboss.ejb.plugins.AbstractInterceptor;
039: import org.jboss.invocation.Invocation;
040: import org.jboss.security.SecurityAssociation;
041: import org.jboss.security.srp.SRPParameters;
042:
043: /** A server side interceptor that encrypts
044:
045: @author Scott.Stark@jboss.org
046: @version $Revision: 57211 $
047: */
048: public class ServerEncryptionInterceptor extends AbstractInterceptor {
049: /** The is initialized the first time */
050: private Cipher decryptCipher;
051: private Cipher encryptCipher;
052: private Container container;
053:
054: /** Creates a new instance of EncryptionInterceptor */
055: public ServerEncryptionInterceptor() {
056: }
057:
058: public void setContainer(Container container) {
059: this .container = container;
060: }
061:
062: public Container getContainer() {
063: return container;
064: }
065:
066: public Object invoke(Invocation mi) throws Exception {
067: if (decryptCipher == null) {
068: Subject subject = SecurityAssociation.getSubject();
069: initCipher(subject);
070: }
071:
072: log.debug("invoke mi=" + mi.getMethod());
073: // Check for arguments to decrypt
074: Object[] args = mi.getArguments();
075: int length = args != null ? args.length : 0;
076: for (int a = 0; a < length; a++) {
077: if ((args[a] instanceof SealedObject) == false)
078: continue;
079: SealedObject sarg = (SealedObject) args[a];
080: Object arg = sarg.getObject(decryptCipher);
081: args[a] = arg;
082: log.debug(" Unsealed arg(" + a + "): " + arg);
083: }
084: // We must set the arguments because args[] may be a copy
085: mi.setArguments(args);
086:
087: Interceptor next = getNext();
088: Object value = next.invoke(mi);
089: if (value instanceof Serializable) {
090: Serializable svalue = (Serializable) value;
091: value = new SealedObject(svalue, encryptCipher);
092: }
093: return value;
094: }
095:
096: private void initCipher(Subject subject)
097: throws GeneralSecurityException {
098: Set credentials = subject
099: .getPrivateCredentials(SecretKey.class);
100: Iterator iter = credentials.iterator();
101: SecretKey key = null;
102: while (iter.hasNext()) {
103: key = (SecretKey) iter.next();
104: }
105: if (key == null)
106: throw new GeneralSecurityException(
107: "Failed to find SecretKey in Subject.PrivateCredentials");
108:
109: credentials = subject
110: .getPrivateCredentials(SRPParameters.class);
111: iter = credentials.iterator();
112: SRPParameters params = null;
113: while (iter.hasNext()) {
114: params = (SRPParameters) iter.next();
115: }
116: if (params == null)
117: throw new GeneralSecurityException(
118: "Failed to find SRPParameters in Subject.PrivateCredentials");
119:
120: encryptCipher = Cipher.getInstance(key.getAlgorithm());
121: encryptCipher.init(Cipher.ENCRYPT_MODE, key);
122: decryptCipher = Cipher.getInstance(key.getAlgorithm());
123: decryptCipher.init(Cipher.DECRYPT_MODE, key);
124: }
125: }
|