roostify_pkcs11_luna 0.2.5

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: feb72630e1eeafa2e9e140548ce5e5764a5a2927
4
+ data.tar.gz: 91e5fa56735cf20e889ec8ab2fe2377749807b0a
5
+ SHA512:
6
+ metadata.gz: 1cd90a4079b898b186a5e859c6d87dfa6292b2a7b352be4173fdf653993600374538881291c194d6b58e1a5d4f481ee9e17259daa47eea0f9a26b2e8431f7809
7
+ data.tar.gz: 085834d7ab130cfe82287dca2b73c48a7fe730803205ed33cd21a46ed94cbf771bfe0b4e5182ac3bb73551b9d8f58540cacf376fa39c1dd41eb9d6bb07597048
File without changes
@@ -0,0 +1 @@
1
+ --title "PKCS#11-Luna/Ruby Interface" --no-private lib/**/*.rb ext/*.c ext/*.doc
@@ -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
@@ -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.
@@ -0,0 +1,91 @@
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 'roostify_pkcs11_luna' do
35
+ developer('SafeNet', 'support@safenet-inc.com')
36
+ developer('Zee@Roostify', 'zee@roostify.com')
37
+ extra_deps << ['roostify-pkcs11', "= #{pkcs11_version}"]
38
+ extra_dev_deps << ['yard', '>= 0.6']
39
+ extra_dev_deps << ['rake-compiler', '>= 0.7']
40
+
41
+ self.urls = ['http://github.com/roostify/pkcs11']
42
+ self.summary = 'SafeNet-Luna extensions for PKCS#11-Ruby'
43
+ self.description = 'This module allows Ruby programs to use vendor extensions for SafeNet Luna.'
44
+ self.version = pkcs11_version
45
+
46
+ self.readme_file = 'README_LUNA.rdoc'
47
+ self.history_file = '../History.txt'
48
+ self.extra_rdoc_files << self.readme_file << 'ext/pk11l.c'
49
+ spec_extras[:extensions] = 'ext/extconf.rb'
50
+ spec_extras[:files] = File.read_utf("Manifest.txt").split(/\r?\n\r?/)
51
+ spec_extras[:files] += GENERATED_FILES
52
+ spec_extras[:has_rdoc] = 'yard'
53
+ end
54
+
55
+ ENV['RUBY_CC_VERSION'] ||= '1.8.7:1.9.3'
56
+
57
+ Rake::ExtensionTask.new('pkcs11_luna_ext', hoe.spec) do |ext|
58
+ ext.ext_dir = 'ext'
59
+ ext.cross_compile = true # enable cross compilation (requires cross compile toolchain)
60
+ ext.cross_platform = ['i386-mingw32'] # forces the Windows platform instead of the default one
61
+ puts "LUNA_INCLUDE_DIR: #{LUNA_INCLUDE_DIR.inspect}"
62
+ ext.config_options << "--with-luna-dir-include=\"#{LUNA_INCLUDE_DIR}\""
63
+ end
64
+
65
+ def copy_from_base_task(filename)
66
+ file File.join('ext', filename) => File.join(RUBY_PKCS11_EXT_DIR, filename) do |t|
67
+ cp t.prerequisites.first, t.name, :verbose=>true
68
+ end
69
+ end
70
+
71
+ copy_from_base_task 'pk11_struct_macros.h'
72
+ copy_from_base_task 'pk11_const_macros.h'
73
+ copy_from_base_task 'pk11_version.h'
74
+
75
+ HEADER_FILES = "#{LUNA_INCLUDE_DIR}/RSA/pkcs11t.h #{LUNA_INCLUDE_DIR}/cryptoki_v2.h"
76
+
77
+ 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']
78
+ file 'ext/pk11l_struct_def.inc' => 'ext/generate_structs.rb' do
79
+ sh "#{RbConfig::CONFIG['ruby_install_name']} ext/generate_structs.rb --def ext/pk11l_struct_def.inc --impl ext/pk11l_struct_impl.inc --doc ext/pk11l_struct.doc #{HEADER_FILES}"
80
+ end
81
+ file 'ext/pk11l_struct_impl.inc' => 'ext/pk11l_struct_def.inc'
82
+ file 'ext/pk11l_struct.doc' => 'ext/pk11l_struct_def.inc'
83
+
84
+ file 'ext/pk11l_const_def.inc' => 'ext/generate_constants.rb' do
85
+ sh "#{RbConfig::CONFIG['ruby_install_name']} ext/generate_constants.rb --const ext/pk11l_const_def.inc #{HEADER_FILES}"
86
+ end
87
+ file 'ext/pk11l.c' => ['ext/pk11l_struct_def.inc', 'ext/pk11l_struct_impl.inc', 'ext/pk11l_const_def.inc']
88
+
89
+ task :doc_files => 'ext/pk11l_struct.doc'
90
+
91
+ # vim: syntax=ruby
@@ -0,0 +1,5 @@
1
+ #These settings are used to control the examples.
2
+ module SamplesConfig
3
+ SLOT = 1
4
+ PIN = "userpin"
5
+ end
@@ -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