Skip to content

Commit 8f7da40

Browse files
committed
Move parsing of Luna client header files to gem installation
In the pkcs11 base gem, we ship the PKCS#11 standard header files and link to the PKCS#11 ABI (not the API). Due to copyright constraints we're not allowed to distribute Luna client header files in the gem as we do in the pkcs11 gem. Instead we compile with the header files provided by the Luna client package, so we're using the API (not the ABI). So far we distibuted the struct names and constant name/value pairs extracted from the header files at build time. This however leads to inconsistencies between generated files at build time and the header files at install time, if the Luna client versions differ. Moving the parsers to extconf.rb ensures that generated structs and constants fit to the header files that are included by the compiler.
1 parent c6d1941 commit 8f7da40

File tree

5 files changed

+45
-41
lines changed

5 files changed

+45
-41
lines changed

pkcs11_luna/Manifest.txt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
Manifest.txt
44
README_LUNA.rdoc
55
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
166
examples/config.rb
177
examples/derive_aes_ecdh_key.rb
18-
examples/sign_verify.rb
198
examples/encrypt_decrypt_aes.rb
209
examples/encrypt_decrypt_rsa.rb
2110
examples/mechanism_list.rb
2211
examples/multithread.rb
2312
examples/objects_list.rb
13+
examples/sign_verify.rb
2414
examples/slot_info.rb
15+
ext/extconf.rb
16+
ext/generate_luna_constants.rb
17+
ext/generate_luna_structs.rb
18+
ext/pk11l.c
19+
lib/pkcs11_luna.rb
20+
lib/pkcs11_luna/extensions.rb
21+
test/app_id_helper.rb
22+
test/luna_helper.rb
23+
test/test_pkcs11_luna.rb
24+
test/test_pkcs11_luna_crypt.rb

pkcs11_luna/Rakefile

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,28 @@ require 'rake/extensiontask'
77
require 'rbconfig'
88

99
LUNA_INCLUDE_DIR = ENV['LUNA_INCLUDE_DIR'] || '/usr/safenet/lunaclient/samples/include'
10-
RUBY_PKCS11_EXT_DIR = File.expand_path('../ext')
10+
RUBY_PKCS11_DIR = File.expand_path('..')
1111

1212

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',
13+
SHARED_FILES = [
1814
'ext/pk11_struct_macros.h',
1915
'ext/pk11_const_macros.h',
2016
'ext/pk11_version.h',
17+
'ext/generate_structs.rb',
18+
'ext/generate_constants.rb',
19+
]
20+
GENERATED_FILES = [
21+
"ext/std_structs.rb"
2122
]
2223

24+
CLEAN.include SHARED_FILES
2325
CLEAN.include GENERATED_FILES
2426
CLEAN.include 'lib/pkcs11_luna_ext.so'
2527
CLEAN.include 'tmp'
2628
CLEAN.include 'examples/output'
2729

2830
def pkcs11_version
29-
file = File.join(RUBY_PKCS11_EXT_DIR, 'pk11_version.h')
31+
file = File.join(RUBY_PKCS11_DIR, 'ext/pk11_version.h')
3032
version_re = /VERSION += +([\"\'])([\d][\d\w\.]+)\1/
3133
File.read_utf(file)[version_re, 2]
3234
end
@@ -46,7 +48,7 @@ hoe = Hoe.spec 'pkcs11_luna' do
4648
self.extra_rdoc_files << self.readme_file << 'ext/pk11l.c'
4749
spec_extras[:extensions] = 'ext/extconf.rb'
4850
spec_extras[:files] = File.read_utf("Manifest.txt").split(/\r?\n\r?/)
49-
spec_extras[:files] += GENERATED_FILES
51+
spec_extras[:files] += SHARED_FILES + GENERATED_FILES
5052
spec_extras[:required_ruby_version] = '>= 2.2.0'
5153
end
5254

@@ -58,29 +60,20 @@ Rake::ExtensionTask.new('pkcs11_luna_ext', hoe.spec) do |ext|
5860
ext.config_options << "--with-luna-dir-include=\"#{LUNA_INCLUDE_DIR}\""
5961
end
6062

61-
def copy_from_base_task(filename)
62-
file File.join('ext', filename) => File.join(RUBY_PKCS11_EXT_DIR, filename) do |t|
63+
# Add shared file from base pkcs11 gem
64+
SHARED_FILES.each do |filename|
65+
file filename => File.join(RUBY_PKCS11_DIR, filename) do |t|
6366
cp t.prerequisites.first, t.name, verbose: true
6467
end
68+
file 'ext/extconf.rb' => filename
6569
end
6670

67-
copy_from_base_task 'pk11_struct_macros.h'
68-
copy_from_base_task 'pk11_const_macros.h'
69-
copy_from_base_task 'pk11_version.h'
70-
71-
HEADER_FILES = "#{LUNA_INCLUDE_DIR}/RSA/pkcs11t.h #{LUNA_INCLUDE_DIR}/cryptoki_v2.h"
72-
73-
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']
74-
file 'ext/pk11l_struct_def.inc' => 'ext/generate_structs.rb' do
75-
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}"
76-
end
77-
file 'ext/pk11l_struct_impl.inc' => 'ext/pk11l_struct_def.inc'
78-
file 'ext/pk11l_struct.doc' => 'ext/pk11l_struct_def.inc'
71+
file "ext/std_structs.rb" do |t|
72+
require "pkcs11"
73+
std_structs = PKCS11.constants.select{|c| PKCS11.const_get(c).respond_to?(:ancestors) && !(PKCS11.const_get(c).ancestors & [PKCS11::CStruct, PKCS11::CK_ATTRIBUTE]).empty? }
7974

80-
file 'ext/pk11l_const_def.inc' => 'ext/generate_constants.rb' do
81-
sh "#{RbConfig::CONFIG['ruby_install_name']} -I../lib ext/generate_constants.rb --const ext/pk11l_const_def.inc #{HEADER_FILES}"
75+
File.write t.name, "PKCS11_STD_STRUCTS = #{std_structs.inspect}"
8276
end
83-
file 'ext/pk11l.c' => ['ext/pk11l_struct_def.inc', 'ext/pk11l_struct_impl.inc', 'ext/pk11l_const_def.inc']
8477

8578
task doc_files: 'ext/pk11l_struct.doc'
8679

pkcs11_luna/ext/extconf.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@
44
inc, lib = dir_config('luna-dir', '/usr/safenet/lunaclient/samples')
55
puts "using Luna Client include:#{inc}"
66

7+
require_relative "generate_luna_constants"
8+
require_relative "generate_luna_structs"
9+
10+
header_files = [File.join(inc, "RSA/pkcs11t.h"), File.join(inc, "cryptoki_v2.h")]
11+
12+
args = ["--const", "pk11l_const_def.inc", *header_files]
13+
puts "running const parser with: #{args.join(" ")}"
14+
PKCS11::Luna::ConstantParser.run(args)
15+
16+
args = ["--def", "pk11l_struct_def.inc", "--impl", "pk11l_struct_impl.inc", "--doc", "pk11l_struct.doc", *header_files]
17+
puts "running struct parser with: #{args.join(" ")}"
18+
PKCS11::Luna::StructParser.run(args)
719

820
find_header('pk11_struct_macros.h')
921
find_header('pk11_const_macros.h')

pkcs11_luna/ext/generate_constants.rb renamed to pkcs11_luna/ext/generate_luna_constants.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Quick and dirty parser for PKCS#11 constants and
33
# generator for Ruby wrapper classes.
44

5-
require File.expand_path(File.join(File.dirname(__FILE__), '../../ext/generate_constants'))
5+
require_relative "generate_constants"
66

77
module PKCS11
88
module Luna
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
# Quick and dirty parser for PKCS#11 structs and
33
# generator for Ruby wrapper classes.
44

5-
require 'rubygems'
6-
require 'pkcs11'
7-
require File.expand_path(File.join(File.dirname(__FILE__), '../../ext/generate_structs'))
5+
require_relative "generate_structs"
6+
require_relative "std_structs"
87

98
module PKCS11
109
module Luna
@@ -63,7 +62,7 @@ def parse_files(files)
6362
def start!
6463
@structs = parse_files(options.files)
6564
@structs_by_name = @structs.inject({}){|sum, v| sum[v.name]=v; sum }
66-
@std_structs_by_name = PKCS11.constants.select{|c| PKCS11.const_get(c).respond_to?(:ancestors) && !(PKCS11.const_get(c).ancestors & [PKCS11::CStruct, PKCS11::CK_ATTRIBUTE]).empty? }.inject({}){|sum, v| sum[v.to_s]=true; sum }
65+
@std_structs_by_name = PKCS11_STD_STRUCTS.inject({}){|sum, v| sum[v.to_s]=true; sum }
6766

6867
write_files
6968
end

0 commit comments

Comments
 (0)