Class: CryptoToolchain::DiffieHellman::MITM

Inherits:
Peer
  • Object
show all
Defined in:
lib/crypto_toolchain/diffie_hellman/mitm.rb

Instance Attribute Summary collapse

Attributes inherited from Peer

#addresses, #channel, #debug, #g, #name, #p, #received_messages

Instance Method Summary collapse

Methods inherited from Peer

#add_address, #die_response, #encrypted_message_for, #info_for, #invalid_pubkey?, #my_address_message, #privkey, #process!, #pubkey, #send_msg, #valid_pubkey?, #when_ready

Constructor Details

#initialize(debug: false, name: "MITM", p: NIST_P, g: NIST_G, peer_a:, peer_b:, pubkey: nil) ⇒ MITM

Returns a new instance of MITM.



5
6
7
8
9
10
11
12
13
14
# File 'lib/crypto_toolchain/diffie_hellman/mitm.rb', line 5

def initialize(debug: false, name: "MITM", p: NIST_P, g: NIST_G, peer_a: , peer_b: , pubkey: nil)
  @peer_a = peer_a
  @peer_b = peer_b
  @pubkey = pubkey
  super(debug: debug, name: name, p: p, g: g)
  [peer_a, peer_b].each do |peer|
    puts "Adding #{peer.name} to #{name} at startup" if debug
    add_address(peer)
  end
end

Instance Attribute Details

#peer_aObject (readonly)

Returns the value of attribute peer_a.



49
50
51
# File 'lib/crypto_toolchain/diffie_hellman/mitm.rb', line 49

def peer_a
  @peer_a
end

#peer_bObject (readonly)

Returns the value of attribute peer_b.



49
50
51
# File 'lib/crypto_toolchain/diffie_hellman/mitm.rb', line 49

def peer_b
  @peer_b
end

Instance Method Details

#datum_response(msg) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/crypto_toolchain/diffie_hellman/mitm.rb', line 36

def datum_response(msg)
  data = msg.decrypt(key: info_for(msg.peer).session_key)
  puts "#{name} got message containing #{data} from #{msg.peer.name}" if debug
  other = other_peer(msg.peer)
  encrypted = encrypted_message_for(other, message: data, initial: msg.initial)
  send_msg(other, encrypted)
  @received_messages << ReceivedMessage.new(from: msg.peer.name, contents: data)
end

#do_key_exchangeObject



20
21
22
23
24
25
26
# File 'lib/crypto_toolchain/diffie_hellman/mitm.rb', line 20

def do_key_exchange
  msg = Messages::KeyExchange.new(peer: self, pubkey: pubkey, p: p, g: g, initial: true)
  [peer_a, peer_b].each do |peer|
    info_for(peer).update(p: p, g: g)
    send_msg(peer, msg)
  end
end

#key_exchange_response(msg) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/crypto_toolchain/diffie_hellman/mitm.rb', line 28

def key_exchange_response(msg)
  info = info_for(msg.peer)
  info.update(pubkey: msg.pubkey)
  secret_override = invalid_pubkey? ? 0 : nil
  info.set_shared_secret(privkey, override: secret_override)
  puts "#{name} generated secret #{info.shared_secret} for #{msg.peer.name}" if debug
end

#other_peer(peer) ⇒ Object



45
46
47
# File 'lib/crypto_toolchain/diffie_hellman/mitm.rb', line 45

def other_peer(peer)
  peer == peer_a ? peer_b : peer_a
end

#peer_address_response(msg) ⇒ Object



16
17
18
# File 'lib/crypto_toolchain/diffie_hellman/mitm.rb', line 16

def peer_address_response(msg)
  send_msg other_peer(msg.peer), my_address_message(initial: msg.initial)
end