TimeStampResponseGenerator.cs :  » PDF » iTextSharp » Org » BouncyCastle » Tsp » 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 » Tsp » TimeStampResponseGenerator.cs
using System;
using System.Collections;
using System.IO;

using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Cmp;
using Org.BouncyCastle.Asn1.Cms;
using Org.BouncyCastle.Asn1.Tsp;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Utilities.Date;

namespace Org.BouncyCastle.Tsp{
  /**
   * Generator for RFC 3161 Time Stamp Responses.
   */
  public class TimeStampResponseGenerator
  {
    private PkiStatus        status;

    private Asn1EncodableVector    statusStrings;

    private int            failInfo;
    private TimeStampTokenGenerator  tokenGenerator;
    private IList          acceptedAlgorithms;
    private IList          acceptedPolicies;
    private IList          acceptedExtensions;

    public TimeStampResponseGenerator(
      TimeStampTokenGenerator  tokenGenerator,
      IList          acceptedAlgorithms)
      : this(tokenGenerator, acceptedAlgorithms, null, null)
    {
    }

    public TimeStampResponseGenerator(
      TimeStampTokenGenerator  tokenGenerator,
      IList          acceptedAlgorithms,
      IList          acceptedPolicy)
      : this(tokenGenerator, acceptedAlgorithms, acceptedPolicy, null)
    {
    }

    public TimeStampResponseGenerator(
      TimeStampTokenGenerator  tokenGenerator,
      IList          acceptedAlgorithms,
      IList          acceptedPolicies,
      IList          acceptedExtensions)
    {
      this.tokenGenerator = tokenGenerator;
      this.acceptedAlgorithms = acceptedAlgorithms;
      this.acceptedPolicies = acceptedPolicies;
      this.acceptedExtensions = acceptedExtensions;

      statusStrings = new Asn1EncodableVector();
    }

    private void addStatusString(
      string statusString)
    {
      statusStrings.Add(new DerUtf8String(statusString));
    }

    private void setFailInfoField(int field)
    {
      failInfo = failInfo | field;
    }

    private PkiStatusInfo getPkiStatusInfo()
    {
      Asn1EncodableVector  v = new Asn1EncodableVector(
        new DerInteger((int) status));

      if (statusStrings.Count > 0)
      {
        v.Add(new PkiFreeText(new DerSequence(statusStrings)));
      }

      if (failInfo != 0)
      {
        v.Add(new FailInfo(failInfo));
      }

      return new PkiStatusInfo(new DerSequence(v));
    }

    public TimeStampResponse Generate(
      TimeStampRequest  request,
      BigInteger      serialNumber,
      DateTime      genTime)
    {
      return Generate(request, serialNumber, new DateTimeObject(genTime));
    }

      /**
       * Return an appropriate TimeStampResponse.
       * <p>
       * If genTime is null a timeNotAvailable error response will be returned.
       *
       * @param request the request this response is for.
       * @param serialNumber serial number for the response token.
       * @param genTime generation time for the response token.
       * @param provider provider to use for signature calculation.
       * @return
       * @throws NoSuchAlgorithmException
       * @throws NoSuchProviderException
       * @throws TSPException
     * </p>
       */
    public TimeStampResponse Generate(
      TimeStampRequest  request,
      BigInteger      serialNumber,
      DateTimeObject    genTime)
    {
      TimeStampResp resp;

      try
      {
              if (genTime == null)
                  throw new TspValidationException("The time source is not available.",
            PkiFailureInfo.TimeNotAvailable);

        request.Validate(acceptedAlgorithms, acceptedPolicies, acceptedExtensions);

        status = PkiStatus.Granted;
        this.addStatusString("Operation Okay");

        PkiStatusInfo pkiStatusInfo = getPkiStatusInfo();

        ContentInfo tstTokenContentInfo;
        try
        {
          TimeStampToken token = tokenGenerator.Generate(request, serialNumber, genTime.Value);
          byte[] encoded = token.ToCmsSignedData().GetEncoded();

          tstTokenContentInfo = ContentInfo.GetInstance(Asn1Object.FromByteArray(encoded));
        }
        catch (IOException ioEx)
        {
          throw new TspException(
            "Timestamp token received cannot be converted to ContentInfo", ioEx);
        }

        resp = new TimeStampResp(pkiStatusInfo, tstTokenContentInfo);
      }
      catch (TspValidationException e)
      {
        status = PkiStatus.Rejection;

        this.setFailInfoField(e.FailureCode);
        this.addStatusString(e.Message);

        PkiStatusInfo pkiStatusInfo = getPkiStatusInfo();

        resp = new TimeStampResp(pkiStatusInfo, null);
      }

      try
      {
        return new TimeStampResponse(resp);
      }
      catch (IOException)
      {
        throw new TspException("created badly formatted response!");
      }
    }

    class FailInfo
      : DerBitString
    {
      internal FailInfo(
        int failInfoValue)
        : base(GetBytes(failInfoValue), GetPadBits(failInfoValue))
      {
      }
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.