Class: Themis::Ssession

Inherits:
Object
  • Object
show all
Extended by:
Gem::Deprecate
Includes:
ThemisCommon, ThemisImport
Defined in:
lib/rbthemis.rb

Constant Summary collapse

MAPPING =
{}
GetPubKeyByIDCallback =
FFI::Function.new(:int, [:pointer, :int, :pointer, :int, :pointer]) do |id_buf, id_length, pubkey_buf, pubkey_length, obj|
  pub_key = MAPPING[obj.read_uint64].get_pub_key_by_id(
    id_buf.get_bytes(0, id_length))
  return -1 unless pub_key
  pubkey_buf.put_bytes(0, pub_key)
  0
end

Constants included from ThemisImport

ThemisImport::THEMIS_KEY_EC_PRIVATE, ThemisImport::THEMIS_KEY_EC_PUBLIC, ThemisImport::THEMIS_KEY_INVALID, ThemisImport::THEMIS_KEY_RSA_PRIVATE, ThemisImport::THEMIS_KEY_RSA_PUBLIC

Instance Method Summary collapse

Methods included from ThemisImport

canonical_themis_paths, load_themis

Methods included from ThemisCommon

empty?, string_to_pointer_size

Constructor Details

#initialize(id, private_key, transport) ⇒ Ssession

Returns a new instance of Ssession.

Raises:



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/rbthemis.rb', line 304

def initialize(id, private_key, transport)
  id_buf, id_length = string_to_pointer_size(id)
  private_key_buf, private_key_length = string_to_pointer_size(private_key)

  @callbacks = CallbacksStruct.new
  @callbacks[:get_pub_key_for_id] = GetPubKeyByIDCallback

  MAPPING[transport.object_id] = transport
  @transport_obj_id = transport.object_id

  @callbacks[:user_data] = FFI::MemoryPointer.new(:uint64)
  @callbacks[:user_data].write_uint64(@transport_obj_id)

  @session = secure_session_create(
    id_buf, id_length, private_key_buf, private_key_length, @callbacks)

  raise ThemisError, 'Secure Session failed creating' if @session.null?
end

Instance Method Details

#connect_requestObject



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/rbthemis.rb', line 332

def connect_request
  connect_request_length = FFI::MemoryPointer.new(:uint)
  res = secure_session_generate_connect_request(
    @session, nil, connect_request_length)
  if res != BUFFER_TOO_SMALL
    raise(ThemisError,
          "Secure Session failed making connection request: #{res}")
  end
  connect_request = FFI::MemoryPointer.new(
    :char, connect_request_length.read_uint)
  res = secure_session_generate_connect_request(
    @session, connect_request, connect_request_length)
  if res != SUCCESS
    raise(ThemisError,
          "Secure Session failed making connection request: #{res}")
  end
  connect_request.get_bytes(0, connect_request_length.read_uint)
end

#established?Boolean

Returns:

  • (Boolean)


328
329
330
# File 'lib/rbthemis.rb', line 328

def established?
  secure_session_is_established @session
end

#finalizeObject

Raises:



394
395
396
397
# File 'lib/rbthemis.rb', line 394

def finalize
  res = secure_session_destroy(@session)
  raise ThemisError, 'Secure Session failed destroying' if res != SUCCESS
end

#is_establishedObject



323
324
325
# File 'lib/rbthemis.rb', line 323

def is_established
  established?
end

#unwrap(message) ⇒ Object



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/rbthemis.rb', line 351

def unwrap(message)
  message_, message_length_ = string_to_pointer_size message
  unwrapped_message_length = FFI::MemoryPointer.new(:uint)

  res = secure_session_unwrap(
    @session, message_, message_length_, nil, unwrapped_message_length)
  return SUCCESS, '' if res == SUCCESS
  if res != BUFFER_TOO_SMALL
    raise ThemisError, "Secure Session failed decrypting: #{res}"
  end

  unwrapped_message = FFI::MemoryPointer.new(
    :char, unwrapped_message_length.read_uint)
  res = secure_session_unwrap(@session, message_, message_length_,
                              unwrapped_message, unwrapped_message_length)
  if res != SUCCESS && res != SEND_AS_IS
    raise ThemisError, "Secure Session failed decrypting: #{res}"
  end

  [res, unwrapped_message.get_bytes(0, unwrapped_message_length.read_uint)]
end

#wrap(message) ⇒ Object



373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/rbthemis.rb', line 373

def wrap(message)
  message_, message_length_ = string_to_pointer_size(message)

  wrapped_message_length = FFI::MemoryPointer.new(:uint)
  res = secure_session_wrap(
    @session, message_, message_length_, nil, wrapped_message_length)
  if res != BUFFER_TOO_SMALL
    raise ThemisError, "Secure Session failed encrypting: #{res}"
  end

  wrapped_message = FFI::MemoryPointer.new(
    :char, wrapped_message_length.read_uint)
  res = secure_session_wrap(@session, message_, message_length_,
                            wrapped_message, wrapped_message_length)
  if res != SUCCESS && res != SEND_AS_IS
    raise ThemisError, "Secure Session failed encrypting: #{res}"
  end

  wrapped_message.get_bytes(0, wrapped_message_length.read_uint)
end