1:
37:
38:
39: package ;
40:
41: import ;
42:
43: import ;
44:
45: import ;
46: import ;
47: import ;
48:
49: import ;
50: import ;
51: import ;
52:
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61:
62:
67: public abstract class Session implements SSLSession, Serializable
68: {
69: protected final long creationTime;
70: protected long lastAccessedTime;
71: protected int applicationBufferSize;
72:
73: protected ID sessionId;
74: protected Certificate[] localCerts;
75: protected Certificate[] peerCerts;
76: protected X509Certificate[] peerCertChain;
77: protected String peerHost;
78: protected int peerPort;
79: protected boolean peerVerified;
80: protected HashMap<String,Object> values;
81: protected boolean valid;
82: protected boolean truncatedMac = false;
83: transient protected SecureRandom random;
84: transient protected SSLSessionContext context;
85:
86: protected Session()
87: {
88: creationTime = System.currentTimeMillis();
89: values = new HashMap<String, Object>();
90: applicationBufferSize = (1 << 14);
91: }
92:
93: public void access()
94: {
95: lastAccessedTime = System.currentTimeMillis ();
96: }
97:
98: public int getApplicationBufferSize()
99: {
100: return applicationBufferSize;
101: }
102:
103: public String getCipherSuite()
104: {
105: return null;
106: }
107:
108: public long getCreationTime()
109: {
110: return creationTime;
111: }
112:
113: public byte[] getId()
114: {
115: return sessionId.id();
116: }
117:
118: public ID id()
119: {
120: return sessionId;
121: }
122:
123: public long getLastAccessedTime()
124: {
125: return lastAccessedTime;
126: }
127:
128: public Certificate[] getLocalCertificates()
129: {
130: if (localCerts == null)
131: return null;
132: return (Certificate[]) localCerts.clone();
133: }
134:
135: public Principal getLocalPrincipal()
136: {
137: if (localCerts != null)
138: {
139: if (localCerts[0] instanceof java.security.cert.X509Certificate)
140: return ((java.security.cert.X509Certificate) localCerts[0]).getSubjectDN();
141: }
142: return null;
143: }
144:
145: public int getPacketBufferSize()
146: {
147: return applicationBufferSize + 2048;
148: }
149:
150: public Certificate[] getPeerCertificates() throws SSLPeerUnverifiedException
151: {
152: if (!peerVerified)
153: throw new SSLPeerUnverifiedException("peer not verified");
154: if (peerCerts == null)
155: return null;
156: return (Certificate[]) peerCerts.clone();
157: }
158:
159: public X509Certificate[] getPeerCertificateChain()
160: throws SSLPeerUnverifiedException
161: {
162: if (!peerVerified)
163: throw new SSLPeerUnverifiedException("peer not verified");
164: if (peerCertChain == null)
165: return null;
166: return (X509Certificate[]) peerCertChain.clone();
167: }
168:
169: public String getPeerHost()
170: {
171: return peerHost;
172: }
173:
174: public int getPeerPort()
175: {
176: return peerPort;
177: }
178:
179: public Principal getPeerPrincipal() throws SSLPeerUnverifiedException
180: {
181: if (!peerVerified)
182: throw new SSLPeerUnverifiedException("peer not verified");
183: if (peerCertChain == null)
184: return null;
185: return peerCertChain[0].getSubjectDN();
186: }
187:
188: public SSLSessionContext getSessionContext()
189: {
190: return context;
191: }
192:
193: public String[] getValueNames()
194: {
195: Set<String> keys = this.values.keySet();
196: return keys.toArray(new String[keys.size()]);
197: }
198:
199: public Object getValue(String name)
200: {
201: return values.get(name);
202: }
203:
204: public void invalidate()
205: {
206: valid = false;
207: }
208:
209: public boolean isValid()
210: {
211: return valid;
212: }
213:
214: public void putValue(String name, Object value)
215: {
216: values.put(name, value);
217: try
218: {
219: if (value instanceof SSLSessionBindingListener)
220: ((SSLSessionBindingListener) value).valueBound
221: (new SSLSessionBindingEvent(this, name));
222: }
223: catch (Exception x)
224: {
225: }
226: }
227:
228: public void removeValue(String name)
229: {
230: Object value = values.remove(name);
231: try
232: {
233: if (value instanceof SSLSessionBindingListener)
234: ((SSLSessionBindingListener) value).valueUnbound
235: (new SSLSessionBindingEvent(this, name));
236: }
237: catch (Exception x)
238: {
239: }
240: }
241:
242: public final boolean isTruncatedMac()
243: {
244: return truncatedMac;
245: }
246:
247:
255: public abstract void prepare (char[] password) throws SSLException;
256:
257:
265: public abstract void repair(char[] password) throws SSLException;
266:
267:
274: public abstract SealedObject privateData() throws SSLException;
275:
276:
281: public abstract void setPrivateData(SealedObject data) throws SSLException;
282:
283:
284:
285:
286:
289: public static final class ID implements Comparable, Serializable
290: {
291:
292:
293:
294:
295: static final long serialVersionUID = 7887036954666565936L;
296:
297: private final byte[] id;
298:
299:
300:
301:
302:
307: public ID (final byte[] id)
308: {
309: if (id.length > 32)
310: throw new IllegalArgumentException ("session ID's are limited to 32 bytes");
311: this.id = (byte[]) id.clone();
312: }
313:
314:
315:
316:
317: public byte[] id()
318: {
319: return (byte[]) id.clone();
320: }
321:
322: public boolean equals(Object other)
323: {
324: if (!(other instanceof ID))
325: return false;
326: return Arrays.equals(id, ((ID) other).id);
327: }
328:
329: public int hashCode()
330: {
331: int code = 0;
332: for (int i = 0; i < id.length; i++)
333: code |= (id[i] & 0xFF) << ((i & 3) << 3);
334: return code;
335: }
336:
337: public int compareTo(Object other)
338: {
339: byte[] id2 = ((ID) other).id;
340: if (id.length != id2.length)
341: return (id.length < id2.length) ? -1 : 1;
342: for (int i = 0; i < id.length; i++)
343: {
344: if ((id[i] & 0xFF) < (id2[i] & 0xFF))
345: return -1;
346: if ((id[i] & 0xFF) > (id2[i] & 0xFF))
347: return 1;
348: }
349: return 0;
350: }
351:
352: public String toString()
353: {
354: CPStringBuilder str = new CPStringBuilder (3 * id.length + 1);
355: for (int i = 0; i < id.length; i++)
356: {
357: int x = id[i] & 0xFF;
358: str.append (Character.forDigit ((x >>> 4) & 0xF, 16));
359: str.append (Character.forDigit (x & 0xF, 16));
360: if (i != id.length - 1)
361: str.append (':');
362: }
363: return str.toString ();
364: }
365: }
366: }