Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Objective-C Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
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
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Objective-C) 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 Objective-C Library Downloads

MAC OS X (Cocoa) Libs

iOS Libs

#import <CkoCrypt2.h>
#import <CkoJsonObject.h>
#import <NSString.h>
#import <CkoDtObj.h>

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

CkoCrypt2 *crypt = [[CkoCrypt2 alloc] init];
BOOL success;

// Indicate that the CAdES-T timestamp tokens must also pass validation for the signature to be validated.
CkoJsonObject *cmsOptions = [[CkoJsonObject alloc] init];
[cmsOptions UpdateBool: @"ValidateTimestampTokens" value: YES];
crypt.CmsOptions = [cmsOptions Emit];

// 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.
success = [crypt VerifyP7M: @"qa_data/cades/CAdES-T/Signature-C-T-1.p7m" destPath: @"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.)
CkoJsonObject *json = [crypt LastJsonData];
json.EmitCompact = NO;
NSLog(@"%@",[json Emit]);

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

int i;
int count_i;
NSString *strVal = 0;
NSString *certSerialNumber = 0;
NSString *certIssuerCN = 0;
NSString *certIssuerDN = 0;
NSString *certDigestAlgOid = 0;
NSString *certDigestAlgName = 0;
NSString *contentType = 0;
CkoDtObj *signingTime = [[CkoDtObj alloc] init];
NSString *messageDigest = 0;
NSString *signingAlgOid = 0;
NSString *signingAlgName = 0;
NSString *authAttrContentTypeName = 0;
NSString *authAttrContentTypeOid = 0;
NSString *authAttrSigningTimeName = 0;
CkoDtObj *authAttrSigningTimeUtctime = [[CkoDtObj alloc] init];
NSString *authAttrMessageDigestName = 0;
NSString *authAttrMessageDigestDigest = 0;
NSString *authAttrSigningCertificateV2Name = 0;
NSString *authAttrSigningCertificateV2Der = 0;
NSString *unauthAttrTimestampTokenName = 0;
NSString *unauthAttrTimestampTokenDer = 0;
BOOL unauthAttrTimestampTokenTimestampSignatureVerified;
NSString *unauthAttrTimestampTokenTstInfoTsaPolicyId = 0;
NSString *unauthAttrTimestampTokenTstInfoMessageImprintHashAlg = 0;
NSString *unauthAttrTimestampTokenTstInfoMessageImprintDigest = 0;
BOOL unauthAttrTimestampTokenTstInfoMessageImprintDigestMatches;
NSString *unauthAttrTimestampTokenTstInfoSerialNumber = 0;
CkoDtObj *unauthAttrTimestampTokenTstInfoGenTime = [[CkoDtObj alloc] init];
int j;
int count_j;

// Iterate over the hash algorithms used in the signature.
i = 0;
count_i = [[json SizeOfArray: @"pkcs7.verify.digestAlgorithms"] intValue];
while (i < count_i) {
    json.I = [NSNumber numberWithInt: i];
    strVal = [json StringOf: @"pkcs7.verify.digestAlgorithms[i]"];
    i = i + 1;
}

// For each signer...
i = 0;
count_i = [[json SizeOfArray: @"pkcs7.verify.signerInfo"] intValue];
while (i < count_i) {
    json.I = [NSNumber numberWithInt: i];

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

    // Get additional information for this signer, such as the signingTime, signature algorithm, etc.
    contentType = [json StringOf: @"pkcs7.verify.signerInfo[i].contentType"];
    [json DtOf: @"pkcs7.verify.signerInfo[i].signingTime" bLocal: NO dt: signingTime];
    messageDigest = [json StringOf: @"pkcs7.verify.signerInfo[i].messageDigest"];
    signingAlgOid = [json StringOf: @"pkcs7.verify.signerInfo[i].signingAlgOid"];
    signingAlgName = [json StringOf: @"pkcs7.verify.signerInfo[i].signingAlgName"];

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

    // contentType
    if ([json HasMember: @"pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.3\""] == YES) {
        authAttrContentTypeName = [json StringOf: @"pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.3\".name"];
        authAttrContentTypeOid = [json StringOf: @"pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.3\".oid"];
    }

    // signingTime
    if ([json HasMember: @"pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.5\""] == YES) {
        authAttrSigningTimeName = [json StringOf: @"pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.5\".name"];
        [json DtOf: @"pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.5\".utctime" bLocal: NO dt: authAttrSigningTimeUtctime];
    }

    // messageDigest
    if ([json HasMember: @"pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.4\""] == YES) {
        authAttrMessageDigestName = [json StringOf: @"pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.4\".name"];
        authAttrMessageDigestDigest = [json StringOf: @"pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.4\".digest"];
    }

    // signingCertificateV2
    if ([json HasMember: @"pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.16.2.47\""] == YES) {
        authAttrSigningCertificateV2Name = [json StringOf: @"pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.16.2.47\".name"];
        authAttrSigningCertificateV2Der = [json StringOf: @"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 ([json HasMember: @"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\""] == YES) {

        unauthAttrTimestampTokenName = [json StringOf: @"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".name"];
        unauthAttrTimestampTokenDer = [json StringOf: @"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 = [json BoolOf: @"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".timestampSignatureVerified"];

        unauthAttrTimestampTokenTstInfoTsaPolicyId = [json StringOf: @"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".tstInfo.tsaPolicyId"];
        unauthAttrTimestampTokenTstInfoMessageImprintHashAlg = [json StringOf: @"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".tstInfo.messageImprint.hashAlg"];
        unauthAttrTimestampTokenTstInfoMessageImprintDigest = [json StringOf: @"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 = [json BoolOf: @"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".tstInfo.messageImprint.digestMatches"];

        unauthAttrTimestampTokenTstInfoSerialNumber = [json StringOf: @"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)
        [json DtOf: @"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".tstInfo.genTime" bLocal: NO dt: unauthAttrTimestampTokenTstInfoGenTime];

        // The following code gets details about the validity of the timestampToken's signature...
        j = 0;
        count_j = [[json SizeOfArray: @"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.digestAlgorithms"] intValue];
        while (j < count_j) {
            json.J = [NSNumber numberWithInt: j];
            strVal = [json StringOf: @"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.digestAlgorithms[j]"];
            j = j + 1;
        }

        j = 0;
        count_j = [[json SizeOfArray: @"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo"] intValue];
        while (j < count_j) {
            json.J = [NSNumber numberWithInt: j];
            certSerialNumber = [json StringOf: @"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].cert.serialNumber"];
            certIssuerCN = [json StringOf: @"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].cert.issuerCN"];
            certIssuerDN = [json StringOf: @"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].cert.issuerDN"];
            certDigestAlgOid = [json StringOf: @"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].cert.digestAlgOid"];
            certDigestAlgName = [json StringOf: @"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].cert.digestAlgName"];
            contentType = [json StringOf: @"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].contentType"];
            messageDigest = [json StringOf: @"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].messageDigest"];
            signingAlgOid = [json StringOf: @"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].signingAlgOid"];
            signingAlgName = [json StringOf: @"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].signingAlgName"];
            authAttrContentTypeName = [json StringOf: @"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 = [json StringOf: @"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 = [json StringOf: @"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 = [json StringOf: @"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 = [json StringOf: @"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 = [json StringOf: @"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;
        }

    }

    i = i + 1;
}

if (success != YES) {
    NSLog(@"%@",crypt.LastErrorText);
    NSLog(@"%@",@"CAdES-T verification failed.");
}
else {
    NSLog(@"%@",@"CAdES-T signature is valid.");
}
 

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