Frames | No Frames |
1: /* ServerStore.java -- 2: Copyright (C) 2003, 2006 Free Software Foundation, Inc. 3: 4: This file is a part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2 of the License, or (at 9: your option) any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; if not, write to the Free Software 18: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19: USA 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: 39: package gnu.javax.crypto.sasl.srp; 40: 41: import gnu.java.lang.CPStringBuilder; 42: 43: import java.util.HashMap; 44: 45: /** 46: * The server-side implementation of the SRP security context store. 47: */ 48: public class ServerStore 49: { 50: /** The underlying singleton. */ 51: private static ServerStore singleton = null; 52: /** The map of sid --> Security Context record. */ 53: private static final HashMap sid2ssc = new HashMap(); 54: /** The map of sid --> Session timing record. */ 55: private static final HashMap sid2ttl = new HashMap(); 56: /** A synchronisation lock. */ 57: private static final Object lock = new Object(); 58: /** A counter to generate legible SIDs. */ 59: private static int counter = 0; 60: 61: /** Private constructor to enforce Singleton pattern. */ 62: private ServerStore() 63: { 64: super(); 65: 66: // TODO: add a cleaning timer thread 67: } 68: 69: /** 70: * Returns the classloader Singleton. 71: * 72: * @return the classloader Singleton instance. 73: */ 74: static synchronized final ServerStore instance() 75: { 76: if (singleton == null) 77: singleton = new ServerStore(); 78: return singleton; 79: } 80: 81: /** 82: * Returns a legible new session identifier. 83: * 84: * @return a new session identifier. 85: */ 86: static synchronized final byte[] getNewSessionID() 87: { 88: final String sid = String.valueOf(++counter); 89: return new CPStringBuilder("SID-") 90: .append("0000000000".substring(0, 10 - sid.length())).append(sid) 91: .toString().getBytes(); 92: } 93: 94: /** 95: * Returns a boolean flag indicating if the designated session is still alive 96: * or not. 97: * 98: * @param sid the identifier of the session to check. 99: * @return <code>true</code> if the designated session is still alive. 100: * <code>false</code> otherwise. 101: */ 102: boolean isAlive(final byte[] sid) 103: { 104: boolean result = false; 105: if (sid != null && sid.length != 0) 106: { 107: synchronized (lock) 108: { 109: final String key = new String(sid); 110: final StoreEntry ctx = (StoreEntry) sid2ttl.get(key); 111: if (ctx != null) 112: { 113: result = ctx.isAlive(); 114: if (! result) // invalidate it en-passant 115: { 116: sid2ssc.remove(key); 117: sid2ttl.remove(key); 118: } 119: } 120: } 121: } 122: return result; 123: } 124: 125: /** 126: * Records a mapping between a session identifier and the Security Context of 127: * the designated SRP server mechanism instance. 128: * 129: * @param ttl the session's Time-To-Live indicator (in seconds). 130: * @param ctx the server's security context. 131: */ 132: void cacheSession(final int ttl, final SecurityContext ctx) 133: { 134: synchronized (lock) 135: { 136: final String key = new String(ctx.getSID()); 137: sid2ssc.put(key, ctx); 138: sid2ttl.put(key, new StoreEntry(ttl)); 139: } 140: } 141: 142: /** 143: * Updates the mapping between the designated session identifier and the 144: * designated server's SASL Security Context. In the process, computes and 145: * return the underlying mechanism server's evidence that shall be returned to 146: * the client in a session re-use exchange. 147: * 148: * @param sid the identifier of the session to restore. 149: * @return an SRP server's security context. 150: */ 151: SecurityContext restoreSession(final byte[] sid) 152: { 153: final String key = new String(sid); 154: final SecurityContext result; 155: synchronized (lock) 156: { 157: result = (SecurityContext) sid2ssc.remove(key); 158: sid2ttl.remove(key); 159: } 160: return result; 161: } 162: 163: /** 164: * Removes all information related to the designated session ID. 165: * 166: * @param sid the identifier of the seesion to invalidate. 167: */ 168: void invalidateSession(final byte[] sid) 169: { 170: final String key = new String(sid); 171: synchronized (lock) 172: { 173: sid2ssc.remove(key); 174: sid2ttl.remove(key); 175: } 176: } 177: }