Files
ESPAsyncWebServer/src/WebAuthentication.cpp

248 lines
7.1 KiB
C++
Raw Normal View History

/*
Asynchronous WebServer library for Espressif MCUs
Copyright (c) 2016 Hristo Gochkov. All rights reserved.
This file is part of the esp8266 core for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "WebAuthentication.h"
#include <libb64/cencode.h>
2024-06-14 01:34:29 +05:30
#if defined(ESP32) || defined(TARGET_RP2040)
2024-06-27 20:34:10 +02:00
#include <MD5Builder.h>
2017-09-07 22:01:58 +03:00
#else
2024-06-27 20:34:10 +02:00
#include "md5.h"
2017-09-07 22:01:58 +03:00
#endif
#include "literals.h"
2017-09-07 22:01:58 +03:00
// Basic Auth hash = base64("username:password")
2024-06-27 20:34:10 +02:00
bool checkBasicAuthentication(const char* hash, const char* username, const char* password) {
if (username == NULL || password == NULL || hash == NULL)
return false;
2024-06-27 20:34:10 +02:00
size_t toencodeLen = strlen(username) + strlen(password) + 1;
size_t encodedLen = base64_encode_expected_len(toencodeLen);
2024-06-27 20:34:10 +02:00
if (strlen(hash) != encodedLen)
// Fix from https://github.com/me-no-dev/ESPAsyncWebServer/issues/667
#ifdef ARDUINO_ARCH_ESP32
2024-06-27 20:34:10 +02:00
if (strlen(hash) != encodedLen)
#else
if (strlen(hash) != encodedLen - 1)
#endif
2024-06-27 20:34:10 +02:00
return false;
2024-06-27 20:34:10 +02:00
char* toencode = new char[toencodeLen + 1];
if (toencode == NULL) {
return false;
}
2024-06-27 20:34:10 +02:00
char* encoded = new char[base64_encode_expected_len(toencodeLen) + 1];
if (encoded == NULL) {
delete[] toencode;
return false;
}
sprintf_P(toencode, PSTR("%s:%s"), username, password);
2024-06-27 20:34:10 +02:00
if (base64_encode_chars(toencode, toencodeLen, encoded) > 0 && memcmp(hash, encoded, encodedLen) == 0) {
delete[] toencode;
delete[] encoded;
return true;
}
delete[] toencode;
delete[] encoded;
return false;
}
2024-06-27 20:34:10 +02:00
static bool getMD5(uint8_t* data, uint16_t len, char* output) { // 33 bytes or more
2024-06-14 01:34:29 +05:30
#if defined(ESP32) || defined(TARGET_RP2040)
MD5Builder md5;
md5.begin();
md5.add(data, len);
md5.calculate();
md5.getChars(output);
2017-09-07 22:01:58 +03:00
#else
md5_context_t _ctx;
2024-06-27 20:34:10 +02:00
uint8_t* _buf = (uint8_t*)malloc(16);
if (_buf == NULL)
return false;
memset(_buf, 0x00, 16);
MD5Init(&_ctx);
MD5Update(&_ctx, data, len);
MD5Final(_buf, &_ctx);
2024-06-27 20:34:10 +02:00
for (uint8_t i = 0; i < 16; i++) {
sprintf_P(output + (i * 2), PSTR("%02x"), _buf[i]);
}
free(_buf);
#endif
return true;
}
2024-06-27 20:34:10 +02:00
static String genRandomMD5() {
2016-08-02 14:28:17 +03:00
#ifdef ESP8266
uint32_t r = RANDOM_REG32;
2016-08-02 14:28:17 +03:00
#else
uint32_t r = rand();
#endif
2024-06-27 20:34:10 +02:00
char* out = (char*)malloc(33);
if (out == NULL || !getMD5((uint8_t*)(&r), 4, out))
return emptyString;
String res = String(out);
free(out);
return res;
}
2024-06-27 20:34:10 +02:00
static String stringMD5(const String& in) {
char* out = (char*)malloc(33);
if (out == NULL || !getMD5((uint8_t*)(in.c_str()), in.length(), out))
return emptyString;
String res = String(out);
free(out);
return res;
}
2024-06-27 20:34:10 +02:00
String generateDigestHash(const char* username, const char* password, const char* realm) {
if (username == NULL || password == NULL || realm == NULL) {
return emptyString;
}
2024-06-27 20:34:10 +02:00
char* out = (char*)malloc(33);
String res = String(username);
res += ':';
res.concat(realm);
res += ':';
String in = res;
in.concat(password);
2024-06-27 20:34:10 +02:00
if (out == NULL || !getMD5((uint8_t*)(in.c_str()), in.length(), out))
return emptyString;
res.concat(out);
free(out);
return res;
}
2024-06-27 20:34:10 +02:00
String requestDigestAuthentication(const char* realm) {
String header(T_realm__);
2024-06-27 20:34:10 +02:00
if (realm == NULL)
header.concat(T_asyncesp);
else
header.concat(realm);
header.concat(T_auth_nonce);
header.concat(genRandomMD5());
header.concat(T__opaque);
header.concat(genRandomMD5());
header += (char)0x22; // '"'
return header;
}
#ifndef ESP8266
2024-06-27 20:34:10 +02:00
bool checkDigestAuthentication(const char* header, const char* method, const char* username, const char* password, const char* realm, bool passwordIsHash, const char* nonce, const char* opaque, const char* uri)
#else
2024-06-27 20:34:10 +02:00
bool checkDigestAuthentication(const char* header, const __FlashStringHelper* method, const char* username, const char* password, const char* realm, bool passwordIsHash, const char* nonce, const char* opaque, const char* uri)
#endif
{
2024-06-27 20:34:10 +02:00
if (username == NULL || password == NULL || header == NULL || method == NULL) {
// os_printf("AUTH FAIL: missing requred fields\n");
return false;
}
String myHeader(header);
int nextBreak = myHeader.indexOf(',');
2024-06-27 20:34:10 +02:00
if (nextBreak < 0) {
// os_printf("AUTH FAIL: no variables\n");
return false;
}
String myUsername;
String myRealm;
String myNonce;
String myUri;
String myResponse;
String myQop;
String myNc;
String myCnonce;
myHeader += (char)0x2c; // ','
myHeader += (char)0x20; // ' '
do {
String avLine(myHeader.substring(0, nextBreak));
2016-10-18 16:30:14 +03:00
avLine.trim();
2024-06-27 20:34:10 +02:00
myHeader = myHeader.substring(nextBreak + 1);
nextBreak = myHeader.indexOf(',');
int eqSign = avLine.indexOf('=');
2024-06-27 20:34:10 +02:00
if (eqSign < 0) {
// os_printf("AUTH FAIL: no = sign\n");
return false;
}
String varName(avLine.substring(0, eqSign));
avLine = avLine.substring(eqSign + 1);
2024-06-27 20:34:10 +02:00
if (avLine.startsWith(String('"'))) {
avLine = avLine.substring(1, avLine.length() - 1);
}
if (varName.equals(T_username)) {
2024-06-27 20:34:10 +02:00
if (!avLine.equals(username)) {
// os_printf("AUTH FAIL: username\n");
return false;
}
myUsername = avLine;
} else if (varName.equals(T_realm)) {
2024-06-27 20:34:10 +02:00
if (realm != NULL && !avLine.equals(realm)) {
// os_printf("AUTH FAIL: realm\n");
return false;
}
myRealm = avLine;
} else if (varName.equals(T_nonce)) {
2024-06-27 20:34:10 +02:00
if (nonce != NULL && !avLine.equals(nonce)) {
// os_printf("AUTH FAIL: nonce\n");
return false;
}
myNonce = avLine;
} else if (varName.equals(T_opaque)) {
2024-06-27 20:34:10 +02:00
if (opaque != NULL && !avLine.equals(opaque)) {
// os_printf("AUTH FAIL: opaque\n");
return false;
}
} else if (varName.equals(T_uri)) {
2024-06-27 20:34:10 +02:00
if (uri != NULL && !avLine.equals(uri)) {
// os_printf("AUTH FAIL: uri\n");
return false;
}
myUri = avLine;
} else if (varName.equals(T_response)) {
myResponse = avLine;
} else if (varName.equals(T_qop)) {
myQop = avLine;
} else if (varName.equals(T_nc)) {
myNc = avLine;
} else if (varName.equals(T_cnonce)) {
myCnonce = avLine;
}
2024-06-27 20:34:10 +02:00
} while (nextBreak > 0);
String ha1 = (passwordIsHash) ? String(password) : stringMD5(myUsername + ':' + myRealm + ':' + password);
String ha2 = String(method) + ':' + myUri;
String response = ha1 + ':' + myNonce + ':' + myNc + ':' + myCnonce + ':' + myQop + ':' + stringMD5(ha2);
2024-06-27 20:34:10 +02:00
if (myResponse.equals(stringMD5(response))) {
// os_printf("AUTH SUCCESS\n");
return true;
}
2024-06-27 20:34:10 +02:00
// os_printf("AUTH FAIL: password\n");
return false;
}