Sample code for 30+ languages & platforms
PowerBuilder

RFC3161 Timestamp Client - Fetch from Timestamp Authority (TSA) and Verify

See more HTTP Examples

Sends an RFC 3161 timestamp request to a TSA (Timestamp Authority) server and validates the timestamp token response.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Crypt
string ls_Base64Hash
oleobject loo_Http
oleobject loo_RequestToken
string ls_OptionalPolicyOid
integer li_AddNonce
integer li_RequestTsaCert
string ls_TsaUrl
oleobject loo_Resp
oleobject loo_TimestampReply
oleobject loo_TsaCert
integer li_PkiStatus
oleobject loo_Json
oleobject loo_SigningTime
oleobject loo_AuthAttrSigningTimeUtctime
string ls_StrVal
string ls_CertSerialNumber
string ls_CertIssuerCN
string ls_CertDigestAlgOid
string ls_CertDigestAlgName
string ls_ContentType
string ls_MessageDigest
string ls_SigningAlgOid
string ls_SigningAlgName
string ls_AuthAttrContentTypeName
string ls_AuthAttrContentTypeOid
string ls_AuthAttrSigningTimeName
string ls_AuthAttrSigningCertificateName
string ls_AuthAttrSigningCertificateDer
string ls_AuthAttrMessageDigestName
string ls_AuthAttrMessageDigestDigest
integer li_TimestampReplyPkiStatusValue
string ls_TimestampReplyPkiStatusMeaning
integer i
integer li_Count_i

li_Success = 0

// 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"

loo_Crypt = create oleobject
li_rc = loo_Crypt.ConnectToNewObject("Chilkat.Crypt2")
if li_rc < 0 then
    destroy loo_Crypt
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Crypt.HashAlgorithm = "sha256"
loo_Crypt.EncodingMode = "base64"
ls_Base64Hash = loo_Crypt.HashStringENC("Hello World")

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")

loo_RequestToken = create oleobject
li_rc = loo_RequestToken.ConnectToNewObject("Chilkat.BinData")

ls_OptionalPolicyOid = ""
li_AddNonce = 0
li_RequestTsaCert = 1

// Create a time-stamp request token
li_Success = loo_Http.CreateTimestampRequest("sha256",ls_Base64Hash,ls_OptionalPolicyOid,li_AddNonce,li_RequestTsaCert,loo_RequestToken)
if li_Success = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Crypt
    destroy loo_Http
    destroy loo_RequestToken
    return
end if

// 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' https://freetsa.org/tsr > file.tsr
ls_TsaUrl = "https://freetsa.org/tsr"
// Another timestamp server you could try is: http://timestamp.digicert.com
ls_TsaUrl = "http://timestamp.digicert.com"
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

li_Success = loo_Http.HttpBd("POST",ls_TsaUrl,loo_RequestToken,"application/timestamp-query",loo_Resp)
if li_Success = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Crypt
    destroy loo_Http
    destroy loo_RequestToken
    destroy loo_Resp
    return
end if

// Get the timestamp reply from the HTTP response object.
loo_TimestampReply = create oleobject
li_rc = loo_TimestampReply.ConnectToNewObject("Chilkat.BinData")

loo_Resp.GetBodyBd(loo_TimestampReply)

// Show the base64 encoded timestamp reply.
Write-Debug loo_TimestampReply.GetEncoded("base64")

// Let's verify the timestamp reply against the TSA's cert, which we've previously downloaded.
// See https://freetsa.org/index_en.php
loo_TsaCert = create oleobject
li_rc = loo_TsaCert.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_TsaCert.LoadFromFile("qa_data/certs/freetsa.org.cer")
if li_Success = 0 then
    Write-Debug loo_TsaCert.LastErrorText
    destroy loo_Crypt
    destroy loo_Http
    destroy loo_RequestToken
    destroy loo_Resp
    destroy loo_TimestampReply
    destroy loo_TsaCert
    return
end if

// The VerifyTimestampReply method will return one of the following values:
// -1:  The timestampReply does not contain a valid timestamp reply.
// -2: The  timestampReply is a valid timestamp reply, but failed verification using the public key of the tsaCert.
// 0:  Granted and verified.
// 1: Granted and verified, with mods (see RFC 3161)
// 2: Rejected.
// 3: Waiting.
// 4: Revocation Warning
// 5: Revocation Notification
li_PkiStatus = loo_Http.VerifyTimestampReply(loo_TimestampReply,loo_TsaCert)
if li_PkiStatus < 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Crypt
    destroy loo_Http
    destroy loo_RequestToken
    destroy loo_Resp
    destroy loo_TimestampReply
    destroy loo_TsaCert
    return
end if

Write-Debug "pkiStatus = " + string(li_PkiStatus)

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Http.GetLastJsonData(loo_Json)

loo_Json.EmitCompact = 0
Write-Debug loo_Json.Emit()

// The JSON looks like the following.

// Use this online tool to generate parsing code from sample JSON: 
// Generate Parsing Code from JSON

// {
//   "timestampReply": {
//     "pkiStatus": {
//       "value": 0,
//       "meaning": "granted"
//     }
//   },
//   "pkcs7": {
//     "verify": {
//       "digestAlgorithms": [
//         "sha256"
//       ],
//       "signerInfo": [
//         {
//           "cert": {
//             "serialNumber": "04CD3F8568AE76C61BB0FE7160CCA76D",
//             "issuerCN": "DigiCert SHA2 Assured ID Timestamping CA",
//             "digestAlgOid": "2.16.840.1.101.3.4.2.1",
//             "digestAlgName": "SHA256"
//           },
//           "contentType": "1.2.840.113549.1.9.16.1.4",
//           "signingTime": "200405023019Z",
//           "messageDigest": "f14zOsdnN9vyyV3HjjBiLzNDi1PF28hAFMODxNkNRZs=",
//           "signingAlgOid": "1.2.840.113549.1.1.1",
//           "signingAlgName": "RSA-PKCSV-1_5",
//           "authAttr": {
//             "1.2.840.113549.1.9.3": {
//               "name": "contentType",
//               "oid": "1.2.840.113549.1.9.16.1.4"
//             },
//             "1.2.840.113549.1.9.5": {
//               "name": "signingTime",
//               "utctime": "200405023019Z"
//             },
//             "1.2.840.113549.1.9.16.2.12": {
//               "name": "signingCertificate",
//               "der": "MBowGDAWBBQDJb1QXtqWMC3CL0+gHkwovig0xQ=="
//             },
//             "1.2.840.113549.1.9.4": {
//               "name": "messageDigest",
//               "digest": "f14zOsdnN9vyyV3HjjBiLzNDi1PF28hAFMODxNkNRZs="
//             }
//           }
//         }
//       ]
//     }
//   }
// }

loo_SigningTime = create oleobject
li_rc = loo_SigningTime.ConnectToNewObject("Chilkat.DtObj")

loo_AuthAttrSigningTimeUtctime = create oleobject
li_rc = loo_AuthAttrSigningTimeUtctime.ConnectToNewObject("Chilkat.DtObj")

li_TimestampReplyPkiStatusValue = loo_Json.IntOf("timestampReply.pkiStatus.value")
ls_TimestampReplyPkiStatusMeaning = loo_Json.StringOf("timestampReply.pkiStatus.meaning")
i = 0
li_Count_i = loo_Json.SizeOfArray("pkcs7.verify.digestAlgorithms")
do while i < li_Count_i
    loo_Json.I = i
    ls_StrVal = loo_Json.StringOf("pkcs7.verify.digestAlgorithms[i]")
    i = i + 1
loop
i = 0
li_Count_i = loo_Json.SizeOfArray("pkcs7.verify.signerInfo")
do while i < li_Count_i
    loo_Json.I = i
    ls_CertSerialNumber = loo_Json.StringOf("pkcs7.verify.signerInfo[i].cert.serialNumber")
    ls_CertIssuerCN = loo_Json.StringOf("pkcs7.verify.signerInfo[i].cert.issuerCN")
    ls_CertDigestAlgOid = loo_Json.StringOf("pkcs7.verify.signerInfo[i].cert.digestAlgOid")
    ls_CertDigestAlgName = loo_Json.StringOf("pkcs7.verify.signerInfo[i].cert.digestAlgName")
    ls_ContentType = loo_Json.StringOf("pkcs7.verify.signerInfo[i].contentType")
    loo_Json.DtOf("pkcs7.verify.signerInfo[i].signingTime",0,loo_SigningTime)
    ls_MessageDigest = loo_Json.StringOf("pkcs7.verify.signerInfo[i].messageDigest")
    ls_SigningAlgOid = loo_Json.StringOf("pkcs7.verify.signerInfo[i].signingAlgOid")
    ls_SigningAlgName = loo_Json.StringOf("pkcs7.verify.signerInfo[i].signingAlgName")
    ls_AuthAttrContentTypeName = loo_Json.StringOf("pkcs7.verify.signerInfo[i].authAttr.~"1.2.840.113549.1.9.3~".name")
    ls_AuthAttrContentTypeOid = loo_Json.StringOf("pkcs7.verify.signerInfo[i].authAttr.~"1.2.840.113549.1.9.3~".oid")
    ls_AuthAttrSigningTimeName = loo_Json.StringOf("pkcs7.verify.signerInfo[i].authAttr.~"1.2.840.113549.1.9.5~".name")
    loo_Json.DtOf("pkcs7.verify.signerInfo[i].authAttr.~"1.2.840.113549.1.9.5~".utctime",0,loo_AuthAttrSigningTimeUtctime)
    ls_AuthAttrSigningCertificateName = loo_Json.StringOf("pkcs7.verify.signerInfo[i].authAttr.~"1.2.840.113549.1.9.16.2.12~".name")
    ls_AuthAttrSigningCertificateDer = loo_Json.StringOf("pkcs7.verify.signerInfo[i].authAttr.~"1.2.840.113549.1.9.16.2.12~".der")
    ls_AuthAttrMessageDigestName = loo_Json.StringOf("pkcs7.verify.signerInfo[i].authAttr.~"1.2.840.113549.1.9.4~".name")
    ls_AuthAttrMessageDigestDigest = loo_Json.StringOf("pkcs7.verify.signerInfo[i].authAttr.~"1.2.840.113549.1.9.4~".digest")
    i = i + 1
loop


destroy loo_Crypt
destroy loo_Http
destroy loo_RequestToken
destroy loo_Resp
destroy loo_TimestampReply
destroy loo_TsaCert
destroy loo_Json
destroy loo_SigningTime
destroy loo_AuthAttrSigningTimeUtctime