Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

SQL Server Examples

Web API Categories

ASN.1
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
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
NTLM
OAuth1
OAuth2
Office365
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(SQL Server) Export a Private Key from an MS Storage Provider

Demonstrates how to export a private key from a Microsoft Storage Provider.

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

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    -- This example requires Chilkat v9.5.0.83 or greater.

    -- We'll export a certificate's private key from the MS storage provider.
    -- It is assumed the certificate + private key is already installed on the Windows system.
    -- The export does not remove the key from the Windows storage provider.

    -- First, let's get a certificate in one of the many ways we can do it.
    -- (I ran certmgr.msc, opened a certificate, and noted it's thumbprint.)
    DECLARE @cert int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Cert', @cert OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int
    EXEC sp_OAMethod @cert, 'LoadByThumbprint', @success OUT, 'ea5a129c1919a52d238ee28d9f3a8f345b768388', 'hex'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END


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

    -- First export the private key the easy way.
    DECLARE @privKey int
    EXEC sp_OAMethod @cert, 'ExportPrivateKey', @privKey OUT
    EXEC sp_OAGetProperty @cert, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    -- OK.. we have the private key.  Do whatever we want with it..

    EXEC @hr = sp_OADestroy @privKey

    -- -------------------------------------------------------------
    -- Now let's export in a more roundabout way by getting information about the 
    -- storage provider and key name and then we'll export completely independent
    -- of the certificate.
    -- 
    -- Remember: The private key is not contained within the certificate.  An X.509 certificate
    -- embeds the public key, but the counterpart private key is stored elsewhere, such
    -- as in a .pfx/.p12, or as in this case, in the Windows "protected store", or perhaps on
    -- a smartcard or hardware token.  (But a private key stored on a smartcard or token cannot
    -- be exported.  It must remain on the hardware.)
    -- 

    DECLARE @storageProvider nvarchar(4000)
    EXEC sp_OAGetProperty @cert, 'CspName', @storageProvider OUT
    DECLARE @keyName nvarchar(4000)
    EXEC sp_OAGetProperty @cert, 'KeyContainerName', @keyName OUT


    PRINT 'Information about the certificate''s private key:'

    PRINT 'Storage Provider: ' + @storageProvider

    PRINT 'Key Name: ' + @keyName

    -- Export using just the name of the storage provider and key.
    DECLARE @keyCon int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.KeyContainer', @keyCon OUT

    DECLARE @privKey2 int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.PrivateKey', @privKey2 OUT

    DECLARE @silentFlag int
    SELECT @silentFlag = 0
    EXEC sp_OAMethod @keyCon, 'ExportKey', @success OUT, @keyName, @storageProvider, @silentFlag, @privKey2
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @keyCon, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @keyCon
        EXEC @hr = sp_OADestroy @privKey2
        RETURN
      END

    -- OK.. we have the private key in privKey2.  Do whatever we want with it..
    -- Perhaps we save as encrypted PKCS8 PEM.
    EXEC sp_OAMethod @privKey2, 'SavePkcs8EncryptedPemFile', @success OUT, 'myPassword', 'qa_output/privKey2.pem'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @privKey2, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @keyCon
        EXEC @hr = sp_OADestroy @privKey2
        RETURN
      END


    PRINT 'Success.'

    EXEC @hr = sp_OADestroy @cert
    EXEC @hr = sp_OADestroy @keyCon
    EXEC @hr = sp_OADestroy @privKey2


END
GO

 

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