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.FileNotFoundException;
021: import java.io.IOException;
022: import java.security.NoSuchAlgorithmException;
023: import java.security.NoSuchProviderException;
024: import java.security.cert.CRLException;
025: import java.security.cert.CertificateException;
026: import java.security.cert.X509CRL;
027: import java.security.cert.X509CRLEntry;
028: import java.security.cert.X509Certificate;
029: import java.util.Collection;
030: import java.util.Iterator;
031:
032: /**
033: * Class for managing Certificate Revocation Lists (CRLs).
034: */
035: public class CRLManager {
036: /**
037: * Checks if the certificate given in the file is contained in the CRL which
038: * is stored in the file. If the file name is not given, stdin is used.
039: * File with CRL and the checked certificate file are specified in param.
040: *
041: * @return true if found at least one revoked certificate
042: * @param param
043: * @throws IOException
044: * @throws CRLException
045: * @throws NoSuchProviderException
046: * @throws CertificateException
047: * @throws FileNotFoundException
048: * @throws NoSuchAlgorithmException
049: */
050: static boolean checkRevoked(KeytoolParameters param)
051: throws FileNotFoundException, CertificateException,
052: NoSuchProviderException, CRLException, IOException,
053: NoSuchAlgorithmException {
054:
055: String provider = param.getProvider();
056: String certProvider = (param.getCertProvider() != null) ? param
057: .getCertProvider() : provider;
058: String mdProvider = (param.getMdProvider() != null) ? param
059: .getMdProvider() : provider;
060: // firstly, get CRLs from the file
061: Collection crls = CertReader.readCRLs(param.getCrlFile(),
062: certProvider);
063: // quit, if couldn't read anything
064: if (crls.isEmpty()) {
065: throw new CRLException(
066: "Failed to generate a CRL from the input. ");
067: }
068:
069: // secondly, get certificates from another file
070: Collection certs = CertReader.readCerts(param.getFileName(),
071: false, param.getProvider());
072: if (certs.isEmpty()) {
073: throw new CertificateException(
074: "Failed to generate a certificate from the input. ");
075: }
076:
077: boolean foundRevoked = false;
078:
079: // search in the CRLs for revocations of the certificates
080: Iterator crlIter = crls.iterator();
081: while (crlIter.hasNext()) {
082: X509CRL crl = (X509CRL) crlIter.next();
083: Iterator certIter = certs.iterator();
084: while (certIter.hasNext()) {
085: X509Certificate cert = (X509Certificate) certIter
086: .next();
087: X509CRLEntry entry = crl.getRevokedCertificate(cert);
088: if (entry != null) {
089: System.out.println("The certificate ...");
090: KeyStoreCertPrinter.printX509CertDetailed(cert,
091: mdProvider);
092: System.out.println("... is revoked on "
093: + entry.getRevocationDate() + "\n");
094: foundRevoked = true;
095: continue;
096: }
097: }
098: }
099:
100: if (certs.size() == 1 && !foundRevoked) {
101: System.out.println("The certificate ...");
102: KeyStoreCertPrinter.printX509CertDetailed(
103: (X509Certificate) certs.iterator().next(),
104: mdProvider);
105: System.out.println("... is not found in CRLs given");
106: } else if (!foundRevoked) {
107: System.out
108: .println("The certificates are not found in CRLs given");
109: }
110: return foundRevoked;
111: }
112: }
|