Module: Thales::Pse::Cli::Token

Includes:
TR::CliUtils, TR::CondUtils, Thales::Pse::Cli
Included in:
Token
Defined in:
lib/thales/pse/token.rb

Instance Method Summary collapse

Methods included from Thales::Pse::Cli

#cli_exec, #logger

Instance Method Details

#export_cert(label, outFile, &block) ⇒ Object

Raises:



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/thales/pse/token.rb', line 274

def export_cert(label, outFile, &block)

  raise TokenError, "Key label cannot be empty" if is_empty?(label) 
  raise TokenError, "Output file cannot be empty" if is_empty?(outFile) 
  raise TokenError, "Slot is not available!" if is_empty?(@slot)

  slot = @slot.id
  label.gsub!(" ","_")

  begin

    cli_exec("ctcert") do |ops|
      case ops
      when :params
        ["x","-l#{label}","-s#{slot}","-f#{outFile}"]
      end
    end

  rescue ExecutableNotFoundError
    raise Error, "Executable 'ctcert' not found from PATH. Please install the driver or add the executable to PATH"
  end


end

#gencert(*args, &block) ⇒ Object

Raises:



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/thales/pse/token.rb', line 180

def gencert(*args, &block)
  
  raise TokenError, "Block is required" if not block
  raise TokenError, "Slot is not available!" if is_empty?(@slot)

  expect = [
    { matcher: "Enter user PIN.+:", block_key: :token_user_pin, timeout: 60 }
  ]

  keylabel = block.call(:keylabel)
  raise Error, "Keylabel should not be empty" if is_empty?(keylabel)
  keylabel.gsub!(" ","_")

  validFrom = block.call(:valid_from)
  #if is_empty?(validFrom) or not validFrom.is_a?(Time)
  #  today = Time.now
  #  validFrom = Time.new(today.year, today.month, today.day)
  #end
  validity = block.call(:validity)
  if not (validity =~ /h|d|m|y/)
    raise Error, "Validity requires unit in hour (h), day (d), month (m) or year (y)"
  end
  validTo = block.call(:valid_to)
  raise Error, "Valid_to requires a time object" if (not_empty?(validTo) and not validTo.is_a?(Time))

  #slot = block.call(:slot) || 0
  slot = @slot.id
  certFile = block.call(:cert_file)

  cn = block.call(:common_name)
  raise Error, "Common name is mandatory" if is_empty?(cn)
  expect << { matcher: "Common Name:", block_key: :cn, timeout: 5 }
  org = block.call(:org)
  expect << { matcher: "Organization:", block_key: :org, timeout: 5 }
  ou = block.call(:ou)
  expect << { matcher: "Organizational Unit:", block_key: :ou, timeout: 5 }
  loc = block.call(:locality)
  expect << { matcher: "Locality:", block_key: :loc, timeout: 5 }
  st = block.call(:state)
  expect << { matcher: "State:", block_key: :st, timeout: 5 }
  ctry = block.call(:country)
  expect << { matcher: "Country:", block_key: :ctry, timeout: 5 }
  sn = block.call(:serial_no) || SecureRandom.uuid.gsub("-","")
  expect << { matcher: "certificate\'s serial number.+:", block_key: :sn, timeout: 5 }

  params = ["c","-l#{keylabel}", "-s#{slot}"]
  if not_empty?(validFrom)
    params << "-b#{validFrom.strftime("%Y%m%d%H%M%S")}"
  end
  if not_empty?(validity)
    params << "-d#{validity}"
  end
  if not_empty?(validTo)
    params << "-e#{validTo.strftime("%Y%m%d%H%M%S")}"
  end

  begin

    cli_exec("ctcert") do |ops, val|
      case ops
      when :params
        params
      when :expect_list
        #[expect[0]]
        expect
      when :cn
        cn
      when :org
        org
      when :ou
        ou
      when :loc
        loc
      when :st
        st
      when :ctry
        ctry
      when :sn
        sn
      else
        block.call(ops)
      end
    end

  rescue ExecutableNotFoundError
    raise Error, "Executable 'ctkmu' not found from PATH. Please install the driver or add the executable to PATH"
  end

  if not_empty?(certFile)
    export_cert(keylabel, certFile)
  end

end

#genkey(*args, &block) ⇒ Object

Raises:



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/thales/pse/token.rb', line 95

def genkey(*args, &block)

  raise TokenError, "Block is required" if not block
  raise TokenError, "Slot is not available!" if is_empty?(@slot)

  expect = [
    { matcher: "Enter user PIN.+:", block_key: :token_user_pin, timeout: 1, timeout_after: 60*5 }
  ]

  type = args.first
  case type
  when :rsa
  else
    raise Error, "Unsupported key type #{type}"
  end

  #slot = block.call(:slot) || 0
  slot = @slot.id

  keylabel = block.call(:keylabel) 
  raise Error, "Keylabel is required " if is_empty?(keylabel)
  keylabel.gsub!(" ","_")

  attr = block.call(:attr)
  raise Error, "Attributes (attr) is required " if is_empty?(attr)
  attr = [attr] if not attr.is_a?(Array)
  atRes = []
  attr.each do |at|
    case at
    when :private
      atRes << "P"
    when :modifiable
      atRes << "M"
    when :sensitive
      atRes << "T"
    when :wrap
      atRes << "W"
    when :export
      atRes << "w"
    when :import
      atRes << "I"
    when :unwrap
      atRes << "U"
    when :extractable
      atRes << "X"
    when :exportable
      atRes << "x"
    when :derive
      atRes << "R"
    when :encrypt
      atRes << "E"
    when :decrypt
      atRes << "D"
    when :sign
      atRes << "S"
    when :verify
      atRes << "V"
    when :sign_local_cert
      atRes << "L"
    when :usage_count
      atRes << "C"
    end
  end

  keysize = block.call(:keysize) || 2048

  begin

    cli_exec("ctkmu") do |ops, val|
      case ops
      when :params
        ["c","-t#{type}","-s#{slot}","-n#{keylabel}","-a#{atRes.join}","-z#{keysize}"]
      when :expect_list
        expect
      else
        block.call(ops)
      end
    end

  rescue ExecutableNotFoundError
    raise Error, "Executable 'ctkmu' not found from PATH. Please install the driver or add the executable to PATH"
  end

end

#init_token(*args, &block) ⇒ Object



13
14
15
16
17
18
# File 'lib/thales/pse/token.rb', line 13

def init_token(*args, &block)
  
  init_token_so(&block)
  init_token_user(&block)

end

#init_token_so(*args, &block) ⇒ Object

Raises:



20
21
22
23
24
25
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
# File 'lib/thales/pse/token.rb', line 20

def init_token_so(*args, &block)

  raise TokenError, "Block is required" if not block
  raise TokenError, "Slot is not available!" if is_empty?(@slot)

  expect = [
    { matcher: "new token label:", block_key: :token_label, timeout: 5 },
    { matcher: "enter Security Officer\'s pin:", block_key: :token_so_pin, timeout: 5 },
    { matcher: "confirm Security Officer\'s pin:", block_key: :token_so_pin_confirm, timeout: 5 }
  ]

  #slot = block.call(:slot) || 0
  slot = @slot.id

  begin

    cli_exec("ctconf") do |ops|
      case ops
      when :params
        ["-n#{slot}"]
      when :expect_list
        expect
      else
        block.call(ops)
      end
    end

  rescue ExecutableNotFoundError
    raise Error, "Executable 'ctconf' not found from PATH. Please install the driver or add the executable to PATH"
  end

end

#init_token_user(*args, &block) ⇒ Object

Raises:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/thales/pse/token.rb', line 53

def init_token_user(*args, &block)

  raise TokenError, "Block is required" if not block
  raise TokenError, "Slot is not available!" if is_empty?(@slot)

  expect = [
    # this key is handled locally not passed to external
    { matcher: "Security Officer PIN.+:|current user PIN.+:", block_key: :auth_pin, timeout: 5 },
    { matcher: "enter the new user PIN.+:", block_key: :token_user_pin, timeout: 5 },
    { matcher: "confirm the new user PIN.+:", block_key: :token_user_pin_confirm, timeout: 5 }
  ]

  #slot = block.call(:slot) || 0
  slot = @slot.id

  begin

    cli_exec("ctkmu") do |ops, val|
      case ops
      when :params
        ["p","-s#{slot}"]
      when :expect_list
        expect
      when :auth_pin
        if not_empty?(val)
          if val.join =~ /Security Officer/
            block.call(:token_so_pin)
          else
            block.call(:token_user_pin)
          end
        end
      else
        block.call(ops)
      end
    end

  rescue ExecutableNotFoundError
    raise Error, "Executable 'ctconf' not found from PATH. Please install the driver or add the executable to PATH"
  end

end