Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / javax / net / ssl / provider / Extensions.java
blob9ed9619f06fb2cb872d54093f4d1699e90878029
1 /* Extensions.java -- various static extension utilities.
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
19 USA
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
24 combination.
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 java.io.ByteArrayInputStream;
42 import java.io.IOException;
43 import java.io.UnsupportedEncodingException;
44 import java.util.Collections;
45 import java.util.LinkedList;
46 import java.util.List;
48 import javax.security.auth.x500.X500Principal;
50 import gnu.java.security.x509.X500DistinguishedName;
52 final class Extensions
55 // Constants.
56 // -------------------------------------------------------------------------
58 private static final Integer _512 = new Integer(512),
59 _1024 = new Integer(1024), _2048 = new Integer(2048),
60 _4096 = new Integer(4096);
62 // Class methods only.
63 private Extensions() { }
65 // Class methods.
66 // -------------------------------------------------------------------------
68 static List getServerName(Extension ex)
70 LinkedList l = new LinkedList();
71 byte[] buf = ex.getValue();
72 int pos = 0;
73 try
75 while (pos < buf.length)
77 if (buf[pos++] != 0)
78 break;
79 int len = (buf[pos++] & 0xFF) << 8;
80 len |= buf[pos++] & 0xFF;
81 l.add(new String(buf, pos, len, "UTF-8"));
82 pos += len;
85 catch (Exception x)
88 return Collections.unmodifiableList(l);
91 static List getClientCertTypes(Extension ex) throws IOException
93 List l = new LinkedList();
94 ByteArrayInputStream in = new ByteArrayInputStream(ex.getValue());
95 final int len = in.read() & 0xFF;
96 for (int i = 0; i < len; i++)
98 l.add(CertificateType.read(in));
100 return Collections.unmodifiableList(l);
103 static CertificateType getServerCertType(Extension ex) throws IOException
105 return CertificateType.read(new ByteArrayInputStream(ex.getValue()));
108 static Integer getMaxFragmentLength(Extension ex)
110 switch (ex.getValue()[0] & 0xFF)
112 case 1: return _512;
113 case 2: return _1024;
114 case 3: return _2048;
115 case 4: return _4096;
117 throw new IllegalArgumentException();
120 static Object[] getTrustedCA(Extension ex)
122 byte[] buf = ex.getValue();
123 int type = buf[0] & 0xFF;
126 switch (type)
128 case 0:
129 return new Object[] { new Integer(type), null };
130 case 1:
131 case 3:
132 return new Object[] { new Integer(type),
133 Util.trim(buf, 1, 20) };
134 case 2:
135 return new Object[] { new Integer(type),
136 new X500Principal(Util.trim(buf, 1, 20)) };
139 catch (Exception x)
142 throw new IllegalArgumentException();
145 static String getSRPUsername(Extension ex)
147 int len = ex.getValue()[0] & 0xFF;
148 if (len > ex.getValue().length - 1)
149 throw new IllegalArgumentException();
152 return new String(ex.getValue(), 1, len, "UTF-8");
154 catch (UnsupportedEncodingException uee)
156 throw new Error(uee.toString());