private static TimeStampToken RequestTST(string fileName, string tsaService, Oid hashAlg, string policy, string nonce, bool certReq, UserCredentials credentials, TimestampResult result)
        {
            byte[] nonceBytes    = null;
            byte[] hashedMessage = DigestUtils.ComputeDigest(fileName, hashAlg);
            if (!string.IsNullOrEmpty(nonce))
            {
                nonceBytes = HexStringToBytes(nonce);
            }

            Request request = new Request(hashedMessage, hashAlg, nonceBytes, policy, certReq);

            result.TimeStampRequest = request;
            result.Tsq = request.ToByteArray();

            return(TimeStampClient.RequestTimeStampToken(tsaService, request, credentials));
        }
        private static TimeStampToken RequestTimeStamp(string tsaAddress, byte[] contentToTimestamp,
                                                       Oid hashAlg, string requestedPolicy, string nonce, bool certReq,
                                                       UserCredentials credentials, LogDelegate logger, bool logExceptions,
                                                       TimestampResult result)
        {
            try
            {
                if (logger == null)
                {
                    return(null);
                }

                logger(string.Format("=== {0:" + DateTimeFormat + "} =============================================", DateTime.UtcNow));
                logger($"Requesting time stamp from {tsaAddress}");

                TimeStampToken token = RequestTST(contentToTimestamp, tsaAddress, hashAlg,
                                                  requestedPolicy, nonce, certReq, credentials, result);
                if (null == token)
                {
                    var msg = "Empty response token";
                    logger(msg);
                    throw new Exception(msg);
                }

                result.Tsr = token.ToByteArray();

                logger("Time stamp successfully received:");

                var timeStampSerNum = BytesToHexString(token.SerialNumber);
                logger($"    Serial number: {timeStampSerNum}");
                result.SerialNumber = timeStampSerNum;

                var localTime = (token.Time).ToLocalTime();
                result.TimeUTC   = token.Time;
                result.TimeLocal = localTime;
                logger($"    UTCTime: {token.Time:dd MMM yyyy HH':'mm':'ss}");
                logger($"    Time: {localTime:dd MMM yyyy HH':'mm':'ss}");

                logger("TSA certificate:");
                var issuerName = token.TsaInformation?.TsaCertIssuerName?.Name ?? "";
                result.TSAName = issuerName;
                logger($"    Issuer: {issuerName}");
                logger($"    Serial: {BytesToHexString(token.TsaInformation?.TsaCertSerialNumber ?? new byte[0])}");

                if (null != token.TsaInformation?.TsaCert)
                {
                    logger($"    Subject: {token.TsaInformation.TsaCert.Subject}");
                    logger($"    Valid from: {token.TsaInformation.TsaCert.NotBefore}");
                    logger($"    Valid to: {token.TsaInformation.TsaCert.NotAfter}");
                }

                if (null != token.PolicyOid)
                {
                    result.Policy = token.PolicyOid;
                }

                return(token);
            }
            catch (Exception e)
            {
                logger("Error occurred:");
                logger(logExceptions ? e.ToString() : e.Message);

                throw;
            }
        }
예제 #3
0
 /// <summary>
 /// Requests time stamp for the data from stream.
 /// </summary>
 /// <param name="tsaUri">URL of a TSA service.</param>
 /// <param name="dataToTimestamp">Specifies the data to time-stamp.</param>
 /// <param name="digestType">Specifies the hash algorithm to be used to compute digest from data.</param>
 /// <returns>Time-stamp token</returns>
 public static TimeStampToken RequestTimeStampToken(string tsaUri, Stream dataToTimestamp, Oid digestType)
 {
     return(RequestTimeStampToken(tsaUri, dataToTimestamp, digestType, null));
 }
예제 #4
0
 /// <summary>
 /// Requests time stamp for the file specified by the path.
 /// </summary>
 /// <param name="tsaUri">URL of a TSA service.</param>
 /// <param name="pathToFileToTimestamp">Specifies the file to time-stamp.</param>
 /// <param name="digestType">Specifies the hash algorithm to be used to compute digest from data.</param>
 /// <returns>Time-stamp token</returns>
 public static TimeStampToken RequestTimeStampToken(string tsaUri, string pathToFileToTimestamp, Oid digestType)
 {
     return(RequestTimeStampToken(tsaUri, pathToFileToTimestamp, digestType, null));
 }
예제 #5
0
        /// <summary>
        /// Requests time stamp for the file specified by the path.
        /// </summary>
        /// <param name="tsaUri">URL of a TSA service.</param>
        /// <param name="pathToFileToTimestamp">Specifies the file to time-stamp.</param>
        /// <param name="digestType">Specifies the hash algorithm to be used to compute digest from data.</param>
        /// <param name="credentials">User's credentials to access TSA service.</param>
        /// <returns>Time-stamp token</returns>
        public static TimeStampToken RequestTimeStampToken(string tsaUri, string pathToFileToTimestamp, Oid digestType, UserCredentials credentials)
        {
            if (null == pathToFileToTimestamp)
            {
                throw new ArgumentNullException("pathToFileToTimestamp");
            }

            using (FileStream fs = new FileStream(pathToFileToTimestamp, FileMode.Open, FileAccess.Read))
            {
                return(RequestTimeStampToken(tsaUri, fs, digestType, credentials));
            }
        }
예제 #6
0
        /// <summary>
        /// Requests time stamp for the data stored in the byte array.
        /// </summary>
        /// <param name="tsaUri">URL of a TSA service.</param>
        /// <param name="dataToTimestamp">Specifies the data to time-stamp.</param>
        /// <param name="digestType">Specifies the hash algorithm to be used to compute digest from data.</param>
        /// <param name="credentials">User's credentials to access TSA service.</param>
        /// <returns>Time-stamp token</returns>
        public static TimeStampToken RequestTimeStampToken(string tsaUri, byte[] dataToTimestamp, Oid digestType, UserCredentials credentials)
        {
            if (null == dataToTimestamp)
            {
                throw new ArgumentNullException("dataToTimestamp");
            }

            using (MemoryStream ms = new MemoryStream(dataToTimestamp))
            {
                return(RequestTimeStampToken(tsaUri, ms, digestType, credentials));
            }
        }
예제 #7
0
        /// <summary>
        /// Requests time stamp for the data from stream.
        /// </summary>
        /// <param name="tsaUri">URL of a TSA service.</param>
        /// <param name="dataToTimestamp">Specifies the data to time-stamp.</param>
        /// <param name="digestType">Specifies the hash algorithm to be used to compute digest from data.</param>
        /// <param name="credentials">User's credentials to access TSA service.</param>
        /// <returns>Time-stamp token</returns>
        public static TimeStampToken RequestTimeStampToken(string tsaUri, Stream dataToTimestamp, Oid digestType, UserCredentials credentials)
        {
            if (null == tsaUri)
            {
                throw new ArgumentNullException("tsaUri");
            }

            if (null == dataToTimestamp)
            {
                throw new ArgumentNullException("dataToTimestamp");
            }

            if (null == digestType)
            {
                digestType = Oid.SHA512;
            }

            byte[]  digest  = DigestUtils.ComputeDigest(dataToTimestamp, digestType);
            Request request = new Request(digest, digestType.OID);

            return(RequestTST(tsaUri, request, credentials));
        }