OSDN Git Service

* config/i386/i386.md (*sinxf2): Rename to *sinxf2_i387.
[pf3gnuchains/gcc-fork.git] / libjava / classpath / gnu / javax / net / ssl / provider / Session.java
1 /* Session.java -- SSL and TLS session data.
2    Copyright (C) 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.net.ssl.provider;
40
41 import java.security.SecureRandom;
42 import java.security.cert.Certificate;
43 import java.security.cert.CertificateException;
44
45 import java.util.Arrays;
46 import java.util.ArrayList;
47 import java.util.Date;
48 import java.util.HashMap;
49 import java.util.List;
50 import java.util.Set;
51 import java.util.SortedSet;
52 import java.util.TreeSet;
53
54 import javax.net.ssl.SSLPeerUnverifiedException;
55 import javax.net.ssl.SSLPermission;
56 import javax.net.ssl.SSLSession;
57 import javax.net.ssl.SSLSessionBindingEvent;
58 import javax.net.ssl.SSLSessionBindingListener;
59 import javax.net.ssl.SSLSessionContext;
60 import javax.net.ssl.X509KeyManager;
61 import javax.net.ssl.X509TrustManager;
62 import javax.security.cert.X509Certificate;
63
64 import gnu.javax.net.ssl.SRPTrustManager;
65
66 /**
67  * A generic SSL session implementation for SSL and TLS.
68  */
69 final class Session implements SSLSession
70 {
71
72   // Constants and fields.
73   // -------------------------------------------------------------------------
74
75   private static final SSLPermission GET_SESSION_CONTEXT_PERMISSION =
76     new SSLPermission("getSSLSessionContext");
77
78   private final long creationTime;
79   private Date lastAccessedTime;
80   ID sessionId;
81   Certificate[] localCerts;
82   Certificate[] peerCerts;
83   X509Certificate[] peerCertChain;
84   String peerHost;
85   boolean peerVerified;
86   SessionContext context;
87   HashMap values;
88   boolean valid;
89   List enabledSuites;
90   CipherSuite cipherSuite;
91   SortedSet enabledProtocols;
92   ProtocolVersion protocol;
93   byte[] masterSecret;
94   SRPTrustManager srpTrustManager;
95   X509TrustManager trustManager;
96   X509KeyManager keyManager;
97   SecureRandom random;
98   SecurityParameters params;
99   Alert currentAlert;
100
101   // Constructor.
102   // -------------------------------------------------------------------------
103
104   Session()
105   {
106     this(System.currentTimeMillis());
107   }
108
109   Session(long creationTime)
110   {
111     peerVerified = false;
112     valid = true;
113     this.creationTime = creationTime;
114     lastAccessedTime = new Date(0L);
115     values = new HashMap();
116     if (("true").equalsIgnoreCase (Util.getSecurityProperty ("jessie.with.jce")))
117       params = new JCESecurityParameters();
118     else
119       params = new GNUSecurityParameters (this);
120   }
121
122   // Public instance methods.
123   // -------------------------------------------------------------------------
124
125   protected Object clone()
126   {
127     Session result = new Session(creationTime);
128     result.lastAccessedTime = lastAccessedTime;
129     result.sessionId = sessionId;
130     result.localCerts = (localCerts != null ? (Certificate[]) localCerts.clone() : null);
131     result.peerCerts = (peerCerts != null ? (Certificate[]) peerCerts.clone() : null);
132     result.peerHost = peerHost;
133     result.peerVerified = peerVerified;
134     result.context = context;
135     result.values = values;
136     result.enabledSuites = new ArrayList(enabledSuites);
137     result.cipherSuite = cipherSuite;
138     result.enabledProtocols = new TreeSet(enabledProtocols);
139     result.protocol = protocol;
140     result.masterSecret = masterSecret;
141     result.keyManager = keyManager;
142     result.srpTrustManager = srpTrustManager;
143     result.trustManager = trustManager;
144     result.random = random;
145     return result;
146   }
147
148   public String getCipherSuite()
149   {
150     return cipherSuite.toString();
151   }
152
153   public long getCreationTime()
154   {
155     return creationTime;
156   }
157
158   public byte[] getId()
159   {
160     return (sessionId != null ? sessionId.getId() : null);
161   }
162
163   public long getLastAccessedTime()
164   {
165     return lastAccessedTime.getTime();
166   }
167
168   public Certificate[] getLocalCertificates()
169   {
170     return (Certificate[]) (localCerts != null ? localCerts.clone() : null);
171   }
172
173   public Certificate[] getPeerCertificates() throws SSLPeerUnverifiedException
174   {
175     if (!peerVerified)
176       {
177         throw new SSLPeerUnverifiedException("peer not verified");
178       }
179     return (Certificate[]) (peerCerts != null ? peerCerts.clone() : null);
180   }
181
182   public X509Certificate[] getPeerCertificateChain()
183     throws SSLPeerUnverifiedException
184   {
185     if (!peerVerified)
186       {
187         throw new SSLPeerUnverifiedException("peer not verified");
188       }
189     if (peerCerts == null)
190       {
191         return null;
192       }
193     if (peerCertChain != null)
194       {
195         return (X509Certificate[]) peerCertChain.clone();
196       }
197     try
198       {
199         peerCertChain = new X509Certificate[peerCerts.length];
200         for (int i = 0; i < peerCerts.length; i++)
201           {
202             peerCertChain[i] = X509Certificate.getInstance(peerCerts[i].getEncoded());
203           }
204         return (X509Certificate[]) peerCertChain.clone();
205       }
206     catch (javax.security.cert.CertificateException ce)
207       {
208         return null;
209       }
210     catch (CertificateException ce2)
211       {
212         return null;
213       }
214   }
215
216   public String getPeerHost()
217   {
218     return peerHost;
219   }
220
221   public String getProtocol()
222   {
223     return protocol.toString();
224   }
225
226   public SSLSessionContext getSessionContext()
227   {
228     SecurityManager sm = System.getSecurityManager();
229     if (sm != null)
230       {
231         sm.checkPermission(GET_SESSION_CONTEXT_PERMISSION);
232       }
233     return context;
234   }
235
236   public String[] getValueNames()
237   {
238     Set names = values.keySet();
239     return (String[]) names.toArray(new String[names.size()]);
240   }
241
242   public Object getValue(String name)
243   {
244     return values.get(name);
245   }
246
247   public void putValue(String name, Object value)
248   {
249     values.put(name, value);
250     if (value instanceof SSLSessionBindingListener)
251       {
252         ((SSLSessionBindingListener) value).valueBound(
253           new SSLSessionBindingEvent(this, name));
254       }
255   }
256
257   public void removeValue(String name)
258   {
259     Object value = values.remove(name);
260     if (value != null && (value instanceof SSLSessionBindingListener))
261       {
262         ((SSLSessionBindingListener) value).valueUnbound(
263           new SSLSessionBindingEvent(this, name));
264       }
265   }
266
267   public void invalidate()
268   {
269     if (masterSecret != null)
270       {
271         for (int i = 0; i < masterSecret.length; i++)
272           {
273             masterSecret[i] = 0;
274           }
275         masterSecret = null;
276       }
277     valid = false;
278   }
279
280   synchronized void access()
281   {
282     lastAccessedTime.setTime(System.currentTimeMillis());
283     context.notifyAccess(this);
284   }
285
286   void setLastAccessedTime(long lastAccessedTime)
287   {
288     this.lastAccessedTime.setTime(lastAccessedTime);
289   }
290
291   // Inner classes.
292   // -------------------------------------------------------------------------
293
294   /**
295    * A byte array with appropriate <code>equals()</code>,
296    * <code>hashCode()</code>, and <code>compareTo()</code> semantics.
297    */
298   static final class ID implements Comparable
299   {
300
301     // Fields.
302     // -----------------------------------------------------------------------
303
304     /** The ID itself. */
305     private final byte[] id;
306
307     // Constructor.
308     // -----------------------------------------------------------------------
309
310     /**
311      * Creates a new ID.
312      *
313      * @param id The ID. The array is not cloned.
314      */
315     ID(byte[] id)
316     {
317       if (id == null)
318         {
319           throw new IllegalArgumentException();
320         }
321       this.id = id;
322     }
323
324     // Instance methods.
325     // -----------------------------------------------------------------------
326
327     public byte[] getId()
328     {
329       return (byte[]) id.clone();
330     }
331
332     public boolean equals(Object other)
333     {
334       if (other == null || !(other instanceof ID))
335         {
336           return false;
337         }
338       return Arrays.equals(id, ((ID) other).id);
339     }
340
341     public int hashCode()
342     {
343       int code = 0;
344       for (int i = 0; i < id.length; i++)
345         {
346           code |= (id[i] & 0xFF) << ((i & 3) << 3);
347         }
348       return code;
349     }
350
351     public int compareTo(Object other)
352     {
353       if (other == null || !(other instanceof ID))
354         {
355           return 1;
356         }
357       byte[] id2 = ((ID) other).id;
358       if (id.length != id2.length)
359         {
360           return (id.length < id2.length) ? -1 : 1;
361         }
362       for (int i = 0; i < id.length; i++)
363         {
364           if (id[i] < id2[i])
365             {
366               return -1;
367             }
368           else if (id[i] > id2[i])
369             {
370               return 1;
371             }
372         }
373       return 0;
374     }
375
376     public String toString()
377     {
378       return Util.toHexString(id, ':');
379     }
380   }
381 }