Source for gnu.javax.crypto.sasl.srp.ServerStore

   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 java.util.HashMap;
  42: 
  43: /**
  44:  * The server-side implementation of the SRP security context store.
  45:  */
  46: public class ServerStore
  47: {
  48:   /** The underlying singleton. */
  49:   private static ServerStore singleton = null;
  50:   /** The map of sid --> Security Context record. */
  51:   private static final HashMap sid2ssc = new HashMap();
  52:   /** The map of sid --> Session timing record. */
  53:   private static final HashMap sid2ttl = new HashMap();
  54:   /** A synchronisation lock. */
  55:   private static final Object lock = new Object();
  56:   /** A counter to generate legible SIDs. */
  57:   private static int counter = 0;
  58: 
  59:   /** Private constructor to enforce Singleton pattern. */
  60:   private ServerStore()
  61:   {
  62:     super();
  63: 
  64:     // TODO: add a cleaning timer thread
  65:   }
  66: 
  67:   /**
  68:    * Returns the classloader Singleton.
  69:    * 
  70:    * @return the classloader Singleton instance.
  71:    */
  72:   static synchronized final ServerStore instance()
  73:   {
  74:     if (singleton == null)
  75:       singleton = new ServerStore();
  76:     return singleton;
  77:   }
  78: 
  79:   /**
  80:    * Returns a legible new session identifier.
  81:    * 
  82:    * @return a new session identifier.
  83:    */
  84:   static synchronized final byte[] getNewSessionID()
  85:   {
  86:     final String sid = String.valueOf(++counter);
  87:     return new StringBuffer("SID-")
  88:         .append("0000000000".substring(0, 10 - sid.length())).append(sid)
  89:         .toString().getBytes();
  90:   }
  91: 
  92:   /**
  93:    * Returns a boolean flag indicating if the designated session is still alive
  94:    * or not.
  95:    * 
  96:    * @param sid the identifier of the session to check.
  97:    * @return <code>true</code> if the designated session is still alive.
  98:    *         <code>false</code> otherwise.
  99:    */
 100:   boolean isAlive(final byte[] sid)
 101:   {
 102:     boolean result = false;
 103:     if (sid != null && sid.length != 0)
 104:       {
 105:         synchronized (lock)
 106:           {
 107:             final String key = new String(sid);
 108:             final StoreEntry ctx = (StoreEntry) sid2ttl.get(key);
 109:             if (ctx != null)
 110:               {
 111:                 result = ctx.isAlive();
 112:                 if (! result) // invalidate it en-passant
 113:                   {
 114:                     sid2ssc.remove(key);
 115:                     sid2ttl.remove(key);
 116:                   }
 117:               }
 118:           }
 119:       }
 120:     return result;
 121:   }
 122: 
 123:   /**
 124:    * Records a mapping between a session identifier and the Security Context of
 125:    * the designated SRP server mechanism instance.
 126:    * 
 127:    * @param ttl the session's Time-To-Live indicator (in seconds).
 128:    * @param ctx the server's security context.
 129:    */
 130:   void cacheSession(final int ttl, final SecurityContext ctx)
 131:   {
 132:     synchronized (lock)
 133:       {
 134:         final String key = new String(ctx.getSID());
 135:         sid2ssc.put(key, ctx);
 136:         sid2ttl.put(key, new StoreEntry(ttl));
 137:       }
 138:   }
 139: 
 140:   /**
 141:    * Updates the mapping between the designated session identifier and the
 142:    * designated server's SASL Security Context. In the process, computes and
 143:    * return the underlying mechanism server's evidence that shall be returned to
 144:    * the client in a session re-use exchange.
 145:    * 
 146:    * @param sid the identifier of the session to restore.
 147:    * @return an SRP server's security context.
 148:    */
 149:   SecurityContext restoreSession(final byte[] sid)
 150:   {
 151:     final String key = new String(sid);
 152:     final SecurityContext result;
 153:     synchronized (lock)
 154:       {
 155:         result = (SecurityContext) sid2ssc.remove(key);
 156:         sid2ttl.remove(key);
 157:       }
 158:     return result;
 159:   }
 160: 
 161:   /**
 162:    * Removes all information related to the designated session ID.
 163:    * 
 164:    * @param sid the identifier of the seesion to invalidate.
 165:    */
 166:   void invalidateSession(final byte[] sid)
 167:   {
 168:     final String key = new String(sid);
 169:     synchronized (lock)
 170:       {
 171:         sid2ssc.remove(key);
 172:         sid2ttl.remove(key);
 173:       }
 174:   }
 175: }