KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > util > cert > CrlExtensions


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13
14 package org.ejbca.util.cert;
15
16 import java.io.ByteArrayInputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.math.BigInteger JavaDoc;
19 import java.security.cert.X509CRL JavaDoc;
20
21 import org.apache.log4j.Logger;
22 import org.bouncycastle.asn1.ASN1InputStream;
23 import org.bouncycastle.asn1.ASN1OctetString;
24 import org.bouncycastle.asn1.DERInteger;
25 import org.bouncycastle.asn1.DERObject;
26 import org.bouncycastle.asn1.x509.CRLNumber;
27 import org.bouncycastle.asn1.x509.X509Extensions;
28
29 /**
30  * A class for reading values from CRL extensions.
31  *
32  * @author Tomas Gustavsson
33  * @version $Id: CrlExtensions.java,v 1.2 2006/07/28 07:14:16 anatom Exp $
34  */

35 public class CrlExtensions {
36     private static Logger log = Logger.getLogger(CrlExtensions.class);
37
38     /** Returns the CRL number if it exists as a CRL exension
39      *
40      * @return the CRLnumber, or 0 if no CRL number extension was found or an error reading it occured. Never return null.
41      */

42     public static BigInteger JavaDoc getCrlNumber(X509CRL JavaDoc crl) {
43         BigInteger JavaDoc ret = BigInteger.valueOf(0);
44         try {
45             DERObject obj = CrlExtensions.getExtensionValue(crl, X509Extensions.CRLNumber.getId());
46             DERInteger crlnum = CRLNumber.getInstance(obj);
47             ret = crlnum.getPositiveValue();
48         } catch (IOException JavaDoc e) {
49             log.error("Error reading CRL number extension: ", e);
50         }
51         return ret;
52     }
53     /**
54      * Return an Extension DERObject from a CRL
55      */

56     protected static DERObject getExtensionValue(X509CRL JavaDoc crl, String JavaDoc oid)
57       throws IOException JavaDoc {
58         if (crl == null) {
59             return null;
60         }
61         byte[] bytes = crl.getExtensionValue(oid);
62         if (bytes == null) {
63             return null;
64         }
65         ASN1InputStream aIn = new ASN1InputStream(new ByteArrayInputStream JavaDoc(bytes));
66         ASN1OctetString octs = (ASN1OctetString) aIn.readObject();
67         aIn = new ASN1InputStream(new ByteArrayInputStream JavaDoc(octs.getOctets()));
68         return aIn.readObject();
69     } //getExtensionValue
70

71
72 }
73
Popular Tags