Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

PowerBuilder Examples

Web API Categories

ASN.1
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip

HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

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

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

Note: This example requires Chilkat v9.5.0.75 or greater.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

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

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

loo_Crypt = create oleobject
li_rc = loo_Crypt.ConnectToNewObject("Chilkat_9_5_0.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_9_5_0.Http")

loo_RequestToken = create oleobject
li_rc = loo_RequestToken.ConnectToNewObject("Chilkat_9_5_0.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 <> 1 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 = loo_Http.PBinaryBd("POST",ls_TsaUrl,loo_RequestToken,"application/timestamp-query",0,0)
if loo_Http.LastMethodSuccess <> 1 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Crypt
    destroy loo_Http
    destroy loo_RequestToken
    return
end if

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

loo_Resp.GetBodyBd(loo_TimestampReply)
destroy loo_Resp

// 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_9_5_0.Cert")

li_Success = loo_TsaCert.LoadFromFile("qa_data/certs/freetsa.org.cer")
if li_Success <> 1 then
    Write-Debug loo_TsaCert.LastErrorText
    destroy loo_Crypt
    destroy loo_Http
    destroy loo_RequestToken
    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_TimestampReply
    destroy loo_TsaCert
    return
end if

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

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

// The LastJsonData looks like the following.
// Note: The "timestampReply.pkiStatus" portion of the LastJsonData was added in Chilkat v9.5.0.83

// 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_9_5_0.DtObj")

loo_AuthAttrSigningTimeUtctime = create oleobject
li_rc = loo_AuthAttrSigningTimeUtctime.ConnectToNewObject("Chilkat_9_5_0.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_Json


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

 

© 2000-2020 Chilkat Software, Inc. All Rights Reserved.