1 22 package org.jboss.security.srp; 23 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileOutputStream ; 27 import java.io.IOException ; 28 import java.io.ObjectInputStream ; 29 import java.io.ObjectOutputStream ; 30 import java.math.BigInteger ; 31 import java.security.KeyException ; 32 import java.security.NoSuchAlgorithmException ; 33 import java.util.Collections ; 34 import java.util.HashMap ; 35 import java.util.Map ; 36 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.SRPVerifierStore; 41 import org.jboss.security.srp.SRPVerifierStore.VerifierInfo; 42 43 57 public class SerialObjectStore implements SRPVerifierStore 58 { 59 private static Logger log = Logger.getLogger(SerialObjectStore.class); 60 private Map infoMap; 61 private BigInteger g; 62 private BigInteger N; 63 64 67 public SerialObjectStore() throws IOException 68 { 69 this(null); 70 } 71 74 public SerialObjectStore(File storeFile) throws IOException 75 { 76 if( storeFile == null ) 77 storeFile = new File ("SerialObjectStore.ser"); 78 if( storeFile.exists() == true ) 79 { 80 FileInputStream fis = new FileInputStream (storeFile); 81 ObjectInputStream ois = new ObjectInputStream (fis); 82 try 83 { 84 infoMap = (Map ) ois.readObject(); 85 } 86 catch(ClassNotFoundException e) 87 { 88 } 89 ois.close(); 90 fis.close(); 91 } 92 else 93 { 94 infoMap = Collections.synchronizedMap(new HashMap ()); 95 } 96 97 try 98 { 99 Util.init(); 100 } 101 catch(NoSuchAlgorithmException e) 102 { 103 e.printStackTrace(); 104 throw new IOException ("Failed to initialzed security utils: "+e.getMessage()); 105 } 106 N = SRPConf.getDefaultParams().N(); 107 g = SRPConf.getDefaultParams().g(); 108 log.trace("N: "+Util.tob64(N.toByteArray())); 109 log.trace("g: "+Util.tob64(g.toByteArray())); 110 byte[] hn = Util.newDigest().digest(N.toByteArray()); 111 log.trace("H(N): "+Util.tob64(hn)); 112 byte[] hg = Util.newDigest().digest(g.toByteArray()); 113 log.trace("H(g): "+Util.tob64(hg)); 114 } 115 116 public VerifierInfo getUserVerifier(String username) throws KeyException , IOException 118 { 119 VerifierInfo info = null; 120 if( infoMap != null ) 121 info = (VerifierInfo) infoMap.get(username); 122 if( info == null ) 123 throw new KeyException ("username: "+username+" not found"); 124 return info; 125 } 126 public void setUserVerifier(String username, VerifierInfo info) 127 { 128 infoMap.put(username, info); 129 } 130 131 public void verifyUserChallenge(String username, Object auxChallenge) 132 throws SecurityException 133 { 134 throw new SecurityException ("verifyUserChallenge not supported"); 135 } 136 138 141 public void save(File storeFile) throws IOException 142 { 143 FileOutputStream fos = new FileOutputStream (storeFile); 144 ObjectOutputStream oos = new ObjectOutputStream (fos); 145 synchronized( infoMap ) 146 { 147 oos.writeObject(infoMap); 148 } 149 oos.close(); 150 fos.close(); 151 } 152 153 public void addUser(String username, String password) 154 { 155 log.trace("addUser, username='"+username+"', password='"+password+"'"); 156 VerifierInfo info = new VerifierInfo(); 157 info.username = username; 158 162 String rs = "123456"; 163 info.salt = rs.getBytes(); 164 try 165 { 166 char[] pass = password.toCharArray(); 167 info.verifier = Util.calculateVerifier(username, pass, 168 info.salt, N, g); 169 info.g = g.toByteArray(); 170 info.N = N.toByteArray(); 171 if( log.isTraceEnabled() ) 172 { 173 log.trace("N: "+Util.tob64(info.N)); 174 log.trace("g: "+Util.tob64(info.g)); 175 log.trace("s: "+Util.tob64(info.salt)); 176 byte[] xb = Util.calculatePasswordHash(username, pass, info.salt); 177 log.trace("x: "+Util.tob64(xb)); 178 log.trace("v: "+Util.tob64(info.verifier)); 179 byte[] hn = Util.newDigest().digest(info.N); 180 log.trace("H(N): "+Util.tob64(hn)); 181 byte[] hg = Util.newDigest().digest(info.g); 182 log.trace("H(g): "+Util.tob64(hg)); 183 } 184 } 185 catch(Throwable t) 186 { 187 log.error("Failed to calculate verifier", t); 188 return; 189 } 190 191 setUserVerifier(username, info); 192 } 193 public void delUser(String username) 194 { 195 infoMap.remove(username); 196 } 197 198 public static void main(String [] args) throws IOException 199 { 200 File storeFile = new File ("SerialObjectStore.ser"); 201 SerialObjectStore store = new SerialObjectStore(); 202 203 for(int a = 0; a < args.length; a ++) 204 { 205 if( args[a].startsWith("-a") ) 206 { 207 store.addUser(args[a+1], args[a+2]); 208 } 209 else if( args[a].startsWith("-d") ) 210 { 211 store.delUser(args[a+1]); 212 } 213 } 214 store.save(storeFile); 215 } 216 } 217 | Popular Tags |