Search code examples
c#timestampbouncycastle

BouncyCastle TimeStampProtocol | How Do I get the original hash from a TimeStampToken?


I've created a TimeStampRequest from an hashed data and sent it to a tsa.

The TSA responded with a Granted response and I've got the byte array with the timestamp.

How do I get the original hashed data so I can validate that the TimeStamp sent by the TSA is the one I clain to have?

Thanks In advance.

Request

TimeStampRequestGenerator reqGen = new TimeStampRequestGenerator();
SHA1 sha1 = SHA1CryptoServiceProvider.Create();

ValidateInput(data);
reqGen.SetCertReq(true);

Hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(data));

TimeStampRequest request = reqGen.Generate(
                 TspAlgorithms.Sha1, Hash, BigInteger.ValueOf(100));

byte[] reqData = request.GetEncoded();

record.DtRequest = DateTime.Now;

HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(stampURI);
httpReq.Method = "POST";
httpReq.ContentType = "application/timestamp-query";
httpReq.ContentLength = reqData.Length;

// Write the request content
Stream reqStream = httpReq.GetRequestStream();
reqStream.Write(reqData, 0, reqData.Length);
reqStream.Close();

HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();

// Read the response
Stream respStream = new BufferedStream(httpResp.GetResponseStream());
TimeStampResponse response = new 

TimeStampResponse(respStream);
                    respStream.Close();
TimeStamp = response.TimeStampToken.GetEncoded();

Validation

var TSToken = new TimeStampToken(new CmsSignedData(TSPTimeStamp.DataContent));
//Here, I should reverse the TimeStampToken to the original hash

Solution

  • You can use this method to get the signed digest of the TimeStampToken

    byte[] digest = TSToken.TimeStampInfo.GetMessageImprintDigest();
    

    Then you can compare the value with the original Hash value