Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / javax / net / ssl / provider / CertificateRequest.java
blob0f788039b0bf4b79b00c5060a69be4386398cc9f
1 /* CertificateRequest.java -- SSL CertificateRequest message.
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.ByteArrayOutputStream;
43 import java.io.DataInputStream;
44 import java.io.EOFException;
45 import java.io.InputStream;
46 import java.io.IOException;
47 import java.io.OutputStream;
48 import java.io.PrintWriter;
49 import java.io.StringWriter;
51 import java.lang.reflect.Constructor;
52 import java.lang.reflect.Method;
54 import java.util.LinkedList;
55 import java.security.Principal;
57 final class CertificateRequest implements Handshake.Body
60 // Fields.
61 // -------------------------------------------------------------------------
63 private final ClientType[] types;
64 private final Principal[] authorities;
66 // Constructor.
67 // -------------------------------------------------------------------------
69 CertificateRequest(ClientType[] types, Principal[] authorities)
71 if (types == null)
73 throw new NullPointerException();
75 this.types = types;
76 if (authorities == null)
78 throw new NullPointerException();
80 this.authorities = authorities;
83 // Class methods.
84 // -------------------------------------------------------------------------
86 static CertificateRequest read(InputStream in) throws IOException
88 DataInputStream din = new DataInputStream(in);
89 ClientType[] types = new ClientType[din.readUnsignedByte()];
90 for (int i = 0; i < types.length; i++)
92 types[i] = ClientType.read(din);
95 LinkedList authorities = new LinkedList();
96 byte[] buf = new byte[din.readUnsignedShort()];
97 din.readFully(buf);
98 ByteArrayInputStream bin = new ByteArrayInputStream(buf);
99 try
101 String x500name = Util.getSecurityProperty("jessie.x500.class");
102 if (x500name == null)
104 x500name = "org.metastatic.jessie.pki.X500Name";
106 Class x500class = null;
107 ClassLoader cl = ClassLoader.getSystemClassLoader();
108 if (cl != null)
110 x500class = cl.loadClass(x500name);
112 else
114 x500class = Class.forName(x500name);
116 Constructor c = x500class.getConstructor(new Class[] { new byte[0].getClass() });
117 while (bin.available() > 0)
119 buf = new byte[(bin.read() & 0xFF) << 8 | (bin.read() & 0xFF)];
120 bin.read(buf);
121 authorities.add(c.newInstance(new Object[] { buf }));
124 catch (IOException ioe)
126 throw ioe;
128 catch (Exception ex)
130 throw new Error(ex.toString());
132 return new CertificateRequest(types,
133 (Principal[]) authorities.toArray(new Principal[authorities.size()]));
136 // Instance methods.
137 // -------------------------------------------------------------------------
139 public void write(OutputStream out) throws IOException
141 ByteArrayOutputStream bout = new ByteArrayOutputStream();
142 out.write(types.length);
143 for (int i = 0; i < types.length; i++)
145 out.write(types[i].getValue());
150 Class x500class = authorities[0].getClass();
151 Method m = x500class.getMethod("getEncoded", null);
152 for (int i = 0; i < authorities.length; i++)
154 byte[] buf = (byte[]) m.invoke(authorities[i], null);
155 bout.write(buf.length >>> 8 & 0xFF);
156 bout.write(buf.length & 0xFF);
157 bout.write(buf, 0, buf.length);
160 catch (Exception ex)
162 throw new Error(ex.toString());
164 out.write(bout.size() >>> 8 & 0xFF);
165 out.write(bout.size() & 0xFF);
166 bout.writeTo(out);
169 ClientType[] getTypes()
171 return types;
174 String[] getTypeStrings()
178 return (String[]) Util.transform(types, String.class, "toString", null);
180 catch (Exception x)
182 return null;
186 Principal[] getAuthorities()
188 return authorities;
191 public String toString()
193 StringWriter str = new StringWriter();
194 PrintWriter out = new PrintWriter(str);
195 out.println("struct {");
196 out.print(" types = ");
197 for (int i = 0; i < types.length; i++)
199 out.print(types[i]);
200 if (i != types.length - 1)
201 out.print(", ");
203 out.println(";");
204 out.println(" authorities =");
205 for (int i = 0; i < authorities.length; i++)
207 out.print(" ");
208 out.print(authorities[i].getName());
209 if (i != types.length - 1)
210 out.println(",");
212 out.println(";");
213 out.println("} CertificateRequest;");
214 return str.toString();
217 // Inner class.
218 // -------------------------------------------------------------------------
220 static final class ClientType implements Enumerated
223 // Constants and fields.
224 // -----------------------------------------------------------------------
226 static final ClientType
227 RSA_SIGN = new ClientType(1), DSS_SIGN = new ClientType(2),
228 RSA_FIXED_DH = new ClientType(3), DSS_FIXED_DH = new ClientType(4);
230 private final int value;
232 // Constructor.
233 // -----------------------------------------------------------------------
235 private ClientType(int value)
237 this.value = value;
240 // Class method.
241 // -----------------------------------------------------------------------
243 static ClientType read(InputStream in) throws IOException
245 int i = in.read();
246 if (i == -1)
248 throw new EOFException("unexpected end of input stream");
250 switch (i & 0xFF)
252 case 1: return RSA_SIGN;
253 case 2: return DSS_SIGN;
254 case 3: return RSA_FIXED_DH;
255 case 4: return DSS_FIXED_DH;
256 default: return new ClientType(i);
260 // Instance methods.
261 // -----------------------------------------------------------------------
263 public byte[] getEncoded()
265 return new byte[] { (byte) value };
268 public int getValue()
270 return value;
273 public String toString()
275 switch (value)
277 case 1: return "rsa_sign";
278 case 2: return "dss_sign";
279 case 3: return "rsa_fixed_dh";
280 case 4: return "dss_fixed_dh";
281 default: return "unknown(" + value + ")";