Class: Rack::TCTP::HALEC

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/tctp/halec.rb

Overview

HTTP application layer encryption channel. Used for the Trusted Cloud Transfer Protocol (TCTP)

Direct Known Subclasses

ClientHALEC, ServerHALEC

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ HALEC

Returns a new instance of HALEC.



58
59
60
# File 'lib/rack/tctp/halec.rb', line 58

def initialize(options = {})
  @url = options[:url] || nil
end

Instance Attribute Details

#async_threadObject (readonly)

The thread for encrypting and decrypting data of this HALEC



22
23
24
# File 'lib/rack/tctp/halec.rb', line 22

def async_thread
  @async_thread
end

#certificateObject (readonly)

A server or client certificate (if any)



19
20
21
# File 'lib/rack/tctp/halec.rb', line 19

def certificate
  @certificate
end

#engineObject (readonly)

The SSL engine



10
11
12
# File 'lib/rack/tctp/halec.rb', line 10

def engine
  @engine
end

#private_keyObject (readonly)

The private key for a certificate (if any)



16
17
18
# File 'lib/rack/tctp/halec.rb', line 16

def private_key
  @private_key
end

#urlObject

The URL of this HALEC



13
14
15
# File 'lib/rack/tctp/halec.rb', line 13

def url
  @url
end

Instance Method Details

#async_queueQueue

The queue for encrypting and decrypting data

Returns:

  • (Queue)

    The async queue



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
# File 'lib/rack/tctp/halec.rb', line 27

def async_queue
  unless @async_queue
    @async_queue = Queue.new

    @async_thread = Thread.new do
      begin
        while true
          item = @async_queue.pop

          case item[0]
            when :encrypt
              item[2].call encrypt_data(item[1])
            when :decrypt
              item[2].call decrypt_data(item[1])
            when :call
              item[2].call
          end
        end
      rescue Exception => e
        #TODO Handle HALEC encryption thread shutdown
        #TODO Handle OpenSSL error
        unless item[0] == :call
          item[2].call e
        end
      end
    end
  end

  @async_queue
end

#call_async(&block) ⇒ Object

Calls a given block asynchronously, considering the order of all calls to async methods.



139
140
141
# File 'lib/rack/tctp/halec.rb', line 139

def call_async(&block)
  async_queue.push [:call, nil, block]
end

#decrypt_data(encrypted) {|The| ... } ⇒ String

Decrypts encrypted data and either returns the plaintext or calls a block with it.

Parameters:

  • encrypted (String)

    The encrypted data

Yields:

  • Gives the plaintext to the block

Yield Parameters:

  • The (String)

    plaintext

Returns:

  • (String)

    The plaintext



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
# File 'lib/rack/tctp/halec.rb', line 102

def decrypt_data(encrypted, &decrypted)
  injected = @engine.inject encrypted

  unless injected
    exit -1
  end

  read_data = []

  begin
    while(read_chunk = @engine.read)
      read_data << read_chunk
    end
  rescue Exception => e
    unless @engine.state == 'SSLOK '
      raise e
    end
  end

  if block_given? then
    read_data.each do |data|
      decrypted.call data
    end
  else
    read_data.join
  end
end

#decrypt_data_async(encrypted) {|the| ... } ⇒ Object

Decrypts encrypted data asynchronously, yielding data to the block when done.

Parameters:

  • encrypted (String)

    The encrypted data

Yields:

  • Gives the decrypted data to the block

Yield Parameters:

  • the (String|Exception)

    decrypted data or an exception



134
135
136
# File 'lib/rack/tctp/halec.rb', line 134

def decrypt_data_async(encrypted, &decrypted)
  async_queue.push [:decrypt, encrypted, decrypted]
end

#encrypt_data(plaintext) {|The| ... } ⇒ String

Encrypts plaintext data and either returns the encrypted data or calls a block with it.

Parameters:

  • plaintext (String)

    The plaintext

Yields:

  • Gives the encrypted data to the block

Yield Parameters:

  • The (String)

    encrypted data

Returns:

  • (String)

    The encrypted data



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rack/tctp/halec.rb', line 67

def encrypt_data(plaintext, &encrypted)
  written = @engine.write plaintext

  if written < plaintext.length
    exit -1
  end

  read_data = []

  while(read_chunk = @engine.extract)
    read_data << read_chunk
  end

  if block_given?
    read_data.each do |data|
      encrypted.call data
    end
  else
    read_data.join
  end
end

#encrypt_data_async(plaintext) {|The| ... } ⇒ Object

Encrypts plaintext data asynchronously, yielding data to the block when done.

Parameters:

  • plaintext (String)

    The plaintext

Yields:

  • Gives the encrypted data to the block

Yield Parameters:

  • The (String|Exception)

    encrypted data or an exception



93
94
95
# File 'lib/rack/tctp/halec.rb', line 93

def encrypt_data_async(plaintext, &encrypted)
  async_queue.push [:encrypt, plaintext, encrypted]
end