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
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 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
65 // -------------------------------------------------------------------------
67 private final X509Certificate
[] certs
;
70 // -------------------------------------------------------------------------
72 Certificate(X509Certificate
[] certs
)
76 throw new NullPointerException();
82 // -------------------------------------------------------------------------
84 static Certificate
read(InputStream in
, CertificateType type
)
87 if (type
== CertificateType
.X509
)
89 int len
= (in
.read() & 0xFF) << 16 | (in
.read() & 0xFF) << 8
91 byte[] buf
= new byte[len
];
95 int l
= in
.read(buf
, count
, len
- count
);
98 throw new EOFException("unexpected end of stream");
104 LinkedList certs
= new LinkedList();
105 CertificateFactory fact
= CertificateFactory
.getInstance("X.509");
106 ByteArrayInputStream bin
= new ByteArrayInputStream(buf
);
110 int len2
= (bin
.read() & 0xFF) << 16 | (bin
.read() & 0xFF) << 8
111 | (bin
.read() & 0xFF);
112 certs
.add(fact
.generateCertificate(bin
));
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
);
125 else if (type
== CertificateType
.OPEN_PGP
)
127 throw new UnsupportedOperationException("not yet implemented");
130 throw new Error("unsupported certificate type "+type
);
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);
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);
163 X509Certificate
[] getCertificates()
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
++)
177 new BufferedReader(new StringReader(certs
[i
].toString()));
181 while ((s
= r
.readLine()) != null)
187 catch (IOException ignored
)
191 out
.println("} Certificate;");
192 return str
.toString();