2 // ClientSessionCache.cs: Client-side cache for re-using sessions
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2006 Novell (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections;
32 namespace Mono.Security.Protocol.Tls {
34 internal class ClientSessionInfo : IDisposable {
36 // (by default) we keep this item valid for 3 minutes (if unused)
37 private const int DefaultValidityInterval = 3 * 60;
38 private static readonly int ValidityInterval;
40 private bool disposed;
41 private DateTime validuntil;
44 // see RFC2246 - Section 7
46 private byte[] masterSecret;
48 static ClientSessionInfo ()
51 ValidityInterval = DefaultValidityInterval;
53 string user_cache_timeout = Environment.GetEnvironmentVariable ("MONO_TLS_SESSION_CACHE_TIMEOUT");
54 if (user_cache_timeout == null) {
55 ValidityInterval = DefaultValidityInterval;
58 ValidityInterval = Int32.Parse (user_cache_timeout);
61 ValidityInterval = DefaultValidityInterval;
67 public ClientSessionInfo (string hostname, byte[] id)
80 public string HostName {
89 get { return ((masterSecret != null) && (validuntil > DateTime.UtcNow)); }
93 public void GetContext (Context context)
96 if (context.MasterSecret != null)
97 masterSecret = (byte[]) context.MasterSecret.Clone ();
100 public void SetContext (Context context)
103 if (masterSecret != null)
104 context.MasterSecret = (byte[]) masterSecret.Clone ();
107 public void KeepAlive ()
110 validuntil = DateTime.UtcNow.AddSeconds (ValidityInterval);
113 public void Dispose ()
116 GC.SuppressFinalize (this);
119 private void Dispose (bool disposing)
122 validuntil = DateTime.MinValue;
126 if (masterSecret != null) {
127 Array.Clear (masterSecret, 0, masterSecret.Length);
134 private void CheckDisposed ()
137 string msg = Locale.GetText ("Cache session information were disposed.");
138 throw new ObjectDisposedException (msg);
143 // note: locking is aggressive but isn't used often (and we gain much more :)
144 internal class ClientSessionCache {
146 static Hashtable cache;
147 static object locker;
149 static ClientSessionCache ()
151 cache = new Hashtable ();
152 locker = new object ();
155 // note: we may have multiple connections with a host, so
156 // possibly multiple entries per host (each with a different
157 // id), so we do not use the host as the hashtable key
158 static public void Add (string host, byte[] id)
161 string uid = BitConverter.ToString (id);
162 ClientSessionInfo si = (ClientSessionInfo) cache[uid];
164 cache.Add (uid, new ClientSessionInfo (host, id));
165 } else if (si.HostName == host) {
166 // we already have this and it's still valid
167 // on the server, so we'll keep it a little longer
170 // it's very unlikely but the same session id
171 // could be used by more than one host. In this
172 // case we replace the older one with the new one
175 cache.Add (uid, new ClientSessionInfo (host, id));
180 // return the first session us
181 static public byte[] FromHost (string host)
184 foreach (ClientSessionInfo si in cache.Values) {
185 if (si.HostName == host) {
187 // ensure it's still valid when we really need it
197 // only called inside the lock
198 static private ClientSessionInfo FromContext (Context context, bool checkValidity)
203 byte[] id = context.SessionId;
204 if ((id == null) || (id.Length == 0))
207 // do we have a session cached for this host ?
208 string uid = BitConverter.ToString (id);
210 ClientSessionInfo si = (ClientSessionInfo) cache[uid];
214 // In the unlikely case of multiple hosts using the same
215 // session id, we just act like we do not know about it
216 if (context.ClientSettings.TargetHost != si.HostName)
219 // yes, so what's its status ?
220 if (checkValidity && !si.Valid) {
230 static public bool SetContextInCache (Context context)
233 // Don't check the validity because the masterKey of the ClientSessionInfo
234 // can still be null when this is called the first time
235 ClientSessionInfo csi = FromContext (context, false);
239 csi.GetContext (context);
245 static public bool SetContextFromCache (Context context)
248 ClientSessionInfo csi = FromContext (context, true);
252 csi.SetContext (context);