Class: Dropzone::Session
- Inherits:
-
Object
show all
- Defined in:
- lib/dropzone/session.rb
Defined Under Namespace
Classes: DerAlreadyExists, InvalidCommunication, InvalidWithReceiver, MissingReceiver, SessionInvalid, Unauthenticated
Constant Summary
collapse
- CIPHER_ALGORITHM =
'AES-256-CBC'
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(priv_key, session_secret, options = {}) ⇒ Session
Returns a new instance of Session.
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/dropzone/session.rb', line 14
def initialize(priv_key, session_secret, options = {})
@priv_key, @session_key = priv_key, OpenSSL::BN.new(session_secret, 16)
@end_block = options[:end_block] if options.has_key? :end_block
case
when options.has_key?(:receiver_addr)
@receiver_addr = options[:receiver_addr]
when options.has_key?(:with)
raise InvalidWithReceiver unless options[:with].receiver_addr == sender_addr
@with = options[:with]
@receiver_addr = @with.sender_addr
else
raise MissingReceiver
end
end
|
Instance Attribute Details
#end_block ⇒ Object
Returns the value of attribute end_block.
12
13
14
|
# File 'lib/dropzone/session.rb', line 12
def end_block
@end_block
end
|
#priv_key ⇒ Object
Returns the value of attribute priv_key.
12
13
14
|
# File 'lib/dropzone/session.rb', line 12
def priv_key
@priv_key
end
|
#receiver_addr ⇒ Object
Returns the value of attribute receiver_addr.
12
13
14
|
# File 'lib/dropzone/session.rb', line 12
def receiver_addr
@receiver_addr
end
|
#session_key ⇒ Object
Returns the value of attribute session_key.
12
13
14
|
# File 'lib/dropzone/session.rb', line 12
def session_key
@session_key
end
|
#with ⇒ Object
Returns the value of attribute with.
12
13
14
|
# File 'lib/dropzone/session.rb', line 12
def with
@with
end
|
Class Method Details
.all(addr) ⇒ Object
155
156
157
|
# File 'lib/dropzone/session.rb', line 155
def all(addr)
blockchain.messages_by_addr(addr, type: 'COMMUN').find_all(&:is_init?)
end
|
Instance Method Details
#authenticate!(der = nil) ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/dropzone/session.rb', line 59
def authenticate!(der = nil)
is_init = (communication_init.nil? || authenticated?)
raise DerAlreadyExists unless der.nil? if !is_init
der ||= (with) ? with.der : 1024
dh = OpenSSL::PKey::DH.new der
dh.priv_key = session_key
dh.generate_key!
communicate! session_pkey: [dh.pub_key.to_s(16)].pack('H*'),
der: (is_init) ? dh.public_key.to_der : nil
end
|
#authenticated? ⇒ Boolean
92
93
94
|
# File 'lib/dropzone/session.rb', line 92
def authenticated?
communication_init && communication_auth
end
|
#blockchain ⇒ Object
33
|
# File 'lib/dropzone/session.rb', line 33
def blockchain; self.class.blockchain; end
|
#communication_auth ⇒ Object
This is the response to the init
102
103
104
105
106
107
|
# File 'lib/dropzone/session.rb', line 102
def communication_auth
commun_messages.find{|c|
break if c.is_init?
c.is_auth? }
end
|
#communication_init ⇒ Object
96
97
98
99
|
# File 'lib/dropzone/session.rb', line 96
def communication_init
commun_messages.find(&:is_init?)
end
|
#communications ⇒ Object
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/dropzone/session.rb', line 114
def communications
if authenticated?
communications = commun_messages(
start_block: communication_init.block_height ).reject(&:is_auth?)
communications.each{|c| c.symm_key = symm_key}
communications
else
[]
end
end
|
#send(contents, iv = nil) ⇒ Object
Also known as:
<<
Iv passing is supported only for the purpose of making tests completely deterministic
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/dropzone/session.rb', line 38
def send(contents, iv = nil)
raise Unauthenticated unless authenticated?
aes = OpenSSL::Cipher::Cipher.new CIPHER_ALGORITHM
aes.encrypt
iv ||= aes.random_iv
aes.iv = iv
aes.key = symm_key
cipher = aes.update contents
cipher << aes.final
communicate! contents: cipher.to_s, iv: iv
end
|
#sender_addr ⇒ Object
34
|
# File 'lib/dropzone/session.rb', line 34
def sender_addr; blockchain.privkey_to_addr priv_key; end
|
#symm_key ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/dropzone/session.rb', line 78
def symm_key
return @symm_key if @symm_key
return nil unless communication_init && communication_auth
dh = OpenSSL::PKey::DH.new communication_init.der
dh.priv_key = session_key
dh.generate_key!
@symm_key = dh.compute_key OpenSSL::BN.new(
their_pkey.session_pkey.unpack('H*').first, 16)
end
|
#their_pkey ⇒ Object
109
110
111
112
|
# File 'lib/dropzone/session.rb', line 109
def their_pkey
[communication_init, communication_auth].find{|c|
c.sender_addr == receiver_addr && c.receiver_addr == sender_addr }
end
|