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, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017:
018: package org.apache.harmony.tools.keytool;
019:
020: import java.io.FileInputStream;
021: import java.io.FileNotFoundException;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.security.NoSuchProviderException;
025: import java.security.cert.CRLException;
026: import java.security.cert.CertificateException;
027: import java.security.cert.CertificateFactory;
028: import java.util.Collection;
029: import java.util.Collections;
030:
031: /**
032: * Class for reading an X.509 certificate or an X.509 certificate chain from the
033: * file or standard input.
034: */
035: public class CertReader {
036: // certificate factory to read certificates and CRLs
037: private static CertificateFactory certFactory;
038: // time to wait for user to input the data.
039: // need this for user to have time to paste the certificate to stdin.
040: private static long sleepPeriod;
041:
042: /**
043: * Reads an X.509 certificate or a certificate chain from the file with the
044: * given name or from stdin if the fileName is null and generates a
045: * collection of Certificates.
046: *
047: * @param fileName
048: * @param readOnlyFirst
049: * @param providerName
050: * @return
051: * @throws NoSuchProviderException
052: * @throws CertificateException
053: * @throws IOException
054: */
055: static Collection readCerts(String fileName, boolean readOnlyFirst,
056: String providerName) throws CertificateException,
057: NoSuchProviderException, IOException {
058:
059: InputStream input = getInputStream(fileName);
060: CertificateFactory factory = getCertificateFactory(providerName);
061: if (input == System.in) {
062: System.out.println("Please, input certificate(s)...");
063: }
064: try {
065: // let the user paste the certificates or CRLs, if read from stdin.
066: // If reading from file, don't sleep.
067: Thread.sleep(sleepPeriod);
068: } catch (InterruptedException e) {
069: // do nothing
070: }
071:
072: // if the file is empty or nothing was entered
073: // FIXME: remove available. Try to read and catch exception?
074: if (input.available() <= 0) {
075: throw new IOException("Empty input");
076: }
077:
078: Collection certCollection;
079: try {
080: // if only the first certificate is requested, return a
081: // single-element Collection
082: if (readOnlyFirst) {
083: certCollection = Collections.singleton(factory
084: .generateCertificate(input));
085: } else {
086: certCollection = factory.generateCertificates(input);
087: }
088: if (input != System.in) {
089: input.close();
090: }
091: return certCollection;
092: } catch (CertificateException e) {
093: throw new CertificateException(
094: "Failed to generate a certificate from the input. ",
095: e);
096: }
097: }
098:
099: /**
100: * Reads CRLs from the file with given name and generates a collection of
101: * CRLs.
102: *
103: * @param fileName
104: * @param providerName
105: * @return
106: * @throws NoSuchProviderException
107: * @throws CertificateException
108: * @throws IOException
109: * @throws CRLException
110: *
111: */
112: static Collection readCRLs(String fileName, String providerName)
113: throws CertificateException, NoSuchProviderException,
114: IOException, CRLException {
115:
116: InputStream input = getInputStream(fileName);
117: CertificateFactory factory = getCertificateFactory(providerName);
118: if (input == System.in) {
119: System.out.println("Please, input CRL(s)...");
120: }
121: try {
122: // let the user paste the certificates or CRLs, if read from stdin.
123: // If reading from file, don't sleep.
124: Thread.sleep(sleepPeriod);
125: } catch (InterruptedException e) {
126: // do nothing
127: }
128:
129: // if the file is empty or nothing was entered
130: // FIXME: remove available. Try to read and catch exception?
131: if (input.available() <= 0) {
132: throw new IOException("Empty input");
133: }
134:
135: try {
136: Collection crlCollection = factory.generateCRLs(input);
137: if (input != System.in) {
138: input.close();
139: }
140: return crlCollection;
141: } catch (CRLException e) {
142: throw new CRLException(
143: "Failed to generate a CRL from the input. ", e);
144: }
145: }
146:
147: // Returns an input stream - FileInputStream or System.in.
148: private static InputStream getInputStream(String fileName)
149: throws FileNotFoundException {
150: if (fileName != null) {
151: sleepPeriod = 0;
152: // use the file if its name is specified
153: return new FileInputStream(fileName);
154: } else {// if the file name is not given, use stdin
155: sleepPeriod = 3000;
156: return System.in;
157: }
158: }
159:
160: // Sets certFactory if it is still not set and returns it
161: private static CertificateFactory getCertificateFactory(
162: String providerName) throws CertificateException,
163: NoSuchProviderException {
164: if (certFactory == null) {
165: try {
166: if (providerName == null) {
167: certFactory = CertificateFactory
168: .getInstance("X.509");
169: } else {
170: certFactory = CertificateFactory.getInstance(
171: "X.509", providerName);
172: }
173: } catch (CertificateException e) {
174: throw new CertificateException(
175: "This type of certificate is not "
176: + "available from the provider. ", e);
177: } catch (NoSuchProviderException e) {
178: throw (NoSuchProviderException) new NoSuchProviderException(
179: "The provider " + providerName
180: + " is not found in the environment.")
181: .initCause(e);
182: }
183: }
184: return certFactory;
185: }
186: }
|