Class: RubyTls::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-tls/connection.rb

Defined Under Namespace

Classes: Callback, Callbacks, DataCallback

Constant Summary collapse

CALLBACKS =
[:close_cb, :verify_cb, :dispatch_cb, :transmit_cb, :handshake_cb].freeze

Instance Method Summary collapse

Constructor Details

#initialize(callback_obj = nil) {|_self| ... } ⇒ Connection

Initializes the State instance.

Yields:

  • (_self)

Yield Parameters:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ruby-tls/connection.rb', line 10

def initialize(callback_obj = nil)
    @state = ::RubyTls::State.new
    @callbacks = Callbacks.new      # so GC doesn't clean them up on java
    @started = false

    # Attach callbacks if there is an object passed in to handle the callbacks
    if not callback_obj.nil?
        CALLBACKS.each do |callback|
            self.__send__(callback, &callback_obj.method(callback)) if callback_obj.respond_to? callback
        end
    end

    yield self if block_given?
end

Instance Method Details

#cleanupObject



84
85
86
# File 'lib/ruby-tls/connection.rb', line 84

def cleanup
    ::RubyTls.cleanup(@state)
end

#close_cb(&block) ⇒ Object



25
26
27
28
29
# File 'lib/ruby-tls/connection.rb', line 25

def close_cb(&block)
    cb = Callback.new(@callbacks, &block)
    @callbacks[:close_cb] = cb
    @state[:close_cb] = cb
end

#decrypt(data) ⇒ Object



80
81
82
# File 'lib/ruby-tls/connection.rb', line 80

def decrypt(data)
    ::RubyTls.decrypt_data(@state, data, data.length)
end

#dispatch_cb(&block) ⇒ Object



44
45
46
47
48
# File 'lib/ruby-tls/connection.rb', line 44

def dispatch_cb(&block)
    cb = DataCallback.new(@callbacks, &block)
    @callbacks[:dispatch_cb] = cb
    @state[:dispatch_cb] = cb
end

#encrypt(data) ⇒ Object



76
77
78
# File 'lib/ruby-tls/connection.rb', line 76

def encrypt(data)
    ::RubyTls.encrypt_data(@state, data, data.length)
end

#handshake_cb(&block) ⇒ Object



56
57
58
59
60
# File 'lib/ruby-tls/connection.rb', line 56

def handshake_cb(&block)
    cb = Callback.new(@callbacks, &block)
    @callbacks[:handshake_cb] = cb
    @state[:handshake_cb] = cb
end

#start(args = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ruby-tls/connection.rb', line 63

def start(args = {})
    return if @started

    server, priv_key, cert_chain, verify_peer = args.values_at(:server, :private_key_file, :cert_chain_file, :verify_peer)
    [priv_key, cert_chain].each do |file|
        next if file.nil? or file.empty?
        raise FileNotFoundException,
        "Could not find #{file} to start tls" unless File.exists? file
    end
    @started = true
    ::RubyTls.start_tls(@state, server || false, priv_key || '', cert_chain || '', verify_peer || !!@callbacks[:verify_cb])
end

#transmit_cb(&block) ⇒ Object



50
51
52
53
54
# File 'lib/ruby-tls/connection.rb', line 50

def transmit_cb(&block)
    cb = DataCallback.new(@callbacks, &block)
    @callbacks[:transmit_cb] = cb
    @state[:transmit_cb] = cb
end

#verify_cbObject



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ruby-tls/connection.rb', line 31

def verify_cb
    cb = ::FFI::Function.new(:int, [::RubyTls::State.ptr, :string]) do |state, cert|
        begin
            yield(cert) == true ? 1 : 0
        rescue
            # TODO:: Provide some debugging output
            0
        end
    end
    @callbacks[:verify_cb] = cb
    @state[:verify_cb] = cb
end