Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TimeStampException"/> class with the specified error message, PKI status code, PKI status string and PKI failure info.
 /// </summary>
 /// <param name="message">The message that describes the error.</param>
 /// <param name="pkiStatus">PKI status.</param>
 /// <param name="pkiStatusString">PKI status string.</param>
 /// <param name="pkiFailureInfo">PKI failure info.</param>
 public TimeStampException(string message, PkiStatus pkiStatus, string pkiStatusString, PkiFailureInfo?pkiFailureInfo)
     : base(message)
 {
     this.PKIStatus       = pkiStatus;
     this.PKIStatusString = pkiStatusString;
     this.PKIFailureInfo  = pkiFailureInfo;
 }
Ejemplo n.º 3
0
        private bool ProcessResponse(
            ReadOnlyMemory <byte> source,
            out Rfc3161TimestampToken token,
            out Rfc3161RequestResponseStatus status,
            out int bytesConsumed,
            bool shouldThrow)
        {
            status = Rfc3161RequestResponseStatus.Unknown;
            token  = null;

            Rfc3161TimeStampResp resp;

            try
            {
                resp = AsnSerializer.Deserialize <Rfc3161TimeStampResp>(source, AsnEncodingRules.DER, out bytesConsumed);
            }
            catch (CryptographicException) when(!shouldThrow)
            {
                bytesConsumed = 0;
                status        = Rfc3161RequestResponseStatus.DoesNotParse;
                return(false);
            }

            // bytesRead will be set past this point

            PkiStatus pkiStatus = (PkiStatus)resp.Status.Status;

            if (pkiStatus != PkiStatus.Granted &&
                pkiStatus != PkiStatus.GrantedWithMods)
            {
                if (shouldThrow)
                {
                    throw new CryptographicException(
                              SR.Format(
                                  SR.Cryptography_TimestampReq_Failure,
                                  pkiStatus,
                                  resp.Status.FailInfo.GetValueOrDefault()));
                }

                status = Rfc3161RequestResponseStatus.RequestFailed;
                return(false);
            }

            if (!Rfc3161TimestampToken.TryDecode(resp.TimeStampToken.GetValueOrDefault(), out token, out _))
            {
                if (shouldThrow)
                {
                    throw new CryptographicException(SR.Cryptography_TimestampReq_BadResponse);
                }

                bytesConsumed = 0;
                status        = Rfc3161RequestResponseStatus.DoesNotParse;
                return(false);
            }

            status = ValidateResponse(token, shouldThrow);
            return(status == Rfc3161RequestResponseStatus.Accepted);
        }
Ejemplo n.º 4
0
        public TimeStampResponse Generate(
            TimeStampRequest request,
            BigInteger serialNumber,
            DateTime genTime)
        {
            TimeStampResp resp;

            try
            {
                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);
                    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!");
            }
        }
Ejemplo n.º 5
0
        public TimeStampResponse Generate(TimeStampRequest request, BigInteger serialNumber, DateTimeObject genTime)
        {
            //IL_0076: Expected O, but got Unknown
            //IL_00cf: Expected O, but got Unknown
            TimeStampResp resp;

            try
            {
                if (genTime == null)
                {
                    throw new TspValidationException("The time source is not available.", 512);
                }
                request.Validate(acceptedAlgorithms, acceptedPolicies, acceptedExtensions);
                status = PkiStatus.Granted;
                AddStatusString("Operation Okay");
                PkiStatusInfo pkiStatusInfo = GetPkiStatusInfo();
                ContentInfo   instance;
                try
                {
                    TimeStampToken timeStampToken = tokenGenerator.Generate(request, serialNumber, genTime.Value);
                    byte[]         encoded        = timeStampToken.ToCmsSignedData().GetEncoded();
                    instance = ContentInfo.GetInstance(Asn1Object.FromByteArray(encoded));
                }
                catch (IOException val)
                {
                    IOException e = val;
                    throw new TspException("Timestamp token received cannot be converted to ContentInfo", (global::System.Exception)(object) e);
                }
                resp = new TimeStampResp(pkiStatusInfo, instance);
            }
            catch (TspValidationException ex)
            {
                status = PkiStatus.Rejection;
                SetFailInfoField(ex.FailureCode);
                AddStatusString(((global::System.Exception)ex).get_Message());
                PkiStatusInfo pkiStatusInfo2 = GetPkiStatusInfo();
                resp = new TimeStampResp(pkiStatusInfo2, null);
            }
            try
            {
                return(new TimeStampResponse(resp));
            }
            catch (IOException val2)
            {
                IOException e2 = val2;
                throw new TspException("created badly formatted response!", (global::System.Exception)(object) e2);
            }
        }
Ejemplo n.º 6
0
        public TimeStampResponse Generate(TimeStampRequest request, BigInteger serialNumber, DateTimeObject genTime)
        {
            TimeStampResp resp;

            try
            {
                if (genTime == null)
                {
                    throw new TspValidationException("The time source is not available.", 512);
                }
                request.Validate(this.acceptedAlgorithms, this.acceptedPolicies, this.acceptedExtensions);
                this.status = PkiStatus.Granted;
                this.AddStatusString("Operation Okay");
                PkiStatusInfo pkiStatusInfo = this.GetPkiStatusInfo();
                ContentInfo   instance;
                try
                {
                    TimeStampToken timeStampToken = this.tokenGenerator.Generate(request, serialNumber, genTime.Value);
                    byte[]         encoded        = timeStampToken.ToCmsSignedData().GetEncoded();
                    instance = ContentInfo.GetInstance(Asn1Object.FromByteArray(encoded));
                }
                catch (IOException e)
                {
                    throw new TspException("Timestamp token received cannot be converted to ContentInfo", e);
                }
                resp = new TimeStampResp(pkiStatusInfo, instance);
            }
            catch (TspValidationException ex)
            {
                this.status = PkiStatus.Rejection;
                this.SetFailInfoField(ex.FailureCode);
                this.AddStatusString(ex.Message);
                PkiStatusInfo pkiStatusInfo2 = this.GetPkiStatusInfo();
                resp = new TimeStampResp(pkiStatusInfo2, null);
            }
            TimeStampResponse result;

            try
            {
                result = new TimeStampResponse(resp);
            }
            catch (IOException e2)
            {
                throw new TspException("created badly formatted response!", e2);
            }
            return(result);
        }
Ejemplo n.º 7
0
    public TimeStampResponse GenerateFailResponse(PkiStatus status, int failInfoField, string statusString)
    {
        this.status = status;
        SetFailInfoField(failInfoField);
        if (statusString != null)
        {
            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);
        }
    }
Ejemplo n.º 8
0
        public TimeStampResponse GenerateFailResponse(PkiStatus status, int failInfoField, string statusString)
        {
            //IL_0031: Expected O, but got Unknown
            this.status = status;
            SetFailInfoField(failInfoField);
            if (statusString != null)
            {
                AddStatusString(statusString);
            }
            PkiStatusInfo pkiStatusInfo = GetPkiStatusInfo();
            TimeStampResp resp          = new TimeStampResp(pkiStatusInfo, null);

            try
            {
                return(new TimeStampResponse(resp));
            }
            catch (IOException val)
            {
                IOException e = val;
                throw new TspException("created badly formatted response!", (global::System.Exception)(object) e);
            }
        }
 private PkiStatusEncodable(PkiStatus status)
     : this(new DerInteger((int)status))
 {
 }
Ejemplo n.º 10
0
 private PkiStatusEncodable(PkiStatus status)
     : this(new DerInteger((int)status))
 {
 }
        /**
         * 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,
            IBigInteger 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);
            }
        }
        /**
         * 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);
            }
        }
Ejemplo n.º 13
0
        private bool ProcessResponse(
            ReadOnlyMemory <byte> source,
            [NotNullWhen(true)] out Rfc3161TimestampToken?token,
            out Rfc3161RequestResponseStatus status,
            out int bytesConsumed,
            bool shouldThrow)
        {
            status = Rfc3161RequestResponseStatus.Unknown;
            token  = null;

            Rfc3161TimeStampResp resp;

            try
            {
                AsnValueReader reader         = new AsnValueReader(source.Span, AsnEncodingRules.DER);
                int            localBytesRead = reader.PeekEncodedValue().Length;

                Rfc3161TimeStampResp.Decode(ref reader, source, out resp);
                bytesConsumed = localBytesRead;
            }
            catch (CryptographicException) when(!shouldThrow)
            {
                bytesConsumed = 0;
                status        = Rfc3161RequestResponseStatus.DoesNotParse;
                return(false);
            }

            // bytesRead will be set past this point

            PkiStatus pkiStatus = (PkiStatus)resp.Status.Status;

            if (pkiStatus != PkiStatus.Granted &&
                pkiStatus != PkiStatus.GrantedWithMods)
            {
                if (shouldThrow)
                {
                    throw new CryptographicException(
                              SR.Format(
                                  SR.Cryptography_TimestampReq_Failure,
                                  pkiStatus,
                                  resp.Status.FailInfo.GetValueOrDefault()));
                }

                status = Rfc3161RequestResponseStatus.RequestFailed;
                return(false);
            }

            if (!Rfc3161TimestampToken.TryDecode(resp.TimeStampToken.GetValueOrDefault(), out token, out _))
            {
                if (shouldThrow)
                {
                    throw new CryptographicException(SR.Cryptography_TimestampReq_BadResponse);
                }

                bytesConsumed = 0;
                status        = Rfc3161RequestResponseStatus.DoesNotParse;
                return(false);
            }

            status = ValidateResponse(token, shouldThrow);
            return(status == Rfc3161RequestResponseStatus.Accepted);
        }
Ejemplo n.º 14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TimeStampException"/> class with the specified error message, nested exception and PKI status code.
 /// </summary>
 /// <param name="message">The message that describes the error.</param>
 /// <param name="statusCode">PKI status code.</param>
 public TimeStampException(string message, PkiStatus statusCode)
     : base(message)
 {
     this.PKIStatus = statusCode;
 }
Ejemplo n.º 15
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TimeStampException"/> class with the specified PKI status.
 /// </summary>
 /// <param name="pkiStatus">PKI status.</param>
 public TimeStampException(PkiStatus pkiStatus)
     : base()
 {
     this.PKIStatus = pkiStatus;
 }