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.

http/httpquery.c

Committer:
andrewboyson
Date:
2019-09-24
Revision:
130:9a5b8fe308f1
Child:
141:1dac268a197d

File content as of revision 130:9a5b8fe308f1:

#include <stdio.h>
#include <stdlib.h>

static int hexToInt(char c)
{
    int nibble;
    if (c >= '0' && c <= '9') nibble = c - '0';
    if (c >= 'A' && c <= 'F') nibble = c - 'A' + 0xA;
    if (c >= 'a' && c <= 'f') nibble = c - 'a' + 0xA;
    return nibble;
}
void HttpQueryUnencode(char* pValue)
{
    char* pDst = pValue;
    int a;
    for (char* pSrc = pValue; *pSrc; pSrc++)
    {
        char c = *pSrc;
        switch (c)
        {
            case '+':
                c = ' ';
                break;
            case '%':
                c = *++pSrc;
                if (c == 0) break;
                a = hexToInt(c);
                a <<= 4;
                c = *++pSrc;
                if (c == 0) break;
                a += hexToInt(c);
                c = a;
                break;
            default:
                c = *pSrc;
                break;
        }
        *pDst++ = c;
    }
    *pDst = 0;
}
char* HttpQuerySplit(char* p, char** ppName, char** ppValue) //returns the start of the next name value pair
{    
    *ppName    = p;                     //Record the start of the name
    *ppValue   = NULL;

    while (*p != '=')                   //Loop to an '='
    {
        if (*p == 0)    return 0;
        p++;
    }
    *p = 0;                             //Terminate the name by replacing the '=' with a NUL char
    p++;                                //Move on to the start of the value
    *ppValue = p;                       //Record the start of the value
    while (*p != '&')                   //Loop to a '&'
    {
        if (*p == 0)    return 0;
        p++;
    }
    *p = 0;                            //Terminate the value by replacing the '&' with a NULL
    return p + 1;
}
int HttpQueryValueAsInt(char* pValue)
{
    return (int)strtol(pValue, NULL, 10);
}
char* HttpCookiesSplit(char* p, char** ppName, char** ppValue) //returns the start of the next name value pair
{    
    *ppValue   = NULL;
    *ppName    = NULL;

    *ppName    = p;                     //Record the start of the name
    while (*p != '=')                   //Loop to an '='
    {
        if (*p == 0) return 0;
        p++;
    }
    *p = 0;                             //Terminate the name by replacing the '=' with a NUL char
    p++;                                //Move on to the start of the value
    *ppValue = p;                       //Record the start of the value
    while (*p != ';')                   //Loop to a ';'
    {
        if (*p == 0) return 0;
        p++;
    }
    *p = 0;                            //Terminate the value by replacing the ';' with a NULL
    p++;
    if (*p == 0)     return 0;
    while (*p == ' ') p++;             //Move past any spaces after the ';'
    return p;
}