Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_BdTsr
oleobject loo_AsnTsr
oleobject loo_XmlTsr
oleobject loo_BdContent
oleobject loo_Xml
oleobject loo_XContext
oleobject loo_Tsd
string ls_TsdBase64

li_Success = 0

// 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
loo_BdTsr = create oleobject
li_rc = loo_BdTsr.ConnectToNewObject("Chilkat.BinData")
if li_rc < 0 then
    destroy loo_BdTsr
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_BdTsr.LoadFile("qa_data/tsd/sample.tsr")
if li_Success <> 1 then
    Write-Debug "Failed to load timestamp reply file."
    destroy loo_BdTsr
    return
end if

// Load the tsr into an ASN.1 object.
loo_AsnTsr = create oleobject
li_rc = loo_AsnTsr.ConnectToNewObject("Chilkat.Asn")

li_Success = loo_AsnTsr.LoadEncoded(loo_BdTsr.GetEncoded("base64"),"base64")
if li_Success <> 1 then
    Write-Debug loo_AsnTsr.LastErrorText
    destroy loo_BdTsr
    destroy loo_AsnTsr
    return
end if

// Get the timestamp reply as XML.
loo_XmlTsr = create oleobject
li_rc = loo_XmlTsr.ConnectToNewObject("Chilkat.Xml")

loo_XmlTsr.LoadXml(loo_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..
loo_XmlTsr.RemoveChildByIndex(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..
loo_BdContent = create oleobject
li_rc = loo_BdContent.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_BdContent.LoadFile("qa_data/tsd/sample.p7m")
if li_Success <> 1 then
    Write-Debug "Failed to load content file."
    destroy loo_BdTsr
    destroy loo_AsnTsr
    destroy loo_XmlTsr
    destroy loo_BdContent
    return
end if

// Begin building the TimeStampData.  
// We'll build as XML and then convert to ASN.1
loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")

loo_Xml.Tag = "sequence"
loo_Xml.UpdateChildContent("oid","1.2.840.113549.1.9.16.1.31")
loo_Xml.UpdateAttrAt("contextSpecific",1,"tag","0")
loo_Xml.UpdateAttrAt("contextSpecific",1,"constructed","1")
loo_Xml.UpdateChildContent("contextSpecific|sequence|int","01")
loo_Xml.UpdateChildContent("contextSpecific|sequence|octets",loo_BdContent.GetEncoded("base64"))
loo_Xml.UpdateAttrAt("contextSpecific|sequence|contextSpecific",1,"tag","0")
loo_Xml.UpdateAttrAt("contextSpecific|sequence|contextSpecific",1,"constructed","1")

loo_XContext = loo_Xml.GetChildWithTag("contextSpecific|sequence|contextSpecific")
loo_XContext.AddChildTree(loo_XmlTsr)
destroy loo_XContext

// Convert the XML to ASN.1
loo_Tsd = create oleobject
li_rc = loo_Tsd.ConnectToNewObject("Chilkat.Asn")

loo_Tsd.LoadAsnXml(loo_Xml.GetXml())

// Write the timestamped data to a file.
li_Success = loo_Tsd.WriteBinaryDer("qa_output/sample.tsd")

// Alternatively, get the tsd ASN.1 as base64..
ls_TsdBase64 = loo_Tsd.GetEncodedDer("base64_mime")
Write-Debug ls_TsdBase64


destroy loo_BdTsr
destroy loo_AsnTsr
destroy loo_XmlTsr
destroy loo_BdContent
destroy loo_Xml
destroy loo_Tsd