User: starksm 
  Date: 02/03/07 21:55:49

  Modified:    src/main/org/jboss/test SRPProtocolTestCase.java
                        SimpleSRPServer.java TestJCE.java TestProtocol.java
                        TstClient.java
  Added:       src/main/org/jboss/test SecurityProviderlTestCase.java
                        TestJCEIntegration.java
  Log:
  Update test cases for SRP changes
  
  Revision  Changes    Path
  1.3       +4 -4      jbosssx/src/main/org/jboss/test/SRPProtocolTestCase.java
  
  Index: SRPProtocolTestCase.java
  ===================================================================
  RCS file: /cvsroot/jboss/jbosssx/src/main/org/jboss/test/SRPProtocolTestCase.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- SRPProtocolTestCase.java  24 Feb 2002 10:16:53 -0000      1.2
  +++ SRPProtocolTestCase.java  8 Mar 2002 05:55:49 -0000       1.3
  @@ -21,19 +21,19 @@
   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.SRPParameters;
   import org.jboss.security.srp.SRPServerSession;
   
   /** Test of the SRP protocol msg exchange sequence.
   
   @author [EMAIL PROTECTED]
  -@version $Revision: 1.2 $
  +@version $Revision: 1.3 $
   */
   public class SRPProtocolTestCase extends junit.framework.TestCase
   {
       String username = "stark";
  -    String password = "scott";
  +    char[] password = "scott".toCharArray();
       SRPServerInterface server;
   
       public SRPProtocolTestCase(String name)
  @@ -57,7 +57,7 @@
       {
           SRPParameters params = server.getSRPParameters(username);
           NDC.push("C,");
  -        SRPClientSession client = new SRPClientSession(username, password, 
params.s, params.N, params.g);
  +        SRPClientSession client = new SRPClientSession(username, password, params);
           byte[] A = client.exponential();
           NDC.pop();
           NDC.push("S,");
  
  
  
  1.4       +45 -42    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.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- SimpleSRPServer.java      8 Feb 2002 23:21:30 -0000       1.3
  +++ SimpleSRPServer.java      8 Mar 2002 05:55:49 -0000       1.4
  @@ -4,55 +4,58 @@
   import java.math.BigInteger;
   import java.rmi.RemoteException;
   import java.security.KeyException;
  +import java.security.NoSuchAlgorithmException;
   
   import org.jboss.security.Util;
   import org.jboss.security.srp.SRPConf;
  +import org.jboss.security.srp.SRPParameters;
   import org.jboss.security.srp.SRPServerInterface;
  -import org.jboss.security.srp.SRPServerInterface.SRPParameters;
   import org.jboss.security.srp.SRPServerSession;
   
   /** 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.3 $
  -*/
  + any given username to the password and salt provided to its constructor.
  + 
  + @author [EMAIL PROTECTED]
  + @version $Revision: 1.4 $
  + */
   public class SimpleSRPServer implements SRPServerInterface
   {
  -    SRPParameters params = new SRPParameters();
  -    SRPServerSession session;
  -    String password;
  -
  -    SimpleSRPServer(String password, String salt)
  -    {
  -        params.N = SRPConf.getDefaultParams().Nbytes();
  -        params.g = SRPConf.getDefaultParams().gbytes();
  -        params.s = Util.fromb64(salt);
  -        this.password = password;
  -    }
  -
  -    public SRPParameters getSRPParameters(String username) throws KeyException, 
RemoteException
  -    {
  -        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();
  -    }
  -
  +   SRPParameters params;
  +   SRPServerSession session;
  +   char[] password;
  +   
  +   SimpleSRPServer(char[] password, String salt)
  +   {
  +      byte[] N = SRPConf.getDefaultParams().Nbytes();
  +      byte[] g = SRPConf.getDefaultParams().gbytes();
  +      byte[] s = Util.fromb64(salt);
  +      params = new SRPParameters(N, g, s);
  +      this.password = password;
  +   }
  +   
  +   public SRPParameters getSRPParameters(String username) throws KeyException, 
RemoteException
  +   {
  +      return params;
  +   }
  +   
  +   public byte[] init(String username,byte[] A) throws SecurityException,
  +      NoSuchAlgorithmException, 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, v, params);
  +      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.5       +105 -121  jbosssx/src/main/org/jboss/test/TestJCE.java
  
  Index: TestJCE.java
  ===================================================================
  RCS file: /cvsroot/jboss/jbosssx/src/main/org/jboss/test/TestJCE.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- TestJCE.java      24 Feb 2002 10:16:53 -0000      1.4
  +++ TestJCE.java      8 Mar 2002 05:55:49 -0000       1.5
  @@ -2,139 +2,123 @@
   
   import java.io.Serializable;
   import java.math.BigInteger;
  -import java.rmi.RemoteException;
  +import java.security.AlgorithmParameters;
   import java.security.Key;
   import java.security.KeyException;
   import java.security.MessageDigest;
  +import java.security.Provider;
   import java.security.SecureRandom;
  +import java.security.Security;
  +import java.util.Iterator;
   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.WriterAppender;
  -import org.apache.log4j.NDC;
  -import org.apache.log4j.PatternLayout;
  -
  -import org.jboss.logging.XPriority;
  -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;
  -
  -/** Tests of the Java Cryptography framework
  +/** Tests of the Java Cryptography Extension framework
    @author [EMAIL PROTECTED]
  - @version $Revision: 1.4 $
  + @version $Revision: 1.5 $
   */
   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(XPriority.TRACE);
  -        root.addAppender(new WriterAppender(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();
  -        }
  -    }
  +   static void showProviders() throws Exception
  +   {
  +      Provider[] providers = Security.getProviders();
  +      for(int p = 0; p < providers.length; p ++)
  +      {
  +         Iterator iter = providers[p].keySet().iterator();
  +         System.out.println("Provider: "+providers[p].getInfo());
  +         while( iter.hasNext() )
  +         {
  +            String key = (String) iter.next();
  +            System.out.println("  key="+key+", 
value="+providers[p].getProperty(key));
  +         }
  +      }
  +   }
  +
  +   static void testBlowfish() throws Exception
  +   {
  +      KeyGenerator kgen = KeyGenerator.getInstance("Blowfish");
  +      Cipher cipher = Cipher.getInstance("Blowfish");
  +      SecretKey key = null;
  +      int minKeyBits = -1, maxKeyBits = 0;
  +      int minCipherBits = -1, maxCipherBits = 0;
  +      for(int size = 1; size <= 448/8; size ++)
  +      {
  +         int bits = size * 8;
  +         try
  +         {
  +            kgen.init(bits);
  +            key = kgen.generateKey();
  +            if( minKeyBits == -1 )
  +               minKeyBits = bits;
  +            maxKeyBits = bits;
  +         }
  +         catch(Exception e)
  +         {
  +         }
  +
  +         try
  +         {
  +            cipher.init(Cipher.ENCRYPT_MODE, key);
  +            if( minCipherBits == -1 )
  +               minCipherBits = bits;
  +            maxCipherBits = bits;
  +         }
  +         catch(Exception e)
  +         {
  +         }
  +      }
  +      System.out.println("Key range: "+minKeyBits+".."+maxKeyBits);
  +      System.out.println("Cipher range: "+minCipherBits+".."+maxCipherBits);
  +   }
  +
  +   static void testKey() throws Exception
  +   {
  +      int size = 8 * 24;
  +      KeyGenerator kgen = KeyGenerator.getInstance("Blowfish");
  +      kgen.init(size);
  +      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);
  +      
  +      Cipher cipher = Cipher.getInstance("Blowfish");
  +      AlgorithmParameters params = cipher.getParameters();
  +      System.out.println("Blowfish.params = "+params);
  +      cipher.init(Cipher.ENCRYPT_MODE, key);
  +      SealedObject msg = new SealedObject("This is a secret", cipher);
  +      
  +      SecretKeySpec serverKey = new SecretKeySpec(kbytes, "Blowfish");
  +      Cipher scipher = Cipher.getInstance("Blowfish");
  +      scipher.init(Cipher.DECRYPT_MODE, serverKey);
  +      String theMsg = (String) msg.getObject(scipher);
  +      System.out.println("Decrypted: "+theMsg);
  +      
  +      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.showProviders();
  +         //tst.testKey();
  +         tst.testBlowfish();
  +      }
  +      catch(Throwable t)
  +      {
  +         t.printStackTrace();
  +      }
  +   }
   }
  
  
  
  1.5       +123 -120  jbosssx/src/main/org/jboss/test/TestProtocol.java
  
  Index: TestProtocol.java
  ===================================================================
  RCS file: /cvsroot/jboss/jbosssx/src/main/org/jboss/test/TestProtocol.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- TestProtocol.java 24 Feb 2002 10:16:53 -0000      1.4
  +++ TestProtocol.java 8 Mar 2002 05:55:49 -0000       1.5
  @@ -11,6 +11,7 @@
   import java.rmi.RemoteException;
   import java.security.KeyException;
   import java.security.MessageDigest;
  +import java.security.NoSuchAlgorithmException;
   
   import org.apache.log4j.Category;
   import org.apache.log4j.ConsoleAppender;
  @@ -22,134 +23,136 @@
   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.SRPParameters;
   import org.jboss.security.srp.SRPServerSession;
   
   /** Test of the SRP protocol msg exchange sequence.
  -
  -@author [EMAIL PROTECTED]
  -@version $Revision: 1.4 $
  -*/
  + 
  + @author [EMAIL PROTECTED]
  + @version $Revision: 1.5 $
  + */
   public class TestProtocol extends junit.framework.TestCase
   {
  -    static Logger log = Logger.getLogger(TestProtocol.class);
  -    String username = "jduke";
  -    String password = "theduke";
  -    SRPServerInterface server;
  -
  -    /** A simple hard coded implementation of SRPServerInterface that validates
  +   static Logger log = Logger.getLogger(TestProtocol.class);
  +   String username = "jduke";
  +   char[] password = "theduke".toCharArray();
  +   SRPServerInterface server;
  +   
  +   /** A simple hard coded implementation of SRPServerInterface that validates
       any given username to the password and salt provided to its constructor.
       */
  -    static class TstImpl implements SRPServerInterface
  -    {
  -        SRPParameters params = new SRPParameters();
  -        SRPServerSession session;
  -        String password;
  -
  -        TstImpl(String password, String salt)
  -        {
  -            BigInteger N = SRPConf.getDefaultParams().N();
  -            log.trace("N: "+Util.tob64(N.toByteArray()));
  -            BigInteger g = SRPConf.getDefaultParams().g();
  -            log.trace("g: "+Util.tob64(g.toByteArray()));
  -            params.N = SRPConf.getDefaultParams().Nbytes();
  -            log.trace("N': "+Util.tob64(params.N));
  -            params.g = SRPConf.getDefaultParams().gbytes();
  -            log.trace("g': "+Util.tob64(params.g));
  -            byte[] hn = Util.newDigest().digest(params.N);
  -            log.trace("H(N): "+Util.tob64(hn));
  -            byte[] hg = Util.newDigest().digest(params.g);
  -            log.trace("H(g): "+Util.tob64(hg));
  -            params.s = Util.fromb64(salt);
  -            this.password = password;
  -        }
  -
  -        public SRPParameters getSRPParameters(String username) throws KeyException, 
RemoteException
  -        {
  -            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();
  -        }
  -    }
  -
  -    public TestProtocol(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(XPriority.TRACE);
  -        root.addAppender(new ConsoleAppender(new PatternLayout("%x%m%n")));
  -        Util.init();
  -        NDC.push("S,");
  -        server = new TstImpl(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();
  -    }
  -
  -    /**
  +   static class TstImpl implements SRPServerInterface
  +   {
  +      SRPParameters params;
  +      SRPServerSession session;
  +      char[] password;
  +      
  +      TstImpl(char[] password, String salt)
  +      {
  +         BigInteger N = SRPConf.getDefaultParams().N();
  +         log.trace("N: "+Util.tob64(N.toByteArray()));
  +         BigInteger g = SRPConf.getDefaultParams().g();
  +         log.trace("g: "+Util.tob64(g.toByteArray()));
  +         byte[] Nb = SRPConf.getDefaultParams().Nbytes();
  +         log.trace("N': "+Util.tob64(params.N));
  +         byte[] gb = SRPConf.getDefaultParams().gbytes();
  +         log.trace("g': "+Util.tob64(params.g));
  +         byte[] hn = Util.newDigest().digest(params.N);
  +         log.trace("H(N): "+Util.tob64(hn));
  +         byte[] hg = Util.newDigest().digest(params.g);
  +         log.trace("H(g): "+Util.tob64(hg));
  +         byte[] sb = Util.fromb64(salt);
  +         this.password = password;
  +         params = new SRPParameters(Nb, gb, sb);
  +      }
  +      
  +      public SRPParameters getSRPParameters(String username) throws KeyException, 
RemoteException
  +      {
  +         return params;
  +      }
  +      
  +      public byte[] init(String username,byte[] A) throws SecurityException,
  +         NoSuchAlgorithmException, 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, v, params);
  +         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();
  +      }
  +   }
  +   
  +   public TestProtocol(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(XPriority.TRACE);
  +      root.addAppender(new ConsoleAppender(new PatternLayout("%x%m%n")));
  +      Util.init();
  +      NDC.push("S,");
  +      server = new TstImpl(password, "123456");
  +      NDC.pop();
  +   }
  +   
  +   public void testProtocol() throws Exception
  +   {
  +      SRPParameters params = server.getSRPParameters(username);
  +      NDC.push("C,");
  +      SRPClientSession client = new SRPClientSession(username, password, params);
  +      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
  -        {
  -            TestProtocol tst = new TestProtocol("main");
  -            tst.setUp();
  -            tst.testProtocol();
  -        }
  -        catch(Exception e)
  -        {
  -            e.printStackTrace(System.out);
  -        }
  -        finally
  -        {
  -            long end = System.currentTimeMillis();
  -            System.out.println("Elapsed time = "+(end - start));
  -        }
  -    }
  -
  +   public static void main(String args[])
  +   {
  +      long start = System.currentTimeMillis();
  +      try
  +      {
  +         TestProtocol tst = new TestProtocol("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.3       +4 -4      jbosssx/src/main/org/jboss/test/TstClient.java
  
  Index: TstClient.java
  ===================================================================
  RCS file: /cvsroot/jboss/jbosssx/src/main/org/jboss/test/TstClient.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TstClient.java    8 Feb 2002 23:21:30 -0000       1.2
  +++ TstClient.java    8 Mar 2002 05:55:49 -0000       1.3
  @@ -8,26 +8,26 @@
   
   import org.jboss.security.srp.SRPClientSession;
   import org.jboss.security.srp.SRPServerInterface;
  -import org.jboss.security.srp.SRPServerInterface.SRPParameters;
  +import org.jboss.security.srp.SRPParameters;
   
   /** A simple test client that looks up the SimpleSRPServer in the RMI
   registry and attempts to validate the username and password passed
   on the command line.
   
   @author [EMAIL PROTECTED]
  -@version $Revision: 1.2 $
  +@version $Revision: 1.3 $
   */
   public class TstClient
   {
       public static void main(String[] args) throws Exception
       {
           String username = args[0];
  -        String password = args[1];
  +        char[] password = args[1].toCharArray();
           SRPServerInterface server = (SRPServerInterface) 
Naming.lookup("SimpleSRPServer");
           System.out.println("Found SRPServerInterface");
           SRPParameters params = server.getSRPParameters(username);
           System.out.println("Found params for username: "+username);
  -        SRPClientSession client = new SRPClientSession(username, password, 
params.s, params.N, params.g);
  +        SRPClientSession client = new SRPClientSession(username, password, params);
           byte[] A = client.exponential();
           byte[] B = server.init(username, A);
           System.out.println("Sent A public key, got B public key");
  
  
  
  1.1                  jbosssx/src/main/org/jboss/test/SecurityProviderlTestCase.java
  
  Index: SecurityProviderlTestCase.java
  ===================================================================
  package org.jboss.test;
  
  import java.security.MessageDigest;
  import java.security.Security;
  
  import junit.extensions.TestSetup;
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  import org.jboss.crypto.JBossSXProvider;
  import org.jboss.security.Util;
  
  /** Tests of the org.jboss.crypto.*  Java Cryptography Architecture plugin
   classes
   
   @author [EMAIL PROTECTED]
   @version $Revision: 1.1 $
   */
  public class SecurityProviderlTestCase extends TestCase
  {
     public SecurityProviderlTestCase(String name)
     {
        super(name);
     }
     
     /** Compare Util.sessionKeyHash against the SHA-SRP MessageDigest. This
      will not match the Util.sessionKeyHash as the algorithm described in
      RFC2945 does not reverse the odd and even byte arrays as is done in
      Util.sessionKeyHash.
      */
     public void testSHAInterleave() throws Exception
     {
        System.out.println("testSHAInterleave");
        MessageDigest md = MessageDigest.getInstance("SHA-SRP");
        byte[] test = "session_key".getBytes();
  
        byte[] hash1 = Util.sessionKeyHash(test);
        String hash1b64 = Util.encodeBase64(hash1);
        System.out.println("hash1 = "+hash1b64);
        byte[] hash2 = md.digest(test);
        String hash2b64 = Util.encodeBase64(hash2);
        System.out.println("hash2 = "+hash2b64);
        super.assertTrue(hash1b64.equals(hash2b64) == false);
     }
     /** This should match the Util.sessionKeyHash
      */
     public void testSHAReverseInterleave() throws Exception
     {
        System.out.println("testSHAReverseInterleave");
        MessageDigest md = MessageDigest.getInstance("SHA-SRP-Reverse");
        byte[] test = "session_key".getBytes();
  
        byte[] hash1 = Util.sessionKeyHash(test);
        String hash1b64 = Util.encodeBase64(hash1);
        System.out.println("hash1 = "+hash1b64);
        byte[] hash2 = md.digest(test);
        String hash2b64 = Util.encodeBase64(hash2);
        System.out.println("hash2 = "+hash2b64);
        super.assertEquals(hash1b64, hash2b64);
     }
  
     public static Test suite()
     {
        TestSuite suite = new TestSuite(SecurityProviderlTestCase.class);
  
        // Create an initializer for the test suite
        TestSetup wrapper = new TestSetup(suite)
        {
           protected void setUp() throws Exception
           {
              Util.init();
              JBossSXProvider provider = new JBossSXProvider();
              Security.addProvider(provider);
           }
           protected void tearDown() throws Exception
           {
              Security.removeProvider(JBossSXProvider.PROVIDER_NAME);
           }
        };
        return wrapper;
     }
  
     public static void main(java.lang.String[] args)
     {
        System.setErr(System.out);
        Test suite = suite();
        junit.textui.TestRunner.run(suite);
     }
  }
  
  
  
  1.1                  jbosssx/src/main/org/jboss/test/TestJCEIntegration.java
  
  Index: TestJCEIntegration.java
  ===================================================================
  package org.jboss.test;
  
  import java.io.Serializable;
  import java.math.BigInteger;
  import java.rmi.RemoteException;
  import java.security.AlgorithmParameters;
  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.WriterAppender;
  import org.apache.log4j.NDC;
  import org.apache.log4j.PatternLayout;
  
  import org.jboss.logging.XPriority;
  import org.jboss.security.Util;
  import org.jboss.security.srp.SRPConf;
  import org.jboss.security.srp.SRPParameters;
  import org.jboss.security.srp.SRPServerInterface;
  import org.jboss.security.srp.SRPClientSession;
  import org.jboss.security.srp.SRPServerSession;
  
  /** Tests of using the Java Cryptography Extension framework with SRP.
  
   @author [EMAIL PROTECTED]
   @version $Revision: 1.1 $
   */
  public class TestJCEIntegration
  {
     SimpleSRPServer server;
     SRPClientSession client;
     
     TestJCEIntegration() throws Exception
     {
        // Set up a simple configuration that logs on the console.
        Category root = Category.getRoot();
        root.setPriority(XPriority.TRACE);
        root.addAppender(new WriterAppender(new PatternLayout("%x%m%n"), System.out));
        Util.init();
        NDC.push("S,");
        server = new SimpleSRPServer("secret".toCharArray(), "123456");
        NDC.pop();
     }
     void login(String username, char[] password) throws Exception
     {
        SRPParameters params = server.getSRPParameters(username);
        NDC.push("C,");
        client = new SRPClientSession(username, password, params);
        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 use the resulting session key to
      encrypt/decrypt a msg. Also test that a random key cannot be used to
      decrypt the msg.
      */
     void testSecureExchange() throws Exception
     {
        login("jduke", "secret".toCharArray());
        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");
        scipher.init(Cipher.DECRYPT_MODE, serverKey);
        String theMsg = (String) msg.getObject(scipher);
        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
     {
        int size = 8 * 24;
        KeyGenerator kgen = KeyGenerator.getInstance("Blowfish");
        kgen.init(size);
        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);
           TestJCEIntegration tst = new TestJCEIntegration();
           tst.testSecureExchange();
           //tst.testKey();
        }
        catch(Throwable t)
        {
           t.printStackTrace();
        }
     }
  }
  
  
  

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

Reply via email to