123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- #pragma warning disable
- using System;
- using System.Collections;
- using System.IO;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cmp;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Tsp;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Date;
- namespace BestHTTP.SecureProtocol.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 |= 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);
- this.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 e)
- {
- throw new TspException("Timestamp token received cannot be converted to ContentInfo", e);
- }
- 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 e)
- {
- throw new TspException("created badly formatted response!", e);
- }
- }
- public TimeStampResponse GenerateGrantedResponse(
- TimeStampRequest request,
- BigInteger serialNumber,
- DateTimeObject genTime,
- String statusString,
- X509Extensions additionalExtensions)
- {
- TimeStampResp resp;
- try
- {
- if (genTime == null)
- throw new TspValidationException("The time source is not available.",
- PkiFailureInfo.TimeNotAvailable);
- request.Validate(acceptedAlgorithms, acceptedPolicies, acceptedExtensions);
- this.status = PkiStatus.Granted;
- this.AddStatusString(statusString);
- PkiStatusInfo pkiStatusInfo = GetPkiStatusInfo();
- ContentInfo tstTokenContentInfo;
- try
- {
- TimeStampToken token = tokenGenerator.Generate(request, serialNumber, genTime.Value,additionalExtensions);
- byte[] encoded = token.ToCmsSignedData().GetEncoded();
- tstTokenContentInfo = ContentInfo.GetInstance(Asn1Object.FromByteArray(encoded));
- }
- catch (IOException e)
- {
- throw new TspException("Timestamp token received cannot be converted to ContentInfo", e);
- }
- 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 e)
- {
- throw new TspException("created badly formatted response!", e);
- }
- }
-
- class FailInfo
- : DerBitString
- {
- internal FailInfo(int failInfoValue)
- : base(failInfoValue)
- {
- }
- }
- /**
- * Generate a TimeStampResponse with chosen status and FailInfoField.
- *
- * @param status the PKIStatus to set.
- * @param failInfoField the FailInfoField to set.
- * @param statusString an optional string describing the failure.
- * @return a TimeStampResponse with a failInfoField and optional statusString
- * @throws TSPException in case the response could not be created
- */
- public TimeStampResponse GenerateFailResponse(PkiStatus status, int failInfoField, string statusString)
- {
- this.status = status;
- this.SetFailInfoField(failInfoField);
- if (statusString != null)
- {
- this.AddStatusString(statusString);
- }
- PkiStatusInfo pkiStatusInfo = GetPkiStatusInfo();
- TimeStampResp resp = new TimeStampResp(pkiStatusInfo, null);
- try
- {
- return new TimeStampResponse(resp);
- }
- catch (IOException e)
- {
- throw new TspException("created badly formatted response!", e);
- }
- }
- }
- }
- #pragma warning restore
- #endif
|