diff --git a/flaskserver.py b/flaskserver.py index e7a6d03..e39d961 100644 --- a/flaskserver.py +++ b/flaskserver.py @@ -20,9 +20,9 @@ def flask_server(): - if not os.path.exists(PINServerECDH.STATIC_SERVER_PRIVATE_KEY_FILE): - print(f'Key file not available, bailing out {PINServerECDH.STATIC_SERVER_PRIVATE_KEY_FILE}') - raise Exception + # Load, verify, and cache server static key at startup + # (Refuse to start if key non-existing or invalid) + PINServerECDH.load_private_key() sessions = {} app = Flask(__name__) diff --git a/server.py b/server.py index 625eea3..c992292 100644 --- a/server.py +++ b/server.py @@ -30,7 +30,7 @@ def generate_server_key_pair(cls): print(f'New public key written to file {cls.STATIC_SERVER_PUBLIC_KEY_FILE}') @classmethod - def _load_private_key(cls): + def load_private_key(cls): if not cls.STATIC_SERVER_PRIVATE_KEY: with open(cls.STATIC_SERVER_PRIVATE_KEY_FILE, 'rb') as f: cls.STATIC_SERVER_PRIVATE_KEY = f.read() @@ -38,7 +38,7 @@ def _load_private_key(cls): @classmethod def _sign_with_static_key(cls, msg): - cls._load_private_key() + assert cls.STATIC_SERVER_PRIVATE_KEY hashed = sha256(msg) return ec_sig_from_bytes(cls.STATIC_SERVER_PRIVATE_KEY, @@ -47,7 +47,8 @@ def _sign_with_static_key(cls, msg): @classmethod def _get_aes_pin_data_key(cls): - cls._load_private_key() + assert cls.STATIC_SERVER_PRIVATE_KEY + if not cls.STATIC_SERVER_AES_PIN_DATA: cls.STATIC_SERVER_AES_PIN_DATA = hmac_sha256(cls.STATIC_SERVER_PRIVATE_KEY, b'pin_data') return cls.STATIC_SERVER_AES_PIN_DATA diff --git a/test/test_ecdh.py b/test/test_ecdh.py index d4ac8d2..1c755cf 100644 --- a/test/test_ecdh.py +++ b/test/test_ecdh.py @@ -13,6 +13,8 @@ class ECDHTest(unittest.TestCase): @classmethod def setUpClass(cls): + PINServerECDH.load_private_key() + # The server public key the client would know with open(PINServerECDH.STATIC_SERVER_PUBLIC_KEY_FILE, 'rb') as f: cls.static_server_public_key = f.read()