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

SQL Server 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
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

 

 

 

(SQL Server) Create CMS (PKCS7) Detached MIME Signature

Demonstrates how to add a CMS detached signature to MIME.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @mime int
    -- Use "Chilkat_9_5_0.Mime" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.Mime', @mime OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    -- Load a certificate w/ private key for signing.
    DECLARE @cert int
    -- Use "Chilkat_9_5_0.Cert" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT

    DECLARE @success int
    EXEC sp_OAMethod @cert, 'LoadPfxFile', @success OUT, 'qa_data/pfx/ecc256_12345.p12', '12345'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END


    EXEC sp_OAGetProperty @cert, 'SubjectDN', @sTmp0 OUT
    PRINT 'Cert: ' + @sTmp0

    -- Create a simple MIME message to be signed:
    EXEC sp_OAMethod @mime, 'AddHeaderField', @success OUT, 'Subject', 'This is a test'
    EXEC sp_OAMethod @mime, 'AddHeaderField', @success OUT, 'MyCustomHeader', 'abc123'
    EXEC sp_OASetProperty @mime, 'ContentType', 'text/plain'
    EXEC sp_OAMethod @mime, 'SetBody', NULL, 'This is a plain-text body.'

    -- Examine the MIME prior to signing:

    PRINT '--- MIME prior to signing ---'
    EXEC sp_OAMethod @mime, 'GetMime', @sTmp0 OUT
    PRINT @sTmp0

    -- The MIME prior to signing looks like this:

    -- Subject: This is a test
    -- MyCustomHeader: abc123
    -- Content-Type: text/plain; format=flowed
    -- 
    -- This is a plain-text body.

    -- Use SHA256 as the hash algorithm in the CMS signature.
    EXEC sp_OASetProperty @mime, 'SigningHashAlg', 'SHA256'

    -- Add a detached signature:
    EXEC sp_OAMethod @mime, 'AddDetachedSignature', @success OUT, @cert
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    -- Examine the MIME with detached signature:

    PRINT '--- MIME with detached signature ---'
    EXEC sp_OAMethod @mime, 'GetMime', @sTmp0 OUT
    PRINT @sTmp0

    -- Content-Type: multipart/signed; boundary="----=_NextPart_6e3_c655_9641494d.b4f00501";
    --  protocol="application/x-pkcs7-signature";
    --  micalg=sha256
    -- 
    -- ------=_NextPart_6e3_c655_9641494d.b4f00501
    -- Subject: This is a test
    -- MyCustomHeader: abc123
    -- Content-Type: text/plain; format=flowed
    -- 
    -- This is a plain-text body.
    -- ------=_NextPart_6e3_c655_9641494d.b4f00501
    -- Content-Transfer-Encoding: base64
    -- Content-Type: application/x-pkcs7-signature; name="smime.p7s"
    -- Content-Disposition: attachment; filename="smime.p7s"
    -- 
    -- MIID7AYJKoZIhvcNAQcCoIID3TCCA9kCAQExDzANBglghkgBZQMEAgEFADALBgkqhkiG9w0BBwGg
    -- ggJxMIICbTCCAhOgAwIBAgIJAJuS2kgOoyr+MAkGByqGSM49BAEwWzELMAkGA1UEBhMCQVUxEzAR
    -- BgNVBAgTClNvbWUtU3RhdGUxITAfBgNVBAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDEUMBIG
    -- A1UEAxMLZWNjMjU2LXRlc3QwHhcNMTYwODIzMTU0OTM5WhcNNDQwMTA4MTU0OTM5WjBbMQswCQYD
    -- VQQGEwJBVTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0cyBQ
    -- dHkgTHRkMRQwEgYDVQQDEwtlY2MyNTYtdGVzdDBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABARy
    -- Hd86A7+qQ4DlIfKynZaFGdGLpkU3GlQwqaVD6GIJg3QIDhaWEksYtZ9OWjNHn9a6+i/P9o5/NrdI
    -- SP0VWDWjgcAwgb0wHQYDVR0OBBYEFGmd2DfK4c3VBTVn8ebKH2BTmRicMIGNBgNVHSMEgYUwgYKA
    -- FGmd2DfK4c3VBTVn8ebKH2BTmRicoV+kXTBbMQswCQYDVQQGEwJBVTETMBEGA1UECBMKU29tZS1T
    -- dGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMRQwEgYDVQQDEwtlY2MyNTYt
    -- dGVzdIIJAJuS2kgOoyr+MAwGA1UdEwQFMAMBAf8wCQYHKoZIzj0EAQNJADBGAiEAhuiwGy0lZKtK
    -- LA13Ffc8AMTK7NsePPSfs0clXlZUR78CIQDXsJXpYsJ7tlTeockrK+6099sG/gGFTRPiQYU8juyf
    -- LzGCAT8wggE7AgEBMGgwWzELMAkGA1UEBhMCQVUxEzARBgNVBAgTClNvbWUtU3RhdGUxITAfBgNV
    -- BAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDEUMBIGA1UEAxMLZWNjMjU2LXRlc3QCCQCbktpI
    -- DqMq/jANBglghkgBZQMEAgEFAKBpMBgGCSqGSIb3DQEJAzELBgkqhkiG9w0BBwEwHAYJKoZIhvcN
    -- AQkFMQ8XDTIyMDUwODE0MzM1NFowLwYJKoZIhvcNAQkEMSIEIMvNpYuymLvF68aFFe+l3NEQTdkB
    -- 7T8U6/j+Y5FgDlIUMAoGCCqGSM49BAMCBEYwRAIgQ6migEn3Le9ciIVUP/qc2JZAJZWjpulJXY9Q
    -- IZJ3XK0CIGZwpLOgiv7ES8958hqT9a/q7aPc1xmkaZHtsat/k+Ki
    -- 
    -- ------=_NextPart_6e3_c655_9641494d.b4f00501--

    EXEC @hr = sp_OADestroy @mime
    EXEC @hr = sp_OADestroy @cert


END
GO

 

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