From: Agnibho Mondal Date: Thu, 14 Sep 2023 18:01:29 +0000 (+0530) Subject: Disable signature if unavailable X-Git-Tag: v0.3~13^2~2 X-Git-Url: https://code.agnibho.com/repo?a=commitdiff_plain;h=7e536eadbb9e7746a288f85bd65243ffef5021d2;p=medscript.git Disable signature if unavailable --- diff --git a/config.py b/config.py index 1396588..8bfe45c 100644 --- a/config.py +++ b/config.py @@ -5,12 +5,19 @@ # MedScript is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. # You should have received a copy of the GNU General Public License along with MedScript. If not, see . -import argparse, json, os, sys, shutil +import argparse, json, os, sys, shutil, imp default_config_file=os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), "config", "config.json")) real_dir=os.path.dirname(os.path.realpath(sys.argv[0])) +try: + imp.find_module("M2Crypto") + sign_available=True +except Exception as e: + print(e) + sign_available=False + parser = argparse.ArgumentParser() parser.add_argument("filename", nargs="?") parser.add_argument("-c", "--config") diff --git a/setting.py b/setting.py index 152a850..02037ed 100644 --- a/setting.py +++ b/setting.py @@ -9,10 +9,12 @@ from PyQt6.QtWidgets import QWidget, QMainWindow, QFormLayout, QHBoxLayout, QPus from PyQt6.QtGui import QIcon from PyQt6.QtCore import pyqtSignal import os, json -from config import config, config_file +from config import config, config_file, sign_available class EditConfiguration(QMainWindow): + global sign_available + def select_directory(self): d=QFileDialog.getExistingDirectory(self, "Select Directory", config["data_directory"]) if(d): @@ -41,10 +43,11 @@ class EditConfiguration(QMainWindow): self.input_prescriber.setText(self.config["prescriber"]) self.input_newline.setChecked(bool(self.config["preset_newline"])) self.input_delimiter.setCurrentText(self.config["preset_delimiter"]) - self.input_smime.setChecked(bool(self.config["smime"])) - self.input_key.setText(self.config["private_key"]) - self.input_certificate.setText(self.config["certificate"]) - self.input_root.setText(self.config["root_bundle"]) + if sign_available: + self.input_smime.setChecked(bool(self.config["smime"])) + self.input_key.setText(self.config["private_key"]) + self.input_certificate.setText(self.config["certificate"]) + self.input_root.setText(self.config["root_bundle"]) except Exception as e: QMessageBox.critical(self,"Failed to load", "Failed to load the data into the application.") raise(e) @@ -56,10 +59,11 @@ class EditConfiguration(QMainWindow): self.config["prescriber"]=self.input_prescriber.text() self.config["preset_newline"]=self.input_newline.isChecked() self.config["preset_delimiter"]=self.input_delimiter.currentText() - self.config["smime"]=self.input_smime.isChecked() - self.config["private_key"]=self.input_key.text() - self.config["certificate"]=self.input_certificate.text() - self.config["root_bundle"]=self.input_root.text() + if sign_available: + self.config["smime"]=self.input_smime.isChecked() + self.config["private_key"]=self.input_key.text() + self.config["certificate"]=self.input_certificate.text() + self.config["root_bundle"]=self.input_root.text() with open(config_file, "w") as f: f.write(json.dumps(self.config, indent=4)) QMessageBox.information(self,"Saved", "Configuration saved. Please restart MedScript.") @@ -80,14 +84,14 @@ class EditConfiguration(QMainWindow): widget=QWidget(self) layout=QFormLayout(widget) self.input_directory=QLineEdit(self) - btn_directory=QPushButton("...", self) + btn_directory=QPushButton("Select Directory", self) btn_directory.clicked.connect(self.select_directory) layout_directory=QHBoxLayout() layout_directory.addWidget(self.input_directory) layout_directory.addWidget(btn_directory) layout.addRow("Data Directory", layout_directory) self.input_prescriber=QLineEdit(self) - btn_prescriber=QPushButton("...", self) + btn_prescriber=QPushButton("Select File", self) btn_prescriber.clicked.connect(self.select_prescriber) layout_prescriber=QHBoxLayout() layout_prescriber.addWidget(self.input_prescriber) @@ -98,29 +102,30 @@ class EditConfiguration(QMainWindow): self.input_delimiter=QComboBox(self) self.input_delimiter.addItems([",", ";"]) layout.addRow("Preset Delimiter", self.input_delimiter) - self.input_smime=QCheckBox("Enable digital signature (experimental)", self) - layout.addRow("S/MIME", self.input_smime) - self.input_key=QLineEdit(self) - btn_key=QPushButton("...", self) - btn_key.clicked.connect(self.select_key) - layout_key=QHBoxLayout() - layout_key.addWidget(self.input_key) - layout_key.addWidget(btn_key) - layout.addRow("Private Key", layout_key) - self.input_certificate=QLineEdit(self) - btn_certificate=QPushButton("...", self) - btn_certificate.clicked.connect(self.select_certificate) - layout_certificate=QHBoxLayout() - layout_certificate.addWidget(self.input_certificate) - layout_certificate.addWidget(btn_certificate) - layout.addRow("X509 Certificate", layout_certificate) - self.input_root=QLineEdit(self) - btn_root=QPushButton("...", self) - btn_root.clicked.connect(self.select_root) - layout_root=QHBoxLayout() - layout_root.addWidget(self.input_root) - layout_root.addWidget(btn_root) - layout.addRow("Root Bundle", layout_root) + if sign_available: + self.input_smime=QCheckBox("Enable digital signature (experimental)", self) + layout.addRow("S/MIME", self.input_smime) + self.input_key=QLineEdit(self) + btn_key=QPushButton("Select File", self) + btn_key.clicked.connect(self.select_key) + layout_key=QHBoxLayout() + layout_key.addWidget(self.input_key) + layout_key.addWidget(btn_key) + layout.addRow("Private Key", layout_key) + self.input_certificate=QLineEdit(self) + btn_certificate=QPushButton("Select File", self) + btn_certificate.clicked.connect(self.select_certificate) + layout_certificate=QHBoxLayout() + layout_certificate.addWidget(self.input_certificate) + layout_certificate.addWidget(btn_certificate) + layout.addRow("X509 Certificate", layout_certificate) + self.input_root=QLineEdit(self) + btn_root=QPushButton("Select File", self) + btn_root.clicked.connect(self.select_root) + layout_root=QHBoxLayout() + layout_root.addWidget(self.input_root) + layout_root.addWidget(btn_root) + layout.addRow("Root Bundle", layout_root) button_save=QPushButton("Save") button_save.clicked.connect(self.save) button_reset=QPushButton("Reset") diff --git a/signature.py b/signature.py index 0e35e28..9a06c81 100644 --- a/signature.py +++ b/signature.py @@ -5,10 +5,14 @@ # MedScript is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. # You should have received a copy of the GNU General Public License along with MedScript. If not, see . -from M2Crypto import BIO, Rand, SMIME, X509 -from config import config +from config import config, sign_available from hashlib import sha256 from datetime import datetime +try: + from M2Crypto import BIO, Rand, SMIME, X509 +except Exception as e: + print(e) + sign_available=False class Signature(): diff --git a/window.py b/window.py index b1843f6..9b758ed 100644 --- a/window.py +++ b/window.py @@ -11,10 +11,8 @@ from PyQt6.QtWidgets import QWidget, QMainWindow, QMessageBox, QLabel, QPushButt from PyQt6.QtGui import QAction, QIcon from pathlib import Path from hashlib import md5 -from M2Crypto.EVP import EVPError -from M2Crypto.BIO import BIOError -from M2Crypto.SMIME import SMIME_Error -from config import config + +from config import config, sign_available from prescription import Prescription from renderer import Renderer from filehandler import FileHandler @@ -22,6 +20,13 @@ from renderbox import RenderBox from setting import EditConfiguration, EditPrescriber from viewbox import ViewBox from preset import Preset +try: + from M2Crypto.EVP import EVPError + from M2Crypto.BIO import BIOError + from M2Crypto.SMIME import SMIME_Error +except Exception as e: + print(e) + sign_available=False class MainWindow(QMainWindow): @@ -110,26 +115,29 @@ class MainWindow(QMainWindow): password, ok=QInputDialog.getText(self, "Enter password", "Private key password", QLineEdit.EchoMode.Password) if(ok): try: - self.current_file.sign(password) - self.cmd_save() - except FileNotFoundError as e: - print(e) - QMessageBox.information(self, "Save first", "Please save the file before signing.") - except TypeError as e: - print(e) - QMessageBox.information(self, "Configure", "Please add valid key and certificate to the config file.") - except EVPError as e: - print(e) - QMessageBox.information(self, "Check password", "Failed to load key. Please check if password is correct.") - except BIOError as e: - print(e) - QMessageBox.information(self, "Not found", "Certifcate and/or key not found.") - except SMIME_Error as e: - print(e) - QMessageBox.information(self, "Failed to load", "Failed to sign. Please check if certificate and key match.") + try: + self.current_file.sign(password) + self.cmd_save() + except FileNotFoundError as e: + print(e) + QMessageBox.information(self, "Save first", "Please save the file before signing.") + except TypeError as e: + print(e) + QMessageBox.information(self, "Configure", "Please add valid key and certificate to the config file.") + except EVPError as e: + print(e) + QMessageBox.information(self, "Check password", "Failed to load key. Please check if password is correct.") + except BIOError as e: + print(e) + QMessageBox.information(self, "Not found", "Certifcate and/or key not found.") + except SMIME_Error as e: + print(e) + QMessageBox.information(self, "Failed to load", "Failed to sign. Please check if certificate and key match.") + except Exception as e: + print(e) + QMessageBox.information(self, "Failed", "Failed to sign.") except Exception as e: print(e) - QMessageBox.information(self, "Failed", "Failed to sign.") else: QMessageBox.information(self, "Save first", "Please save the file before signing.") @@ -253,7 +261,7 @@ class MainWindow(QMainWindow): def load_interface(self, file="", date=None, id="", name="", age="", sex="", address="", contact="", extra="", mode="", daw="", diagnosis="", note="", report="", advice="", investigation="", medication="", additional=""): try: file_msg=self.current_file.file if self.current_file.file else "New file" - sign_msg="(signed)" if config["smime"] and self.current_file.is_signed() else "" + sign_msg="(signed)" if sign_available and config["smime"] and self.current_file.is_signed() else "" self.statusbar.showMessage(file_msg+" "+sign_msg) if date is None: d=QDateTime.currentDateTime() @@ -386,6 +394,8 @@ class MainWindow(QMainWindow): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) + global sign_available + self.setWindowTitle("MedScript") self.setGeometry(100, 100, 600, 400) self.setWindowIcon(QIcon(os.path.join(config["resource"], "icon_medscript.ico"))) @@ -452,7 +462,7 @@ class MainWindow(QMainWindow): menu_prepare=menubar.addMenu("Prepare") menu_prepare.addAction(action_render) menu_prepare.addAction(action_refresh) - if(config["smime"]): + if(sign_available and config["smime"]): menu_prepare.addAction(action_sign) menu_prepare.addAction(action_unsign) menu_prepare.addAction(action_verify) @@ -484,7 +494,6 @@ class MainWindow(QMainWindow): toolbar.addWidget(self.label_prescriber) self.addToolBar(toolbar) - tab_info=QWidget(self) layout_info=QFormLayout(tab_info) layout_info2=QHBoxLayout()