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