diff options
-rwxr-xr-x | hexnoise.py | 5 | ||||
-rwxr-xr-x | pairing.py | 84 |
2 files changed, 52 insertions, 37 deletions
diff --git a/hexnoise.py b/hexnoise.py index 55a2be2..f2c1790 100755 --- a/hexnoise.py +++ b/hexnoise.py @@ -208,8 +208,6 @@ class NoiseEngine: self.proto.start_handshake() self.paired = False self.connected = False - self.packetizer.send_packet(PacketType.INITIATE_HANDSHAKE, b'') - self.debug_print('Handshake started') @wraps(print) def debug_print(self, *args, **kwargs): @@ -217,6 +215,9 @@ class NoiseEngine: print(*args, **kwargs) def perform_handshake(self): + self.packetizer.send_packet(PacketType.INITIATE_HANDSHAKE, b'') + self.debug_print('Handshake started') + while True: if self.proto.handshake_finished: break @@ -10,9 +10,9 @@ from gi.repository import Gtk, Gdk, Pango, GLib import hexnoise class PairingWindow(Gtk.Window): - def __init__(self, serial, debug=False): + def __init__(self, noise, debug=False): Gtk.Window.__init__(self, title='SecureHID pairing') - self.serial = serial + self.noise = noise self.debug = debug self.set_border_width(10) @@ -36,9 +36,6 @@ class PairingWindow(Gtk.Window): self.handshaker.start() def pair(self): - self.packetizer = hexnoise.Packetizer(self.serial, debug=self.debug) - self.noise = hexnoise.NoiseEngine(self.packetizer, debug=self.debug) - for i in range(10): try: self.run_handshake() @@ -47,34 +44,56 @@ class PairingWindow(Gtk.Window): print(e) def run_handshake(self): - self.noise.perform_handshake() - - if not self.noise.paired: - binding_incantation = self.noise.channel_binding_incantation() - GLib.idle_add(self.label.set_markup, - f'<b>Step 2</b>\n\nPerform channel binding ritual.\n' - f'Enter the following incantation, then press enter.\n' - f'<b>{binding_incantation}</b>') - - def update_text(text): - self.entry.set_text(text) - self.entry.set_position(len(text)) - - clean = lambda s: re.sub('[^a-z0-9-]', '', s.lower()) - if clean(binding_incantation).startswith(clean(text)): - color = 0.9, 1.0, 0.9 # light red - else: - color = 1.0, 0.9, 0.9 # light green - self.entry.override_background_color(Gtk.StateType.NORMAL, Gdk.RGBA(*color, 1.0)) - + binding_incantation = self.noise.channel_binding_incantation() + GLib.idle_add(self.label.set_markup, + f'<b>Step 2</b>\n\nPerform channel binding ritual.\n' + f'Enter the following incantation, then press enter.\n' + f'<b>{binding_incantation}</b>') + + def update_text(text): + self.entry.set_text(text) + self.entry.set_position(len(text)) + + clean = lambda s: re.sub('[^a-z0-9-]', '', s.lower()) + if clean(binding_incantation).startswith(clean(text)): + color = 0.9, 1.0, 0.9 # light red + else: + color = 1.0, 0.9, 0.9 # light green + self.entry.override_background_color(Gtk.StateType.NORMAL, Gdk.RGBA(*color, 1.0)) + + try: for user_input in self.noise.pairing_messages(): - print(f'User input: "{user_input}"') GLib.idle_add(update_text, user_input) + self.destroy() + except noise.ProtocolError as e: + GLib.idle_add(self.label.set_markup, f'<b>Error: {e}!</b>') + + +class StatusIcon(Gtk.StatusIcon): + def __init__(self): + Gtk.StatusIcon.__init__(self) + self.set_tooltip_text('SecureHID connected') + self.set_from_file('secureusb_icon.png') - GLib.idle_add(self.label.set_markup, f'<b>Done!</b>') - # FIXME demo - self.noise.uinput_passthrough() +def run_pairing_gui(serial, baudrate, debug=False): + ser = serial.Serial(serial, baudrate) + packetizer = hexnoise.Packetizer(serial, debug=debug) + noise = hexnoise.NoiseEngine(packetizer, debug=debug) + noise.perform_handshake() + + if not noise.paired: + window = PairingWindow(noise, debug=debug) + window.connect('destroy', Gtk.main_quit) + window.show_all() + Gtk.main() + + if self.noise.paired: + input_runner = threading.Thread(target=noise.uinput_passthrough, daemon=True) + input_runner.start() + + status_icon = StatusIcon() + Gtk.main() if __name__ == '__main__': import argparse @@ -84,10 +103,5 @@ if __name__ == '__main__': parser.add_argument('-d', '--debug', action='store_true') args = parser.parse_args() - ser = serial.Serial(args.serial, args.baudrate) - - window = PairingWindow(ser, debug=args.debug) - window.connect('destroy', Gtk.main_quit) - window.show_all() - Gtk.main() + run_pairing_gui(args.serial, args.baudrate, args.debug) |