X509CrlParser.cs :  » PDF » iTextSharp » Org » BouncyCastle » X509 » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » PDF » iTextSharp 
iTextSharp » Org » BouncyCastle » X509 » X509CrlParser.cs
using System;
using System.Collections;
using System.IO;
using System.Text;

using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Security.Certificates;
using Org.BouncyCastle.Utilities.Encoders;
using Org.BouncyCastle.Utilities.IO;

namespace Org.BouncyCastle.X509{
  public class X509CrlParser
  {
    private static readonly PemParser PemCrlParser = new PemParser("CRL");

    private readonly bool lazyAsn1;

    private Asn1Set  sCrlData;
    private int    sCrlDataObjectCount;
    private Stream  currentCrlStream;

    public X509CrlParser()
      : this(false)
    {
    }

    public X509CrlParser(
      bool lazyAsn1)
    {
      this.lazyAsn1 = lazyAsn1;
    }

    private X509Crl ReadPemCrl(
      Stream inStream)
    {
      Asn1Sequence seq = PemCrlParser.ReadPemObject(inStream);

      return seq == null
        ?  null
        :  CreateX509Crl(CertificateList.GetInstance(seq));
    }

    private X509Crl ReadDerCrl(
      Asn1InputStream dIn)
    {
      Asn1Sequence seq = (Asn1Sequence)dIn.ReadObject();

      if (seq.Count > 1 && seq[0] is DerObjectIdentifier)
      {
        if (seq[0].Equals(PkcsObjectIdentifiers.SignedData))
        {
          sCrlData = SignedData.GetInstance(
            Asn1Sequence.GetInstance((Asn1TaggedObject) seq[1], true)).Crls;

          return GetCrl();
        }
      }

      return CreateX509Crl(CertificateList.GetInstance(seq));
    }

    private X509Crl GetCrl()
    {
      if (sCrlData == null || sCrlDataObjectCount >= sCrlData.Count)
      {
        return null;
      }

      return CreateX509Crl(
        CertificateList.GetInstance(
          sCrlData[sCrlDataObjectCount++]));
    }

    protected virtual X509Crl CreateX509Crl(
      CertificateList c)
    {
      return new X509Crl(c);
    }

    /// <summary>
    /// Create loading data from byte array.
    /// </summary>
    /// <param name="input"></param>
    public X509Crl ReadCrl(
      byte[] input)
    {
      return ReadCrl(new MemoryStream(input, false));
    }

    /// <summary>
    /// Create loading data from byte array.
    /// </summary>
    /// <param name="input"></param>
    public ICollection ReadCrls(
      byte[] input)
    {
      return ReadCrls(new MemoryStream(input, false));
    }

    /**
     * Generates a certificate revocation list (CRL) object and initializes
     * it with the data read from the input stream inStream.
     */
    public X509Crl ReadCrl(
      Stream inStream)
    {
      if (inStream == null)
        throw new ArgumentNullException("inStream");
      if (!inStream.CanRead)
        throw new ArgumentException("inStream must be read-able", "inStream");

      if (currentCrlStream == null)
      {
        currentCrlStream = inStream;
        sCrlData = null;
        sCrlDataObjectCount = 0;
      }
      else if (currentCrlStream != inStream) // reset if input stream has changed
      {
        currentCrlStream = inStream;
        sCrlData = null;
        sCrlDataObjectCount = 0;
      }

      try
      {
        if (sCrlData != null)
        {
          if (sCrlDataObjectCount != sCrlData.Count)
          {
            return GetCrl();
          }

          sCrlData = null;
          sCrlDataObjectCount = 0;
          return null;
        }

        PushbackStream pis = new PushbackStream(inStream);
        int tag = pis.ReadByte();

        if (tag < 0)
          return null;

        pis.Unread(tag);

        if (tag != 0x30)  // assume ascii PEM encoded.
        {
          return ReadPemCrl(pis);
        }

        Asn1InputStream asn1 = lazyAsn1
          ?  new LazyAsn1InputStream(pis)
          :  new Asn1InputStream(pis);

        return ReadDerCrl(asn1);
      }
      catch (CrlException e)
      {
        throw e;
      }
      catch (Exception e)
      {
        throw new CrlException(e.ToString());
      }
    }

    /**
     * Returns a (possibly empty) collection view of the CRLs read from
     * the given input stream inStream.
     *
     * The inStream may contain a sequence of DER-encoded CRLs, or
     * a PKCS#7 CRL set.  This is a PKCS#7 SignedData object, with the
     * only significant field being crls.  In particular the signature
     * and the contents are ignored.
     */
    public ICollection ReadCrls(
      Stream inStream)
    {
      X509Crl crl;
      IList crls = new ArrayList();

      while ((crl = ReadCrl(inStream)) != null)
      {
        crls.Add(crl);
      }

      return crls;
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.