Sample code for 30+ languages & platforms
Swift

Combine Timestamp Reply and Content File into a TimeStampData

See more ASN.1 Examples

Demonstrates how to combine a timestamp reply (RFC 3161) with a data file (any type of file) to create a TimeStampData structure (RFC 5544).

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    // Also see this example:
    // Send a TimeStamp Request to a TimeStamp Authority and get the Response

    // Load a timestamp reply file
    let bdTsr = CkoBinData()!
    success = bdTsr.loadFile(path: "qa_data/tsd/sample.tsr")
    if success != true {
        print("Failed to load timestamp reply file.")
        return
    }

    // Load the tsr into an ASN.1 object.
    let asnTsr = CkoAsn()!
    success = asnTsr.loadEncoded(asnContent: bdTsr.getEncoded(encoding: "base64"), encoding: "base64")
    if success != true {
        print("\(asnTsr.lastErrorText!)")
        return
    }

    // Get the timestamp reply as XML.
    let xmlTsr = CkoXml()!
    xmlTsr.load(xmlData: asnTsr.asnToXml())

    // The timestamp reply XML begins like this:
    // We'll want to remove the 1st "sequence" subtree.

    //     <?xml version="1.0" encoding="utf-8"?>
    //     <sequence>
    //         <sequence>       <---- Remove this sub-tree.
    //             <int>00</int>
    //             <sequence>
    //                 <utf8>Operation Okay</utf8>
    //             </sequence>
    //         </sequence>
    //         <sequence>   
    //             <oid>1.2.840.113549.1.7.2</oid>
    //             <contextSpecific tag="0" constructed="1">
    //             ...

    // Remove the 1st sub-tree..
    xmlTsr.removeChild(byIndex: 0)

    // In this example, the data file we're combining with the timestamp into a timestampData is a .p7m.
    // However, it can be any type of file (text or binary), it doesn't matter..
    let bdContent = CkoBinData()!
    success = bdContent.loadFile(path: "qa_data/tsd/sample.p7m")
    if success != true {
        print("Failed to load content file.")
        return
    }

    // Begin building the TimeStampData.  
    // We'll build as XML and then convert to ASN.1
    let xml = CkoXml()!
    xml.tag = "sequence"
    xml.updateChildContent(tagPath: "oid", value: "1.2.840.113549.1.9.16.1.31")
    xml.updateAttrAt(tagPath: "contextSpecific", autoCreate: true, attrName: "tag", attrValue: "0")
    xml.updateAttrAt(tagPath: "contextSpecific", autoCreate: true, attrName: "constructed", attrValue: "1")
    xml.updateChildContent(tagPath: "contextSpecific|sequence|int", value: "01")
    xml.updateChildContent(tagPath: "contextSpecific|sequence|octets", value: bdContent.getEncoded(encoding: "base64"))
    xml.updateAttrAt(tagPath: "contextSpecific|sequence|contextSpecific", autoCreate: true, attrName: "tag", attrValue: "0")
    xml.updateAttrAt(tagPath: "contextSpecific|sequence|contextSpecific", autoCreate: true, attrName: "constructed", attrValue: "1")

    var xContext: CkoXml? = xml.getChild(withTag: "contextSpecific|sequence|contextSpecific")
    xContext!.addChildTree(tree: xmlTsr)
    xContext = nil

    // Convert the XML to ASN.1
    let tsd = CkoAsn()!
    tsd.loadXml(xmlStr: xml.getXml())

    // Write the timestamped data to a file.
    success = tsd.writeBinaryDer(path: "qa_output/sample.tsd")

    // Alternatively, get the tsd ASN.1 as base64..
    var tsdBase64: String? = tsd.getEncodedDer(encoding: "base64_mime")
    print("\(tsdBase64!)")

}