RFC 3161 Client

1.0.6 · active · verified Fri Apr 17

A Python client library for interacting with RFC 3161 compliant Timestamping Authorities (TSAs). It enables users to request timestamps for data and verify existing RFC 3161 timestamp responses. The library is actively maintained, with regular releases addressing bug fixes, security enhancements, and feature improvements, currently at version 1.0.6.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to request an RFC 3161 timestamp for a message hash and then verify the received response. It highlights the basic flow: hashing data, sending a `TimestampRequest`, and using a `VerifierBuilder` to confirm the authenticity and integrity of the timestamp.

import os
import hashlib
from rfc3161_client import TimestampRequest, VerifierBuilder, HashAlgorithm
from rfc3161_client.exceptions import TSAResponseError, TimestampVerificationError

# Use a public TSA URL. For production, ensure this is a trusted service.
# Example: http://timestamp.digicert.com or a URL from your trusted provider.
TSA_URL = os.environ.get('RFC3161_TSA_URL', 'http://timestamp.digicert.com')

# 1. Prepare data to be timestamped
message = b"This is the data to be timestamped."
message_hash = hashlib.sha256(message).digest()

print(f"Attempting to timestamp data using TSA: {TSA_URL}")

# 2. Request a timestamp from the TSA
try:
    request = TimestampRequest(
        tsa_url=TSA_URL,
        hashed_message=message_hash,
        hash_algorithm=HashAlgorithm.SHA256,
    )
    timestamp_response = request.get_timestamp_response()
    print("Timestamp received successfully.")

    # 3. Verify the timestamp response
    # For robust verification, provide 'trusted_root_certs' of the TSA.
    # If not provided, the verifier attempts to build a chain from certs
    # embedded in the response or system CAs where possible (less secure).
    verifier = VerifierBuilder().build()
    is_valid = verifier.verify(
        timestamp_response=timestamp_response,
        hashed_message=message_hash,
        hash_algorithm=HashAlgorithm.SHA256,
        # trusted_root_certs=[b"-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----"],
    )

    if is_valid:
        print("Timestamp verification successful.")
    else:
        print("Timestamp verification FAILED.")

except TSAResponseError as e:
    print(f"Error from TSA: {e}")
except TimestampVerificationError as e:
    print(f"Timestamp verification error: {e}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →