|
| 1 | +# |
| 2 | +# Copyright (C) 2023 UAVCAN Development Team <dronecan.org> |
| 3 | +# |
| 4 | +# This software is distributed under the terms of the MIT License. |
| 5 | +# |
| 6 | +# Author: Andrew Tridgell |
| 7 | +# |
| 8 | + |
| 9 | +import dronecan |
| 10 | +from functools import partial |
| 11 | +from PyQt5.QtWidgets import QVBoxLayout, QWidget, QLabel, QDialog, \ |
| 12 | + QPlainTextEdit, QPushButton, QLineEdit, QFileDialog, QComboBox, QHBoxLayout |
| 13 | +from PyQt5.QtCore import QTimer, Qt |
| 14 | +from logging import getLogger |
| 15 | +from ..widgets import make_icon_button, get_icon, get_monospace_font, directory_selection |
| 16 | +import random |
| 17 | +import base64 |
| 18 | +import struct |
| 19 | + |
| 20 | +__all__ = 'PANEL_NAME', 'spawn', 'get_icon' |
| 21 | + |
| 22 | +PANEL_NAME = 'RemoteID Panel' |
| 23 | + |
| 24 | +logger = getLogger(__name__) |
| 25 | + |
| 26 | +_singleton = None |
| 27 | + |
| 28 | +SECURE_COMMAND_GET_REMOTEID_SESSION_KEY = dronecan.dronecan.remoteid.SecureCommand.Request().SECURE_COMMAND_GET_REMOTEID_SESSION_KEY |
| 29 | +SECURE_COMMAND_SET_REMOTEID_CONFIG = dronecan.dronecan.remoteid.SecureCommand.Request().SECURE_COMMAND_SET_REMOTEID_CONFIG |
| 30 | + |
| 31 | +class RemoteIDPanel(QDialog): |
| 32 | + DEFAULT_INTERVAL = 0.1 |
| 33 | + |
| 34 | + def __init__(self, parent, node): |
| 35 | + super(RemoteIDPanel, self).__init__(parent) |
| 36 | + self.setWindowTitle('RemoteID Management Panel') |
| 37 | + self.setAttribute(Qt.WA_DeleteOnClose) # This is required to stop background timers! |
| 38 | + |
| 39 | + self.timeout = 5 |
| 40 | + |
| 41 | + self._node = node |
| 42 | + self.session_key = None |
| 43 | + self.sequence = random.randint(0, 0xFFFFFFFF) |
| 44 | + |
| 45 | + layout = QVBoxLayout() |
| 46 | + |
| 47 | + self.key_selection = directory_selection.DirectorySelectionWidget(self, 'Secret key file') |
| 48 | + self.command = QLineEdit(self) |
| 49 | + self.send = QPushButton('Send', self) |
| 50 | + self.send.clicked.connect(self.on_send) |
| 51 | + |
| 52 | + self.node_select = QComboBox() |
| 53 | + |
| 54 | + self.state = QLineEdit() |
| 55 | + self.state.setText("") |
| 56 | + self.state.setReadOnly(True) |
| 57 | + |
| 58 | + layout.addLayout(self.labelWidget('Node', self.node_select)) |
| 59 | + layout.addWidget(self.key_selection) |
| 60 | + layout.addLayout(self.labelWidget('Command:', self.command)) |
| 61 | + layout.addLayout(self.labelWidget('Status:', self.state)) |
| 62 | + layout.addWidget(self.send) |
| 63 | + |
| 64 | + self.setLayout(layout) |
| 65 | + self.resize(400, 200) |
| 66 | + QTimer.singleShot(250, self.update_nodes) |
| 67 | + |
| 68 | + |
| 69 | + def labelWidget(self, label, widget): |
| 70 | + '''a widget with a label''' |
| 71 | + hlayout = QHBoxLayout() |
| 72 | + hlayout.addWidget(QLabel(label, self)) |
| 73 | + hlayout.addWidget(widget) |
| 74 | + return hlayout |
| 75 | + |
| 76 | + def on_send(self): |
| 77 | + '''callback for send button''' |
| 78 | + priv_key = self.key_selection.get_selection() |
| 79 | + if priv_key is None: |
| 80 | + self.status_update("Need to select private key") |
| 81 | + return |
| 82 | + self.status_update("Requesting session key") |
| 83 | + self.request_session_key() |
| 84 | + |
| 85 | + def status_update(self, text): |
| 86 | + '''update status line''' |
| 87 | + self.state.setText(text) |
| 88 | + |
| 89 | + def update_nodes(self): |
| 90 | + '''update list of available nodes''' |
| 91 | + QTimer.singleShot(250, self.update_nodes) |
| 92 | + from ..widgets.node_monitor import app_node_monitor |
| 93 | + if app_node_monitor is None: |
| 94 | + return |
| 95 | + node_list = [] |
| 96 | + for nid in app_node_monitor._registry.keys(): |
| 97 | + r = app_node_monitor._registry[nid] |
| 98 | + if r.info is not None: |
| 99 | + node_list.append("%u: %s" % (nid, r.info.name.decode())) |
| 100 | + else: |
| 101 | + node_list.append("%u" % nid) |
| 102 | + node_list = sorted(node_list) |
| 103 | + current_node = sorted([self.node_select.itemText(i) for i in range(self.node_select.count())]) |
| 104 | + for n in node_list: |
| 105 | + if not n in current_node: |
| 106 | + self.node_select.addItem(n) |
| 107 | + |
| 108 | + def get_session_key_response(self, reply): |
| 109 | + '''handle session key response''' |
| 110 | + if not reply: |
| 111 | + self.status_update("timed out") |
| 112 | + return |
| 113 | + self.session_key = bytearray(reply.response.data) |
| 114 | + self.status_update("Got session key") |
| 115 | + self.send_config_change() |
| 116 | + |
| 117 | + def get_private_key(self): |
| 118 | + '''get private key, return 32 byte key or None''' |
| 119 | + priv_key_file = self.key_selection.get_selection() |
| 120 | + if priv_key_file is None: |
| 121 | + self.status_update("Please select private key file") |
| 122 | + return None |
| 123 | + try: |
| 124 | + d = open(priv_key_file,'r').read() |
| 125 | + except Exception as ex: |
| 126 | + print(ex) |
| 127 | + return None |
| 128 | + ktype = "PRIVATE_KEYV1:" |
| 129 | + if not d.startswith(ktype): |
| 130 | + return None |
| 131 | + return base64.b64decode(d[len(ktype):]) |
| 132 | + |
| 133 | + def make_signature(self, seq, command, data): |
| 134 | + '''make a signature''' |
| 135 | + import monocypher |
| 136 | + private_key = self.get_private_key() |
| 137 | + d = struct.pack("<II", seq, command) |
| 138 | + d += data |
| 139 | + if command != SECURE_COMMAND_GET_REMOTEID_SESSION_KEY: |
| 140 | + if self.session_key is None: |
| 141 | + raise Exception("No session key") |
| 142 | + d += self.session_key |
| 143 | + return monocypher.signature_sign(private_key, d) |
| 144 | + |
| 145 | + def get_target_node(self): |
| 146 | + '''get the target node''' |
| 147 | + return int(self.node_select.currentText().split(':')[0]) |
| 148 | + |
| 149 | + def request_session_key(self): |
| 150 | + '''request a session key''' |
| 151 | + sig = self.make_signature(self.sequence, SECURE_COMMAND_GET_REMOTEID_SESSION_KEY, bytes()) |
| 152 | + self._node.request(dronecan.dronecan.remoteid.SecureCommand.Request( |
| 153 | + sequence=self.sequence, |
| 154 | + operation=SECURE_COMMAND_GET_REMOTEID_SESSION_KEY, |
| 155 | + sig_length=len(sig), |
| 156 | + data=sig, |
| 157 | + timeout=self.timeout), |
| 158 | + self.get_target_node(), |
| 159 | + self.get_session_key_response) |
| 160 | + self.sequence = (self.sequence+1) % (1<<32) |
| 161 | + print("Requested session key") |
| 162 | + |
| 163 | + def config_change_response(self, reply): |
| 164 | + if not reply: |
| 165 | + self.status_update("timed out") |
| 166 | + return |
| 167 | + result_map = { |
| 168 | + 0: "ACCEPTED", |
| 169 | + 1: "TEMPORARILY_REJECTED", |
| 170 | + 2: "DENIED", |
| 171 | + 3: "UNSUPPORTED", |
| 172 | + 4: "FAILED" } |
| 173 | + result = result_map.get(reply.response.result, "invalid") |
| 174 | + self.status_update("Got change response: %s" % result) |
| 175 | + |
| 176 | + def send_config_change(self): |
| 177 | + '''send remoteid config change''' |
| 178 | + req = self.command.text().encode('utf-8') |
| 179 | + sig = self.make_signature(self.sequence, SECURE_COMMAND_SET_REMOTEID_CONFIG, req) |
| 180 | + self._node.request(dronecan.dronecan.remoteid.SecureCommand.Request( |
| 181 | + sequence=self.sequence, |
| 182 | + operation=SECURE_COMMAND_SET_REMOTEID_CONFIG, |
| 183 | + sig_length=len(sig), |
| 184 | + data=req+sig, |
| 185 | + timeout=self.timeout), |
| 186 | + self.get_target_node(), |
| 187 | + self.config_change_response) |
| 188 | + self.sequence = (self.sequence+1) % (1<<32) |
| 189 | + self.status_update("Requested config change") |
| 190 | + |
| 191 | + def __del__(self): |
| 192 | + global _singleton |
| 193 | + _singleton = None |
| 194 | + |
| 195 | + def closeEvent(self, event): |
| 196 | + global _singleton |
| 197 | + _singleton = None |
| 198 | + super(RemoteIDPanel, self).closeEvent(event) |
| 199 | + |
| 200 | + |
| 201 | +def spawn(parent, node): |
| 202 | + global _singleton |
| 203 | + if _singleton is None: |
| 204 | + try: |
| 205 | + _singleton = RemoteIDPanel(parent, node) |
| 206 | + except Exception as ex: |
| 207 | + print(ex) |
| 208 | + |
| 209 | + _singleton.show() |
| 210 | + _singleton.raise_() |
| 211 | + _singleton.activateWindow() |
| 212 | + |
| 213 | + return _singleton |
| 214 | + |
| 215 | + |
| 216 | +get_icon = partial(get_icon, 'asterisk') |
0 commit comments