User: starksm 
  Date: 01/04/04 14:39:02

  Modified:    src/main/org/jboss/test LoginModulesTest.java
                        SimpleSRPServer.java
  Added:       src/main/org/jboss/test SRPLoginTest.java
                        SRPProtocolTest.java SRPServerImpl.java
                        SecurityPolicyParserTest.java TestJCE.java
  Log:
  Update the security module tests
  
  Revision  Changes    Path
  1.3       +2 -2      jbosssx/src/main/org/jboss/test/LoginModulesTest.java
  
  Index: LoginModulesTest.java
  ===================================================================
  RCS file: /cvsroot/jboss/jbosssx/src/main/org/jboss/test/LoginModulesTest.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- LoginModulesTest.java     2001/03/29 02:30:34     1.2
  +++ LoginModulesTest.java     2001/04/04 21:39:02     1.3
  @@ -28,13 +28,13 @@
   
   import org.jboss.security.SimpleGroup;
   import org.jboss.security.SimplePrincipal;
  -import org.jboss.security.plugins.UsernamePasswordHandler;
  +import org.jboss.security.auth.UsernamePasswordHandler;
   import org.jboss.security.plugins.samples.UsernamePasswordLoginModule;
   
   /** Tests of the sample LoginModule classes.
   
   @author [EMAIL PROTECTED]
  -@version $Revision: 1.2 $
  +@version $Revision: 1.3 $
   */
   public class LoginModulesTest extends TestCase
   {
  
  
  
  1.2       +41 -25    jbosssx/src/main/org/jboss/test/SimpleSRPServer.java
  
  Index: SimpleSRPServer.java
  ===================================================================
  RCS file: /cvsroot/jboss/jbosssx/src/main/org/jboss/test/SimpleSRPServer.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SimpleSRPServer.java      2001/03/05 09:53:37     1.1
  +++ SimpleSRPServer.java      2001/04/04 21:39:02     1.2
  @@ -1,43 +1,59 @@
  -/*
  - * JBoss, the OpenSource EJB server
  - *
  - * Distributable under LGPL license.
  - * See terms of license at gnu.org.
  - */
   package org.jboss.test;
   
  -import java.io.IOException;
  -import java.rmi.AlreadyBoundException;
  +import java.io.Serializable;
  +import java.math.BigInteger;
   import java.rmi.RemoteException;
  -import java.rmi.registry.LocateRegistry;
  -import java.rmi.registry.Registry;
  +import java.security.KeyException;
   
   import org.jboss.security.Util;
  -import org.jboss.security.srp.SerialObjectStore;
  -import org.jboss.security.srp.SRPRemoteServer;
  +import org.jboss.security.srp.SRPConf;
  +import org.jboss.security.srp.SRPServerInterface;
  +import org.jboss.security.srp.SRPServerInterface.SRPParameters;
  +import org.jboss.security.srp.SRPServerSession;
  +import org.jboss.security.srp.TracePriority;
   
  -/** An RMI application that creates a SRPRemoteServer instance and
  -exports it on the standard RMI register 1099 port. It creates a
  -SerialObjectStore as the SRPVerifierStore for the SRPRemoteServer.
  +/** A simple hard coded implementation of SRPServerInterface that validates
  +    any given username to the password and salt provided to its constructor.
   
   @author [EMAIL PROTECTED]
  -@version $Revision: 1.1 $
  +@version $Revision: 1.2 $
   */
  -public class SimpleSRPServer
  +public class SimpleSRPServer implements SRPServerInterface
   {
  -    SerialObjectStore store;
  +    SRPParameters params = new SRPParameters();
  +    SRPServerSession session;
  +    String password;
   
  -    void run() throws IOException, AlreadyBoundException, RemoteException
  +    SimpleSRPServer(String password, String salt)
       {
  -        store = new SerialObjectStore();
  -        SRPRemoteServer server = new SRPRemoteServer(store);
  -        Registry reg = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
  -        reg.bind("SimpleSRPServer", server);
  +        params.N = SRPConf.getDefaultParams().Nbytes();
  +        params.g = SRPConf.getDefaultParams().gbytes();
  +        params.s = Util.fromb64(salt);
  +        this.password = password;
       }
   
  -    public static void main(String[] args) throws Exception
  +    public SRPParameters getSRPParameters(String username) throws KeyException, 
RemoteException
       {
  -        SimpleSRPServer server = new SimpleSRPServer();
  -        server.run();
  +        return params;
       }
  +
  +    public byte[] init(String username,byte[] A) throws SecurityException, 
RemoteException
  +    {
  +        // Calculate the password verfier v
  +        byte[] v = Util.calculateVerifier(username, password, params.s, params.N, 
params.g);
  +        // Create an SRP session
  +        session = new SRPServerSession(username, params.s, v, params.N, params.g);
  +        byte[] B = session.exponential();
  +        session.buildSessionKey(A);
  +
  +        return B;
  +    }
  +
  +    public byte[] verify(String username, byte[] M1) throws SecurityException, 
RemoteException
  +    {
  +        if( session.verify(M1) == false )
  +            throw new SecurityException("Failed to verify M1");
  +        return session.getServerResponse();
  +    }
  +
   }
  
  
  
  1.1                  jbosssx/src/main/org/jboss/test/SRPLoginTest.java
  
  Index: SRPLoginTest.java
  ===================================================================
  package org.jboss.test;
  
  import java.net.URL;
  import javax.security.auth.Policy;
  import javax.security.auth.Subject;
  import javax.security.auth.callback.CallbackHandler;
  import javax.security.auth.login.Configuration;
  import javax.security.auth.login.LoginContext;
  import javax.security.auth.login.LoginException;
  
  import org.jboss.security.SimplePrincipal;
  import org.jboss.security.SecurityPolicy;
  import org.jboss.security.SecurityPolicyParser;
  import org.jboss.security.auth.UsernamePasswordHandler;
  
  /** A test of the SRPLogin module
  
  @see org.jboss.security.srp.jaas.SRPLoginModule
  
  @author [EMAIL PROTECTED]
  @version $Revision: 1.1 $
  */
  public class SRPLoginTest extends junit.framework.TestCase
  {
      public SRPLoginTest(String name)
      {
          super(name);
      }
  
      /** Create a SecurityPolicy from a xml policy file and install it as the
          JAAS Policy and Configuration implementations.
      */
      protected void setUp() throws Exception
      {
          // Create a subject security policy
          String policyName = "tst-policy.xml";
          URL policyURL = getClass().getClassLoader().getResource(policyName);
          if( policyURL == null )
              throw new IllegalStateException("Failed to find "+policyName+" in 
classpath");
          SecurityPolicyParser policyStore = new SecurityPolicyParser(policyURL);
          SecurityPolicy policy = new SecurityPolicy(policyStore);
          policy.refresh();
          Policy.setPolicy(policy);
          Configuration.setConfiguration(policy.getLoginConfiguration());
      }
  
      public void testLogin()
      {
          CallbackHandler handler = new UsernamePasswordHandler("scott", 
"stark".toCharArray());
          try
          {
              LoginContext lc = new LoginContext("srp-login", handler);
              lc.login();
              Subject subject = lc.getSubject();
              System.out.println("Subject="+subject);
          }
          catch(LoginException e)
          {
              e.printStackTrace();
              fail(e.getMessage());
          }
      }
  
      public static void main(String args[])
      {
          try
          {
              SRPLoginTest tst = new SRPLoginTest("main");
              tst.setUp();
          }
          catch(Exception e)
          {
              e.printStackTrace(System.out);
          }
      }
  }
  
  
  
  1.1                  jbosssx/src/main/org/jboss/test/SRPProtocolTest.java
  
  Index: SRPProtocolTest.java
  ===================================================================
  /*
   * JBoss, the OpenSource EJB server
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.test;
  
  import java.io.Serializable;
  import java.math.BigInteger;
  import java.rmi.RemoteException;
  import java.security.KeyException;
  import java.security.MessageDigest;
  
  import org.apache.log4j.Category;
  import org.apache.log4j.FileAppender;
  import org.apache.log4j.NDC;
  import org.apache.log4j.PatternLayout;
  
  import org.jboss.security.Util;
  import org.jboss.security.srp.SRPConf;
  import org.jboss.security.srp.SRPServerInterface;
  import org.jboss.security.srp.SRPServerInterface.SRPParameters;
  import org.jboss.security.srp.SRPClientSession;
  import org.jboss.security.srp.SRPServerSession;
  import org.jboss.security.srp.TracePriority;
  
  /** Test of the SRP protocol msg exchange sequence.
  
  @author [EMAIL PROTECTED]
  @version $Revision: 1.1 $
  */
  public class SRPProtocolTest extends junit.framework.TestCase
  {
      String username = "stark";
      String password = "scott";
      SRPServerInterface server;
  
      public SRPProtocolTest(String name)
      {
          super(name);
      }
  
      protected void setUp() throws Exception
      {
          // Set up a simple configuration that logs on the console.
          Category root = Category.getRoot();
          root.setPriority(TracePriority.TRACE);
          root.addAppender(new FileAppender(new PatternLayout("%x%m%n"), System.out));
          Util.init();
          NDC.push("S,");
          server = new SimpleSRPServer(password, "123456");
          NDC.pop();
      }
  
      public void testProtocol() throws Exception
      {
          SRPParameters params = server.getSRPParameters(username);
          NDC.push("C,");
          SRPClientSession client = new SRPClientSession(username, password, params.s, 
params.N, params.g);
          byte[] A = client.exponential();
          NDC.pop();
          NDC.push("S,");
          byte[] B = server.init(username, A);
          NDC.pop();
          NDC.push("C,");
          byte[] M1 = client.response(B);
          NDC.pop();
          NDC.push("S,");
          byte[] M2 = server.verify(username, M1);
          NDC.pop();
          NDC.push("C,");
          if( client.verify(M2) == false )
              throw new SecurityException("Failed to validate server reply");
          NDC.pop();
      }
  
      /**
      * @param args the command line arguments
      */
      public static void main(String args[])
      {
          long start = System.currentTimeMillis();
          try
          {
              SRPProtocolTest tst = new SRPProtocolTest("main");
              tst.setUp();
              tst.testProtocol();
          }
          catch(Exception e)
          {
              e.printStackTrace(System.out);
          }
          finally
          {
              long end = System.currentTimeMillis();
              System.out.println("Elapsed time = "+(end - start));
          }
      }
  
  }
  
  
  
  1.1                  jbosssx/src/main/org/jboss/test/SRPServerImpl.java
  
  Index: SRPServerImpl.java
  ===================================================================
  /*
   * JBoss, the OpenSource EJB server
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.test;
  
  import java.io.IOException;
  import java.rmi.AlreadyBoundException;
  import java.rmi.RemoteException;
  import java.rmi.registry.LocateRegistry;
  import java.rmi.registry.Registry;
  
  import org.jboss.security.Util;
  import org.jboss.security.srp.SerialObjectStore;
  import org.jboss.security.srp.SRPRemoteServer;
  
  /** An RMI application that creates a SRPRemoteServer instance and
  exports it on the standard RMI register 1099 port. It creates a
  SerialObjectStore as the SRPVerifierStore for the SRPRemoteServer.
  
  @author [EMAIL PROTECTED]
  @version $Revision: 1.1 $
  */
  public class SRPServerImpl
  {
      SerialObjectStore store;
  
      void run() throws IOException, AlreadyBoundException, RemoteException
      {
          store = new SerialObjectStore();
          SRPRemoteServer server = new SRPRemoteServer(store);
          Registry reg = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
          reg.bind("SimpleSRPServer", server);
      }
  
      public static void main(String[] args) throws Exception
      {
          SRPServerImpl server = new SRPServerImpl();
          server.run();
      }
  }
  
  
  
  1.1                  jbosssx/src/main/org/jboss/test/SecurityPolicyParserTest.java
  
  Index: SecurityPolicyParserTest.java
  ===================================================================
  
  package org.jboss.test;
  
  import java.net.URL;
  import java.security.AccessController;
  import java.security.CodeSource;
  import java.security.Permission;
  import java.security.PermissionCollection;
  import java.security.PrivilegedAction;
  import java.security.ProtectionDomain;
  import java.util.PropertyPermission;
  import javax.security.auth.Policy;
  import javax.security.auth.Subject;
  import javax.security.auth.login.Configuration;
  import javax.security.auth.login.LoginContext;
  import javax.security.auth.login.LoginException;
  
  import org.jboss.security.SimplePrincipal;
  import org.jboss.security.SecurityPolicy;
  import org.jboss.security.SecurityPolicyParser;
  
  /** Tests of the SecurityPolicyParser and SecurityPolicy classes.
  
  @author [EMAIL PROTECTED]
  @version $Revision: 1.1 $
  */
  public class SecurityPolicyParserTest extends junit.framework.TestCase
  {
  
      public SecurityPolicyParserTest(String name)
      {
          super(name);
      }
  
      protected void setUp() throws Exception
      {
          String policyName = "tst-policy.xml";
          URL policyURL = getClass().getClassLoader().getResource(policyName);
          SecurityPolicyParser policyStore = new SecurityPolicyParser(policyURL);
          SecurityPolicy policy = new SecurityPolicy(policyStore);
          Policy.setPolicy(policy);
          policy.refresh();
          Configuration.setConfiguration(policy.getLoginConfiguration());
      }
  
      public void testParser()
      {
          Subject subject = new Subject();
          subject.getPrincipals().add(new SimplePrincipal("starksm"));
          CodeSource cs = new CodeSource(null, null);
          Policy policy = Policy.getPolicy();
          SecurityPolicy.setActiveApp("test-domain");
          PermissionCollection perms = policy.getPermissions(subject, cs);
          Permission p = new NamespacePermission("Project1/Documents/Public", "r---");
          boolean implied = perms.implies(p);
          assert(p.toString(), implied);
      }
  
      public void testSubject() throws LoginException
      {
          LoginContext lc = new LoginContext("test-domain");
          lc.login();
          Subject subject = lc.getSubject();
          System.out.println("Subject="+subject);
          SecurityPolicy.setActiveApp("test-domain");
          CodeSource cs = new CodeSource(null, null);
          ProtectionDomain pd = getClass().getProtectionDomain();
          cs = pd.getCodeSource();
          Subject.doAsPrivileged(subject, new PrivilegedAction()
              {
                  public Object run()
                  {
                      SecurityDelegate.accessProject("Project1", "r---", false);
                      SecurityDelegate.accessProject("Project1", "rw--", false);
                      SecurityDelegate.accessProject("Project1", "rw-d", false);
                      SecurityDelegate.accessProject("Project1/Documents/Private", 
"rwxd", false);
                      SecurityDelegate.accessProject("Project1/Documents/Public", 
"r---", true);
                      
SecurityDelegate.accessProject("Project1/Documents/Public/readme.html", "r---", true);
                      
SecurityDelegate.accessProject("Project1/Documents/Public/readme.html", "rw--", false);
                      return null;
                  }
              },
              null
          );
      }
  
      /** The code that executes the custom permission check needs to be in a
          separate class so that we can jar it up to create a codesource whcich
        is disticnt from the TestSecurityPolicyParser codesource.
      */
      static class SecurityDelegate
      {
          static void accessProject(String path, String action, boolean shouldAllow)
          {
              Permission p = new NamespacePermission(path, action);
              try
              {
                  AccessController.checkPermission(p);
                  if( shouldAllow == false )
                     throw new IllegalStateException("Access allowed for: "+path+", 
action="+action);
              }
              catch(SecurityException e)
              {
                  if( shouldAllow == true )
                      throw new IllegalStateException("Access denied for: "+path+", 
action="+action);
              }
  
              try
              {
                  // Try a Java2 permission check that should fail for everyone
                  p = new PropertyPermission("java.class.path", "write");
                  AccessController.checkPermission(p);
                  throw new IllegalStateException("access allowed for: "+p);
              }
              catch(SecurityException e)
              {
              }
          }
      }
  
      public static void main(String[] args) throws Exception
      {
          SecurityPolicyParserTest tst = new SecurityPolicyParserTest("main");
          tst.setUp();
          //tst.testParser();
          tst.testSubject();
      }
  }
  
  
  
  1.1                  jbosssx/src/main/org/jboss/test/TestJCE.java
  
  Index: TestJCE.java
  ===================================================================
  package org.jboss.test;
  
  import java.io.Serializable;
  import java.math.BigInteger;
  import java.rmi.RemoteException;
  import java.security.Key;
  import java.security.KeyException;
  import java.security.MessageDigest;
  import java.security.SecureRandom;
  import javax.crypto.Cipher;
  import javax.crypto.KeyGenerator;
  import javax.crypto.SealedObject;
  import javax.crypto.SecretKey;
  import javax.crypto.spec.SecretKeySpec;
  
  import org.apache.log4j.Category;
  import org.apache.log4j.FileAppender;
  import org.apache.log4j.NDC;
  import org.apache.log4j.PatternLayout;
  
  import org.jboss.security.Util;
  import org.jboss.security.srp.SRPConf;
  import org.jboss.security.srp.SRPServerInterface;
  import org.jboss.security.srp.SRPServerInterface.SRPParameters;
  import org.jboss.security.srp.SRPClientSession;
  import org.jboss.security.srp.SRPServerSession;
  import org.jboss.security.srp.TracePriority;
  
  /** Tests of the Java Cryptography framework
  */
  public class TestJCE
  {
      SimpleSRPServer server;
      SRPClientSession client;
  
      TestJCE() throws Exception
      {
          // Set up a simple configuration that logs on the console.
          Category root = Category.getRoot();
          root.setPriority(TracePriority.TRACE);
          root.addAppender(new FileAppender(new PatternLayout("%x%m%n"), System.out));
          Util.init();
          NDC.push("S,");
          server = new SimpleSRPServer("secret", "123456");
          NDC.pop();
      }
      void login(String username, String password) throws Exception
      {
          SRPParameters params = server.getSRPParameters(username);
          NDC.push("C,");
          client = new SRPClientSession(username, password, params.s, params.N, 
params.g);
          byte[] A = client.exponential();
          NDC.pop();
          NDC.push("S,");
          byte[] B = server.init(username, A);
          NDC.pop();
          NDC.push("C,");
          byte[] M1 = client.response(B);
          NDC.pop();
          NDC.push("S,");
          byte[] M2 = server.verify(username, M1);
          NDC.pop();
          NDC.push("C,");
          if( client.verify(M2) == false )
              throw new SecurityException("Failed to validate server reply");
          NDC.pop();
      }
  
      /** Performs an SRP exchange and then uses the resulting session key to
       encrypt/decrypt a msg.
      */
      void testSecureExchange() throws Exception
      {
          login("jduke", "secret");
          System.out.println("Logged into server");
          byte[] kbytes = client.getSessionKey();
          System.out.println("Session key size = "+kbytes.length);
          SecretKeySpec clientKey = new SecretKeySpec(kbytes, "Blowfish");
          System.out.println("clientKey");
          Cipher cipher = Cipher.getInstance("Blowfish");
          cipher.init(Cipher.ENCRYPT_MODE, clientKey);
          SealedObject msg = new SealedObject("This is a secret", cipher);
  
          // Now use the server key to decrypt the msg
          byte[] skbytes = server.session.getSessionKey();
          SecretKeySpec serverKey = new SecretKeySpec(skbytes, "Blowfish");
          Cipher scipher = Cipher.getInstance("Blowfish");
          cipher.init(Cipher.DECRYPT_MODE, serverKey);
          String theMsg = (String) msg.getObject(cipher);
          System.out.println("Decrypted: "+theMsg);
          // Try a key that should fail
          KeyGenerator kgen = KeyGenerator.getInstance("Blowfish");
          kgen.init(320);
          SecretKey key = kgen.generateKey();
          cipher.init(Cipher.DECRYPT_MODE, key);
          try
          {
              String tmp = (String) msg.getObject(cipher);
              throw new IllegalArgumentException("Should have failed to decrypt the 
msg");
          }
          catch(Exception e)
          {
              System.out.println("Arbitrary key failed as expected");
          }
      }
  
      static void testKey() throws Exception
      {
          KeyGenerator kgen = KeyGenerator.getInstance("Blowfish");
          kgen.init(320);
          SecretKey key = kgen.generateKey();
          byte[] kbytes = key.getEncoded();
          System.out.println("key.Algorithm = "+key.getAlgorithm());
          System.out.println("key.Format = "+key.getFormat());
          System.out.println("key.Encoded Size = "+kbytes.length);
          SecureRandom rnd = SecureRandom.getInstance("SHA1PRNG");
          BigInteger bi = new BigInteger(320, rnd);
          byte[] k2bytes = bi.toByteArray();
          SecretKeySpec keySpec = new SecretKeySpec(k2bytes, "Blowfish");
          System.out.println("key2.Algorithm = "+key.getAlgorithm());
          System.out.println("key2.Format = "+key.getFormat());
          System.out.println("key2.Encoded Size = "+kbytes.length);        
      }
  
      public static void main(String[] args)
      {
          try
          {
              System.setOut(System.err);
              TestJCE tst = new TestJCE();
              tst.testSecureExchange();
          }
          catch(Throwable t)
          {
              t.printStackTrace();
          }
      }
  }
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to