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

Visual FoxPro 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
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
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
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(Visual FoxPro) PKCS11 Sign PDF using Certificate and Private Key on Smart Card / USB Token

See more PKCS11 Examples

Sample code showing how to use PKCS11 to sign a PDF with a certificate and private key stored on a smart card or USB token.

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

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

LOCAL loPkcs11
LOCAL lnSuccess
LOCAL lnOnlyTokensPresent
LOCAL loJsonPkcs11
LOCAL lnSlotID
LOCAL lnReadWrite
LOCAL lnUserType
LOCAL lcPin
LOCAL loCert
LOCAL loPdf
LOCAL loJson
LOCAL i

* 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.

loPkcs11 = CreateObject('Chilkat_9_5_0.Pkcs11')

* This example is tested on Windows using a Giesecke and Devrient Sm@rtCafe Expert 3.2 smart card.
loPkcs11.SharedLibPath = "aetpkss1.dll"

lnSuccess = loPkcs11.Initialize()
IF (lnSuccess = 0) THEN
    ? loPkcs11.LastErrorText
    RELEASE loPkcs11
    CANCEL
ENDIF

* We need to call Discover to get the slot ID.
lnOnlyTokensPresent = 1
loJsonPkcs11 = CreateObject('Chilkat_9_5_0.JsonObject')
lnSuccess = loPkcs11.Discover(lnOnlyTokensPresent,loJsonPkcs11)
IF (lnSuccess = 0) THEN
    ? loPkcs11.LastErrorText
    RELEASE loPkcs11
    RELEASE loJsonPkcs11
    CANCEL
ENDIF

* Make sure we have at least one slot.
IF (loJsonPkcs11.SizeOfArray("slot") <= 0) THEN
    ? "No occuplied slots."
    RELEASE loPkcs11
    RELEASE loJsonPkcs11
    CANCEL
ENDIF

* Get the ID of the 1st slot
lnSlotID = loJsonPkcs11.IntOf("slot[0].id")

* Open a session.
lnReadWrite = 1
lnSuccess = loPkcs11.OpenSession(lnSlotID,lnReadWrite)
IF (lnSuccess = 0) THEN
    ? loPkcs11.LastErrorText
    RELEASE loPkcs11
    RELEASE loJsonPkcs11
    CANCEL
ENDIF

* Make it an authenticated session by calling Login.
lnUserType = 1
* Make sure to use the correct PIN for your smart card..
lcPin = "0000"
lnSuccess = loPkcs11.Login(lnUserType,lcPin)
IF (lnSuccess = 0) THEN
    ? loPkcs11.LastErrorText
    lnSuccess = loPkcs11.CloseSession()
    RELEASE loPkcs11
    RELEASE loJsonPkcs11
    CANCEL
ENDIF

* Get the certificate (on the smart card) that has a private key.
loCert = CreateObject('Chilkat_9_5_0.Cert')
lnSuccess = loPkcs11.FindCert("privateKey","",loCert)
IF (lnSuccess = 1) THEN
    ? "Cert with private key: " + loCert.SubjectCN
ELSE
    ? "No certificates having a private key were found."
    lnSuccess = loPkcs11.CloseSession()
    RELEASE loPkcs11
    RELEASE loJsonPkcs11
    RELEASE loCert
    CANCEL
ENDIF

* --------------------------------------------------------------------------
* At this point, we have the cert to be used for signing.
* Our PDF signing code is the same as for a cert obtained from any other source..

loPdf = CreateObject('Chilkat_9_5_0.Pdf')

* Load a PDF to be signed.
lnSuccess = loPdf.LoadFile("qa_data/pdf/hello.pdf")
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    lnSuccess = loPkcs11.CloseSession()
    RELEASE loPkcs11
    RELEASE loJsonPkcs11
    RELEASE loCert
    RELEASE loPdf
    CANCEL
ENDIF

loJson = CreateObject('Chilkat_9_5_0.JsonObject')

loJson.UpdateInt("page",1)
loJson.UpdateString("appearance.y","top")
loJson.UpdateString("appearance.x","left")
loJson.UpdateString("appearance.fontScale","10.0")
loJson.UpdateString("signingAlgorithm","pss")
loJson.UpdateString("hashAlgorithm","sha256")

i = 0
loJson.I = i
loJson.UpdateString("appearance.text[i]","Digitaly signed by: Xyz Widgets, Inc.")
i = i + 1
loJson.I = i
loJson.UpdateString("appearance.text[i]","current_dt")
i = i + 1
loJson.I = i
loJson.UpdateString("appearance.text[i]","blah blah blah")

* The certificate is internally linked to the Pkcs11 object, which is currently in an authenticated session.
lnSuccess = loPdf.SetSigningCert(loCert)

lnSuccess = loPdf.SignPdf(loJson,"qa_output/out.pdf")
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    lnSuccess = loPkcs11.CloseSession()
    RELEASE loPkcs11
    RELEASE loJsonPkcs11
    RELEASE loCert
    RELEASE loPdf
    RELEASE loJson
    CANCEL
ENDIF

* --------------------------------------------------------------------------

* Revert to an unauthenticated session by calling Logout.
lnSuccess = loPkcs11.Logout()
IF (lnSuccess = 0) THEN
    ? loPkcs11.LastErrorText
    lnSuccess = loPkcs11.CloseSession()
    RELEASE loPkcs11
    RELEASE loJsonPkcs11
    RELEASE loCert
    RELEASE loPdf
    RELEASE loJson
    CANCEL
ENDIF

* When finished, close the session.
* It is important to close the session (memory leaks will occur if the session is not properly closed).
lnSuccess = loPkcs11.CloseSession()
IF (lnSuccess = 0) THEN
    ? loPkcs11.LastErrorText
    RELEASE loPkcs11
    RELEASE loJsonPkcs11
    RELEASE loCert
    RELEASE loPdf
    RELEASE loJson
    CANCEL
ENDIF

? "Success."

RELEASE loPkcs11
RELEASE loJsonPkcs11
RELEASE loCert
RELEASE loPdf
RELEASE loJson


 

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