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:
94:d7226b2c14b6
Parent:
77:4689596a2f3f
Child:
95:8c9dda8a0caf
--- a/clock/http-clock-script.js	Fri Apr 12 14:12:02 2019 +0000
+++ b/clock/http-clock-script.js	Tue Apr 16 19:18:27 2019 +0000
@@ -1,223 +1,182 @@
+//Clock script
 'use strict';
 
-var response        = '';
-var headers         = '';
-var msRtc           = 0;     //nibbles  0 to  3: 16 bits
-var msCountAtRtcSet = 0;
-var msDiff          = 0;
-var rtcIsSet        = false; //nibble   4      : bit 0
-var clockIsSet      = false; //nibble   4      : bit 1
-var sourceIsOk      = false; //nibble   4      : bit 2
-var rateIsLocked    = false; //nibble   4      : bit 3
-var timeIsLocked    = false; //nibble   5      : bit 0
-var leapEnable      = false; //nibble   5      : bit 1
-var leapForward     = false; //nibble   5      : bit 2
-var leapmonths1970  = 0;     //nibbles  6 to  8: 12 bits
-var leapmonth       = 0;
-var leapyear        = 0;
-var leaps           = 0;     //nibbles  9 to 12: 16 bits
-var ppb             = 0;     //nibbles 13 to 20: 32 bits
-var scanavg         = 0;     //nibbles 21 to 28: 32 bits
-var scanmax         = 0;     //nibbles 29 to 36: 32 bits
-var scanmin         = 0;     //nibbles 37 to 44: 32 bits
-var msCount         = 0;
+let pseudo          = new Utc();
+let rtc             = new Utc();
+
+let pseudoDisplay   = false;
+let pseudoStartMs   = 0;
+
+let diffMs          = 0;
+let rtcIsSet        = false;
+let clockIsSet      = false;
+let sourceIsOk      = false;
+let rateIsLocked    = false;
+let timeIsLocked    = false;
+let leapmonths1970  = 0;
 
-const         TICK_MS =   100;
-const AJAX_REFRESH_MS = 10000;
-const   AJAX_QUIET_MS =  3000;
+let ppb             = 0;
+let ppbdivisor      = 0;
+let ppbmaxchange    = 0;
+let syncedlimitppb  = 0;
+let syncedhysppb    = 0;
+let slewdivisor     = 0;
+let slewmax         = 0;
+let syncedlimitns   = 0;
+let syncedhysns     = 0;
+let maxoffsetsecs   = 0;
+let govTrace        = false;
 
-function hexToBit(iChar, iBit)
-{
-   var value = parseInt(response.charAt(iChar), 16);
-   value >>= iBit;
-   return value & 1;
-}
-function parseAjax()
+let ntpserver       = '';
+let ntpinitial      = 0;
+let ntpnormal       = 0;
+let ntpretry        = 0;
+let ntpoffset       = 0;
+let ntpmaxdelay     = 0;
+
+let scanavg         = 0;
+let scanmax         = 0;
+let scanmin         = 0;
+
+const DISPLAY_LEAP_MS = 10000;
+const          SERVER = '/clock-ajax';
+
+function parseDate()
 {
-   var iDateStart  = headers.toLowerCase().indexOf('date:');
-   var iDateEnd    = headers.indexOf('\r', iDateStart);
-   var rtcDate     = new Date(headers.slice(iDateStart + 5, iDateEnd));
-   msRtc           = parseInt(response.substr(0, 4), 16);
-   msRtc          += rtcDate.getTime();
-   msDiff          = msRtc - Date.now();
-   msCountAtRtcSet = msCount;
-   rtcIsSet        = hexToBit(4, 0);
-   clockIsSet      = hexToBit(4, 1);
-   sourceIsOk      = hexToBit(4, 2);
-   rateIsLocked    = hexToBit(4, 3);
-   timeIsLocked    = hexToBit(5, 0);
-   leapEnable      = hexToBit(5, 1);
-   leapForward     = hexToBit(5, 2);
-   leapmonths1970  = parseInt(response.substr(6, 3), 16);
-   leapmonth       =  leapmonths1970              % 12;
-   leapyear        = (leapmonths1970 - leapmonth) / 12;
-   leapmonth      += 1;
-   leapyear       += 1970;
-   leaps           = parseInt(response.substr( 9, 4), 16);
-   ppb             = parseInt(response.substr(13, 8), 16);
-   scanavg         = parseInt(response.substr(21, 8), 16);
-   scanmax         = parseInt(response.substr(29, 8), 16);
-   scanmin         = parseInt(response.substr(37, 8), 16);
-}
-function displayGeneral()
-{
-   var elem;
-   elem = document.getElementById('ajax-rtc-set'      ); if (elem) elem.setAttribute('dir', rtcIsSet     ? 'rtl' : 'ltr');
-   elem = document.getElementById('ajax-clock-set'    ); if (elem) elem.setAttribute('dir', clockIsSet   ? 'rtl' : 'ltr');
-   elem = document.getElementById('ajax-source-ok'    ); if (elem) elem.setAttribute('dir', sourceIsOk   ? 'rtl' : 'ltr');
-   elem = document.getElementById('ajax-rate-locked'  ); if (elem) elem.setAttribute('dir', rateIsLocked ? 'rtl' : 'ltr');
-   elem = document.getElementById('ajax-time-locked'  ); if (elem) elem.setAttribute('dir', timeIsLocked ? 'rtl' : 'ltr');
-
-   elem = document.getElementById('ajax-leap-enable'  ); if (elem) elem.setAttribute('dir', leapEnable   ? 'rtl' : 'ltr');
-   elem = document.getElementById('ajax-leap-forward' ); if (elem) elem.setAttribute('dir', leapForward  ? 'rtl' : 'ltr');
-
-   elem = document.getElementById('ajax-leap-year'    ); if (elem) elem.value = leapmonths1970 ? leapyear  : '';
-   elem = document.getElementById('ajax-leap-month'   ); if (elem) elem.value = leapmonths1970 ? leapmonth : '';
-
-   elem = document.getElementById('ajax-leap-count'   ); if (elem) elem.value = leaps;
-
-   elem = document.getElementById('ajax-ppb'          ); if (elem) elem.value = ppb;
-
-   elem = document.getElementById('ajax-scan-avg'     ); if (elem) elem.textContent = scanavg;
-   elem = document.getElementById('ajax-scan-max'     ); if (elem) elem.textContent = scanmax;
-   elem = document.getElementById('ajax-scan-min'     ); if (elem) elem.textContent = scanmin;
-
-   elem = document.getElementById('ajax-response'     ); if (elem) elem.textContent = response;
-   elem = document.getElementById('ajax-headers'      ); if (elem) elem.textContent = headers;
-   
-   elem = document.getElementById('ajax-date-diff'    ); if (elem) elem.textContent = msDiff;
+    let iDateStart  = BaseHeaders.toLowerCase().indexOf('date:');
+    let iDateEnd    = BaseHeaders.indexOf('\r', iDateStart);
+    let rtcDate     = new Date(BaseHeaders.slice(iDateStart + 5, iDateEnd));
+    rtc.ms          = rtcDate.getTime();
 }
 
-function formatNumbers00(i)
-{
-   if (i<10) i='0' + i;
-   return i;
-}
-function formatDayOfWeek(wday)
+function parseLinesTime(text)
 {
-    switch(wday)
-    {
-        case  0: return 'Sun';
-        case  1: return 'Mon';
-        case  2: return 'Tue';
-        case  3: return 'Wed';
-        case  4: return 'Thu';
-        case  5: return 'Fri';
-        case  6: return 'Sat';
-        default: return '---';
-    }
+    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.leaps       = parseInt(lines[3], 16);
 }
-function adjustLeap()
+function parseLinesGov(text)
 {
-    if (msRtc == 0) return; //Don't attempt to adjust an invalid time
-    
-    if (!leapEnable) return; // Adjustment disabled
-    
-    //Get the calander date and time from the ms
-    var now       = msCount - msCountAtRtcSet + msRtc;
-    var leapStart = Date.UTC(leapyear, leapmonth - 1, 1, 0, 0, leapForward ? 0: -1);
-    
-    if (now < leapStart) return; //Do nothing until reached the leap start
-    
-    if (leapForward) { msRtc -= 1000; leaps += 1; } //repeat 59
-    else             { msRtc += 1000; leaps -= 1; } //skip   59
-    
-    leapEnable = false;
+    let lines = text.split('\n');
+    ppb             = parseInt(lines[0], 16);
+    ppbdivisor      = parseInt(lines[1], 16);
+    ppbmaxchange    = parseInt(lines[2], 16);
+    syncedlimitppb  = parseInt(lines[3], 16);
+    syncedhysppb    = parseInt(lines[4], 16);
+    slewdivisor     = parseInt(lines[5], 16);
+    slewmax         = parseInt(lines[6], 16);
+    syncedlimitns   = parseInt(lines[7], 16);
+    syncedhysns     = parseInt(lines[8], 16);
+    maxoffsetsecs   = parseInt(lines[9], 16);
 }
-function displayTime()
+function parseLinesNtp(text)
+{
+    let lines = text.split('\n');
+    ntpserver       =          lines[0];
+    ntpinitial      = parseInt(lines[1], 16);
+    ntpnormal       = parseInt(lines[2], 16);
+    ntpretry        = parseInt(lines[3], 16);
+    ntpoffset       = parseInt(lines[4], 16);
+    ntpmaxdelay     = parseInt(lines[5], 16);
+}
+function parseLinesScan(text)
 {
-    if (msRtc == 0) return; //Don't attempt to display an invalid time
+    let lines = text.split('\n');
+    scanavg         = parseInt(lines[0], 16);
+    scanmax         = parseInt(lines[1], 16);
+    scanmin         = parseInt(lines[2], 16);
+}
+function DerivedParse()
+{
+    let topics = BaseResponse.split('\f');
+    parseDate();
+    parseLinesTime(topics[0]);
+    parseLinesGov (topics[1]);
+    parseLinesNtp (topics[2]);
+    parseLinesScan(topics[3]);
+}
+function DerivedDisplay()
+{
+    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');
     
-    //Get the calander date and time from the ms
-    var  now = new Date(msCount - msCountAtRtcSet + msRtc);
-    var    y = now.getUTCFullYear();
-    var    n = now.getUTCMonth   () + 1;
-    var    d = now.getUTCDate    ();
-    var    w = now.getUTCDay     (); // 0 == Sunday
-    var    h = now.getUTCHours   ();
-    var    m = now.getUTCMinutes ();
-    var    s = now.getUTCSeconds ();
-    
-    //Format time
-    n = formatNumbers00(n);
-    d = formatNumbers00(d);
-    h = formatNumbers00(h);
-    m = formatNumbers00(m);
-    s = formatNumbers00(s);
-    w = formatDayOfWeek(w);
+    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');
     
-    //Display time
-    var elem;
-        
-    elem = document.getElementById('ajax-date-utc');
-    if (elem) elem.textContent = y + '-' + n + '-' + d + ' ' + w + ' ' + h + ':' + m + ':' + s + ' TAI-UTC=' + leaps;
-
-    elem = document.getElementById('ajax-date-pc');
-    var options = 
-    {
-        year:         'numeric',
-        month:        'short',
-        day:          '2-digit',
-        weekday:      'short',
-        hour:         '2-digit',
-        minute:       '2-digit',
-        second:       '2-digit',
-        timeZoneName: 'short'
-    };
-    if (elem) elem.textContent = now.toLocaleString(undefined, options);
+    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 = 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 = 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 = 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 = getElementOrNull('ajax-date-diff'    ); if (elem) elem.textContent = diffMs;
 }
 
-var ajax;
-var msCountAtAjaxSend = 0;
-function AjaxRequest(request) //Used by this script and from HTML page
-{
-   ajax=new XMLHttpRequest();
-   ajax.onreadystatechange=handleAjaxResponse;
-   if (request) ajax.open('GET', '/clock-ajax' + '?' + request, true);
-   else         ajax.open('GET', '/clock-ajax'                , true);
-   ajax.send();
-}
-function requestAjax() //Used in this script
-{
-   
-   AjaxRequest('');
-   msCountAtAjaxSend = msCount;
-}
-
-function counter()
+function DerivedTick() //This typically called every 100ms
 {
-    msCount += TICK_MS; //Don't use Date.now() as we don't know when its clock will be updated around a leap second
-    adjustLeap();
-    displayTime();
-    if (msCount >= msCountAtRtcSet + AJAX_REFRESH_MS && //Wait until time to refresh
-        msCount >= msCountAtAjaxSend + AJAX_QUIET_MS) //Don't repeat during quiet period
+    if (pseudoDisplay)
     {
-        requestAjax(); //RequestAjax will set msRtc and reset msCount
+        pseudo.adjustLeap (BaseMs);
+        pseudo.displayTime(BaseMs);
+        if (BaseMs >= pseudoStartMs + DISPLAY_LEAP_MS + 500) pseudoDisplay = false;
     }
-}
-
-function handleAjaxResponse()
-{
-   if (ajax.readyState==4 && ajax.status==200)
-   {
-       response = ajax.responseText;
-       headers  = ajax.getAllResponseHeaders();
-       parseAjax();
-       displayGeneral();
-   }
+    else
+    {
+        rtc.adjustLeap (BaseMs);
+        rtc.displayTime(BaseMs);
+    }
 }
 
 function DisplayLeap() //Called by display leap button in HTML
 {
-   leapEnable      = true;
-   msRtc           = Date.UTC(leapyear, leapmonth - 1, 1) - AJAX_REFRESH_MS / 2; //displays the refresh period around the latest leap second
-   msCountAtRtcSet = msCount;
+   pseudoDisplay = true;
+   pseudoStartMs = BaseMs;
+   
+   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;
 }
-
-function init()
-{
-    setInterval(counter, TICK_MS);
-    requestAjax();
-}
-if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init ); // Loading hasn't finished yet
-else                                                                                 init(); //`DOMContentLoaded` has already fired