1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45:
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61:
62: public class X509CertificateFactory extends CertificateFactorySpi
63: {
64:
65:
66:
67:
68: public static final String BEGIN_CERTIFICATE = "-----BEGIN CERTIFICATE-----";
69: public static final String END_CERTIFICATE = "-----END CERTIFICATE-----";
70: public static final String BEGIN_X509_CRL = "-----BEGIN X509 CRL-----";
71: public static final String END_X509_CRL = "-----END X509 CRL-----";
72:
73:
74:
75:
76: public X509CertificateFactory()
77: {
78: super();
79: }
80:
81:
82:
83:
84: public Certificate engineGenerateCertificate(InputStream inStream)
85: throws CertificateException
86: {
87: try
88: {
89: return generateCert(inStream);
90: }
91: catch (IOException ioe)
92: {
93: CertificateException ce = new CertificateException(ioe.getMessage());
94: ce.initCause (ioe);
95: throw ce;
96: }
97: }
98:
99: public Collection engineGenerateCertificates(InputStream inStream)
100: throws CertificateException
101: {
102: LinkedList certs = new LinkedList();
103: while (true)
104: {
105: try
106: {
107: certs.add(generateCert(inStream));
108: }
109: catch (EOFException eof)
110: {
111: break;
112: }
113: catch (IOException ioe)
114: {
115: CertificateException ce = new CertificateException(ioe.getMessage());
116: ce.initCause (ioe);
117: throw ce;
118: }
119: }
120: return certs;
121: }
122:
123: public CRL engineGenerateCRL(InputStream inStream) throws CRLException
124: {
125: try
126: {
127: return generateCRL(inStream);
128: }
129: catch (IOException ioe)
130: {
131: CRLException crle = new CRLException(ioe.getMessage());
132: crle.initCause (ioe);
133: throw crle;
134: }
135: }
136:
137: public Collection engineGenerateCRLs(InputStream inStream)
138: throws CRLException
139: {
140: LinkedList crls = new LinkedList();
141: while (true)
142: {
143: try
144: {
145: crls.add(generateCRL(inStream));
146: }
147: catch (EOFException eof)
148: {
149: break;
150: }
151: catch (IOException ioe)
152: {
153: CRLException crle = new CRLException(ioe.getMessage());
154: crle.initCause (ioe);
155: throw crle;
156: }
157: }
158: return crls;
159: }
160:
161: public CertPath engineGenerateCertPath(List certs)
162: {
163: return new X509CertPath(certs);
164: }
165:
166: public CertPath engineGenerateCertPath(InputStream in)
167: throws CertificateEncodingException
168: {
169: return new X509CertPath(in);
170: }
171:
172: public CertPath engineGenerateCertPath(InputStream in, String encoding)
173: throws CertificateEncodingException
174: {
175: return new X509CertPath(in, encoding);
176: }
177:
178: public Iterator engineGetCertPathEncodings()
179: {
180: return X509CertPath.ENCODINGS.iterator();
181: }
182:
183:
184:
185:
186: private X509Certificate generateCert(InputStream inStream)
187: throws IOException, CertificateException
188: {
189: if (inStream == null)
190: throw new CertificateException("missing input stream");
191: if (!inStream.markSupported())
192: inStream = new BufferedInputStream(inStream, 8192);
193: inStream.mark(20);
194: int i = inStream.read();
195: if (i == -1)
196: throw new EOFException();
197:
198:
199:
200:
201:
202:
203: if (i != 0x30)
204: {
205: inStream.reset();
206: StringBuffer line = new StringBuffer(80);
207: do
208: {
209: line.setLength(0);
210: do
211: {
212: i = inStream.read();
213: if (i == -1)
214: throw new EOFException();
215: if (i != '\n' && i != '\r')
216: line.append((char) i);
217: }
218: while (i != '\n' && i != '\r');
219: }
220: while (!line.toString().equals(BEGIN_CERTIFICATE));
221: X509Certificate ret = new X509Certificate(
222: new BufferedInputStream(new Base64InputStream(inStream), 8192));
223: line.setLength(0);
224: line.append('-');
225: do
226: {
227: i = inStream.read();
228: if (i == -1)
229: throw new EOFException();
230: if (i != '\n' && i != '\r')
231: line.append((char) i);
232: }
233: while (i != '\n' && i != '\r');
234:
235: if (!line.toString().equals(END_CERTIFICATE))
236: throw new CertificateException("no end-of-certificate marker");
237: return ret;
238: }
239: else
240: {
241: inStream.reset();
242: return new X509Certificate(inStream);
243: }
244: }
245:
246: private X509CRL generateCRL(InputStream inStream)
247: throws IOException, CRLException
248: {
249: if (inStream == null)
250: throw new CRLException("missing input stream");
251: if (!inStream.markSupported())
252: inStream = new BufferedInputStream(inStream, 8192);
253: inStream.mark(20);
254: int i = inStream.read();
255: if (i == -1)
256: throw new EOFException();
257:
258:
259:
260:
261:
262:
263: if (i != 0x30)
264: {
265: inStream.reset();
266: StringBuffer line = new StringBuffer(80);
267: do
268: {
269: line.setLength(0);
270: do
271: {
272: i = inStream.read();
273: if (i == -1)
274: throw new EOFException();
275: if (i != '\n' && i != '\r')
276: line.append((char) i);
277: }
278: while (i != '\n' && i != '\r');
279: }
280: while (!line.toString().startsWith(BEGIN_X509_CRL));
281: X509CRL ret = new X509CRL(
282: new BufferedInputStream(new Base64InputStream(inStream), 8192));
283: line.setLength(0);
284: line.append('-');
285: do
286: {
287: i = inStream.read();
288: if (i == -1)
289: throw new EOFException();
290: if (i != '\n' && i != '\r')
291: line.append((char) i);
292: }
293: while (i != '\n' && i != '\r');
294:
295: if (!line.toString().startsWith(END_X509_CRL))
296: throw new CRLException("no end-of-CRL marker");
297: return ret;
298: }
299: else
300: {
301: inStream.reset();
302: return new X509CRL(inStream);
303: }
304: }
305: }