Typically when creating a timestamp, you send a hash of the data you want timestamped (and the hash algorithm used to generate it). So, you create your signature (which may involve hashing your data), and then you hash the signature itself, and send that to the TSA. The TSA will return this hash+algorithm (the "messageImprint"), along with the time and its own signature, in the response token. Because the TSA's response (the "TimeStampToken") contains a hash of the original data (your signature), plus a timestamp, and is signed by the TSA, you can check the validity of the TSA's signature and if it's valid (and you trust the TSA), be sure that your signature existed at the specified timestamp. You can then (or first, if you want) verify that your CA's signature was valid at that time.
More specifically, the response token is "SignedData" in CMS (Cryptographic Message Syntax). The signature on the CMS is generated by the TSA. The content of the CMS includes the "messageImprint" that you supplied (the hash of your original signature, plus the algorithm used to hash it), plus the "genTime" when the timestamp was generated.
To verify a timestamp, you obtain the TSA's public key, and use the public key to verify the signature on the CMS. Assuming that checks out, you parse the CMS and extract the "messageImprint" from its content. You use the hash function specified in the "messageImprint" to hash the data (original signature that your tool generated), and see if it matches the hash in the messageImprint. If it does, then you know the timestamp was generated from that original data (your program's signature). Then you can check the "genTime" to see when the timestamp was created. Once you know the TSA's signature is valid, and that the signed data is your CA's signature, you can verify the CA's signature using its public key, and check that the timestamp from the TSA is during the CA's certificate's validity period (e.g. using the
-attime flag to
openssl -verify, or using the
-no_check_time flag plus manually verifying).
Note, of course, that all this relies on the TSA having a strong, well-protected private key that it maintains for many years. An attacker who compromises that private key could forge timestamps of arbitrary data for arbitrary points in time.