From c9738d58d79f8865caa0eb11e1fd2e030d7e1dfe Mon Sep 17 00:00:00 2001 From: Francesco Manghi Date: Fri, 30 May 2025 11:39:52 +0200 Subject: [PATCH] Added HTTPS --- background_installer.sh | 21 ++++++++++++++++++--- webserver/key_create.py | 40 ++++++++++++++++++++++++++++++++++++++++ webserver/webserver.py | 3 ++- 3 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 webserver/key_create.py diff --git a/background_installer.sh b/background_installer.sh index dae1784..7f10310 100755 --- a/background_installer.sh +++ b/background_installer.sh @@ -92,13 +92,25 @@ function install_py_deps { python3 -m venv "$VENV_DIR" "$VENV_DIR/bin/python3" -m pip install --upgrade pip if [ "$1" == "neuron" ]; then - "$VENV_DIR/bin/python3" -m pip install flask==2.2.5 werkzeug==2.2.2 flask-login==0.6.2 pyserial pymodbus==2.5.3 + "$VENV_DIR/bin/python3" -m pip install flask==2.2.5 werkzeug==3.0.1 flask-login==0.6.3 flask-wtf==1.2.1 pyserial pymodbus==2.5.3 pycryptodome pyopenssl else - "$VENV_DIR/bin/python3" -m pip install flask==2.3.3 werkzeug==2.3.7 flask-login==0.6.2 pyserial pymodbus==2.5.3 + "$VENV_DIR/bin/python3" -m pip install flask==2.3.3 werkzeug==3.0.1 flask-login==0.6.3 flask-wtf==1.2.1 pyserial pymodbus==2.5.3 pycryptodome pyopenssl fi python3 -m pip install pymodbus==2.5.3 + + echo "[CREATING ENCRYPTION KEY]" + cd webserver + "$VENV_DIR/bin/python3" ./key_create.py + if [ $? -ne 0 ]; then + echo "Error creating encryption key" + echo "OpenPLC was NOT installed!" + exit 1 + fi + cd ../ } + + function swap_on { echo "creating swapfile..." @@ -239,6 +251,9 @@ function install_all_libs { } function finalize_install { + echo "[CREATE SSL KEY WEBSITE]" + cd "$OPENPLC_DIR/webserver/" + openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -sha256 -days 3650 -nodes -subj "/C=XX/ST=StateName/L=CityName/O=CompanyName/OU=CompanySectionName/CN=CommonNameOrHostname" echo "[FINALIZING]" cd "$OPENPLC_DIR/webserver/scripts" if [ "$1" == "win" ]; then @@ -306,7 +321,7 @@ elif [ "$1" == "win_msys2" ]; then #Setting up venv python3 -m venv "$VENV_DIR" "$VENV_DIR/bin/python3" get-pip3.py - "$VENV_DIR/bin/python3" -m pip install flask==2.3.3 werkzeug==2.3.7 flask-login==0.6.2 pyserial pymodbus==2.5.3 + "$VENV_DIR/bin/python3" -m pip install flask==2.3.3 werkzeug==3.0.1 flask-login==0.6.3 pyserial pymodbus==2.5.3 echo "" echo "[MATIEC COMPILER]" diff --git a/webserver/key_create.py b/webserver/key_create.py new file mode 100644 index 0000000..f899022 --- /dev/null +++ b/webserver/key_create.py @@ -0,0 +1,40 @@ +import os + +KEYSIZE = 16 +BLOCKSIZE = 16 + +def createKey(keysize): + key = os.urandom(keysize) + return key + +def createIV(blocksize): + iv = os.urandom(blocksize) + return iv + +def ivcheck(): + ivexists = os.path.exists('./iv.bin') + return ivexists + +def keycheck(): + keyexists = os.path.exists('./key.bin') + return keyexists + +def main(): + check1 = keycheck() + check2 = ivcheck() + if check1 == False or check2 == False: + key = createKey(KEYSIZE) + iv = createIV(BLOCKSIZE) + + with open("key.bin", 'wb') as keyfile: + keyfile.write(key) + keyfile.close() + + with open('iv.bin', 'wb') as ivfile: + ivfile.write(iv) + ivfile.close() + else: + pass + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/webserver/webserver.py b/webserver/webserver.py index b3defec..ada48a0 100644 --- a/webserver/webserver.py +++ b/webserver/webserver.py @@ -12,6 +12,7 @@ import openplc import monitoring as monitor import sys import ctypes +import key_create import socket import mimetypes @@ -2527,7 +2528,7 @@ if __name__ == '__main__': configure_runtime() monitor.parse_st(openplc_runtime.project_file) - app.run(debug=False, host='0.0.0.0', threaded=True, port=8080) + app.run(sl_context=("cert.pem", "key.pem"), debug=False, host='0.0.0.0', threaded=True, port=8080) except Error as e: print("error connecting to the database" + str(e))