001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Vera Y. Petrashkova
020: * @version $Revision$
021: */package java.security.cert;
022:
023: import java.io.InputStream;
024: import java.security.NoSuchAlgorithmException;
025: import java.security.NoSuchProviderException;
026: import java.security.Provider;
027: import java.security.Security;
028: import java.util.Collection;
029: import java.util.Iterator;
030: import java.util.List;
031:
032: import org.apache.harmony.security.fortress.Engine;
033: import org.apache.harmony.security.internal.nls.Messages;
034:
035: /**
036: * This class provides the functionality of a certificate factory algorithm.
037: */
038:
039: public class CertificateFactory {
040:
041: // Store CertificateFactory service name
042: private static final String SERVICE = "CertificateFactory"; //$NON-NLS-1$
043:
044: // Used to access common engine functionality
045: private static Engine engine = new Engine(SERVICE);
046:
047: // Store used provider
048: private final Provider provider;
049:
050: // Store used CertificateFactorySpi implementation
051: private final CertificateFactorySpi spiImpl;
052:
053: // Store used type
054: private final String type;
055:
056: /**
057: * @com.intel.drl.spec_ref
058: *
059: */
060: protected CertificateFactory(CertificateFactorySpi certFacSpi,
061: Provider provider, String type) {
062: this .provider = provider;
063: this .type = type;
064: this .spiImpl = certFacSpi;
065: }
066:
067: /**
068: * Answers a new CertificateFactory of the given type.
069: *
070: * @param type
071: * java.lang.String Type of certificate desired
072: * @return CertificateFactory a concrete implementation for the certificate
073: * type desired.
074: *
075: * @exception CertificateException
076: * If the type cannot be found
077: *
078: * @exception NullPointerException
079: * If the type is null
080: */
081: public static final CertificateFactory getInstance(String type)
082: throws CertificateException {
083: if (type == null) {
084: throw new NullPointerException(Messages
085: .getString("security.07")); //$NON-NLS-1$
086: }
087: try {
088: synchronized (engine) {
089: engine.getInstance(type, null);
090: return new CertificateFactory(
091: (CertificateFactorySpi) engine.spi,
092: engine.provider, type);
093: }
094: } catch (NoSuchAlgorithmException e) {
095: throw new CertificateException(e);
096: }
097: }
098:
099: /**
100: * @com.intel.drl.spec_ref
101: *
102: * throws NullPointerException if algorithm is null (instead of
103: * CertificateException as in 1.4 release)
104: */
105: public static final CertificateFactory getInstance(String type,
106: String provider) throws CertificateException,
107: NoSuchProviderException {
108: if ((provider == null) || (provider.length() == 0)) {
109: throw new IllegalArgumentException(Messages
110: .getString("security.02")); //$NON-NLS-1$
111: }
112: Provider impProvider = Security.getProvider(provider);
113: if (impProvider == null) {
114: throw new NoSuchProviderException(provider);
115: }
116: return getInstance(type, impProvider);
117: }
118:
119: /**
120: * Answers a new CertificateFactory of the given type.
121: *
122: * @param type
123: * java.lang.String Type of certificate desired
124: * @param provider
125: * java.security.Provider Provider which has to implement the
126: * algorithm
127: * @return CertificateFactory a concrete implementation for the certificate
128: * type desired.
129: *
130: * @exception CertificateException
131: * If the type cannot be found
132: *
133: * @exception NullPointerException
134: * If algorithm is null
135: */
136: public static final CertificateFactory getInstance(String type,
137: Provider provider) throws CertificateException {
138: if (provider == null) {
139: throw new IllegalArgumentException(Messages
140: .getString("security.04")); //$NON-NLS-1$
141: }
142: if (type == null) {
143: throw new NullPointerException(Messages
144: .getString("security.07")); //$NON-NLS-1$
145: }
146: try {
147: synchronized (engine) {
148: engine.getInstance(type, provider, null);
149: return new CertificateFactory(
150: (CertificateFactorySpi) engine.spi, provider,
151: type);
152: }
153: } catch (NoSuchAlgorithmException e) {
154: throw new CertificateException(e.getMessage());
155: }
156: }
157:
158: /**
159: * Returns the Provider of the certificate factory represented by the
160: * receiver.
161: *
162: * @return Provider an instance of a subclass of java.security.Provider
163: */
164: public final Provider getProvider() {
165: return provider;
166: }
167:
168: /**
169: * Returns the Certificate type
170: *
171: * @return String type of certificate being used
172: */
173: public final String getType() {
174: return type;
175: }
176:
177: /**
178: * Generates and initializes a Certificate from data from the
179: * provided input stream.
180: *
181: * @param inStream
182: * InputStream Stream from where data is read to create the
183: * Certificate
184: *
185: * @return Certificate an initialized Certificate
186: * @exception CertificateException
187: * if parsing problems are detected
188: */
189: public final Certificate generateCertificate(InputStream inStream)
190: throws CertificateException {
191: return spiImpl.engineGenerateCertificate(inStream);
192: }
193:
194: /**
195: * Returns an Iterator over the supported CertPath encodings (as Strings).
196: * The first element is the default encoding.
197: *
198: * @return Iterator Iterator over supported CertPath encodings (as Strings)
199: */
200: public final Iterator<String> getCertPathEncodings() {
201: return spiImpl.engineGetCertPathEncodings();
202: }
203:
204: /**
205: * Generates a <code>CertPath</code> from data from the provided
206: * <code>InputStream</code>. The default encoding is assumed.
207: *
208: * @param inStream
209: * InputStream with PKCS7 or PkiPath encoded data
210: *
211: * @return CertPath a CertPath initialized from the provided data
212: *
213: * @throws CertificateException
214: * if parsing problems are detected
215: */
216: public final CertPath generateCertPath(InputStream inStream)
217: throws CertificateException {
218: Iterator it = getCertPathEncodings();
219: if (!it.hasNext()) {
220: throw new CertificateException(Messages
221: .getString("security.74")); //$NON-NLS-1$
222: }
223: return spiImpl.engineGenerateCertPath(inStream, (String) it
224: .next());
225: }
226:
227: /**
228: * Generates a <code>CertPath</code> from data from the provided
229: * <code>InputStream</code>. The encoding is that specified by the
230: * encoding parameter.
231: *
232: * @param inStream
233: * InputStream containing certificate path data in specified
234: * encoding
235: * @param encoding
236: * encoding of the data in the input stream
237: *
238: * @return CertPath a CertPath initialized from the provided data
239: *
240: * @throws CertificateException
241: * if parsing problems are detected
242: * @throws UnsupportedOperationException
243: * if the provider does not implement this method
244: */
245: public final CertPath generateCertPath(InputStream inStream,
246: String encoding) throws CertificateException {
247: return spiImpl.engineGenerateCertPath(inStream, encoding);
248: }
249:
250: /**
251: * Generates a <code>CertPath</code> from the provided List of
252: * Certificates. The encoding is the default encoding.
253: *
254: * @param certificates
255: * List containing certificates in a format supported by the
256: * CertificateFactory
257: *
258: * @return CertPath a CertPath initialized from the provided data
259: *
260: * @throws CertificateException
261: * if parsing problems are detected
262: * @throws UnsupportedOperationException
263: * if the provider does not implement this method
264: */
265: public final CertPath generateCertPath(
266: List<? extends Certificate> certificates)
267: throws CertificateException {
268: return spiImpl.engineGenerateCertPath(certificates);
269: }
270:
271: /**
272: * Generates and initializes a collection of Certificates from
273: * data from the provided input stream.
274: *
275: * @param inStream
276: * InputStream Stream from where data is read to create the
277: * Certificates
278: *
279: * @return Collection an initialized collection of Certificates
280: * @exception CertificateException
281: * if parsing problems are detected
282: */
283: public final Collection<? extends Certificate> generateCertificates(
284: InputStream inStream) throws CertificateException {
285: return spiImpl.engineGenerateCertificates(inStream);
286: }
287:
288: /**
289: * Generates and initializes a Certificate Revocation List from data from
290: * the provided input stream.
291: *
292: * @param inStream
293: * InputStream Stream from where data is read to create the CRL
294: *
295: * @return CRL an initialized Certificate Revocation List
296: * @exception CRLException
297: * if parsing problems are detected
298: */
299: public final CRL generateCRL(InputStream inStream)
300: throws CRLException {
301: return spiImpl.engineGenerateCRL(inStream);
302: }
303:
304: /**
305: * Generates and initializes a collection of Certificate Revocation List
306: * from data from the provided input stream.
307: *
308: * @param inStream
309: * InputStream Stream from where data is read to create the CRLs
310: *
311: * @return Collection an initialized collection of Certificate Revocation
312: * List
313: * @exception CRLException
314: * if parsing problems are detected
315: *
316: */
317: public final Collection<? extends CRL> generateCRLs(
318: InputStream inStream) throws CRLException {
319: return spiImpl.engineGenerateCRLs(inStream);
320: }
321: }
|