Sample code for 30+ languages & platforms
Delphi DLL Requires Chilkat v11.0.0+

Validate CAdES-T Signature (.p7m)

See more CAdES Examples

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

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
crypt: HCkCrypt2;
cmsOptions: HCkJsonObject;
json: HCkJsonObject;
i: Integer;
count_i: Integer;
strVal: PWideChar;
certSerialNumber: PWideChar;
certIssuerCN: PWideChar;
certIssuerDN: PWideChar;
certDigestAlgOid: PWideChar;
certDigestAlgName: PWideChar;
contentType: PWideChar;
signingTime: HCkDtObj;
messageDigest: PWideChar;
signingAlgOid: PWideChar;
signingAlgName: PWideChar;
authAttrContentTypeName: PWideChar;
authAttrContentTypeOid: PWideChar;
authAttrSigningTimeName: PWideChar;
authAttrSigningTimeUtctime: HCkDtObj;
authAttrMessageDigestName: PWideChar;
authAttrMessageDigestDigest: PWideChar;
authAttrSigningCertificateV2Name: PWideChar;
authAttrSigningCertificateV2Der: PWideChar;
unauthAttrTimestampTokenName: PWideChar;
unauthAttrTimestampTokenDer: PWideChar;
unauthAttrTimestampTokenTimestampSignatureVerified: Boolean;
unauthAttrTimestampTokenTstInfoTsaPolicyId: PWideChar;
unauthAttrTimestampTokenTstInfoMessageImprintHashAlg: PWideChar;
unauthAttrTimestampTokenTstInfoMessageImprintDigest: PWideChar;
unauthAttrTimestampTokenTstInfoMessageImprintDigestMatches: Boolean;
unauthAttrTimestampTokenTstInfoSerialNumber: PWideChar;
unauthAttrTimestampTokenTstInfoGenTime: HCkDtObj;
j: Integer;
count_j: Integer;

begin
success := False;

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

crypt := CkCrypt2_Create();

//  Indicate that the CAdES-T timestamp tokens must also pass validation for the signature to be validated.
cmsOptions := CkJsonObject_Create();
CkJsonObject_UpdateBool(cmsOptions,'ValidateTimestampTokens',True);
CkCrypt2_putCmsOptions(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 retrieves and parses the last JSON data.
//  for details about examining timestampToken.
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 last JSON data.
//  The detailed results of the signature validation are available in the last JSON data.
//  (If the non-success return status was caused by an error such as "file not found", then the
//  last JSON data would be empty.)
json := CkJsonObject_Create();
CkCrypt2_GetLastJsonData(crypt,json);
CkJsonObject_putEmitCompact(json,False);
Memo1.Lines.Add(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"
//                }
//              }
//            }
//          }
//        ]
//      }
//    }
//  }
//  

signingTime := CkDtObj_Create();

authAttrSigningTimeUtctime := CkDtObj_Create();

unauthAttrTimestampTokenTstInfoGenTime := CkDtObj_Create();

//  Iterate over the hash algorithms used in the signature.
i := 0;
count_i := CkJsonObject_SizeOfArray(json,'pkcs7.verify.digestAlgorithms');
while i < count_i do
  begin
    CkJsonObject_putI(json,i);
    strVal := CkJsonObject__stringOf(json,'pkcs7.verify.digestAlgorithms[i]');
    i := i + 1;
  end;

//  For each signer...
i := 0;
count_i := CkJsonObject_SizeOfArray(json,'pkcs7.verify.signerInfo');
while i < count_i do
  begin
    CkJsonObject_putI(json,i);

    //  Get information about the certificate used by this signer.
    certSerialNumber := CkJsonObject__stringOf(json,'pkcs7.verify.signerInfo[i].cert.serialNumber');
    certIssuerCN := CkJsonObject__stringOf(json,'pkcs7.verify.signerInfo[i].cert.issuerCN');
    certIssuerDN := CkJsonObject__stringOf(json,'pkcs7.verify.signerInfo[i].cert.issuerDN');
    certDigestAlgOid := CkJsonObject__stringOf(json,'pkcs7.verify.signerInfo[i].cert.digestAlgOid');
    certDigestAlgName := CkJsonObject__stringOf(json,'pkcs7.verify.signerInfo[i].cert.digestAlgName');

    //  Get additional information for this signer, such as the signingTime, signature algorithm, etc.
    contentType := CkJsonObject__stringOf(json,'pkcs7.verify.signerInfo[i].contentType');
    CkJsonObject_DtOf(json,'pkcs7.verify.signerInfo[i].signingTime',False,signingTime);
    messageDigest := CkJsonObject__stringOf(json,'pkcs7.verify.signerInfo[i].messageDigest');
    signingAlgOid := CkJsonObject__stringOf(json,'pkcs7.verify.signerInfo[i].signingAlgOid');
    signingAlgName := CkJsonObject__stringOf(json,'pkcs7.verify.signerInfo[i].signingAlgName');

    //  --------------------------------
    //  Examine authenticated attributes.
    //  --------------------------------

    //  contentType
    if (CkJsonObject_HasMember(json,'pkcs7.verify.signerInfo[i].authAttr."1.2.840.113549.1.9.3"') = True) then
      begin
        authAttrContentTypeName := CkJsonObject__stringOf(json,'pkcs7.verify.signerInfo[i].authAttr."1.2.840.113549.1.9.3".name');
        authAttrContentTypeOid := CkJsonObject__stringOf(json,'pkcs7.verify.signerInfo[i].authAttr."1.2.840.113549.1.9.3".oid');
      end;

    //  signingTime
    if (CkJsonObject_HasMember(json,'pkcs7.verify.signerInfo[i].authAttr."1.2.840.113549.1.9.5"') = True) then
      begin
        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',False,authAttrSigningTimeUtctime);
      end;

    //  messageDigest
    if (CkJsonObject_HasMember(json,'pkcs7.verify.signerInfo[i].authAttr."1.2.840.113549.1.9.4"') = True) then
      begin
        authAttrMessageDigestName := CkJsonObject__stringOf(json,'pkcs7.verify.signerInfo[i].authAttr."1.2.840.113549.1.9.4".name');
        authAttrMessageDigestDigest := CkJsonObject__stringOf(json,'pkcs7.verify.signerInfo[i].authAttr."1.2.840.113549.1.9.4".digest');
      end;

    //  signingCertificateV2
    if (CkJsonObject_HasMember(json,'pkcs7.verify.signerInfo[i].authAttr."1.2.840.113549.1.9.16.2.47"') = True) then
      begin
        authAttrSigningCertificateV2Name := CkJsonObject__stringOf(json,'pkcs7.verify.signerInfo[i].authAttr."1.2.840.113549.1.9.16.2.47".name');
        authAttrSigningCertificateV2Der := CkJsonObject__stringOf(json,'pkcs7.verify.signerInfo[i].authAttr."1.2.840.113549.1.9.16.2.47".der');
      end;

    //  --------------------------------
    //  Examine unauthenticated attributes.
    //  --------------------------------

    //  timestampToken  (the timestampToken is what makes this signature a CAdES-T)
    if (CkJsonObject_HasMember(json,'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14"') = True) then
      begin

        unauthAttrTimestampTokenName := CkJsonObject__stringOf(json,'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".name');
        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.
        unauthAttrTimestampTokenTimestampSignatureVerified := CkJsonObject_BoolOf(json,'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".timestampSignatureVerified');

        unauthAttrTimestampTokenTstInfoTsaPolicyId := CkJsonObject__stringOf(json,'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".tstInfo.tsaPolicyId');
        unauthAttrTimestampTokenTstInfoMessageImprintHashAlg := CkJsonObject__stringOf(json,'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".tstInfo.messageImprint.hashAlg');
        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
        unauthAttrTimestampTokenTstInfoMessageImprintDigestMatches := CkJsonObject_BoolOf(json,'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".tstInfo.messageImprint.digestMatches');

        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',False,unauthAttrTimestampTokenTstInfoGenTime);

        //  The following code gets details about the validity of the timestampToken's signature...
        j := 0;
        count_j := CkJsonObject_SizeOfArray(json,'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".verify.digestAlgorithms');
        while j < count_j do
          begin
            CkJsonObject_putJ(json,j);
            strVal := CkJsonObject__stringOf(json,'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".verify.digestAlgorithms[j]');
            j := j + 1;
          end;

        j := 0;
        count_j := CkJsonObject_SizeOfArray(json,'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".verify.signerInfo');
        while j < count_j do
          begin
            CkJsonObject_putJ(json,j);
            certSerialNumber := CkJsonObject__stringOf(json,'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".verify.signerInfo[j].cert.serialNumber');
            certIssuerCN := CkJsonObject__stringOf(json,'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".verify.signerInfo[j].cert.issuerCN');
            certIssuerDN := CkJsonObject__stringOf(json,'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".verify.signerInfo[j].cert.issuerDN');
            certDigestAlgOid := CkJsonObject__stringOf(json,'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".verify.signerInfo[j].cert.digestAlgOid');
            certDigestAlgName := CkJsonObject__stringOf(json,'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".verify.signerInfo[j].cert.digestAlgName');
            contentType := CkJsonObject__stringOf(json,'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".verify.signerInfo[j].contentType');
            messageDigest := CkJsonObject__stringOf(json,'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".verify.signerInfo[j].messageDigest');
            signingAlgOid := CkJsonObject__stringOf(json,'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".verify.signerInfo[j].signingAlgOid');
            signingAlgName := CkJsonObject__stringOf(json,'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".verify.signerInfo[j].signingAlgName');
            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');
            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');
            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');
            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');
            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');
            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');
            j := j + 1;
          end;

      end;

    i := i + 1;
  end;

if (success <> True) then
  begin
    Memo1.Lines.Add(CkCrypt2__lastErrorText(crypt));
    Memo1.Lines.Add('CAdES-T verification failed.');
  end
else
  begin
    Memo1.Lines.Add('CAdES-T signature is valid.');
  end;

CkCrypt2_Dispose(crypt);
CkJsonObject_Dispose(cmsOptions);
CkJsonObject_Dispose(json);
CkDtObj_Dispose(signingTime);
CkDtObj_Dispose(authAttrSigningTimeUtctime);
CkDtObj_Dispose(unauthAttrTimestampTokenTstInfoGenTime);