Class: CARPS::Mailer

Inherits:
Object
  • Object
show all
Defined in:
lib/carps/crypt/mailer.rb

Overview

High level CARPS mail client supporting strong cryptographic message signing.

It has knowledge of our own public and private key. Its big responsibility is turning Messages into Strings and signing them.

Instance Method Summary collapse

Constructor Details

#initialize(address, mailbox) ⇒ Mailer

The first parameter is the email address

The second the Mailbox.



51
52
53
54
55
56
57
58
59
# File 'lib/carps/crypt/mailer.rb', line 51

def initialize address, mailbox
   @addr = address
   @mailbox = mailbox
   @private_key = get_keys
   @public_key = @private_key.public_key
   @current_handshakes = Set.new 
   # Load the old peers
   load_peers
end

Instance Method Details

#addressObject

Give our address to interested parties



130
131
132
# File 'lib/carps/crypt/mailer.rb', line 130

def address
   @addr
end

#check(type, must_be_from = nil) ⇒ Object

Check for a message. Don’t block! Return nil if nothing is available.



158
159
160
# File 'lib/carps/crypt/mailer.rb', line 158

def check type, must_be_from=nil
   @mailbox.check type, must_be_from
end

#check_handshakeObject

Check for handshakes



95
96
97
# File 'lib/carps/crypt/mailer.rb', line 95

def check_handshake
   @mailbox.insecure_check Handshake
end

#handle_handshake(handshake) ⇒ Object

Respond to a handshake request



100
101
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
# File 'lib/carps/crypt/mailer.rb', line 100

def handle_handshake handshake
   # Get the peer's address
   from = handshake.from
   puts "Receiving handshake request from #{from}."
   if @mailbox.peer? from
      UI::warn "Handshake request from #{from} has been dropped because #{from} is already a known peer",  "Possible spoofing attack."
   else
      # See if the user accepts the handshake.
      accept = accept_handshake? from
      if accept
         Thread.fork do
            @current_handshakes.add from
            # Send our key to the peer
            send from, PublicKey.new(@public_key)
            # Get their key
            peer_key = @mailbox.insecure_read PublicKey, from
            # Create a new peer
            peer = Peer.new from
            @mailbox.add_peer peer
            peer.your_key peer_key.key
            peer.save
            # Send an okay message
            send from, AcceptHandshake.new
            puts "Established spoof-proof communications with #{from}."
         end
      end
   end
end

#handshake(to) ⇒ Object

Perform a handshake to authenticate with a peer



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/carps/crypt/mailer.rb', line 62

def handshake to
   if @mailbox.peer? to
      puts "No need for handshake: " + to + " is already a known peer."
   else
      puts "Offering cryptographic handshake to #{to}"
      @current_handshakes.add to
      # Create a new peer
      peer = Peer.new to
      @mailbox.add_peer peer
      # Request a handshake 
      send to, Handshake.new
      # Get the peer's key
      their_key = @mailbox.insecure_read PublicKey, to
      peer.your_key their_key.key
      peer.save 
      # Send our key
      send to, PublicKey.new(@public_key)
      # Receive an okay message
      #
      # Has to be insecure for now... :(
      #
      # This is because the client may not know the session yet.
      @mailbox.insecure_read AcceptHandshake, to
      puts "Established spoof-proof communications with #{to}"
   end
end

#read(type, must_be_from = nil) ⇒ Object

Receive a message. Block until it is here.



153
154
155
# File 'lib/carps/crypt/mailer.rb', line 153

def read type, must_be_from=nil
   @mailbox.read type, must_be_from
end

#send(to, message) ⇒ Object

Send a message



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/carps/crypt/mailer.rb', line 135

def send to, message
   unless @mailbox.peer?(to) or @current_handshakes.include?(to)
      Thread.fork do
         handshake to
      end
   end
   text = message.emit
   # The mailbox tags the message with a session key
   text = @mailbox.tag text
   # Sign the message
   digest = Digest::MD5.digest text
   sig = @private_key.syssign digest
   mail = (V.addr @addr) + (V.sig sig) + text + K.end
   @mailbox.send to, mail
   puts "#{message.class} sent to " + to
end

#shutdownObject

Shutdown the mailer



90
91
92
# File 'lib/carps/crypt/mailer.rb', line 90

def shutdown
   @mailbox.shutdown
end