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

Tcl Examples

Web API Categories

ASN.1
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
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 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
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

 

 

 

(Tcl) Validate CAdES-T Signature (.p7m)

Validates a CAdES-T CMS signature and extracts the time-stamp token and gets information about it. Also validates the time-stamp token.

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

Chilkat Tcl Extension Downloads

Chilkat Tcl Extension Downloads

load ./chilkat.dll

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

set crypt [new_CkCrypt2]

# Indicate that the CAdES-T timestamp tokens must also pass validation for the signature to be validated.
set cmsOptions [new_CkJsonObject]

CkJsonObject_UpdateBool $cmsOptions "ValidateTimestampTokens" 1
CkCrypt2_put_CmsOptions $crypt [CkJsonObject_emit $cmsOptions]

# Validate the .p7m and extract the original signed data to an output file.
# Note: The timestampToken is an unauthenticated attribute.  See the code below that parses the LastJsonData
# for details about examining timestampToken.
set success [CkCrypt2_VerifyP7M $crypt "qa_data/cades/CAdES-T/Signature-C-T-1.p7m" "qa_output/out.dat"]

# Get information about the CMS signature in the LastJsonData.
# The detailed results of the signature validation are available in LastJsonData.
# (If the non-success return status was caused by an error such as "file not found", then the
# LastJsonData would be empty.)
# json is a CkJsonObject
set json [CkCrypt2_LastJsonData $crypt]
CkJsonObject_put_EmitCompact $json 0
puts [CkJsonObject_emit $json]

# Here is a sample result:
# See the parsing code below..

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

# {
#   "pkcs7": {
#     "verify": {
#       "digestAlgorithms": [
#         "sha256"
#       ],
#       "signerInfo": [
#         {
#           "cert": {
#             "serialNumber": "00DCB814678CDB",
#             "issuerCN": "LevelBCAOK",
#             "issuerDN": "",
#             "digestAlgOid": "2.16.840.1.101.3.4.2.1",
#             "digestAlgName": "SHA256"
#           },
#           "contentType": "1.2.840.113549.1.7.1",
#           "signingTime": "131203065741Z",
#           "messageDigest": "JJZt41Nt8VsYahP+Xti4rR3vBDkUfRd6gquItl6R5Os=",
#           "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.7.1"
#             },
#             "1.2.840.113549.1.9.5": {
#               "name": "signingTime",
#               "utctime": "131203065741Z"
#             },
#             "1.2.840.113549.1.9.4": {
#               "name": "messageDigest",
#               "digest": "JJZt41Nt8VsYahP+Xti4rR3vBDkUfRd6gquItl6R5Os="
#             },
#             "1.2.840.113549.1.9.16.2.47": {
#               "name": "signingCertificateV2",
#               "der": "MIGIMIGFMIGCBCBJrxOU0w0dWGsVovjLv9QDH3syB5mLVv3grSYA40x9IDBeMFOkUTBPMQswCQYDVQQGEwJGUjENMAsGA1UEChMERVRTSTEcMBoGA1UECwwTUGx1Z3Rlc3RzXzIwMTMtMjAxNDETMBEGA1UEAxMKTGV2ZWxCQ0FPSwIHANy4FGeM2w=="
#             }
#           },
#           "unauthAttr": {
#             "1.2.840.113549.1.9.16.2.14": {
#               "name": "timestampToken",
#               "der": "MIIL+AYJKoZI...u7CfcjURNTY=",
#               "verify": {
#                 "digestAlgorithms": [
#                   "sha256"
#                 ],
#                 "signerInfo": [
#                   {
#                     "cert": {
#                       "serialNumber": "01AA4592D36C61",
#                       "issuerCN": "RootCAOK",
#                       "issuerDN": "",
#                       "digestAlgOid": "2.16.840.1.101.3.4.2.1",
#                       "digestAlgName": "SHA256"
#                     },
#                     "contentType": "1.2.840.113549.1.9.16.1.4",
#                     "messageDigest": "NSsMUrfoyCQ0OszPE1YLx1j3EyyCiBmnE5Sua6ghu/Q=",
#                     "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.4": {
#                         "name": "messageDigest",
#                         "digest": "NSsMUrfoyCQ0OszPE1YLx1j3EyyCiBmnE5Sua6ghu/Q="
#                       },
#                       "1.2.840.113549.1.9.16.2.47": {
#                         "name": "signingCertificateV2",
#                         "der": "MIGGMIGDMIGABCDB/np5UxvhcPnSxD2Kme+C88uXGCMWLAvFPHNvTApTWDBcMFGkTzBNMQswCQYDVQQGEwJGUjENMAsGA1UEChMERVRTSTEcMBoGA1UECwwTUGx1Z3Rlc3RzXzIwMTMtMjAxNDERMA8GA1UEAxMIUm9vdENBT0sCBwGqRZLTbGE="
#                       }
#                     }
#                   }
#                 ]
#               },
#               "timestampSignatureVerified": true,
#               "tstInfo": {
#                 "tsaPolicyId": "1.3.6.1.4.1.2706.2.2.5.2.1.1.1",
#                 "messageImprint": {
#                   "hashAlg": "sha256",
#                   "digest": "C8xEe9NA4X1cUyHGX9zG89ipmQ2byFs3aa+Xe4Fz2P0=",
#                   "digestMatches": true
#                 },
#                 "serialNumber": "313E162121D922",
#                 "genTime": "20131203065742Z"
#               }
#             }
#           }
#         }
#       ]
#     }
#   }
# }
# 

set signingTime [new_CkDtObj]

set authAttrSigningTimeUtctime [new_CkDtObj]

set unauthAttrTimestampTokenTstInfoGenTime [new_CkDtObj]

# Iterate over the hash algorithms used in the signature.
set i 0
set count_i [CkJsonObject_SizeOfArray $json "pkcs7.verify.digestAlgorithms"]
while {[expr $i < $count_i]} {
    CkJsonObject_put_I $json $i
    set strVal [CkJsonObject_stringOf $json "pkcs7.verify.digestAlgorithms[i]"]
    set i [expr $i + 1]
}

# For each signer...
set i 0
set count_i [CkJsonObject_SizeOfArray $json "pkcs7.verify.signerInfo"]
while {[expr $i < $count_i]} {
    CkJsonObject_put_I $json $i

    # Get information about the certificate used by this signer.
    set certSerialNumber [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].cert.serialNumber"]
    set certIssuerCN [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].cert.issuerCN"]
    set certIssuerDN [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].cert.issuerDN"]
    set certDigestAlgOid [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].cert.digestAlgOid"]
    set certDigestAlgName [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].cert.digestAlgName"]

    # Get additional information for this signer, such as the signingTime, signature algorithm, etc.
    set contentType [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].contentType"]
    [CkJsonObject_DtOf $json "pkcs7.verify.signerInfo[i].signingTime" 0 $signingTime]
    set messageDigest [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].messageDigest"]
    set signingAlgOid [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].signingAlgOid"]
    set signingAlgName [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].signingAlgName"]

    # --------------------------------
    # Examine authenticated attributes.
    # --------------------------------

    # contentType
    if {[expr [CkJsonObject_HasMember $json "pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.3\""] == 1]} then {
        set authAttrContentTypeName [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.3\".name"]
        set authAttrContentTypeOid [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.3\".oid"]
    }

    # signingTime
    if {[expr [CkJsonObject_HasMember $json "pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.5\""] == 1]} then {
        set authAttrSigningTimeName [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.5\".name"]
        [CkJsonObject_DtOf $json "pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.5\".utctime" 0 $authAttrSigningTimeUtctime]
    }

    # messageDigest
    if {[expr [CkJsonObject_HasMember $json "pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.4\""] == 1]} then {
        set authAttrMessageDigestName [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.4\".name"]
        set authAttrMessageDigestDigest [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.4\".digest"]
    }

    # signingCertificateV2
    if {[expr [CkJsonObject_HasMember $json "pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.16.2.47\""] == 1]} then {
        set authAttrSigningCertificateV2Name [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.16.2.47\".name"]
        set authAttrSigningCertificateV2Der [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.16.2.47\".der"]
    }

    # --------------------------------
    # Examine unauthenticated attributes.
    # --------------------------------

    # timestampToken  (the timestampToken is what makes this signature a CAdES-T)
    if {[expr [CkJsonObject_HasMember $json "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\""] == 1]} then {

        set unauthAttrTimestampTokenName [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".name"]
        set unauthAttrTimestampTokenDer [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".der"]

        # This is where we find out if the timestampToken's signature is valid.
        set unauthAttrTimestampTokenTimestampSignatureVerified [CkJsonObject_BoolOf $json "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".timestampSignatureVerified"]

        set unauthAttrTimestampTokenTstInfoTsaPolicyId [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".tstInfo.tsaPolicyId"]
        set unauthAttrTimestampTokenTstInfoMessageImprintHashAlg [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".tstInfo.messageImprint.hashAlg"]
        set unauthAttrTimestampTokenTstInfoMessageImprintDigest [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".tstInfo.messageImprint.digest"]

        # Here is where we check to see if the digest in the timestampToken's messageImprint matches the digest of the signature of this signerInfo
        set unauthAttrTimestampTokenTstInfoMessageImprintDigestMatches [CkJsonObject_BoolOf $json "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".tstInfo.messageImprint.digestMatches"]

        set unauthAttrTimestampTokenTstInfoSerialNumber [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".tstInfo.serialNumber"]

        # Here is where we get the date/time of the timestampToken (i.e. when it was timestamped)
        [CkJsonObject_DtOf $json "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".tstInfo.genTime" 0 $unauthAttrTimestampTokenTstInfoGenTime]

        # The following code gets details about the validity of the timestampToken's signature...
        set j 0
        set count_j [CkJsonObject_SizeOfArray $json "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.digestAlgorithms"]
        while {[expr $j < $count_j]} {
            CkJsonObject_put_J $json $j
            set strVal [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.digestAlgorithms[j]"]
            set j [expr $j + 1]
        }
        set j 0
        set count_j [CkJsonObject_SizeOfArray $json "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo"]
        while {[expr $j < $count_j]} {
            CkJsonObject_put_J $json $j
            set certSerialNumber [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].cert.serialNumber"]
            set certIssuerCN [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].cert.issuerCN"]
            set certIssuerDN [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].cert.issuerDN"]
            set certDigestAlgOid [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].cert.digestAlgOid"]
            set certDigestAlgName [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].cert.digestAlgName"]
            set contentType [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].contentType"]
            set messageDigest [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].messageDigest"]
            set signingAlgOid [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].signingAlgOid"]
            set signingAlgName [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].signingAlgName"]
            set authAttrContentTypeName [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].authAttr.\"1.2.840.113549.1.9.3\".name"]
            set authAttrContentTypeOid [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].authAttr.\"1.2.840.113549.1.9.3\".oid"]
            set authAttrMessageDigestName [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].authAttr.\"1.2.840.113549.1.9.4\".name"]
            set authAttrMessageDigestDigest [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].authAttr.\"1.2.840.113549.1.9.4\".digest"]
            set authAttrSigningCertificateV2Name [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].authAttr.\"1.2.840.113549.1.9.16.2.47\".name"]
            set authAttrSigningCertificateV2Der [CkJsonObject_stringOf $json "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].authAttr.\"1.2.840.113549.1.9.16.2.47\".der"]
            set j [expr $j + 1]
        }

    }

    set i [expr $i + 1]
}

if {[expr $success != 1]} then {
    puts [CkCrypt2_lastErrorText $crypt]
    puts "CAdES-T verification failed."
} else {
    puts "CAdES-T signature is valid."
}

delete_CkJsonObject $json


delete_CkCrypt2 $crypt
delete_CkJsonObject $cmsOptions
delete_CkDtObj $signingTime
delete_CkDtObj $authAttrSigningTimeUtctime
delete_CkDtObj $unauthAttrTimestampTokenTstInfoGenTime

 

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