Class: Rack::TCTP::HALEC
- Inherits:
-
Object
- Object
- Rack::TCTP::HALEC
- Defined in:
- lib/rack/tctp/halec.rb
Overview
HTTP application layer encryption channel. Used for the Trusted Cloud Transfer Protocol (TCTP)
Direct Known Subclasses
Instance Attribute Summary collapse
-
#async_thread ⇒ Object
readonly
The thread for encrypting and decrypting data of this HALEC.
-
#certificate ⇒ Object
readonly
A server or client certificate (if any).
-
#engine ⇒ Object
readonly
The SSL engine.
-
#private_key ⇒ Object
readonly
The private key for a certificate (if any).
-
#url ⇒ Object
The URL of this HALEC.
Instance Method Summary collapse
-
#async_queue ⇒ Queue
The queue for encrypting and decrypting data.
-
#call_async(&block) ⇒ Object
Calls a given block asynchronously, considering the order of all calls to async methods.
-
#decrypt_data(encrypted) {|The| ... } ⇒ String
Decrypts
encrypteddata and either returns the plaintext or calls a block with it. -
#decrypt_data_async(encrypted) {|the| ... } ⇒ Object
Decrypts
encrypteddata asynchronously, yielding data to the block when done. -
#encrypt_data(plaintext) {|The| ... } ⇒ String
Encrypts
plaintextdata and either returns the encrypted data or calls a block with it. -
#encrypt_data_async(plaintext) {|The| ... } ⇒ Object
Encrypts
plaintextdata asynchronously, yielding data to the block when done. -
#initialize(options = {}) ⇒ HALEC
constructor
A new instance of HALEC.
Constructor Details
#initialize(options = {}) ⇒ HALEC
Returns a new instance of HALEC.
58 59 60 |
# File 'lib/rack/tctp/halec.rb', line 58 def initialize( = {}) @url = [:url] || nil end |
Instance Attribute Details
#async_thread ⇒ Object (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 |
#certificate ⇒ Object (readonly)
A server or client certificate (if any)
19 20 21 |
# File 'lib/rack/tctp/halec.rb', line 19 def certificate @certificate end |
#engine ⇒ Object (readonly)
The SSL engine
10 11 12 |
# File 'lib/rack/tctp/halec.rb', line 10 def engine @engine end |
#private_key ⇒ Object (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 |
#url ⇒ Object
The URL of this HALEC
13 14 15 |
# File 'lib/rack/tctp/halec.rb', line 13 def url @url end |
Instance Method Details
#async_queue ⇒ Queue
The queue for encrypting and decrypting data
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.
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.
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.
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.
93 94 95 |
# File 'lib/rack/tctp/halec.rb', line 93 def encrypt_data_async(plaintext, &encrypted) async_queue.push [:encrypt, plaintext, encrypted] end |