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