2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / gnu / java / security / provider / X509CertificateFactory.java
blob62d3d38af626daeb9b806d612ec5a1aaea1f7e16
1 /* X509CertificateFactory.java -- generates X.509 certificates.
2 Copyright (C) 2003 Free Software Foundation, Inc.
4 This file is 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, or (at your option)
9 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; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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.java.security.provider;
41 import java.io.BufferedInputStream;
42 import java.io.EOFException;
43 import java.io.InputStream;
44 import java.io.IOException;
46 import java.security.cert.Certificate;
47 import java.security.cert.CertificateException;
48 import java.security.cert.CertificateFactorySpi;
49 import java.security.cert.CRL;
50 import java.security.cert.CRLException;
52 import java.util.Collection;
53 import java.util.LinkedList;
55 import gnu.java.io.Base64InputStream;
56 import gnu.java.security.x509.X509Certificate;
57 import gnu.java.security.x509.X509CRL;
59 public class X509CertificateFactory extends CertificateFactorySpi
62 // Constants.
63 // ------------------------------------------------------------------------
65 public static final String BEGIN_CERTIFICATE = "-----BEGIN CERTIFICATE-----";
66 public static final String END_CERTIFICATE = "-----END CERTIFICATE-----";
67 public static final String BEGIN_X509_CRL = "-----BEGIN X509 CRL-----";
68 public static final String END_X509_CRL = "-----END X509 CRL-----";
70 // Constructors.
71 // ------------------------------------------------------------------------
73 public X509CertificateFactory()
75 super();
78 // Instance methods.
79 // ------------------------------------------------------------------------
81 public Certificate engineGenerateCertificate(InputStream inStream)
82 throws CertificateException
84 try
86 return generateCert(inStream);
88 catch (IOException ioe)
90 throw new CertificateException(ioe.toString());
94 public Collection engineGenerateCertificates(InputStream inStream)
95 throws CertificateException
97 LinkedList certs = new LinkedList();
98 while (true)
102 certs.add(generateCert(inStream));
104 catch (EOFException eof)
106 break;
108 catch (IOException ioe)
110 throw new CertificateException(ioe.toString());
113 return certs;
116 public CRL engineGenerateCRL(InputStream inStream) throws CRLException
120 return generateCRL(inStream);
122 catch (IOException ioe)
124 throw new CRLException(ioe.toString());
128 public Collection engineGenerateCRLs(InputStream inStream)
129 throws CRLException
131 LinkedList crls = new LinkedList();
132 while (true)
136 crls.add(generateCRL(inStream));
138 catch (EOFException eof)
140 break;
142 catch (IOException ioe)
144 throw new CRLException(ioe.toString());
147 return crls;
150 // Own methods.
151 // ------------------------------------------------------------------------
153 private X509Certificate generateCert(InputStream inStream)
154 throws IOException, CertificateException
156 if (!inStream.markSupported())
157 inStream = new BufferedInputStream(inStream, 8192);
158 inStream.mark(20);
159 int i = inStream.read();
160 if (i == -1)
161 throw new EOFException();
163 // If the input is in binary DER format, the first byte MUST be
164 // 0x30, which stands for the ASN.1 [UNIVERSAL 16], which is the
165 // UNIVERSAL SEQUENCE, with the CONSTRUCTED bit (0x20) set.
167 // So if we do not see 0x30 here we will assume it is in Base-64.
168 if (i != 0x30)
170 inStream.reset();
171 StringBuffer line = new StringBuffer(80);
174 line.setLength(0);
177 i = inStream.read();
178 if (i == -1)
179 throw new EOFException();
180 if (i != '\n' && i != '\r')
181 line.append((char) i);
183 while (i != '\n' && i != '\r');
185 while (!line.toString().equals(BEGIN_CERTIFICATE));
186 X509Certificate ret = new X509Certificate(
187 new BufferedInputStream(new Base64InputStream(inStream), 8192));
188 line.setLength(0);
189 line.append('-'); // Base64InputStream will eat this.
192 i = inStream.read();
193 if (i == -1)
194 throw new EOFException();
195 if (i != '\n' && i != '\r')
196 line.append((char) i);
198 while (i != '\n' && i != '\r');
199 // XXX ???
200 if (!line.toString().equals(END_CERTIFICATE))
201 throw new CertificateException("no end-of-certificate marker");
202 return ret;
204 else
206 inStream.reset();
207 return new X509Certificate(inStream);
211 private X509CRL generateCRL(InputStream inStream)
212 throws IOException, CRLException
214 if (!inStream.markSupported())
215 inStream = new BufferedInputStream(inStream, 8192);
216 inStream.mark(20);
217 int i = inStream.read();
218 if (i == -1)
219 throw new EOFException();
221 // If the input is in binary DER format, the first byte MUST be
222 // 0x30, which stands for the ASN.1 [UNIVERSAL 16], which is the
223 // UNIVERSAL SEQUENCE, with the CONSTRUCTED bit (0x20) set.
225 // So if we do not see 0x30 here we will assume it is in Base-64.
226 if (i != 0x30)
228 inStream.reset();
229 StringBuffer line = new StringBuffer(80);
232 line.setLength(0);
235 i = inStream.read();
236 if (i == -1)
237 throw new EOFException();
238 if (i != '\n' && i != '\r')
239 line.append((char) i);
241 while (i != '\n' && i != '\r');
243 while (!line.toString().startsWith(BEGIN_X509_CRL));
244 X509CRL ret = new X509CRL(
245 new BufferedInputStream(new Base64InputStream(inStream), 8192));
246 line.setLength(0);
247 line.append('-'); // Base64InputStream will eat this.
250 i = inStream.read();
251 if (i == -1)
252 throw new EOFException();
253 if (i != '\n' && i != '\r')
254 line.append((char) i);
256 while (i != '\n' && i != '\r');
257 // XXX ???
258 if (!line.toString().startsWith(END_X509_CRL))
259 throw new CRLException("no end-of-CRL marker");
260 return ret;
262 else
264 inStream.reset();
265 return new X509CRL(inStream);