CipherSuiteList.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* CipherSuiteList.java -- A list of cipher suites.
  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.provider;
  32. import java.io.PrintWriter;
  33. import java.io.StringWriter;
  34. import java.nio.ByteBuffer;
  35. import java.util.ConcurrentModificationException;
  36. import java.util.ListIterator;
  37. import java.util.NoSuchElementException;
  38. public final class CipherSuiteList implements Iterable<CipherSuite>
  39. {
  40. private final ByteBuffer buffer;
  41. private final ProtocolVersion version;
  42. private int modCount;
  43. public CipherSuiteList (final ByteBuffer buffer)
  44. {
  45. this (buffer, ProtocolVersion.SSL_3);
  46. }
  47. public CipherSuiteList (final ByteBuffer buffer, final ProtocolVersion version)
  48. {
  49. this.version = version;
  50. this.buffer = buffer;
  51. modCount = 0;
  52. }
  53. /**
  54. * Return the number of elements in this list.
  55. *
  56. * @return The size of this list.
  57. */
  58. public int size ()
  59. {
  60. return (buffer.getShort (0) & 0xFFFF) >>> 1;
  61. }
  62. /**
  63. * Get the cipher suite at the specified index.
  64. *
  65. * @param index The index of the suite to get.
  66. * @return The cipher suite at that index.
  67. * @throws IndexOutOfBoundsException If the index is negative or is
  68. * not less than {@link size()}.
  69. */
  70. public CipherSuite get (final int index)
  71. {
  72. int size = size ();
  73. if (index < 0 || index >= size)
  74. throw new IndexOutOfBoundsException ("limit: " + size
  75. + "; requested: " + index);
  76. return CipherSuite.forValue(buffer.getShort(2 + (index << 1))).resolve();
  77. }
  78. /**
  79. * Set the CipherSuite at the specified index. The list must have
  80. * sufficient size to hold the element (that is, <code>index &lt;=
  81. * size ()</code>).
  82. *
  83. * @param index The index to put the suite.
  84. * @param suite The CipherSuite object.
  85. * @throws IndexOutOfBoundsException If <code>index</code> is not
  86. * less than @{link #size()}, or if it is negative.
  87. * @throws NullPointerException If <code>suite</code> is
  88. * <code>null</code>.
  89. * @throws java.nio.ReadOnlyBufferException If the underlying buffer
  90. * is not writable.
  91. */
  92. public void put (final int index, final CipherSuite suite)
  93. {
  94. int size = size ();
  95. if (index < 0 || index >= size)
  96. throw new IndexOutOfBoundsException ("limit: " + size
  97. + "; requested: " + index);
  98. buffer.position (2 + (index << 1));
  99. buffer.put (suite.id ());
  100. modCount++;
  101. }
  102. /**
  103. * Sets the size of this list. You must call this if you are adding
  104. * elements to the list; calling {@link
  105. * #put(int,gnu.jessie.provider.CipherSuite)} does not expand the
  106. * list size (the same goes for removing elements, as there is no
  107. * <code>remove</code> method).
  108. *
  109. * @param newSize The new size of this list.
  110. * @throws IllegalArgumentException If the new size is negative or
  111. * greater than 32767, or if there is insufficient space for that
  112. * many elements in the underlying buffer.
  113. * @throws java.nio.ReadOnlyBufferException If the underlying buffer
  114. * is not writable.
  115. */
  116. public void setSize (final int newSize)
  117. {
  118. if (newSize < 0 || newSize > 32767)
  119. throw new IllegalArgumentException ("size must be between 0 and 32767");
  120. if ((newSize << 1) + 2 > buffer.capacity ())
  121. throw new IllegalArgumentException ("limit: " + buffer.capacity ()
  122. + "; requested: " + newSize);
  123. buffer.putShort (0, (short) (newSize << 1));
  124. modCount++;
  125. }
  126. public String toString ()
  127. {
  128. return toString (null);
  129. }
  130. public String toString (final String prefix)
  131. {
  132. StringWriter str = new StringWriter ();
  133. PrintWriter out = new PrintWriter (str);
  134. if (prefix != null)
  135. out.print (prefix);
  136. out.print ("[");
  137. out.print (size ());
  138. out.println ("] {");
  139. for (Iterator it = new Iterator (); it.hasNext (); )
  140. {
  141. CipherSuite suite = (CipherSuite) it.next ();
  142. if (prefix != null)
  143. out.print (prefix);
  144. out.print (" ");
  145. out.print (suite);
  146. if (it.hasNext ())
  147. out.print (",");
  148. out.println ();
  149. }
  150. if (prefix != null)
  151. out.print (prefix);
  152. out.print ("};");
  153. return str.toString ();
  154. }
  155. public boolean equals (Object o)
  156. {
  157. if (!(o instanceof CipherSuiteList))
  158. return false;
  159. CipherSuiteList that = (CipherSuiteList) o;
  160. if (size () != that.size ())
  161. return false;
  162. for (Iterator it1 = new Iterator (), it2 = that.new Iterator ();
  163. it1.hasNext () && it2.hasNext (); )
  164. {
  165. if (!it1.next ().equals (it2.next ()))
  166. return false;
  167. }
  168. return true;
  169. }
  170. public java.util.Iterator<CipherSuite> iterator ()
  171. {
  172. return new Iterator ();
  173. }
  174. /**
  175. * An iterator for the elements in this list. The iterator supports
  176. * only the <code>set</code> method out of the optional methods,
  177. * because elements in a CipherSuiteList may not be removed or
  178. * added; only the size of the list can be changed, and elements at
  179. * a specific index changed.
  180. */
  181. public class Iterator implements ListIterator<CipherSuite>
  182. {
  183. private final int modCount;
  184. private int index;
  185. Iterator ()
  186. {
  187. this.modCount = CipherSuiteList.this.modCount;
  188. index = 0;
  189. }
  190. public void add (CipherSuite cs)
  191. {
  192. throw new UnsupportedOperationException ();
  193. }
  194. public boolean hasNext ()
  195. {
  196. return (index < size ());
  197. }
  198. public boolean hasPrevious ()
  199. {
  200. return (index > 0);
  201. }
  202. public CipherSuite next () throws NoSuchElementException
  203. {
  204. if (modCount != CipherSuiteList.this.modCount)
  205. throw new ConcurrentModificationException ();
  206. try
  207. {
  208. return get (index++);
  209. }
  210. catch (IndexOutOfBoundsException ioobe)
  211. {
  212. throw new NoSuchElementException ();
  213. }
  214. }
  215. public int nextIndex ()
  216. {
  217. if (hasNext ())
  218. return (index + 1);
  219. return -1;
  220. }
  221. public CipherSuite previous () throws NoSuchElementException
  222. {
  223. if (index == 0)
  224. throw new NoSuchElementException ();
  225. if (modCount != CipherSuiteList.this.modCount)
  226. throw new ConcurrentModificationException ();
  227. try
  228. {
  229. return get (--index);
  230. }
  231. catch (IndexOutOfBoundsException ioobe) // on empty list
  232. {
  233. throw new NoSuchElementException ();
  234. }
  235. }
  236. public int previousIndex ()
  237. {
  238. return (index - 1);
  239. }
  240. public void remove ()
  241. {
  242. throw new UnsupportedOperationException ();
  243. }
  244. public void set (final CipherSuite cs)
  245. {
  246. put (index, cs);
  247. }
  248. }
  249. }