pkcs11_luna 0.2.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data/.gemtest +0 -0
- data/.yardopts +1 -0
- data/Manifest.txt +24 -0
- data/README_LUNA.rdoc +103 -0
- data/Rakefile +88 -0
- data/examples/config.rb +5 -0
- data/examples/derive_aes_ecdh_key.rb +108 -0
- data/examples/encrypt_decrypt_aes.rb +41 -0
- data/examples/encrypt_decrypt_rsa.rb +47 -0
- data/examples/mechanism_list.rb +20 -0
- data/examples/multithread.rb +73 -0
- data/examples/objects_list.rb +23 -0
- data/examples/sign_verify.rb +56 -0
- data/examples/slot_info.rb +23 -0
- data/ext/extconf.rb +14 -0
- data/ext/generate_constants.rb +57 -0
- data/ext/generate_structs.rb +77 -0
- data/ext/pk11_const_macros.h +38 -0
- data/ext/pk11_struct_macros.h +435 -0
- data/ext/pk11_version.h +6 -0
- data/ext/pk11l.c +352 -0
- data/ext/pk11l_const_def.inc +804 -0
- data/ext/pk11l_struct.doc +1012 -0
- data/ext/pk11l_struct_def.inc +388 -0
- data/ext/pk11l_struct_impl.inc +388 -0
- data/lib/pkcs11_luna/extensions.rb +131 -0
- data/lib/pkcs11_luna.rb +11 -0
- data/test/app_id_helper.rb +29 -0
- data/test/luna_helper.rb +57 -0
- data/test/test_pkcs11_luna.rb +112 -0
- data/test/test_pkcs11_luna_crypt.rb +260 -0
- data.tar.gz.sig +0 -0
- metadata +173 -0
- metadata.gz.sig +1 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 86b7907ea48bf9a5fb15a2fa46add0ed2461160acef2d77413eb04fe97f53e12
|
4
|
+
data.tar.gz: 2347677ddef0eedc4128f7ee9612c4c8d6b8e15fd714348b05b8e630f8b0ed48
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 27b8951067b2c8116284fa5e1d4ee07b0217158d9b2151a8d8ccd4f409729f4702bf71a3eff1ad4e51128d55f556561d49694e122b93f1b75bbde4d252e3dfd6
|
7
|
+
data.tar.gz: 6e5186e12d5709c138e283a6e34a1b87fbe508e7f91508e6a81676b5dd075b3c76a555a49629b3ab86775db52c75097ce086cdd22b08f4f7d952185de729bdd1
|
checksums.yaml.gz.sig
ADDED
Binary file
|
data/.gemtest
ADDED
File without changes
|
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--title "PKCS#11-Luna/Ruby Interface" --no-private lib/**/*.rb ext/*.c ext/*.doc
|
data/Manifest.txt
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
.gemtest
|
2
|
+
.yardopts
|
3
|
+
Manifest.txt
|
4
|
+
README_LUNA.rdoc
|
5
|
+
Rakefile
|
6
|
+
ext/extconf.rb
|
7
|
+
ext/generate_constants.rb
|
8
|
+
ext/generate_structs.rb
|
9
|
+
ext/pk11l.c
|
10
|
+
lib/pkcs11_luna.rb
|
11
|
+
lib/pkcs11_luna/extensions.rb
|
12
|
+
test/luna_helper.rb
|
13
|
+
test/app_id_helper.rb
|
14
|
+
test/test_pkcs11_luna.rb
|
15
|
+
test/test_pkcs11_luna_crypt.rb
|
16
|
+
examples/config.rb
|
17
|
+
examples/derive_aes_ecdh_key.rb
|
18
|
+
examples/sign_verify.rb
|
19
|
+
examples/encrypt_decrypt_aes.rb
|
20
|
+
examples/encrypt_decrypt_rsa.rb
|
21
|
+
examples/mechanism_list.rb
|
22
|
+
examples/multithread.rb
|
23
|
+
examples/objects_list.rb
|
24
|
+
examples/slot_info.rb
|
data/README_LUNA.rdoc
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
= PKCS #11/Ruby Interface for Safenet Luna HSM
|
2
|
+
|
3
|
+
* Homepage: http://github.com/larskanis/pkcs11
|
4
|
+
* API documentation: http://pkcs11.rubyforge.org/pkcs11/
|
5
|
+
* Safenet[http://www.safenet-inc.com] - Luna HSM
|
6
|
+
|
7
|
+
This ruby gem is an add-on to ruby-pkcs11[http://github.com/larskanis/pkcs11] .
|
8
|
+
It allows to use Luna specific extensions, which are beyond the PKCS#11 standard.
|
9
|
+
The module works on the Unix like operating systems and win32.
|
10
|
+
|
11
|
+
== Requirements
|
12
|
+
|
13
|
+
* Luna Client installed including the Luna Sofware Development Kit (SDK)
|
14
|
+
* pkcs11 gem installed (use: <tt>gem install pkcs11</tt> )
|
15
|
+
|
16
|
+
== Installation
|
17
|
+
|
18
|
+
First check the permissions for the directories in the sdk. It may be required to run:
|
19
|
+
chmod a+x <luna client include dir>
|
20
|
+
chmod a+x <luna client include dir>/RSA
|
21
|
+
|
22
|
+
gem install pkcs11_luna -- --with-luna-dir-include=<luna client include dir>
|
23
|
+
|
24
|
+
This installs the Luna-PKCS#11 extension either by compiling (Unix)
|
25
|
+
or by using the precompiled gem for Win32.
|
26
|
+
|
27
|
+
git clone git://github.com/larskanis/pkcs11.git
|
28
|
+
cd pkcs11_luna
|
29
|
+
gem install hoe rake-compiler minitest
|
30
|
+
rake gem LUNA_INCLUDE_DIR=<luna client include dir>
|
31
|
+
gem install --verbose pkg/pkcs11_luna-<version>.gem -- --with-luna-dir-include=<luna client include dir>
|
32
|
+
|
33
|
+
rake test
|
34
|
+
cd ../
|
35
|
+
rake test
|
36
|
+
|
37
|
+
Downloads and installs the gem from git source. If LUNA_INCLUDE_DIR and --with-luna-dir-include are not specified,
|
38
|
+
The default of /usr/safenet/lunaclient/samples/include is used.
|
39
|
+
|
40
|
+
|
41
|
+
== Usage
|
42
|
+
|
43
|
+
Open the software emulation library and login to a session:
|
44
|
+
|
45
|
+
require "rubygems"
|
46
|
+
require "pkcs11_luna"
|
47
|
+
|
48
|
+
pkcs11 = PKCS11::Luna::Library.new
|
49
|
+
p pkcs11.info
|
50
|
+
session = pkcs11.active_slots.last.open
|
51
|
+
session.login(:USER, "1234")
|
52
|
+
# ... crypto operations
|
53
|
+
session.logout
|
54
|
+
session.close
|
55
|
+
|
56
|
+
Look in the examples directories for some more usage examples.
|
57
|
+
|
58
|
+
{PKCS11::Luna::Library#initialize} reads the crystoki.ini or /etc/Chrystoki.conf
|
59
|
+
and parses the configuration file to determine what .so or .dll to use. You may pass
|
60
|
+
the full path to the .dll or .so file.
|
61
|
+
|
62
|
+
== Cross compiling for mswin32
|
63
|
+
|
64
|
+
Using rake-compiler a cross compiled pkcs11_luna.gem can be build on a linux host for
|
65
|
+
the win32 platform. There are no runtime dependencies to any but the standard Windows DLLs.
|
66
|
+
|
67
|
+
Install mingw32. On a debian based system this should work:
|
68
|
+
|
69
|
+
apt-get install mingw32
|
70
|
+
|
71
|
+
On MacOS X, if you have MacPorts installed:
|
72
|
+
|
73
|
+
port install i386-mingw32-gcc
|
74
|
+
|
75
|
+
Install the rake-compiler:
|
76
|
+
|
77
|
+
gem install rake-compiler
|
78
|
+
|
79
|
+
Download and cross compile ruby for win32:
|
80
|
+
|
81
|
+
rake-compiler cross-ruby VERSION=1.8.7-p352
|
82
|
+
rake-compiler cross-ruby VERSION=1.9.2-p290
|
83
|
+
|
84
|
+
Download and cross compile pkcs11_luna for win32:
|
85
|
+
|
86
|
+
rake cross native gem LUNA_CLIENT_DIR=<luna client dir>
|
87
|
+
|
88
|
+
If everything works, there should be pkcs11_luna-VERSION-x86-mswin32.gem in the pkg
|
89
|
+
directory.
|
90
|
+
|
91
|
+
|
92
|
+
== ToDo
|
93
|
+
|
94
|
+
* implement Luna specific function calls
|
95
|
+
* implement possibility to use callbacks
|
96
|
+
* add all structs and constants
|
97
|
+
|
98
|
+
== Authors
|
99
|
+
* Lars Kanis <kanis@comcard.de>
|
100
|
+
* Jonathan Patchell
|
101
|
+
|
102
|
+
== Copying
|
103
|
+
See MIT-LICENSE included in the package.
|
data/Rakefile
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
# -*- ruby -*-
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'hoe'
|
6
|
+
require 'rake/extensiontask'
|
7
|
+
require 'rbconfig'
|
8
|
+
|
9
|
+
LUNA_INCLUDE_DIR = ENV['LUNA_INCLUDE_DIR'] || '/usr/safenet/lunaclient/samples/include'
|
10
|
+
RUBY_PKCS11_EXT_DIR = File.expand_path('../ext')
|
11
|
+
|
12
|
+
|
13
|
+
GENERATED_FILES = [
|
14
|
+
'ext/pk11l_struct_impl.inc',
|
15
|
+
'ext/pk11l_struct_def.inc',
|
16
|
+
'ext/pk11l_const_def.inc',
|
17
|
+
'ext/pk11l_struct.doc',
|
18
|
+
'ext/pk11_struct_macros.h',
|
19
|
+
'ext/pk11_const_macros.h',
|
20
|
+
'ext/pk11_version.h',
|
21
|
+
]
|
22
|
+
|
23
|
+
CLEAN.include GENERATED_FILES
|
24
|
+
CLEAN.include 'lib/pkcs11_luna_ext.so'
|
25
|
+
CLEAN.include 'tmp'
|
26
|
+
CLEAN.include 'examples/output'
|
27
|
+
|
28
|
+
def pkcs11_version
|
29
|
+
file = File.join(RUBY_PKCS11_EXT_DIR, 'pk11_version.h')
|
30
|
+
version_re = /VERSION += +([\"\'])([\d][\d\w\.]+)\1/
|
31
|
+
File.read_utf(file)[version_re, 2]
|
32
|
+
end
|
33
|
+
|
34
|
+
hoe = Hoe.spec 'pkcs11_luna' do
|
35
|
+
developer('SafeNet', 'support@safenet-inc.com')
|
36
|
+
extra_deps << ['pkcs11', "= #{pkcs11_version}"]
|
37
|
+
extra_dev_deps << ['yard', '>= 0.6']
|
38
|
+
extra_dev_deps << ['rake-compiler', '>= 0.7']
|
39
|
+
|
40
|
+
self.url = 'http://github.com/larskanis/pkcs11'
|
41
|
+
self.summary = 'SafeNet-Luna extensions for PKCS#11-Ruby'
|
42
|
+
self.description = 'This module allows Ruby programs to use vendor extensions for SafeNet Luna.'
|
43
|
+
self.version = pkcs11_version
|
44
|
+
|
45
|
+
self.readme_file = 'README_LUNA.rdoc'
|
46
|
+
self.history_file = '../History.txt'
|
47
|
+
self.extra_rdoc_files << self.readme_file << 'ext/pk11l.c'
|
48
|
+
spec_extras[:extensions] = 'ext/extconf.rb'
|
49
|
+
spec_extras[:files] = File.read_utf("Manifest.txt").split(/\r?\n\r?/)
|
50
|
+
spec_extras[:files] += GENERATED_FILES
|
51
|
+
spec_extras[:has_rdoc] = 'yard'
|
52
|
+
end
|
53
|
+
|
54
|
+
Rake::ExtensionTask.new('pkcs11_luna_ext', hoe.spec) do |ext|
|
55
|
+
ext.ext_dir = 'ext'
|
56
|
+
ext.cross_compile = true # enable cross compilation (requires cross compile toolchain)
|
57
|
+
ext.cross_platform = ['i386-mingw32'] # forces the Windows platform instead of the default one
|
58
|
+
puts "LUNA_INCLUDE_DIR: #{LUNA_INCLUDE_DIR.inspect}"
|
59
|
+
ext.config_options << "--with-luna-dir-include=\"#{LUNA_INCLUDE_DIR}\""
|
60
|
+
end
|
61
|
+
|
62
|
+
def copy_from_base_task(filename)
|
63
|
+
file File.join('ext', filename) => File.join(RUBY_PKCS11_EXT_DIR, filename) do |t|
|
64
|
+
cp t.prerequisites.first, t.name, :verbose=>true
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
copy_from_base_task 'pk11_struct_macros.h'
|
69
|
+
copy_from_base_task 'pk11_const_macros.h'
|
70
|
+
copy_from_base_task 'pk11_version.h'
|
71
|
+
|
72
|
+
HEADER_FILES = "#{LUNA_INCLUDE_DIR}/RSA/pkcs11t.h #{LUNA_INCLUDE_DIR}/cryptoki_v2.h"
|
73
|
+
|
74
|
+
file 'ext/extconf.rb' => ['ext/pk11l_struct_def.inc', 'ext/pk11l_const_def.inc', 'ext/pk11_struct_macros.h', 'ext/pk11_const_macros.h', 'ext/pk11_version.h']
|
75
|
+
file 'ext/pk11l_struct_def.inc' => 'ext/generate_structs.rb' do
|
76
|
+
sh "#{RbConfig::CONFIG['ruby_install_name']} -I../lib ext/generate_structs.rb --def ext/pk11l_struct_def.inc --impl ext/pk11l_struct_impl.inc --doc ext/pk11l_struct.doc #{HEADER_FILES}"
|
77
|
+
end
|
78
|
+
file 'ext/pk11l_struct_impl.inc' => 'ext/pk11l_struct_def.inc'
|
79
|
+
file 'ext/pk11l_struct.doc' => 'ext/pk11l_struct_def.inc'
|
80
|
+
|
81
|
+
file 'ext/pk11l_const_def.inc' => 'ext/generate_constants.rb' do
|
82
|
+
sh "#{RbConfig::CONFIG['ruby_install_name']} -I../lib ext/generate_constants.rb --const ext/pk11l_const_def.inc #{HEADER_FILES}"
|
83
|
+
end
|
84
|
+
file 'ext/pk11l.c' => ['ext/pk11l_struct_def.inc', 'ext/pk11l_struct_impl.inc', 'ext/pk11l_const_def.inc']
|
85
|
+
|
86
|
+
task :doc_files => 'ext/pk11l_struct.doc'
|
87
|
+
|
88
|
+
# vim: syntax=ruby
|
data/examples/config.rb
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'pkcs11_luna'
|
5
|
+
require File.join(File.dirname(__FILE__), 'config')
|
6
|
+
include PKCS11
|
7
|
+
|
8
|
+
#This example demonstrates deriving an AES key using the ECDH public key of
|
9
|
+
#another participant and using the keys to encrypt and decrypt data.
|
10
|
+
|
11
|
+
|
12
|
+
PUBLIC_KEY_LABEL = "'s Ruby Public EC Key"
|
13
|
+
PRIVATE_KEY_LABEL = "'s Ruby Private EC Key"
|
14
|
+
DERIVED_KEY_LABEL = "'s Ruby ECDH Derived AES Key"
|
15
|
+
|
16
|
+
def destroy_object(session, label)
|
17
|
+
session.find_objects(:LABEL=>label) do |obj|
|
18
|
+
puts "Destroying object: #{obj.to_i}"
|
19
|
+
obj.destroy
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class Party
|
24
|
+
include PKCS11
|
25
|
+
|
26
|
+
attr_reader :pub_key
|
27
|
+
attr_reader :priv_key
|
28
|
+
|
29
|
+
def initialize(session, name)
|
30
|
+
@session = session
|
31
|
+
@name = name
|
32
|
+
@shared_data = "SHARED DATA"
|
33
|
+
end
|
34
|
+
|
35
|
+
def generate_key()
|
36
|
+
destroy_object(@session, @name + PUBLIC_KEY_LABEL)
|
37
|
+
destroy_object(@session, @name + PRIVATE_KEY_LABEL)
|
38
|
+
|
39
|
+
#DER encoding of OID 1.3.132.0.10 secp256k1
|
40
|
+
curve_oid_der = [0x06, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x0A].pack("C*")
|
41
|
+
|
42
|
+
attributes_public = {:TOKEN=>true, :ENCRYPT=>true, :VERIFY=>true, :WRAP=>true,
|
43
|
+
:EC_PARAMS=>curve_oid_der, :LABEL=>@name + PUBLIC_KEY_LABEL}
|
44
|
+
attributes_private = {:TOKEN=>true, :DECRYPT=>true, :SIGN=>true,
|
45
|
+
:DERIVE=>true, :UNWRAP=>true, :SENSITIVE=>true, :LABEL=>@name + PRIVATE_KEY_LABEL}
|
46
|
+
|
47
|
+
@pub_key, @priv_key = @session.generate_key_pair(:EC_KEY_PAIR_GEN, attributes_public, attributes_private)
|
48
|
+
|
49
|
+
puts "Generated Public EC key: (#{@pub_key[:LABEL]}, #{@pub_key.to_i})"
|
50
|
+
puts "Generated Private EC key: (#{@priv_key[:LABEL]}, #{@priv_key.to_i})"
|
51
|
+
end
|
52
|
+
|
53
|
+
def derive_key(other)
|
54
|
+
destroy_object(@session, @name + DERIVED_KEY_LABEL)
|
55
|
+
|
56
|
+
ec_point = other.pub_key.attributes(:EC_POINT)[0].value
|
57
|
+
mechanism = {:ECDH1_DERIVE=>{:kdf=>Luna::CKD_SHA512_KDF, :pSharedData=>@shared_data, :pPublicData=>ec_point}}
|
58
|
+
|
59
|
+
derive_attributes = {:CLASS=>CKO_SECRET_KEY, :KEY_TYPE=>CKK_AES, :TOKEN=>true, :SENSITIVE=>true, :PRIVATE=>true,
|
60
|
+
:ENCRYPT=>true, :DECRYPT=>true, :SIGN=>true, :VERIFY=>true, :VALUE_LEN=>32, :LABEL=>@name + DERIVED_KEY_LABEL}
|
61
|
+
|
62
|
+
@derived_key = @session.derive_key(mechanism, @priv_key, derive_attributes)
|
63
|
+
|
64
|
+
puts "Derived AES key: (#{@derived_key[:LABEL]}, #{@derived_key.to_i})"
|
65
|
+
end
|
66
|
+
|
67
|
+
def send_message(message)
|
68
|
+
iv = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16].pack("C*")
|
69
|
+
encrypted_message = @session.encrypt({:AES_CBC_PAD=>iv}, @derived_key, message)
|
70
|
+
hex = encrypted_message.bytes.map { |b| sprintf("%02X",b) }.join
|
71
|
+
puts "#{@name} sent encrypted message: #{hex}"
|
72
|
+
return encrypted_message
|
73
|
+
end
|
74
|
+
|
75
|
+
def receive_message(encrypted_message)
|
76
|
+
iv = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16].pack("C*")
|
77
|
+
decrypted_message = @session.decrypt({:AES_CBC_PAD=>iv}, @derived_key, encrypted_message)
|
78
|
+
puts "#{@name} decrypted message: #{decrypted_message}"
|
79
|
+
return decrypted_message
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
pkcs11 = Luna::Library.new
|
86
|
+
|
87
|
+
slot = PKCS11::Slot.new(pkcs11, SamplesConfig::SLOT)
|
88
|
+
session = slot.open
|
89
|
+
|
90
|
+
session.login(:USER, SamplesConfig::PIN)
|
91
|
+
|
92
|
+
alice = Party.new(session, "Alice")
|
93
|
+
bob = Party.new(session, "Bob")
|
94
|
+
alice.generate_key()
|
95
|
+
bob.generate_key()
|
96
|
+
alice.derive_key(bob)
|
97
|
+
bob.derive_key(alice)
|
98
|
+
|
99
|
+
encrypted_message = alice.send_message("Hello Bob!")
|
100
|
+
bob.receive_message(encrypted_message)
|
101
|
+
|
102
|
+
encrypted_message = bob.send_message("Hi Alice!")
|
103
|
+
alice.receive_message(encrypted_message)
|
104
|
+
|
105
|
+
|
106
|
+
session.logout
|
107
|
+
session.close
|
108
|
+
pkcs11.close
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'pkcs11_luna'
|
3
|
+
require File.join(File.dirname(__FILE__), 'config')
|
4
|
+
include PKCS11
|
5
|
+
|
6
|
+
#This example generates an AES key and uses it to encrypt and decrypt a message
|
7
|
+
|
8
|
+
pkcs11 = Luna::Library.new
|
9
|
+
|
10
|
+
KEY_LABEL = "Ruby AES Key"
|
11
|
+
|
12
|
+
slot = Slot.new(pkcs11, SamplesConfig::SLOT)
|
13
|
+
session = slot.open(CKF_RW_SESSION | CKF_SERIAL_SESSION)
|
14
|
+
session.login(:USER, SamplesConfig::PIN)
|
15
|
+
|
16
|
+
session.find_objects(:LABEL=>KEY_LABEL) do |obj|
|
17
|
+
puts "Destroying object: #{obj.to_i}"
|
18
|
+
obj.destroy
|
19
|
+
end
|
20
|
+
|
21
|
+
key = session.generate_key(:AES_KEY_GEN,
|
22
|
+
:CLASS=>CKO_SECRET_KEY, :ENCRYPT=>true, :DECRYPT=>true, :SENSITIVE=>true,
|
23
|
+
:TOKEN=>true, :VALUE_LEN=>32, :LABEL=>KEY_LABEL)
|
24
|
+
|
25
|
+
puts "Generated AES key: (#{key[:LABEL]}, #{key.to_i})"
|
26
|
+
|
27
|
+
iv = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16].pack('C*')
|
28
|
+
mechanism = {:AES_CBC_PAD=>iv}
|
29
|
+
cryptogram = ""
|
30
|
+
cryptogram = session.encrypt(mechanism, key, "Can you read this?")
|
31
|
+
|
32
|
+
puts "Encrypted: " + cryptogram.bytes.map { |b| sprintf("%02X",b) }.join
|
33
|
+
|
34
|
+
decrypted = session.decrypt(mechanism, key, cryptogram)
|
35
|
+
|
36
|
+
puts "Decrypted: " + decrypted
|
37
|
+
|
38
|
+
session.logout
|
39
|
+
session.close
|
40
|
+
pkcs11.close
|
41
|
+
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'pkcs11_luna'
|
3
|
+
require File.join(File.dirname(__FILE__), 'config')
|
4
|
+
include PKCS11
|
5
|
+
|
6
|
+
#This example generates a public/private RSA key pair and uses the public key
|
7
|
+
#to encrypt a message and the private key to decrypt it.
|
8
|
+
|
9
|
+
pkcs11 = Luna::Library.new
|
10
|
+
|
11
|
+
def destroy_object(session, label)
|
12
|
+
session.find_objects(:LABEL=>label) do |obj|
|
13
|
+
puts "Destroying object: #{obj.to_i}"
|
14
|
+
obj.destroy
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
slot = Slot.new(pkcs11, SamplesConfig::SLOT)
|
19
|
+
session = slot.open(CKF_RW_SESSION | CKF_SERIAL_SESSION)
|
20
|
+
session.login(:USER, SamplesConfig::PIN)
|
21
|
+
|
22
|
+
pub_label = "Ruby RSA public key"
|
23
|
+
priv_label = "Ruby RSA private key"
|
24
|
+
destroy_object(session, pub_label)
|
25
|
+
destroy_object(session, priv_label)
|
26
|
+
|
27
|
+
pub_attr = {:ENCRYPT=>true, :VERIFY=>true, :MODULUS_BITS=>2048,
|
28
|
+
:TOKEN=>true, :WRAP=>true, :LABEL=>pub_label}
|
29
|
+
priv_attr = {:DECRYPT=>true, :SIGN=>true, :SENSITIVE=>true, :PRIVATE=>true, :TOKEN=>true,
|
30
|
+
:UNWRAP=>true, :LABEL=>pub_label}
|
31
|
+
|
32
|
+
#RSA_PKCS_KEY_PAIR_GEN
|
33
|
+
pub_key, priv_key = session.generate_key_pair(:RSA_FIPS_186_3_AUX_PRIME_KEY_PAIR_GEN, pub_attr, priv_attr)
|
34
|
+
|
35
|
+
puts "Generated RSA public/private keys: #{pub_key[:LABEL]} (#{pub_key.to_i}), #{priv_key[:LABEL]} (#{priv_key.to_i})"
|
36
|
+
|
37
|
+
ciphertext = session.encrypt(:RSA_PKCS, pub_key, "Can you read this?")
|
38
|
+
puts "Encrypted: " + ciphertext.bytes.map { |b| sprintf("%02X",b) }.join
|
39
|
+
|
40
|
+
decrypted = session.decrypt(:RSA_PKCS, priv_key, ciphertext)
|
41
|
+
|
42
|
+
puts "Decrypted: " + decrypted
|
43
|
+
|
44
|
+
session.logout
|
45
|
+
session.close
|
46
|
+
pkcs11.close
|
47
|
+
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'pkcs11_luna'
|
3
|
+
require File.join(File.dirname(__FILE__), 'config')
|
4
|
+
|
5
|
+
include PKCS11
|
6
|
+
|
7
|
+
#This example gets the mechanisms list and displays each mechanism's
|
8
|
+
#name and id
|
9
|
+
|
10
|
+
pkcs11 = Luna::Library.new
|
11
|
+
|
12
|
+
slot = Slot.new(pkcs11, SamplesConfig::SLOT)
|
13
|
+
mechanisms = slot.mechanisms
|
14
|
+
|
15
|
+
puts "Mechanisms(#{mechanisms.size}): "
|
16
|
+
mechanisms.each do |mech|
|
17
|
+
puts "#{Luna::MECHANISMS[mech]}: #{mech}"
|
18
|
+
end
|
19
|
+
|
20
|
+
pkcs11.close
|
@@ -0,0 +1,73 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'pkcs11_luna'
|
5
|
+
require File.join(File.dirname(__FILE__), 'config')
|
6
|
+
include PKCS11
|
7
|
+
|
8
|
+
#This example demonstrates the use of multiple threads and
|
9
|
+
#gathers some performance data. The NUMBER_OF_THREADS and TRANSACTIONS
|
10
|
+
#constants can be modified to gather more data points.
|
11
|
+
|
12
|
+
NUMBER_OF_THREADS = 20
|
13
|
+
TRANSACTIONS = 500
|
14
|
+
|
15
|
+
KEY_LABEL = "Ruby AES Key"
|
16
|
+
|
17
|
+
def destroy_object(session, label)
|
18
|
+
session.find_objects(:LABEL=>label) do |obj|
|
19
|
+
puts "Destroying object: #{obj.to_i}"
|
20
|
+
obj.destroy
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def process(slot, key)
|
25
|
+
session = slot.open
|
26
|
+
iv = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16].pack('C*')
|
27
|
+
mechanism = {:AES_CBC_PAD=>iv}
|
28
|
+
Thread.current[:start] = Time.now
|
29
|
+
(1..TRANSACTIONS).each do |i|
|
30
|
+
ciphertext = session.encrypt(mechanism, key, "Performance Test With Multiple Threads.")
|
31
|
+
end
|
32
|
+
Thread.current[:stop] = Time.now
|
33
|
+
session.close
|
34
|
+
end
|
35
|
+
|
36
|
+
pkcs11 = Luna::Library.new
|
37
|
+
|
38
|
+
slot = Slot.new(pkcs11, SamplesConfig::SLOT)
|
39
|
+
|
40
|
+
session = slot.open
|
41
|
+
session.login(:USER, SamplesConfig::PIN)
|
42
|
+
|
43
|
+
destroy_object(session, KEY_LABEL)
|
44
|
+
|
45
|
+
key = session.generate_key(:AES_KEY_GEN,
|
46
|
+
:CLASS=>CKO_SECRET_KEY, :ENCRYPT=>true, :DECRYPT=>true, :SENSITIVE=>true,
|
47
|
+
:TOKEN=>true, :VALUE_LEN=>32, :LABEL=>KEY_LABEL)
|
48
|
+
|
49
|
+
threads = []
|
50
|
+
|
51
|
+
(1..NUMBER_OF_THREADS).each do |n|
|
52
|
+
threads << Thread.new{ process(slot, key) }
|
53
|
+
end
|
54
|
+
|
55
|
+
threads.each do |t|
|
56
|
+
t.join
|
57
|
+
end
|
58
|
+
|
59
|
+
total_time = 0
|
60
|
+
threads.each do |t|
|
61
|
+
total_time += t[:stop] - t[:start]
|
62
|
+
end
|
63
|
+
|
64
|
+
elapsed_time = total_time / NUMBER_OF_THREADS
|
65
|
+
|
66
|
+
total = TRANSACTIONS*NUMBER_OF_THREADS
|
67
|
+
puts "Elapsed Time: " + sprintf('%.3f', elapsed_time)
|
68
|
+
puts "Total Number of Transactions: #{total}"
|
69
|
+
puts "Transactions Per Second: " + sprintf('%.3f', total/elapsed_time )
|
70
|
+
|
71
|
+
session.logout
|
72
|
+
session.close
|
73
|
+
pkcs11.close
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'pkcs11_luna'
|
3
|
+
require File.join(File.dirname(__FILE__), 'config')
|
4
|
+
include PKCS11
|
5
|
+
|
6
|
+
#This example obtains and displays the name and object handle
|
7
|
+
#of all objects
|
8
|
+
|
9
|
+
pkcs11 = Luna::Library.new
|
10
|
+
|
11
|
+
KEY_LABEL = "Ruby AES Key"
|
12
|
+
|
13
|
+
slot = Slot.new(pkcs11, SamplesConfig::SLOT)
|
14
|
+
session = slot.open(CKF_RW_SESSION | CKF_SERIAL_SESSION)
|
15
|
+
session.login(:USER, SamplesConfig::PIN)
|
16
|
+
|
17
|
+
session.find_objects() do |obj|
|
18
|
+
puts "#{obj[:LABEL]}: #{obj.to_i}"
|
19
|
+
end
|
20
|
+
|
21
|
+
session.logout
|
22
|
+
session.close
|
23
|
+
pkcs11.close
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'pkcs11_luna'
|
3
|
+
require File.join(File.dirname(__FILE__), 'config')
|
4
|
+
include PKCS11
|
5
|
+
|
6
|
+
#This example performs a digest on some data and proceeds to sign and verify the data
|
7
|
+
#with the signature
|
8
|
+
|
9
|
+
pkcs11 = Luna::Library.new
|
10
|
+
|
11
|
+
def destroy_object(session, label)
|
12
|
+
session.find_objects(:LABEL=>label) do |obj|
|
13
|
+
puts "Destroying object: #{obj.to_i}"
|
14
|
+
obj.destroy
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def get_data
|
19
|
+
data = ""
|
20
|
+
(0..2048).each do |i|
|
21
|
+
data << (i%26+65).chr
|
22
|
+
end
|
23
|
+
data
|
24
|
+
end
|
25
|
+
|
26
|
+
slot = Slot.new(pkcs11, SamplesConfig::SLOT)
|
27
|
+
session = slot.open(CKF_RW_SESSION | CKF_SERIAL_SESSION)
|
28
|
+
session.login(:USER, SamplesConfig::PIN)
|
29
|
+
|
30
|
+
pub_label = "Ruby RSA public key"
|
31
|
+
priv_label = "Ruby RSA private key"
|
32
|
+
destroy_object(session, pub_label)
|
33
|
+
destroy_object(session, priv_label)
|
34
|
+
|
35
|
+
pub_attr = {:ENCRYPT=>true, :VERIFY=>true, :MODULUS_BITS=>2048,
|
36
|
+
:TOKEN=>true, :WRAP=>true, :LABEL=>pub_label}
|
37
|
+
priv_attr = {:DECRYPT=>true, :SIGN=>true, :SENSITIVE=>true, :PRIVATE=>true, :TOKEN=>true,
|
38
|
+
:UNWRAP=>true, :LABEL=>pub_label}
|
39
|
+
|
40
|
+
pub_key, priv_key = session.generate_key_pair(:RSA_FIPS_186_3_PRIME_KEY_PAIR_GEN, pub_attr, priv_attr)
|
41
|
+
|
42
|
+
data = get_data
|
43
|
+
|
44
|
+
signature = session.sign(:SHA256_RSA_PKCS, priv_key, data)
|
45
|
+
puts "Signature: " + signature .bytes.map { |b| sprintf("%02X",b) }.join + " (#{signature.size})"
|
46
|
+
|
47
|
+
session.verify(:SHA256_RSA_PKCS, pub_key, signature, data)
|
48
|
+
|
49
|
+
puts "The signature was verified successfully"
|
50
|
+
|
51
|
+
session.logout
|
52
|
+
session.close
|
53
|
+
pkcs11.close
|
54
|
+
|
55
|
+
|
56
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'pkcs11_luna'
|
3
|
+
require File.join(File.dirname(__FILE__), 'config')
|
4
|
+
|
5
|
+
include PKCS11
|
6
|
+
|
7
|
+
#This example shows the label and token for all slots.
|
8
|
+
|
9
|
+
pkcs11 = Luna::Library.new
|
10
|
+
|
11
|
+
pkcs11.slots.each do |slot|
|
12
|
+
info = slot.info
|
13
|
+
puts "Slot: #{slot.to_i}"
|
14
|
+
puts " Label: #{info.slotDescription.strip}"
|
15
|
+
begin
|
16
|
+
info = slot.token_info
|
17
|
+
puts " Token: #{info.label}"
|
18
|
+
rescue CKR_TOKEN_NOT_PRESENT
|
19
|
+
puts " Token: No token"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
pkcs11.close
|
data/ext/extconf.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "mkmf"
|
2
|
+
require "rubygems"
|
3
|
+
|
4
|
+
inc, lib = dir_config('luna-dir', '/usr/safenet/lunaclient/samples')
|
5
|
+
puts "using Luna Client include:#{inc}"
|
6
|
+
|
7
|
+
|
8
|
+
find_header('pk11_struct_macros.h')
|
9
|
+
find_header('pk11_const_macros.h')
|
10
|
+
|
11
|
+
have_func("rb_thread_call_without_gvl")
|
12
|
+
have_func("rb_str_set_len")
|
13
|
+
|
14
|
+
create_makefile("pkcs11_luna_ext");
|