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

Unicode 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
Secrets
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

 

 

 

(Unicode C) Rewrite PFX using AES256-SHA256

Demonstrates how to load a .pfx/.p12, examine the encryption algorithm used, and rewrite using aes256-sha256.

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

Chilkat C/C++ Library Downloads

MS Visual C/C++

Linux/CentOS C/C++

Alpine Linux C/C++

MAC OS X C/C++

armhf/aarch64 C/C++

C++ Builder

iOS C/C++

Android C/C++

Solaris C/C++

MinGW C/C++

#include <C_CkPfxW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    HCkPfxW pfx;
    BOOL success;
    HCkJsonObjectW json;

    // This example requires Chilkat v9.5.0.83 or greater.

    pfx = CkPfxW_Create();

    // Let's load a .pfx and examine the encryption algorithms used to protect the private key:
    success = CkPfxW_LoadPfxFile(pfx,L"qa_data/pfx/test_secret.pfx",L"secret");
    if (success == FALSE) {
        wprintf(L"%s\n",CkPfxW_lastErrorText(pfx));
        CkPfxW_Dispose(pfx);
        return;
    }

    // Examine the algorithms:

    // "pbeWithSHAAnd3_KeyTripleDES_CBC" or "pbes2"?
    wprintf(L"Algorithm: %s\n",CkPfxW_algorithmId(pfx));

    // If the algorithm is "pbes2" then examine the actual encryption and HMAC algorithms used within pbes2.
    // (If the algorithm is NOT "pbes2", then the following properties are meaningless and will not be modified from their previous values prior to loading the PFX.)
    wprintf(L"Pbes2CryptAlg: %s\n",CkPfxW_pbes2CryptAlg(pfx));
    wprintf(L"Pbes2HmacAlg: %s\n",CkPfxW_pbes2HmacAlg(pfx));

    // Our output so far:

    // Algorithm: pbeWithSHAAnd3_KeyTripleDES_CBC
    // Pbes2CryptAlg: aes256-cbc
    // Pbes2HmacAlg: hmacWithSha256

    // This tells us that the PFX we loaded was protected using triple-DES with SHA1.
    // (Most existing .pfx/.p12 files use 3DES w/ SHA1.)
    // The Pbes2CryptAlg and Pbes2HmacAlg properties do not apply here because the AlgorithmId is not equal to "pbes2".  We can ignore those values.

    // Examine the LastJsonData for the call to LoadPfxFile.  This gives us information about what is contained in the PFX, including extended attributes.
    json = CkPfxW_LastJsonData(pfx);
    CkJsonObjectW_putEmitCompact(json,FALSE);
    wprintf(L"%s\n",CkJsonObjectW_emit(json));

    CkJsonObjectW_Dispose(json);

    // Here's a sample LastJsonData:

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

    // {
    //   "authenticatedSafe": {
    //     "contentInfo": [
    //       {
    //         "type": "Data",
    //         "safeBag": [
    //           {
    //             "type": "pkcs8ShroudedKeyBag",
    //             "attrs": {
    //               "localKeyId": "16444216",
    //               "keyContainerName": "{F09B755A-1E90-444D-9851-02B86CA14961}",
    //               "msStorageProvider": "Microsoft Enhanced Cryptographic Provider v1.0"
    //             }
    //           }
    //         ]
    //       },
    //       {
    //         "type": "EncryptedData",
    //         "safeBag": [
    //           {
    //             "type": "certBag",
    //             "attrs": {
    //               "localKeyId": "16444216"
    //             },
    //             "subject": "....",
    //             "serialNumber": "9999999999999999999999999999"
    //           },
    //           {
    //             "type": "certBag",
    //             "attrs": {
    //               "authRootSha256Hash": "0vkOXTXKxNQffUTOZq/4heGBX7M5GFhTqH5mwFyb7x4=",
    //               "friendlyName": "XYZ",
    //               "enhKeyUsage": [
    //                 {
    //                   "oid": "1.3.6.1.5.5.7.3.2",
    //                   "usage": "clientAuth"
    //                 },
    //                 {
    //                   "oid": "1.3.6.1.5.5.7.3.4",
    //                   "usage": "emailProtection"
    //                 },
    //                 {
    //                   "oid": "1.3.6.1.5.5.7.3.3",
    //                   "usage": "codeSigning"
    //                 },
    //                 {
    //                   "oid": "1.3.6.1.5.5.7.3.8",
    //                   "usage": "timeStamping"
    //                 },
    //                 {
    //                   "oid": "1.3.6.1.4.1.311.10.3.4",
    //                   "usage": "encryptedFileSystem"
    //                 },
    //                 {
    //                   "oid": "1.3.6.1.5.5.8.2.2",
    //                   "usage": "iKEIntermediate"
    //                 },
    // 
    //                 {
    //                   "oid": "1.3.6.1.5.5.7.3.6",
    //                   "usage": "ipsecTunnel"
    //                 },
    //                 {
    //                   "oid": "1.3.6.1.5.5.7.3.7",
    //                   "usage": "ipsecUser"
    //                 },
    //                 {
    //                   "oid": "1.3.6.1.5.5.7.3.5",
    //                   "usage": "ipsecEndSystem"
    //                 }
    //               ]
    //             },
    //             "subject": "...",
    //             "serialNumber": "8888888888888888888888888888"
    //           },
    //           {
    //             "type": "certBag",
    //             "subject": "...",
    //             "serialNumber": "777777777777777777777777777"
    //           }
    //         ]
    //       }
    //     ]
    //   }
    // }

    // ------------------------------------------------------------------------------------------
    // OK... now let's change the AlgorithmId to "pbes2" 

    CkPfxW_putAlgorithmId(pfx,L"pbes2");

    // We already know from above that the PBES2 crypt and HMAC algorithms are "aes256-cbc" and "hmacWithSha256".
    // Let's set them anyway just for the example...
    CkPfxW_putPbes2CryptAlg(pfx,L"aes256-cbc");
    CkPfxW_putPbes2HmacAlg(pfx,L"hmacWithSha256");

    // Rewrite the PFX using pbes2/aes256 + sha256
    success = CkPfxW_ToFile(pfx,L"secret",L"qa_output/test_secret_aes256.pfx");
    if (success == FALSE) {
        wprintf(L"%s\n",CkPfxW_lastErrorText(pfx));
        CkPfxW_Dispose(pfx);
        return;
    }

    wprintf(L"Success.\n");


    CkPfxW_Dispose(pfx);

    }

 

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