Class: Tem::OpenSSL::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/ossl/executor.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, test_options) ⇒ Executor

Returns a new instance of Executor.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ossl/executor.rb', line 4

def initialize(args, test_options)
  @args = args
  # unknown args get thrown here
  @arg_bag = {}
  # read key from here
  @in_key = nil 
  # read (original) data from here
  @in_data = nil
  # read input from here
  @in = $stdin
  # dump output here
  @out = $stdout
  # run the procs here to clean up
  @cleanup_procs = []
  
  # hash of flags to help unit tests
  @test_options = test_options
  
  connect_to_tem
  parse_args
end

Class Method Details

.run(args, test_options = {}) ⇒ Object



113
114
115
116
117
# File 'lib/ossl/executor.rb', line 113

def self.run(args, test_options = {})
  ex = self.new args, test_options
  ex.run
  ex.cleanup
end

Instance Method Details

#cleanupObject



91
92
93
# File 'lib/ossl/executor.rb', line 91

def cleanup
  @cleanup_procs.each { |p| p.call }
end

#connect_to_temObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/ossl/executor.rb', line 95

def connect_to_tem
  @terminal = Tem::SCard::JCOPRemoteTerminal.new
  if !@terminal.connect or @test_options[:no_tem]
    @terminal.disconnect
    @terminal = Tem::SCard::PCSCTerminal.new
    if !@terminal.connect or @test_options[:no_tem]
      @terminal.disconnect
      @terminal = nil
    end
  end
  unless @terminal.nil?
    @javacard = Tem::SCard::JavaCard.new(@terminal)
    @tem = Tem::Session.new(@javacard)
  
    @cleanup_procs << Proc.new { @tem.disconnect; @terminal.disconnect }
  end
end

#parse_argsObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ossl/executor.rb', line 68

def parse_args
  0.upto(@args.length - 1) do |i|
    # the tokens that don't start with - are processed OOB
    next unless @args[i][0] == ?-
    case @args[i]
    when '-in'
      @in = File.open(@args[i + 1], 'rb')
      @cleanup_procs << Proc.new { @in.close }
    when '-inkey'
      @in_key = File.open(@args[i + 1], 'r')
      @cleanup_procs << Proc.new { @in_key.close }
    when '-indata'
      @in_data = File.open(@args[i + 1], 'r')
      @cleanup_procs << Proc.new { @in_data.close }
    when '-out'
      @out = File.open(@args[i + 1], 'wb')
      @cleanup_procs << Proc.new { @out.close }
    else
      @arg_bag[@args[i][1..-1].to_sym] = true
    end
  end
end

#runObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ossl/executor.rb', line 26

def run    
  case @args[0]
  when 'reset'
    @tem.kill
    @tem.activate
    @tem.emit
  when 'rsa'
    if @arg_bag[:pubout]
      @key = Tem::OpenSSL::Key.load_from_tkfile @in
      @out.write @key.pub_key.ssl_key.to_s
    end
  when 'rsagen'
    @key = Tem::OpenSSL::Key.new_tem_key @tem
    @out.write @key.to_tkfile
  when 'rsautl'
    @key = Tem::OpenSSL::Key.load_from_tkfile @in_key
    data = @in.read
    case
  when @arg_bag[:decrypt]
      # decrypting with private key
      result = @key.privk_decrypt data, @tem
    when @arg_bag[:encrypt]
      # encrypting with public key
      result = @key.pub_key.encrypt data
    when @arg_bag[:sign]
      # fake-signing (encrypting with private key)
      result = @key.privk_encrypt data, @tem
    when @arg_bag[:verify]
      # fake-verifying (decrypting with public key)
      result = @key.pub_key.decrypt data
    when @arg_bag[:xsign]
      result = @key.privk_sign data, @tem
    when @arg_bag[:xverify]
      orig_data = @in_data.read
      result = @key.pub_key.verify orig_data, data
    else
      # ?!
    end
    @out.write result
  end    
end