1 22 package org.jboss.test; 23 24 import java.io.Serializable ; 25 import java.math.BigInteger ; 26 import java.rmi.RemoteException ; 27 import java.security.KeyException ; 28 import java.security.MessageDigest ; 29 import java.security.NoSuchAlgorithmException ; 30 31 import org.jboss.logging.Logger; 32 import org.apache.log4j.ConsoleAppender; 33 import org.apache.log4j.NDC; 34 import org.apache.log4j.PatternLayout; 35 36 import org.jboss.logging.XLevel; 37 import org.jboss.logging.Logger; 38 import org.jboss.security.Util; 39 import org.jboss.security.srp.SRPConf; 40 import org.jboss.security.srp.SRPServerInterface; 41 import org.jboss.security.srp.SRPClientSession; 42 import org.jboss.security.srp.SRPParameters; 43 import org.jboss.security.srp.SRPServerSession; 44 45 50 public class TestProtocol extends junit.framework.TestCase 51 { 52 static Logger log = Logger.getLogger(TestProtocol.class); 53 String username = "jduke"; 54 char[] password = "theduke".toCharArray(); 55 SRPServerInterface server; 56 57 60 static class TstImpl implements SRPServerInterface 61 { 62 SRPParameters params; 63 SRPServerSession session; 64 char[] password; 65 66 public Object [] getSRPParameters(String username, boolean mutipleSessions) 67 throws KeyException , RemoteException 68 { 69 return new Object [0]; 70 } 71 72 public byte[] init(String username, byte[] A, int sessionID) throws SecurityException , 73 NoSuchAlgorithmException , RemoteException 74 { 75 return new byte[0]; 76 } 77 78 public byte[] verify(String username, byte[] M1, int sessionID) 79 throws SecurityException , RemoteException 80 { 81 return new byte[0]; 82 } 83 84 public byte[] verify(String username, byte[] M1, Object auxChallenge) 85 throws SecurityException , RemoteException 86 { 87 return new byte[0]; 88 } 89 90 public byte[] verify(String username, byte[] M1, Object auxChallenge, int sessionID) 91 throws SecurityException , RemoteException 92 { 93 return new byte[0]; 94 } 95 96 public void close(String username, int sessionID) throws SecurityException , RemoteException 97 { 98 } 99 100 TstImpl(char[] password, String salt) 101 { 102 BigInteger N = SRPConf.getDefaultParams().N(); 103 log.trace("N: "+Util.tob64(N.toByteArray())); 104 BigInteger g = SRPConf.getDefaultParams().g(); 105 log.trace("g: "+Util.tob64(g.toByteArray())); 106 byte[] Nb = SRPConf.getDefaultParams().Nbytes(); 107 log.trace("N': "+Util.tob64(params.N)); 108 byte[] gb = SRPConf.getDefaultParams().gbytes(); 109 log.trace("g': "+Util.tob64(params.g)); 110 byte[] hn = Util.newDigest().digest(params.N); 111 log.trace("H(N): "+Util.tob64(hn)); 112 byte[] hg = Util.newDigest().digest(params.g); 113 log.trace("H(g): "+Util.tob64(hg)); 114 byte[] sb = Util.fromb64(salt); 115 this.password = password; 116 params = new SRPParameters(Nb, gb, sb); 117 } 118 119 public SRPParameters getSRPParameters(String username) throws KeyException , RemoteException 120 { 121 return params; 122 } 123 124 public byte[] init(String username,byte[] A) throws SecurityException , 125 NoSuchAlgorithmException , RemoteException 126 { 127 byte[] v = Util.calculateVerifier(username, password, params.s, params.N, params.g); 129 session = new SRPServerSession(username, v, params); 131 byte[] B = session.exponential(); 132 session.buildSessionKey(A); 133 134 return B; 135 } 136 137 public byte[] verify(String username, byte[] M1) throws SecurityException , RemoteException 138 { 139 if( session.verify(M1) == false ) 140 throw new SecurityException ("Failed to verify M1"); 141 return session.getServerResponse(); 142 } 143 144 146 public void close(String username) throws SecurityException , RemoteException 147 { 148 } 149 150 } 151 152 public TestProtocol(String name) 153 { 154 super(name); 155 } 156 157 protected void setUp() throws Exception 158 { 159 Logger root = Logger.getRoot(); 161 root.setLevel(XLevel.TRACE); 162 root.addAppender(new ConsoleAppender(new PatternLayout("%x%m%n"))); 163 Util.init(); 164 NDC.push("S,"); 165 server = new TstImpl(password, "123456"); 166 NDC.pop(); 167 NDC.remove(); 168 } 169 170 public void testProtocol() throws Exception 171 { 172 SRPParameters params = server.getSRPParameters(username); 173 NDC.push("C,"); 174 SRPClientSession client = new SRPClientSession(username, password, params); 175 byte[] A = client.exponential(); 176 NDC.pop(); 177 NDC.push("S,"); 178 byte[] B = server.init(username, A); 179 NDC.pop(); 180 NDC.push("C,"); 181 byte[] M1 = client.response(B); 182 NDC.pop(); 183 NDC.push("S,"); 184 byte[] M2 = server.verify(username, M1); 185 NDC.pop(); 186 NDC.push("C,"); 187 if( client.verify(M2) == false ) 188 throw new SecurityException ("Failed to validate server reply"); 189 NDC.pop(); 190 NDC.remove(); 191 } 192 193 196 public static void main(String args[]) 197 { 198 long start = System.currentTimeMillis(); 199 try 200 { 201 TestProtocol tst = new TestProtocol("main"); 202 tst.setUp(); 203 tst.testProtocol(); 204 } 205 catch(Exception e) 206 { 207 e.printStackTrace(System.out); 208 } 209 finally 210 { 211 long end = System.currentTimeMillis(); 212 System.out.println("Elapsed time = "+(end - start)); 213 } 214 } 215 216 } 217 | Popular Tags |