SQL Server
SQL Server
ScMinidriver - Get CSP Container Map for Smart Card or USB Token
See more ScMinidriver Examples
Returns the contents of the CSP container map file (cmapfile). This gives an overview of what key containers and certificates exist in the smart card from a CSP's point of view.Chilkat SQL Server Downloads
-- 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.
DECLARE @scmd int
EXEC @hr = sp_OACreate 'Chilkat.ScMinidriver', @scmd OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Reader names (smart card readers or USB tokens) can be discovered
-- via PCSC List Readers or PCSC Find Smart Cards
DECLARE @readerName nvarchar(4000)
SELECT @readerName = 'SCM Microsystems Inc. SCR33x USB Smart Card Reader 0'
EXEC sp_OAMethod @scmd, 'AcquireContext', @success OUT, @readerName
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @scmd, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @scmd
RETURN
END
DECLARE @json int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
EXEC sp_OASetProperty @json, 'EmitCompact', 0
EXEC sp_OAMethod @scmd, 'GetCspContainerMap', @success OUT, @json
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @scmd, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @scmd
EXEC @hr = sp_OADestroy @json
RETURN
END
EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
PRINT @sTmp0
-- Here is sample output:
-- See below for sample code to parse the JSON.
-- --------------------------------------------------------------
-- Some notes:
--
-- 1) Empty containers are not listed.
-- 2) The "index" is the index of the key container on the card.
-- Note that in the example output below, the card has 4 occupied containers
-- at indexes 0, 1, 2, and 5.
-- 3) If you see a key size (in bits) equal to 0, it means that key does not exist.
-- 4) A container can potentially contain 2 keys + certificates -- a key for signing (also known as
-- the "signature" key), and a key for authentication/authorization (also known as the
-- "key exchange" key).
-- 5) It is possible for a key to exist without the certificate. For example, in the results
-- below there is a 2048-bit key-exchange key at index 1, but no certificate.
-- 6) A certificate belonging to the key-exchange key is contained in the "kexCert" JSON member,
-- a certificate belonging to the signature key is contained in the "sigCert" JSON member (not shown below).
-- --------------------------------------------------------------
-- {
-- "containers": [
-- {
-- "index": 0,
-- "guid": "CertReq-PIVKeyC910-205fc3c2-19fe--42448",
-- "default": true,
-- "sigKeySizeBits": 0,
-- "kexKeySizeBits": 2048,
-- "kexCert": {
-- "serialNum": "15FBFBCF00010000313D",
-- "issuerCN": "PIVKey Device Certificate Authority",
-- "subjectCN": "PIVKey Device Certificate Authority"
-- }
-- },
-- {
-- "index": 1,
-- "guid": "{7b3c32dd-e992-c58f-0822-67d72fd52d43}",
-- "default": false,
-- "sigKeySizeBits": 0,
-- "kexKeySizeBits": 2048
-- },
-- {
-- "index": 2,
-- "guid": "{57547a77-18e7-7516-f5da-6aee13b46bbc}",
-- "default": false,
-- "sigKeySizeBits": 0,
-- "kexKeySizeBits": 2048
-- },
-- {
-- "index": 5,
-- "guid": "{306fd6da-9954-4f75-95c0-178f607a41fb}",
-- "default": false,
-- "sigKeySizeBits": 0,
-- "kexKeySizeBits": 2048,
-- "kexCert": {
-- "serialNum": "17C1B793A24D1A81490F5F768E6D23A5",
-- "issuerCN": "Matt",
-- "subjectCN": "Matt"
-- }
-- }
-- ]
-- }
-- Use this online tool to generate parsing code from sample JSON:
-- Generate Parsing Code from JSON
DECLARE @index int
DECLARE @guid nvarchar(4000)
DECLARE @default int
DECLARE @sigKeySizeBits int
DECLARE @kexKeySizeBits int
DECLARE @kexCertSerialNum nvarchar(4000)
DECLARE @kexCertIssuerCN nvarchar(4000)
DECLARE @kexCertSubjectCN nvarchar(4000)
DECLARE @sigCertSerialNum nvarchar(4000)
DECLARE @sigCertIssuerCN nvarchar(4000)
DECLARE @sigCertSubjectCN nvarchar(4000)
DECLARE @i int
SELECT @i = 0
DECLARE @count_i int
EXEC sp_OAMethod @json, 'SizeOfArray', @count_i OUT, 'containers'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @json, 'I', @i
EXEC sp_OAMethod @json, 'IntOf', @index OUT, 'containers[i].index'
EXEC sp_OAMethod @json, 'StringOf', @guid OUT, 'containers[i].guid'
EXEC sp_OAMethod @json, 'BoolOf', @default OUT, 'containers[i].default'
EXEC sp_OAMethod @json, 'IntOf', @sigKeySizeBits OUT, 'containers[i].sigKeySizeBits'
EXEC sp_OAMethod @json, 'IntOf', @kexKeySizeBits OUT, 'containers[i].kexKeySizeBits'
EXEC sp_OAMethod @json, 'StringOf', @kexCertSerialNum OUT, 'containers[i].kexCert.serialNum'
EXEC sp_OAMethod @json, 'StringOf', @kexCertIssuerCN OUT, 'containers[i].kexCert.issuerCN'
EXEC sp_OAMethod @json, 'StringOf', @kexCertSubjectCN OUT, 'containers[i].kexCert.subjectCN'
EXEC sp_OAMethod @json, 'StringOf', @sigCertSerialNum OUT, 'containers[i].sigCert.serialNum'
EXEC sp_OAMethod @json, 'StringOf', @sigCertIssuerCN OUT, 'containers[i].sigCert.issuerCN'
EXEC sp_OAMethod @json, 'StringOf', @sigCertSubjectCN OUT, 'containers[i].sigCert.subjectCN'
SELECT @i = @i + 1
END
-- Delete the context when finished with the card.
EXEC sp_OAMethod @scmd, 'DeleteContext', @success OUT
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @scmd, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
END
EXEC @hr = sp_OADestroy @scmd
EXEC @hr = sp_OADestroy @json
END
GO