summaryrefslogtreecommitdiff
path: root/gnu/javax/net/ssl/provider/ExtensionList.java
blob: d5aaad62155bf73f7d045db1f4229d62481f74cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package gnu.javax.net.ssl.provider;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;

/**
 * A list of extensions, that may appear in either the {@link ClientHello} or
 * {@link ServerHello}. The form of the extensions list is:
 * 
 * <tt>   Extension extensions_list&lt;1..2^16-1&gt;</tt>
 * 
 * @author csm
 */
public class ExtensionList implements Builder, Iterable<Extension>
{
  private final ByteBuffer buffer;
  private int modCount;

  public ExtensionList (ByteBuffer buffer)
  {
    this.buffer = buffer.duplicate().order(ByteOrder.BIG_ENDIAN);
    modCount = 0;
  }
  
  public ExtensionList(List<Extension> extensions)
  {
    int length = 2;
    for (Extension extension : extensions)
      length += extension.length();
    buffer = ByteBuffer.allocate(length);
    buffer.putShort((short) (length - 2));
    for (Extension extension : extensions)
      buffer.put(extension.buffer());
    buffer.rewind();
  }
  
  public ByteBuffer buffer()
  {
    return (ByteBuffer) buffer.duplicate().limit(length());
  }

  public Extension get (final int index)
  {
    int length = length ();
    int i;
    int n = 0;
    for (i = 2; i < length && n < index; )
      {
        int l = buffer.getShort (i+2) & 0xFFFF;
        i += l + 4;
        n++;
      }
    if (n < index)
      throw new IndexOutOfBoundsException ("no elemenet at " + index);
    int el = buffer.getShort (i+2) & 0xFFFF;
    ByteBuffer b = (ByteBuffer) buffer.duplicate().position(i).limit(i+el+4);
    return new Extension(b.slice());
  }
  
  /**
   * Returns the number of extensions this list contains.
   * 
   * @return The number of extensions.
   */
  public int size ()
  {
    int length = length ();
    if (length == 0)
      return 0;
    int n = 0;
    for (int i = 2; i < length; )
      {
        int len = buffer.getShort (i+2) & 0xFFFF;
        i += len + 4;
        n++;
      }
    return n;
  }

  /**
   * Returns the length of this extension list, in bytes.
   * 
   * @return The length of this extension list, in bytes.
   */
  public int length ()
  {
    return (buffer.getShort (0) & 0xFFFF) + 2;
  }
  
  /**
   * Sets the extension at index <i>i</i> to <i>e</i>. Note that setting an
   * element at an index <b>may</b> invalidate any other elements that come
   * after element at index <i>i</i>. In other words, no attempt is made to
   * move existing elements in this list, and since extensions are variable
   * length, you can <em>not</em> guarantee that extensions later in the list
   * will still be valid.
   * 
   * <p>Thus, elements of this list <b>must</b> be set in order of increasing
   * index.
   * 
   * @param index The index to set the extension at.
   * @param e The extension.
   * @throws java.nio.BufferOverflowException If setting the extension overflows
   *  the buffer.
   * @throws IllegalArgumentException If it isn't possible to find the given index
   *  in the current list (say, if no element index - 1 is set), or if setting
   *  the extension will overflow the current list length (given by {@link
   *  #length()}).
   */
  public void set (final int index, Extension e)
  {
    int length = length();
    int n = 0;
    int i;
    for (i = 2; i < length && n < index; )
      {
        int len = buffer.getShort(i+2) & 0xFFFF;
        i += len + 4;
        n++;
      }
    if (n < index)
      throw new IllegalArgumentException("nothing set at index " + (index-1)
                                         + " or insufficient space");
    if (i + e.length() + 2 > length)
      throw new IllegalArgumentException("adding this element will exceed the "
                                         + "list length");
    buffer.putShort(i, (short) e.type().getValue());
    buffer.putShort(i+2, (short) e.length());
    ((ByteBuffer) buffer.duplicate().position(i+4)).put (e.valueBuffer());
    modCount++;
  }
  
  /**
   * Reserve space for an extension at index <i>i</i> in the list. In other
   * words, this does the job of {@link #set(int, Extension)}, but does not
   * copy the extension value to the underlying buffer.
   * 
   * @param index The index of the extension to reserve space for.
   * @param t The type of the extension.
   * @param eLength The number of bytes to reserve for this extension. The total
   *  number of bytes used by this method is this length, plus four.
   */
  public void set (final int index, Extension.Type t, final int eLength)
  {
    int length = length ();
    int n = 0;
    int i;
    for (i = 2; i < length && n < index; )
      {
        int len = buffer.getShort (i+2) & 0xFFFF;
        i += len + 4;
        n++;
      }
    if (n < index)
      throw new IllegalArgumentException ("nothing set at index " + (index-1)
                                          + " or insufficient space");
    if (i + eLength + 2 > length)
      throw new IllegalArgumentException ("adding this element will exceed the "
                                          + "list length");
    buffer.putShort(i, (short) t.getValue());
    buffer.putShort(i+2, (short) eLength);
    modCount++;
  }
  
  /**
   * Set the total length of this list, in bytes.
   * 
   * @param newLength The new list length.
   */
  public void setLength (final int newLength)
  {
    if (newLength < 0 || newLength > 65535)
      throw new IllegalArgumentException ("invalid length");
    buffer.putShort (0, (short) newLength);
    modCount++;
  }
  
  public Iterator<Extension> iterator()
  {
    return new ExtensionsIterator();
  }

  public String toString()
  {
    return toString (null);
  }
  
  public String toString(final String prefix)
  {
    StringWriter str = new StringWriter();
    PrintWriter out = new PrintWriter(str);
    if (prefix != null) out.print(prefix);
    out.println("ExtensionList {");
    if (prefix != null) out.print(prefix);
    out.print("  length = ");
    out.print(length());
    out.println(";");
    String subprefix = "  ";
    if (prefix != null)
      subprefix = prefix + subprefix;
    for (Extension e : this)
      out.println(e.toString(subprefix));
    if (prefix != null) out.print(prefix);
    out.print("};");
    return str.toString();
  }

  /**
   * List iterator interface to an extensions list.
   * 
   * @author csm@gnu.org
   */
  public final class ExtensionsIterator implements ListIterator<Extension>
  {
    private final int modCount;
    private int index;
    private final int size;
    
    public ExtensionsIterator ()
    {
      this.modCount = ExtensionList.this.modCount;
      index = 0;
      size = size ();
    }

    public boolean hasNext()
    {
      return index < size;
    }

    public boolean hasPrevious()
    {
      return index > 0;
    }

    public Extension next() throws NoSuchElementException
    {
      if (modCount != ExtensionList.this.modCount)
        throw new ConcurrentModificationException ();
      if (!hasNext ())
        throw new NoSuchElementException ();
      return get (index++);
    }

    public Extension previous() throws NoSuchElementException
    {
      if (modCount != ExtensionList.this.modCount)
        throw new ConcurrentModificationException ();
      if (!hasPrevious ())
        throw new NoSuchElementException ();
      return get (--index);
    }

    public int nextIndex()
    {
      if (hasNext ())
        return index + 1;
      return index;
    }

    public int previousIndex()
    {
      if (hasPrevious ())
        return index - 1;
      return -1;
    }

    public void add(Extension e)
    {
      throw new UnsupportedOperationException ("cannot add items to this iterator");
    }

    public void remove()
    {
      throw new UnsupportedOperationException ("cannot remove items from this iterator");
    }

    public void set(Extension e)
    {
      ExtensionList.this.set (index, e);
    }
  }
}