ServerStore.java 5.4 KB

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