Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / javax / net / ssl / provider / Certificate.java
blobb1d6b2a014356ed4834a4339e10e75070601ae21
1 /* Certificate.java -- SSL Certificate 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.BufferedReader;
42 import java.io.ByteArrayInputStream;
43 import java.io.ByteArrayOutputStream;
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.StringReader;
50 import java.io.StringWriter;
52 import java.security.cert.CertificateEncodingException;
53 import java.security.cert.CertificateException;
54 import java.security.cert.CertificateFactory;
55 import java.security.cert.X509Certificate;
57 import java.util.LinkedList;
59 import javax.net.ssl.SSLProtocolException;
61 final class Certificate implements Handshake.Body
64 // Fields.
65 // -------------------------------------------------------------------------
67 private final X509Certificate[] certs;
69 // Constructors.
70 // -------------------------------------------------------------------------
72 Certificate(X509Certificate[] certs)
74 if (certs == null)
76 throw new NullPointerException();
78 this.certs = certs;
81 // Class methods.
82 // -------------------------------------------------------------------------
84 static Certificate read(InputStream in, CertificateType type)
85 throws IOException
87 if (type == CertificateType.X509)
89 int len = (in.read() & 0xFF) << 16 | (in.read() & 0xFF) << 8
90 | (in.read() & 0xFF);
91 byte[] buf = new byte[len];
92 int count = 0;
93 while (count < len)
95 int l = in.read(buf, count, len - count);
96 if (l == -1)
98 throw new EOFException("unexpected end of stream");
100 count += l;
104 LinkedList certs = new LinkedList();
105 CertificateFactory fact = CertificateFactory.getInstance("X.509");
106 ByteArrayInputStream bin = new ByteArrayInputStream(buf);
107 count = 0;
108 while (count < len)
110 int len2 = (bin.read() & 0xFF) << 16 | (bin.read() & 0xFF) << 8
111 | (bin.read() & 0xFF);
112 certs.add(fact.generateCertificate(bin));
113 count += len2 + 3;
115 return new Certificate((X509Certificate[])
116 certs.toArray(new X509Certificate[certs.size()]));
118 catch (CertificateException ce)
120 SSLProtocolException sslpe = new SSLProtocolException(ce.getMessage());
121 sslpe.initCause (ce);
122 throw sslpe;
125 else if (type == CertificateType.OPEN_PGP)
127 throw new UnsupportedOperationException("not yet implemented");
129 else
130 throw new Error("unsupported certificate type "+type);
133 // Instance methods.
134 // -------------------------------------------------------------------------
136 public void write(OutputStream out) throws IOException
138 ByteArrayOutputStream bout = new ByteArrayOutputStream();
141 for (int i = 0; i < certs.length; i++)
143 byte[] enc = certs[i].getEncoded();
144 bout.write((enc.length >>> 16) & 0xFF);
145 bout.write((enc.length >>> 8) & 0xFF);
146 bout.write( enc.length & 0xFF);
147 bout.write(enc);
150 catch (CertificateEncodingException cee)
152 throw new Error("cannot encode certificates");
154 catch (IOException ignored)
157 out.write(bout.size() >>> 16 & 0xFF);
158 out.write(bout.size() >>> 8 & 0xFF);
159 out.write(bout.size() & 0xFF);
160 bout.writeTo(out);
163 X509Certificate[] getCertificates()
165 return certs;
168 public String toString()
170 StringWriter str = new StringWriter();
171 PrintWriter out = new PrintWriter(str);
172 out.println("struct {");
173 out.println(" certificateList =");
174 for (int i = 0; i < certs.length; i++)
176 BufferedReader r =
177 new BufferedReader(new StringReader(certs[i].toString()));
178 String s;
181 while ((s = r.readLine()) != null)
183 out.print(" ");
184 out.println(s);
187 catch (IOException ignored)
191 out.println("} Certificate;");
192 return str.toString();