diff --git a/conn.py b/conn.py index 80fbb20..50615da 100644 --- a/conn.py +++ b/conn.py @@ -149,6 +149,12 @@ class Connection(metaclass=Singleton): logging.info(f"Starting hidden service: %s" % BP['onion_addr']) asyncio.create_task(TOR.start_tunnel()) + h = STATUS.hsm + if ('summary' in h) and h.summary and not BP.get('priv_over_ux') and not BP.get('summary'): + logging.info("Captured CC's summary of the policy") + BP['summary'] = h.summary + BP.save() + STATUS.reset_pending_auth() STATUS.notify_watchers() @@ -195,11 +201,6 @@ class Connection(metaclass=Singleton): # won't be required STATUS.local_code = None - if ('summary' in h) and h.summary and not BP.get('priv_over_ux') and not BP.get('summary'): - logging.info("Captured CC's summary of the policy") - BP['summary'] = h.summary - BP.save() - # has it just transitioned into HSM mode? if STATUS.connected and STATUS.hsm.active and not b4: await self.activated_hsm() diff --git a/data/README.md b/data/README.md index 1f034b0..eb047e1 100644 --- a/data/README.md +++ b/data/README.md @@ -2,7 +2,8 @@ # Data Files - this directory will hold data files for the Bunker -- they are encrypted with a private key held in the "storage locker" of the Coldcard -- contents include Tor hidden service private key and settings +- they are encrypted with a private key held in the "storage locker" of a Coldcard +- contents include Tor hidden service private key and settings for Bunker - filename based on key -- default key maps to `bp-1850f665aa1e22c0.dat` +- you may see unused junk accumulate in this directory; those are random keys that + never got saved as a policy file for any Coldcard diff --git a/docs/setup.md b/docs/setup.md index 870c5f0..cbbff93 100644 --- a/docs/setup.md +++ b/docs/setup.md @@ -115,12 +115,9 @@ it reads the storage locker and uses the NaCl private key (32 bytes) to select and open the corresponding Bunker settings file. Therefore, each Coldcard has it's own settings for the Bunker. -Before a Coldcard is connected, or before the policy is saved for -the first time, the settings are held with a fixed key, and end up -in file: `data/bp-1850f665aa1e22c0.dat` based on that key. At the -point where you upload a new policy to a Coldcard, a new key is -picked and added to the policy file. Data on disk at that point is -re-encrypted and moved over. +In setup mode, bunker settings are effectively not saved until +the NaCL secret is saved into the policy of a Coldcard and saved +there. #### Other Notes diff --git a/persist.py b/persist.py index 97e2575..21402c1 100644 --- a/persist.py +++ b/persist.py @@ -39,10 +39,6 @@ class Settings(metaclass=Singleton): # path to data files DATA_FILES = './data' - # used during secret/when we don't have a key yet - # - maps to data/bp-1850f665aa1e22c0.dat - PLACEHOLDER_KEY = b'ab'*16 - # endpoint to use for sending txn; we assume it's Explora protocol (Blockstream.info) EXPLORA = 'http://explorerzydxu5ecjrkwceayqybizmpjjznk5izmitf2modhcusuqlid.onion' @@ -111,12 +107,13 @@ class BunkerPersistance(WatchableMixin, dict, metaclass=Singleton): def __init__(self): super(BunkerPersistance, self).__init__() - self.key = None self.filename = None - self.set_defaults() + self.reset() def reset(self): - self.open(settings.PLACEHOLDER_KEY) + self.clear() + self.set_secret(os.urandom(32)) + self.set_defaults() def set_defaults(self): # defaults here @@ -135,11 +132,6 @@ class BunkerPersistance(WatchableMixin, dict, metaclass=Singleton): bn = 'bp-%s.dat' % sha256(sha256(b'salty' + self.key).digest()).hexdigest()[-16:].lower() self.filename = os.path.join(settings.DATA_FILES, bn) - @staticmethod - def new_secret(): - # rotate key - return os.urandom(32) - def open(self, key): # Given a private key (via storage locker) open a Nacl secret box # and use that for the data. @@ -182,8 +174,5 @@ class BunkerPersistance(WatchableMixin, dict, metaclass=Singleton): except: pass - def is_default_secret(self): - return self.key == settings.PLACEHOLDER_KEY - # EOF diff --git a/policy.py b/policy.py index f4317ec..a251798 100644 --- a/policy.py +++ b/policy.py @@ -129,13 +129,8 @@ def update_sl(proposed): xk = None if not xk: - if not BP.key or BP.is_default_secret(): - # pick a new key - logging.info("Making new secret for holding Bunker settings") - xk = BP.new_secret() - else: - # keep using same key - xk = BP.key + # capture settings key + xk = BP.key assert len(xk) == 32 proposed['set_sl'] = b64encode(b'Bunk' + xk).decode('ascii') diff --git a/webapp.py b/webapp.py index a07d73e..238e684 100755 --- a/webapp.py +++ b/webapp.py @@ -480,9 +480,10 @@ async def ws_api_handler(ses, send_json, req, orig_request): # handle_api if not BP['tor_enabled']: await TOR.stop_tunnel() - elif BP.get('onion_pk') and not (STATUS.force_local_mode or STATUS.setup_mode): - # connect/reconnect - await TOR.start_tunnel() + elif BP.get('onion_pk') and not (STATUS.force_local_mode or STATUS.setup_mode) \ + and TOR.get_current_addr() != BP.get('onion_addr'): + # disconnect/reconnect + await TOR.start_tunnel() elif action == 'sign_message': # sign a short text message