V2TBSCertListGenerator.java
上传用户:lior1029
上传日期:2013-05-07
资源大小:209k
文件大小:5k
源码类别:

CA认证

开发平台:

Java

  1. package org.bouncycastle.asn1.x509;
  2. import java.io.*;
  3. import java.util.Vector;
  4. import java.util.Enumeration;
  5. import org.bouncycastle.asn1.*;
  6. /**
  7.  * Generator for Version 2 TBSCertList structures.
  8.  * <pre>
  9.  *  TBSCertList  ::=  SEQUENCE  {
  10.  *       version                 Version OPTIONAL,
  11.  *                                    -- if present, shall be v2
  12.  *       signature               AlgorithmIdentifier,
  13.  *       issuer                  Name,
  14.  *       thisUpdate              Time,
  15.  *       nextUpdate              Time OPTIONAL,
  16.  *       revokedCertificates     SEQUENCE OF SEQUENCE  {
  17.  *            userCertificate         CertificateSerialNumber,
  18.  *            revocationDate          Time,
  19.  *            crlEntryExtensions      Extensions OPTIONAL
  20.  *                                          -- if present, shall be v2
  21.  *                                 }  OPTIONAL,
  22.  *       crlExtensions           [0]  EXPLICIT Extensions OPTIONAL
  23.  *                                          -- if present, shall be v2
  24.  *                                 }
  25.  * </pre>
  26.  *
  27.  * <b>Note: This class may be subject to change</b>
  28.  */
  29. public class V2TBSCertListGenerator
  30. {
  31.     DERInteger version = new DERInteger(1);
  32.     AlgorithmIdentifier     signature;
  33.     X509Name                issuer;
  34.     Time                    thisUpdate, nextUpdate=null;
  35.     X509Extensions          extensions=null;
  36.     private Vector          crlentries=null;
  37.     public V2TBSCertListGenerator()
  38.     {
  39.     }
  40.     public void setSignature(
  41.         AlgorithmIdentifier    signature)
  42.     {
  43.         this.signature = signature;
  44.     }
  45.     public void setIssuer(
  46.         X509Name    issuer)
  47.     {
  48.         this.issuer = issuer;
  49.     }
  50.     public void setThisUpdate(
  51.         DERUTCTime thisUpdate)
  52.     {
  53.         this.thisUpdate = new Time(thisUpdate);
  54.     }
  55.     public void setNextUpdate(
  56.         DERUTCTime nextUpdate)
  57.     {
  58.         this.nextUpdate = new Time(nextUpdate);
  59.     }
  60.     public void setThisUpdate(
  61.         Time thisUpdate)
  62.     {
  63.         this.thisUpdate = thisUpdate;
  64.     }
  65.     public void setNextUpdate(
  66.         Time nextUpdate)
  67.     {
  68.         this.nextUpdate = nextUpdate;
  69.     }
  70.     public void addCRLEntry(
  71.         ASN1Sequence crlEntry)
  72.     {
  73.         if (crlentries == null)
  74.             crlentries = new Vector();
  75.         crlentries.addElement(crlEntry);
  76.     }
  77.     public void addCRLEntry(DERInteger userCertificate, DERUTCTime revocationDate, int reason)
  78.     {
  79.         addCRLEntry(userCertificate, new Time(revocationDate), reason);
  80.     }
  81.     public void addCRLEntry(DERInteger userCertificate, Time revocationDate, int reason)
  82.     {
  83.         ASN1EncodableVector v = new ASN1EncodableVector();
  84.         v.add(userCertificate);
  85.         v.add(revocationDate);
  86.         if (reason != 0)
  87.         {
  88.             CRLReason rf = new CRLReason(reason);
  89.             ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
  90.             DEROutputStream         dOut = new DEROutputStream(bOut);
  91.             try
  92.             {
  93.                 dOut.writeObject(rf);
  94.             }
  95.             catch (IOException e)
  96.             {
  97.                 throw new IllegalArgumentException("error encoding value: " + e);
  98.             }
  99.             byte[] value = bOut.toByteArray();
  100.             ASN1EncodableVector v1 = new ASN1EncodableVector();
  101.             v1.add(X509Extensions.ReasonCode);
  102.             v1.add(new DEROctetString(value));
  103.             X509Extensions ex = new X509Extensions(new DERSequence(
  104.                                                         new DERSequence(v1)));
  105.             v.add(ex);
  106.         }
  107.         if (crlentries == null)
  108.         {
  109.             crlentries = new Vector();
  110.         }
  111.         crlentries.addElement(new DERSequence(v));
  112.     }
  113.     public void setExtensions(
  114.         X509Extensions    extensions)
  115.     {
  116.         this.extensions = extensions;
  117.     }
  118.     public TBSCertList generateTBSCertList()
  119.     {
  120.         if ((signature == null) || (issuer == null) || (thisUpdate == null))
  121.         {
  122.             throw new IllegalStateException("Not all mandatory fields set in V2 TBSCertList generator.");
  123.         }
  124.         ASN1EncodableVector  v = new ASN1EncodableVector();
  125.         v.add(version);
  126.         v.add(signature);
  127.         v.add(issuer);
  128.         v.add(thisUpdate);
  129.         if (nextUpdate != null)
  130.         {
  131.             v.add(nextUpdate);
  132.         }
  133.         // Add CRLEntries if they exist
  134.         if (crlentries != null)
  135.         {
  136.             ASN1EncodableVector certs = new ASN1EncodableVector();
  137.             Enumeration it = crlentries.elements();
  138.             while( it.hasMoreElements() )
  139.             {
  140.                 certs.add((ASN1Sequence)it.nextElement());
  141.             }
  142.             v.add(new DERSequence(certs));
  143.         }
  144.         if (extensions != null)
  145.         {
  146.             v.add(new DERTaggedObject(0, extensions));
  147.         }
  148.         return new TBSCertList(new DERSequence(v));
  149.     }
  150. }