PKCS #11/Ruby Interface for Safenet Luna HSM


API documentation

Safenet Luna HSM

This ruby gem is an add-on to ruby-pkcs11 . It allows to use Luna specific extensions, which are beyond the PKCS#11 standard. The module works on the Unix like operating systems and win32.


  • Luna Client installed including the Luna Sofware Development Kit (SDK)

  • pkcs11 gem installed (use: gem install pkcs11 )


First check the permissions for the directories in the sdk. It may be required to run:

chmod a+x <luna client include dir>
chmod a+x <luna client include dir>/RSA

gem install pkcs11_luna -- --with-luna-dir-include=<luna client include dir>

This installs the Luna-PKCS#11 extension either by compiling (Unix) or by using the precompiled gem for Win32.

git clone git://
cd pkcs11_luna
gem install hoe rake-compiler minitest
rake gem LUNA_INCLUDE_DIR=<luna client include dir>
gem install --verbose  pkg/pkcs11_luna-<version>.gem -- --with-luna-dir-include=<luna client include dir>

rake test
cd ../
rake test

Downloads and installs the gem from git source. If LUNA_INCLUDE_DIR and –with-luna-dir-include are not specified,

The default of /usr/safenet/lunaclient/samples/include is used.


Open the software emulation library and login to a session:

require "rubygems"
require "pkcs11_luna"

pkcs11 =
session =
session.login(:USER, "1234")
# ... crypto operations

Look in the examples directories for some more usage examples.

PKCS11::Luna::Library#initialize reads the crystoki.ini or /etc/Chrystoki.conf and parses the configuration file to determine what .so or .dll to use. You may pass the full path to the .dll or .so file.

Cross compiling for mswin32

Using rake-compiler a cross compiled pkcs11_luna.gem can be build on a linux host for the win32 platform. There are no runtime dependencies to any but the standard Windows DLLs.

Install mingw32. On a debian based system this should work:

apt-get install mingw32

On MacOS X, if you have MacPorts installed:

port install i386-mingw32-gcc

Install the rake-compiler:

gem install rake-compiler

Download and cross compile ruby for win32:

rake-compiler cross-ruby VERSION=1.8.7-p352
rake-compiler cross-ruby VERSION=1.9.2-p290

Download and cross compile pkcs11_luna for win32:

rake cross native gem LUNA_CLIENT_DIR=<luna client dir>

If everything works, there should be pkcs11_luna-VERSION-x86-mswin32.gem in the pkg directory.


  • implement Luna specific function calls

  • implement possibility to use callbacks

  • add all structs and constants



See MIT-LICENSE included in the package.