Tor fails to start if onion keys are zero length
I'm using a very unstable embedded device to run a transparent Tor router/relay/hidden service. In some cases - the device will reset the clock to 1970 (hooray, no RTC!) or the clock will be otherwise incorrect (hooray, low battery on the RTC!). Furthermore, the device will sometimes be unplugged at random (solar panel dies, janitor unplugs it, etc) and this results in a state/keys directory that looks like as follows:
Sep 10 13:31:48.000 [notice] Tor 0.2.4.23 (git-417f0cacd726e549) opening log file.
Sep 10 13:31:48.000 [notice] Not disabling debugger attaching for unprivileged users.
Sep 10 13:31:48.000 [notice] Parsing GEOIP IPv4 file /usr/share/tor/geoip.
Sep 10 13:31:48.000 [notice] Parsing GEOIP IPv6 file /usr/share/tor/geoip6.
Sep 10 13:31:49.000 [notice] Configured to measure statistics. Look for the *-stats files that will first be written to the data directory in 24 hours from now.
Sep 10 13:31:49.000 [warn] crypto error while Error parsing private key: no start line (in PEM routines:PEM_read_bio)
Sep 10 13:31:49.000 [err] Error loading private key.
Sep 10 13:31:49.000 [err] do_main_loop(): Bug: Error initializing keys; exiting
root@debian:/var/lib/tor/keys# ls -al
total 20
drwx--S--- 2 debian-tor debian-tor 4096 Aug 15 14:56 .
drwx--S--- 5 debian-tor debian-tor 4096 Sep 10 13:31 ..
-rw------- 1 debian-tor debian-tor 887 Sep 10 13:30 secret_id_key
-rw------- 1 debian-tor debian-tor 0 Sep 10 13:30 secret_onion_key
-rw------- 1 debian-tor debian-tor 891 Sep 10 13:30 secret_onion_key.old
-rw------- 1 debian-tor debian-tor 0 Sep 10 13:30 secret_onion_key_ntor
-rw------- 1 debian-tor debian-tor 96 Sep 10 13:30 secret_onion_key_ntor.old
If I remove the zero byte files and restart Tor, all is well:
root@debian:/var/lib/tor/keys# rm secret_onion_key*
/etc/init.d/tor restart
I think that Tor should notice that the files are zero bytes in length and gracefully generate the keys.