1 /* TrustedAuthorities.java
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is a part of GNU Classpath.
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.
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.
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
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
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. */
39 package gnu.javax.net.ssl.provider;
41 import gnu.java.security.x509.X500DistinguishedName;
42 import gnu.javax.net.ssl.provider.Extension.Value;
44 import java.io.PrintWriter;
45 import java.io.StringWriter;
46 import java.nio.ByteBuffer;
47 import java.nio.ByteOrder;
48 import java.util.Iterator;
49 import java.util.NoSuchElementException;
51 import javax.security.auth.x500.X500Principal;
54 * The trusted authorities hello extension.
58 TrustedAuthority trusted_authorities_list<0..2^16-1>;
62 IdentifierType identifier_type;
63 select (identifier_type) {
64 case pre_agreed: struct {};
65 case key_sha1_hash: SHA1Hash;
66 case x509_name: DistinguishedName;
67 case cert_sha1_hash: SHA1Hash;
72 pre_agreed(0), key_sha1_hash(1), x509_name(2),
73 cert_sha1_hash(3), (255)
76 opaque DistinguishedName<1..2^16-1>;</pre>
80 public class TrustedAuthorities extends Value
81 implements Iterable<TrustedAuthorities.TrustedAuthority>
83 private final ByteBuffer buffer;
85 public TrustedAuthorities(final ByteBuffer buffer)
87 this.buffer = buffer.duplicate().order(ByteOrder.BIG_ENDIAN);
90 // XXX really implement Builder.
94 return 2 + (buffer.getShort(0) & 0xFFFF);
97 public ByteBuffer buffer()
99 return (ByteBuffer) buffer.duplicate().limit(length());
104 int len = buffer.getShort(0) & 0xFFFF;
106 for (int i = 2; i < len; i++)
108 TrustedAuthority auth =
109 new TrustedAuthority((ByteBuffer) buffer.duplicate().position(i));
116 public TrustedAuthority get(final int index)
118 int len = buffer.getShort(0) & 0xFFFF;
121 while (i < len && n <= index)
123 TrustedAuthority auth =
124 new TrustedAuthority((ByteBuffer) buffer.duplicate().position(i));
130 throw new IndexOutOfBoundsException();
133 public String toString()
135 return toString(null);
138 public String toString(String prefix)
140 StringWriter str = new StringWriter();
141 PrintWriter out = new PrintWriter(str);
142 if (prefix != null) out.print(prefix);
143 out.println("struct {");
144 String subprefix = " ";
146 subprefix = prefix + subprefix;
147 for(TrustedAuthority ta : this)
149 if (prefix != null) out.print(prefix);
150 out.print("} TrustedAuthorities;");
151 return str.toString();
154 public Iterator<TrustedAuthority> iterator()
156 return new AuthoritiesIterator();
159 public class AuthoritiesIterator implements Iterator<TrustedAuthority>
163 public AuthoritiesIterator()
168 public TrustedAuthority next() throws NoSuchElementException
174 catch (IndexOutOfBoundsException ioobe)
176 throw new NoSuchElementException();
180 public boolean hasNext()
182 return index < size();
187 throw new UnsupportedOperationException();
191 public static class TrustedAuthority implements Constructed
193 private final ByteBuffer buffer;
195 public TrustedAuthority(final ByteBuffer buffer)
197 this.buffer = buffer;
202 switch (type().getValue())
207 case 2: return 3 + (buffer.getShort(1) & 0xFFFF);
209 throw new IllegalArgumentException("unknown authority type");
212 public byte[] sha1Hash()
214 IdentifierType t = type();
215 if (t != IdentifierType.CERT_SHA1_HASH
216 && t != IdentifierType.KEY_SHA1_HASH)
217 throw new IllegalArgumentException(t + " does not have a hash value");
218 byte[] b = new byte[20];
219 ((ByteBuffer) buffer.duplicate().position(1)).get(b);
223 public X500Principal name()
225 int len = buffer.getShort(1) & 0xFFFF;
226 byte[] b = new byte[len];
227 ((ByteBuffer) buffer.duplicate().position(3)).get(b);
228 return new X500Principal(b);
231 public IdentifierType type()
233 switch (buffer.get(0))
235 case 0: return IdentifierType.PRE_AGREED;
236 case 1: return IdentifierType.KEY_SHA1_HASH;
237 case 2: return IdentifierType.X509_NAME;
238 case 3: return IdentifierType.CERT_SHA1_HASH;
241 throw new IllegalArgumentException("invalid IdentifierType");
244 public String toString()
246 return toString(null);
249 public String toString(String prefix)
251 StringWriter str = new StringWriter();
252 PrintWriter out = new PrintWriter(str);
253 if (prefix != null) out.print(prefix);
254 out.println("struct {");
255 if (prefix != null) out.print(prefix);
256 out.print(" identifier_type = ");
259 switch (type().getValue())
264 if (prefix != null) out.print(prefix);
265 out.print(" sha1_hash = ");
266 out.print(Util.toHexString(sha1Hash(), ':'));
271 if (prefix != null) out.print(prefix);
272 out.print(" name = ");
276 if (prefix != null) out.print(prefix);
277 out.print("} TrustedAuthority;");
278 return str.toString();
282 public static enum IdentifierType
284 PRE_AGREED (0), KEY_SHA1_HASH (1), X509_NAME (2), CERT_SHA1_HASH (3);
286 private final int value;
288 private IdentifierType(final int value)
293 public int getValue()