2016-06-29 16:20:03 +03:00
|
|
|
/*
|
|
|
|
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
|
2024-07-02 00:52:01 +09:00
|
|
|
#include "literals.h"
|
2017-09-07 22:01:58 +03:00
|
|
|
|
2016-06-29 16:20:03 +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)
|
2016-06-29 16:20:03 +03:00
|
|
|
return false;
|
|
|
|
|
2024-06-27 20:34:10 +02:00
|
|
|
size_t toencodeLen = strlen(username) + strlen(password) + 1;
|
2016-06-29 16:20:03 +03:00
|
|
|
size_t encodedLen = base64_encode_expected_len(toencodeLen);
|
2024-06-27 20:34:10 +02:00
|
|
|
if (strlen(hash) != encodedLen)
|
2020-05-04 13:04:18 -04:00
|
|
|
// 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)
|
2020-05-04 13:04:18 -04:00
|
|
|
#else
|
|
|
|
if (strlen(hash) != encodedLen - 1)
|
|
|
|
#endif
|
2024-06-27 20:34:10 +02:00
|
|
|
return false;
|
2016-06-29 16:20:03 +03:00
|
|
|
|
2024-06-27 20:34:10 +02:00
|
|
|
char* toencode = new char[toencodeLen + 1];
|
|
|
|
if (toencode == NULL) {
|
2016-06-29 16:20:03 +03:00
|
|
|
return false;
|
|
|
|
}
|
2024-06-27 20:34:10 +02:00
|
|
|
char* encoded = new char[base64_encode_expected_len(toencodeLen) + 1];
|
|
|
|
if (encoded == NULL) {
|
2016-06-29 16:20:03 +03:00
|
|
|
delete[] toencode;
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-28 16:36:40 -04:00
|
|
|
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) {
|
2016-06-29 16:20:03 +03:00
|
|
|
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)
|
2024-06-05 22:30:12 +02:00
|
|
|
MD5Builder md5;
|
|
|
|
md5.begin();
|
|
|
|
md5.add(data, len);
|
|
|
|
md5.calculate();
|
|
|
|
md5.getChars(output);
|
2017-09-07 22:01:58 +03:00
|
|
|
#else
|
2024-06-05 22:30:12 +02:00
|
|
|
md5_context_t _ctx;
|
2024-06-27 20:34:10 +02:00
|
|
|
|
|
|
|
uint8_t* _buf = (uint8_t*)malloc(16);
|
|
|
|
if (_buf == NULL)
|
2016-06-29 16:20:03 +03:00
|
|
|
return false;
|
|
|
|
memset(_buf, 0x00, 16);
|
2024-06-05 22:30:12 +02:00
|
|
|
|
2016-06-29 16:20:03 +03:00
|
|
|
MD5Init(&_ctx);
|
|
|
|
MD5Update(&_ctx, data, len);
|
|
|
|
MD5Final(_buf, &_ctx);
|
2024-06-05 22:30:12 +02:00
|
|
|
|
2024-06-27 20:34:10 +02:00
|
|
|
for (uint8_t i = 0; i < 16; i++) {
|
2020-04-28 16:36:40 -04:00
|
|
|
sprintf_P(output + (i * 2), PSTR("%02x"), _buf[i]);
|
2016-06-29 16:20:03 +03:00
|
|
|
}
|
2024-06-05 22:30:12 +02:00
|
|
|
|
2016-06-29 16:20:03 +03:00
|
|
|
free(_buf);
|
2024-06-05 22:30:12 +02:00
|
|
|
#endif
|
2016-06-29 16:20:03 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-06-27 20:34:10 +02:00
|
|
|
static String genRandomMD5() {
|
2016-08-02 14:28:17 +03:00
|
|
|
#ifdef ESP8266
|
2016-06-29 16:20:03 +03:00
|
|
|
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))
|
2020-04-28 16:36:40 -04:00
|
|
|
return emptyString;
|
2016-06-29 16:20:03 +03:00
|
|
|
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))
|
2020-04-28 16:36:40 -04:00
|
|
|
return emptyString;
|
2016-06-29 16:20:03 +03:00
|
|
|
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) {
|
2020-04-28 16:36:40 -04:00
|
|
|
return emptyString;
|
2016-06-29 16:20:03 +03:00
|
|
|
}
|
2024-06-27 20:34:10 +02:00
|
|
|
char* out = (char*)malloc(33);
|
2016-06-29 16:20:03 +03:00
|
|
|
String res = String(username);
|
2020-04-28 16:36:40 -04:00
|
|
|
res += ':';
|
2016-06-29 16:20:03 +03:00
|
|
|
res.concat(realm);
|
2020-04-28 16:36:40 -04:00
|
|
|
res += ':';
|
2016-06-29 16:20:03 +03:00
|
|
|
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))
|
2020-04-28 16:36:40 -04:00
|
|
|
return emptyString;
|
2016-06-29 16:20:03 +03:00
|
|
|
res.concat(out);
|
|
|
|
free(out);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2024-06-27 20:34:10 +02:00
|
|
|
String requestDigestAuthentication(const char* realm) {
|
2024-07-02 00:52:01 +09:00
|
|
|
String header(T_realm__);
|
2024-06-27 20:34:10 +02:00
|
|
|
if (realm == NULL)
|
2024-07-02 00:52:01 +09:00
|
|
|
header.concat(T_asyncesp);
|
2016-06-29 16:20:03 +03:00
|
|
|
else
|
|
|
|
header.concat(realm);
|
2024-07-02 00:52:01 +09:00
|
|
|
header.concat(T_auth_nonce);
|
2016-06-29 16:20:03 +03:00
|
|
|
header.concat(genRandomMD5());
|
2024-07-02 00:52:01 +09:00
|
|
|
header.concat(T__opaque);
|
2016-06-29 16:20:03 +03:00
|
|
|
header.concat(genRandomMD5());
|
2024-07-02 00:52:01 +09:00
|
|
|
header += (char)0x22; // '"'
|
2016-06-29 16:20:03 +03:00
|
|
|
return header;
|
|
|
|
}
|
|
|
|
|
2024-06-28 00:45:05 +09:00
|
|
|
#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)
|
2024-06-28 00:45:05 +09:00
|
|
|
#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)
|
2024-06-28 00:45:05 +09:00
|
|
|
#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");
|
2016-06-29 16:20:03 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-06-28 00:45:05 +09:00
|
|
|
String myHeader(header);
|
2020-04-28 16:36:40 -04:00
|
|
|
int nextBreak = myHeader.indexOf(',');
|
2024-06-27 20:34:10 +02:00
|
|
|
if (nextBreak < 0) {
|
|
|
|
// os_printf("AUTH FAIL: no variables\n");
|
2016-06-29 16:20:03 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-06-28 00:45:05 +09:00
|
|
|
String myUsername;
|
|
|
|
String myRealm;
|
|
|
|
String myNonce;
|
|
|
|
String myUri;
|
|
|
|
String myResponse;
|
|
|
|
String myQop;
|
|
|
|
String myNc;
|
|
|
|
String myCnonce;
|
2016-06-29 16:20:03 +03:00
|
|
|
|
2024-07-02 00:52:01 +09:00
|
|
|
myHeader += (char)0x2c; // ','
|
|
|
|
myHeader += (char)0x20; // ' '
|
2016-06-29 16:20:03 +03:00
|
|
|
do {
|
2024-06-28 00:45:05 +09:00
|
|
|
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);
|
2020-04-28 16:36:40 -04:00
|
|
|
nextBreak = myHeader.indexOf(',');
|
2016-06-29 16:20:03 +03:00
|
|
|
|
2020-04-28 16:36:40 -04:00
|
|
|
int eqSign = avLine.indexOf('=');
|
2024-06-27 20:34:10 +02:00
|
|
|
if (eqSign < 0) {
|
|
|
|
// os_printf("AUTH FAIL: no = sign\n");
|
2016-06-29 16:20:03 +03:00
|
|
|
return false;
|
|
|
|
}
|
2024-06-28 00:45:05 +09:00
|
|
|
String varName(avLine.substring(0, eqSign));
|
2016-06-29 16:20:03 +03:00
|
|
|
avLine = avLine.substring(eqSign + 1);
|
2024-06-27 20:34:10 +02:00
|
|
|
if (avLine.startsWith(String('"'))) {
|
2016-06-29 16:20:03 +03:00
|
|
|
avLine = avLine.substring(1, avLine.length() - 1);
|
|
|
|
}
|
|
|
|
|
2024-07-02 00:52:01 +09:00
|
|
|
if (varName.equals(T_username)) {
|
2024-06-27 20:34:10 +02:00
|
|
|
if (!avLine.equals(username)) {
|
|
|
|
// os_printf("AUTH FAIL: username\n");
|
2016-06-29 16:20:03 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
myUsername = avLine;
|
2024-07-02 00:52:01 +09:00
|
|
|
} 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");
|
2016-06-29 16:20:03 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
myRealm = avLine;
|
2024-07-02 00:52:01 +09:00
|
|
|
} 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");
|
2016-06-29 16:20:03 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
myNonce = avLine;
|
2024-07-02 00:52:01 +09:00
|
|
|
} 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");
|
2016-06-29 16:20:03 +03:00
|
|
|
return false;
|
|
|
|
}
|
2024-07-02 00:52:01 +09:00
|
|
|
} 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");
|
2016-06-29 16:20:03 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
myUri = avLine;
|
2024-07-02 00:52:01 +09:00
|
|
|
} else if (varName.equals(T_response)) {
|
2016-06-29 16:20:03 +03:00
|
|
|
myResponse = avLine;
|
2024-07-02 00:52:01 +09:00
|
|
|
} else if (varName.equals(T_qop)) {
|
2016-06-29 16:20:03 +03:00
|
|
|
myQop = avLine;
|
2024-07-02 00:52:01 +09:00
|
|
|
} else if (varName.equals(T_nc)) {
|
2016-06-29 16:20:03 +03:00
|
|
|
myNc = avLine;
|
2024-07-02 00:52:01 +09:00
|
|
|
} else if (varName.equals(T_cnonce)) {
|
2016-06-29 16:20:03 +03:00
|
|
|
myCnonce = avLine;
|
|
|
|
}
|
2024-06-27 20:34:10 +02:00
|
|
|
} while (nextBreak > 0);
|
2016-06-29 16:20:03 +03:00
|
|
|
|
2024-06-28 00:45:05 +09:00
|
|
|
String ha1 = (passwordIsHash) ? String(password) : stringMD5(myUsername + ':' + myRealm + ':' + password);
|
2020-04-28 16:36:40 -04:00
|
|
|
String ha2 = String(method) + ':' + myUri;
|
|
|
|
String response = ha1 + ':' + myNonce + ':' + myNc + ':' + myCnonce + ':' + myQop + ':' + stringMD5(ha2);
|
2016-06-29 16:20:03 +03:00
|
|
|
|
2024-06-27 20:34:10 +02:00
|
|
|
if (myResponse.equals(stringMD5(response))) {
|
|
|
|
// os_printf("AUTH SUCCESS\n");
|
2016-06-29 16:20:03 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-06-27 20:34:10 +02:00
|
|
|
// os_printf("AUTH FAIL: password\n");
|
2016-06-29 16:20:03 +03:00
|
|
|
return false;
|
|
|
|
}
|