Session.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /* SessionImpl.java -- concrete definition of SSLSession.
  2. Copyright (C) 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.net.ssl;
  32. import gnu.java.lang.CPStringBuilder;
  33. import java.io.Serializable;
  34. import java.security.Principal;
  35. import java.security.SecureRandom;
  36. import java.security.cert.Certificate;
  37. import java.util.Arrays;
  38. import java.util.HashMap;
  39. import java.util.Set;
  40. import javax.crypto.SealedObject;
  41. import javax.net.ssl.SSLException;
  42. import javax.net.ssl.SSLPeerUnverifiedException;
  43. import javax.net.ssl.SSLSession;
  44. import javax.net.ssl.SSLSessionBindingEvent;
  45. import javax.net.ssl.SSLSessionBindingListener;
  46. import javax.net.ssl.SSLSessionContext;
  47. import javax.security.cert.X509Certificate;
  48. /**
  49. * A concrete implementation of the {@link SSLSession} interface. This
  50. * class is provided to allow pluggable {@link AbstractSessionContext}
  51. * implementations.
  52. */
  53. public abstract class Session implements SSLSession, Serializable
  54. {
  55. protected final long creationTime;
  56. protected long lastAccessedTime;
  57. protected int applicationBufferSize;
  58. protected ID sessionId;
  59. protected Certificate[] localCerts;
  60. protected Certificate[] peerCerts;
  61. protected X509Certificate[] peerCertChain;
  62. protected String peerHost;
  63. protected int peerPort;
  64. protected boolean peerVerified;
  65. protected HashMap<String,Object> values;
  66. protected boolean valid;
  67. protected boolean truncatedMac = false;
  68. transient protected SecureRandom random;
  69. transient protected SSLSessionContext context;
  70. protected Session()
  71. {
  72. creationTime = System.currentTimeMillis();
  73. values = new HashMap<String, Object>();
  74. applicationBufferSize = (1 << 14);
  75. }
  76. public void access()
  77. {
  78. lastAccessedTime = System.currentTimeMillis ();
  79. }
  80. public int getApplicationBufferSize()
  81. {
  82. return applicationBufferSize;
  83. }
  84. public String getCipherSuite()
  85. {
  86. return null;
  87. }
  88. public long getCreationTime()
  89. {
  90. return creationTime;
  91. }
  92. public byte[] getId()
  93. {
  94. return sessionId.id();
  95. }
  96. public ID id()
  97. {
  98. return sessionId;
  99. }
  100. public long getLastAccessedTime()
  101. {
  102. return lastAccessedTime;
  103. }
  104. public Certificate[] getLocalCertificates()
  105. {
  106. if (localCerts == null)
  107. return null;
  108. return (Certificate[]) localCerts.clone();
  109. }
  110. public Principal getLocalPrincipal()
  111. {
  112. if (localCerts != null)
  113. {
  114. if (localCerts[0] instanceof java.security.cert.X509Certificate)
  115. return ((java.security.cert.X509Certificate) localCerts[0]).getSubjectDN();
  116. }
  117. return null;
  118. }
  119. public int getPacketBufferSize()
  120. {
  121. return applicationBufferSize + 2048;
  122. }
  123. public Certificate[] getPeerCertificates() throws SSLPeerUnverifiedException
  124. {
  125. if (!peerVerified)
  126. throw new SSLPeerUnverifiedException("peer not verified");
  127. if (peerCerts == null)
  128. return null;
  129. return (Certificate[]) peerCerts.clone();
  130. }
  131. public X509Certificate[] getPeerCertificateChain()
  132. throws SSLPeerUnverifiedException
  133. {
  134. if (!peerVerified)
  135. throw new SSLPeerUnverifiedException("peer not verified");
  136. if (peerCertChain == null)
  137. return null;
  138. return (X509Certificate[]) peerCertChain.clone();
  139. }
  140. public String getPeerHost()
  141. {
  142. return peerHost;
  143. }
  144. public int getPeerPort()
  145. {
  146. return peerPort;
  147. }
  148. public Principal getPeerPrincipal() throws SSLPeerUnverifiedException
  149. {
  150. if (!peerVerified)
  151. throw new SSLPeerUnverifiedException("peer not verified");
  152. if (peerCertChain == null)
  153. return null;
  154. return peerCertChain[0].getSubjectDN();
  155. }
  156. public SSLSessionContext getSessionContext()
  157. {
  158. return context;
  159. }
  160. public String[] getValueNames()
  161. {
  162. Set<String> keys = this.values.keySet();
  163. return keys.toArray(new String[keys.size()]);
  164. }
  165. public Object getValue(String name)
  166. {
  167. return values.get(name);
  168. }
  169. public void invalidate()
  170. {
  171. valid = false;
  172. }
  173. public boolean isValid()
  174. {
  175. return valid;
  176. }
  177. public void putValue(String name, Object value)
  178. {
  179. values.put(name, value);
  180. try
  181. {
  182. if (value instanceof SSLSessionBindingListener)
  183. ((SSLSessionBindingListener) value).valueBound
  184. (new SSLSessionBindingEvent(this, name));
  185. }
  186. catch (Exception x)
  187. {
  188. }
  189. }
  190. public void removeValue(String name)
  191. {
  192. Object value = values.remove(name);
  193. try
  194. {
  195. if (value instanceof SSLSessionBindingListener)
  196. ((SSLSessionBindingListener) value).valueUnbound
  197. (new SSLSessionBindingEvent(this, name));
  198. }
  199. catch (Exception x)
  200. {
  201. }
  202. }
  203. public final boolean isTruncatedMac()
  204. {
  205. return truncatedMac;
  206. }
  207. /**
  208. * Prepare this session for serialization. Private data will be encrypted
  209. * with the given password, and this object will then be ready to be
  210. * serialized.
  211. *
  212. * @param password The password to protect this session with.
  213. * @throws SSLException If encrypting this session's private data fails.
  214. */
  215. public abstract void prepare (char[] password) throws SSLException;
  216. /**
  217. * Repair this session's private data after deserialization. This method
  218. * will decrypt this session's private data, and prepare the session for
  219. * use in new SSL connections.
  220. *
  221. * @param password The password to decrypt the private data with.
  222. * @throws SSLException
  223. */
  224. public abstract void repair(char[] password) throws SSLException;
  225. /**
  226. * Get the private data of this session. This method may only be called
  227. * after first calling {@link #prepare(char[])}.
  228. *
  229. * @return The sealed private data.
  230. * @throws SSLException If the private data have not been sealed.
  231. */
  232. public abstract SealedObject privateData() throws SSLException;
  233. /**
  234. * Set the private data of this session.
  235. * @param data
  236. * @throws SSLException
  237. */
  238. public abstract void setPrivateData(SealedObject data) throws SSLException;
  239. // Inner classes.
  240. // -------------------------------------------------------------------------
  241. /**
  242. * An SSL or TLS session ID.
  243. */
  244. public static final class ID implements Comparable, Serializable
  245. {
  246. // Fields.
  247. // -----------------------------------------------------------------------
  248. static final long serialVersionUID = 7887036954666565936L;
  249. /** The ID itself. */
  250. private final byte[] id;
  251. // Constructor.
  252. // -----------------------------------------------------------------------
  253. /**
  254. * Creates a new ID.
  255. *
  256. * @param id The ID. The array is cloned.
  257. */
  258. public ID (final byte[] id)
  259. {
  260. if (id.length > 32)
  261. throw new IllegalArgumentException ("session ID's are limited to 32 bytes");
  262. this.id = (byte[]) id.clone();
  263. }
  264. // Instance methods.
  265. // -----------------------------------------------------------------------
  266. public byte[] id()
  267. {
  268. return (byte[]) id.clone();
  269. }
  270. public boolean equals(Object other)
  271. {
  272. if (!(other instanceof ID))
  273. return false;
  274. return Arrays.equals(id, ((ID) other).id);
  275. }
  276. public int hashCode()
  277. {
  278. int code = 0;
  279. for (int i = 0; i < id.length; i++)
  280. code |= (id[i] & 0xFF) << ((i & 3) << 3);
  281. return code;
  282. }
  283. public int compareTo(Object other)
  284. {
  285. byte[] id2 = ((ID) other).id;
  286. if (id.length != id2.length)
  287. return (id.length < id2.length) ? -1 : 1;
  288. for (int i = 0; i < id.length; i++)
  289. {
  290. if ((id[i] & 0xFF) < (id2[i] & 0xFF))
  291. return -1;
  292. if ((id[i] & 0xFF) > (id2[i] & 0xFF))
  293. return 1;
  294. }
  295. return 0;
  296. }
  297. public String toString()
  298. {
  299. CPStringBuilder str = new CPStringBuilder (3 * id.length + 1);
  300. for (int i = 0; i < id.length; i++)
  301. {
  302. int x = id[i] & 0xFF;
  303. str.append (Character.forDigit ((x >>> 4) & 0xF, 16));
  304. str.append (Character.forDigit (x & 0xF, 16));
  305. if (i != id.length - 1)
  306. str.append (':');
  307. }
  308. return str.toString ();
  309. }
  310. }
  311. }