Common stuff for all my devices' web server pages: css, login, log, ipv4, ipv6, firmware update, clock, reset info etc.

Dependents:   oldheating gps motorhome heating

Security

A password has to be set whenever there has been a software reset. Resets following faults or power on do not require a new password as the hash is restored from the RTC GPREG register.

The password is not saved on the device; instead a 32 bit hash of the password is saved. It would take 2^31 attempts to brute force the password: this could be done in under a month if an attempt were possible every millisecond. To prevent this a 200 ms delay is introduced in the reply to the login form, that gives a more reasonable 13 years to brute force the password.

Once the password is accepted a random session id is created. This is 36 bit to give six base 64 characters but without an extra delay. If an attempt could be made every ms then this would still take over a year to brute force.

The most likely attack would to use a dictionary with, say, 10 million entries against the password which would still take 20 days to do.

Revision:
95:8c9dda8a0caf
Parent:
94:d7226b2c14b6
Child:
96:eb2eb75bad0f
--- a/clock/http-clock-script.js	Tue Apr 16 19:18:27 2019 +0000
+++ b/clock/http-clock-script.js	Thu Apr 18 15:26:29 2019 +0000
@@ -1,8 +1,8 @@
 //Clock script
 'use strict';
 
-let pseudo          = new Utc();
-let rtc             = new Utc();
+let pseudo          = new Clock();
+let rtc             = new Clock();
 
 let pseudoDisplay   = false;
 let pseudoStartMs   = 0;
@@ -13,7 +13,6 @@
 let sourceIsOk      = false;
 let rateIsLocked    = false;
 let timeIsLocked    = false;
-let leapmonths1970  = 0;
 
 let ppb             = 0;
 let ppbdivisor      = 0;
@@ -39,13 +38,12 @@
 let scanmin         = 0;
 
 const DISPLAY_LEAP_MS = 10000;
-const          SERVER = '/clock-ajax';
 
 function parseDate()
 {
-    let iDateStart  = BaseHeaders.toLowerCase().indexOf('date:');
-    let iDateEnd    = BaseHeaders.indexOf('\r', iDateStart);
-    let rtcDate     = new Date(BaseHeaders.slice(iDateStart + 5, iDateEnd));
+    let iDateStart  = Ajax.headers.toLowerCase().indexOf('date:');
+    let iDateEnd    = Ajax.headers.indexOf('\r', iDateStart);
+    let rtcDate     = new Date(Ajax.headers.slice(iDateStart + 5, iDateEnd));
     rtc.ms          = rtcDate.getTime();
 }
 
@@ -53,21 +51,17 @@
 {
     let lines = text.split('\n');
     rtc.ms         += parseInt(lines[0], 16);
-    rtc.ms         -= BaseMs;
-    diffMs          = rtc.ms + BaseMs - Date.now();
-    rtcIsSet        = hexToBit(lines[1], 0);
-    clockIsSet      = hexToBit(lines[1], 1);
-    sourceIsOk      = hexToBit(lines[1], 2);
-    rateIsLocked    = hexToBit(lines[1], 3);
-    timeIsLocked    = hexToBit(lines[1], 4);
-    rtc.leapEnable  = hexToBit(lines[1], 5);
-    rtc.leapForward = hexToBit(lines[1], 6);
-    govTrace        = hexToBit(lines[1], 7);
-    leapmonths1970  = parseInt(lines[2], 16);
-    rtc.leapMonth   =  leapmonths1970                  % 12;
-    rtc.leapYear    = (leapmonths1970 - rtc.leapMonth) / 12;
-    rtc.leapMonth  += 1;
-    rtc.leapYear   += 1970;
+    rtc.ms         -= Ajax.ms;
+    diffMs          = rtc.ms + Ajax.ms - Date.now();
+    rtcIsSet        = Ajax.hexToBit(lines[1], 0);
+    clockIsSet      = Ajax.hexToBit(lines[1], 1);
+    sourceIsOk      = Ajax.hexToBit(lines[1], 2);
+    rateIsLocked    = Ajax.hexToBit(lines[1], 3);
+    timeIsLocked    = Ajax.hexToBit(lines[1], 4);
+    rtc.leapEnable  = Ajax.hexToBit(lines[1], 5);
+    rtc.leapForward = Ajax.hexToBit(lines[1], 6);
+    govTrace        = Ajax.hexToBit(lines[1], 7);
+    rtc.months1970  = parseInt(lines[2], 16);
     rtc.leaps       = parseInt(lines[3], 16);
 }
 function parseLinesGov(text)
@@ -101,82 +95,86 @@
     scanmax         = parseInt(lines[1], 16);
     scanmin         = parseInt(lines[2], 16);
 }
-function DerivedParse()
+function parse()
 {
-    let topics = BaseResponse.split('\f');
+    let topics = Ajax.response.split('\f');
     parseDate();
     parseLinesTime(topics[0]);
     parseLinesGov (topics[1]);
     parseLinesNtp (topics[2]);
     parseLinesScan(topics[3]);
 }
-function DerivedDisplay()
+function display()
 {
     let elem;
-    elem = getElementOrNull('ajax-rtc-set'      ); if (elem) elem.setAttribute('dir', rtcIsSet     ? 'rtl' : 'ltr');
-    elem = getElementOrNull('ajax-clock-set'    ); if (elem) elem.setAttribute('dir', clockIsSet   ? 'rtl' : 'ltr');
-    elem = getElementOrNull('ajax-source-ok'    ); if (elem) elem.setAttribute('dir', sourceIsOk   ? 'rtl' : 'ltr');
-    elem = getElementOrNull('ajax-rate-locked'  ); if (elem) elem.setAttribute('dir', rateIsLocked ? 'rtl' : 'ltr');
-    elem = getElementOrNull('ajax-time-locked'  ); if (elem) elem.setAttribute('dir', timeIsLocked ? 'rtl' : 'ltr');
+    elem = Ajax.getElementOrNull('ajax-rtc-set'      ); if (elem) elem.setAttribute('dir', rtcIsSet     ? 'rtl' : 'ltr');
+    elem = Ajax.getElementOrNull('ajax-clock-set'    ); if (elem) elem.setAttribute('dir', clockIsSet   ? 'rtl' : 'ltr');
+    elem = Ajax.getElementOrNull('ajax-source-ok'    ); if (elem) elem.setAttribute('dir', sourceIsOk   ? 'rtl' : 'ltr');
+    elem = Ajax.getElementOrNull('ajax-rate-locked'  ); if (elem) elem.setAttribute('dir', rateIsLocked ? 'rtl' : 'ltr');
+    elem = Ajax.getElementOrNull('ajax-time-locked'  ); if (elem) elem.setAttribute('dir', timeIsLocked ? 'rtl' : 'ltr');
     
-    elem = getElementOrNull('ajax-leap-enable'  ); if (elem) elem.setAttribute('dir', rtc.leapEnable   ? 'rtl' : 'ltr');
-    elem = getElementOrNull('ajax-leap-forward' ); if (elem) elem.setAttribute('dir', rtc.leapForward  ? 'rtl' : 'ltr');
+    elem = Ajax.getElementOrNull('ajax-leap-enable'  ); if (elem) elem.setAttribute('dir', rtc.leapEnable   ? 'rtl' : 'ltr');
+    elem = Ajax.getElementOrNull('ajax-leap-forward' ); if (elem) elem.setAttribute('dir', rtc.leapForward  ? 'rtl' : 'ltr');
     
-    elem = getElementOrNull('ajax-leap-year'    ); if (elem) elem.value = leapmonths1970 ? rtc.leapYear  : '';
-    elem = getElementOrNull('ajax-leap-month'   ); if (elem) elem.value = leapmonths1970 ? rtc.leapMonth : '';
+    elem = Ajax.getElementOrNull('ajax-leap-year'    ); if (elem) elem.value = rtc.months1970 ? rtc.leapYear  : '';
+    elem = Ajax.getElementOrNull('ajax-leap-month'   ); if (elem) elem.value = rtc.months1970 ? rtc.leapMonth : '';
     
-    elem = getElementOrNull('ajax-leap-count'   ); if (elem) elem.value = rtc.leaps;
+    elem = Ajax.getElementOrNull('ajax-leap-count'   ); if (elem) elem.value = rtc.leaps;
     
-    elem = getElementOrNull('ajax-ppb'          ); if (elem) elem.value = ppb;
-    elem = getElementOrNull('ajax-ppb-divisor'  ); if (elem) elem.value = ppbdivisor;
-    elem = getElementOrNull('ajax-ppb-max-chg'  ); if (elem) elem.value = ppbmaxchange;
-    elem = getElementOrNull('ajax-ppb-syn-lim'  ); if (elem) elem.value = syncedlimitppb;
-    elem = getElementOrNull('ajax-ppb-syn-hys'  ); if (elem) elem.value = syncedhysppb;
-    elem = getElementOrNull('ajax-off-divisor'  ); if (elem) elem.value = slewdivisor;
-    elem = getElementOrNull('ajax-off-max'      ); if (elem) elem.value = slewmax;
-    elem = getElementOrNull('ajax-off-syn-lim'  ); if (elem) elem.value = syncedlimitns / 1000000;
-    elem = getElementOrNull('ajax-off-syn-hys'  ); if (elem) elem.value = syncedhysns   / 1000000;
-    elem = getElementOrNull('ajax-off-rst-lim'  ); if (elem) elem.value = maxoffsetsecs;
-    elem = getElementOrNull('ajax-gov-trace'    ); if (elem) elem.setAttribute('dir', govTrace     ? 'rtl' : 'ltr');
+    elem = Ajax.getElementOrNull('ajax-ppb'          ); if (elem) elem.value = ppb;
+    elem = Ajax.getElementOrNull('ajax-ppb-divisor'  ); if (elem) elem.value = ppbdivisor;
+    elem = Ajax.getElementOrNull('ajax-ppb-max-chg'  ); if (elem) elem.value = ppbmaxchange;
+    elem = Ajax.getElementOrNull('ajax-ppb-syn-lim'  ); if (elem) elem.value = syncedlimitppb;
+    elem = Ajax.getElementOrNull('ajax-ppb-syn-hys'  ); if (elem) elem.value = syncedhysppb;
+    elem = Ajax.getElementOrNull('ajax-off-divisor'  ); if (elem) elem.value = slewdivisor;
+    elem = Ajax.getElementOrNull('ajax-off-max'      ); if (elem) elem.value = slewmax;
+    elem = Ajax.getElementOrNull('ajax-off-syn-lim'  ); if (elem) elem.value = syncedlimitns / 1000000;
+    elem = Ajax.getElementOrNull('ajax-off-syn-hys'  ); if (elem) elem.value = syncedhysns   / 1000000;
+    elem = Ajax.getElementOrNull('ajax-off-rst-lim'  ); if (elem) elem.value = maxoffsetsecs;
+    elem = Ajax.getElementOrNull('ajax-gov-trace'    ); if (elem) elem.setAttribute('dir', govTrace     ? 'rtl' : 'ltr');
     
-    elem = getElementOrNull('ajax-ntp-server'   ); if (elem) elem.value = ntpserver;
-    elem = getElementOrNull('ajax-ntp-initial'  ); if (elem) elem.value = ntpinitial;
-    elem = getElementOrNull('ajax-ntp-normal'   ); if (elem) elem.value = ntpnormal / 60;
-    elem = getElementOrNull('ajax-ntp-retry'    ); if (elem) elem.value = ntpretry;
-    elem = getElementOrNull('ajax-ntp-offset'   ); if (elem) elem.value = ntpoffset;
-    elem = getElementOrNull('ajax-ntp-max-delay'); if (elem) elem.value = ntpmaxdelay;
+    elem = Ajax.getElementOrNull('ajax-ntp-server'   ); if (elem) elem.value = ntpserver;
+    elem = Ajax.getElementOrNull('ajax-ntp-initial'  ); if (elem) elem.value = ntpinitial;
+    elem = Ajax.getElementOrNull('ajax-ntp-normal'   ); if (elem) elem.value = ntpnormal / 60;
+    elem = Ajax.getElementOrNull('ajax-ntp-retry'    ); if (elem) elem.value = ntpretry;
+    elem = Ajax.getElementOrNull('ajax-ntp-offset'   ); if (elem) elem.value = ntpoffset;
+    elem = Ajax.getElementOrNull('ajax-ntp-max-delay'); if (elem) elem.value = ntpmaxdelay;
     
-    elem = getElementOrNull('ajax-scan-avg'     ); if (elem) elem.textContent = scanavg;
-    elem = getElementOrNull('ajax-scan-max'     ); if (elem) elem.textContent = scanmax;
-    elem = getElementOrNull('ajax-scan-min'     ); if (elem) elem.textContent = scanmin;
+    elem = Ajax.getElementOrNull('ajax-scan-avg'     ); if (elem) elem.textContent = scanavg;
+    elem = Ajax.getElementOrNull('ajax-scan-max'     ); if (elem) elem.textContent = scanmax;
+    elem = Ajax.getElementOrNull('ajax-scan-min'     ); if (elem) elem.textContent = scanmin;
     
-    elem = getElementOrNull('ajax-date-diff'    ); if (elem) elem.textContent = diffMs;
+    elem = Ajax.getElementOrNull('ajax-date-diff'    ); if (elem) elem.textContent = diffMs;
 }
 
-function DerivedTick() //This typically called every 100ms
+function handleTick() //This typically called every 100ms
 {
     if (pseudoDisplay)
     {
-        pseudo.adjustLeap (BaseMs);
-        pseudo.displayTime(BaseMs);
-        if (BaseMs >= pseudoStartMs + DISPLAY_LEAP_MS + 500) pseudoDisplay = false;
+        pseudo.adjustLeap (Ajax.ms);
+        pseudo.displayTime(Ajax.ms);
+        if (Ajax.ms >= pseudoStartMs + DISPLAY_LEAP_MS + 500) pseudoDisplay = false;
     }
     else
     {
-        rtc.adjustLeap (BaseMs);
-        rtc.displayTime(BaseMs);
+        rtc.adjustLeap (Ajax.ms);
+        rtc.displayTime(Ajax.ms);
     }
 }
 
-function DisplayLeap() //Called by display leap button in HTML
+function displayLeap() //Called by display leap button in HTML
 {
    pseudoDisplay = true;
-   pseudoStartMs = BaseMs;
+   pseudoStartMs = Ajax.ms;
    
    pseudo.leapEnable  = true;
    pseudo.leapForward = rtc.leapForward;
    pseudo.leaps       = rtc.leaps;
    pseudo.leapMonth   = rtc.leapMonth;
    pseudo.leapYear    = rtc.leapYear;
-   pseudo.ms          = Date.UTC(rtc.leapYear, rtc.leapMonth - 1, 1) - DISPLAY_LEAP_MS / 2 - BaseMs;
+   pseudo.ms          = Date.UTC(rtc.leapYear, rtc.leapMonth - 1, 1) - DISPLAY_LEAP_MS / 2 - Ajax.ms;
 }
+Ajax.server     = '/clock-ajax';
+Ajax.onResponse = function() { parse(); display(); };
+Ajax.onTick     = handleTick;
+Ajax.init();
\ No newline at end of file