Sample code for 30+ languages & platforms
Swift

Extract TSTInfo from RFC3161 Timestamp Reply

See more HTTP Examples

Sends an RFC 3161 timestamp request to a TSA (Timestamp Authority) server and converts the timestamp reply to XML, and then extracts the TSTInfo from the XML and converts it to XML.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    // Note: Requires Chilkat v9.5.0.75 or greater.

    // This requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // First sha-256 hash the data that is to be timestamped.
    // In this example, the data is the string "Hello World"

    let crypt = CkoCrypt2()!
    crypt.hashAlgorithm = "sha256"
    crypt.encodingMode = "base64"
    var base64Hash: String? = crypt.hashFileENC(path: "qa_data/hamlet.xml")

    let http = CkoHttp()!

    let requestToken = CkoBinData()!
    var optionalPolicyOid: String? = ""
    var addNonce: Bool = false
    var requestTsaCert: Bool = true

    // Create a time-stamp request token
    success = http.createTimestampRequest(hashAlg: "sha256", hashVal: base64Hash, reqPolicyOid: optionalPolicyOid, addNonce: addNonce, reqTsaCert: requestTsaCert, timestampToken: requestToken)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    // Send the time-stamp request token to the TSA.
    // This is the equivalent of the following CURL command:
    // curl -H "Content-Type: application/timestamp-query" --data-binary '@file.tsq' http://timestamp.digicert.com > file.tsr
    var tsaUrl: String? = "http://timestamp.digicert.com"
    let resp = CkoHttpResponse()!
    success = http.httpBd(verb: "POST", url: tsaUrl, bd: requestToken, contentType: "application/timestamp-query", response: resp)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    // Get the timestamp reply from the HTTP response object.
    let timestampReply = CkoBinData()!
    resp.getBodyBd(binData: timestampReply)

    // Convert the binary timestamp reply to XML
    let asn = CkoAsn()!
    success = asn.loadBd(bd: timestampReply)
    if success == false {
        print("\(asn.lastErrorText!)")
        return
    }

    let xml = CkoXml()!
    success = xml.load(xmlData: asn.asnToXml())

    // Extract the TSTInfo from the XML.
    // The TSTInfo is this base64 encoded ASN.1 

    // TSTInfo ::= SEQUENCE  {
    //    version                      INTEGER  { v1(1) },
    //    policy                       TSAPolicyId,
    //    messageImprint               MessageImprint,
    //      -- MUST have the same value as the similar field in
    //      -- TimeStampReq
    //    serialNumber                 INTEGER,
    //     -- Time-Stamping users MUST be ready to accommodate integers
    //     -- up to 160 bits.
    //    genTime                      GeneralizedTime,
    //    accuracy                     Accuracy                 OPTIONAL,
    //    ordering                     BOOLEAN             DEFAULT FALSE,
    //    nonce                        INTEGER                  OPTIONAL,
    //      -- MUST be present if the similar field was present
    //      -- in TimeStampReq.  In that case it MUST have the same value.
    //    tsa                          [0] GeneralName          OPTIONAL,
    //    extensions                   [1] IMPLICIT Extensions   OPTIONAL  }

    var tstInfoBase64: String? = xml.getChildContent(tagPath: "sequence[1]|contextSpecific|sequence|sequence|contextSpecific|octets")

    let bdTstInfo = CkoBinData()!
    bdTstInfo.appendEncoded(encData: tstInfoBase64, encoding: "base64")

    let asnTstInfo = CkoAsn()!
    success = asnTstInfo.loadBd(bd: bdTstInfo)
    if success == false {
        print("\(asnTstInfo.lastErrorText!)")
        return
    }

    let xmlTstInfo = CkoXml()!
    success = xmlTstInfo.load(xmlData: asnTstInfo.asnToXml())
    print("\(xmlTstInfo.getXml()!)")

    // Here's the TSTInfo XML:

    // <?xml version="1.0" encoding="utf-8"?>
    // <sequence>
    //     <int>01</int>
    //     <oid>2.16.840.1.114412.7.1</oid>
    //     <sequence>
    //         <sequence>
    //             <oid>2.16.840.1.101.3.4.2.1</oid>
    //             <null/>
    //         </sequence>
    //         <octets>4sRRyWOzC7EOic4fQ9+Op1pa10DbgoBGjBvkq09LZmE=</octets>
    //     </sequence>
    //     <int>00AD2C86E49872597B60F87D5C54BCFFAE</int>
    //     <universal tag="24" constructed="0">MjAyMzAzMTYxMTQ5NTJa</universal>
    // </sequence>

    //    The genTime (GeneralizedTime) is contained in the final "universal" XML element and is 
    //    in base64. It is the time at which the time-stamp token has been created by
    //    the TSA. After decoding from base64, it is:
    // 
    //    The syntax is: YYYYMMDDhhmmss[.s...]Z
    //    Example: 19990609001326.34352Z

    let sbGenTime = CkoStringBuilder()!
    sbGenTime.decodeAndAppend(value: xmlTstInfo.getChildContent(tagPath: "universal"), encoding: "base64", charset: "utf-8")
    print("\(sbGenTime.getAsString()!)")

    // Result:
    // 20230316115718Z

}