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) Sign MIME using RSASSA-PSS with SHA256

Demonstrates how to sign an MIME using the RSASSA-PSS signing algorithm with the SHA256 hash algorithm.

Note: This example requires Chilkat v9.5.0.67 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_CkMimeW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkPfxW.h>
#include <C_CkCertW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkMimeW mime;
    HCkMimeW part0;
    HCkMimeW part1;
    HCkStringBuilderW sbUnsignedMime;
    HCkPfxW pfx;
    HCkCertW cert;

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

    mime = CkMimeW_Create();

    // Create a MIME message to sign.
    CkMimeW_NewMultipartMixed(mime);
    CkMimeW_AddHeaderField(mime,L"someHeader1",L"Some value 1");
    CkMimeW_AddHeaderField(mime,L"someHeader2",L"Some value 2");

    part0 = CkMimeW_Create();
    CkMimeW_AddHeaderField(part0,L"hdrA",L"Some value A");
    CkMimeW_AddHeaderField(part0,L"hdrB",L"Some value B");
    CkMimeW_putContentType(part0,L"text/plain");
    CkMimeW_SetBody(part0,L"This is the plain-text body.");

    part1 = CkMimeW_Create();
    CkMimeW_AddHeaderField(part1,L"hdrX",L"Some value X");
    CkMimeW_AddHeaderField(part1,L"hdrY",L"Some value Y");
    CkMimeW_putContentType(part1,L"text/xml");
    CkMimeW_SetBody(part1,L"<a>This is the XML body</a>");

    CkMimeW_AppendPart(mime,part0);
    CkMimeW_AppendPart(mime,part1);

    // The MIME to be signed:
    sbUnsignedMime = CkStringBuilderW_Create();
    CkMimeW_GetMimeSb(mime,sbUnsignedMime);
    wprintf(L"%s\n",CkStringBuilderW_getAsString(sbUnsignedMime));

    // 	Content-Type: multipart/mixed; boundary="------------080303020600020604010008"
    // 	someHeader1: Some value 1
    // 	someHeader2: Some value 2
    // 
    // 	--------------080303020600020604010008
    // 	hdrA: Some value A
    // 	hdrB: Some value B
    // 	Content-Type: text/plain
    // 
    // 	This is the plain-text body.
    // 	--------------080303020600020604010008
    // 	hdrX: Some value X
    // 	hdrY: Some value Y
    // 	Content-Type: text/xml
    // 
    // 	<a>This is the XML body</a>
    // 	--------------080303020600020604010008--
    // 

    // Get a digital certificate with private key from a .pfx
    // (Chilkat has many different ways to provide a cert + private key for siging.
    // Using a PFX is just one possible option.)
    pfx = CkPfxW_Create();
    success = CkPfxW_LoadPfxFile(pfx,L"qa_data/rsassa-pss/privatekey.pfx",L"PFX_PASSWORD");
    if (success != TRUE) {
        wprintf(L"%s\n",CkPfxW_lastErrorText(pfx));
        CkMimeW_Dispose(mime);
        CkMimeW_Dispose(part0);
        CkMimeW_Dispose(part1);
        CkStringBuilderW_Dispose(sbUnsignedMime);
        CkPfxW_Dispose(pfx);
        return;
    }

    // Get the certificate to be used for signing.
    // (The typical case for a PFX is that it contains a cert with an associated private key,
    // as well as other certificates in the chain of authentication.  The cert with the private
    // key should be in the first position at index 0.)
    cert = CkPfxW_GetCert(pfx,0);
    if (CkPfxW_getLastMethodSuccess(pfx) != TRUE) {
        wprintf(L"%s\n",CkPfxW_lastErrorText(pfx));
        CkMimeW_Dispose(mime);
        CkMimeW_Dispose(part0);
        CkMimeW_Dispose(part1);
        CkStringBuilderW_Dispose(sbUnsignedMime);
        CkPfxW_Dispose(pfx);
        return;
    }

    // Indicate that RSASSA-PSS with SHA256 should be used.
    CkMimeW_putSigningAlg(mime,L"pss");
    CkMimeW_putSigningHashAlg(mime,L"sha256");

    // Add a detached signature.
    success = CkMimeW_AddDetachedSignature(mime,cert);
    if (success != TRUE) {
        wprintf(L"%s\n",CkMimeW_lastErrorText(mime));
        CkCertW_Dispose(cert);
        CkMimeW_Dispose(mime);
        CkMimeW_Dispose(part0);
        CkMimeW_Dispose(part1);
        CkStringBuilderW_Dispose(sbUnsignedMime);
        CkPfxW_Dispose(pfx);
        return;
    }

    // Show the clear-signed MIME. (see the sample output below)
    wprintf(L"-------------------------------------------------------------\n");
    wprintf(L"Clear-signed MIME using detached signature:\n");
    wprintf(L"-------------------------------------------------------------\n");
    wprintf(L"%s\n",CkMimeW_getMime(mime));

    // Restore the original unsigned MIME, and this time create an opaque signature.
    CkMimeW_LoadMime(mime,CkStringBuilderW_getAsString(sbUnsignedMime));

    // Sign using an opaque signature
    success = CkMimeW_ConvertToSigned(mime,cert);
    if (success != TRUE) {
        wprintf(L"%s\n",CkMimeW_lastErrorText(mime));
        CkCertW_Dispose(cert);
        CkMimeW_Dispose(mime);
        CkMimeW_Dispose(part0);
        CkMimeW_Dispose(part1);
        CkStringBuilderW_Dispose(sbUnsignedMime);
        CkPfxW_Dispose(pfx);
        return;
    }

    // Show the opaque-signed MIME. (see the sample output below)
    wprintf(L"-------------------------------------------------------------\n");
    wprintf(L"Opaque-signed MIME :\n");
    wprintf(L"-------------------------------------------------------------\n");
    wprintf(L"%s\n",CkMimeW_getMime(mime));

    CkCertW_Dispose(cert);
    wprintf(L"Success.\n");

    // -----------------------------------------------
    // This is sample output for the clear-signed MIME:
    // -----------------------------------------------
    // 
    // 	Content-Type: multipart/signed; boundary="----=_NextPart_672_c95a_97b64c9a.90b5204d";
    // 	 protocol="application/x-pkcs7-signature";
    // 	 micalg=sha256
    // 
    // 	------=_NextPart_672_c95a_97b64c9a.90b5204d
    // 	Content-Type: multipart/mixed; boundary="------------030207090102000301050504"
    // 	someHeader1: Some value 1
    // 	someHeader2: Some value 2
    // 
    // 	--------------030207090102000301050504
    // 	hdrA: Some value A
    // 	hdrB: Some value B
    // 	Content-Type: text/plain
    // 
    // 	This is the plain-text body.
    // 	--------------030207090102000301050504
    // 	hdrX: Some value X
    // 	hdrY: Some value Y
    // 	Content-Type: text/xml
    // 
    // 	<a>This is the XML body</a>
    // 	--------------030207090102000301050504--
    // 
    // 	------=_NextPart_672_c95a_97b64c9a.90b5204d
    // 	Content-Transfer-Encoding: base64
    // 	Content-Type: application/x-pkcs7-signature; name="smime.p7s"
    // 	Content-Disposition: attachment; filename="smime.p7s"
    // 
    // 	MIIG5wYJKoZIhvcNAQcCoIIG2DCCBtQCAQExDzANBglghkgBZQMEAgEFADALBgkqhkiG9w0BBwGg
    // 	ggL4MIIC9DCCAl2gAwIBAgIJAMPsJCT11cniMA0GCSqGSIb3DQEBCwUAMIGSMQswCQYDVQQGEwJB
    // 	VTERMA8GA1UECAwIVmljdG9yaWExEjAQBgNVBAcMCU1lbGJvdXJuZTEhMB8GA1UECgwYSW50ZXJu
    // 	ZXQgV2lkZ2l0cyBQdHkgTHRkMQ8wDQYDVQQDDAZXaWRnZXQxKDAmBgkqhkiG9w0BCQEWGWFkbWlu
    // 	QGludGVybmV0d2lkZ2V0cy5jb20wHhcNMTYxMTAxMTY1MjMyWhcNMjExMDMxMTY1MjMyWjCBkjEL
    // 	MAkGA1UEBhMCQVUxETAPBgNVBAgMCFZpY3RvcmlhMRIwEAYDVQQHDAlNZWxib3VybmUxITAfBgNV
    // 	BAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDEPMA0GA1UEAwwGV2lkZ2V0MSgwJgYJKoZIhvcN
    // 	AQkBFhlhZG1pbkBpbnRlcm5ldHdpZGdldHMuY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB
    // 	gQDGIdoCjyavs+F/Rm0VIB4m6O7VL1j+1IqieoR9NEX2GQvu2VCdceyxf9qaw1bxipEvjLwUkw7M
    // 	e+BTlLpWQbBMH87s6KpsC8MVyXhMLpP0oM8NFix/vLz2wdLhUh7CZvJA0plqkJk9bj57QIu+EO1k
    // 	tUHM2DFb6sckvCL2yybD1wIDAQABo1AwTjAdBgNVHQ4EFgQUONKKu2zsXIrinWxIGT654vrcQwsw
    // 	HwYDVR0jBBgwFoAUONKKu2zsXIrinWxIGT654vrcQwswDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0B
    // 	AQsFAAOBgQArFvdi5u9i2QF1Qw+cdC1l7w2Y3+q6RIkln2W8rWJFje00644o8hXy7v46giJ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EludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDEPMA0GA1UEAwwGV2lkZ2V0MSgwJgYJKoZIhvcNAQkB
    // 	FhlhZG1pbkBpbnRlcm5ldHdpZGdldHMuY29tAgkAw+wkJPXVyeIwPQYJKoZIhvcNAQEKMDCgDTAL
    // 	BglghkgBZQMEAgGhGjAYBgkqhkiG9w0BAQgwCwYJYIZIAWUDBAIBogMCASAEgYAvqIqKBzNXyVbh
    // 	TmaxdAc4d0ZTqWT6Na3C+DMO8owSKWy5FdGbJ2DLKm92qe/KLPTcj51vuwLUQb0Sw7r5Xknl+/6p
    // 	Eqn2qe/6hgDn06vvzNofVAweOtSVJsBO7/cYYPc+pXZZ2BcpZDdf/9Ci56Yrlk7A72BeqPe47J3F
    // 	neC8qw==
    // 
    // 	------=_NextPart_672_c95a_97b64c9a.90b5204d--
    // 

    // -----------------------------------------------
    // This is sample output for the opaque-signed MIME:
    // -----------------------------------------------
    // 	Content-Type: application/x-pkcs7-mime; name="smime.p7m"; smime-type="signed-data"
    // 	someHeader1: Some value 1
    // 	someHeader2: Some value 2
    // 	Content-Disposition: attachment; filename="smime.p7m"
    // 	Content-Transfer-Encoding: base64
    // 
    // 	MIIItAYJKoZIhvcNAQcCoIIIpTCCCKECAQExDzANBglghkgBZQMEAgEFADCCAdYGCSqGSIb3DQEH
    // 	AaCCAccEggHDQ29udGVudC1UeXBlOiBtdWx0aXBhcnQvbWl4ZWQ7IGJvdW5kYXJ5PSItLS0tLS0t
    // 	LS0tLS0wMDA2MDcwNzA3MDcwMTA1MDAwMTA5MDciDQpzb21lSGVhZGVyMTogU29tZSB2YWx1ZSAx
    // 	DQpzb21lSGVhZGVyMjogU29tZSB2YWx1ZSAyDQoNCi0tLS0tLS0tLS0tLS0tMDAwNjA3MDcwNzA3
    // 	MDEwNTAwMDEwOTA3DQpoZHJBOiBTb21lIHZhbHVlIEENCmhkckI6IFNvbWUgdmFsdWUgQg0KQ29u
    // 	dGVudC1UeXBlOiB0ZXh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wwGV2lkZ2V0MSgwJgYJKoZIhvcNAQkBFhlhZG1p
    // 	bkBpbnRlcm5ldHdpZGdldHMuY29tAgkAw+wkJPXVyeIwgbMGCyqGSIb3DQEJEAILMYGjoIGgMIGS
    // 	MQswCQYDVQQGEwJBVTERMA8GA1UECAwIVmljdG9yaWExEjAQBgNVBAcMCU1lbGJvdXJuZTEhMB8G
    // 	A1UECgwYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMQ8wDQYDVQQDDAZXaWRnZXQxKDAmBgkqhkiG
    // 	9w0BCQEWGWFkbWluQGludGVybmV0d2lkZ2V0cy5jb20CCQDD7CQk9dXJ4jA9BgkqhkiG9w0BAQow
    // 	MKANMAsGCWCGSAFlAwQCAaEaMBgGCSqGSIb3DQEBCDALBglghkgBZQMEAgGiAwIBIASBgILtktxt
    // 	tXWMWio7hLcvHUWk0Q3jP3rRdgNhcd/NdZsgDFP9/aq8/nHLLHSWfSh1PH+eZeKJwXxUH4Lwk6Sm
    // 	xFt97jyi3HaC3fezUSEqyg5WFG6fwOZGK4qF0WF+q29lvyNyV3iIQgDWxZQOcVL2fhpqUjoytkxj
    // 	/QTfrLOuzXKy
    // 


    CkMimeW_Dispose(mime);
    CkMimeW_Dispose(part0);
    CkMimeW_Dispose(part1);
    CkStringBuilderW_Dispose(sbUnsignedMime);
    CkPfxW_Dispose(pfx);

    }

 

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