Sample code for 30+ languages & platforms
SQL Server

PKCS11 Establish Session with Login (QuickSession)

See more PKCS11 Examples

Demonstrates a shorter and quicker way to establish a PKCS11 session.

Note: This example requires Chilkat v9.5.0.96 or later.

Chilkat SQL Server Downloads

SQL Server
-- 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)
    DECLARE @success int
    SELECT @success = 0

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

    -- Note: Chilkat's PKCS11 implementation runs on Windows, Linux, Mac OS X, and other supported operating systems.

    DECLARE @pkcs11 int
    EXEC @hr = sp_OACreate 'Chilkat.Pkcs11', @pkcs11 OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    -- Use the PKCS11 driver (.dll, .so, .dylib) for your particular HSM.
    -- For example:
    EXEC sp_OASetProperty @pkcs11, 'SharedLibPath', 'C:/Program Files (x86)/Gemalto/IDGo 800 PKCS#11/IDPrimePKCS1164.dll'

    -- Use your HSM's PIN to login.
    -- If you wish to establish a session without logging in, then set the pin equal to the empty string.
    DECLARE @pin nvarchar(4000)
    SELECT @pin = '0000'

    -- Normal user = 1
    DECLARE @userType int
    SELECT @userType = 1

    -- Establish a logged-on user session with the HSM.
    -- The call to QuickSession takes the place of calling Initialize, OpenSession, and Login.
    EXEC sp_OAMethod @pkcs11, 'QuickSession', @success OUT, @userType, @pin
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pkcs11, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pkcs11
        RETURN
      END

    -- Do whatever is needed in your PKCS11 session.
    -- ...
    -- ...

    EXEC sp_OAMethod @pkcs11, 'Logout', @success OUT
    EXEC sp_OAMethod @pkcs11, 'CloseSession', @success OUT


    PRINT 'Success.'

    EXEC @hr = sp_OADestroy @pkcs11


END
GO