KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > TestProtocol


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test;
23
24 import java.io.Serializable JavaDoc;
25 import java.math.BigInteger JavaDoc;
26 import java.rmi.RemoteException JavaDoc;
27 import java.security.KeyException JavaDoc;
28 import java.security.MessageDigest JavaDoc;
29 import java.security.NoSuchAlgorithmException JavaDoc;
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 /** Test of the SRP protocol msg exchange sequence.
46  
47  @author Scott.Stark@jboss.org
48  @version $Revision: 58118 $
49  */

50 public class TestProtocol extends junit.framework.TestCase
51 {
52    static Logger log = Logger.getLogger(TestProtocol.class);
53    String JavaDoc username = "jduke";
54    char[] password = "theduke".toCharArray();
55    SRPServerInterface server;
56    
57    /** A simple hard coded implementation of SRPServerInterface that validates
58     any given username to the password and salt provided to its constructor.
59     */

60    static class TstImpl implements SRPServerInterface
61    {
62       SRPParameters params;
63       SRPServerSession session;
64       char[] password;
65
66       public Object JavaDoc[] getSRPParameters(String JavaDoc username, boolean mutipleSessions)
67             throws KeyException JavaDoc, RemoteException JavaDoc
68       {
69          return new Object JavaDoc[0];
70       }
71
72       public byte[] init(String JavaDoc username, byte[] A, int sessionID) throws SecurityException JavaDoc,
73             NoSuchAlgorithmException JavaDoc, RemoteException JavaDoc
74       {
75          return new byte[0];
76       }
77
78       public byte[] verify(String JavaDoc username, byte[] M1, int sessionID)
79             throws SecurityException JavaDoc, RemoteException JavaDoc
80       {
81          return new byte[0];
82       }
83
84       public byte[] verify(String JavaDoc username, byte[] M1, Object JavaDoc auxChallenge)
85             throws SecurityException JavaDoc, RemoteException JavaDoc
86       {
87          return new byte[0];
88       }
89
90       public byte[] verify(String JavaDoc username, byte[] M1, Object JavaDoc auxChallenge, int sessionID)
91             throws SecurityException JavaDoc, RemoteException JavaDoc
92       {
93          return new byte[0];
94       }
95
96       public void close(String JavaDoc username, int sessionID) throws SecurityException JavaDoc, RemoteException JavaDoc
97       {
98       }
99
100       TstImpl(char[] password, String JavaDoc salt)
101       {
102          BigInteger JavaDoc N = SRPConf.getDefaultParams().N();
103          log.trace("N: "+Util.tob64(N.toByteArray()));
104          BigInteger JavaDoc 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 JavaDoc username) throws KeyException JavaDoc, RemoteException JavaDoc
120       {
121          return params;
122       }
123       
124       public byte[] init(String JavaDoc username,byte[] A) throws SecurityException JavaDoc,
125          NoSuchAlgorithmException JavaDoc, RemoteException JavaDoc
126       {
127          // Calculate the password verfier v
128
byte[] v = Util.calculateVerifier(username, password, params.s, params.N, params.g);
129          // Create an SRP session
130
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 JavaDoc username, byte[] M1) throws SecurityException JavaDoc, RemoteException JavaDoc
138       {
139          if( session.verify(M1) == false )
140             throw new SecurityException JavaDoc("Failed to verify M1");
141          return session.getServerResponse();
142       }
143       
144       /** Close the SRP session for the given username.
145        */

146       public void close(String JavaDoc username) throws SecurityException JavaDoc, RemoteException JavaDoc
147       {
148       }
149
150    }
151    
152    public TestProtocol(String JavaDoc name)
153    {
154       super(name);
155    }
156    
157    protected void setUp() throws Exception JavaDoc
158    {
159       // Set up a simple configuration that logs on the console.
160
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 JavaDoc
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 JavaDoc("Failed to validate server reply");
189       NDC.pop();
190       NDC.remove();
191    }
192    
193    /**
194     * @param args the command line arguments
195     */

196    public static void main(String JavaDoc 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 JavaDoc 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