Module: Tem::SeClosures

Includes:
MixedMethods
Included in:
Session
Defined in:
lib/tem/seclosures.rb

Defined Under Namespace

Modules: MixedMethods

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MixedMethods

#assemble

Class Method Details

.included(klass) ⇒ Object



11
12
13
# File 'lib/tem/seclosures.rb', line 11

def self.included(klass)
  klass.extend MixedMethods
end

Instance Method Details

#execute(secpack, key_id = 0) ⇒ Object



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
67
68
69
70
71
72
73
74
75
# File 'lib/tem/seclosures.rb', line 37

def execute(secpack, key_id = 0)
  # load SECpack

  buffer_id = post_buffer(secpack.tem_formatted_body)
  response = @transport.iso_apdu! :ins => 0x50, :p1 => buffer_id,
                                                   :p2 => key_id
  tem_secpack_error(response) if read_tem_byte(response, 0) != 1
  
  # execute SEC

  sec_exception = nil
  loop do 
    response = @transport.iso_apdu! :ins => 0x52
    sec_status = read_tem_byte(response, 0)
    case sec_status
    when 2 # success

      break
    when 3 # exception

      # there is an exception, try to collect the trace

      b_stat = stat_buffers() rescue nil
      k_stat = stat_keys() rescue nil
      trace = sec_trace()
      sec_exception = Tem::SecExecError.new secpack, trace, b_stat, k_stat
      break
    when 4 # persistent store fault

      solve_psfault
    else
      raise "Unrecognized execution engine status #{sec_status}"
    end
  end

  # unbind SEC

  response = @transport.iso_apdu! :ins => 0x51
  raise sec_exception if sec_exception
  buffer_id = read_tem_byte(response, 0)
  buffer_length = read_tem_short(response, 1)
  data_buffer = read_buffer buffer_id
  release_buffer buffer_id
  
  return data_buffer[0...buffer_length]
end

#sec_traceObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/tem/seclosures.rb', line 15

def sec_trace
  #begin      

    trace = @transport.iso_apdu! :ins => 0x54
    if trace.length > 2
      case read_tem_short(trace, 0) # trace version

      when 1
        return {:sp => read_tem_short(trace, 2), :ip => read_tem_short(trace, 4),
                :out => read_tem_short(trace, 6), :pscell =>  read_tem_short(trace, 8)}
      end        
    end
    return nil # unreadable trace

  #rescue

  #  return nil

  #end

end

#solve_psfaultObject



31
32
33
34
35
# File 'lib/tem/seclosures.rb', line 31

def solve_psfault
  # TODO: better strategy, lol

  next_cell = rand(16)
  @transport.iso_apdu! :ins => 0x53, :p12 => to_tem_ushort(next_cell)
end