Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(C) RSASSA-PSS Sign Binary DataSigns binary data to create a PKCS7/CMS signature. The signature algorithm is RSASSA-PSS with SHA256. Note: This example requires Chilkat v9.5.0.67 or greater.
#include <C_CkCrypt2.h> #include <C_CkPfx.h> #include <C_CkCert.h> #include <C_CkBinData.h> void ChilkatSample(void) { HCkCrypt2 crypt; HCkPfx pfx; BOOL success; HCkCert cert; HCkBinData binaryData; const char *pkcs7sig; // This example requires the Chilkat Crypt API to have been previously unlocked. // See Unlock Chilkat Crypt for sample code. crypt = CkCrypt2_Create(); // 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 = CkPfx_Create(); success = CkPfx_LoadPfxFile(pfx,"qa_data/rsassa-pss/privatekey.pfx","PFX_PASSWORD"); if (success != TRUE) { printf("%s\n",CkPfx_lastErrorText(pfx)); CkCrypt2_Dispose(crypt); CkPfx_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 = CkPfx_GetCert(pfx,0); if (CkPfx_getLastMethodSuccess(pfx) != TRUE) { printf("%s\n",CkPfx_lastErrorText(pfx)); CkCrypt2_Dispose(crypt); CkPfx_Dispose(pfx); return; } CkCrypt2_SetSigningCert(crypt,cert); // Indicate that RSASSA-PSS with SHA256 should be used. CkCrypt2_putSigningAlg(crypt,"pss"); CkCrypt2_putHashAlgorithm(crypt,"sha256"); CkCrypt2_putEncodingMode(crypt,"base64_mime"); // Load a binary file to be signed: binaryData = CkBinData_Create(); success = CkBinData_LoadFile(binaryData,"qa_data/jpg/starfish20.jpg"); if (success != TRUE) { printf("Failed to load file.\n"); CkCert_Dispose(cert); CkCrypt2_Dispose(crypt); CkPfx_Dispose(pfx); CkBinData_Dispose(binaryData); return; } // Sign the binary bytes to get a PKCS7 detached signature in base64 format: pkcs7sig = CkCrypt2_signBdENC(crypt,binaryData); printf("Detached PCKS7 Signature:\n"); printf("%s\n",pkcs7sig); // This signature looks like this: // MIIG5wYJKoZIhvcNAQcCoIIG2DCCBtQCAQExDzANBglghkgBZQMEAgEFADALBgkqhkiG9w0BBwGg // ggL4MIIC9DCCAl2gAwIBAgIJAMPsJCT11cniMA0GCSqGSIb3DQEBCwUAMIGSMQswCQYDVQQGEwJB // VTERMA8GA1UECAwIVmljdG9yaWExEjAQBgNVBAcMCU1lbGJvdXJuZTEhMB8GA1UECgwYSW50ZXJu // ZXQgV2lkZ2l0cyBQdHkgTHRkMQ8wDQYDVQQDDAZXaWRnZXQxKDAmBgkqhkiG9w0BCQEWGWFkbWlu // QGludGVybmV0d2lkZ2V0cy5jb20wHhcNMTYxMTAxMTY1MjMyWhcNMjExMDMxMTY1MjMyWjCBkjEL // MAkGA1UEBhMCQVUxETAPBgNVBAgMCFZpY3RvcmlhMRIwEAYDVQQHDAlNZWxib3VybmUxITAfBgNV // BAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDEPMA0GA1UEAwwGV2lkZ2V0MSgwJgYJKoZIhvcN // AQkBFhlhZG1pbkBpbnRlcm5ldHdpZGdldHMuY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB // gQDGIdoCjyavs+F/Rm0VIB4m6O7VL1j+1IqieoR9NEX2GQvu2VCdceyxf9qaw1bxipEvjLwUkw7M // e+BTlLpWQbBMH87s6KpsC8MVyXhMLpP0oM8NFix/vLz2wdLhUh7CZvJA0plqkJk9bj57QIu+EO1k // tUHM2DFb6sckvCL2yybD1wIDAQABo1AwTjAdBgNVHQ4EFgQUONKKu2z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iG9w0BCRACCzGBo6CBoDCBkjELMAkG // A1UEBhMCQVUxETAPBgNVBAgMCFZpY3RvcmlhMRIwEAYDVQQHDAlNZWxib3VybmUxITAfBgNVBAoM // GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDEPMA0GA1UEAwwGV2lkZ2V0MSgwJgYJKoZIhvcNAQkB // FhlhZG1pbkBpbnRlcm5ldHdpZGdldHMuY29tAgkAw+wkJPXVyeIwPQYJKoZIhvcNAQEKMDCgDTAL // BglghkgBZQMEAgGhGjAYBgkqhkiG9w0BAQgwCwYJYIZIAWUDBAIBogMCASAEgYCWV0g82volvnwf // YpwIpqpQzMmTPBKNQmFGjbyH2opdcbJwgu2qEFvaXkyjYDtgQ7XsCqc15dm6Ee1Ujkosbp57kLTt // /WbwxY1CC/uxs3oV+5ESUyB+2iocTYABYn4ye0FhBPut86n/gzZTL+RLG6Z1fxwwzkoxWUp7GjKK // 58mveQ== // The ASN.1 of the signature can be examined by browsing to https://lapo.it/asn1js/ , // then copy-and-paste the Base64 signature into the form and decode.. // The signature can be verified against the original data like this: success = CkCrypt2_VerifyBdENC(crypt,binaryData,pkcs7sig); printf("Signature verified: %d\n",success); // Now we'll create an opaque signature (the opposite of a detached signature). // An opaque signature is a PKCS7/CMS message that contains both the original data and // the signature. The verification process extracts the original data. // Then OpaqueSignBd method in-place signs the binaryData. // The contents of binaryData are replaced with the CMS/PKCS7 message. success = CkCrypt2_OpaqueSignBd(crypt,binaryData); // Show the contents of the opaque signature in base64 format: printf("Opaque Signature:\n"); printf("%s\n",CkBinData_getEncoded(binaryData,"base64_mime")); // MIIKCgYJKoZIhvcNAQcCoIIJ+zCCCfcCAQExDzANBglghkgBZQMEAgEFADCCAywGCSqGSIb3DQEH // AaCCAx0EggMZ/9j/4AAQSkZJRgABAQEASABIAAD//gAmRmlsZSB3cml0dGVuIGJ5IEFkb2JlIFBo // b3Rvc2hvcD8gNC4w/9sAQwAQCwwODAoQDg0OEhEQExgoGhgWFhgxIyUdK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KEFIbzfxD3gSZKJPWFgDa19DojarmwJMkSPXt9TQEzdjDPrbsCGLYOy29Puq // ZDI1rUcyxg7Y // The ASN.1 of the signature can be examined by browsing to https://lapo.it/asn1js/ , // then copy-and-paste the Base64 signature into the form and decode.. // The signature is verified, and the original data restored like this: success = CkCrypt2_OpaqueVerifyBd(crypt,binaryData); if (success != TRUE) { printf("Signature verification failed.\n"); printf("%s\n",CkCrypt2_lastErrorText(crypt)); CkCert_Dispose(cert); CkCrypt2_Dispose(crypt); CkPfx_Dispose(pfx); CkBinData_Dispose(binaryData); return; } // Save the extracted data to a file: success = CkBinData_WriteFile(binaryData,"qa_output/extractedStarfish20.jpg"); printf("Signature verified.\n"); CkCert_Dispose(cert); CkCrypt2_Dispose(crypt); CkPfx_Dispose(pfx); CkBinData_Dispose(binaryData); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.